<?php
/**
 * Filename : homework.class.php
 * Author : huchengjun
 * Creation time : 2012-11-14
 * Description : 【橙创家校通v4.0】 (C)2010-2012 Tuntron Inc.
 * 
 * 作业管理类文件
 * 基本思路：作业类型是基于subject_item
 * 
 */

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


class tj_homework extends tj_model {
	var $dbinfo;
	var $cate;//作业类型
    var $key = 'sid';
	var $table;
    
	function __construct() {
		global $_G;
		parent::__construct();
		$this->init_field();
		$this->dbinfo = $_G['dbinfo'];
		$this->cate = $this->dbinfo['dbpre'] . 'subject_item';//从subject_item获取作业类型
		$this->table = $this->dbinfo['dbpre'] . 'homework';
	}

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

	function init_field() {
		$this->add_field('homeworkid,homeworkname,bid,author,addtime');
		$this->add_field_fun();
		$this->add_field_fun();
        $this->add_field_fun();
	}
	
	function read_worktype($type=null){
		
		$sql="
			SELECT * FROM {$this->cate};
		";
		$tsql="
			SELECT subjectname FROM {$this->cate} WHERE sid= {$type};
		";
		$ssql=isset($type)?$tsql:$sql;
		return $this->db->fetch_all_assoc($ssql);	
	}
	
	function read_one($homeworkid){
		$sql="SELECT * FROM {$this->table} WHERE homeworkid='".$homeworkid."'";
		return $this->db->once_fetch_assoc($sql);
	}

	function read_all($bid){
		$sql="SELECT * FROM {$this->table} WHERE bid ='".$bid."' ORDER BY addtime DESC";
		return $this->db->fetch_all_assoc($sql);
	} 
	
	function add_homework($post){
		$homeworkname = $post['homeworkname'];
		$content=$post['content'];
		$homeworkid ='';
		$type=$post['type'];
		$bid = $post['bid'];
		$author= $post['author'];
		$addtime=$post['addtime'];
		$sql = "
			INSERT INTO {$this->table}
			(homeworkid, homeworkname, content, type, bid, author, addtime)
			VALUES
			('$homeworkid', '$homeworkname', '$content', '$type', '$bid', '$author', '$addtime')
		";
		return $this->db->ady_query($sql,'insert');
	}

	function del_homework($homeworkid) {
		
        $this->db->where('homeworkid', $homeworkid);
		
		$this->db->from($this->table);

        $this->db->delete();
	}
	
	function rename($homeworkname,$homeworkid){
        
		$sql="UPDATE {$this->table} SET homeworkname='".$homeworkname."' WHERE homeworkid='".$homeworkid."'";

		$this->db->ady_query($sql,'update');
    }
    
    function get_work_info($workid){
    	$sql = "
    		SELECT * FROM {$this->table} WHERE homeworkid='$workid'
    	";
    	return $this->db->once_fetch_assoc($sql);
    }
}
?>