ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilPCQuestion.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 
6 require_once("./Services/COPage/classes/class.ilPageContent.php");
7 
19 {
23  protected $lng;
24 
28  protected $ctrl;
29 
33  protected $user;
34 
35  public $dom;
36  public $q_node; // node of Paragraph element
37 
38  protected static $initial_done; // [bool]
39 
43  public function init()
44  {
45  global $DIC;
46 
47  $this->lng = $DIC->language();
48  $this->ctrl = $DIC->ctrl();
49  $this->user = $DIC->user();
50  $this->setType("pcqst");
51  }
52 
56  public function setNode($a_node)
57  {
58  parent::setNode($a_node); // this is the PageContent node
59  $this->q_node = $a_node->first_child(); //... and this the Question
60  }
61 
67  public function setQuestionReference($a_questionreference)
68  {
69  if (is_object($this->q_node)) {
70  $this->q_node->set_attribute("QRef", $a_questionreference);
71  }
72  }
73 
79  public function getQuestionReference()
80  {
81  if (is_object($this->q_node)) {
82  return $this->q_node->get_attribute("QRef", $a_questionreference);
83  }
84  return false;
85  }
86 
90  public function create(&$a_pg_obj, $a_hier_id)
91  {
92  $this->createPageContentNode();
93  $a_pg_obj->insertContent($this, $a_hier_id, IL_INSERT_AFTER);
94  $this->q_node = $this->dom->create_element("Question");
95  $this->q_node = $this->node->append_child($this->q_node);
96  $this->q_node->set_attribute("QRef", "");
97  }
98 
105  public function copyPoolQuestionIntoPage($a_q_id, $a_hier_id)
106  {
107  include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
108  include_once "./Modules/TestQuestionPool/classes/class.assQuestionGUI.php";
109  $question = assQuestion::_instanciateQuestion($a_q_id);
110  $duplicate_id = $question->copyObject(0, $question->getTitle());
111  $duplicate = assQuestion::_instanciateQuestion($duplicate_id);
112  $duplicate->setObjId(0);
113 
114  /* PATCH-BEGIN: moved cleanup code to central place ilAssSelfAssessmentQuestionFormatter */
115  /*
116  // we remove everything not supported by the non-tiny self
117  // assessment question editor
118  $q = $duplicate->getQuestion();
119 
120  // we try to save all latex tags
121  $try = true;
122  $ls = '<span class="latex">';
123  $le = '</span>';
124  while ($try)
125  {
126  // search position of start tag
127  $pos1 = strpos($q, $ls);
128  if (is_int($pos1))
129  {
130  $pos2 = strpos($q, $le, $pos1);
131  if (is_int($pos2))
132  {
133  // both found: replace end tag
134  $q = substr($q, 0, $pos2)."[/tex]".substr($q, $pos2+7);
135  $q = substr($q, 0, $pos1)."[tex]".substr($q, $pos1+20);
136  }
137  else
138  {
139  $try = false;
140  }
141  }
142  else
143  {
144  $try = false;
145  }
146  }
147 
148  $tags = assQuestionGUI::getSelfAssessmentTags();
149  $tstr = "";
150  foreach ($tags as $t)
151  {
152  $tstr.="<".$t.">";
153  }
154  $q = ilUtil::secureString($q, true, $tstr);
155  // self assessment uses nl2br, not p
156  $duplicate->setQuestion($q);
157 
158  $duplicate->saveQuestionDataToDb();
159  */
160 
161  require_once 'Modules/TestQuestionPool/classes/questions/class.ilAssSelfAssessmentQuestionFormatter.php';
163 
164  /* PATCH-END: moved cleanup code to central place ilAssSelfAssessmentQuestionFormatter */
165 
166  $this->q_node->set_attribute("QRef", "il__qst_" . $duplicate_id);
167  }
168 
173  public static function getLangVars()
174  {
175  return array("ed_insert_pcqst", "empty_question", "pc_qst");
176  }
177 
186  public static function afterPageUpdate($a_page, DOMDocument $a_domdoc, $a_xml, $a_creation)
187  {
188  global $DIC;
189 
190  $ilDB = $DIC->database();
191 
192  include_once("./Services/Link/classes/class.ilInternalLink.php");
193 
194  $ilDB->manipulateF(
195  "DELETE FROM page_question WHERE page_parent_type = %s " .
196  " AND page_id = %s AND page_lang = %s",
197  array("text", "integer", "text"),
198  array($a_page->getParentType(), $a_page->getId(), $a_page->getLanguage())
199  );
200 
201  $xpath = new DOMXPath($a_domdoc);
202  $nodes = $xpath->query('//Question');
203  $q_ids = array();
204  foreach ($nodes as $node) {
205  $q_ref = $node->getAttribute("QRef");
206 
207  $inst_id = ilInternalLink::_extractInstOfTarget($q_ref);
208  if (!($inst_id > 0)) {
210  if ($q_id > 0) {
211  $q_ids[$q_id] = $q_id;
212  }
213  }
214  }
215  foreach ($q_ids as $qid) {
216  $ilDB->manipulateF(
217  "INSERT INTO page_question (page_parent_type, page_id, page_lang, question_id)" .
218  " VALUES (%s,%s,%s,%s)",
219  array("text", "integer", "text", "integer"),
220  array($a_page->getParentType(), $a_page->getId(), $a_page->getLanguage(), $qid)
221  );
222  }
223  }
224 
230  public static function beforePageDelete($a_page)
231  {
232  global $DIC;
233 
234  $ilDB = $DIC->database();
235 
236  $ilDB->manipulateF(
237  "DELETE FROM page_question WHERE page_parent_type = %s " .
238  " AND page_id = %s AND page_lang = %s",
239  array("text", "integer", "text"),
240  array($a_page->getParentType(), $a_page->getId(), $a_page->getLanguage())
241  );
242  }
243 
247  public static function _getQuestionIdsForPage($a_parent_type, $a_page_id, $a_lang = "-")
248  {
249  global $DIC;
250 
251  $ilDB = $DIC->database();
252 
253  $res = $ilDB->queryF(
254  "SELECT * FROM page_question WHERE page_parent_type = %s " .
255  " AND page_id = %s AND page_lang = %s",
256  array("text", "integer", "text"),
257  array($a_parent_type, $a_page_id, $a_lang)
258  );
259  $q_ids = array();
260  while ($rec = $ilDB->fetchAssoc($res)) {
261  $q_ids[] = $rec["question_id"];
262  }
263 
264  return $q_ids;
265  }
266 
273  public static function _getPageForQuestionId($a_q_id, $a_parent_type = "")
274  {
275  global $DIC;
276 
277  $ilDB = $DIC->database();
278 
279  $set = $ilDB->query(
280  "SELECT * FROM page_question " .
281  " WHERE question_id = " . $ilDB->quote($a_q_id, "integer")
282  );
283  while ($rec = $ilDB->fetchAssoc($set)) {
284  if ($a_parent_type == "" || $rec["page_parent_type"] == $a_parent_type) {
285  return array("page_id" => $rec["page_id"], "parent_type" => $rec["page_parent_type"]);
286  }
287  }
288  return false;
289  }
290 
297  public function modifyPageContentPostXsl($a_output, $a_mode)
298  {
299  $lng = $this->lng;
300 
301  $qhtml = "";
302 
303  if ($this->getPage()->getPageConfig()->getEnableSelfAssessment()) {
304  // #14154
305  $q_ids = $this->getPage()->getQuestionIds();
306  if (sizeof($q_ids)) {
307  include_once "./Modules/TestQuestionPool/classes/class.assQuestionGUI.php";
308  foreach ($q_ids as $q_id) {
309  $q_gui = assQuestionGUI::_getQuestionGUI("", $q_id);
310  // object check due to #16557
311  if (is_object($q_gui->object) && !$q_gui->object->isComplete()) {
312  $a_output = str_replace(
313  "{{{{{Question;il__qst_" . $q_id . "}}}}}",
314  "<i>" . $lng->txt("cont_empty_question") . "</i>",
315  $a_output
316  );
317  }
318  }
319 
320  // this exports the questions which is needed below
321  $qhtml = $this->getQuestionJsOfPage(($a_mode == "edit") ? true : false, $a_mode);
322 
323  require_once './Modules/Scorm2004/classes/class.ilQuestionExporter.php';
324  $a_output = "<script>" . ilQuestionExporter::questionsJS($q_ids) . "</script>" . $a_output;
325  if (!self::$initial_done) {
326  $a_output = "<script>var ScormApi=null; var questions = new Array();</script>" . $a_output;
327  self::$initial_done = true;
328  }
329  }
330  } else {
331  // set by T&A components
332  $qhtml = $this->getPage()->getPageConfig()->getQuestionHTML();
333 
334  // address #19788
335  if (!is_array($qhtml) || count($qhtml) == 0) {
336  // #14154
337  $q_ids = $this->getPage()->getQuestionIds();
338  if (sizeof($q_ids)) {
339  foreach ($q_ids as $k) {
340  $a_output = str_replace("{{{{{Question;il__qst_$k" . "}}}}}", " " . $lng->txt("copg_questions_not_supported_here"), $a_output);
341  }
342  }
343  }
344  }
345 
346  if (is_array($qhtml)) {
347  foreach ($qhtml as $k => $h) {
348  $a_output = str_replace("{{{{{Question;il__qst_$k" . "}}}}}", " " . $h, $a_output);
349  }
350  }
351 
352  return $a_output;
353  }
354 
358  public static function resetInitialState()
359  {
360  self::$initial_done = false;
361  }
362 
366  public function getJavascriptFiles($a_mode)
367  {
368  $js_files = array();
369 
370  if ($this->getPage()->getPageConfig()->getEnableSelfAssessment()) {
371  $js_files[] = "./Modules/Scorm2004/scripts/questions/pure.js";
372  $js_files[] = "./Modules/Scorm2004/scripts/questions/question_handling.js";
373  $js_files[] = "Modules/TestQuestionPool/js/ilMatchingQuestion.js";
374  $js_files[] = "Modules/TestQuestionPool/js/ilMultipleChoiceQuestion.js";
375 
376  global $DIC;
377  if ($DIC['ilBrowser']->isMobile() || $DIC['ilBrowser']->isIpad()) {
378  $js_files[] = 'libs/bower/bower_components/jqueryui-touch-punch/jquery.ui.touch-punch.min.js';
379  }
380  }
381 
382  if (!$this->getPage()->getPageConfig()->getEnableSelfAssessmentScorm() && $a_mode != IL_PAGE_PREVIEW
383  && $a_mode != "offline") {
384  $js_files[] = "./Services/COPage/js/ilCOPageQuestionHandler.js";
385  }
386 
387  return $js_files;
388  }
389 
393  public function getCssFiles($a_mode)
394  {
395  if ($this->getPage()->getPageConfig()->getEnableSelfAssessment()) {
396  return array("./Modules/Scorm2004/templates/default/question_handling.css",
397  "Modules/TestQuestionPool/templates/default/test_javascript.css");
398  }
399  return array();
400  }
401 
405  public function getOnloadCode($a_mode)
406  {
409 
410  $code = array();
411 
412  if ($this->getPage()->getPageConfig()->getEnableSelfAssessment()) {
413  if (!$this->getPage()->getPageConfig()->getEnableSelfAssessmentScorm() && $a_mode != IL_PAGE_PREVIEW
414  && $a_mode != "offline") {
415  $ilCtrl->setParameterByClass(strtolower(get_class($this->getPage())) . "gui", "page_id", $this->getPage()->getId());
416  $url = $ilCtrl->getLinkTargetByClass(strtolower(get_class($this->getPage())) . "gui", "processAnswer", "", true, false);
417  $code[] = "ilCOPageQuestionHandler.initCallback('" . $url . "');";
418  }
419 
420  if ($this->getPage()->getPageConfig()->getDisableDefaultQuestionFeedback()) {
421  $code[] = "ilias.questions.default_feedback = false;";
422  }
423 
424  $code[] = self::getJSTextInitCode($this->getPage()->getPageConfig()->getLocalizationLanguage()) . ' il.COPagePres.updateQuestionOverviews();';
425  }
426 
427  $get_stored_tries = $this->getPage()->getPageConfig()->getUseStoredQuestionTries();
428  if ($get_stored_tries) {
429  $q_ids = $this->getPage()->getQuestionIds();
430  if (count($q_ids) > 0) {
431  foreach ($q_ids as $q_id) {
432  include_once("./Services/COPage/classes/class.ilPageQuestionProcessor.php");
434  $code[] = "ilias.questions.initAnswer(" . $q_id . ", " . (int) $as["try"] . ", " . ($as["passed"] ? "true" : "null") . ");";
435  }
436  }
437  }
438  return $code;
439  }
440 
447  public static function getJSTextInitCode($a_lang)
448  {
449  global $DIC;
450 
451  $lng = $DIC->language();
452  $ilUser = $DIC->user();
453 
454  if ($a_lang == "") {
455  $a_lang = $ilUser->getLanguage();
456  }
457 
458  return
459  '
460  ilias.questions.txt.wrong_answers = "' . $lng->txtlng("content", "cont_wrong_answers", $a_lang) . '";
461  ilias.questions.txt.wrong_answers_single = "' . $lng->txtlng("content", "cont_wrong_answers_single", $a_lang) . '";
462  ilias.questions.txt.tries_remaining = "' . $lng->txtlng("content", "cont_tries_remaining", $a_lang) . '";
463  ilias.questions.txt.please_try_again = "' . $lng->txtlng("content", "cont_please_try_again", $a_lang) . '";
464  ilias.questions.txt.all_answers_correct = "' . $lng->txtlng("content", "cont_all_answers_correct", $a_lang) . '";
465  ilias.questions.txt.enough_answers_correct = "' . $lng->txtlng("content", "cont_enough_answers_correct", $a_lang) . '";
466  ilias.questions.txt.nr_of_tries_exceeded = "' . $lng->txtlng("content", "cont_nr_of_tries_exceeded", $a_lang) . '";
467  ilias.questions.txt.correct_answers_shown = "' . $lng->txtlng("content", "cont_correct_answers_shown", $a_lang) . '";
468  ilias.questions.txt.correct_answers_also = "' . $lng->txtlng("content", "cont_correct_answers_also", $a_lang) . '";
469  ilias.questions.txt.correct_answer_also = "' . $lng->txtlng("content", "cont_correct_answer_also", $a_lang) . '";
470  ilias.questions.txt.ov_all_correct = "' . $lng->txtlng("content", "cont_ov_all_correct", $a_lang) . '";
471  ilias.questions.txt.ov_some_correct = "' . $lng->txtlng("content", "cont_ov_some_correct", $a_lang) . '";
472  ilias.questions.txt.ov_wrong_answered = "' . $lng->txtlng("content", "cont_ov_wrong_answered", $a_lang) . '";
473  ilias.questions.txt.please_select = "' . $lng->txtlng("content", "cont_please_select", $a_lang) . '";
474  ilias.questions.txt.ov_preview = "' . $lng->txtlng("content", "cont_ov_preview", $a_lang) . '";
475  ilias.questions.txt.submit_answers = "' . $lng->txtlng("content", "cont_submit_answers", $a_lang) . '";
476  ilias.questions.refresh_lang();
477  ';
478  }
479 
483  public function getQuestionJsOfPage($a_no_interaction, $a_mode)
484  {
485  require_once './Modules/Scorm2004/classes/class.ilQuestionExporter.php';
486  $q_ids = $this->getPage()->getQuestionIds();
487  $js = array();
488  if (count($q_ids) > 0) {
489  foreach ($q_ids as $q_id) {
490  $q_exporter = new ilQuestionExporter($a_no_interaction);
491  $image_path = null;
492  if ($a_mode == "offline") {
493  if ($this->getPage()->getParentType() == "sahs") {
494  $image_path = "./objects/";
495  }
496  if ($this->getPage()->getParentType() == "lm") {
497  $image_path = "./assessment/0/" . $q_id . "/images/";
498  }
499  }
500 
501  $js[$q_id] = $q_exporter->exportQuestion($q_id, $image_path, $a_mode);
502  }
503  }
504  return $js;
505  }
506 }
static getJSTextInitCode($a_lang)
Get js txt init code.
create(&$a_pg_obj, $a_hier_id)
Create Question Element.
getQuestionJsOfPage($a_no_interaction, $a_mode)
Get question js.
static _instanciateQuestion($question_id)
Creates an instance of a question with a given question id.
Scorm 2004 Question Exporter.
const IL_PAGE_PREVIEW
init()
Init page content component.
static _getQuestionIdsForPage($a_parent_type, $a_page_id, $a_lang="-")
Get all questions of a page.
$js
global $DIC
Definition: saml.php:7
$h
$code
Definition: example_050.php:99
static beforePageDelete($a_page)
Before page is being deleted.
setType($a_type)
Set Type.
static getLangVars()
Get lang vars needed for editing.
user()
Definition: user.php:4
Class ilPageContent.
global $ilCtrl
Definition: ilias.php:18
static getAnswerStatus($a_q_id, $a_user_id=0)
Get statistics for question.
getJavascriptFiles($a_mode)
Get Javascript files.
foreach($_POST as $key=> $value) $res
static _getPageForQuestionId($a_q_id, $a_parent_type="")
Get page for question id.
Class ilPCQuestion.
static _getQuestionGUI($question_type, $question_id=-1)
Creates a question gui representation and returns the alias to the question gui note: please do not u...
$as
$ilUser
Definition: imgupload.php:18
getOnloadCode($a_mode)
Get on load code.
getCssFiles($a_mode)
Get css files.
const IL_INSERT_AFTER
static afterPageUpdate($a_page, DOMDocument $a_domdoc, $a_xml, $a_creation)
After page has been updated (or created)
copyPoolQuestionIntoPage($a_q_id, $a_hier_id)
Copy question from pool into page.
modifyPageContentPostXsl($a_output, $a_mode)
Modify page content after xsl.
static resetInitialState()
Reset initial state (for exports)
createPageContentNode($a_set_this_node=true)
Create page content node (always use this method first when adding a new element) ...
setNode($a_node)
Set node.
global $ilDB
setQuestionReference($a_questionreference)
Set Question Reference.
$url
getQuestionReference()
Get Question Reference.
static questionsJS(array $a_qids=null)