ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 {
20  var $dom;
21  var $q_node; // node of Paragraph element
22 
23  protected static $initial_done; // [bool]
24 
28  function init()
29  {
30  $this->setType("pcqst");
31  }
32 
36  function setNode($a_node)
37  {
38  parent::setNode($a_node); // this is the PageContent node
39  $this->q_node = $a_node->first_child(); //... and this the Question
40  }
41 
47  function setQuestionReference($a_questionreference)
48  {
49  if (is_object($this->q_node))
50  {
51  $this->q_node->set_attribute("QRef", $a_questionreference);
52  }
53  }
54 
61  {
62  if (is_object($this->q_node))
63  {
64  return $this->q_node->get_attribute("QRef", $a_questionreference);
65  }
66  return false;
67  }
68 
72  function create(&$a_pg_obj, $a_hier_id)
73  {
74  $this->createPageContentNode();
75  $a_pg_obj->insertContent($this, $a_hier_id, IL_INSERT_AFTER);
76  $this->q_node = $this->dom->create_element("Question");
77  $this->q_node = $this->node->append_child($this->q_node);
78  $this->q_node->set_attribute("QRef", "");
79  }
80 
87  function copyPoolQuestionIntoPage($a_q_id, $a_hier_id)
88  {
89  include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
90  include_once "./Modules/TestQuestionPool/classes/class.assQuestionGUI.php";
91  $question = assQuestion::_instanciateQuestion($a_q_id);
92  $duplicate_id = $question->copyObject(0, $question->getTitle());
93  $duplicate = assQuestion::_instanciateQuestion($duplicate_id);
94  $duplicate->setObjId(0);
95 
96  /* PATCH-BEGIN: moved cleanup code to central place ilAssSelfAssessmentQuestionFormatter */
97  /*
98  // we remove everything not supported by the non-tiny self
99  // assessment question editor
100  $q = $duplicate->getQuestion();
101 
102  // we try to save all latex tags
103  $try = true;
104  $ls = '<span class="latex">';
105  $le = '</span>';
106  while ($try)
107  {
108  // search position of start tag
109  $pos1 = strpos($q, $ls);
110  if (is_int($pos1))
111  {
112  $pos2 = strpos($q, $le, $pos1);
113  if (is_int($pos2))
114  {
115  // both found: replace end tag
116  $q = substr($q, 0, $pos2)."[/tex]".substr($q, $pos2+7);
117  $q = substr($q, 0, $pos1)."[tex]".substr($q, $pos1+20);
118  }
119  else
120  {
121  $try = false;
122  }
123  }
124  else
125  {
126  $try = false;
127  }
128  }
129 
130  $tags = assQuestionGUI::getSelfAssessmentTags();
131  $tstr = "";
132  foreach ($tags as $t)
133  {
134  $tstr.="<".$t.">";
135  }
136  $q = ilUtil::secureString($q, true, $tstr);
137  // self assessment uses nl2br, not p
138  $duplicate->setQuestion($q);
139 
140  $duplicate->saveQuestionDataToDb();
141  */
142 
143  require_once 'Modules/TestQuestionPool/classes/questions/class.ilAssSelfAssessmentQuestionFormatter.php';
145 
146  /* PATCH-END: moved cleanup code to central place ilAssSelfAssessmentQuestionFormatter */
147 
148  $this->q_node->set_attribute("QRef", "il__qst_".$duplicate_id);
149  }
150 
155  static function getLangVars()
156  {
157  return array("ed_insert_pcqst", "empty_question", "pc_qst");
158  }
159 
168  static function afterPageUpdate($a_page, DOMDocument $a_domdoc, $a_xml, $a_creation)
169  {
170  global $ilDB;
171 
172  include_once("./Services/Link/classes/class.ilInternalLink.php");
173 
174  $ilDB->manipulateF("DELETE FROM page_question WHERE page_parent_type = %s ".
175  " AND page_id = %s AND page_lang = %s", array("text", "integer", "text"),
176  array($a_page->getParentType(), $a_page->getId(), $a_page->getLanguage()));
177 
178  $xpath = new DOMXPath($a_domdoc);
179  $nodes = $xpath->query('//Question');
180  $q_ids = array();
181  foreach ($nodes as $node)
182  {
183  $q_ref = $node->getAttribute("QRef");
184 
185  $inst_id = ilInternalLink::_extractInstOfTarget($q_ref);
186  if (!($inst_id > 0))
187  {
189  if ($q_id > 0)
190  {
191  $q_ids[$q_id] = $q_id;
192  }
193  }
194  }
195  foreach($q_ids as $qid)
196  {
197  $ilDB->manipulateF("INSERT INTO page_question (page_parent_type, page_id, page_lang, question_id)".
198  " VALUES (%s,%s,%s,%s)",
199  array("text", "integer", "text", "integer"),
200  array($a_page->getParentType(), $a_page->getId(), $a_page->getLanguage(), $qid));
201  }
202 
203  }
204 
210  static function beforePageDelete($a_page)
211  {
212  global $ilDB;
213 
214  $ilDB->manipulateF("DELETE FROM page_question WHERE page_parent_type = %s ".
215  " AND page_id = %s AND page_lang = %s", array("text", "integer", "text"),
216  array($a_page->getParentType(), $a_page->getId(), $a_page->getLanguage()));
217  }
218 
222  static function _getQuestionIdsForPage($a_parent_type, $a_page_id, $a_lang = "-")
223  {
224  global $ilDB;
225 
226  $res = $ilDB->queryF("SELECT * FROM page_question WHERE page_parent_type = %s ".
227  " AND page_id = %s AND page_lang = %s",
228  array("text", "integer", "text"),
229  array($a_parent_type, $a_page_id, $a_lang));
230  $q_ids = array();
231  while ($rec = $ilDB->fetchAssoc($res))
232  {
233  $q_ids[] = $rec["question_id"];
234  }
235 
236  return $q_ids;
237  }
238 
245  static function _getPageForQuestionId($a_q_id, $a_parent_type = "")
246  {
247  global $ilDB;
248 
249  $set = $ilDB->query("SELECT * FROM page_question ".
250  " WHERE question_id = ".$ilDB->quote($a_q_id, "integer")
251  );
252  while ($rec = $ilDB->fetchAssoc($set))
253  {
254  if ($a_parent_type == "" || $rec["page_parent_type"] == $a_parent_type)
255  {
256  return array("page_id" => $rec["page_id"], "parent_type" => $rec["page_parent_type"]);
257  }
258  }
259  return false;
260  }
261 
268  function modifyPageContentPostXsl($a_output, $a_mode)
269  {
270  global $lng;
271 
272  $qhtml = "";
273 
274  if ($this->getPage()->getPageConfig()->getEnableSelfAssessment())
275  {
276  // #14154
277  $q_ids = $this->getPage()->getQuestionIds();
278  if(sizeof($q_ids))
279  {
280  include_once "./Modules/TestQuestionPool/classes/class.assQuestionGUI.php";
281  foreach($q_ids as $q_id)
282  {
283  $q_gui = assQuestionGUI::_getQuestionGUI("", $q_id);
284  // object check due to #16557
285  if(is_object($q_gui->object) && !$q_gui->object->isComplete())
286  {
287  $a_output = str_replace("{{{{{Question;il__qst_".$q_id."}}}}}",
288  "<i>".$lng->txt("cont_empty_question")."</i>",
289  $a_output);
290  }
291  }
292 
293  // this exports the questions which is needed below
294  $qhtml = $this->getQuestionJsOfPage(($a_mode == "edit") ? true : false, $a_mode);
295 
296  require_once './Modules/Scorm2004/classes/class.ilQuestionExporter.php';
297  $a_output = "<script>".ilQuestionExporter::questionsJS($q_ids)."</script>".$a_output;
298  if(!self::$initial_done)
299  {
300  $a_output = "<script>var ScormApi=null; var questions = new Array();</script>".$a_output;
301  self::$initial_done = true;
302  }
303  }
304  }
305  else
306  {
307  // set by T&A components
308  $qhtml = $this->getPage()->getPageConfig()->getQuestionHTML();
309 
310  // address #19788
311  if (!is_array($qhtml) || count($qhtml) == 0)
312  {
313  // #14154
314  $q_ids = $this->getPage()->getQuestionIds();
315  if(sizeof($q_ids))
316  {
317  foreach ($q_ids as $k)
318  {
319  $a_output = str_replace("{{{{{Question;il__qst_$k"."}}}}}", " ".$lng->txt("copg_questions_not_supported_here"), $a_output);
320  }
321  }
322  }
323  }
324 
325  if (is_array($qhtml))
326  {
327  foreach ($qhtml as $k => $h)
328  {
329  $a_output = str_replace("{{{{{Question;il__qst_$k"."}}}}}", " ".$h, $a_output);
330  }
331  }
332 
333  return $a_output;
334  }
335 
339  static function resetInitialState()
340  {
341  self::$initial_done = false;
342  }
343 
347  function getJavascriptFiles($a_mode)
348  {
349  $js_files = array();
350 
351  if ($this->getPage()->getPageConfig()->getEnableSelfAssessment())
352  {
353  $js_files[] = "./Modules/Scorm2004/scripts/questions/pure.js";
354  $js_files[] = "./Modules/Scorm2004/scripts/questions/question_handling.js";
355  $js_files[] = "Modules/TestQuestionPool/js/ilMatchingQuestion.js";
356 
357  global $DIC;
358  if( $DIC['ilBrowser']->isMobile() || $DIC['ilBrowser']->isIpad() )
359  {
360  $js_files[] = 'libs/bower/bower_components/jqueryui-touch-punch/jquery.ui.touch-punch.min.js';
361  }
362  }
363 
364  if (!$this->getPage()->getPageConfig()->getEnableSelfAssessmentScorm() && $a_mode != IL_PAGE_PREVIEW
365  && $a_mode != "offline")
366  {
367  $js_files[] = "./Services/COPage/js/ilCOPageQuestionHandler.js";
368  }
369 
370  return $js_files;
371  }
372 
376  function getCssFiles($a_mode)
377  {
378  if ($this->getPage()->getPageConfig()->getEnableSelfAssessment())
379  {
380  return array("./Modules/Scorm2004/templates/default/question_handling.css",
381  "Modules/TestQuestionPool/templates/default/test_javascript.css");
382  }
383  return array();
384  }
385 
389  function getOnloadCode($a_mode)
390  {
391  global $ilCtrl, $ilUser;
392 
393  $code = array();
394 
395  if ($this->getPage()->getPageConfig()->getEnableSelfAssessment())
396  {
397  if (!$this->getPage()->getPageConfig()->getEnableSelfAssessmentScorm() && $a_mode != IL_PAGE_PREVIEW
398  && $a_mode != "offline")
399  {
400  $ilCtrl->setParameterByClass(strtolower(get_class($this->getPage()))."gui", "page_id", $this->getPage()->getId());
401  $url = $ilCtrl->getLinkTargetByClass(strtolower(get_class($this->getPage()))."gui", "processAnswer", "", true, false);
402  $code[] = "ilCOPageQuestionHandler.initCallback('".$url."');";
403  }
404 
405  if ($this->getPage()->getPageConfig()->getDisableDefaultQuestionFeedback())
406  {
407  $code[] = "ilias.questions.default_feedback = false;";
408  }
409 
410  $code[] = self::getJSTextInitCode($this->getPage()->getPageConfig()->getLocalizationLanguage()).' il.COPagePres.updateQuestionOverviews();';
411  }
412 
413  $get_stored_tries = $this->getPage()->getPageConfig()->getUseStoredQuestionTries();
414  if ($get_stored_tries)
415  {
416  $q_ids = $this->getPage()->getQuestionIds();
417  if (count($q_ids) > 0)
418  {
419  foreach ($q_ids as $q_id)
420  {
421  include_once("./Services/COPage/classes/class.ilPageQuestionProcessor.php");
422  $as = ilPageQuestionProcessor::getAnswerStatus($q_id, $ilUser->getId());
423  $code[] = "ilias.questions.initAnswer(".$q_id.", ".(int) $as["try"].", ".($as["passed"] ? "true" : "null").");";
424  }
425  }
426  }
427  return $code;
428  }
429 
436  static function getJSTextInitCode($a_lang)
437  {
438  global $lng, $ilUser;
439 
440  if ($a_lang == "")
441  {
442  $a_lang = $ilUser->getLanguage();
443  }
444 
445  return
446  '
447  ilias.questions.txt.wrong_answers = "'.$lng->txtlng("content", "cont_wrong_answers", $a_lang).'";
448  ilias.questions.txt.wrong_answers_single = "'.$lng->txtlng("content", "cont_wrong_answers_single", $a_lang).'";
449  ilias.questions.txt.tries_remaining = "'.$lng->txtlng("content", "cont_tries_remaining", $a_lang).'";
450  ilias.questions.txt.please_try_again = "'.$lng->txtlng("content", "cont_please_try_again", $a_lang).'";
451  ilias.questions.txt.all_answers_correct = "'.$lng->txtlng("content", "cont_all_answers_correct", $a_lang).'";
452  ilias.questions.txt.enough_answers_correct = "'.$lng->txtlng("content", "cont_enough_answers_correct", $a_lang).'";
453  ilias.questions.txt.nr_of_tries_exceeded = "'.$lng->txtlng("content", "cont_nr_of_tries_exceeded", $a_lang).'";
454  ilias.questions.txt.correct_answers_shown = "'.$lng->txtlng("content", "cont_correct_answers_shown", $a_lang).'";
455  ilias.questions.txt.correct_answers_also = "'.$lng->txtlng("content", "cont_correct_answers_also", $a_lang).'";
456  ilias.questions.txt.correct_answer_also = "'.$lng->txtlng("content", "cont_correct_answer_also", $a_lang).'";
457  ilias.questions.txt.ov_all_correct = "'.$lng->txtlng("content", "cont_ov_all_correct", $a_lang).'";
458  ilias.questions.txt.ov_some_correct = "'.$lng->txtlng("content", "cont_ov_some_correct", $a_lang).'";
459  ilias.questions.txt.ov_wrong_answered = "'.$lng->txtlng("content", "cont_ov_wrong_answered", $a_lang).'";
460  ilias.questions.txt.please_select = "'.$lng->txtlng("content", "cont_please_select", $a_lang).'";
461  ilias.questions.txt.ov_preview = "'.$lng->txtlng("content", "cont_ov_preview", $a_lang).'";
462  ilias.questions.refresh_lang();
463  ';
464 
465  }
466 
470  function getQuestionJsOfPage($a_no_interaction, $a_mode)
471  {
472  require_once './Modules/Scorm2004/classes/class.ilQuestionExporter.php';
473  $q_ids = $this->getPage()->getQuestionIds();
474  $js = array();
475  if (count($q_ids) > 0)
476  {
477  foreach ($q_ids as $q_id)
478  {
479  $q_exporter = new ilQuestionExporter($a_no_interaction);
480  $image_path = null;
481  if ($a_mode == "offline")
482  {
483  if ($this->getPage()->getParentType() == "sahs")
484  {
485  $image_path = "./objects/";
486  }
487  if ($this->getPage()->getParentType() == "lm")
488  {
489  $image_path = "./assessment/0/".$q_id."/images/";
490  }
491  }
492 
493  $js[$q_id] = $q_exporter->exportQuestion($q_id, $image_path, $a_mode);
494  }
495  }
496  return $js;
497  }
498 
499 }
500 ?>
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
$h
$code
Definition: example_050.php:99
static beforePageDelete($a_page)
Before page is being deleted.
setType($a_type)
Set Type.
$url
Definition: shib_logout.php:72
static getLangVars()
Get lang vars needed for editing.
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.
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...
$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)
Create styles array
The data for the language used.
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 $lng
Definition: privfeed.php:17
global $ilDB
setQuestionReference($a_questionreference)
Set Question Reference.
global $DIC
getQuestionReference()
Get Question Reference.