ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assFlashQuestion.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once './Modules/TestQuestionPool/classes/class.assQuestion.php';
5 require_once './Modules/Test/classes/inc.AssessmentConstants.php';
6 require_once './Modules/TestQuestionPool/interfaces/interface.ilObjQuestionScoringAdjustable.php';
7 require_once './Modules/TestQuestionPool/interfaces/interface.iQuestionCondition.php';
8 
21 {
22  private $width;
23  private $height;
24  private $parameters;
25  private $applet;
26 
40  function __construct(
41  $title = "",
42  $comment = "",
43  $author = "",
44  $owner = -1,
45  $question = ""
46  )
47  {
49  $this->parameters = array();
50  $this->width = 540;
51  $this->height = 400;
52  $this->applet = "";
53  }
54 
61  function isComplete()
62  {
63  if (strlen($this->title)
64  && ($this->author)
65  && ($this->question)
66  && ($this->getMaximumPoints() > 0)
67  && (strlen($this->getApplet()))
68  )
69  {
70  return true;
71  }
72  return false;
73  }
74 
80  function saveToDb($original_id = "")
81  {
85  }
86 
88  {
89  global $ilDB;
90  $ilDB->manipulateF( "DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
91  array( "integer" ),
92  array( $this->getId() )
93  );
94  $ilDB->manipulateF( "INSERT INTO " . $this->getAdditionalTableName(
95  ) . " (question_fi, width, height, applet, params) VALUES (%s, %s, %s, %s, %s)",
96  array( "integer", "integer", "integer", "text", "text" ),
97  array(
98  $this->getId(),
99  (strlen( $this->getWidth() )) ? $this->getWidth() : 550,
100  (strlen( $this->getHeight() )) ? $this->getHeight() : 400,
101  $this->getApplet(),
102  serialize( $this->getParameters() )
103  )
104  );
105  if ($_SESSION["flash_upload_filename"])
106  {
107  $path = $this->getFlashPath();
109  @rename( $_SESSION["flash_upload_filename"], $path . $this->getApplet() );
110  unset($_SESSION["flash_upload_filename"]);
111  }
112  }
113 
121  function loadFromDb($question_id)
122  {
123  global $ilDB;
124  $result = $ilDB->queryF("SELECT qpl_questions.*, " . $this->getAdditionalTableName() . ".* FROM qpl_questions LEFT JOIN " . $this->getAdditionalTableName() . " ON " . $this->getAdditionalTableName() . ".question_fi = qpl_questions.question_id WHERE qpl_questions.question_id = %s",
125  array("integer"),
126  array($question_id)
127  );
128  if ($result->numRows() == 1)
129  {
130  $data = $ilDB->fetchAssoc($result);
131  $this->setId($question_id);
132  $this->setNrOfTries($data['nr_of_tries']);
133  $this->setTitle($data["title"]);
134  $this->setComment($data["description"]);
135  $this->setSuggestedSolution($data["solution_hint"]);
136  $this->setOriginalId($data["original_id"]);
137  $this->setObjId($data["obj_fi"]);
138  $this->setAuthor($data["author"]);
139  $this->setOwner($data["owner"]);
140  $this->setPoints($data["points"]);
141 
142  include_once("./Services/RTE/classes/class.ilRTE.php");
143  $this->setQuestion(ilRTE::_replaceMediaObjectImageSrc($data["question_text"], 1));
144  $this->setEstimatedWorkingTime(substr($data["working_time"], 0, 2), substr($data["working_time"], 3, 2), substr($data["working_time"], 6, 2));
145 
146  try
147  {
148  $this->setAdditionalContentEditingMode($data['add_cont_edit_mode']);
149  }
151  {
152  }
153 
154  // load additional data
155  $result = $ilDB->queryF("SELECT * FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
156  array("integer"),
157  array($question_id)
158  );
159  if ($result->numRows() == 1)
160  {
161  $data = $ilDB->fetchAssoc($result);
162  $this->setWidth($data["width"]);
163  $this->setHeight($data["height"]);
164  $this->setApplet($data["applet"]);
165  $this->parameters = unserialize($data["params"]);
166  if (!is_array($this->parameters)) $this->clearParameters();
167  unset($_SESSION["flash_upload_filename"]);
168  }
169  }
170  parent::loadFromDb($question_id);
171  }
172 
180  function duplicate($for_test = true, $title = "", $author = "", $owner = "", $testObjId = null)
181  {
182  if ($this->id <= 0)
183  {
184  // The question has not been saved. It cannot be duplicated
185  return;
186  }
187  // duplicate the question in database
188  $this_id = $this->getId();
189  $thisObjId = $this->getObjId();
190 
191  $clone = $this;
192  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
194  $clone->id = -1;
195 
196  if( (int)$testObjId > 0 )
197  {
198  $clone->setObjId($testObjId);
199  }
200 
201  if ($title)
202  {
203  $clone->setTitle($title);
204  }
205 
206  if ($author)
207  {
208  $clone->setAuthor($author);
209  }
210  if ($owner)
211  {
212  $clone->setOwner($owner);
213  }
214 
215  if ($for_test)
216  {
217  $clone->saveToDb($original_id);
218  }
219  else
220  {
221  $clone->saveToDb();
222  }
223 
224  // copy question page content
225  $clone->copyPageOfQuestion($this_id);
226  // copy XHTML media objects
227  $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
228  // duplicate the applet
229  $clone->duplicateApplet($this_id, $thisObjId);
230 
231  $clone->onDuplicate($thisObjId, $this_id, $clone->getObjId(), $clone->getId());
232 
233  return $clone->id;
234  }
235 
243  function copyObject($target_questionpool_id, $title = "")
244  {
245  if ($this->id <= 0)
246  {
247  // The question has not been saved. It cannot be duplicated
248  return;
249  }
250  // duplicate the question in database
251  $clone = $this;
252  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
254  $clone->id = -1;
255  $source_questionpool_id = $this->getObjId();
256  $clone->setObjId($target_questionpool_id);
257  if ($title)
258  {
259  $clone->setTitle($title);
260  }
261  $clone->saveToDb();
262 
263  // copy question page content
264  $clone->copyPageOfQuestion($original_id);
265  // copy XHTML media objects
266  $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
267  // duplicate the applet
268  $clone->copyApplet($original_id, $source_questionpool_id);
269 
270  $clone->onCopy($source_questionpool_id, $original_id, $clone->getObjId(), $clone->getId());
271 
272  return $clone->id;
273  }
274 
275  public function createNewOriginalFromThisDuplicate($targetParentId, $targetQuestionTitle = "")
276  {
277  if ($this->id <= 0)
278  {
279  // The question has not been saved. It cannot be duplicated
280  return;
281  }
282 
283  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
284 
285  $sourceQuestionId = $this->id;
286  $sourceParentId = $this->getObjId();
287 
288  // duplicate the question in database
289  $clone = $this;
290  $clone->id = -1;
291 
292  $clone->setObjId($targetParentId);
293 
294  if ($targetQuestionTitle)
295  {
296  $clone->setTitle($targetQuestionTitle);
297  }
298 
299  $clone->saveToDb();
300  // copy question page content
301  $clone->copyPageOfQuestion($sourceQuestionId);
302  // copy XHTML media objects
303  $clone->copyXHTMLMediaObjectsOfQuestion($sourceQuestionId);
304  // duplicate the applet
305  $clone->copyApplet($sourceQuestionId, $sourceParentId);
306 
307  $clone->onCopy($sourceParentId, $sourceQuestionId, $clone->getObjId(), $clone->getId());
308 
309  return $clone->id;
310  }
311 
318  protected function duplicateApplet($question_id, $objectId = null)
319  {
320  $flashpath = $this->getFlashPath();
321  $flashpath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $flashpath);
322 
323  if( (int)$objectId > 0 )
324  {
325  $flashpath_original = str_replace("/$this->obj_id/", "/$objectId/", $flashpath_original);
326  }
327 
328  if (!file_exists($flashpath))
329  {
330  ilUtil::makeDirParents($flashpath);
331  }
332  $filename = $this->getApplet();
333  if (!copy($flashpath_original . $filename, $flashpath . $filename)) {
334  print "flash applet could not be duplicated!!!! ";
335  }
336  }
337 
344  protected function copyApplet($question_id, $source_questionpool)
345  {
346  $flashpath = $this->getFlashPath();
347  $flashpath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $flashpath);
348  $flashpath_original = str_replace("/$this->obj_id/", "/$source_questionpool/", $flashpath_original);
349  if (!file_exists($flashpath))
350  {
351  ilUtil::makeDirParents($flashpath);
352  }
353  $filename = $this->getApplet();
354  if (!copy($flashpath_original . $filename, $flashpath . $filename))
355  {
356  print "flash applet could not be copied!!!! ";
357  }
358  }
359 
366  function getMaximumPoints()
367  {
368  return $this->points;
369  }
370 
381  public function calculateReachedPoints($active_id, $pass = NULL, $returndetails = FALSE)
382  {
383  if( $returndetails )
384  {
385  throw new ilTestException('return details not implemented for '.__METHOD__);
386  }
387 
388  global $ilDB;
389 
390  $found_values = array();
391  if (is_null($pass))
392  {
393  $pass = $this->getSolutionMaxPass($active_id);
394  }
395  $result = $ilDB->queryF("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
396  array("integer", "integer", "integer"),
397  array($active_id, $this->getId(), $pass)
398  );
399 
400  $points = 0;
401  while ($data = $ilDB->fetchAssoc($result))
402  {
403  $points += $data["points"];
404  }
405 
406  return $points;
407  }
408 
410  {
411  $points = 0;
412  foreach($previewSession->getParticipantsSolution() as $solution)
413  {
414  if( isset($solution['points']) )
415  {
416  $points += $solution['points'];
417  }
418  }
419  return $points;
420  }
421 
422  function sendToHost($url, $data, $optional_headers = null)
423  {
424  $params = array('http' => array(
425  'method' => 'POST',
426  'content' => $data
427  ));
428  if ($optional_headers !== null)
429  {
430  $params['http']['header'] = $optional_headers;
431  }
432  $ctx = stream_context_create($params);
433  $fp = @fopen($url, 'rb', false, $ctx);
434  if (!$fp)
435  {
436  throw new Exception("Problem with $url, $php_errormsg");
437  }
438  $response = @stream_get_contents($fp);
439  if ($response === false)
440  {
441  throw new Exception("Problem reading data from $url, $php_errormsg");
442  }
443  return $response;
444  }
445 
454  function moveUploadedFile($tmpfile, $flashfile)
455  {
456  $result = "";
457  if (!empty($tmpfile))
458  {
459  $flashfile = str_replace(" ", "_", $flashfile);
460  $flashpath = $this->getFlashPath();
461  if (!file_exists($flashpath))
462  {
463  ilUtil::makeDirParents($flashpath);
464  }
465  if (ilUtil::moveUploadedFile($tmpfile, $flashfile, $flashpath.$flashfile))
466  {
467  $result = $flashfile;
468  }
469  }
470  return $result;
471  }
472 
473  function deleteApplet()
474  {
475  @unlink($this->getFlashPath() . $this->getApplet());
476  $this->applet = "";
477  }
478 
487  public function saveWorkingData($active_id, $pass = NULL)
488  {
489  // nothing to save!
490 
491  //$this->getProcessLocker()->requestUserSolutionUpdateLock();
492  // store in tst_solutions
493  //$this->getProcessLocker()->releaseUserSolutionUpdateLock();
494 
495  return true;
496  }
497 
498  protected function savePreviewData(ilAssQuestionPreviewSession $previewSession)
499  {
500  // nothing to save!
501 
502  return true;
503  }
504 
513  protected function reworkWorkingData($active_id, $pass, $obligationsAnswered)
514  {
515  // nothing to rework!
516  }
517 
526  function getQuestionType()
527  {
528  return "assFlashQuestion";
529  }
530 
540  {
541  return "qpl_qst_flash";
542  }
543 
553  {
554  return "";
555  }
556 
563  function deleteAnswers($question_id)
564  {
565  }
566 
572  {
574  return $text;
575  }
576 
589  public function setExportDetailsXLS(&$worksheet, $startrow, $active_id, $pass, &$format_title, &$format_bold)
590  {
591  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
592  $worksheet->writeString($startrow, 0, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())), $format_title);
593  $worksheet->writeString($startrow, 1, ilExcelUtils::_convert_text($this->getTitle()), $format_title);
594  return $startrow + 1;
595  }
596 
610  function fromXML(&$item, &$questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
611  {
612  include_once "./Modules/TestQuestionPool/classes/import/qti12/class.assFlashQuestionImport.php";
613  $import = new assFlashQuestionImport($this);
614  $import->fromXML($item, $questionpool_id, $tst_id, $tst_object, $question_counter, $import_mapping);
615  }
616 
624  function toXML($a_include_header = true, $a_include_binary = true, $a_shuffle = false, $test_output = false, $force_image_references = false)
625  {
626  include_once "./Modules/TestQuestionPool/classes/export/qti12/class.assFlashQuestionExport.php";
627  $export = new assFlashQuestionExport($this);
628  return $export->toXML($a_include_header, $a_include_binary, $a_shuffle, $test_output, $force_image_references);
629  }
630 
637  public function getBestSolution($active_id, $pass)
638  {
639  $user_solution = array();
640  return $user_solution;
641  }
642 
643  public function setHeight($a_height)
644  {
645  if (!$a_height) $a_height = 400;
646  $this->height = $a_height;
647  }
648 
649  public function getHeight()
650  {
651  return $this->height;
652  }
653 
654  public function setWidth($a_width)
655  {
656  if (!$a_width) $a_width = 550;
657  $this->width = $a_width;
658  }
659 
660  public function getWidth()
661  {
662  return $this->width;
663  }
664 
665  public function setApplet($a_applet)
666  {
667  $this->applet = $a_applet;
668  }
669 
670  public function getApplet()
671  {
672  return $this->applet;
673  }
674 
675  public function addParameter($name, $value)
676  {
677  $this->parameters[$name] = $value;
678  }
679 
680  public function setParameters($params)
681  {
682  if (is_array($params))
683  {
684  $this->parameters = $params;
685  }
686  else
687  {
688  $this->parameters = array();
689  }
690  }
691 
692  public function removeParameter($name)
693  {
694  unset($this->parameters[$name]);
695  }
696 
697  public function clearParameters()
698  {
699  $this->parameters = array();
700  }
701 
702  public function getParameters()
703  {
704  return $this->parameters;
705  }
706 
707  public function isAutosaveable()
708  {
709  return FALSE;
710  }
711 
720  public function getOperators($expression)
721  {
722  require_once "./Modules/TestQuestionPool/classes/class.ilOperatorsExpressionMapping.php";
724  }
725 
730  public function getExpressionTypes()
731  {
732  return array(
736  );
737  }
738 
747  public function getUserQuestionResult($active_id, $pass)
748  {
749  // TODO: Implement getUserQuestionResult() method.
750  }
751 
760  public function getAvailableAnswerOptions($index = null)
761  {
762  // TODO: Implement getAvailableAnswerOptions() method.
763  }
764 }