ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assFlashQuestion.php
Go to the documentation of this file.
1 <?php
2  /*
3  +----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +----------------------------------------------------------------------------+
22 */
23 include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
24 include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
25 
34 {
35  private $width;
36  private $height;
37  private $parameters;
38  private $applet;
39 
53  function __construct(
54  $title = "",
55  $comment = "",
56  $author = "",
57  $owner = -1,
58  $question = ""
59  )
60  {
62  $this->parameters = array();
63  $this->width = 540;
64  $this->height = 400;
65  $this->applet = "";
66  }
67 
74  function isComplete()
75  {
76  if (($this->title) and ($this->author) and ($this->question) and ($this->getMaximumPoints() > 0) and (strlen($this->getApplet())))
77  {
78  return true;
79  }
80  else
81  {
82  return false;
83  }
84  }
85 
91  function saveToDb($original_id = "")
92  {
93  global $ilDB, $ilLog;
94 
96 
97  // save additional data
98  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
99  array("integer"),
100  array($this->getId())
101  );
102  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, width, height, applet, params) VALUES (%s, %s, %s, %s, %s)",
103  array("integer", "integer", "integer", "text", "text"),
104  array(
105  $this->getId(),
106  (strlen($this->getWidth())) ? $this->getWidth() : 550,
107  (strlen($this->getHeight())) ? $this->getHeight() : 400,
108  $this->getApplet(),
109  serialize($this->getParameters())
110  )
111  );
112  if ($_SESSION["flash_upload_filename"])
113  {
114  $path = $this->getFlashPath();
116  @rename($_SESSION["flash_upload_filename"], $path . $this->getApplet());
117  unset($_SESSION["flash_upload_filename"]);
118  }
119 
121  }
122 
130  function loadFromDb($question_id)
131  {
132  global $ilDB;
133  $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",
134  array("integer"),
135  array($question_id)
136  );
137  if ($result->numRows() == 1)
138  {
139  $data = $ilDB->fetchAssoc($result);
140  $this->setId($question_id);
141  $this->setNrOfTries($data['nr_of_tries']);
142  $this->setTitle($data["title"]);
143  $this->setComment($data["description"]);
144  $this->setSuggestedSolution($data["solution_hint"]);
145  $this->setOriginalId($data["original_id"]);
146  $this->setObjId($data["obj_fi"]);
147  $this->setAuthor($data["author"]);
148  $this->setOwner($data["owner"]);
149  $this->setPoints($data["points"]);
150 
151  include_once("./Services/RTE/classes/class.ilRTE.php");
152  $this->setQuestion(ilRTE::_replaceMediaObjectImageSrc($data["question_text"], 1));
153  $this->setEstimatedWorkingTime(substr($data["working_time"], 0, 2), substr($data["working_time"], 3, 2), substr($data["working_time"], 6, 2));
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 = "")
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  $clone = $this;
190  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
192  $clone->id = -1;
193  if ($title)
194  {
195  $clone->setTitle($title);
196  }
197 
198  if ($author)
199  {
200  $clone->setAuthor($author);
201  }
202  if ($owner)
203  {
204  $clone->setOwner($owner);
205  }
206 
207  if ($for_test)
208  {
209  $clone->saveToDb($original_id);
210  }
211  else
212  {
213  $clone->saveToDb();
214  }
215 
216  // copy question page content
217  $clone->copyPageOfQuestion($this_id);
218  // copy XHTML media objects
219  $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
220  // duplicate the generic feedback
221  $clone->duplicateFeedbackGeneric($this_id);
222  // duplicate the applet
223  $clone->duplicateApplet($this_id);
224 
225  $clone->onDuplicate($this_id);
226  return $clone->id;
227  }
228 
236  function copyObject($target_questionpool, $title = "")
237  {
238  if ($this->id <= 0)
239  {
240  // The question has not been saved. It cannot be duplicated
241  return;
242  }
243  // duplicate the question in database
244  $clone = $this;
245  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
247  $clone->id = -1;
248  $source_questionpool = $this->getObjId();
249  $clone->setObjId($target_questionpool);
250  if ($title)
251  {
252  $clone->setTitle($title);
253  }
254  $clone->saveToDb();
255 
256  // copy question page content
257  $clone->copyPageOfQuestion($original_id);
258  // copy XHTML media objects
259  $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
260  // duplicate the generic feedback
261  $clone->duplicateFeedbackGeneric($original_id);
262 
263  // duplicate the applet
264  $clone->copyApplet($original_id, $source_questionpool);
265  $clone->onCopy($this->getObjId(), $this->getId());
266 
267  return $clone->id;
268  }
269 
276  protected function duplicateApplet($question_id)
277  {
278  $flashpath = $this->getFlashPath();
279  $flashpath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $flashpath);
280  if (!file_exists($flashpath))
281  {
282  ilUtil::makeDirParents($flashpath);
283  }
284  $filename = $this->getApplet();
285  if (!copy($flashpath_original . $filename, $flashpath . $filename)) {
286  print "flash applet could not be duplicated!!!! ";
287  }
288  }
289 
296  protected function copyApplet($question_id, $source_questionpool)
297  {
298  $flashpath = $this->getFlashPath();
299  $flashpath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $flashpath);
300  $flashpath_original = str_replace("/$this->obj_id/", "/$source_questionpool/", $flashpath_original);
301  if (!file_exists($flashpath))
302  {
303  ilUtil::makeDirParents($flashpath);
304  }
305  $filename = $this->getApplet();
306  if (!copy($flashpath_original . $filename, $flashpath . $filename))
307  {
308  print "flash applet could not be copied!!!! ";
309  }
310  }
311 
318  function getMaximumPoints()
319  {
320  return $this->points;
321  }
322 
332  function calculateReachedPoints($active_id, $pass = NULL)
333  {
334  global $ilDB;
335 
336  $found_values = array();
337  if (is_null($pass))
338  {
339  $pass = $this->getSolutionMaxPass($active_id);
340  }
341  $result = $ilDB->queryF("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
342  array("integer", "integer", "integer"),
343  array($active_id, $this->getId(), $pass)
344  );
345 
346  $points = 0;
347  while ($data = $ilDB->fetchAssoc($result))
348  {
349  $points += $data["points"];
350  }
351 
352  $points = parent::calculateReachedPoints($active_id, $pass = NULL, $points);
353  return $points;
354  }
355 
356  function sendToHost($url, $data, $optional_headers = null)
357  {
358  $params = array('http' => array(
359  'method' => 'POST',
360  'content' => $data
361  ));
362  if ($optional_headers !== null)
363  {
364  $params['http']['header'] = $optional_headers;
365  }
366  $ctx = stream_context_create($params);
367  $fp = @fopen($url, 'rb', false, $ctx);
368  if (!$fp)
369  {
370  throw new Exception("Problem with $url, $php_errormsg");
371  }
372  $response = @stream_get_contents($fp);
373  if ($response === false)
374  {
375  throw new Exception("Problem reading data from $url, $php_errormsg");
376  }
377  return $response;
378  }
379 
388  function moveUploadedFile($tmpfile, $flashfile)
389  {
390  $result = "";
391  if (!empty($tmpfile))
392  {
393  $flashfile = str_replace(" ", "_", $flashfile);
394  $flashpath = $this->getFlashPath();
395  if (!file_exists($flashpath))
396  {
397  ilUtil::makeDirParents($flashpath);
398  }
399  if (ilUtil::moveUploadedFile($tmpfile, $flashfile, $flashpath.$flashfile))
400  {
401  $result = $flashfile;
402  }
403  }
404  return $result;
405  }
406 
407  function deleteApplet()
408  {
409  @unlink($this->getFlashPath() . $this->getApplet());
410  $this->applet = "";
411  }
412 
421  function saveWorkingData($active_id, $pass = NULL)
422  {
423  parent::saveWorkingData($active_id, $pass);
424  return true;
425  }
426 
435  function getQuestionType()
436  {
437  return "assFlashQuestion";
438  }
439 
449  {
450  return "qpl_qst_flash";
451  }
452 
462  {
463  return "";
464  }
465 
472  function deleteAnswers($question_id)
473  {
474  }
475 
481  {
483  return $text;
484  }
485 
495  public function setExportDetailsXLS(&$adapter, $startrow, $active_id, $pass)
496  {
497  $adapter->setCellValue($startrow, 0, $this->lng->txt($this->getQuestionType()), CELL_FORMAT_TITLE);
498  $adapter->setCellValue($startrow, 1, $this->getTitle(), CELL_FORMAT_TITLE);
499  return $startrow + 1;
500  }
501 
515  function fromXML(&$item, &$questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
516  {
517  include_once "./Modules/TestQuestionPool/classes/import/qti12/class.assFlashQuestionImport.php";
518  $import = new assFlashQuestionImport($this);
519  $import->fromXML($item, $questionpool_id, $tst_id, $tst_object, $question_counter, $import_mapping);
520  }
521 
529  function toXML($a_include_header = true, $a_include_binary = true, $a_shuffle = false, $test_output = false, $force_image_references = false)
530  {
531  include_once "./Modules/TestQuestionPool/classes/export/qti12/class.assFlashQuestionExport.php";
532  $export = new assFlashQuestionExport($this);
533  return $export->toXML($a_include_header, $a_include_binary, $a_shuffle, $test_output, $force_image_references);
534  }
535 
542  public function getBestSolution($active_id, $pass)
543  {
544  $user_solution = array();
545  return $user_solution;
546  }
547 
548  public function setHeight($a_height)
549  {
550  if (!$a_height) $a_height = 400;
551  $this->height = $a_height;
552  }
553 
554  public function getHeight()
555  {
556  return $this->height;
557  }
558 
559  public function setWidth($a_width)
560  {
561  if (!$a_width) $a_width = 550;
562  $this->width = $a_width;
563  }
564 
565  public function getWidth()
566  {
567  return $this->width;
568  }
569 
570  public function setApplet($a_applet)
571  {
572  $this->applet = $a_applet;
573  }
574 
575  public function getApplet()
576  {
577  return $this->applet;
578  }
579 
580  public function addParameter($name, $value)
581  {
582  $this->parameters[$name] = $value;
583  }
584 
585  public function setParameters($params)
586  {
587  if (is_array($params))
588  {
589  $this->parameters = $params;
590  }
591  else
592  {
593  $this->parameters = array();
594  }
595  }
596 
597  public function removeParameter($name)
598  {
599  unset($this->parameters[$name]);
600  }
601 
602  public function clearParameters()
603  {
604  $this->parameters = array();
605  }
606 
607  public function getParameters()
608  {
609  return $this->parameters;
610  }
611 }
612 
613 ?>