<?php
// +----------------------------------------------------------------------+
// | PHP version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2005 Snapshot Inc.                                |
// +----------------------------------------------------------------------+
// | License                                                              |
// | GNU General Public License                                           |
// +----------------------------------------------------------------------+
// | Authors: Mai Kawasaki                                                |
// +----------------------------------------------------------------------+
//
// $Id: InputAction.class.php,v 1.1 2005/06/20 09:31:27 snapshot Exp $
//
// Description:
// 2005/05/18
//


/** 
*	InputAction.class.php
*	Mai Kawasaki
*	2005/05/18
*/
require_once(OPT_DIR.'u-learning/config.php');
require_once(OPT_DIR.'u-learning/action/SecureStudentAction.class.php');
require_once(OPT_DIR.'u-learning/data/Admin.class.php');
require_once(OPT_DIR.'u-learning/data/Student.class.php');
require_once(OPT_DIR.'u-learning/data/SubjectStudent.class.php');

/** 
*	InputAction
*/
class InputAction extends SecureStudentAction {
    function execute (&$controller, &$request, &$user) {

		// ブラウザの判別
		$type = $this->getUserAgentType();
		$request->setAttribute('type', $type);
		
		$DB = $request->getAttribute('DB');
		$uid = $user->getAttribute('uid');
		$aid = $user->getAttribute('aid');

		// 教員情報取得
		$request->setAttribute('admin', new Admin($aid, $DB));
		
		// 科目を取得
    	$admin = new Admin($user->getAttribute('aid'), $DB);
    	$subjects = $admin->getUsableSubjects($DB);
		// 学生情報取得
		$student = new Student($aid, $uid, $DB);
    	$subjects_user = $student->getSubjectStudent($DB);
		
		if($request->getParameter('edit') ) {
			// ユーザ情報変更処理
			$pass = $request->getParameter('pass');
			$pass_new = $request->getParameter('pass_new');
			$pass_new2 = $request->getParameter('pass_new2');
			$mail = $request->getParameter('mail');
			$mail_old = $request->getParameter('mail_old');
			$name = $request->getParameter('name');
			$number = $request->getParameter('number');
			$year = $request->getParameter('year');
			
			$subjects_user_new = $request->getParameter('subject');

			// 入力チェック
			$error = "";
			$pass_info = false;
			// パスワード
			if(trim($pass) != '' || trim($pass_new) != '' || trim($pass_new2) != '') {
				if($student->pass != $pass) {
					$error .= "現在のパスワードが違います。\n";
			    	$request->setAttribute('student', $student);
				}
				if($pass_new != $pass_new2) {
					$error .= "確認用のパスワードが異なっています。\n";
			    	$request->setAttribute('student', $student);
				}
				if ( !(ereg("[0-9]+", $pass_new)) || strlen($pass_new) > 6 || strlen($pass_new) < 4){
					$error .= "パスワードは4桁から6桁までの数字にしてください。 \n";
				}
				// エラーがなかったらパスワードを変更
				if($error == ""){
					$student->pass = $pass_new;
					$pass_info = true;
				}
			}
			if( trim($mail) == '' || trim($name) =='' || trim($number) == '' || trim($year) == ''){	
				$error .= "個人情報は全て入力してください。 \n";
			}			
			if (!ereg(".+@.+\..+", $mail) ){
				$error .= "メールアドレスが不正です。 \n";			
			}
	
			$student->mail = $mail;
			$student->name = $name;
			$student->number = $number;
			$student->year = $year;
			// エラーがなかったら更新
			if($error == ""){
				$student->update($DB);
				// 科目更新
				
				$subject_user = new SubjectStudent();
				$subject_user->aid = $aid;
				$subject_user->uid = $uid;
				$subject_user->deleteSubject($DB);
				
				foreach ($subjects_user_new as $sid) {
					$subject_user->sid = $sid;
					$subject_user->insert($DB);
												
				}				
				
				// メールアドレス変更時はメールする
				if($mail != $mail_old){

					mb_internal_encoding('SJIS');
					
					$from = $aid;        //from
					$subject = "[Uラーニング]アドレス変更完了";  //サブジェクト
					$message = "※このメールアドレスは送信用です。\n返信はできません。\n\n";
					$message .= "メールアドレスを変更しました。\n\n";

					// ヘッダ
					$head  = "From: $from".MAIL_DOMAIN."\n";		//From
					$head .= "X-Mailer: PHP/".phpversion()."\n";
					$head .= "MIME-version: 1.0\n";	//MIMEヘッダ
					$head .= "Content-Type: text/plain; charset=iso-2022-jp\n";
					$head .= "Content-Transfer-Encoding: 7bit\n";
					
					// 本文
					$body = $name."さん\n\n".$message;
					
					mb_send_mail($mail, $subject, $body, $head);				
				
				}
				
				$request->setAttribute('student', $student);
				$request->setAttribute('mail_old', $mail_old);
				$request->setAttribute('pass_info', $pass_info);
				return VIEW_SUCCESS;

			}
			
			$request->setAttribute('error', $error);
			$request->setAttribute('student', $student);
			$request->setAttribute('mail_old', $mail_old);
			$request->setAttribute('pass', $pass);
			$request->setAttribute('pass_new', $pass_new);
			$request->setAttribute('pass_new2', $pass_new2);
			$request->setAttribute('subjects', $subjects);
			$request->setAttribute('subjects_user', $subjects_user_new);

			return VIEW_ERROR;

		}
		if($request->getParameter('back') ) {
			$this->location('Top', 'Index');
			return VIEW_NONE;
		}	
		$request->setAttribute('student', $student);
		$request->setAttribute('subjects', $subjects);
		$request->setAttribute('subjects_user', $subjects_user);
		return VIEW_INPUT;
    }
	
	
}

?>