ILIAS  Release_4_4_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 
26  function init()
27  {
28  $this->setType("pcqst");
29  }
30 
34  function setNode(&$a_node)
35  {
36  parent::setNode($a_node); // this is the PageContent node
37  $this->q_node =& $a_node->first_child(); //... and this the Question
38  }
39 
45  function setQuestionReference($a_questionreference)
46  {
47  if (is_object($this->q_node))
48  {
49  $this->q_node->set_attribute("QRef", $a_questionreference);
50  }
51  }
52 
59  {
60  if (is_object($this->q_node))
61  {
62  return $this->q_node->get_attribute("QRef", $a_questionreference);
63  }
64  return false;
65  }
66 
70  function create(&$a_pg_obj, $a_hier_id)
71  {
72  $this->createPageContentNode();
73  $a_pg_obj->insertContent($this, $a_hier_id, IL_INSERT_AFTER);
74  $this->q_node = $this->dom->create_element("Question");
75  $this->q_node = $this->node->append_child($this->q_node);
76  $this->q_node->set_attribute("QRef", "");
77  }
78 
85  function copyPoolQuestionIntoPage($a_q_id, $a_hier_id)
86  {
87  include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
88  include_once "./Modules/TestQuestionPool/classes/class.assQuestionGUI.php";
89  $question = assQuestion::_instanciateQuestion($a_q_id);
90  $duplicate_id = $question->copyObject(0, $question->getTitle());
91  $duplicate = assQuestion::_instanciateQuestion($duplicate_id);
92  $duplicate->setObjId(0);
93 
94  // we remove everything not supported by the non-tiny self
95  // assessment question editor
96  $q = $duplicate->getQuestion();
97 
98  // we try to save all latex tags
99  $try = true;
100  $ls = '<span class="latex">';
101  $le = '</span>';
102  while ($try)
103  {
104  // search position of start tag
105  $pos1 = strpos($q, $ls);
106  if (is_int($pos1))
107  {
108  $pos2 = strpos($q, $le, $pos1);
109  if (is_int($pos2))
110  {
111  // both found: replace end tag
112  $q = substr($q, 0, $pos2)."[/tex]".substr($q, $pos2+7);
113  $q = substr($q, 0, $pos1)."[tex]".substr($q, $pos1+20);
114  }
115  else
116  {
117  $try = false;
118  }
119  }
120  else
121  {
122  $try = false;
123  }
124  }
125 
127  $tstr = "";
128  foreach ($tags as $t)
129  {
130  $tstr.="<".$t.">";
131  }
132  $q = ilUtil::secureString($q, true, $tstr);
133  // self assessment uses nl2br, not p
134  $duplicate->setQuestion($q);
135 
136  $duplicate->saveQuestionDataToDb();
137 
138  $this->q_node->set_attribute("QRef", "il__qst_".$duplicate_id);
139  }
140 
145  static function getLangVars()
146  {
147  return array("ed_insert_pcqst", "empty_question", "pc_qst");
148  }
149 
158  static function afterPageUpdate($a_page, DOMDocument $a_domdoc, $a_xml, $a_creation)
159  {
160  global $ilDB;
161 
162  include_once("./Services/COPage/classes/class.ilInternalLink.php");
163 
164  $ilDB->manipulateF("DELETE FROM page_question WHERE page_parent_type = %s ".
165  " AND page_id = %s AND page_lang = %s", array("text", "integer", "text"),
166  array($a_page->getParentType(), $a_page->getId(), $a_page->getLanguage()));
167 
168  $xpath = new DOMXPath($a_domdoc);
169  $nodes = $xpath->query('//Question');
170  $q_ids = array();
171  foreach ($nodes as $node)
172  {
173  $q_ref = $node->getAttribute("QRef");
174 
175  $inst_id = ilInternalLink::_extractInstOfTarget($q_ref);
176  if (!($inst_id > 0))
177  {
179  if ($q_id > 0)
180  {
181  $q_ids[$q_id] = $q_id;
182  }
183  }
184  }
185  foreach($q_ids as $qid)
186  {
187  $ilDB->manipulateF("INSERT INTO page_question (page_parent_type, page_id, page_lang, question_id)".
188  " VALUES (%s,%s,%s,%s)",
189  array("text", "integer", "text", "integer"),
190  array($a_page->getParentType(), $a_page->getId(), $a_page->getLanguage(), $qid));
191  }
192 
193  }
194 
200  static function beforePageDelete($a_page)
201  {
202  global $ilDB;
203 
204  $ilDB->manipulateF("DELETE FROM page_question WHERE page_parent_type = %s ".
205  " AND page_id = %s AND page_lang = %s", array("text", "integer", "text"),
206  array($a_page->getParentType(), $a_page->getId(), $a_page->getLanguage()));
207  }
208 
212  static function _getQuestionIdsForPage($a_parent_type, $a_page_id, $a_lang = "-")
213  {
214  global $ilDB;
215 
216  $res = $ilDB->queryF("SELECT * FROM page_question WHERE page_parent_type = %s ".
217  " AND page_id = %s AND page_lang = %s",
218  array("text", "integer", "text"),
219  array($a_parent_type, $a_page_id, $a_lang));
220  $q_ids = array();
221  while ($rec = $ilDB->fetchAssoc($res))
222  {
223  $q_ids[] = $rec["question_id"];
224  }
225 
226  return $q_ids;
227  }
228 
235  function _getPageForQuestionId($a_q_id, $a_parent_type = "")
236  {
237  global $ilDB;
238 
239  $set = $ilDB->query("SELECT * FROM page_question ".
240  " WHERE question_id = ".$ilDB->quote($a_q_id, "integer")
241  );
242  while ($rec = $ilDB->fetchAssoc($set))
243  {
244  if ($a_parent_type == "" || $rec["page_parent_type"] == $a_parent_type)
245  {
246  return array("page_id" => $rec["page_id"], "parent_type" => $rec["page_parent_type"]);
247  }
248  }
249  return false;
250  }
251 
252 
253 }
254 ?>