ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assSingleChoice.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 
36 {
44  var $answers;
45 
55 
61  protected $thumb_size;
62 
77  function __construct(
78  $title = "",
79  $comment = "",
80  $author = "",
81  $owner = -1,
82  $question = "",
84  )
85  {
87  $this->thumb_size = 150;
88  $this->output_type = $output_type;
89  $this->answers = array();
90  $this->shuffle = 1;
91  }
92 
99  function isComplete()
100  {
101  if (strlen($this->title) and ($this->author) and ($this->question) and (count($this->answers)) and ($this->getMaximumPoints() > 0))
102  {
103  foreach ($this->answers as $answer)
104  {
105  if ((strlen($answer->getAnswertext()) == 0) && (strlen($answer->getImage()) == 0)) return false;
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  $oldthumbsize = 0;
128  if ($this->isSingleline && ($this->getThumbSize()))
129  {
130  // get old thumbnail size
131  $result = $ilDB->queryF("SELECT thumb_size FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
132  array("integer"),
133  array($this->getId())
134  );
135  if ($result->numRows() == 1)
136  {
137  $data = $ilDB->fetchAssoc($result);
138  $oldthumbsize = $data['thumb_size'];
139  }
140  }
141  if (!$this->isSingleline)
142  {
143  ilUtil::delDir($this->getImagePath());
144  }
145 
146  // save additional data
147  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
148  array("integer"),
149  array($this->getId())
150  );
151 
152  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, shuffle, allow_images, thumb_size) VALUES (%s, %s, %s, %s)",
153  array("integer", "text", "text", "integer"),
154  array(
155  $this->getId(),
156  $this->getShuffle(),
157  ($this->isSingleline) ? "0" : "1",
158  (strlen($this->getThumbSize()) == 0) ? null : $this->getThumbSize()
159  )
160  );
161 
162  $affectedRows = $ilDB->manipulateF("DELETE FROM qpl_a_sc WHERE question_fi = %s",
163  array('integer'),
164  array($this->getId())
165  );
166 
167  foreach ($this->answers as $key => $value)
168  {
169  $answer_obj = $this->answers[$key];
170  $next_id = $ilDB->nextId('qpl_a_sc');
171  $affectedRows = $ilDB->manipulateF("INSERT INTO qpl_a_sc (answer_id, question_fi, answertext, points, aorder, imagefile, tstamp) VALUES (%s, %s, %s, %s, %s, %s, %s)",
172  array('integer','integer','text','float','integer','text','integer'),
173  array(
174  $next_id,
175  $this->getId(),
176  ilRTE::_replaceMediaObjectImageSrc($answer_obj->getAnswertext(), 0),
177  $answer_obj->getPoints(),
178  $answer_obj->getOrder(),
179  $answer_obj->getImage(),
180  time()
181  )
182  );
183  }
184 
185  $this->rebuildThumbnails();
186 
188  }
189 
190  /*
191  * Rebuild the thumbnail images with a new thumbnail size
192  */
193  protected function rebuildThumbnails()
194  {
195  if ($this->isSingleline && ($this->getThumbSize()))
196  {
197  foreach ($this->getAnswers() as $answer)
198  {
199  if (strlen($answer->getImage()))
200  {
201  $this->generateThumbForFile($this->getImagePath(), $answer->getImage());
202  }
203  }
204  }
205  }
206 
207  public function getThumbPrefix()
208  {
209  return "thumb.";
210  }
211 
212  protected function generateThumbForFile($path, $file)
213  {
214  $filename = $path . $file;
215  if (@file_exists($filename))
216  {
217  $thumbpath = $path . $this->getThumbPrefix() . $file;
218  $path_info = @pathinfo($filename);
219  $ext = "";
220  switch (strtoupper($path_info['extension']))
221  {
222  case 'PNG':
223  $ext = 'PNG';
224  break;
225  case 'GIF':
226  $ext = 'GIF';
227  break;
228  default:
229  $ext = 'JPEG';
230  break;
231  }
232  ilUtil::convertImage($filename, $thumbpath, $ext, $this->getThumbSize());
233  }
234  }
235 
243  function loadFromDb($question_id)
244  {
245  global $ilDB;
246 
247  $hasimages = 0;
248 
249  $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",
250  array("integer"),
251  array($question_id)
252  );
253  if ($result->numRows() == 1)
254  {
255  $data = $ilDB->fetchAssoc($result);
256  $this->setId($question_id);
257  $this->setObjId($data["obj_fi"]);
258  $this->setTitle($data["title"]);
259  $this->setNrOfTries($data['nr_of_tries']);
260  $this->setComment($data["description"]);
261  $this->setOriginalId($data["original_id"]);
262  $this->setAuthor($data["author"]);
263  $this->setPoints($data["points"]);
264  $this->setOwner($data["owner"]);
265  include_once("./Services/RTE/classes/class.ilRTE.php");
266  $this->setQuestion(ilRTE::_replaceMediaObjectImageSrc($data["question_text"], 1));
267  $shuffle = (is_null($data['shuffle'])) ? true : $data['shuffle'];
268  $this->setShuffle($shuffle);
269  $this->setEstimatedWorkingTime(substr($data["working_time"], 0, 2), substr($data["working_time"], 3, 2), substr($data["working_time"], 6, 2));
270  $this->setThumbSize($data['thumb_size']);
271  $this->isSingleline = ($data['allow_images']) ? false : true;
272  $this->lastChange = $data['tstamp'];
273  }
274 
275  $result = $ilDB->queryF("SELECT * FROM qpl_a_sc WHERE question_fi = %s ORDER BY aorder ASC",
276  array('integer'),
277  array($question_id)
278  );
279  include_once "./Modules/TestQuestionPool/classes/class.assAnswerBinaryStateImage.php";
280  if ($result->numRows() > 0)
281  {
282  while ($data = $ilDB->fetchAssoc($result))
283  {
284  $imagefilename = $this->getImagePath() . $data["imagefile"];
285  if (!@file_exists($imagefilename))
286  {
287  $data["imagefile"] = "";
288  }
289  include_once("./Services/RTE/classes/class.ilRTE.php");
290  $data["answertext"] = ilRTE::_replaceMediaObjectImageSrc($data["answertext"], 1);
291  array_push($this->answers, new ASS_AnswerBinaryStateImage($data["answertext"], $data["points"], $data["aorder"], 1, $data["imagefile"]));
292  }
293  }
294 
295  parent::loadFromDb($question_id);
296  }
297 
303  function duplicate($for_test = true, $title = "", $author = "", $owner = "", $testObjId = null)
304  {
305  if ($this->id <= 0)
306  {
307  // The question has not been saved. It cannot be duplicated
308  return;
309  }
310  // duplicate the question in database
311  $this_id = $this->getId();
312 
313  if( (int)$testObjId > 0 )
314  {
315  $thisObjId = $this->getObjId();
316  }
317 
318  $clone = $this;
319  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
321  $clone->id = -1;
322 
323  if( (int)$testObjId > 0 )
324  {
325  $clone->setObjId($testObjId);
326  }
327 
328  if ($title)
329  {
330  $clone->setTitle($title);
331  }
332 
333  if ($author)
334  {
335  $clone->setAuthor($author);
336  }
337  if ($owner)
338  {
339  $clone->setOwner($owner);
340  }
341  if ($for_test)
342  {
343  $clone->saveToDb($original_id);
344  }
345  else
346  {
347  $clone->saveToDb();
348  }
349 
350  // copy question page content
351  $clone->copyPageOfQuestion($this_id);
352 
353  // copy XHTML media objects
354  $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
355  // duplicate the images
356  $clone->duplicateImages($this_id, $thisObjId);
357  // duplicate the generic feedback
358  $clone->duplicateFeedbackGeneric($this_id);
359  // duplicate the answer specific feedback
360  $clone->duplicateFeedbackAnswer($this_id);
361 
362  $clone->onDuplicate($thisObjId, $this_id, $clone->getObjId(), $clone->getId());
363 
364  return $clone->id;
365  }
366 
372  function copyObject($target_questionpool, $title = "")
373  {
374  if ($this->id <= 0)
375  {
376  // The question has not been saved. It cannot be duplicated
377  return;
378  }
379  // duplicate the question in database
380  $clone = $this;
381  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
383  $clone->id = -1;
384  $source_questionpool = $this->getObjId();
385  $clone->setObjId($target_questionpool);
386  if ($title)
387  {
388  $clone->setTitle($title);
389  }
390  $clone->saveToDb();
391  // copy question page content
392  $clone->copyPageOfQuestion($original_id);
393  // copy XHTML media objects
394  $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
395  // duplicate the image
396  $clone->copyImages($original_id, $source_questionpool);
397  // duplicate the generic feedback
398  $clone->duplicateFeedbackGeneric($original_id);
399  // duplicate the answer specific feedback
400  $clone->duplicateFeedbackAnswer($original_id);
401  $clone->onCopy($this->getObjId(), $this->getId());
402 
403  return $clone->id;
404  }
405 
413  function getOutputType()
414  {
415  return $this->output_type;
416  }
417 
426  {
427  $this->output_type = $output_type;
428  }
429 
443  function addAnswer(
444  $answertext = "",
445  $points = 0.0,
446  $order = 0,
447  $answerimage = ""
448  )
449  {
450  include_once "./Modules/TestQuestionPool/classes/class.assAnswerBinaryStateImage.php";
451  if (array_key_exists($order, $this->answers))
452  {
453  // insert answer
454  $answer = new ASS_AnswerBinaryStateImage($answertext, $points, $order, 1, $answerimage);
455  $newchoices = array();
456  for ($i = 0; $i < $order; $i++)
457  {
458  array_push($newchoices, $this->answers[$i]);
459  }
460  array_push($newchoices, $answer);
461  for ($i = $order; $i < count($this->answers); $i++)
462  {
463  $changed = $this->answers[$i];
464  $changed->setOrder($i+1);
465  array_push($newchoices, $changed);
466  }
467  $this->answers = $newchoices;
468  }
469  else
470  {
471  // add answer
472  $answer = new ASS_AnswerBinaryStateImage($answertext, $points, count($this->answers), 1, $answerimage);
473  array_push($this->answers, $answer);
474  }
475  }
476 
484  function getAnswerCount()
485  {
486  return count($this->answers);
487  }
488 
498  function getAnswer($index = 0)
499  {
500  if ($index < 0) return NULL;
501  if (count($this->answers) < 1) return NULL;
502  if ($index >= count($this->answers)) return NULL;
503 
504  return $this->answers[$index];
505  }
506 
515  function deleteAnswer($index = 0)
516  {
517  if ($index < 0) return;
518  if (count($this->answers) < 1) return;
519  if ($index >= count($this->answers)) return;
520  $answer = $this->answers[$index];
521  if (strlen($answer->getImage())) $this->deleteImage($answer->getImage());
522  unset($this->answers[$index]);
523  $this->answers = array_values($this->answers);
524  for ($i = 0; $i < count($this->answers); $i++)
525  {
526  if ($this->answers[$i]->getOrder() > $index)
527  {
528  $this->answers[$i]->setOrder($i);
529  }
530  }
531  }
532 
539  function flushAnswers()
540  {
541  $this->answers = array();
542  }
543 
550  function getMaximumPoints()
551  {
552  $points = 0;
553  foreach ($this->answers as $key => $value)
554  {
555  if ($value->getPoints() > $points)
556  {
557  $points = $value->getPoints();
558  }
559  }
560  return $points;
561  }
562 
572  function calculateReachedPoints($active_id, $pass = NULL)
573  {
574  global $ilDB;
575 
576  $found_values = array();
577  if (is_null($pass))
578  {
579  $pass = $this->getSolutionMaxPass($active_id);
580  }
581  $result = $ilDB->queryF("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
582  array('integer','integer','integer'),
583  array($active_id, $this->getId(), $pass)
584  );
585  while ($data = $ilDB->fetchAssoc($result))
586  {
587  if (strcmp($data["value1"], "") != 0)
588  {
589  array_push($found_values, $data["value1"]);
590  }
591  }
592  $points = 0;
593  foreach ($this->answers as $key => $answer)
594  {
595  if (count($found_values) > 0)
596  {
597  if (in_array($key, $found_values))
598  {
599  $points += $answer->getPoints();
600  }
601  }
602  }
603 
604  $points = parent::calculateReachedPoints($active_id, $pass = NULL, $points);
605  return $points;
606  }
607 
616  function saveWorkingData($active_id, $pass = NULL)
617  {
618  global $ilDB;
619  global $ilUser;
620 
621  if (is_null($pass))
622  {
623  include_once "./Modules/Test/classes/class.ilObjTest.php";
624  $pass = ilObjTest::_getPass($active_id);
625  }
626  $entered_values = 0;
627 
628  $result = $ilDB->queryF("SELECT solution_id FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
629  array('integer','integer','integer'),
630  array($active_id, $this->getId(), $pass)
631  );
632  $row = $ilDB->fetchAssoc($result);
633  $update = $row["solution_id"];
634 
635  if ($update)
636  {
637  if (strlen($_POST["multiple_choice_result"]))
638  {
639  $affectedRows = $ilDB->update("tst_solutions", array(
640  "value1" => array("clob", $_POST["multiple_choice_result"]),
641  "tstamp" => array("integer", time())
642  ), array(
643  "solution_id" => array("integer", $update)
644  ));
645  $entered_values++;
646  }
647  else
648  {
649  $affectedRows = $ilDB->manipulateF("DELETE FROM tst_solutions WHERE solution_id = %s",
650  array('integer'),
651  array($update)
652  );
653  }
654  }
655  else
656  {
657  if (strlen($_POST["multiple_choice_result"]))
658  {
659  $next_id = $ilDB->nextId('tst_solutions');
660  $affectedRows = $ilDB->insert("tst_solutions", array(
661  "solution_id" => array("integer", $next_id),
662  "active_fi" => array("integer", $active_id),
663  "question_fi" => array("integer", $this->getId()),
664  "value1" => array("clob", $_POST['multiple_choice_result']),
665  "value2" => array("clob", null),
666  "pass" => array("integer", $pass),
667  "tstamp" => array("integer", time())
668  ));
669  $entered_values++;
670  }
671  }
672  if ($entered_values)
673  {
674  include_once ("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
676  {
677  $this->logAction($this->lng->txtlng("assessment", "log_user_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
678  }
679  }
680  else
681  {
682  include_once ("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
684  {
685  $this->logAction($this->lng->txtlng("assessment", "log_user_not_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
686  }
687  }
688  parent::saveWorkingData($active_id, $pass);
689  return true;
690  }
691 
697  function syncFeedbackSingleAnswers()
698  {
699  global $ilDB;
700 
701  $feedback = "";
702 
703  // delete generic feedback of the original
704  $affectedRows = $ilDB->manipulateF("DELETE FROM qpl_fb_sc WHERE question_fi = %s",
705  array('integer'),
706  array($this->original_id)
707  );
708 
709  // get generic feedback of the actual question
710  $result = $ilDB->queryF("SELECT * FROM qpl_fb_sc WHERE question_fi = %s",
711  array('integer'),
712  array($this->getId())
713  );
714 
715  // save generic feedback to the original
716  if ($result->numRows())
717  {
718  while ($row = $ilDB->fetchAssoc($result))
719  {
720  $next_id = $ilDB->nextId('qpl_fb_sc');
721 
723  $ilDB->insert('qpl_fb_sc', array(
724  'feedback_id' => array( 'integer', $next_id ),
725  'question_fi' => array( 'integer', $this->original_id ),
726  'answer' => array( 'integer', $row["answer"] ),
727  'feedback' => array( 'clob', $row["feedback"] ),
728  'tstamp' => array( 'integer', time() ),
729  )
730  );
731 
732  }
733  }
734  }
735 
736  function syncWithOriginal()
737  {
738  if ($this->getOriginalId())
739  {
740  $this->syncFeedbackSingleAnswers();
741  $this->syncImages();
743  }
744  }
745 
752  function getQuestionType()
753  {
754  return "assSingleChoice";
755  }
756 
764  {
765  return "qpl_qst_sc";
766  }
767 
775  {
776  return "qpl_a_sc";
777  }
778 
787  function setImageFile($image_filename, $image_tempfilename = "")
788  {
789  $result = 0;
790  if (!empty($image_tempfilename))
791  {
792  $image_filename = str_replace(" ", "_", $image_filename);
793  $imagepath = $this->getImagePath();
794  if (!file_exists($imagepath))
795  {
796  ilUtil::makeDirParents($imagepath);
797  }
798  //if (!move_uploaded_file($image_tempfilename, $imagepath . $image_filename))
799  if (!ilUtil::moveUploadedFile($image_tempfilename, $image_filename, $imagepath.$image_filename))
800  {
801  $result = 2;
802  }
803  else
804  {
805  include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
806  $mimetype = ilObjMediaObject::getMimeType($imagepath . $image_filename);
807  if (!preg_match("/^image/", $mimetype))
808  {
809  unlink($imagepath . $image_filename);
810  $result = 1;
811  }
812  else
813  {
814  // create thumbnail file
815  if ($this->isSingleline && ($this->getThumbSize()))
816  {
817  $this->generateThumbForFile($imagepath, $image_filename);
818  }
819  }
820  }
821  }
822  return $result;
823  }
824 
831  function deleteImage($image_filename)
832  {
833  $imagepath = $this->getImagePath();
834  @unlink($imagepath . $image_filename);
835  $thumbpath = $imagepath . $this->getThumbPrefix() . $image_filename;
836  @unlink($thumbpath);
837  }
838 
839  function duplicateImages($question_id, $objectId = null)
840  {
841  global $ilLog;
842  $imagepath = $this->getImagePath();
843  $imagepath_original = str_replace("/$this->id/images", "/$question_id/images", $imagepath);
844 
845  if( (int)$objectId > 0 )
846  {
847  $imagepath_original = str_replace("/$this->obj_id/", "/$objectId/", $imagepath_original);
848  }
849 
850  foreach ($this->answers as $answer)
851  {
852  $filename = $answer->getImage();
853  if (strlen($filename))
854  {
855  if (!file_exists($imagepath))
856  {
857  ilUtil::makeDirParents($imagepath);
858  }
859  if (!@copy($imagepath_original . $filename, $imagepath . $filename))
860  {
861  $ilLog->write("image could not be duplicated!!!!", $ilLog->ERROR);
862  $ilLog->write("object: " . print_r($this, TRUE), $ilLog->ERROR);
863  }
864  if (@file_exists($imagepath_original. $this->getThumbPrefix(). $filename))
865  {
866  if (!@copy($imagepath_original . $this->getThumbPrefix() . $filename, $imagepath . $this->getThumbPrefix() . $filename))
867  {
868  $ilLog->write("image thumbnail could not be duplicated!!!!", $ilLog->ERROR);
869  $ilLog->write("object: " . print_r($this, TRUE), $ilLog->ERROR);
870  }
871  }
872  }
873  }
874  }
875 
876  function copyImages($question_id, $source_questionpool)
877  {
878  global $ilLog;
879  $imagepath = $this->getImagePath();
880  $imagepath_original = str_replace("/$this->id/images", "/$question_id/images", $imagepath);
881  $imagepath_original = str_replace("/$this->obj_id/", "/$source_questionpool/", $imagepath_original);
882  foreach ($this->answers as $answer)
883  {
884  $filename = $answer->getImage();
885  if (strlen($filename))
886  {
887  if (!file_exists($imagepath))
888  {
889  ilUtil::makeDirParents($imagepath);
890  }
891  if (!@copy($imagepath_original . $filename, $imagepath . $filename))
892  {
893  $ilLog->write("image could not be duplicated!!!!", $ilLog->ERROR);
894  $ilLog->write("object: " . print_r($this, TRUE), $ilLog->ERROR);
895  }
896  if (@file_exists($imagepath_original. $this->getThumbPrefix(). $filename))
897  {
898  if (!@copy($imagepath_original . $this->getThumbPrefix() . $filename, $imagepath . $this->getThumbPrefix() . $filename))
899  {
900  $ilLog->write("image thumbnail could not be duplicated!!!!", $ilLog->ERROR);
901  $ilLog->write("object: " . print_r($this, TRUE), $ilLog->ERROR);
902  }
903  }
904  }
905  }
906  }
907 
911  protected function syncImages()
912  {
913  global $ilLog;
914  $question_id = $this->getOriginalId();
915  $imagepath = $this->getImagePath();
916  $imagepath_original = str_replace("/$this->id/images", "/$question_id/images", $imagepath);
917  ilUtil::delDir($imagepath_original);
918  foreach ($this->answers as $answer)
919  {
920  $filename = $answer->getImage();
921  if (strlen($filename))
922  {
923  if (@file_exists($imagepath . $filename))
924  {
925  if (!file_exists($imagepath))
926  {
927  ilUtil::makeDirParents($imagepath);
928  }
929  if (!file_exists($imagepath_original))
930  {
931  ilUtil::makeDirParents($imagepath_original);
932  }
933  if (!@copy($imagepath . $filename, $imagepath_original . $filename))
934  {
935  $ilLog->write("image could not be duplicated!!!!", $ilLog->ERROR);
936  $ilLog->write("object: " . print_r($this, TRUE), $ilLog->ERROR);
937  }
938  }
939  if (@file_exists($imagepath . $this->getThumbPrefix() . $filename))
940  {
941  if (!@copy($imagepath . $this->getThumbPrefix() . $filename, $imagepath_original . $this->getThumbPrefix() . $filename))
942  {
943  $ilLog->write("image thumbnail could not be duplicated!!!!", $ilLog->ERROR);
944  $ilLog->write("object: " . print_r($this, TRUE), $ilLog->ERROR);
945  }
946  }
947  }
948  }
949  }
950 
958  function saveFeedbackSingleAnswer($answer_index, $feedback)
959  {
960  global $ilDB;
961 
962  $affectedRows = $ilDB->manipulateF("DELETE FROM qpl_fb_sc WHERE question_fi = %s AND answer = %s",
963  array('integer','integer'),
964  array($this->getId(), $answer_index)
965  );
966  if (strlen($feedback))
967  {
968  include_once("./Services/RTE/classes/class.ilRTE.php");
969  $next_id = $ilDB->nextId('qpl_fb_sc');
970 
972  $ilDB->insert('qpl_fb_sc', array(
973  'feedback_id' => array( 'integer', $next_id ),
974  'question_fi' => array( 'integer', $this->getId() ),
975  'answer' => array( 'integer', $answer_index ),
976  'feedback' => array( 'clob', ilRTE::_replaceMediaObjectImageSrc($feedback, 0) ),
977  'tstamp' => array( 'integer', time() ),
978  )
979  );
980  }
981  }
982 
990  function getFeedbackSingleAnswer($answer_index)
991  {
992  global $ilDB;
993 
994  $feedback = "";
995  $result = $ilDB->queryF("SELECT * FROM qpl_fb_sc WHERE question_fi = %s AND answer = %s",
996  array('integer','integer'),
997  array($this->getId(), $answer_index)
998  );
999  if ($result->numRows())
1000  {
1001  $row = $ilDB->fetchAssoc($result);
1002  include_once("./Services/RTE/classes/class.ilRTE.php");
1003  $feedback = ilRTE::_replaceMediaObjectImageSrc($row["feedback"], 1);
1004  }
1005  return $feedback;
1006  }
1007 
1014  function duplicateFeedbackAnswer($original_id)
1015  {
1016  global $ilDB;
1017 
1018  $feedback = "";
1019  $result = $ilDB->queryF("SELECT * FROM qpl_fb_sc WHERE question_fi = %s",
1020  array('integer'),
1021  array($original_id)
1022  );
1023  if ($result->numRows())
1024  {
1025  while ($row = $ilDB->fetchAssoc($result))
1026  {
1027  $next_id = $ilDB->nextId('qpl_fb_sc');
1028 
1030  $ilDB->insert('qpl_fb_sc', array(
1031  'feedback_id' => array( 'integer', $next_id ),
1032  'question_fi' => array( 'integer', $this->getId() ),
1033  'answer' => array( 'integer', $row["answer"] ),
1034  'feedback' => array( 'clob', $row["feedback"] ),
1035  'tstamp' => array( 'integer', time() ),
1036  )
1037  );
1038  }
1039  }
1040  }
1041 
1047  {
1049  foreach ($this->answers as $index => $answer)
1050  {
1051  $text .= $this->getFeedbackSingleAnswer($index);
1052  $answer_obj = $this->answers[$index];
1053  $text .= $answer_obj->getAnswertext();
1054  }
1055  return $text;
1056  }
1057 
1061  function &getAnswers()
1062  {
1063  return $this->answers;
1064  }
1065 
1078  public function setExportDetailsXLS(&$worksheet, $startrow, $active_id, $pass, &$format_title, &$format_bold)
1079  {
1080  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
1081  $solution = $this->getSolutionValues($active_id, $pass);
1082  $worksheet->writeString($startrow, 0, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())), $format_title);
1083  $worksheet->writeString($startrow, 1, ilExcelUtils::_convert_text($this->getTitle()), $format_title);
1084  $i = 1;
1085  foreach ($this->getAnswers() as $id => $answer)
1086  {
1087  $worksheet->writeString($startrow + $i, 0, ilExcelUtils::_convert_text($answer->getAnswertext()), $format_bold);
1088  if ($id == $solution[0]["value1"])
1089  {
1090  $worksheet->write($startrow + $i, 1, 1);
1091  }
1092  else
1093  {
1094  $worksheet->write($startrow + $i, 1, 0);
1095  }
1096  $i++;
1097  }
1098  return $startrow + $i + 1;
1099  }
1100 
1101  public function getThumbSize()
1102  {
1103  return $this->thumb_size;
1104  }
1105 
1106  public function setThumbSize($a_size)
1107  {
1108  $this->thumb_size = $a_size;
1109  }
1110 
1114  public function toJSON()
1115  {
1116  include_once("./Services/RTE/classes/class.ilRTE.php");
1117  $result = array();
1118  $result['id'] = (int) $this->getId();
1119  $result['type'] = (string) $this->getQuestionType();
1120  $result['title'] = (string) $this->getTitle();
1121  $result['question'] = $this->formatSAQuestion($this->getQuestion());
1122  $result['nr_of_tries'] = (int) $this->getNrOfTries();
1123  $result['shuffle'] = (bool) $this->getShuffle();
1124  $result['feedback'] = array(
1125  "onenotcorrect" => ilRTE::_replaceMediaObjectImageSrc($this->getFeedbackGeneric(0), 0),
1126  "allcorrect" => ilRTE::_replaceMediaObjectImageSrc($this->getFeedbackGeneric(1), 0)
1127  );
1128 
1129  $answers = array();
1130  $has_image = false;
1131  foreach ($this->getAnswers() as $key => $answer_obj)
1132  {
1133  if((string) $answer_obj->getImage())
1134  {
1135  $has_image = true;
1136  }
1137  array_push($answers, array(
1138  "answertext" => (string) $answer_obj->getAnswertext(),
1139  "points" => (float)$answer_obj->getPoints(),
1140  "order" => (int)$answer_obj->getOrder(),
1141  "image" => (string) $answer_obj->getImage(),
1142  "feedback" => ilRTE::_replaceMediaObjectImageSrc($this->getFeedbackSingleAnswer($key), 0)
1143  ));
1144  }
1145  $result['answers'] = $answers;
1146  if($has_image)
1147  {
1148  $result['path'] = $this->getImagePathWeb();
1149  $result['thumb'] = $this->getThumbSize();
1150  }
1151 
1152  $mobs = ilObjMediaObject::_getMobsOfObject("qpl:html", $this->getId());
1153  $result['mobs'] = $mobs;
1154 
1155  return json_encode($result);
1156  }
1157 
1158  public function removeAnswerImage($index)
1159  {
1160  $answer = $this->answers[$index];
1161  if (is_object($answer))
1162  {
1163  $this->deleteImage($answer->getImage());
1164  $answer->setImage('');
1165  }
1166  }
1167 
1168  function createRandomSolution($active_id, $pass)
1169  {
1170  $value = rand(0, count($this->answers)-1);
1171  $_POST["multiple_choice_result"] = (strlen($value)) ? (string)$value : '0';
1172  $this->saveWorkingData($active_id, $pass);
1173  }
1174 
1176  {
1177  global $ilUser;
1178 
1179  $multilineAnswerSetting = $ilUser->getPref("tst_multiline_answers");
1180  if ($multilineAnswerSetting != 1)
1181  {
1182  $multilineAnswerSetting = 0;
1183  }
1184  return $multilineAnswerSetting;
1185  }
1186 
1187  function setMultilineAnswerSetting($a_setting = 0)
1188  {
1189  global $ilUser;
1190  $ilUser->writePref("tst_multiline_answers", $a_setting);
1191  }
1192 }
1193 
1194 ?>