<?php

/**
 *      【橙创家校通v4.0】 (C)2010-2012 Tuntron Inc.
 *      
 *    	 http://www.tuntron.com   QQ：1121251729
 *
 *      &系统日志基类文件& EDIT BY HUCHENGJUN
 */

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

class tj_sysnote extends tj_model {
	
	var $sysnote;//系统日志表
	
	
	function __construct() {
		global $_G;
		parent::__construct();
		$this->dbinfo = $_G['dbinfo'];
		$this->sysnote = $this->dbinfo['dbpre'].'sysnote';
	}
	/*
	*$post:为提交的数组或者字符串
	*$dotype:操作类型 1:增加2删除3修改
	*$systype:日志类型-1:人员2:考试3:作业4:公告
	*$douserid:操作者id
	*/
	
	function sysnote($post,$dotype,$systype,$douserid){
		global $_CO;
		$time=time();
		$dobid=$_CO['userbid'];
		if($dotype==1){//增加操作
			if($systype==1){	//人员
				$M_branch=G_class('branch');
				$branch=$M_branch->findbranch($post['bid']);//被操作者uid
				$content=$post['realname'].'('.$branch.')';
			}
			if($systype==2){	//考试
				$content=$post['examname'];
			}
			if($systype==3){	//作业
				$content=$post['homeworkname'];
			}
			/* if($systype==4){	//公告
			} */
			if($systype==5){
				$content = $post;
			}
		}
		
		if($dotype==2){	//删除操作
			if($systype==1){	//人员
				if(strpos($post,',')){
					$content='多个学生';
				}else{
					$M_user=G_class('user');
					$u=$M_user->read($post,FALSE,'realname');//获取被删除用户的信息
					$content=$u['realname'];
				}
			}
			
			if($systype==2){	//考试
				$M_exam=G_class(exam);
				$exam=$M_exam->read_one($post['examid']);
				$content=$exam['examname'];
			}
			if($systype==3){	//作业
				$M_homework=G_class('homework');
				$homework=$M_homework->read_one($post['homeworkid']);
				$content=$homework['homeworkname'];
			}
			if($systype==4){	//公告
			
			}
		} 
		
		if($dotype==3){	//修改
			if($systype==1){	//	人员
				$M_user=G_class('user');
				$u=$M_user->read($post['uid'],FALSE,'realname');//获取修改之前的用户名
				$content=$u['realname'].'|'.$post['uid'];
			}
			if($systype==2){	//考试
				$M_exam=G_class(exam);
				$exam=$M_exam->read_one($post['examid']);
				$content=$exam['examname'].'|'.$post['examid'];
			}
			if($systype==3){	//作业
				$M_homework=G_class('homework');
				$homework=$M_homework->read_one($post['homeworkid']);
				$content=$homework['homeworkname'].'|'.$post['homeworkid'];
			}
			if($systype==4){	//公告
			
			}
		}
		
		$sql="INSERT INTO {$this->sysnote} VALUES ('','".$systype."','".$dotype."','".$douserid."','".$dobid."','".$content."','".$time."')";
		$this->db->ady_query($sql);
	}
	
	function insert_sysnote($notetype, $dotype, $douserid, $dobid, $des){
		$time = time();
		$sql = "
			INSERT INTO {$this->sysnote}
			(sysnote_type, dotype, douserid, dobid, other, addtime)
			VALUES
			('$notetype', '$dotype', '$douserid', '$dobid', '$des', '$time')
		";
		$this->db->ady_query($sql);		
	}
	
	function get_sum($bid, $type){
		$sql="
			SELECT sysnoteid
			FROM {$this->sysnote}
			WHERE dobid in ($bid) AND sysnote_type in ($type)
		";
		return $this->db->ady_query($sql, 'select');
	}
	
	function get_sysnote($bid, $type, $pid, $listnum){
		$pidf = $pid-1;
		$start = $pidf*$listnum;
		$M = G_class('user');
		$sql="SELECT * FROM {$this->sysnote} WHERE dobid in ($bid) AND sysnote_type in ($type) ORDER BY addtime desc LIMIT $start,$listnum";
		$con=$this->db->fetch_all_assoc($sql);
		foreach($con as $key=>$item){
			$content[]=$item;
			//操作者信息
			$M_user=G_class('user');
			$u=$M_user->read($item['douserid'],FALSE,'realname');
			$item['douserid']=$u['realname'];
			if($item['dotype']==3&&$item['sysnote_type']==1){	//修改人员
				$temp=explode('|',$item['other']);
				$modified=$M_user->read($temp[1],FALSE,'realname');
				$content[$key]['desc']=$item['douserid'].'修改了('.$temp[0].'->'.$modified['realname'].')';
			}
			if($item['dotype']==3&&$item['sysnote_type']==2){	//修改考试
				$temp=explode('|',$item['other']);
				$M_exam=G_class('exam');
				$modified=$M_exam->read_one($temp[1]);
				$content[$key]['desc']=$item['douserid'].'修改了('.$temp[0].'->'.$modified['examname'].')';
			}
			if($item['dotype']==3&&$item['sysnote_type']==3){	//修改作业
				$temp=explode('|',$item['other']);
				$M_homework=G_class('homework');
				$modified=$M_homework->read_one($temp[1]);
				$content[$key]['desc']=$item['douserid'].'修改了('.$temp[0].'->'.$modified['homeworkname'].')';
			}
			if($item['dotype']==3&&$item['sysnote_type']==4){	//修改公告
				$temp=explode('|',$item['other']);
			
			
			}
			
			
			if($item['sysnote_type']==1) $content[$key]['sysnote_type']='人员日志';
			if($item['sysnote_type']==2) $content[$key]['sysnote_type']='考试日志';
			if($item['sysnote_type']==3) $content[$key]['sysnote_type']='作业日志';
			if($item['sysnote_type']==4) $content[$key]['sysnote_type']='公告日志';
			if($item['dotype']==1){
				$content[$key]['desc']=$item['douserid'].'添加了'.$item['other'];
			}
			if($item['dotype']==2){
				$content[$key]['desc']=$item['douserid'].'删除了'.$item['other'];
			}
			$content[$key]['time']=date('Y-m-d H:i',$item['addtime']);
			$content[$key]['realname']=$u['realname'];
			$content[$key]['username']=$u['username'];
			$branch = $M->B->findbranch($item['dobid']);
			$content[$key]['branch_cn'] = $branch;
		}
		return $content;
	}
}