<?php

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

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

class tj_examtype extends tj_model {
	var $dbpre;
	var $table = 'dbpre_exam_type';
    var $key = 'examtypeid';

	function __construct() {
		global $_G;
		parent::__construct();
		$this->init_field();
		$this->dbpre = $_G['dbinfo']['dbpre'];
	}

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

	function init_field() {
		$this->add_field('examtypeid,bid,dztype,subject,tname');
		$this->add_field_fun('examtypeid,bid,dztype', 'intval');
        $this->add_field_fun('subject,tname', '_T');
	}

	function read_all($type=null) {
        $this->db->from($this->table);
        if($type) {
			if(strpos($type,'/')){
			$type=explode("/",$type);
			}
            $this->db->where('bid', $type);
        }
		$this->db->order_by('examtypeid','DESC');//必须为大写DESC或ASC
        $this->db->select('examtypeid,bid,dztype,subject,tname');
        return $this->db->fetch_all_assoc();
    }
	
	function read_one($examtypeid) {
        $this->db->from($this->table);
        $this->db->where('examtypeid', $examtypeid);
        $this->db->select('examtypeid,bid,dztype,subject,tname');
        return $this->db->once_fetch_assoc();
    }

	
    function save(& $post, $examtypeid=NULL, $cache=TRUE) {
        $this->check_post($post);
        return parent::save($post, $examtypeid, $cache, FALSE);
    }

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

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

    function check_post($post,$is_edit=false) {
        if(!$post['tname']) Note('考试名称不能为空！');
		if(!$post['subject']) Note('考试课程不能为空！');
		if(!$post['bid']) Note('权限设置失败！');
    }
    
    // Edit by zhaobin 16:17 2012/11/14 START
    function get_one($id){
    	$sql = "
    		SELECT examtypeid, dztype, subject, tname, sid, subjectname
    		FROM {$this->dbpre}exam_type, {$this->dbpre}subject_item
    		WHERE subject=sid AND examtypeid='$id'
    	";
    	return $this->db->once_fetch_assoc($sql);
    }
    
    function del_one($id){
    	$sql = "
    		DELETE FROM {$this->dbpre}exam_type WHERE examtypeid='$id'
    	";
    	return $this->db->ady_query($sql);
    }
    
    function update2($post){
    	$sql = "
    		UPDATE {$this->dbpre}exam_type
    		SET
    			bid='$post[bid]',
    			dztype='$post[dztype]',
    			subject='$post[subject]',
    			tname='$post[tname]'
    		WHERE examtypeid='$post[examtypeid]'
    	";
    	return $this->db->ady_query($sql, 'update');
    }
    // Edit by zhaobin 16:17 2012/11/14 END
}
?>