ILIAS  Release_4_4_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 
19 class assFlashQuestion extends assQuestion //implements ilObjQuestionScoringAdjustable
20 {
21  private $width;
22  private $height;
23  private $parameters;
24  private $applet;
25 
39  function __construct(
40  $title = "",
41  $comment = "",
42  $author = "",
43  $owner = -1,
44  $question = ""
45  )
46  {
48  $this->parameters = array();
49  $this->width = 540;
50  $this->height = 400;
51  $this->applet = "";
52  }
53 
60  function isComplete()
61  {
62  if (strlen($this->title)
63  && ($this->author)
64  && ($this->question)
65  && ($this->getMaximumPoints() > 0)
66  && (strlen($this->getApplet()))
67  )
68  {
69  return true;
70  }
71  return false;
72  }
73 
79  function saveToDb($original_id = "")
80  {
84  }
85 
87  {
88  global $ilDB;
89  $ilDB->manipulateF( "DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
90  array( "integer" ),
91  array( $this->getId() )
92  );
93  $ilDB->manipulateF( "INSERT INTO " . $this->getAdditionalTableName(
94  ) . " (question_fi, width, height, applet, params) VALUES (%s, %s, %s, %s, %s)",
95  array( "integer", "integer", "integer", "text", "text" ),
96  array(
97  $this->getId(),
98  (strlen( $this->getWidth() )) ? $this->getWidth() : 550,
99  (strlen( $this->getHeight() )) ? $this->getHeight() : 400,
100  $this->getApplet(),
101  serialize( $this->getParameters() )
102  )
103  );
104  if ($_SESSION["flash_upload_filename"])
105  {
106  $path = $this->getFlashPath();
108  @rename( $_SESSION["flash_upload_filename"], $path . $this->getApplet() );
109  unset($_SESSION["flash_upload_filename"]);
110  }
111  }
112 
120  function loadFromDb($question_id)
121  {
122  global $ilDB;
123  $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",
124  array("integer"),
125  array($question_id)
126  );
127  if ($result->numRows() == 1)
128  {
129  $data = $ilDB->fetchAssoc($result);
130  $this->setId($question_id);
131  $this->setNrOfTries($data['nr_of_tries']);
132  $this->setTitle($data["title"]);
133  $this->setComment($data["description"]);
134  $this->setSuggestedSolution($data["solution_hint"]);
135  $this->setOriginalId($data["original_id"]);
136  $this->setObjId($data["obj_fi"]);
137  $this->setAuthor($data["author"]);
138  $this->setOwner($data["owner"]);
139  $this->setPoints($data["points"]);
140 
141  include_once("./Services/RTE/classes/class.ilRTE.php");
142  $this->setQuestion(ilRTE::_replaceMediaObjectImageSrc($data["question_text"], 1));
143  $this->setEstimatedWorkingTime(substr($data["working_time"], 0, 2), substr($data["working_time"], 3, 2), substr($data["working_time"], 6, 2));
144 
145  try
146  {
147  $this->setAdditionalContentEditingMode($data['add_cont_edit_mode']);
148  }
150  {
151  }
152 
153  // load additional data
154  $result = $ilDB->queryF("SELECT * FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
155  array("integer"),
156  array($question_id)
157  );
158  if ($result->numRows() == 1)
159  {
160  $data = $ilDB->fetchAssoc($result);
161  $this->setWidth($data["width"]);
162  $this->setHeight($data["height"]);
163  $this->setApplet($data["applet"]);
164  $this->parameters = unserialize($data["params"]);
165  if (!is_array($this->parameters)) $this->clearParameters();
166  unset($_SESSION["flash_upload_filename"]);
167  }
168  }
169  parent::loadFromDb($question_id);
170  }
171 
179  function duplicate($for_test = true, $title = "", $author = "", $owner = "", $testObjId = null)
180  {
181  if ($this->id <= 0)
182  {
183  // The question has not been saved. It cannot be duplicated
184  return;
185  }
186  // duplicate the question in database
187  $this_id = $this->getId();
188  $thisObjId = $this->getObjId();
189 
190  $clone = $this;
191  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
193  $clone->id = -1;
194 
195  if( (int)$testObjId > 0 )
196  {
197  $clone->setObjId($testObjId);
198  }
199 
200  if ($title)
201  {
202  $clone->setTitle($title);
203  }
204 
205  if ($author)
206  {
207  $clone->setAuthor($author);
208  }
209  if ($owner)
210  {
211  $clone->setOwner($owner);
212  }
213 
214  if ($for_test)
215  {
216  $clone->saveToDb($original_id);
217  }
218  else
219  {
220  $clone->saveToDb();
221  }
222 
223  // copy question page content
224  $clone->copyPageOfQuestion($this_id);
225  // copy XHTML media objects
226  $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
227  // duplicate the applet
228  $clone->duplicateApplet($this_id, $thisObjId);
229 
230  $clone->onDuplicate($thisObjId, $this_id, $clone->getObjId(), $clone->getId());
231 
232  return $clone->id;
233  }
234 
242  function copyObject($target_questionpool_id, $title = "")
243  {
244  if ($this->id <= 0)
245  {
246  // The question has not been saved. It cannot be duplicated
247  return;
248  }
249  // duplicate the question in database
250  $clone = $this;
251  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
253  $clone->id = -1;
254  $source_questionpool_id = $this->getObjId();
255  $clone->setObjId($target_questionpool_id);
256  if ($title)
257  {
258  $clone->setTitle($title);
259  }
260  $clone->saveToDb();
261 
262  // copy question page content
263  $clone->copyPageOfQuestion($original_id);
264  // copy XHTML media objects
265  $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
266  // duplicate the applet
267  $clone->copyApplet($original_id, $source_questionpool_id);
268 
269  $clone->onCopy($source_questionpool_id, $original_id, $clone->getObjId(), $clone->getId());
270 
271  return $clone->id;
272  }
273 
274  public function createNewOriginalFromThisDuplicate($targetParentId, $targetQuestionTitle = "")
275  {
276  if ($this->id <= 0)
277  {
278  // The question has not been saved. It cannot be duplicated
279  return;
280  }
281 
282  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
283 
284  $sourceQuestionId = $this->id;
285  $sourceParentId = $this->getObjId();
286 
287  // duplicate the question in database
288  $clone = $this;
289  $clone->id = -1;
290 
291  $clone->setObjId($targetParentId);
292 
293  if ($targetQuestionTitle)
294  {
295  $clone->setTitle($targetQuestionTitle);
296  }
297 
298  $clone->saveToDb();
299  // copy question page content
300  $clone->copyPageOfQuestion($sourceQuestionId);
301  // copy XHTML media objects
302  $clone->copyXHTMLMediaObjectsOfQuestion($sourceQuestionId);
303  // duplicate the applet
304  $clone->copyApplet($sourceQuestionId, $sourceParentId);
305 
306  $clone->onCopy($sourceParentId, $sourceQuestionId, $clone->getObjId(), $clone->getId());
307 
308  return $clone->id;
309  }
310 
317  protected function duplicateApplet($question_id, $objectId = null)
318  {
319  $flashpath = $this->getFlashPath();
320  $flashpath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $flashpath);
321 
322  if( (int)$objectId > 0 )
323  {
324  $flashpath_original = str_replace("/$this->obj_id/", "/$objectId/", $flashpath_original);
325  }
326 
327  if (!file_exists($flashpath))
328  {
329  ilUtil::makeDirParents($flashpath);
330  }
331  $filename = $this->getApplet();
332  if (!copy($flashpath_original . $filename, $flashpath . $filename)) {
333  print "flash applet could not be duplicated!!!! ";
334  }
335  }
336 
343  protected function copyApplet($question_id, $source_questionpool)
344  {
345  $flashpath = $this->getFlashPath();
346  $flashpath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $flashpath);
347  $flashpath_original = str_replace("/$this->obj_id/", "/$source_questionpool/", $flashpath_original);
348  if (!file_exists($flashpath))
349  {
350  ilUtil::makeDirParents($flashpath);
351  }
352  $filename = $this->getApplet();
353  if (!copy($flashpath_original . $filename, $flashpath . $filename))
354  {
355  print "flash applet could not be copied!!!! ";
356  }
357  }
358 
365  function getMaximumPoints()
366  {
367  return $this->points;
368  }
369 
380  public function calculateReachedPoints($active_id, $pass = NULL, $returndetails = FALSE)
381  {
382  if( $returndetails )
383  {
384  throw new ilTestException('return details not implemented for '.__METHOD__);
385  }
386 
387  global $ilDB;
388 
389  $found_values = array();
390  if (is_null($pass))
391  {
392  $pass = $this->getSolutionMaxPass($active_id);
393  }
394  $result = $ilDB->queryF("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
395  array("integer", "integer", "integer"),
396  array($active_id, $this->getId(), $pass)
397  );
398 
399  $points = 0;
400  while ($data = $ilDB->fetchAssoc($result))
401  {
402  $points += $data["points"];
403  }
404 
405  return $points;
406  }
407 
408  function sendToHost($url, $data, $optional_headers = null)
409  {
410  $params = array('http' => array(
411  'method' => 'POST',
412  'content' => $data
413  ));
414  if ($optional_headers !== null)
415  {
416  $params['http']['header'] = $optional_headers;
417  }
418  $ctx = stream_context_create($params);
419  $fp = @fopen($url, 'rb', false, $ctx);
420  if (!$fp)
421  {
422  throw new Exception("Problem with $url, $php_errormsg");
423  }
424  $response = @stream_get_contents($fp);
425  if ($response === false)
426  {
427  throw new Exception("Problem reading data from $url, $php_errormsg");
428  }
429  return $response;
430  }
431 
440  function moveUploadedFile($tmpfile, $flashfile)
441  {
442  $result = "";
443  if (!empty($tmpfile))
444  {
445  $flashfile = str_replace(" ", "_", $flashfile);
446  $flashpath = $this->getFlashPath();
447  if (!file_exists($flashpath))
448  {
449  ilUtil::makeDirParents($flashpath);
450  }
451  if (ilUtil::moveUploadedFile($tmpfile, $flashfile, $flashpath.$flashfile))
452  {
453  $result = $flashfile;
454  }
455  }
456  return $result;
457  }
458 
459  function deleteApplet()
460  {
461  @unlink($this->getFlashPath() . $this->getApplet());
462  $this->applet = "";
463  }
464 
473  public function saveWorkingData($active_id, $pass = NULL)
474  {
475  // nothing to save!
476 
477  //$this->getProcessLocker()->requestUserSolutionUpdateLock();
478  // store in tst_solutions
479  //$this->getProcessLocker()->releaseUserSolutionUpdateLock();
480 
481  return true;
482  }
483 
492  protected function reworkWorkingData($active_id, $pass, $obligationsAnswered)
493  {
494  // nothing to rework!
495  }
496 
505  function getQuestionType()
506  {
507  return "assFlashQuestion";
508  }
509 
519  {
520  return "qpl_qst_flash";
521  }
522 
532  {
533  return "";
534  }
535 
542  function deleteAnswers($question_id)
543  {
544  }
545 
551  {
553  return $text;
554  }
555 
568  public function setExportDetailsXLS(&$worksheet, $startrow, $active_id, $pass, &$format_title, &$format_bold)
569  {
570  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
571  $worksheet->writeString($startrow, 0, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())), $format_title);
572  $worksheet->writeString($startrow, 1, ilExcelUtils::_convert_text($this->getTitle()), $format_title);
573  return $startrow + 1;
574  }
575 
589  function fromXML(&$item, &$questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
590  {
591  include_once "./Modules/TestQuestionPool/classes/import/qti12/class.assFlashQuestionImport.php";
592  $import = new assFlashQuestionImport($this);
593  $import->fromXML($item, $questionpool_id, $tst_id, $tst_object, $question_counter, $import_mapping);
594  }
595 
603  function toXML($a_include_header = true, $a_include_binary = true, $a_shuffle = false, $test_output = false, $force_image_references = false)
604  {
605  include_once "./Modules/TestQuestionPool/classes/export/qti12/class.assFlashQuestionExport.php";
606  $export = new assFlashQuestionExport($this);
607  return $export->toXML($a_include_header, $a_include_binary, $a_shuffle, $test_output, $force_image_references);
608  }
609 
616  public function getBestSolution($active_id, $pass)
617  {
618  $user_solution = array();
619  return $user_solution;
620  }
621 
622  public function setHeight($a_height)
623  {
624  if (!$a_height) $a_height = 400;
625  $this->height = $a_height;
626  }
627 
628  public function getHeight()
629  {
630  return $this->height;
631  }
632 
633  public function setWidth($a_width)
634  {
635  if (!$a_width) $a_width = 550;
636  $this->width = $a_width;
637  }
638 
639  public function getWidth()
640  {
641  return $this->width;
642  }
643 
644  public function setApplet($a_applet)
645  {
646  $this->applet = $a_applet;
647  }
648 
649  public function getApplet()
650  {
651  return $this->applet;
652  }
653 
654  public function addParameter($name, $value)
655  {
656  $this->parameters[$name] = $value;
657  }
658 
659  public function setParameters($params)
660  {
661  if (is_array($params))
662  {
663  $this->parameters = $params;
664  }
665  else
666  {
667  $this->parameters = array();
668  }
669  }
670 
671  public function removeParameter($name)
672  {
673  unset($this->parameters[$name]);
674  }
675 
676  public function clearParameters()
677  {
678  $this->parameters = array();
679  }
680 
681  public function getParameters()
682  {
683  return $this->parameters;
684  }
685 
686  public function isAutosaveable()
687  {
688  return FALSE;
689  }
690 }
691 
692 ?>