Skip to content
class.ilQuestionSetPoolJumpConditionGUI.php 12.9 KiB
Newer Older
require_once "abstract.ilQuestionSetPoolConditionGUI.php";

/**
 * Class ilQuestionSetPoolJumpConditionGUI
 *
 * Date: 05.12.13
 * Time: 09:20
 * @author Thomas Joußen <tjoussen@databay.de>
 */ 
class ilQuestionSetPoolJumpConditionGUI extends ilQuestionSetPoolConditionGUI
nkrzywon's avatar
nkrzywon committed
	/**
	 * @var array $feedback_types needed for tiny-mce 
	 */
	protected $feedback_types = array(
				0 => array('post_var' => 'true_feedback', 		'obj_type' => 'tf'),
				1 => array('post_var' => 'true_jump_feedback', 	'obj_type' => 'tjf'),
				2 => array('post_var' => 'false_feedback',		'obj_type' => 'ff'));
	
	/**
	 * @var ilQuestionSetPoolJumpCondition
	 */
	protected $model;

	public function edit()
	{
mjansen's avatar
mjansen committed
		iljQueryUtil::initjQuery();
		ilYuiUtil::initPanel();
		ilYuiUtil::initOverlay();
		$this->tpl->addJavascript('./Services/UIComponent/Overlay/js/ilOverlay.js');
		$this->tpl->addJavaScript("./Services/JavaScript/js/Basic.js");

		$this->model = $this->loadModel();
tjoussen's avatar
tjoussen committed
		$this->initForm($this->model);
		$this->bindModelToForm($this->model);

		ilUtil::sendInfo($this->plugin->txt("jump_condition_target_question"), true);

		$template = new ilTemplate("tpl.il_xqsp_condition.html", true, true, "Customizing/global/plugins/Services/Repository/RepositoryObject/QuestionSetPool");

		$this->renderQuestionInfo($this->model->getNode(), $template);

		$template->setVariable("FORM", $this->form->getHTML());
		$template->setVariable("INFO", ilQuestionSetPoolInfoBox::render($this->plugin));

nkrzywon's avatar
nkrzywon committed
		$button = ilLinkButton::getInstance();
		$button->addCSSClass('toggle_feedback');
		$button->setCaption($this->plugin->txt('hide_feedback_form_parts'), false);
		$this->toolbar->addButtonInstance($button);
		$template->setVariable("TXT_SHOW_FEEDBACK", $this->plugin->txt('show_feedback_form_parts'));
		$template->setVariable("TXT_HIDE_FEEDBACK", $this->plugin->txt('hide_feedback_form_parts'));

		$this->tpl->setContent($template->get());
	}

	public function update()
	{
		$this->model = $this->loadModel();
nkrzywon's avatar
nkrzywon committed
		
tjoussen's avatar
tjoussen committed
		$this->initForm($this->model);
		if($this->form->checkInput())
		{
			$this->model->bindForm($this->form);

			$isValid = ilQuestionSetPoolConditionValidator::isValid(
				$this->model->getJumpCondition(),
				$this->model->getNode(),
				$this->model->getNode()->getPath()->getNodes()->getData(),
				$this->plugin
			);
			if($isValid)
			{
				$this->model->update();
nkrzywon's avatar
nkrzywon committed

				$this->copyTemporaryMobs();
				$this->deleteUnusedMobs();
				$this->deleteTemporaryMobs();

				ilUtil::sendSuccess($this->plugin->txt("msg_obj_modified"), true);
				$this->redirect(
					 $this->getAfterUpdateRedirectCmd(),
						 $this->getAfterUpdateRedirectParams($this->model)
				);
			}
		}
		$this->form->setValuesByPost();
		$template = new ilTemplate("tpl.il_xqsp_condition.html", true, true, "Customizing/global/plugins/Services/Repository/RepositoryObject/QuestionSetPool");

		$this->renderQuestionInfo($this->model->getNode(), $template);
tjoussen's avatar
tjoussen committed

		$template->setVariable("FORM", $this->form->getHTML());
		$template->setVariable("INFO", ilQuestionSetPoolInfoBox::render($this->plugin));

		$this->tpl->setContent($template->get());
	public function up()
	{
		$condition = $this->loadModel();
		$data = $condition->getNode()->getJumpConditionList()->getData();
		foreach($data as $key => $element)
		{
			if($element["id"] == $condition->getId())
			{
				$this->swap($condition, $data[$key-1]["id"]);
nkrzywon's avatar
nkrzywon committed
				break; 	
			}
		}

		$this->redirect("ilQuestionSetPoolNodesGUI.listNodes", array(
			"path_id" => $condition->getNode()->getPath()->getId(),
			"set_id" => $condition->getNode()->getPath()->getSet()->getId()
		));
	}

	public function down()
	{
		$condition = $this->loadModel();
		$data = $condition->getNode()->getJumpConditionList()->getData();
		foreach($data as $key => $element)
		{
			if($element["id"] == $condition->getId())
			{
				$this->swap($condition, $data[$key+1]["id"]);
				break;
			}
		}
		$this->redirect("ilQuestionSetPoolNodesGUI.listNodes", array(
			"path_id" => $condition->getNode()->getPath()->getId(),
			"set_id" => $condition->getNode()->getPath()->getSet()->getId()
		 ));
	}

	/**
	 * @param ilQuestionSetPoolJumpCondition $condition
	 * @param $swap_condition_id
	 */
	protected function swap($condition, $swap_condition_id)
	{
		$swap_condition = new ilQuestionSetPoolJumpCondition();
		$swap_condition->setId($swap_condition_id);
		$swap_condition->read();

		$sorting = $condition->getSorting();
		$condition->setSorting($swap_condition->getSorting());
		$swap_condition->setSorting($sorting);

		$swap_condition->update();
		$condition->update();
	}


	public function remove()
	{
		$model = $this->loadModel();
		$model->remove();

		ilUtil::sendSuccess($this->plugin->txt("msg_obj_modified"), true);
		$this->redirect(
			 $this->getAfterUpdateRedirectCmd(),
			$this->getAfterUpdateRedirectParams($model)
		);
	}

	/**
	 * Loads and returns the Model for the controller gui
	 * @return ilQuestionSetPoolJumpCondition
		$id = (isset($_GET['condition_id']))? (int)$_GET['condition_id'] : (int)$_POST['id'];
		$model = new ilQuestionSetPoolJumpCondition($id);
		$model->read();

		return $model;
	}

	/**
	 * Initialize the PropertyFormGUI for this controller
tjoussen's avatar
tjoussen committed
	 *
	 * @param $model
tjoussen's avatar
tjoussen committed
	protected function initForm($model)
nmatuschek's avatar
nmatuschek committed
		global $DIC;
		$ilUser = $DIC->user();
nkrzywon's avatar
nkrzywon committed

		$this->form = new ilPropertyFormGUI();
		$this->form->setName('condition_form');
		$this->form->setTableWidth("100%");
tjoussen's avatar
tjoussen committed
		$this->form->setTitle(sprintf($this->plugin->txt("jump_condition_title"),
			$model->getNode()->getPath()->getSet()->getTitle(),
			$model->getNode()->getPath()->getTitle(),
			$model->getNode()->getIndex()
			));

		$node_id = new ilHiddenInputGUI("node_fi");
		$condition_id = new ilHiddenInputGUI("id");
		$condition = new ilConditionInputGUI($this->plugin->txt("condition"), "jump_condition");
mjansen's avatar
mjansen committed
		$condition->setInlineStyle('width:95%');

		$questions = new ilSelectInputGUI($this->plugin->txt("target_question"), "question");
		$questions->setRequired(true);
		$questions->setOptions($this->getQuestions());

		$repititions = new ilNumberInputGUI($this->plugin->txt("repititions"), "repititions");
mjansen's avatar
mjansen committed
		$repititions->setSize(5);
		$repititions->allowDecimals(false);
		$repititions->setRequired(false);

		$this->form->addItem($condition_id);
		$this->form->addItem($condition);
		$this->form->addItem($questions);
nkrzywon's avatar
nkrzywon committed
		
		$true_feedback = new ilTextAreaInputGUI($this->plugin->txt('true_feedback'), 'true_feedback');
		$true_feedback->setUseRte(TRUE);
		$true_feedback->addPlugin("latex");
		$true_feedback->addButton("latex");
		if($_GET["condition_id"])
		{
			$true_feedback->setRTESupport($_GET["condition_id"], 'x_tf', 'assessment');	
		}
		else
		{
			$true_feedback->setRTESupport($ilUser->getId(), 'x_tf~', 'assessment');
		}
		$true_feedback->setRteTagSet('full');
		$this->form->addItem($true_feedback);

		$true_jump_feedback = new ilTextAreaInputGUI($this->plugin->txt('true_jump_feedback'), 'true_jump_feedback');
		$true_jump_feedback->setUseRte(TRUE);
		$true_jump_feedback->addPlugin("latex");
		$true_jump_feedback->addButton("latex");
		if($_GET["condition_id"])
		{
			$true_jump_feedback->setRTESupport($_GET["condition_id"], 'x_tjf', 'assessment');
		}
		else
		{
			$true_jump_feedback->setRTESupport($ilUser->getId(), 'x_tjf~:html', 'assessment');
		}
		$true_jump_feedback->setRteTagSet('full');
		$this->form->addItem($true_jump_feedback);

		$false_feedback = new ilTextAreaInputGUI($this->plugin->txt('false_feedback'), 'false_feedback');
		$false_feedback->setUseRte(TRUE);
		$false_feedback->addPlugin("latex");
		$false_feedback->addButton("latex");
		if($_GET["condition_id"])
		{
			$false_feedback->setRTESupport($_GET["condition_id"], 'x_ff:html', 'assessment');
		}
		else
		{
			$false_feedback->setRTESupport($ilUser->getId(), 'x_ff~:html', 'assessment');
		}
		$false_feedback->setRteTagSet('full');
		$this->form->addItem($false_feedback);
		
		$this->ctrl->setParameter($this->controller, "condition_id", $_GET["condition_id"]);
		$this->ctrl->setParameter($this->controller, "path_id", $model->getNode()->getPath()->getId());
		$this->ctrl->setParameter($this->controller, "set_id", $model->getNode()->getPath()->getSet()->getId());
		$this->form->setFormAction($this->ctrl->getFormAction($this->controller));
		$this->ctrl->setParameter($this->controller, "path_id", "");
		$this->ctrl->setParameter($this->controller, "set_id", "");
		$this->ctrl->setParameter($this->controller, "condition_id", "");
		$this->form->addCommandButton("ilQuestionSetPoolJumpConditionGUI.update", $this->plugin->txt("save"));
		$this->form->addCommandButton("ilQuestionSetPoolNodesGUI.listNodes", $this->plugin->txt("cancel"));

	}

	protected function bindModelToForm(ilPluginModelInterface $model)
	{
		parent::bindModelToForm($model);
		$item = $this->form->getItemByPostVar("node_fi");
		$item->setValue($model->getNode()->getId());
		if($model->getQuestion() != null)
		{
			$question_id = $model->getQuestion()->getId();
		$item = $this->form->getItemByPostVar("question");
		$item->setValue($question_id);
nmatuschek's avatar
nmatuschek committed
		global $DIC;
		$ilDB = $DIC->database();
		
		$questions = array();

		$result = $ilDB->queryF(
			"SELECT qs_qst.question_index, q.title, q.question_id
			FROM rep_robj_xqsp_qs_qst qs_qst
			INNER JOIN rep_robj_xqsp_node n ON n.question_fi = qs_qst.question_fi
			INNER JOIN qpl_questions q ON q.question_id = qs_qst.question_fi
			WHERE n.path_fi = %s AND n.node_index <= %s",
			array("integer", "integer"),
			array($this->model->getNode()->getPath()->getId(), $this->model->getNode()->getIndex())
		);

		while(($row = $ilDB->fetchAssoc($result)) != null)
		{
			$questions[$row["question_id"]] = "Q" . $row['question_index'] . " - " . $row["title"];
		}

		$questions["-"] = $this->plugin->txt("tst_termination");

		return $questions;
	}

	/**
	 * Get the redirect action after an update was successful
	 * @return string
	 */
	protected function getAfterUpdateRedirectCmd()
	{
		return "ilQuestionSetPoolNodesGUI.listNodes";
	}

	/**
	 * @param $model
	 *
	 * @return array
	 */
	protected function getAfterUpdateRedirectParams($model)
	{
		return array(
			"set_id" => $model->getNode()->getPath()->getSet()->getId(),
			"path_id" => $model->getNode()->getPath()->getId()
		);
	}

	/**
	 * Returns the current tab name
	 * return string
	 */
	protected function getCurrentTab()
	{
		return "content";
	}

	/**
	 * @return array
	 */
	protected function getRequirements()
	{
		return array(
			"models/class.ilQuestionSetPoolJumpCondition.php",
			"providers/class.ilParserQuestionProvider.php",
			"utils/abstract.ilQuestionSetPoolInfoBox.php",
			"utils/abstract.ilQuestionSetPoolConditionValidator.php",
			"utils/class.ilConditionInputGUI.php"
nkrzywon's avatar
nkrzywon committed

	/**
	 * 
	 */
	protected function copyTemporaryMobs()
	{
nmatuschek's avatar
nmatuschek committed
		global $DIC;
		$ilUser = $DIC->user();
nkrzywon's avatar
nkrzywon committed
		
		foreach($this->feedback_types as $type)
		{
			$mediaObjects   = ilRTE::_getMediaObjects($this->form->getInput($type['post_var']), 0);
			$myMediaObjects = ilObjMediaObject::_getMobsOfObject('x_'.$type['obj_type'].'~:html', $ilUser->getId());
			foreach($mediaObjects as $mob)
			{
				foreach($myMediaObjects as $myMob)
				{
					if($mob == $myMob)
					{
						// change usage
						ilObjMediaObject::_removeUsage($mob, 'x_'.$type['obj_type'].'~:html', $ilUser->getId());
						break;
					}
				}
				ilObjMediaObject::_saveUsage($mob, 'x_'.$type['obj_type'].':html', $this->model->getId());
			}
		}	
	}

	/**
	 * 
	 */
	protected function deleteUnusedMobs()
	{
		foreach($this->feedback_types as $type)
		{
			// remove usage of deleted media objects
			include_once 'Services/MediaObjects/classes/class.ilObjMediaObject.php';
			$oldMediaObjects = ilObjMediaObject::_getMobsOfObject('x_'.$type['obj_type'].':html', $this->model->getId());
			$curMediaObjects = ilRTE::_getMediaObjects($this->form->getInput($type['post_var']), 0);
			foreach($oldMediaObjects as $oldMob)
			{
				$found = false;
				foreach($curMediaObjects as $curMob)
				{
					if($oldMob == $curMob)
					{
						$found = true;
						break;
					}
				}
				if(!$found)
				{
					if(ilObjMediaObject::_exists($oldMob))
					{
						ilObjMediaObject::_removeUsage($oldMob, 'x_' . $type['obj_type'].':html', $this->model->getId());
						$mob_obj = new ilObjMediaObject($oldMob);
						$mob_obj->delete();
					}
				}
			}
		}
	}
	
	/**
	 * 
	 */
	protected function deleteTemporaryMobs()
	{
nmatuschek's avatar
nmatuschek committed
		global $DIC;
		$ilUser = $DIC->user();
nkrzywon's avatar
nkrzywon committed

		try
		{
			include_once 'Services/MediaObjects/classes/class.ilObjMediaObject.php';
			
			foreach($this->feedback_types as $type)
			{
				$mobs = ilObjMediaObject::_getMobsOfObject('x_'.$type['obj_type'].'~:html', $ilUser->getId());
				foreach($mobs as $mob)
				{
					if(ilObjMediaObject::_exists($mob))
					{
						ilObjMediaObject::_removeUsage($mob, 'x_'.$type['obj_type'].'~:html', $ilUser->getId());
						$mob_obj = new ilObjMediaObject($mob);
						$mob_obj->delete();
					}
				}	
			}
		}
		catch(Exception $e)
		{
		}
	}