<?php
/*
	+-----------------------------------------------------------------------------+
	| ILIAS open source                                                           |
	+-----------------------------------------------------------------------------+
	| Copyright (c) 1998-2001 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. |
	+-----------------------------------------------------------------------------+
*/


/**
* Meta Data class (element educational)
*
* @package ilias-core
* @version $Id: class.ilMDEducational.php,v 1.2 2005/05/30 13:20:50 smeyer Exp $
*/
include_once 'class.ilMDBase.php';

class ilMDEducational extends ilMDBase
{
	function ilMDEducational($a_rbac_id = 0,$a_obj_id = 0,$a_obj_type = '')
	{
		parent::ilMDBase($a_rbac_id,
						 $a_obj_id,
						 $a_obj_type);
	}

	// Methods for child objects (TypicalAgeRange, Description, Language)
	function &getTypicalAgeRangeIds()
	{
		include_once 'Services/Migration/DBUpdate_426/classes/class.ilMDTypicalAgeRange.php';

		return ilMDTypicalAgeRange::_getIds($this->getRBACId(),$this->getObjId(),$this->getMetaId(),'meta_educational');
	}
	function &getTypicalAgeRange($a_typical_age_range_id)
	{
		include_once 'Services/Migration/DBUpdate_426/classes/class.ilMDTypicalAgeRange.php';

		if(!$a_typical_age_range_id)
		{
			return false;
		}
		$typ =& new ilMDTypicalAgeRange();
		$typ->setMetaId($a_typical_age_range_id);

		return $typ;
	}
	function &addTypicalAgeRange()
	{
		include_once 'Services/Migration/DBUpdate_426/classes/class.ilMDTypicalAgeRange.php';

		$typ =& new ilMDTypicalAgeRange($this->getRBACId(),$this->getObjId(),$this->getObjType());
		$typ->setParentId($this->getMetaId());
		$typ->setParentType('meta_educational');

		return $typ;
	}
	function &getDescriptionIds()
	{
		include_once 'Services/Migration/DBUpdate_426/classes/class.ilMDDescription.php';

		return ilMDDescription::_getIds($this->getRBACId(),$this->getObjId(),$this->getMetaId(),'meta_educational');
	}
	function &getDescription($a_description_id)
	{
		include_once 'Services/Migration/DBUpdate_426/classes/class.ilMDDescription.php';

		if(!$a_description_id)
		{
			return false;
		}
		$des =& new ilMDDescription();
		$des->setMetaId($a_description_id);

		return $des;
	}
	function &addDescription()
	{
		include_once 'Services/Migration/DBUpdate_426/classes/class.ilMDDescription.php';

		$des =& new ilMDDescription($this->getRBACId(),$this->getObjId(),$this->getObjType());
		$des->setParentId($this->getMetaId());
		$des->setParentType('meta_educational');

		return $des;
	}
	function &getLanguageIds()
	{
		include_once 'Services/Migration/DBUpdate_426/classes/class.ilMDLanguage.php';

		return ilMDLanguage::_getIds($this->getRBACId(),$this->getObjId(),$this->getMetaId(),'meta_educational');
	}
	function &getLanguage($a_language_id)
	{
		include_once 'Services/Migration/DBUpdate_426/classes/class.ilMDLanguage.php';

		if(!$a_language_id)
		{
			return false;
		}
		$lan =& new ilMDLanguage();
		$lan->setMetaId($a_language_id);

		return $lan;
	}
	function &addLanguage()
	{
		include_once 'Services/Migration/DBUpdate_426/classes/class.ilMDLanguage.php';
		
		$lan =& new ilMDLanguage($this->getRBACId(),$this->getObjId(),$this->getObjType());
		$lan->setParentId($this->getMetaId());
		$lan->setParentType('meta_educational');

		return $lan;
	}

