ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  // we remove everything not supported by the non-tiny self
97  // assessment question editor
98  $q = $duplicate->getQuestion();
99 
100  // we try to save all latex tags
101  $try = true;
102  $ls = '<span class="latex">';
103  $le = '</span>';
104  while ($try)
105  {
106  // search position of start tag
107  $pos1 = strpos($q, $ls);
108  if (is_int($pos1))
109  {
110  $pos2 = strpos($q, $le, $pos1);
111  if (is_int($pos2))
112  {
113  // both found: replace end tag
114  $q = substr($q, 0, $pos2)."[/tex]".substr($q, $pos2+7);
115  $q = substr($q, 0, $pos1)."[tex]".substr($q, $pos1+20);
116  }
117  else
118  {
119  $try = false;
120  }
121  }
122  else
123  {
124  $try = false;
125  }
126  }
127 
129  $tstr = "";
130  foreach ($tags as $t)
131  {
132  $tstr.="<".$t.">";
133  }
134  $q = ilUtil::secureString($q, true, $tstr);
135  // self assessment uses nl2br, not p
136  $duplicate->setQuestion($q);
137 
138  $duplicate->saveQuestionDataToDb();
139 
140  $this->q_node->set_attribute("QRef", "il__qst_".$duplicate_id);
141  }
142 
147  static function getLangVars()
148  {
149  return array("ed_insert_pcqst", "empty_question", "pc_qst");
150  }
151 
160  static function afterPageUpdate($a_page, DOMDocument $a_domdoc, $a_xml, $a_creation)
161  {
162  global $ilDB;
163 
164  include_once("./Services/Link/classes/class.ilInternalLink.php");
165 
166  $ilDB->manipulateF("DELETE FROM page_question WHERE page_parent_type = %s ".
167  " AND page_id = %s AND page_lang = %s", array("text", "integer", "text"),
168  array($a_page->getParentType(), $a_page->getId(), $a_page->getLanguage()));
169 
170  $xpath = new DOMXPath($a_domdoc);
171  $nodes = $xpath->query('//Question');
172  $q_ids = array();
173  foreach ($nodes as $node)
174  {
175  $q_ref = $node->getAttribute("QRef");
176 
177  $inst_id = ilInternalLink::_extractInstOfTarget($q_ref);
178  if (!($inst_id > 0))
179  {
181  if ($q_id > 0)
182  {
183  $q_ids[$q_id] = $q_id;
184  }
185  }
186  }
187  foreach($q_ids as $qid)
188  {
189  $ilDB->manipulateF("INSERT INTO page_question (page_parent_type, page_id, page_lang, question_id)".
190  " VALUES (%s,%s,%s,%s)",
191  array("text", "integer", "text", "integer"),
192  array($a_page->getParentType(), $a_page->getId(), $a_page->getLanguage(), $qid));
193  }
194 
195  }
196 
202  static function beforePageDelete($a_page)
203  {
204  global $ilDB;
205 
206  $ilDB->manipulateF("DELETE FROM page_question WHERE page_parent_type = %s ".
207  " AND page_id = %s AND page_lang = %s", array("text", "integer", "text"),
208  array($a_page->getParentType(), $a_page->getId(), $a_page->getLanguage()));
209  }
210 
214  static function _getQuestionIdsForPage($a_parent_type, $a_page_id, $a_lang = "-")
215  {
216  global $ilDB;
217 
218  $res = $ilDB->queryF("SELECT * FROM page_question WHERE page_parent_type = %s ".
219  " AND page_id = %s AND page_lang = %s",
220  array("text", "integer", "text"),
221  array($a_parent_type, $a_page_id, $a_lang));
222  $q_ids = array();
223  while ($rec = $ilDB->fetchAssoc($res))
224  {
225  $q_ids[] = $rec["question_id"];
226  }
227 
228  return $q_ids;
229  }
230 
237  function _getPageForQuestionId($a_q_id, $a_parent_type = "")
238  {
239  global $ilDB;
240 
241  $set = $ilDB->query("SELECT * FROM page_question ".
242  " WHERE question_id = ".$ilDB->quote($a_q_id, "integer")
243  );
244  while ($rec = $ilDB->fetchAssoc($set))
245  {
246  if ($a_parent_type == "" || $rec["page_parent_type"] == $a_parent_type)
247  {
248  return array("page_id" => $rec["page_id"], "parent_type" => $rec["page_parent_type"]);
249  }
250  }
251  return false;
252  }
253 
260  function modifyPageContentPostXsl($a_output, $a_mode)
261  {
262  global $lng;
263 
264  if ($this->getPage()->getPageConfig()->getEnableSelfAssessment())
265  {
266  // #14154
267  $q_ids = $this->getPage()->getQuestionIds();
268  if(sizeof($q_ids))
269  {
270  include_once "./Modules/TestQuestionPool/classes/class.assQuestionGUI.php";
271  foreach($q_ids as $q_id)
272  {
273  $q_gui = assQuestionGUI::_getQuestionGUI("", $q_id);
274  // object check due to #16557
275  if(is_object($q_gui->object) && !$q_gui->object->isComplete())
276  {
277  $a_output = str_replace("{{{{{Question;il__qst_".$q_id."}}}}}",
278  "<i>".$lng->txt("cont_empty_question")."</i>",
279  $a_output);
280  }
281  }
282 
283  // this exports the questions which is needed below
284  $qhtml = $this->getQuestionJsOfPage(($a_mode == "edit") ? true : false, $a_mode);
285 
286  require_once './Modules/Scorm2004/classes/class.ilQuestionExporter.php';
287  $a_output = "<script>".ilQuestionExporter::questionsJS($q_ids)."</script>".$a_output;
288  if(!self::$initial_done)
289  {
290  $a_output = "<script>var ScormApi=null; var questions = new Array();</script>".$a_output;
291  self::$initial_done = true;
292  }
293  }
294  }
295  else
296  {
297  // set by T&A components
298  $qhtml = $this->getPage()->getPageConfig()->getQuestionHTML();
299  }
300 
301  if (is_array($qhtml))
302  {
303  foreach ($qhtml as $k => $h)
304  {
305  $a_output = str_replace("{{{{{Question;il__qst_$k"."}}}}}", " ".$h, $a_output);
306  }
307  }
308 
309  return $a_output;
310  }
311 
315  static function resetInitialState()
316  {
317  self::$initial_done = false;
318  }
319 
323  function getJavascriptFiles($a_mode)
324  {
325  $js_files = array();
326 
327  if ($this->getPage()->getPageConfig()->getEnableSelfAssessment())
328  {
329  $js_files[] = "./Modules/Scorm2004/scripts/questions/pure.js";
330  $js_files[] = "./Modules/Scorm2004/scripts/questions/question_handling.js";
331  $js_files[] = "Modules/TestQuestionPool/js/ilMatchingQuestion.js";
332  }
333 
334  if (!$this->getPage()->getPageConfig()->getEnableSelfAssessmentScorm() && $a_mode != IL_PAGE_PREVIEW
335  && $a_mode != "offline")
336  {
337  $js_files[] = "./Services/COPage/js/ilCOPageQuestionHandler.js";
338  }
339 
340  return $js_files;
341  }
342 
346  function getCssFiles($a_mode)
347  {
348  if ($this->getPage()->getPageConfig()->getEnableSelfAssessment())
349  {
350  return array("./Modules/Scorm2004/templates/default/question_handling.css",
351  "Modules/TestQuestionPool/templates/default/test_javascript.css");
352  }
353  return array();
354  }
355 
359  function getOnloadCode($a_mode)
360  {
361  global $ilCtrl, $ilUser;
362 
363  $code = array();
364 
365  if ($this->getPage()->getPageConfig()->getEnableSelfAssessment())
366  {
367  if (!$this->getPage()->getPageConfig()->getEnableSelfAssessmentScorm() && $a_mode != IL_PAGE_PREVIEW
368  && $a_mode != "offline")
369  {
370  $ilCtrl->setParameterByClass(strtolower(get_class($this->getPage()))."gui", "page_id", $this->getPage()->getId());
371  $url = $ilCtrl->getLinkTargetByClass(strtolower(get_class($this->getPage()))."gui", "processAnswer", "", true, false);
372  $code[] = "ilCOPageQuestionHandler.initCallback('".$url."');";
373  }
374 
375  if ($this->getPage()->getPageConfig()->getDisableDefaultQuestionFeedback())
376  {
377  $code[] = "ilias.questions.default_feedback = false;";
378  }
379 
380  $code[] = self::getJSTextInitCode($this->getPage()->getPageConfig()->getLocalizationLanguage()).' il.COPagePres.updateQuestionOverviews();';
381  }
382 
383  $get_stored_tries = $this->getPage()->getPageConfig()->getUseStoredQuestionTries();
384  if ($get_stored_tries)
385  {
386  $q_ids = $this->getPage()->getQuestionIds();
387  if (count($q_ids) > 0)
388  {
389  foreach ($q_ids as $q_id)
390  {
391  include_once("./Services/COPage/classes/class.ilPageQuestionProcessor.php");
392  $as = ilPageQuestionProcessor::getAnswerStatus($q_id, $ilUser->getId());
393  $code[] = "ilias.questions.initAnswer(".$q_id.", ".(int) $as["try"].", ".($as["passed"] ? "true" : "null").");";
394  }
395  }
396  }
397  return $code;
398  }
399 
406  static function getJSTextInitCode($a_lang)
407  {
408  global $lng, $ilUser;
409 
410  if ($a_lang == "")
411  {
412  $a_lang = $ilUser->getLanguage();
413  }
414 
415  return
416  '
417  ilias.questions.txt.wrong_answers = "'.$lng->txtlng("content", "cont_wrong_answers", $a_lang).'";
418  ilias.questions.txt.wrong_answers_single = "'.$lng->txtlng("content", "cont_wrong_answers_single", $a_lang).'";
419  ilias.questions.txt.tries_remaining = "'.$lng->txtlng("content", "cont_tries_remaining", $a_lang).'";
420  ilias.questions.txt.please_try_again = "'.$lng->txtlng("content", "cont_please_try_again", $a_lang).'";
421  ilias.questions.txt.all_answers_correct = "'.$lng->txtlng("content", "cont_all_answers_correct", $a_lang).'";
422  ilias.questions.txt.enough_answers_correct = "'.$lng->txtlng("content", "cont_enough_answers_correct", $a_lang).'";
423  ilias.questions.txt.nr_of_tries_exceeded = "'.$lng->txtlng("content", "cont_nr_of_tries_exceeded", $a_lang).'";
424  ilias.questions.txt.correct_answers_shown = "'.$lng->txtlng("content", "cont_correct_answers_shown", $a_lang).'";
425  ilias.questions.txt.correct_answers_also = "'.$lng->txtlng("content", "cont_correct_answers_also", $a_lang).'";
426  ilias.questions.txt.correct_answer_also = "'.$lng->txtlng("content", "cont_correct_answer_also", $a_lang).'";
427  ilias.questions.txt.ov_all_correct = "'.$lng->txtlng("content", "cont_ov_all_correct", $a_lang).'";
428  ilias.questions.txt.ov_some_correct = "'.$lng->txtlng("content", "cont_ov_some_correct", $a_lang).'";
429  ilias.questions.txt.ov_wrong_answered = "'.$lng->txtlng("content", "cont_ov_wrong_answered", $a_lang).'";
430  ilias.questions.txt.please_select = "'.$lng->txtlng("content", "cont_please_select", $a_lang).'";
431  ilias.questions.refresh_lang();
432  ';
433 
434  }
435 
439  function getQuestionJsOfPage($a_no_interaction, $a_mode)
440  {
441  require_once './Modules/Scorm2004/classes/class.ilQuestionExporter.php';
442  $q_ids = $this->getPage()->getQuestionIds();
443  $js = array();
444  if (count($q_ids) > 0)
445  {
446  foreach ($q_ids as $q_id)
447  {
448  $q_exporter = new ilQuestionExporter($a_no_interaction);
449  $image_path = null;
450  if ($a_mode == "offline")
451  {
452  if ($this->getPage()->getParentType() == "sahs")
453  {
454  $image_path = "./objects/";
455  }
456  if ($this->getPage()->getParentType() == "lm")
457  {
458  $image_path = "./assessment/0/".$q_id."/images/";
459  }
460  }
461 
462  $js[$q_id] = $q_exporter->exportQuestion($q_id, $image_path, $a_mode);
463  }
464  }
465  return $js;
466  }
467 
468 }
469 ?>