ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assOrderingQuestion.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 
22 {
30  var $answers;
31 
41 
47  var $thumb_geometry = 100;
48 
55 
68  function __construct(
69  $title = "",
70  $comment = "",
71  $author = "",
72  $owner = -1,
73  $question = "",
75  )
76  {
78  $this->answers = array();
79  $this->ordering_type = $ordering_type;
80  }
81 
88  function isComplete()
89  {
90  if (strlen($this->title) and ($this->author) and ($this->question) and (count($this->answers)) and ($this->getMaximumPoints() > 0))
91  {
92  return true;
93  }
94  else
95  {
96  return false;
97  }
98  }
99 
106  function saveToDb($original_id = "")
107  {
108  global $ilDB;
109 
111 
112  // save additional data
113  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
114  array("integer"),
115  array($this->getId())
116  );
117 
118  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, ordering_type, thumb_geometry, element_height) VALUES (%s, %s, %s, %s)",
119  array("integer", "text","integer","integer"),
120  array(
121  $this->getId(),
122  $this->ordering_type,
123  $this->getThumbGeometry(),
124  ($this->getElementHeight() > 20) ? $this->getElementHeight() : NULL
125  )
126  );
127 
128  $affectedRows = $ilDB->manipulateF("DELETE FROM qpl_a_ordering WHERE question_fi = %s",
129  array('integer'),
130  array($this->getId())
131  );
132 
133  // Anworten wegschreiben
134  foreach ($this->answers as $key => $value)
135  {
136  $answer_obj = $this->answers[$key];
137  $next_id = $ilDB->nextId('qpl_a_ordering');
138  $affectedRows = $ilDB->manipulateF("INSERT INTO qpl_a_ordering (answer_id, question_fi, answertext, solution_order, ".
139  "random_id, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
140  array('integer','integer','text','integer','integer','integer'),
141  array(
142  $next_id,
143  $this->getId(),
144  ilRTE::_replaceMediaObjectImageSrc($answer_obj->getAnswertext(), 0),
145  $key,
146  $answer_obj->getRandomID(),
147  time()
148  )
149  );
150  }
151 
152  if ($this->getOrderingType() == OQ_PICTURES)
153  {
154  $this->rebuildThumbnails();
155  }
156 
157  $this->cleanImagefiles();
159  }
160 
168  function loadFromDb($question_id)
169  {
170  global $ilDB;
171 
172  $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",
173  array("integer"),
174  array($question_id)
175  );
176  if ($result->numRows() == 1)
177  {
178  $data = $ilDB->fetchAssoc($result);
179  $this->setId($question_id);
180  $this->setObjId($data["obj_fi"]);
181  $this->setTitle($data["title"]);
182  $this->setComment($data["description"]);
183  $this->setOriginalId($data["original_id"]);
184  $this->setAuthor($data["author"]);
185  $this->setNrOfTries($data['nr_of_tries']);
186  $this->setPoints($data["points"]);
187  $this->setOwner($data["owner"]);
188  include_once("./Services/RTE/classes/class.ilRTE.php");
189  $this->setQuestion(ilRTE::_replaceMediaObjectImageSrc($data["question_text"], 1));
190  $this->ordering_type = strlen($data["ordering_type"]) ? $data["ordering_type"] : OQ_TERMS;
191  $this->thumb_geometry = $data["thumb_geometry"];
192  $this->element_height = $data["element_height"];
193  $this->setEstimatedWorkingTime(substr($data["working_time"], 0, 2), substr($data["working_time"], 3, 2), substr($data["working_time"], 6, 2));
194  }
195 
196  $result = $ilDB->queryF("SELECT * FROM qpl_a_ordering WHERE question_fi = %s ORDER BY solution_order ASC",
197  array('integer'),
198  array($question_id)
199  );
200 
201  include_once "./Modules/TestQuestionPool/classes/class.assAnswerOrdering.php";
202  if ($result->numRows() > 0)
203  {
204  while ($data = $ilDB->fetchAssoc($result))
205  {
206  include_once("./Services/RTE/classes/class.ilRTE.php");
207  $data["answertext"] = ilRTE::_replaceMediaObjectImageSrc($data["answertext"], 1);
208  array_push($this->answers, new ASS_AnswerOrdering($data["answertext"], $data["random_id"]));
209  }
210  }
211  parent::loadFromDb($question_id);
212  }
213 
219  function duplicate($for_test = true, $title = "", $author = "", $owner = "", $testObjId = null)
220  {
221  if ($this->id <= 0)
222  {
223  // The question has not been saved. It cannot be duplicated
224  return;
225  }
226  // duplicate the question in database
227  $this_id = $this->getId();
228 
229  if( (int)$testObjId > 0 )
230  {
231  $thisObjId = $this->getObjId();
232  }
233 
234  $clone = $this;
235  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
237  $clone->id = -1;
238 
239  if( (int)$testObjId > 0 )
240  {
241  $clone->setObjId($testObjId);
242  }
243 
244  if ($title)
245  {
246  $clone->setTitle($title);
247  }
248  if ($author)
249  {
250  $clone->setAuthor($author);
251  }
252  if ($owner)
253  {
254  $clone->setOwner($owner);
255  }
256  if ($for_test)
257  {
258  $clone->saveToDb($original_id);
259  }
260  else
261  {
262  $clone->saveToDb();
263  }
264 
265  // copy question page content
266  $clone->copyPageOfQuestion($this_id);
267  // copy XHTML media objects
268  $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
269  // duplicate the generic feedback
270  $clone->duplicateGenericFeedback($this_id);
271 
272  // duplicate the image
273  $clone->duplicateImages($this_id, $thisObjId, $clone->getId(), $testObjId);
274 
275  $clone->onDuplicate($thisObjId, $this_id, $clone->getObjId(), $clone->getId());
276 
277  return $clone->id;
278  }
279 
285  function copyObject($target_questionpool, $title = "")
286  {
287  if ($this->id <= 0)
288  {
289  // The question has not been saved. It cannot be duplicated
290  return;
291  }
292  // duplicate the question in database
293  $clone = $this;
294  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
296  $clone->id = -1;
297  $source_questionpool = $this->getObjId();
298  $clone->setObjId($target_questionpool);
299  if ($title)
300  {
301  $clone->setTitle($title);
302  }
303 
304  $clone->saveToDb();
305 
306  // copy question page content
307  $clone->copyPageOfQuestion($original_id);
308  // copy XHTML media objects
309  $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
310  // duplicate the generic feedback
311  $clone->duplicateGenericFeedback($original_id);
312 
313  // duplicate the image
314  $clone->copyImages($original_id, $source_questionpool);
315  $clone->onCopy($this->getObjId(), $this->getId());
316  return $clone->id;
317  }
318 
319  function duplicateImages($src_question_id, $src_object_id, $dest_question_id, $dest_object_id)
320  {
321  global $ilLog;
322  if ($this->getOrderingType() == OQ_PICTURES)
323  {
324  $imagepath_original = $this->getImagePath($src_question_id, $src_object_id);
325  $imagepath = $this->getImagePath($dest_question_id, $dest_object_id);
326 
327  if (!file_exists($imagepath)) {
328  ilUtil::makeDirParents($imagepath);
329  }
330  foreach ($this->answers as $answer)
331  {
332  $filename = $answer->getAnswertext();
333  if (!@copy($imagepath_original . $filename, $imagepath . $filename))
334  {
335  $ilLog->write("image could not be duplicated!!!!");
336  }
337  if (@file_exists($imagepath_original. $this->getThumbPrefix(). $filename))
338  {
339  if (!@copy($imagepath_original . $this->getThumbPrefix() . $filename, $imagepath . $this->getThumbPrefix() . $filename))
340  {
341  $ilLog->write("image thumbnail could not be duplicated!!!!");
342  }
343  }
344  }
345  }
346  }
347 
348  function copyImages($question_id, $source_questionpool)
349  {
350  global $ilLog;
351  if ($this->getOrderingType() == OQ_PICTURES)
352  {
353  $imagepath = $this->getImagePath();
354  $imagepath_original = str_replace("/$this->id/images", "/$question_id/images", $imagepath);
355  $imagepath_original = str_replace("/$this->obj_id/", "/$source_questionpool/", $imagepath_original);
356  if (!file_exists($imagepath)) {
357  ilUtil::makeDirParents($imagepath);
358  }
359  foreach ($this->answers as $answer)
360  {
361  $filename = $answer->getAnswertext();
362  if (!@copy($imagepath_original . $filename, $imagepath . $filename))
363  {
364  $ilLog->write("Ordering Question image could not be copied: $imagepath_original$filename");
365  }
366  if (@file_exists($imagepath_original. $this->getThumbPrefix(). $filename))
367  {
368  if (!@copy($imagepath_original . $this->getThumbPrefix() . $filename, $imagepath . $this->getThumbPrefix() . $filename))
369  {
370  $ilLog->write("Ordering Question image thumbnail could not be copied: $imagepath_original" . $this->getThumbPrefix() . $filename);
371  }
372  }
373  }
374  }
375  }
376 
385  {
386  $this->ordering_type = $ordering_type;
387  }
388 
396  function getOrderingType()
397  {
398  return $this->ordering_type;
399  }
400 
414  function addAnswer(
415  $answertext = "",
416  $solution_order = -1
417  )
418  {
419  include_once "./Modules/TestQuestionPool/classes/class.assAnswerOrdering.php";
420  $answer = new ASS_AnswerOrdering($answertext, $this->getRandomID());
421  if (($solution_order >= 0) && ($solution_order < count($this->answers)))
422  {
423  $part1 = array_slice($this->answers, 0, $solution_order);
424  $part2 = array_slice($this->answers, $solution_order);
425  $this->answers = array_merge($part1, array($answer), $part2);
426  }
427  else
428  {
429  array_push($this->answers, $answer);
430  }
431  }
432 
433  public function moveAnswerUp($position)
434  {
435  if ($position > 0)
436  {
437  $temp = $this->answers[$position-1];
438  $this->answers[$position-1] = $this->answers[$position];
439  $this->answers[$position] = $temp;
440  }
441  }
442 
443  public function moveAnswerDown($position)
444  {
445  if ($position < count($this->answers)-1)
446  {
447  $temp = $this->answers[$position+1];
448  $this->answers[$position+1] = $this->answers[$position];
449  $this->answers[$position] = $temp;
450  }
451  }
452 
453  protected function getRandomID()
454  {
455  $random_number = mt_rand(1, 100000);
456  $found = true;
457  while ($found)
458  {
459  $found = false;
460  foreach ($this->getAnswers() as $answer)
461  {
462  if ($answer->getRandomID() == $random_number)
463  {
464  $found = true;
465  $random_number++;
466  }
467  }
468  }
469  return $random_number;
470  }
471 
481  function getAnswer($index = 0)
482  {
483  if ($index < 0) return NULL;
484  if (count($this->answers) < 1) return NULL;
485  if ($index >= count($this->answers)) return NULL;
486  return $this->answers[$index];
487  }
488 
497  function deleteAnswer($index = 0)
498  {
499  if ($index < 0)
500  {
501  return;
502  }
503  if (count($this->answers) < 1)
504  {
505  return;
506  }
507  if ($index >= count($this->answers))
508  {
509  return;
510  }
511  unset($this->answers[$index]);
512  $this->answers = array_values($this->answers);
513  for ($i = 0; $i < count($this->answers); $i++)
514  {
515  if ($this->answers[$i]->getOrder() > $index)
516  {
517  $this->answers[$i]->setOrder($i);
518  }
519  }
520  }
521 
528  function flushAnswers()
529  {
530  $this->answers = array();
531  }
532 
540  function getAnswerCount()
541  {
542  return count($this->answers);
543  }
544 
552  {
553  if (count($this->answers) == 0)
554  {
555  $max = 0;
556  }
557  else
558  {
559  $max = $this->answers[0]->getSolutionOrder();
560  }
561  foreach ($this->answers as $key => $value)
562  {
563  if ($value->getSolutionOrder() > $max)
564  {
565  $max = $value->getSolutionOrder();
566  }
567  }
568  return $max;
569  }
570 
581  public function calculateReachedPoints($active_id, $pass = NULL, $returndetails = FALSE)
582  {
583  if( $returndetails )
584  {
585  throw new ilTestException('return details not implemented for '.__METHOD__);
586  }
587 
588  global $ilDB;
589 
590  $found_value1 = array();
591  $found_value2 = array();
592  if (is_null($pass))
593  {
594  $pass = $this->getSolutionMaxPass($active_id);
595  }
596  $result = $ilDB->queryF("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
597  array('integer','integer','integer'),
598  array($active_id, $this->getId(), $pass)
599  );
600  $user_order = array();
601  while ($data = $ilDB->fetchAssoc($result))
602  {
603  if ((strcmp($data["value1"], "") != 0) && (strcmp($data["value2"], "") != 0))
604  {
605  $user_order[$data["value2"]] = $data["value1"];
606  }
607  }
608  ksort($user_order);
609  $user_order = array_values($user_order);
610  $points = 0;
611  $correctcount = 0;
612  foreach ($this->answers as $index => $answer)
613  {
614  if ($index == $user_order[$index])
615  {
616  $correctcount++;
617  }
618  }
619  if ($correctcount == count($this->answers))
620  {
621  $points = $this->getPoints();
622  }
623 
624  return $points;
625  }
626 
633  public function getMaximumPoints()
634  {
635  return $this->getPoints();
636  }
637 
638  /*
639  * Returns the encrypted save filename of a matching picture
640  * Images are saved with an encrypted filename to prevent users from
641  * cheating by guessing the solution from the image filename
642  *
643  * @param string $filename Original filename
644  * @return string Encrypted filename
645  */
647  {
648  $extension = "";
649  if (preg_match("/.*\\.(\\w+)$/", $filename, $matches))
650  {
651  $extension = $matches[1];
652  }
653  return md5($filename) . "." . $extension;
654  }
655 
656  protected function cleanImagefiles()
657  {
658  if ($this->getOrderingType() == OQ_PICTURES)
659  {
660  if (@file_exists($this->getImagePath()))
661  {
662  $contents = ilUtil::getDir($this->getImagePath());
663  foreach ($contents as $f)
664  {
665  if (strcmp($f['type'], 'file') == 0)
666  {
667  $found = false;
668  foreach ($this->getAnswers() as $answer)
669  {
670  if (strcmp($f['entry'], $answer->getAnswertext()) == 0) $found = true;
671  if (strcmp($f['entry'], $this->getThumbPrefix() . $answer->getAnswertext()) == 0) $found = true;
672  }
673  if (!$found)
674  {
675  if (@file_exists($this->getImagePath() . $f['entry'])) @unlink($this->getImagePath() . $f['entry']);
676  }
677  }
678  }
679  }
680  }
681  else
682  {
683  if (@file_exists($this->getImagePath()))
684  {
685  ilUtil::delDir($this->getImagePath());
686  }
687  }
688  }
689 
690  /*
691  * Deletes an imagefile from the system if the file is deleted manually
692  *
693  * @param string $filename Image file filename
694  * @return boolean Success
695  */
696  public function deleteImagefile($filename)
697  {
698  $deletename = $$filename;
699  $result = @unlink($this->getImagePath().$deletename);
700  $result = $result & @unlink($this->getImagePath().$this->getThumbPrefix() . $deletename);
701  return $result;
702  }
703 
712  function setImageFile($image_tempfilename, $image_filename, $previous_filename)
713  {
714  $result = TRUE;
715  if (strlen($image_tempfilename))
716  {
717  $image_filename = str_replace(" ", "_", $image_filename);
718  $imagepath = $this->getImagePath();
719  if (!file_exists($imagepath))
720  {
721  ilUtil::makeDirParents($imagepath);
722  }
723  $savename = $image_filename;
724  if (!ilUtil::moveUploadedFile($image_tempfilename, $savename, $imagepath.$savename))
725  {
726  $result = FALSE;
727  }
728  else
729  {
730  // create thumbnail file
731  $thumbpath = $imagepath . $this->getThumbPrefix() . $savename;
732  ilUtil::convertImage($imagepath.$savename, $thumbpath, "JPEG", $this->getThumbGeometry());
733  }
734  if ($result && (strcmp($image_filename, $previous_filename) != 0) && (strlen($previous_filename)))
735  {
736  $this->deleteImagefile($previous_filename);
737  }
738  }
739  return $result;
740  }
741 
749  function checkSaveData()
750  {
751  $result = true;
752  if ($this->getOutputType() == OUTPUT_JAVASCRIPT)
753  {
754  if (strlen($_POST["orderresult"]))
755  {
756  return $result;
757  }
758  }
759  $order_values = array();
760  foreach ($_POST as $key => $value)
761  {
762  if (preg_match("/^order_(\d+)/", $key, $matches))
763  {
764  if (strcmp($value, "") != 0)
765  {
766  array_push($order_values, $value);
767  }
768  }
769  }
770  $check_order = array_flip($order_values);
771  if (count($check_order) != count($order_values))
772  {
773  // duplicate order values!!!
774  $result = false;
775  ilUtil::sendInfo($this->lng->txt("duplicate_order_values_entered"), TRUE);
776  }
777  return $result;
778  }
779 
788  public function saveWorkingData($active_id, $pass = NULL)
789  {
790  global $ilDB;
791  global $ilUser;
792 
793  $saveWorkingDataResult = $this->checkSaveData();
794  $entered_values = 0;
795  if ($saveWorkingDataResult)
796  {
797  if (is_null($pass))
798  {
799  include_once "./Modules/Test/classes/class.ilObjTest.php";
800  $pass = ilObjTest::_getPass($active_id);
801  }
802 
803  $affectedRows = $ilDB->manipulateF("DELETE FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
804  array('integer','integer','integer'),
805  array($active_id, $this->getId(), $pass)
806  );
807  if (array_key_exists("orderresult", $_POST))
808  {
809  $orderresult = $_POST["orderresult"];
810  if (strlen($orderresult))
811  {
812  $orderarray = explode(":", $orderresult);
813  $ordervalue = 1;
814  foreach ($orderarray as $index)
815  {
816  if (preg_match("/id_(\\d+)/", $index, $idmatch))
817  {
818  $randomid = $idmatch[1];
819  foreach ($this->getAnswers() as $answeridx => $answer)
820  {
821  if ($answer->getRandomID() == $randomid)
822  {
823  $next_id = $ilDB->nextId('tst_solutions');
824  $affectedRows = $ilDB->insert("tst_solutions", array(
825  "solution_id" => array("integer", $next_id),
826  "active_fi" => array("integer", $active_id),
827  "question_fi" => array("integer", $this->getId()),
828  "value1" => array("clob", $answeridx),
829  "value2" => array("clob", trim($ordervalue)),
830  "pass" => array("integer", $pass),
831  "tstamp" => array("integer", time())
832  ));
833  $ordervalue++;
834  $entered_values++;
835  }
836  }
837  }
838  }
839  }
840  }
841  else
842  {
843  foreach ($_POST as $key => $value)
844  {
845  if (preg_match("/^order_(\d+)/", $key, $matches))
846  {
847  if (!(preg_match("/initial_value_\d+/", $value)))
848  {
849  if (strlen($value))
850  {
851  foreach ($this->getAnswers() as $answeridx => $answer)
852  {
853  if ($answer->getRandomID() == $matches[1])
854  {
855  $next_id = $ilDB->nextId('tst_solutions');
856  $affectedRows = $ilDB->insert("tst_solutions", array(
857  "solution_id" => array("integer", $next_id),
858  "active_fi" => array("integer", $active_id),
859  "question_fi" => array("integer", $this->getId()),
860  "value1" => array("clob", $answeridx),
861  "value2" => array("clob", $value),
862  "pass" => array("integer", $pass),
863  "tstamp" => array("integer", time())
864  ));
865  $entered_values++;
866  }
867  }
868  }
869  }
870  }
871  }
872  }
873  }
874  if ($entered_values)
875  {
876  include_once ("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
878  {
879  $this->logAction($this->lng->txtlng("assessment", "log_user_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
880  }
881  }
882  else
883  {
884  include_once ("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
886  {
887  $this->logAction($this->lng->txtlng("assessment", "log_user_not_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
888  }
889  }
890 
891  return $saveWorkingDataResult;
892  }
893 
902  protected function reworkWorkingData($active_id, $pass, $obligationsAnswered)
903  {
904  // nothing to rework!
905  }
906 
913  function getQuestionType()
914  {
915  return "assOrderingQuestion";
916  }
917 
925  {
926  return "qpl_qst_ordering";
927  }
928 
936  {
937  return "qpl_a_ordering";
938  }
939 
945  {
947  foreach ($this->answers as $index => $answer)
948  {
949  $answer_obj = $this->answers[$index];
950  $text .= $answer_obj->getAnswertext();
951  }
952  return $text;
953  }
954 
958  function &getAnswers()
959  {
960  return $this->answers;
961  }
962 
970  {
971  return TRUE;
972  }
973 
986  public function setExportDetailsXLS(&$worksheet, $startrow, $active_id, $pass, &$format_title, &$format_bold)
987  {
988  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
989  $solutions = $this->getSolutionValues($active_id, $pass);
990  $sol = array();
991  foreach ($solutions as $solution)
992  {
993  $sol[$solution["value1"]] = $solution["value2"];
994  }
995  asort($sol);
996  $sol = array_keys($sol);
997  $worksheet->writeString($startrow, 0, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())), $format_title);
998  $worksheet->writeString($startrow, 1, ilExcelUtils::_convert_text($this->getTitle()), $format_title);
999  $i = 1;
1000  $answers = $this->getAnswers();
1001  foreach ($sol as $idx)
1002  {
1003  foreach ($solutions as $solution)
1004  {
1005  if ($solution["value1"] == $idx) $worksheet->writeString($startrow + $i, 0, ilExcelUtils::_convert_text($solution["value2"]));
1006  }
1007  $worksheet->writeString($startrow + $i, 1, ilExcelUtils::_convert_text($answers[$idx]->getAnswertext()));
1008  $i++;
1009  }
1010  return $startrow + $i + 1;
1011  }
1012 
1013  /*
1014  * Get the thumbnail geometry
1015  *
1016  * @return integer Geometry
1017  */
1018  public function getThumbGeometry()
1019  {
1020  return $this->thumb_geometry;
1021  }
1022 
1023  public function getThumbSize()
1024  {
1025  return $this->getThumbGeometry();
1026  }
1027 
1028  /*
1029  * Set the thumbnail geometry
1030  *
1031  * @param integer $a_geometry Geometry
1032  */
1033  public function setThumbGeometry($a_geometry)
1034  {
1035  $this->thumb_geometry = ($a_geometry < 1) ? 100 : $a_geometry;
1036  }
1037 
1038  /*
1039  * Get the minimum element height
1040  *
1041  * @return integer Height
1042  */
1043  public function getElementHeight()
1044  {
1045  return $this->element_height;
1046  }
1047 
1048  /*
1049  * Set the minimum element height
1050  *
1051  * @param integer $a_height Height
1052  */
1053  public function setElementHeight($a_height)
1054  {
1055  $this->element_height = ($a_height < 20) ? "" : $a_height;
1056  }
1057 
1058  /*
1059  * Rebuild the thumbnail images with a new thumbnail size
1060  */
1061  public function rebuildThumbnails()
1062  {
1063  if ($this->getOrderingType() == OQ_PICTURES)
1064  {
1065  foreach ($this->getAnswers() as $answer)
1066  {
1067  $this->generateThumbForFile($this->getImagePath(), $answer->getAnswertext());
1068  }
1069  }
1070  }
1071 
1072  public function getThumbPrefix()
1073  {
1074  return "thumb.";
1075  }
1076 
1077  protected function generateThumbForFile($path, $file)
1078  {
1079  $filename = $path . $file;
1080  if (@file_exists($filename))
1081  {
1082  $thumbpath = $path . $this->getThumbPrefix() . $file;
1083  $path_info = @pathinfo($filename);
1084  $ext = "";
1085  switch (strtoupper($path_info['extension']))
1086  {
1087  case 'PNG':
1088  $ext = 'PNG';
1089  break;
1090  case 'GIF':
1091  $ext = 'GIF';
1092  break;
1093  default:
1094  $ext = 'JPEG';
1095  break;
1096  }
1097  ilUtil::convertImage($filename, $thumbpath, $ext, $this->getThumbGeometry());
1098  }
1099  }
1100 
1104  public function toJSON()
1105  {
1106  include_once("./Services/RTE/classes/class.ilRTE.php");
1107  $result = array();
1108  $result['id'] = (int) $this->getId();
1109  $result['type'] = (string) $this->getQuestionType();
1110  $result['title'] = (string) $this->getTitle();
1111  $result['question'] = $this->formatSAQuestion($this->getQuestion());
1112  $result['nr_of_tries'] = (int) $this->getNrOfTries();
1113  $result['shuffle'] = (bool) true;
1114  $result['points'] = (bool) $this->getPoints();
1115  $result['feedback'] = array(
1116  "onenotcorrect" => nl2br(ilRTE::_replaceMediaObjectImageSrc($this->getFeedbackGeneric(0), 0)),
1117  "allcorrect" => nl2br(ilRTE::_replaceMediaObjectImageSrc($this->getFeedbackGeneric(1), 0))
1118  );
1119  if ($this->getOrderingType() == OQ_PICTURES)
1120  {
1121  $result['path'] = $this->getImagePathWeb();
1122  }
1123 
1124  $counter = 1;
1125  $answers = array();
1126  foreach ($this->getAnswers() as $answer_obj)
1127  {
1128  $answers[$counter] = $answer_obj->getAnswertext();
1129  $counter++;
1130  }
1131  $answers = $this->pcArrayShuffle($answers);
1132  $arr = array();
1133  foreach ($answers as $order => $answer)
1134  {
1135  array_push($arr, array(
1136  "answertext" => (string) $answer,
1137  "order" => (int) $order
1138  ));
1139  }
1140  $result['answers'] = $arr;
1141 
1142  $mobs = ilObjMediaObject::_getMobsOfObject("qpl:html", $this->getId());
1143  $result['mobs'] = $mobs;
1144 
1145  return json_encode($result);
1146  }
1147 
1148  public function removeAnswerImage($index)
1149  {
1150  $answer = $this->answers[$index];
1151  if (is_object($answer))
1152  {
1153  $this->deleteImagefile($answer->getAnswertext());
1154  $answer->setAnswertext('');
1155  }
1156  }
1157 
1158 }
1159 
1160 ?>