<?php
/*
	+-----------------------------------------------------------------------------+
	| ILIAS open source                                                           |
	+-----------------------------------------------------------------------------+
	| Copyright (c) 1998-2005 ILIAS open source, University of Cologne            |
	|                                                                             |
	| This program is free software; you can redistribute it and/or               |
	| modify it under the terms of the GNU General Public License                 |
	| as published by the Free Software Foundation; either version 2              |
	| of the License, or (at your option) any later version.                      |
	|                                                                             |
	| This program is distributed in the hope that it will be useful,             |
	| but WITHOUT ANY WARRANTY; without even the implied warranty of              |
	| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
	| GNU General Public License for more details.                                |
	|                                                                             |
	| You should have received a copy of the GNU General Public License           |
	| along with this program; if not, write to the Free Software                 |
	| Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
	+-----------------------------------------------------------------------------+
*/

/**
* Class ilObj<module_name>
* 
* @author Stefan Meyer <smeyer@databay.de> 
* @version $Id: class.ilCourseStart.php 15697 2008-01-08 20:04:33Z hschottm $
*
* @extends ilObject
*/

class ilCourseStart
{
	var $db;

	var $ref_id;
	var $id;
	var $start_objs = array();

	/**
	* Constructor
	* @access	public
	* @param	integer	reference_id or object_id
	* @param	boolean	treat the id as reference_id (true) or object_id (false)
	*/
	function ilCourseStart($a_course_ref_id,$a_course_obj_id)
	{
		global $ilDB;

		$this->db =& $ilDB;

		$this->ref_id = $a_course_ref_id;
		$this->id = $a_course_obj_id;

		$this->__read();
	}
	function setId($a_id)
	{
		$this->id = $a_id;
	}
	function getId()
	{
		return $this->id;
	}
	function setRefId($a_ref_id)
	{
		$this->ref_id = $a_ref_id;
	}
	function getRefId()
	{
		return $this->ref_id;
	}
	function getStartObjects()
	{
		return $this->start_objs ? $this->start_objs : array();
	}
	
	/**
	 * Clone dependencies
	 *
	 * @access public
	 * @param int target id
	 * @param int copy id
	 * 
	 */
	public function cloneDependencies($a_target_id,$a_copy_id)
	{
		global $ilObjDataCache,$ilLog;
		
		$ilLog->write(__METHOD__.': Begin course start objects...');
		
		$new_obj_id = $ilObjDataCache->lookupObjId($a_target_id);
		$start = new ilCourseStart($a_target_id,$new_obj_id);
		
	 	include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
	 	$cwo = ilCopyWizardOptions::_getInstance($a_copy_id);
	 	$mappings = $cwo->getMappings();
	 	foreach($this->getStartObjects() as $start_id => $data)
	 	{
	 		$item_ref_id = $data['item_ref_id'];
	 		if(isset($mappings[$item_ref_id]) and $mappings[$item_ref_id])
	 		{
				$ilLog->write(__METHOD__.': Clone start object nr. '.$item_ref_id);
	 			$start->add($mappings[$item_ref_id]);
	 		}
	 		else
	 		{
				$ilLog->write(__METHOD__.': No mapping found for start object nr. '.$item_ref_id);
	 		}
	 	}
		$ilLog->write(__METHOD__.': ... end course start objects');
	 	return true;
	}

	function delete($a_crs_start_id)
	{
		global $ilDB;
		
		$query = "DELETE FROM crs_start ".
			"WHERE crs_start_id = ".$ilDB->quote($a_crs_start_id)." ".
			"AND crs_id = ".$ilDB->quote($this->getId())." ";

		$this->db->query($query);

		return true;
	}

	function exists($a_item_ref_id)
	{
		global $ilDB;
		
		$query = "SELECT * FROM crs_start ".
			"WHERE crs_id = ".$ilDB->quote($this->getId())." ".
			"AND item_ref_id = ".$a_item_ref_id." ";

		$res = $this->db->query($query);

		return $res->numRows() ? true : false;
	}

	function add($a_item_ref_id)
	{
		global $ilDB;
		
		if($a_item_ref_id)
		{
			$query = "INSERT INTO crs_start ".
				"SET crs_id = ".$ilDB->quote($this->getId()).", ".
				"item_ref_id = ".$ilDB->quote($a_item_ref_id)." ";

			$this->db->query($query);

			return true;
		}
		return false;
	}

	function __deleteAll()
	{
		global $ilDB;
		
		$query = "DELETE FROM crs_start ".
			"WHERE crs_id = ".$ilDB->quote($this->getId())." ";


		$this->db->query($query);

		return true;
	}

	function getPossibleStarters(&$item_obj)
	{
		foreach($item_obj->getItems() as $node)
		{
			switch($node['type'])
			{
				case 'lm':
				case 'sahs':
				case 'svy':
				case 'tst':
					$poss_items[] = $node['ref_id'];
					break;
			}
		}
		return $poss_items ? $poss_items : array();
	}

	function allFullfilled($user_id)
	{
		foreach($this->getStartObjects() as $item)
		{
			if(!$this->isFullfilled($user_id,$item['item_ref_id']))
			{
				return false;
			}
		}
		return true;
	}


	function isFullfilled($user_id,$item_id)
	{
		global $ilObjDataCache;

		include_once './Modules/Course/classes/class.ilCourseLMHistory.php';
		$lm_continue =& new ilCourseLMHistory($this->getRefId(),$user_id);
		$continue_data = $lm_continue->getLMHistory();

		$obj_id = $ilObjDataCache->lookupObjId($item_id);
		$type = $ilObjDataCache->lookupType($obj_id);
		
		switch($type)
		{
			case 'tst':
				include_once './Modules/Test/classes/class.ilObjTestAccess.php';
				
				if(!ilObjTestAccess::_checkCondition($obj_id,'finished',''))
				{
					return false;
				}
				break;
			case 'svy':
				include_once './Modules/Survey/classes/class.ilObjSurveyAccess.php';
				if(!ilObjSurveyAccess::_lookupFinished($obj_id, $user_id))
				{
					return false;
				}
				break;
			case 'sahs':
				include_once 'Services/Tracking/classes/class.ilLPStatusWrapper.php';
				$completed = ilLPStatusWrapper::_getCompleted($obj_id);
				if(!in_array($user_id,$completed))
				{
					return false;
				}
				break;

			default:
				if(!isset($continue_data[$item_id]))
				{
					return false;
				}
		}
		return true;
	}


	// PRIVATE
	function __read()
	{
		global $tree,$ilDB;

		$this->start_objs = array();

		$query = "SELECT * FROM crs_start ".
			"WHERE crs_id = ".$ilDB->quote($this->getId())." ";

		$res = $this->db->query($query);
		while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
		{
			if($tree->isInTree($row->item_ref_id))
			{
				$this->start_objs[$row->crs_start_id]['item_ref_id'] = $row->item_ref_id;
			}
			else
			{
				$this->delete($row->item_ref_id);
			}
		}
		return true;
	}

		


} // END class.ilObjCourseGrouping
?>
