<?php

/**
 *      【橙创家校通v4.0】 (C)2010-2012 Tuntron Inc.
 *      
 *    	 http://www.tuntron.com   QQ：1121251729
 *
 *      &考试基类文件&
 */

if (!defined('IS_RUN')) die('Forbidden');

class tj_exam extends tj_model {

	var $table = 'dbpre_exam';
    var $key = 'examid';
	var $sysnote;//系统日志表
	var $user;//当前操作用户
	var $dbpre;

	function __construct() {
		global $_G;
		parent::__construct();
		$this->dbinfo = $_G['dbinfo'];
		$this->sysnote = $this->dbinfo['dbpre'].'sysnote';
		$this->user = $this->dbinfo['dbpre'].'users';
		$this->EY =  G_class('examtype');   //加载考试类型类
		$this->dbpre = $_G['dbinfo']['dbpre'];
		$this->init_field();
	}

    function msm_examouncement() {
        $this->__construct();
    }

	function init_field() {
		$this->add_field('examid,examname,examtypeid,isresult,bid,author,addtime');
		$this->add_field_fun('examid,bid,isresult,examtypeid,addtime', 'intval');
        $this->add_field_fun('examname,author', '_T');
	}

	function read_all($type=null) {
        $this->db->from($this->table);
        if($type) {
			if(strpos($type,'/')){
			$type=explode("/",'1/'.$type);
			}
            $this->db->where('bid', $type);
        }
		$this->db->order_by('addtime','DESC');//必须为大写DESC或ASC
        $this->db->select('examid,examname,examtypeid,bid,author,addtime');
        return $this->db->fetch_all_assoc();
    }
	
	function read_one($examid) {
        $this->db->from($this->table);
        $this->db->where('examid', $examid);
        $this->db->select('examid,examname,examtypeid,isresult,bid,author,addtime');
		$result = $this->db->once_fetch_assoc();
		//$sty=$this->EY->read_one($result['examtypeid']);
		//$sn = G_class('subject_item')->read_one($sty['subject']);
		//$result['subjectname'] = $sn['subjectname'];
        return $result;
    }
	
    function save(& $post, $examid=NULL, $cache=TRUE) {
		global $_CO;
       $post['bid']=$post['bid']?$post['bid']:$_CO['userbid'];
		$this->check_post($post);
        if(!$examid) {
            $post['author'] = $_CO['username'];//准许用户下载
            $post['addtime'] = $this->global['timestamp'];
			$post['isresult'] = 0;//是否录入成绩
			 
        }
        return parent::save($post, $examid, $cache, FALSE);
    }

    function update($post) {
        if(!is_array($post)) Note('global_op_unselect');
        foreach($post as $examid => $val) {
            $val['author'] = (int) $val['author'];
            $this->db->from($this->table);
            $this->db->set($val);
            $this->db->where('examid',$examid);
            $this->db->update();
        }
    }
	
	function delexam($examid) {
		
        $this->db->where('examid', $examid);
		
		$this->db->from($this->table);

        $this->db->delete();
		
        
    }

    function check_post($post,$is_edit=false) {
        if(!$post['examname']) Note('考试名称不能为空！');
		if(!$post['bid']) Note('权限设置失败！');
    }
    
    //根据考试名称获得考试ID Edit by zhaobin
    function get_examid_by_name($name){
    	$sql = "
    		SELECT examid FROM {$this->dbpre}exam WHERE examname='$name'
    	";
    	return $this->db->fetch_all_assoc($sql);
    }
    
    function insert_mark_by_import($uid, $examid, $sid, $mark){
    	$sql = "
    		INSERT INTO {$this->dbpre}result
    		(uid, examid, sid, mark)
    		VALUES
    		('$uid', '$examid', '$sid', '$mark')
    	";
    	$this->db->ady_query($sql, "insert");
    }
    
    //根据学生账号、考试名称、考试科目判断是否已存在相应数据
    function check_mark($uid, $examid, $sid){
    	$sql = "
    		SELECT rid FROM {$this->dbpre}result WHERE uid='$uid' AND sid='$sid' AND examid='$examid'
    	";
    	return $this->db->fetch_all_assoc($sql);
    }
}
?>