ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assOrderingQuestion.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 
24 include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
25 include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
26 
37 {
45  var $answers;
46 
56 
62  var $thumb_geometry = 100;
63 
70 
83  function __construct(
84  $title = "",
85  $comment = "",
86  $author = "",
87  $owner = -1,
88  $question = "",
90  )
91  {
93  $this->answers = array();
94  $this->ordering_type = $ordering_type;
95  }
96 
103  function isComplete()
104  {
105  if (($this->title) and ($this->author) and ($this->question) and (count($this->answers)) and ($this->getMaximumPoints() > 0))
106  {
107  return true;
108  }
109  else
110  {
111  return false;
112  }
113  }
114 
121  function saveToDb($original_id = "")
122  {
123  global $ilDB;
124 
126 
127  // save additional data
128  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
129  array("integer"),
130  array($this->getId())
131  );
132 
133  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, ordering_type, thumb_geometry, element_height) VALUES (%s, %s, %s, %s)",
134  array("integer", "text","integer","integer"),
135  array(
136  $this->getId(),
137  $this->ordering_type,
138  $this->getThumbGeometry(),
139  ($this->getElementHeight() > 20) ? $this->getElementHeight() : NULL
140  )
141  );
142 
143  $affectedRows = $ilDB->manipulateF("DELETE FROM qpl_a_ordering WHERE question_fi = %s",
144  array('integer'),
145  array($this->getId())
146  );
147 
148  // Anworten wegschreiben
149  foreach ($this->answers as $key => $value)
150  {
151  $answer_obj = $this->answers[$key];
152  $next_id = $ilDB->nextId('qpl_a_ordering');
153  $affectedRows = $ilDB->manipulateF("INSERT INTO qpl_a_ordering (answer_id, question_fi, answertext, solution_order, ".
154  "random_id, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
155  array('integer','integer','text','integer','integer','integer'),
156  array(
157  $next_id,
158  $this->getId(),
159  ilRTE::_replaceMediaObjectImageSrc($answer_obj->getAnswertext(), 0),
160  $key,
161  $answer_obj->getRandomID(),
162  time()
163  )
164  );
165  }
166 
167  if ($this->getOrderingType() == OQ_PICTURES)
168  {
169  $this->rebuildThumbnails();
170  }
171 
172  $this->cleanImagefiles();
174  }
175 
183  function loadFromDb($question_id)
184  {
185  global $ilDB;
186 
187  $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",
188  array("integer"),
189  array($question_id)
190  );
191  if ($result->numRows() == 1)
192  {
193  $data = $ilDB->fetchAssoc($result);
194  $this->setId($question_id);
195  $this->setObjId($data["obj_fi"]);
196  $this->setTitle($data["title"]);
197  $this->setComment($data["description"]);
198  $this->setOriginalId($data["original_id"]);
199  $this->setAuthor($data["author"]);
200  $this->setNrOfTries($data['nr_of_tries']);
201  $this->setPoints($data["points"]);
202  $this->setOwner($data["owner"]);
203  include_once("./Services/RTE/classes/class.ilRTE.php");
204  $this->setQuestion(ilRTE::_replaceMediaObjectImageSrc($data["question_text"], 1));
205  $this->ordering_type = strlen($data["ordering_type"]) ? $data["ordering_type"] : OQ_TERMS;
206  $this->thumb_geometry = $data["thumb_geometry"];
207  $this->element_height = $data["element_height"];
208  $this->setEstimatedWorkingTime(substr($data["working_time"], 0, 2), substr($data["working_time"], 3, 2), substr($data["working_time"], 6, 2));
209  }
210 
211  $result = $ilDB->queryF("SELECT * FROM qpl_a_ordering WHERE question_fi = %s ORDER BY solution_order ASC",
212  array('integer'),
213  array($question_id)
214  );
215 
216  include_once "./Modules/TestQuestionPool/classes/class.assAnswerOrdering.php";
217  if ($result->numRows() > 0)
218  {
219  while ($data = $ilDB->fetchAssoc($result))
220  {
221  include_once("./Services/RTE/classes/class.ilRTE.php");
222  $data["answertext"] = ilRTE::_replaceMediaObjectImageSrc($data["answertext"], 1);
223  array_push($this->answers, new ASS_AnswerOrdering($data["answertext"], $data["random_id"]));
224  }
225  }
226  parent::loadFromDb($question_id);
227  }
228 
234  function duplicate($for_test = true, $title = "", $author = "", $owner = "")
235  {
236  if ($this->id <= 0)
237  {
238  // The question has not been saved. It cannot be duplicated
239  return;
240  }
241  // duplicate the question in database
242  $this_id = $this->getId();
243  $clone = $this;
244  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
246  $clone->id = -1;
247  if ($title)
248  {
249  $clone->setTitle($title);
250  }
251  if ($author)
252  {
253  $clone->setAuthor($author);
254  }
255  if ($owner)
256  {
257  $clone->setOwner($owner);
258  }
259  if ($for_test)
260  {
261  $clone->saveToDb($original_id);
262  }
263  else
264  {
265  $clone->saveToDb();
266  }
267 
268  // copy question page content
269  $clone->copyPageOfQuestion($this_id);
270  // copy XHTML media objects
271  $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
272  // duplicate the generic feedback
273  $clone->duplicateFeedbackGeneric($this_id);
274 
275  // duplicate the image
276  $clone->duplicateImages($this_id);
277  $clone->onDuplicate($this_id);
278  return $clone->id;
279  }
280 
286  function copyObject($target_questionpool, $title = "")
287  {
288  if ($this->id <= 0)
289  {
290  // The question has not been saved. It cannot be duplicated
291  return;
292  }
293  // duplicate the question in database
294  $clone = $this;
295  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
297  $clone->id = -1;
298  $source_questionpool = $this->getObjId();
299  $clone->setObjId($target_questionpool);
300  if ($title)
301  {
302  $clone->setTitle($title);
303  }
304 
305  $clone->saveToDb();
306 
307  // copy question page content
308  $clone->copyPageOfQuestion($original_id);
309  // copy XHTML media objects
310  $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
311  // duplicate the generic feedback
312  $clone->duplicateFeedbackGeneric($original_id);
313 
314  // duplicate the image
315  $clone->copyImages($original_id, $source_questionpool);
316  $clone->onCopy($this->getObjId(), $this->getId());
317  return $clone->id;
318  }
319 
320  function duplicateImages($question_id)
321  {
322  global $ilLog;
323  if ($this->getOrderingType() == OQ_PICTURES)
324  {
325  $imagepath = $this->getImagePath();
326  $imagepath_original = str_replace("/$this->id/images", "/$question_id/images", $imagepath);
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 
580  function calculateReachedPoints($active_id, $pass = NULL)
581  {
582  global $ilDB;
583 
584  $found_value1 = array();
585  $found_value2 = array();
586  if (is_null($pass))
587  {
588  $pass = $this->getSolutionMaxPass($active_id);
589  }
590  $result = $ilDB->queryF("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
591  array('integer','integer','integer'),
592  array($active_id, $this->getId(), $pass)
593  );
594  $user_order = array();
595  while ($data = $ilDB->fetchAssoc($result))
596  {
597  if ((strcmp($data["value1"], "") != 0) && (strcmp($data["value2"], "") != 0))
598  {
599  $user_order[$data["value2"]] = $data["value1"];
600  }
601  }
602  ksort($user_order);
603  $user_order = array_values($user_order);
604  $points = 0;
605  $correctcount = 0;
606  foreach ($this->answers as $index => $answer)
607  {
608  if ($index == $user_order[$index])
609  {
610  $correctcount++;
611  }
612  }
613  if ($correctcount == count($this->answers))
614  {
615  $points = $this->getPoints();
616  }
617 
618  $points = parent::calculateReachedPoints($active_id, $pass = NULL, $points);
619  return $points;
620  }
621 
628  public function getMaximumPoints()
629  {
630  return $this->getPoints();
631  }
632 
633  /*
634  * Returns the encrypted save filename of a matching picture
635  * Images are saved with an encrypted filename to prevent users from
636  * cheating by guessing the solution from the image filename
637  *
638  * @param string $filename Original filename
639  * @return string Encrypted filename
640  */
642  {
643  $extension = "";
644  if (preg_match("/.*\\.(\\w+)$/", $filename, $matches))
645  {
646  $extension = $matches[1];
647  }
648  return md5($filename) . "." . $extension;
649  }
650 
651  protected function cleanImagefiles()
652  {
653  if ($this->getOrderingType() == OQ_PICTURES)
654  {
655  if (@file_exists($this->getImagePath()))
656  {
657  $contents = ilUtil::getDir($this->getImagePath());
658  foreach ($contents as $f)
659  {
660  if (strcmp($f['type'], 'file') == 0)
661  {
662  $found = false;
663  foreach ($this->getAnswers() as $answer)
664  {
665  if (strcmp($f['entry'], $answer->getAnswertext()) == 0) $found = true;
666  if (strcmp($f['entry'], $this->getThumbPrefix() . $answer->getAnswertext()) == 0) $found = true;
667  }
668  if (!$found)
669  {
670  if (@file_exists($this->getImagePath() . $f['entry'])) @unlink($this->getImagePath() . $f['entry']);
671  }
672  }
673  }
674  }
675  }
676  else
677  {
678  if (@file_exists($this->getImagePath()))
679  {
680  ilUtil::delDir($this->getImagePath());
681  }
682  }
683  }
684 
685  /*
686  * Deletes an imagefile from the system if the file is deleted manually
687  *
688  * @param string $filename Image file filename
689  * @return boolean Success
690  */
691  public function deleteImagefile($filename)
692  {
693  $deletename = $$filename;
694  $result = @unlink($this->getImagePath().$deletename);
695  $result = $result & @unlink($this->getImagePath().$this->getThumbPrefix() . $deletename);
696  return $result;
697  }
698 
707  function setImageFile($image_tempfilename, $image_filename, $previous_filename)
708  {
709  $result = TRUE;
710  if (strlen($image_tempfilename))
711  {
712  $image_filename = str_replace(" ", "_", $image_filename);
713  $imagepath = $this->getImagePath();
714  if (!file_exists($imagepath))
715  {
716  ilUtil::makeDirParents($imagepath);
717  }
718  $savename = $image_filename;
719  if (!ilUtil::moveUploadedFile($image_tempfilename, $savename, $imagepath.$savename))
720  {
721  $result = FALSE;
722  }
723  else
724  {
725  // create thumbnail file
726  $thumbpath = $imagepath . $this->getThumbPrefix() . $savename;
727  ilUtil::convertImage($imagepath.$savename, $thumbpath, "JPEG", $this->getThumbGeometry());
728  }
729  if ($result && (strcmp($image_filename, $previous_filename) != 0) && (strlen($previous_filename)))
730  {
731  $this->deleteImagefile($previous_filename);
732  }
733  }
734  return $result;
735  }
736 
744  function checkSaveData()
745  {
746  $result = true;
747  if ($this->getOutputType() == OUTPUT_JAVASCRIPT)
748  {
749  if (strlen($_POST["orderresult"]))
750  {
751  return $result;
752  }
753  }
754  $order_values = array();
755  foreach ($_POST as $key => $value)
756  {
757  if (preg_match("/^order_(\d+)/", $key, $matches))
758  {
759  if (strcmp($value, "") != 0)
760  {
761  array_push($order_values, $value);
762  }
763  }
764  }
765  $check_order = array_flip($order_values);
766  if (count($check_order) != count($order_values))
767  {
768  // duplicate order values!!!
769  $result = false;
770  ilUtil::sendInfo($this->lng->txt("duplicate_order_values_entered"), TRUE);
771  }
772  return $result;
773  }
774 
783  function saveWorkingData($active_id, $pass = NULL)
784  {
785  global $ilDB;
786  global $ilUser;
787 
788  $saveWorkingDataResult = $this->checkSaveData();
789  $entered_values = 0;
790  if ($saveWorkingDataResult)
791  {
792  if (is_null($pass))
793  {
794  include_once "./Modules/Test/classes/class.ilObjTest.php";
795  $pass = ilObjTest::_getPass($active_id);
796  }
797 
798  $affectedRows = $ilDB->manipulateF("DELETE FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
799  array('integer','integer','integer'),
800  array($active_id, $this->getId(), $pass)
801  );
802  if (array_key_exists("orderresult", $_POST))
803  {
804  $orderresult = $_POST["orderresult"];
805  if (strlen($orderresult))
806  {
807  $orderarray = explode(":", $orderresult);
808  $ordervalue = 1;
809  foreach ($orderarray as $index)
810  {
811  if (preg_match("/id_(\\d+)/", $index, $idmatch))
812  {
813  $randomid = $idmatch[1];
814  foreach ($this->getAnswers() as $answeridx => $answer)
815  {
816  if ($answer->getRandomID() == $randomid)
817  {
818  $next_id = $ilDB->nextId('tst_solutions');
819  $affectedRows = $ilDB->insert("tst_solutions", array(
820  "solution_id" => array("integer", $next_id),
821  "active_fi" => array("integer", $active_id),
822  "question_fi" => array("integer", $this->getId()),
823  "value1" => array("clob", $answeridx),
824  "value2" => array("clob", trim($ordervalue)),
825  "pass" => array("integer", $pass),
826  "tstamp" => array("integer", time())
827  ));
828  $ordervalue++;
829  $entered_values++;
830  }
831  }
832  }
833  }
834  }
835  }
836  else
837  {
838  foreach ($_POST as $key => $value)
839  {
840  if (preg_match("/^order_(\d+)/", $key, $matches))
841  {
842  if (!(preg_match("/initial_value_\d+/", $value)))
843  {
844  if (strlen($value))
845  {
846  foreach ($this->getAnswers() as $answeridx => $answer)
847  {
848  if ($answer->getRandomID() == $matches[1])
849  {
850  $next_id = $ilDB->nextId('tst_solutions');
851  $affectedRows = $ilDB->insert("tst_solutions", array(
852  "solution_id" => array("integer", $next_id),
853  "active_fi" => array("integer", $active_id),
854  "question_fi" => array("integer", $this->getId()),
855  "value1" => array("clob", $answeridx),
856  "value2" => array("clob", $value),
857  "pass" => array("integer", $pass),
858  "tstamp" => array("integer", time())
859  ));
860  $entered_values++;
861  }
862  }
863  }
864  }
865  }
866  }
867  }
868  }
869  if ($entered_values)
870  {
871  include_once ("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
873  {
874  $this->logAction($this->lng->txtlng("assessment", "log_user_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
875  }
876  }
877  else
878  {
879  include_once ("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
881  {
882  $this->logAction($this->lng->txtlng("assessment", "log_user_not_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
883  }
884  }
885  parent::saveWorkingData($active_id, $pass);
886  return $saveWorkingDataResult;
887  }
888 
895  function getQuestionType()
896  {
897  return "assOrderingQuestion";
898  }
899 
907  {
908  return "qpl_qst_ordering";
909  }
910 
918  {
919  return "qpl_a_ordering";
920  }
921 
927  {
929  foreach ($this->answers as $index => $answer)
930  {
931  $answer_obj = $this->answers[$index];
932  $text .= $answer_obj->getAnswertext();
933  }
934  return $text;
935  }
936 
940  function &getAnswers()
941  {
942  return $this->answers;
943  }
944 
952  {
953  return TRUE;
954  }
955 
968  public function setExportDetailsXLS(&$worksheet, $startrow, $active_id, $pass, &$format_title, &$format_bold)
969  {
970  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
971  $solutions = $this->getSolutionValues($active_id, $pass);
972  $sol = array();
973  foreach ($solutions as $solution)
974  {
975  $sol[$solution["value1"]] = $solution["value2"];
976  }
977  asort($sol);
978  $sol = array_keys($sol);
979  $worksheet->writeString($startrow, 0, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())), $format_title);
980  $worksheet->writeString($startrow, 1, ilExcelUtils::_convert_text($this->getTitle()), $format_title);
981  $i = 1;
982  $answers = $this->getAnswers();
983  foreach ($sol as $idx)
984  {
985  foreach ($solutions as $solution)
986  {
987  if ($solution["value1"] == $idx) $worksheet->writeString($startrow + $i, 0, ilExcelUtils::_convert_text($solution["value2"]));
988  }
989  $worksheet->writeString($startrow + $i, 1, ilExcelUtils::_convert_text($answers[$idx]->getAnswertext()));
990  $i++;
991  }
992  return $startrow + $i + 1;
993  }
994 
995  /*
996  * Get the thumbnail geometry
997  *
998  * @return integer Geometry
999  */
1000  public function getThumbGeometry()
1001  {
1002  return $this->thumb_geometry;
1003  }
1004 
1005  public function getThumbSize()
1006  {
1007  return $this->getThumbGeometry();
1008  }
1009 
1010  /*
1011  * Set the thumbnail geometry
1012  *
1013  * @param integer $a_geometry Geometry
1014  */
1015  public function setThumbGeometry($a_geometry)
1016  {
1017  $this->thumb_geometry = ($a_geometry < 1) ? 100 : $a_geometry;
1018  }
1019 
1020  /*
1021  * Get the minimum element height
1022  *
1023  * @return integer Height
1024  */
1025  public function getElementHeight()
1026  {
1027  return $this->element_height;
1028  }
1029 
1030  /*
1031  * Set the minimum element height
1032  *
1033  * @param integer $a_height Height
1034  */
1035  public function setElementHeight($a_height)
1036  {
1037  $this->element_height = ($a_height < 20) ? "" : $a_height;
1038  }
1039 
1040  /*
1041  * Rebuild the thumbnail images with a new thumbnail size
1042  */
1043  public function rebuildThumbnails()
1044  {
1045  if ($this->getOrderingType() == OQ_PICTURES)
1046  {
1047  foreach ($this->getAnswers() as $answer)
1048  {
1049  $this->generateThumbForFile($this->getImagePath(), $answer->getAnswertext());
1050  }
1051  }
1052  }
1053 
1054  public function getThumbPrefix()
1055  {
1056  return "thumb.";
1057  }
1058 
1059  protected function generateThumbForFile($path, $file)
1060  {
1061  $filename = $path . $file;
1062  if (@file_exists($filename))
1063  {
1064  $thumbpath = $path . $this->getThumbPrefix() . $file;
1065  $path_info = @pathinfo($filename);
1066  $ext = "";
1067  switch (strtoupper($path_info['extension']))
1068  {
1069  case 'PNG':
1070  $ext = 'PNG';
1071  break;
1072  case 'GIF':
1073  $ext = 'GIF';
1074  break;
1075  default:
1076  $ext = 'JPEG';
1077  break;
1078  }
1079  ilUtil::convertImage($filename, $thumbpath, $ext, $this->getThumbGeometry());
1080  }
1081  }
1082 
1086  public function toJSON()
1087  {
1088  include_once("./Services/RTE/classes/class.ilRTE.php");
1089  $result = array();
1090  $result['id'] = (int) $this->getId();
1091  $result['type'] = (string) $this->getQuestionType();
1092  $result['title'] = (string) $this->getTitle();
1093  $result['question'] = (string) ilRTE::_replaceMediaObjectImageSrc($this->getQuestion(), 0);
1094  $result['nr_of_tries'] = (int) $this->getNrOfTries();
1095  $result['shuffle'] = (bool) true;
1096  $result['points'] = (bool) $this->getPoints();
1097  $result['feedback'] = array(
1098  "onenotcorrect" => nl2br(ilRTE::_replaceMediaObjectImageSrc($this->getFeedbackGeneric(0), 0)),
1099  "allcorrect" => nl2br(ilRTE::_replaceMediaObjectImageSrc($this->getFeedbackGeneric(1), 0))
1100  );
1101  $counter = 1;
1102  $answers = array();
1103  foreach ($this->getAnswers() as $answer_obj)
1104  {
1105  $answers[$counter] = $answer_obj->getAnswertext();
1106  $counter++;
1107  }
1108  $answers = $this->pcArrayShuffle($answers);
1109  $arr = array();
1110  foreach ($answers as $order => $answer)
1111  {
1112  array_push($arr, array(
1113  "answertext" => (string) $answer,
1114  "order" => (int) $order
1115  ));
1116  }
1117  $result['answers'] = $arr;
1118  $mobs = ilObjMediaObject::_getMobsOfObject("qpl:html", $this->getId());
1119  $result['mobs'] = $mobs;
1120  return json_encode($result);
1121  }
1122 
1123  public function removeAnswerImage($index)
1124  {
1125  $answer = $this->answers[$index];
1126  if (is_object($answer))
1127  {
1128  $this->deleteImagefile($answer->getAnswertext());
1129  $answer->setAnswertext('');
1130  }
1131  }
1132 
1133 }
1134 
1135 ?>