<?php
/**
 * unitcopy_model - Model for add/update/delete user
 *
 * @author $Author: dtong $
 * @version $Id: unitcopy_model.php,v 1.5 2011/02/11 09:09:23 dtong Exp $
 * @copyright Copyright (c) 2009, Tiller Software Co., Ltd.
*/

class unitcopy_model extends CI_Model {
	function __construct() {
		parent::__construct();
    }

	function get_courseall() {
		$this->db->select('id, name');
	    $this->db->from('course_sis');
	    $this->db->where('enabled',CMS_ADMIN_ENABLE);
	    $this->db->order_by('name');
	    $res = $this->db->get();
			if ( $res->num_rows() <= 0 )
			return FALSE;

		$user = $res->result();
		$result = array();
		foreach($user as $value) {
			$result[$value->id] = $value->name;
		}
		return $result;
	}
	function get_course($courseid) {
		if ( !is_numeric($courseid) )
			return FALSE;
		$this->db->select('id, name');
	    $this->db->from('course_sis');
	  	$this->db->where('id',$courseid);
	    $this->db->order_by('name');
	  	 $res = $this->db->get();
			if ( $res->num_rows() <= 0 )
			return FALSE;

		return $res->result();
	}

	function get_unit($courseid, $datatarget=False) {
		var_dump($courseid);
			var_dump($datatarget);
		/*

		$this->load->library('unit/unit_model');

	    $unit = $this->db->dbprefix('unit');
		$sql = "select id ,title from $unit";
		$where = '';

  		$sep = ',';
		$wherein = '';


	    if ($courseid!=FALSE){
	    	//$this->db->where('courseid',$courseid);
	    	$where = " where courseid=$courseid";

	    	//$this->db->where_in('status',array($this->unit_model->STATUS_CURRENT, $this->unit_model->STATUS_EDIT));
	    	//$where.= " and status in(".$this->unit_model->STATUS_CURRENT. ",". $this->unit_model->STATUS_EDIT.")";
	    }
	   // $wherein = implode(",", $unit);
	    if ($datatarget!= FALSE){
	    	$en = count($datatarget);
		  	for ($i=0;$i< $en;$i++) {

				$unitid   = $datatarget[$i];
				if ($i==$en-1)
					$sep = '';
				$wherein .= "$unitid".$sep;

			}
	  		$where .= " And id not in($wherein)".$sep;
	    }
	    var_dump($where);
	   $orderby =' Order by title';
	   $sql 	= $sql . $where . $orderby;
	   $res= $this->db->query($sql);

	    if ( $res->num_rows() <= 0 )
	    return FALSE;

		$user = $res->result();
		$result = array();
		foreach($user as $value) {
			$result[$value->id] = $value->title;
		}
		return $result;
		*/
	}

	function insert($courseid, $unit) {
		if ( !is_numeric($courseid) || is_array($unit) == FALSE )
			return FALSE;

		$tbunit = $this->db->dbprefix('unit');

		$wherein = implode(",", $unit);

		$fields  = "courseid, origunitid, editunitid, currentunitid, groupid, course_intid, ";
		$fields .= "lasteditorid, creatorid, type, status, ";
		$fields .= "startdate, enddate, createdate, editdate, modifydate, archivedate,";
		$fields .= "version, title, description";

		$sql_ins = "INSERT INTO $tbunit ($fields, parentid)  ";
	    //$courseid as $fields for set target courseid to new unit
		$sql_sel = "SELECT $courseid as $fields, parentid FROM $tbunit WHERE id in ($wherein)";
	 	$res     = $this->db->query($sql_ins . $sql_sel);
		/*if ($res != FALSE) {
			$sql_upd = "UPDATE $tbunit SET groupid=id WHERE groupid in ($wherein) and courseid=$courseid and groupid != id";
			$res     = $this->db->query($sql_upd);
		}
		*/

		$this->db->select('u.id, u.parentid, o.id oldunitid');
	    $this->db->from('unit u');
	    $this->db->join('unit o','o.groupid = u.parentid and o.groupid != o.id','left');
		$this->db->where_in('u.parentid',$wherein);
		$this->db->order_by('u.parentid');
		$res = $this->db->get();
		if ( $res->num_rows() <= 0 )
			return FALSE;
		$resobj = $res->result();

		if ($res != FALSE) {
			$sql_upd = "UPDATE $tbunit SET groupid=id WHERE groupid in ($wherein) and courseid=$courseid and groupid != id";
			$res     = $this->db->query($sql_upd);
		}

		$lastparentid = '';
		foreach ($resobj as $row) {
			if ($lastparentid == '')
			    $lastparentid =  $row->parentid;

			if ($lastparentid != $row->parentid) {
			    $lastparentid =  $row->parentid;

			    $wherein = implode(",", $temp);
				$sql_ins = "INSERT INTO $tbunit ($fields, parentid)  ";
				$sql_sel = "SELECT $fields, $row->oldunitid as parentid FROM $tbunit WHERE id in ($wherein)";
				$res     = $this->db->query($sql_ins . $sql_sel);
				if ($res != FALSE) {
					$sql_upd = "UPDATE $tbunit SET groupid=id WHERE groupid in ($wherein) and groupid != id";
					$res     = $this->db->query($sql_upd);
				}

			    $temp = array();
			}

			$temp[] = $row->id;
		}

		if (count($temp) != 0) {
			$wherein = implode(",", $temp);
			$sql_ins = "INSERT INTO $tbunit ($fields, parentid)  ";
			$sql_sel = "SELECT $fields, $row->oldunitid as parentid FROM $tbunit WHERE id in ($wherein)";
			$res     = $this->db->query($sql_ins . $sql_sel);
			if ($res != FALSE) {
				$sql_upd = "UPDATE $tbunit SET groupid=id WHERE groupid in ($wherein) and groupid != id";
				$res     = $this->db->query($sql_upd);
			}
		}


		 return FALSE;
	}


}
?>