<?php

/**
 *      【橙创家校通v4.0】 (C)2010-2012 Tuntron Inc.
 *      
 *    	 http://www.tuntron.com   QQ：1121251729
 *
 *      &考试管理模块&
 */
 
 if (!defined('IS_RUN')) die('Forbidden');
 
 $M_user = G_class('user');
 $M_exam=G_class('exam');
 $M_subject = G_class('subject_item');
 $M_homework=G_class('homework');
 $c= _get('c','index');
 
 $T=$c=='index'?$do:$do.'_'.$c;
 
 //左侧菜单开始 --后面考虑转到模块入口文件
  $left_menu_html = G_class('menu') ->get_left_menu_html($arrgroup['menuid'],$do,$c);
 
 $Modname = '考试&作业 - 学校管理';
 
switch ($c){
		
		case "index":
		
		$Modname = '考试列表 - '.$Modname;
		
			$all_bid = $M_user->B->FindAllBid($_CO['userbid']);
			$arr_all_bid=explode("/",$all_bid);
			$arrexam = $M_exam->read_all($arr_all_bid);
			foreach ($arrexam as $key=>$item){
				$arrexam[$key]['Branch']=$M_user->B->findbranch($item['bid']);
				$arrexam[$key]['examtype']=$M_exam->EY->read_one($item['examtypeid']);
			}
			$arrexamtype = $M_exam->EY->read_all($arr_all_bid);
			//分页每页显示数据量
			$listnum = '10';
			//当前页
			$pid=$_GET['p'];
			if(is_numeric($pid)&&$pid>0){
				$pid=(int)$pid;
			}else{
				$pid='1';
			}
			//链接地址
			$url = SITEURL . '?mod=school&do=exam&c=index';
			$sum = count($arrexam);
			$pidf = $pid-1;
			$start = $pidf*$listnum;
			$end = $listnum;
			foreach($arrexam as $key=>$value){
				$array[] = $value;
			}
			$arrexam = array();
			$arrexam = array_slice($array, $start, $end);
			$pageinfo = grtpageno($url, $sum, $listnum, $pid);
			//获取参数验证代码
			$arrexam=fields_code($arrexam,'examid');
		
		break;
		
		
		case "examonline":
		
		$Modname = '在线考试 - '.$Modname;
		
		break;
		
		
		case "result":
		
		check_access('show_result');
		$Modname = '成绩查询 - '.$Modname;
		
		$examid = _get('examid');
		
		$self_userinfo =  G_class('user')->read($_CO['uid']);
		$strexamid=$M_exam->read_one($examid);
		
		$examtype = $M_exam->EY->read_one($strexamid['examtypeid']);
		
		$M_subject_item=G_class('subject_item');
		
		$subject = $examtype['dztype']==0?$M_subject_item->read_one($examtype['subject']):'';

		$arrMembers = $M_user->arrmember('6',$strexamid['bid'],$c);//6为学生用户组ID
		
		foreach($arrMembers[1] as $key=>$item){
			
			$result =G_class('result')->read_one($item['uid'],$examid,$subject['sid']);//综合需要再处理
			
			$arrMembers[1][$key]['mark'] =$result['mark']?$result['mark']:0;
			$arrMembers[1][$key]['is_sms'] =$result['is_sms'];
			$arrMembers[1][$key]['rid'] =$result['rid'];
			
			}
		
			//分页每页显示数据量
			$listnum = '20';
			//当前页
			$pid=$_GET['p'];
			if(is_numeric($pid)&&$pid>0){
				$pid=(int)$pid;
			}else{
				$pid='1';
			}
			//链接地址
			$url = SITEURL . '?mod=school&do=exam&c=result';
			$sum = $arrMembers[0];
			$pidf = $pid-1;
			$start = $pidf*$listnum;
			$end = $listnum;
			foreach($arrMembers[1] as $key=>$value){
				$array[] = $value;
			}
			$arrMembers[1] = array();
			$arrMembers[1] = array_slice($array, $start, $end);
			$pageinfo = grtpageno($url, $sum, $listnum, $pid);
		
		
		break;
		
		// 导出考试成绩到excel表
		case 'export_excel':
		
			check_access('down_result');
			$examid = $_GET['exam_id'];
			$strexamid=$M_exam->read_one($examid);
			$examtype = $M_exam->EY->read_one($strexamid['examtypeid']);
			$M_subject_item=G_class('subject_item');
			$subject = $examtype['dztype']==0?$M_subject_item->read_one($examtype['subject']):'';
			$arrMembers = $M_user->arrmember('6',$strexamid['bid'],$c);//6为学生用户组ID
			$array = array();
			$i=0;
			foreach($arrMembers[1] as $key=>$item){
				$mark =G_class('result')->get_mark($item['uid'],$examid,$subject['sid']);//综合需要再处理
				$arrMembers[1][$key]['mark'] =$mark?$mark:0;
				$array[$i]['num'] =$item['stu_num'];
				$array[$i]['name'] = $item['realname'];
				$array[$i]['bname'] = $item['branch']['bname'];
				$array[$i]['subject'] = $subject['subjectname'];
				$array[$i]['mark'] = $mark?$mark:0;
				$i++;
			}
			$title = array('0'=>'学号','1'=>'姓名','2'=>'所在部门','3'=>'考试科目','4'=>'成绩');
			$filename = $strexamid['examname'].'_'.$subject['subjectname'].'成绩表.xls';
			$filename = iconv('UTF-8', 'gb2312', $filename);
			//$M_sysnote = G_class('sysnote');
			//$M_sysnote->insert_sysnote('2', '0', $_CO['uid'], $_CO['userbid'], '导出成绩表 '.$filename);
			export_excel($title, $array, $filename);
			exit;
			break;
		
		case "eidt_result":
		
		//编辑翻页的时候可以根据是否有成绩排序
		check_access('add_result');
		$Modname = '录入成绩 - '.$Modname;
		
		$examid = _get('examid');
		
		$strexamid=$M_exam->read_one($examid);
		
		$examtype = $M_exam->EY->read_one($strexamid['examtypeid']);
		
		$M_subject_item=G_class('subject_item');
		
		$subject = $examtype['dztype']==0?$M_subject_item->read_one($examtype['subject']):'';

		$arrMembers = $M_user->arrmember('6',$strexamid['bid'],$c);//6为学生用户组ID
		
		foreach($arrMembers[1] as $key=>$item){
			
			$mark =G_class('result')->get_mark($item['uid'],$examid,$subject['sid']);//综合需要再处理

			$arrMembers[1][$key]['mark'] =$mark?$mark:0;
			
			}
			
		if($_POST){
			
		$M_result=G_class('result');
		$backurl = $_POST['backurl'];
		$p_uid= $_POST['uid'];
		$p_sid = $_POST['sid'];
		$p_mark = $_POST['mark'];
		foreach($p_uid as $key=>$item){
		$post['uid']    = trim($item);
		$post['sid']    = trim($p_sid[$key]);
		$post['mark']   = trim($p_mark[$key]);
		$post['examid'] = $examid;
		$havemark =$M_result->read_one($post['uid'],$examid,$post['sid']);//综合需要再处理
		if($post['uid']) $M_result->save($post,$havemark?$havemark['rid']:NULL);
		}
		
		Note('成绩录入成功！','>>点击返回<<',$backurl);
			
		}
		$no_temp=1;
		require_once Template('eidt_result');
		
		break;
		
		//批量导入成绩 Edit by zhaobin
		case "import_result_show":
			
			check_access('add_result');
			$bid = $_CO['userbid'];
			$examid = $_POST['examid'];
			if($_FILES != array()){
				$file = $_FILES['file'];
				if(is_uploaded_file($file['tmp_name'])){
					$fileinfo = pathinfo($file['name']);
					$ext = $fileinfo['extension'];
					//最大文件大小为10M
					$maxsize = 10 * 1024 * 1024;
					$typearr = array(
						'xls'
					);
					$filesize = $file['size'];
					$filetype = $file['type'];
					$result = checkFile($filesize,$maxsize,$ext,$typearr);
					if($result != ''){
						Note($result);
						exit;
					}else{
						$directory = TUNTRON_PATH . "/dat/upload/excelfile/";
						$newfilename = time() . '.' . $ext;
						$filepath = $directory.$newfilename;
						if(file_upload($file['tmp_name'], $filepath)){
							if(file_exists($filepath)){
								//读取EXCEL数据方法
								$arr = readExcel($filepath);
								$arr = array_slice($arr, 1);
								$i=0;
								$e=0;
								$dataArr = array();
								$cache = array();
								foreach($arr as $key=>$value){
									$str = substr($value, 0, strlen($value)-1);
									$values = explode(',', $str);
									$j=0;
									$error = 0;
									foreach($values as $key1=>$value1){
										switch($j){
											//检测帐号是否存在
											case '0':
												if(empty($value1)){
													$error = '学生姓名不能为空';
												}
												break;
											case '1':
												if(empty($value1)){
													$error = '学生性别不能为空';
												}
												break;
											case '2':
												if(empty($value1)){
													$error = '学生学号不能为空';
												}
												break;
											case '3':
												if(empty($value1)){
													$error = '学生帐号不能为空';
												}else{
													$uid = $M_user->check_isuser($value1);
													if($uid == NULL){
														$error = "学生帐号 '" .$value1."' 不存在";
													}
												}
												break;
											case '4':
												if(empty($value1)){
													$error = '考试科目不能为空';
												}else{
													$subject = $M_subject->get_sid_by_name($value1);
													if($subject == NULL){
														$error = '考试科目 "' . $value1 . '" 不存在';
													}
												}
												break;
											case '5':
												if(empty($value1)){
													$error = '考试成绩不能为空';
												}
												break;
											default:
												break;
										}
										//判断是否存在重复值
										if($j=='5'){
											$compare = $uid[0]['uid'] . $examid . $subject[0]['sid'];
											$result = $M_exam->check_mark($uid[0]['uid'], $examid, $subject[0]['sid']);
											if($result != NULL){
												$error = '该学生科目成绩已存在';
											}else{
												if(in_array($compare, $cache)){
													$repeat[]=$value1;
													$error = '该学生考试科目存在重复值';
												}else{
													$cache[]=$compare;
												}
											}
										}
										if($error != '0'){
											$dataArrErr[$e]['data'] = $values;
											$dataArrErr[$e]['error'] = $error;
											$e++;
											break;
										}
										$j++;
									}
									if($error == '0'){
										$dataArr[$i] = $values;
									}
									$i++;
								}
								//整理无效数据
								foreach($dataArrErr as $key=>$value){
									$import_data_error[] = implode(',', $value['data']) . ',' . $value['error'];
								}
								$import_data_err_string = implode('|', $import_data_error);
								
								//整理有效数据
								foreach($dataArr as $key=>$value){
									$import_data[] = implode(',', $value);
								}
								$import_data_string = implode('|', $import_data);
								$examname = $_POST['examname'];
								unset($cache);
							}else{
								Note('文件：' . $filepath . ' 不存在');
								exit;
							}
						}else{
							Note('文件上传失败');
							exit;
						}
					}
				}else{
					Note('该文件无法上传');
				}
			}else{
				Note('请上传文件');
				exit;
			}
			$no_temp=1;
			require_once Template('import_result_show');
			break;
		
		//导入成绩
		case "import_result":
		
		check_access('add_result');
		$Modname = '批量导入成绩 - '.$Modname;
		$examname = $_GET['examname'];
		$examid = _get('examid');
		
		$strexamid=$M_exam->read_one($examid);
		
		$examtype = $M_exam->EY->read_one($strexamid['examtypeid']);
		
		$M_subject_item=G_class('subject_item');
		
		$subject = $examtype['dztype']==0?$M_subject_item->read_one($examtype['subject']):'';

		$arrMembers = $M_user->arrmember('6',$strexamid['bid'],$c);//6为学生用户组ID
		
		foreach($arrMembers[1] as $key=>$item){
			
			$mark =G_class('result')->get_mark($item['uid'],$examid,$subject['sid']);//综合需要再处理

			$arrMembers[1][$key]['mark'] =$mark?$mark:0;
			
			}
			
		if($_POST){
			
		$M_result=G_class('result');
		$backurl = $_POST['backurl'];
		$p_uid= $_POST['uid'];
		$p_sid = $_POST['sid'];
		$p_mark = $_POST['mark'];
		foreach($p_uid as $key=>$item){
		$post['uid']    = trim($item);
		$post['sid']    = trim($p_sid[$key]);
		$post['mark']   = trim($p_mark[$key]);
		$post['examid'] = $examid;
		$havemark =$M_result->read_one($post['uid'],$examid,$post['sid']);//综合需要再处理
		if($post['uid']) $M_result->save($post,$havemark?$havemark['rid']:NULL);
		}
		
		Note('成绩录入成功！','>>点击返回<<',$backurl);
			
		}
		$no_temp=1;
		require_once Template('import_result');
		
		break;
		
		case "work":
		
		$Modname = '作业列表 - '.$Modname;
		
			//获取作业分类
			$arrwork = $M_homework->read_worktype();
			//作业列表
			$bid =$_CO['userbid'];
			$homeworkarr=$M_homework->read_all($bid);
			foreach($homeworkarr as $key =>$item){
				$homework[]=$item;
				$type=$M_homework->read_worktype($item['type']);
				$homework[$key]['typename']=$type[0]['subjectname'];
				$item['typename']=$type[0]['subjectname'];
			}
			
			//分页每页显示数据量
			$listnum = '10';
			//当前页
			$pid=$_GET['p'];
			if(is_numeric($pid)&&$pid>0){
				$pid=(int)$pid;
			}else{
				$pid='1';
			}
			//链接地址
			$url = SITEURL . '?mod=school&do=work&c=index';
			$sum = count($homework);
			$pidf = $pid-1;
			$start = $pidf*$listnum;
			$end = $listnum;
			foreach($homework as $key=>$value){
				$array[] = $value;
			}
			$homework = array();
			$homework = array_slice($array, $start, $end);
			$pageinfo = grtpageno($url, $sum, $listnum, $pid);
			
			//获取参数验证代码
			$homework=fields_code($homework,'homeworkid');
			$no_temp = 1;
			require_once Template("work");
		break;
		
		case "work_content":
			if($_GET['workid'] && is_numeric($_GET['workid'])){
				$workid = intval($_GET['workid']);
			}else{
				Note('非法参数');
				exit;
			}
			$content = $M_homework->get_work_info($workid);
			$no_temp = 1;
			require_once Template("work_content");
			break;
		
		case "addwork":
			check_access('add_work');
			$Modname = '发布新作业 - '.$Modname;
			//获取作业分类
			$arrMembers = G_class('user')->arrmember('5',$_CO['userbid']);
			$no_temp = 1;
			require_once Template("work_add");
			break;
		
		case 'export_excel_result':
		
			check_access('down_result');
			if(!$_GET['string']){
				Note("无效的导出数据");
				exit;
			}
			$string = $_GET['string'];
			$arr1 = explode('|', $string);
			foreach($arr1 as $key=>$value){
				$arr[] = explode(',', $value);
			}
			if($_GET['type'] == 'no'){
				$filename = '无效成绩列表.xls';
				$title = array('0'=>'姓名','1'=>'性别','2'=>'学号','3'=>'帐号','4'=>'科目','5'=>'考试成绩','6'=>'错误信息');
			}else{
				$title = array('0'=>'姓名','1'=>'性别','2'=>'学号','3'=>'帐号','4'=>'科目','5'=>'考试成绩');
				$filename = '有效成绩列表.xls';
			}
			$filename = iconv('UTF-8', 'gb2312', $filename);
			$M_sysnote = G_class('sysnote');
			$M_sysnote->insert_sysnote('1', '0', $_CO['uid'], $_CO['userbid'], '导出数据到Excel表：'.$filename);
			export_excel2($title, $arr, $filename);
			exit;
			break;
		
	}
	
 if($no_temp<0) require_once Template($T);

?>