ILIAS  Release_4_2_x_branch Revision 61807
 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 (strlen($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 = "", $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 
190  if( (int)$testObjId > 0 )
191  {
192  $thisObjId = $this->getObjId();
193  }
194 
195  $clone = $this;
196  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
198  $clone->id = -1;
199 
200  if( (int)$testObjId > 0 )
201  {
202  $clone->setObjId($testObjId);
203  }
204 
205  if ($title)
206  {
207  $clone->setTitle($title);
208  }
209 
210  if ($author)
211  {
212  $clone->setAuthor($author);
213  }
214  if ($owner)
215  {
216  $clone->setOwner($owner);
217  }
218 
219  if ($for_test)
220  {
221  $clone->saveToDb($original_id);
222  }
223  else
224  {
225  $clone->saveToDb();
226  }
227 
228  // copy question page content
229  $clone->copyPageOfQuestion($this_id);
230  // copy XHTML media objects
231  $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
232  // duplicate the generic feedback
233  $clone->duplicateFeedbackGeneric($this_id);
234  // duplicate the applet
235  $clone->duplicateApplet($this_id, $thisObjId);
236 
237  $clone->onDuplicate($thisObjId, $this_id, $clone->getObjId(), $clone->getId());
238 
239  return $clone->id;
240  }
241 
249  function copyObject($target_questionpool, $title = "")
250  {
251  if ($this->id <= 0)
252  {
253  // The question has not been saved. It cannot be duplicated
254  return;
255  }
256  // duplicate the question in database
257  $clone = $this;
258  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
260  $clone->id = -1;
261  $source_questionpool = $this->getObjId();
262  $clone->setObjId($target_questionpool);
263  if ($title)
264  {
265  $clone->setTitle($title);
266  }
267  $clone->saveToDb();
268 
269  // copy question page content
270  $clone->copyPageOfQuestion($original_id);
271  // copy XHTML media objects
272  $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
273  // duplicate the generic feedback
274  $clone->duplicateFeedbackGeneric($original_id);
275 
276  // duplicate the applet
277  $clone->copyApplet($original_id, $source_questionpool);
278  $clone->onCopy($this->getObjId(), $this->getId());
279 
280  return $clone->id;
281  }
282 
289  protected function duplicateApplet($question_id, $objectId = null)
290  {
291  $flashpath = $this->getFlashPath();
292  $flashpath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $flashpath);
293 
294  if( (int)$objectId > 0 )
295  {
296  $flashpath_original = str_replace("/$this->obj_id/", "/$objectId/", $flashpath_original);
297  }
298 
299  if (!file_exists($flashpath))
300  {
301  ilUtil::makeDirParents($flashpath);
302  }
303  $filename = $this->getApplet();
304  if (!copy($flashpath_original . $filename, $flashpath . $filename)) {
305  print "flash applet could not be duplicated!!!! ";
306  }
307  }
308 
315  protected function copyApplet($question_id, $source_questionpool)
316  {
317  $flashpath = $this->getFlashPath();
318  $flashpath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $flashpath);
319  $flashpath_original = str_replace("/$this->obj_id/", "/$source_questionpool/", $flashpath_original);
320  if (!file_exists($flashpath))
321  {
322  ilUtil::makeDirParents($flashpath);
323  }
324  $filename = $this->getApplet();
325  if (!copy($flashpath_original . $filename, $flashpath . $filename))
326  {
327  print "flash applet could not be copied!!!! ";
328  }
329  }
330 
337  function getMaximumPoints()
338  {
339  return $this->points;
340  }
341 
351  function calculateReachedPoints($active_id, $pass = NULL)
352  {
353  global $ilDB;
354 
355  $found_values = array();
356  if (is_null($pass))
357  {
358  $pass = $this->getSolutionMaxPass($active_id);
359  }
360  $result = $ilDB->queryF("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
361  array("integer", "integer", "integer"),
362  array($active_id, $this->getId(), $pass)
363  );
364 
365  $points = 0;
366  while ($data = $ilDB->fetchAssoc($result))
367  {
368  $points += $data["points"];
369  }
370 
371  $points = parent::calculateReachedPoints($active_id, $pass = NULL, $points);
372  return $points;
373  }
374 
375  function sendToHost($url, $data, $optional_headers = null)
376  {
377  $params = array('http' => array(
378  'method' => 'POST',
379  'content' => $data
380  ));
381  if ($optional_headers !== null)
382  {
383  $params['http']['header'] = $optional_headers;
384  }
385  $ctx = stream_context_create($params);
386  $fp = @fopen($url, 'rb', false, $ctx);
387  if (!$fp)
388  {
389  throw new Exception("Problem with $url, $php_errormsg");
390  }
391  $response = @stream_get_contents($fp);
392  if ($response === false)
393  {
394  throw new Exception("Problem reading data from $url, $php_errormsg");
395  }
396  return $response;
397  }
398 
407  function moveUploadedFile($tmpfile, $flashfile)
408  {
409  $result = "";
410  if (!empty($tmpfile))
411  {
412  $flashfile = str_replace(" ", "_", $flashfile);
413  $flashpath = $this->getFlashPath();
414  if (!file_exists($flashpath))
415  {
416  ilUtil::makeDirParents($flashpath);
417  }
418  if (ilUtil::moveUploadedFile($tmpfile, $flashfile, $flashpath.$flashfile))
419  {
420  $result = $flashfile;
421  }
422  }
423  return $result;
424  }
425 
426  function deleteApplet()
427  {
428  @unlink($this->getFlashPath() . $this->getApplet());
429  $this->applet = "";
430  }
431 
440  function saveWorkingData($active_id, $pass = NULL)
441  {
442  parent::saveWorkingData($active_id, $pass);
443  return true;
444  }
445 
454  function getQuestionType()
455  {
456  return "assFlashQuestion";
457  }
458 
468  {
469  return "qpl_qst_flash";
470  }
471 
481  {
482  return "";
483  }
484 
491  function deleteAnswers($question_id)
492  {
493  }
494 
500  {
502  return $text;
503  }
504 
517  public function setExportDetailsXLS(&$worksheet, $startrow, $active_id, $pass, &$format_title, &$format_bold)
518  {
519  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
520  $worksheet->writeString($startrow, 0, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())), $format_title);
521  $worksheet->writeString($startrow, 1, ilExcelUtils::_convert_text($this->getTitle()), $format_title);
522  return $startrow + 1;
523  }
524 
538  function fromXML(&$item, &$questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
539  {
540  include_once "./Modules/TestQuestionPool/classes/import/qti12/class.assFlashQuestionImport.php";
541  $import = new assFlashQuestionImport($this);
542  $import->fromXML($item, $questionpool_id, $tst_id, $tst_object, $question_counter, $import_mapping);
543  }
544 
552  function toXML($a_include_header = true, $a_include_binary = true, $a_shuffle = false, $test_output = false, $force_image_references = false)
553  {
554  include_once "./Modules/TestQuestionPool/classes/export/qti12/class.assFlashQuestionExport.php";
555  $export = new assFlashQuestionExport($this);
556  return $export->toXML($a_include_header, $a_include_binary, $a_shuffle, $test_output, $force_image_references);
557  }
558 
565  public function getBestSolution($active_id, $pass)
566  {
567  $user_solution = array();
568  return $user_solution;
569  }
570 
571  public function setHeight($a_height)
572  {
573  if (!$a_height) $a_height = 400;
574  $this->height = $a_height;
575  }
576 
577  public function getHeight()
578  {
579  return $this->height;
580  }
581 
582  public function setWidth($a_width)
583  {
584  if (!$a_width) $a_width = 550;
585  $this->width = $a_width;
586  }
587 
588  public function getWidth()
589  {
590  return $this->width;
591  }
592 
593  public function setApplet($a_applet)
594  {
595  $this->applet = $a_applet;
596  }
597 
598  public function getApplet()
599  {
600  return $this->applet;
601  }
602 
603  public function addParameter($name, $value)
604  {
605  $this->parameters[$name] = $value;
606  }
607 
608  public function setParameters($params)
609  {
610  if (is_array($params))
611  {
612  $this->parameters = $params;
613  }
614  else
615  {
616  $this->parameters = array();
617  }
618  }
619 
620  public function removeParameter($name)
621  {
622  unset($this->parameters[$name]);
623  }
624 
625  public function clearParameters()
626  {
627  $this->parameters = array();
628  }
629 
630  public function getParameters()
631  {
632  return $this->parameters;
633  }
634 }
635 
636 ?>