ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assFlashQuestion.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
6 include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
7 
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) and ($this->author) and ($this->question) and ($this->getMaximumPoints() > 0) and (strlen($this->getApplet())))
63  {
64  return true;
65  }
66  else
67  {
68  return false;
69  }
70  }
71 
77  function saveToDb($original_id = "")
78  {
79  global $ilDB, $ilLog;
80 
82 
83  // save additional data
84  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
85  array("integer"),
86  array($this->getId())
87  );
88  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, width, height, applet, params) VALUES (%s, %s, %s, %s, %s)",
89  array("integer", "integer", "integer", "text", "text"),
90  array(
91  $this->getId(),
92  (strlen($this->getWidth())) ? $this->getWidth() : 550,
93  (strlen($this->getHeight())) ? $this->getHeight() : 400,
94  $this->getApplet(),
95  serialize($this->getParameters())
96  )
97  );
98  if ($_SESSION["flash_upload_filename"])
99  {
100  $path = $this->getFlashPath();
102  @rename($_SESSION["flash_upload_filename"], $path . $this->getApplet());
103  unset($_SESSION["flash_upload_filename"]);
104  }
105 
107  }
108 
116  function loadFromDb($question_id)
117  {
118  global $ilDB;
119  $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",
120  array("integer"),
121  array($question_id)
122  );
123  if ($result->numRows() == 1)
124  {
125  $data = $ilDB->fetchAssoc($result);
126  $this->setId($question_id);
127  $this->setNrOfTries($data['nr_of_tries']);
128  $this->setTitle($data["title"]);
129  $this->setComment($data["description"]);
130  $this->setSuggestedSolution($data["solution_hint"]);
131  $this->setOriginalId($data["original_id"]);
132  $this->setObjId($data["obj_fi"]);
133  $this->setAuthor($data["author"]);
134  $this->setOwner($data["owner"]);
135  $this->setPoints($data["points"]);
136 
137  include_once("./Services/RTE/classes/class.ilRTE.php");
138  $this->setQuestion(ilRTE::_replaceMediaObjectImageSrc($data["question_text"], 1));
139  $this->setEstimatedWorkingTime(substr($data["working_time"], 0, 2), substr($data["working_time"], 3, 2), substr($data["working_time"], 6, 2));
140  // load additional data
141  $result = $ilDB->queryF("SELECT * FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
142  array("integer"),
143  array($question_id)
144  );
145  if ($result->numRows() == 1)
146  {
147  $data = $ilDB->fetchAssoc($result);
148  $this->setWidth($data["width"]);
149  $this->setHeight($data["height"]);
150  $this->setApplet($data["applet"]);
151  $this->parameters = unserialize($data["params"]);
152  if (!is_array($this->parameters)) $this->clearParameters();
153  unset($_SESSION["flash_upload_filename"]);
154  }
155  }
156  parent::loadFromDb($question_id);
157  }
158 
166  function duplicate($for_test = true, $title = "", $author = "", $owner = "", $testObjId = null)
167  {
168  if ($this->id <= 0)
169  {
170  // The question has not been saved. It cannot be duplicated
171  return;
172  }
173  // duplicate the question in database
174  $this_id = $this->getId();
175 
176  if( (int)$testObjId > 0 )
177  {
178  $thisObjId = $this->getObjId();
179  }
180 
181  $clone = $this;
182  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
184  $clone->id = -1;
185 
186  if( (int)$testObjId > 0 )
187  {
188  $clone->setObjId($testObjId);
189  }
190 
191  if ($title)
192  {
193  $clone->setTitle($title);
194  }
195 
196  if ($author)
197  {
198  $clone->setAuthor($author);
199  }
200  if ($owner)
201  {
202  $clone->setOwner($owner);
203  }
204 
205  if ($for_test)
206  {
207  $clone->saveToDb($original_id);
208  }
209  else
210  {
211  $clone->saveToDb();
212  }
213 
214  // copy question page content
215  $clone->copyPageOfQuestion($this_id);
216  // copy XHTML media objects
217  $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
218  // duplicate the generic feedback
219  $clone->duplicateGenericFeedback($this_id);
220  // duplicate the applet
221  $clone->duplicateApplet($this_id, $thisObjId);
222 
223  $clone->onDuplicate($thisObjId, $this_id, $clone->getObjId(), $clone->getId());
224 
225  return $clone->id;
226  }
227 
235  function copyObject($target_questionpool, $title = "")
236  {
237  if ($this->id <= 0)
238  {
239  // The question has not been saved. It cannot be duplicated
240  return;
241  }
242  // duplicate the question in database
243  $clone = $this;
244  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
246  $clone->id = -1;
247  $source_questionpool = $this->getObjId();
248  $clone->setObjId($target_questionpool);
249  if ($title)
250  {
251  $clone->setTitle($title);
252  }
253  $clone->saveToDb();
254 
255  // copy question page content
256  $clone->copyPageOfQuestion($original_id);
257  // copy XHTML media objects
258  $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
259  // duplicate the generic feedback
260  $clone->duplicateGenericFeedback($original_id);
261 
262  // duplicate the applet
263  $clone->copyApplet($original_id, $source_questionpool);
264  $clone->onCopy($this->getObjId(), $this->getId());
265 
266  return $clone->id;
267  }
268 
275  protected function duplicateApplet($question_id, $objectId = null)
276  {
277  $flashpath = $this->getFlashPath();
278  $flashpath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $flashpath);
279 
280  if( (int)$objectId > 0 )
281  {
282  $flashpath_original = str_replace("/$this->obj_id/", "/$objectId/", $flashpath_original);
283  }
284 
285  if (!file_exists($flashpath))
286  {
287  ilUtil::makeDirParents($flashpath);
288  }
289  $filename = $this->getApplet();
290  if (!copy($flashpath_original . $filename, $flashpath . $filename)) {
291  print "flash applet could not be duplicated!!!! ";
292  }
293  }
294 
301  protected function copyApplet($question_id, $source_questionpool)
302  {
303  $flashpath = $this->getFlashPath();
304  $flashpath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $flashpath);
305  $flashpath_original = str_replace("/$this->obj_id/", "/$source_questionpool/", $flashpath_original);
306  if (!file_exists($flashpath))
307  {
308  ilUtil::makeDirParents($flashpath);
309  }
310  $filename = $this->getApplet();
311  if (!copy($flashpath_original . $filename, $flashpath . $filename))
312  {
313  print "flash applet could not be copied!!!! ";
314  }
315  }
316 
323  function getMaximumPoints()
324  {
325  return $this->points;
326  }
327 
338  public function calculateReachedPoints($active_id, $pass = NULL, $returndetails = FALSE)
339  {
340  if( $returndetails )
341  {
342  throw new ilTestException('return details not implemented for '.__METHOD__);
343  }
344 
345  global $ilDB;
346 
347  $found_values = array();
348  if (is_null($pass))
349  {
350  $pass = $this->getSolutionMaxPass($active_id);
351  }
352  $result = $ilDB->queryF("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
353  array("integer", "integer", "integer"),
354  array($active_id, $this->getId(), $pass)
355  );
356 
357  $points = 0;
358  while ($data = $ilDB->fetchAssoc($result))
359  {
360  $points += $data["points"];
361  }
362 
363  return $points;
364  }
365 
366  function sendToHost($url, $data, $optional_headers = null)
367  {
368  $params = array('http' => array(
369  'method' => 'POST',
370  'content' => $data
371  ));
372  if ($optional_headers !== null)
373  {
374  $params['http']['header'] = $optional_headers;
375  }
376  $ctx = stream_context_create($params);
377  $fp = @fopen($url, 'rb', false, $ctx);
378  if (!$fp)
379  {
380  throw new Exception("Problem with $url, $php_errormsg");
381  }
382  $response = @stream_get_contents($fp);
383  if ($response === false)
384  {
385  throw new Exception("Problem reading data from $url, $php_errormsg");
386  }
387  return $response;
388  }
389 
398  function moveUploadedFile($tmpfile, $flashfile)
399  {
400  $result = "";
401  if (!empty($tmpfile))
402  {
403  $flashfile = str_replace(" ", "_", $flashfile);
404  $flashpath = $this->getFlashPath();
405  if (!file_exists($flashpath))
406  {
407  ilUtil::makeDirParents($flashpath);
408  }
409  if (ilUtil::moveUploadedFile($tmpfile, $flashfile, $flashpath.$flashfile))
410  {
411  $result = $flashfile;
412  }
413  }
414  return $result;
415  }
416 
417  function deleteApplet()
418  {
419  @unlink($this->getFlashPath() . $this->getApplet());
420  $this->applet = "";
421  }
422 
431  public function saveWorkingData($active_id, $pass = NULL)
432  {
433  // nothing to save!
434 
435  return true;
436  }
437 
446  protected function reworkWorkingData($active_id, $pass, $obligationsAnswered)
447  {
448  // nothing to rework!
449  }
450 
459  function getQuestionType()
460  {
461  return "assFlashQuestion";
462  }
463 
473  {
474  return "qpl_qst_flash";
475  }
476 
486  {
487  return "";
488  }
489 
496  function deleteAnswers($question_id)
497  {
498  }
499 
505  {
507  return $text;
508  }
509 
522  public function setExportDetailsXLS(&$worksheet, $startrow, $active_id, $pass, &$format_title, &$format_bold)
523  {
524  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
525  $worksheet->writeString($startrow, 0, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())), $format_title);
526  $worksheet->writeString($startrow, 1, ilExcelUtils::_convert_text($this->getTitle()), $format_title);
527  return $startrow + 1;
528  }
529 
543  function fromXML(&$item, &$questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
544  {
545  include_once "./Modules/TestQuestionPool/classes/import/qti12/class.assFlashQuestionImport.php";
546  $import = new assFlashQuestionImport($this);
547  $import->fromXML($item, $questionpool_id, $tst_id, $tst_object, $question_counter, $import_mapping);
548  }
549 
557  function toXML($a_include_header = true, $a_include_binary = true, $a_shuffle = false, $test_output = false, $force_image_references = false)
558  {
559  include_once "./Modules/TestQuestionPool/classes/export/qti12/class.assFlashQuestionExport.php";
560  $export = new assFlashQuestionExport($this);
561  return $export->toXML($a_include_header, $a_include_binary, $a_shuffle, $test_output, $force_image_references);
562  }
563 
570  public function getBestSolution($active_id, $pass)
571  {
572  $user_solution = array();
573  return $user_solution;
574  }
575 
576  public function setHeight($a_height)
577  {
578  if (!$a_height) $a_height = 400;
579  $this->height = $a_height;
580  }
581 
582  public function getHeight()
583  {
584  return $this->height;
585  }
586 
587  public function setWidth($a_width)
588  {
589  if (!$a_width) $a_width = 550;
590  $this->width = $a_width;
591  }
592 
593  public function getWidth()
594  {
595  return $this->width;
596  }
597 
598  public function setApplet($a_applet)
599  {
600  $this->applet = $a_applet;
601  }
602 
603  public function getApplet()
604  {
605  return $this->applet;
606  }
607 
608  public function addParameter($name, $value)
609  {
610  $this->parameters[$name] = $value;
611  }
612 
613  public function setParameters($params)
614  {
615  if (is_array($params))
616  {
617  $this->parameters = $params;
618  }
619  else
620  {
621  $this->parameters = array();
622  }
623  }
624 
625  public function removeParameter($name)
626  {
627  unset($this->parameters[$name]);
628  }
629 
630  public function clearParameters()
631  {
632  $this->parameters = array();
633  }
634 
635  public function getParameters()
636  {
637  return $this->parameters;
638  }
639 
640  public function isAutosaveable()
641  {
642  return FALSE;
643  }
644 }
645 
646 ?>