	// SET/GET
	function setInteractivityType($a_iat)
	{
		switch($a_iat)
		{
			case 'Active':
			case 'Expositive':
			case 'Mixed':
				$this->interactivity_type = $a_iat;
				return true;

			default:
				return false;
		}
	}
	function getInteractivityType()
	{
		return $this->interactivity_type;
	}
	function setLearningResourceType($a_lrt)
	{
		switch($a_lrt)
		{
			case 'Exercise':
			case 'Simulation':
			case 'Questionnaire':
			case 'Diagram':
			case 'Figure':
			case 'Graph':
			case 'Index':
			case 'Slide':
			case 'Table':
			case 'NarrativeText':
			case 'Exam':
			case 'Experiment':
			case 'ProblemStatement':
			case 'SelfAssessment':
			case 'Lecture':
				$this->learning_resource_type = $a_lrt;
				return true;

			default:
				return false;
		}
	}
	function getLearningResourceType()
	{
		return $this->learning_resource_type;
	}
	function setInteractivityLevel($a_iat)
	{
		switch($a_iat)
		{
			case 'VeryLow':
			case 'Low':
			case 'Medium':
			case 'High':
			case 'VeryHigh':
				$this->interactivity_level = $a_iat;
				return true;

			default:
				return false;
		}
	}
	function getInteractivityLevel()
	{
		return $this->interactivity_level;
	}
	function setSemanticDensity($a_sd)
	{
		switch($a_sd)
		{
			case 'VeryLow':
			case 'Low':
			case 'Medium':
			case 'High':
			case 'VeryHigh':
				$this->semantic_density = $a_sd;
				return true;

			default:
				return false;
		}
	}
	function getSemanticDensity()
	{
		return $this->semantic_density;
	}
	function setIntendedEndUserRole($a_ieur)
	{
		switch($a_ieur)
		{
			case 'Teacher':
			case 'Author':
			case 'Learner':
			case 'Manager':
				$this->intended_end_user_role = $a_ieur;
				return true;

			default:
				return false;
		}
	}
	function getIntendedEndUserRole()
	{
		return $this->intended_end_user_role;
	}
	function setContext($a_context)
	{
		switch($a_context)
		{
			case 'School':
			case 'HigherEducation':
			case 'Training':
			case 'Other':
				$this->context = $a_context;
				return true;

			default:
				return false;
		}
	}
	function getContext()
	{
		return $this->context;
	}
	function setDifficulty($a_difficulty)
	{
		switch($a_difficulty)
		{
			case 'VeryEasy':
			case 'Easy':
			case 'Medium':
			case 'Difficult':
			case 'VeryDifficult':
				$this->difficulty = $a_difficulty;
				return true;

			default:
				return false;
		}
	}
	function getDifficulty()
	{
		return $this->difficulty;
	}
	function setTypicalLearningTime($a_tlt)
	{
		$this->typical_learning_time = $a_tlt;
	}
	function getTypicalLearningTime()
	{
		return $this->typical_learning_time;
	}


	function save()
	{
		if($this->db->autoExecute('il_meta_educational',
								  $this->__getFields(),
								  DB_AUTOQUERY_INSERT))
		{
			$this->setMetaId($this->db->getLastInsertId());

			return $this->getMetaId();
		}

		return false;
	}

	function update()
	{
		if($this->getMetaId())
		{
			if($this->db->autoExecute('il_meta_educational',
									  $this->__getFields(),
									  DB_AUTOQUERY_UPDATE,
									  "meta_educational_id = '".$this->getMetaId()."'"))
			{
				return true;
			}
		}
		return false;
	}

	function delete()
	{
		if($this->getMetaId())
		{
			$query = "DELETE FROM il_meta_educational ".
				"WHERE meta_educational_id = '".$this->getMetaId()."'";
			
			$this->db->query($query);

			foreach($this->getTypicalAgeRangeIds() as $id)
			{
				$typ =& $this->getTypicalAgeRange($id);
				$typ->delete();
			}
			foreach($this->getDescriptionIds() as $id)
			{
				$des =& $this->getDescription($id);
				$des->delete();
			}
			foreach($this->getLanguageIds() as $id)
			{
				$lan =& $this->getLanguage($id);
				$lan->delete();
			}

			
			return true;
		}
		return false;
	}
			

