<?php

class InvigilatorController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='common2_1';
	public function init()
	{

	}
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			//'accessControl', // perform access control for CRUD operations
			//'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Invigilator;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Invigilator']))
		{
			$model->attributes=$_POST['Invigilator'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Invigilator']))
		{
			$model->attributes=$_POST['Invigilator'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Invigilator');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Invigilator('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Invigilator']))
			$model->attributes=$_GET['Invigilator'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Invigilator::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='invigilator-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	public function actionUpdatetime()
	{
		$session=new CHttpSession;
		$session->open();
		$exam_id=$id=$session['exam_id'];
		$user_id=Yii::app()->user->id;
		$model_jk=Invigilator::model()->find('student_id=:user_id and exam_id=:exam_id',array(':user_id'=>$user_id,':exam_id'=>$exam_id));
		$jiankao['remaintime']=$_POST['newtime'];
		$model_jk->attributes=$jiankao;
		$model_jk->save(false);
	}
	public function actionList($classroom_id){
		$data=Exammanage::model()->findAll();
		$this->render('admin',array(
			'classroom_id'=>$classroom_id,
			'data'=>$data,
		));
	}
	//取所有数据
	public function actionQq($classroom_id){
		$order=Yii::app()->request->getPost('order');
		$sort=Yii::app()->request->getPost('sort');
		$page=Yii::app()->request->getPost('page');
		$rows=Yii::app()->request->getPost('rows');
		
		$fullname=Yii::app()->request->getPost('fullname');//查询学生姓名
		$exam_id=Yii::app()->request->getPost('exam_id');//考试id
		$condition_1="t.classroom_id='$classroom_id' and t.exam_id='$exam_id'";
		$condition_2="t.classroom_id='$classroom_id' and t.exam_id='$exam_id' and fullname='$fullname'";
		$condition=(empty($fullname))?$condition_1:$condition_2;
		
		$model=new Invigilator;
		$criteria=new CDbCriteria(array(
			'order'=>"t.$sort $order",
			//'select'=>array('id','title'),//'id,title',
			//'select'=>'knowledgepoint.title,t.id,t.title',
			'condition'=>$condition,
			'limit'=>$rows,
			'offset'=>($page-1)*$rows,
			'with'=>array('student_fullname','exammanage_examname'),
		));

		$count = $model->count($criteria);
		//exit;
		$data3=$model->findAll($criteria);
		$json = new CArJSON();
		// Define which relations do you want to load
		$relations = array('student_fullname','exammanage_examname');
		// Define which attributes, foreach relations do you want to load
		// root denote attributes for the main model
		$attributes = array(
			'root' => array('id','starttime','submittime','remaintime','submitmark','cheatmark'),//表示主表中的字段
			'student_fullname' => array( 'fullname' ),//表示从表中的字段
			'exammanage_examname' => array( 'title' ),//表示从表中的字段
			//'votes' => null, // This will load all attributes
		);
		$data=$json->toJSON($data3, $relations, $attributes);
		//print_r(CJSON::decode($data));
		$i=0;$newarr=array();
		foreach(CJSON::decode($data) as $key=>$value)
		{
			//echo $key;echo $value['attributes']['id'];
			$newarr[$i]['id']=$value['attributes']['id'];
			//$newarr[$i]['title']=$value['attributes']['title'];
			$newarr[$i]['starttime']=$value['attributes']['starttime'];
			$newarr[$i]['submittime']=$value['attributes']['submittime'];
			$newarr[$i]['remaintime']=$value['attributes']['remaintime'];
			$newarr[$i]['submitmark']=($value['attributes']['submitmark'])?'Yes':'No';
			$newarr[$i]['cheatmark']=($value['attributes']['cheatmark'])?'Yes':'No';
			$newarr[$i]['fullname']=$value['relations']['student_fullname']['fullname'];
			$newarr[$i]['title']=$value['relations']['exammanage_examname']['title'];$i++;
		}

			$data2=CJSON::encode($newarr);
			$data='{'.'"total":'.$count.','.'"rows":'.$data2.'}';
			echo $data;	
	}
	public function actionReplaceSubmit()
	{
		$id=Yii::app()->request->getPost('id');
		$model=$this->loadModel($id);
		$model->attributes=array('submitmark'=>true);
		if($model->update()){
			echo "yes";
		}			
	}
	public function actionSetCheat()
	{
		$id=Yii::app()->request->getPost('id');
		$model=$this->loadModel($id);
		$model->attributes=array('cheatmark'=>true);
		if($model->update()){
			echo "yes";
		}		
	}	
	//>---------------------单行删除
	public function actionDeleterow(){
		$id=Yii::app()->request->getPost('id');
		if($this->loadModel($id)->delete()){
			echo "yes";
		}
	}
	//>---------------多行删除
	public function actionDeleteall(){
		$arr=(array)(CJSON::decode(Yii::app()->request->getPost('qq')));
		foreach($arr as $value){
			$newarr[]=(array)$value;
		}
		$str='';
		//print_r($newarr);
		foreach($newarr as $value){
			$fullname=trim($value['fullname']);
			$id=trim($value['id']);
				if($this->loadModel($id)->delete()){
					$str.='删除考生《'.$fullname.'》成功！<br/>';
				}else
				{
					$str.='删除考生《'.$fullname.'》失败！<br/>';
				}            
		}
		echo $str;
	}
}