	function __getFields()
	{
		return array('rbac_id'	=> $this->getRBACId(),
					 'obj_id'	=> $this->getObjId(),
					 'obj_type'	=> ilUtil::prepareDBString($this->getObjType()),
					 'interactivity_type' => ilUtil::prepareDBString($this->getInteractivityType()),
					 'learning_resource_type' => ilUtil::prepareDBString($this->getLearningResourceType()),
					 'interactivity_level' => ilUtil::prepareDBString($this->getInteractivityLevel()),
					 'semantic_density' => ilUtil::prepareDBString($this->getSemanticDensity()),
					 'intended_end_user_role' => ilUtil::prepareDBString($this->getIntendedEndUserRole()),
					 'context' => ilUtil::prepareDBString($this->getContext()),
					 'difficulty' => ilUtil::prepareDBString($this->getDifficulty()),
					 'typical_learning_time' => ilUtil::prepareDBString($this->getTypicalLearningTime()));
	}

	function read()
	{
		if($this->getMetaId())
		{

			$query = "SELECT * FROM il_meta_educational ".
				"WHERE meta_educational_id = '".$this->getMetaId()."'";

		
			$res = $this->db->query($query);
			while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
			{
				$this->setRBACId($row->rbac_id);
				$this->setObjId($row->obj_id);
				$this->setObjType($row->obj_type);
				$this->setInteractivityType(ilUtil::stripSlashes($row->interactivity_type));
				$this->setLearningResourceType(ilUtil::stripSlashes($row->learning_resource_type));
				$this->setInteractivityLevel(ilUtil::stripSlashes($row->interactivity_level));
				$this->setSemanticDensity(ilUtil::stripSlashes($row->semantic_density));
				$this->setIntendedEndUserRole(ilUtil::stripSlashes($row->intended_end_user_role));
				$this->setContext(ilUtil::stripSlashes($row->context));
				$this->setDifficulty(ilUtil::stripSlashes($row->difficulty));
				$this->setTypicalLearningTime(ilUtil::stripSlashes($row->typical_learning_time));
			}
			return true;
		}
		return false;
	}
				
	/*
	 * XML Export of all meta data
	 * @param object (xml writer) see class.ilMD2XML.php
	 * 
	 */
	function toXML(&$writer)
	{
		$writer->xmlStartTag('Educational',
							 array('InteractivityType' => $this->getInteractivityType(),
								   'LearningResourceType' => $this->getLearningResourceType(),
								   'InteractivityLevel' => $this->getInteractivityLevel(),
								   'SemanticDensity' => $this->getSemanticDensity(),
								   'IntendedEndUserRole' => $this->getIntendedEndUserRole(),
								   'Context' => $this->getContext(),
								   'Difficulty' => $this->getDifficulty()));
							 
		// TypicalAgeRange
		foreach($this->getTypicalAgeRangeIds() as $id)
		{
			$key =& $this->getTypicalAgeRange($id);
			$key->toXML($writer);
		}
		// TypicalLearningTime
		$writer->xmlElement('TypicalLearningTime',null,$this->getTypicalLearningTime());

		// Description
		foreach($this->getDescriptionIds() as $id)
		{
			$key =& $this->getDescription($id);
			$key->toXML($writer);
		}
		// Language
		foreach($this->getLanguageIds() as $id)
		{
			$lang =& $this->getLanguage($id);
			$lang->toXML($writer);
		}
		$writer->xmlEndTag('Educational');
	}
	// STATIC
	function _getId($a_rbac_id,$a_obj_id)
	{
		global $ilDB;

		$query = "SELECT meta_educational_id FROM il_meta_educational ".
			"WHERE rbac_id = '".$a_rbac_id."' ".
			"AND obj_id = '".$a_obj_id."'";

		$res = $ilDB->query($query);
		while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
		{
			return $row->meta_educational_id;
		}
		return false;
	}
}
?>