ILIAS  release_7 Revision v7.30-3-g800a261c036
class.assSingleChoice.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once './Modules/TestQuestionPool/classes/class.assQuestion.php';
5require_once './Modules/Test/classes/inc.AssessmentConstants.php';
6require_once './Modules/TestQuestionPool/interfaces/interface.ilObjQuestionScoringAdjustable.php';
7require_once './Modules/TestQuestionPool/interfaces/interface.ilObjAnswerScoringAdjustable.php';
8require_once './Modules/TestQuestionPool/interfaces/interface.iQuestionCondition.php';
9require_once './Modules/TestQuestionPool/classes/class.ilUserQuestionResult.php';
10require_once 'Modules/TestQuestionPool/interfaces/interface.ilAssSpecificFeedbackOptionLabelProvider.php';
11
26{
34 public $answers;
35
45
51 protected $thumb_size;
52
60
75 public function __construct(
76 $title = "",
77 $comment = "",
78 $author = "",
79 $owner = -1,
80 $question = "",
82 ) {
84 $this->thumb_size = 150;
85 $this->output_type = $output_type;
86 $this->answers = array();
87 $this->shuffle = 1;
88 $this->feedback_setting = 2;
89 }
90
97 public function isComplete()
98 {
99 if (strlen($this->title) and ($this->author) and ($this->question) and (count($this->answers)) and ($this->getMaximumPoints() > 0)) {
100 foreach ($this->answers as $answer) {
101 if ((strlen($answer->getAnswertext()) == 0) && (strlen($answer->getImage()) == 0)) {
102 return false;
103 }
104 }
105 return true;
106 } else {
107 return false;
108 }
109 }
110
117 public function saveToDb($original_id = "")
118 {
120 global $DIC;
121 $ilDB = $DIC['ilDB'];
122
124
125 // kann das weg?
126 $oldthumbsize = 0;
127 if ($this->isSingleline && ($this->getThumbSize())) {
128 // get old thumbnail size
129 $result = $ilDB->queryF(
130 "SELECT thumb_size FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
131 array("integer"),
132 array($this->getId())
133 );
134 if ($result->numRows() == 1) {
135 $data = $ilDB->fetchAssoc($result);
136 $oldthumbsize = $data['thumb_size'];
137 }
138 }
139
140
142
144
145 parent::saveToDb($original_id);
146 }
147
148 /*
149 * Rebuild the thumbnail images with a new thumbnail size
150 */
151 protected function rebuildThumbnails()
152 {
153 if ($this->isSingleline && ($this->getThumbSize())) {
154 foreach ($this->getAnswers() as $answer) {
155 if (strlen($answer->getImage())) {
156 $this->generateThumbForFile($this->getImagePath(), $answer->getImage());
157 }
158 }
159 }
160 }
161
162 public function getThumbPrefix()
163 {
164 return "thumb.";
165 }
166
167 protected function generateThumbForFile($path, $file)
168 {
169 $filename = $path . $file;
170 if (@file_exists($filename)) {
171 $thumbpath = $path . $this->getThumbPrefix() . $file;
172 $path_info = @pathinfo($filename);
173 $ext = "";
174 switch (strtoupper($path_info['extension'])) {
175 case 'PNG':
176 $ext = 'PNG';
177 break;
178 case 'GIF':
179 $ext = 'GIF';
180 break;
181 default:
182 $ext = 'JPEG';
183 break;
184 }
185 ilUtil::convertImage($filename, $thumbpath, $ext, $this->getThumbSize());
186 }
187 }
188
196 public function loadFromDb($question_id)
197 {
198 global $DIC;
199 $ilDB = $DIC['ilDB'];
200
201 $hasimages = 0;
202
203 $result = $ilDB->queryF(
204 "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",
205 array("integer"),
206 array($question_id)
207 );
208 if ($result->numRows() == 1) {
209 $data = $ilDB->fetchAssoc($result);
210 $this->setId($question_id);
211 $this->setObjId($data["obj_fi"]);
212 $this->setTitle($data["title"]);
213 $this->setNrOfTries($data['nr_of_tries']);
214 $this->setComment($data["description"]);
215 $this->setOriginalId($data["original_id"]);
216 $this->setAuthor($data["author"]);
217 $this->setPoints($data["points"]);
218 $this->setOwner($data["owner"]);
219 include_once("./Services/RTE/classes/class.ilRTE.php");
220 $this->setQuestion(ilRTE::_replaceMediaObjectImageSrc($data["question_text"], 1));
221 $shuffle = (is_null($data['shuffle'])) ? true : $data['shuffle'];
222 $this->setShuffle($shuffle);
223 $this->setEstimatedWorkingTime(substr($data["working_time"], 0, 2), substr($data["working_time"], 3, 2), substr($data["working_time"], 6, 2));
224 $this->setThumbSize($data['thumb_size']);
225 $this->isSingleline = ($data['allow_images']) ? false : true;
226 $this->lastChange = $data['tstamp'];
227 $this->feedback_setting = $data['feedback_setting'];
228
229 try {
233 }
234
235 try {
236 $this->setAdditionalContentEditingMode($data['add_cont_edit_mode']);
238 }
239 }
240
241 $result = $ilDB->queryF(
242 "SELECT * FROM qpl_a_sc WHERE question_fi = %s ORDER BY aorder ASC",
243 array('integer'),
244 array($question_id)
245 );
246 include_once "./Modules/TestQuestionPool/classes/class.assAnswerBinaryStateImage.php";
247 if ($result->numRows() > 0) {
248 while ($data = $ilDB->fetchAssoc($result)) {
249 $imagefilename = $this->getImagePath() . $data["imagefile"];
250 if (!@file_exists($imagefilename)) {
251 $data["imagefile"] = "";
252 }
253 include_once("./Services/RTE/classes/class.ilRTE.php");
254 $data["answertext"] = ilRTE::_replaceMediaObjectImageSrc($data["answertext"], 1);
255 array_push($this->answers, new ASS_AnswerBinaryStateImage($data["answertext"], $data["points"], $data["aorder"], 1, $data["imagefile"], $data["answer_id"]));
256 }
257 }
258
259 parent::loadFromDb($question_id);
260 }
261
267 public function duplicate($for_test = true, $title = "", $author = "", $owner = "", $testObjId = null)
268 {
269 if ($this->id <= 0) {
270 // The question has not been saved. It cannot be duplicated
271 return;
272 }
273 // duplicate the question in database
274 $this_id = $this->getId();
275 $thisObjId = $this->getObjId();
276
277 $clone = $this;
278 include_once("./Modules/TestQuestionPool/classes/class.assQuestion.php");
280 $clone->id = -1;
281
282 if ((int) $testObjId > 0) {
283 $clone->setObjId($testObjId);
284 }
285
286 if ($title) {
287 $clone->setTitle($title);
288 }
289
290 if ($author) {
291 $clone->setAuthor($author);
292 }
293 if ($owner) {
294 $clone->setOwner($owner);
295 }
296 if ($for_test) {
297 $clone->saveToDb($original_id);
298 } else {
299 $clone->saveToDb();
300 }
301
302 // copy question page content
303 $clone->copyPageOfQuestion($this_id);
304
305 // copy XHTML media objects
306 $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
307 // duplicate the images
308 $clone->duplicateImages($this_id, $thisObjId);
309
310 $clone->onDuplicate($thisObjId, $this_id, $clone->getObjId(), $clone->getId());
311
312 return $clone->id;
313 }
314
320 public function copyObject($target_questionpool_id, $title = "")
321 {
322 if ($this->id <= 0) {
323 // The question has not been saved. It cannot be duplicated
324 return;
325 }
326 // duplicate the question in database
327 $clone = $this;
328 include_once("./Modules/TestQuestionPool/classes/class.assQuestion.php");
330 $clone->id = -1;
331 $source_questionpool_id = $this->getObjId();
332 $clone->setObjId($target_questionpool_id);
333 if ($title) {
334 $clone->setTitle($title);
335 }
336 $clone->saveToDb();
337 // copy question page content
338 $clone->copyPageOfQuestion($original_id);
339 // copy XHTML media objects
340 $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
341 // duplicate the image
342 $clone->copyImages($original_id, $source_questionpool_id);
343
344 $clone->onCopy($source_questionpool_id, $original_id, $clone->getObjId(), $clone->getId());
345
346 return $clone->id;
347 }
348
349 public function createNewOriginalFromThisDuplicate($targetParentId, $targetQuestionTitle = "")
350 {
351 if ($this->id <= 0) {
352 // The question has not been saved. It cannot be duplicated
353 return;
354 }
355
356 include_once("./Modules/TestQuestionPool/classes/class.assQuestion.php");
357
358 $sourceQuestionId = $this->id;
359 $sourceParentId = $this->getObjId();
360
361 // duplicate the question in database
362 $clone = $this;
363 $clone->id = -1;
364
365 $clone->setObjId($targetParentId);
366
367 if ($targetQuestionTitle) {
368 $clone->setTitle($targetQuestionTitle);
369 }
370
371 $clone->saveToDb();
372 // copy question page content
373 $clone->copyPageOfQuestion($sourceQuestionId);
374 // copy XHTML media objects
375 $clone->copyXHTMLMediaObjectsOfQuestion($sourceQuestionId);
376 // duplicate the image
377 $clone->copyImages($sourceQuestionId, $sourceParentId);
378
379 $clone->onCopy($sourceParentId, $sourceQuestionId, $clone->getObjId(), $clone->getId());
380
381 return $clone->id;
382 }
383
391 public function getOutputType()
392 {
393 return $this->output_type;
394 }
395
404 {
405 $this->output_type = $output_type;
406 }
407
421 public function addAnswer(
422 $answertext = "",
423 $points = 0.0,
424 $order = 0,
425 $answerimage = "",
426 $answer_id = -1
427 ) {
428 include_once "./Modules/TestQuestionPool/classes/class.assAnswerBinaryStateImage.php";
429 $answertext = $this->getHtmlQuestionContentPurifier()->purify($answertext);
430 if (array_key_exists($order, $this->answers)) {
431 // insert answer
432 $answer = new ASS_AnswerBinaryStateImage($answertext, $points, $order, 1, $answerimage, $answer_id);
433 $newchoices = array();
434 for ($i = 0; $i < $order; $i++) {
435 array_push($newchoices, $this->answers[$i]);
436 }
437 array_push($newchoices, $answer);
438 for ($i = $order; $i < count($this->answers); $i++) {
439 $changed = $this->answers[$i];
440 $changed->setOrder($i + 1);
441 array_push($newchoices, $changed);
442 }
443 $this->answers = $newchoices;
444 } else {
445 // add answer
446 $answer = new ASS_AnswerBinaryStateImage($answertext, $points, count($this->answers), 1, $answerimage, $answer_id);
447 array_push($this->answers, $answer);
448 }
449 }
450
458 public function getAnswerCount()
459 {
460 return count($this->answers);
461 }
462
472 public function getAnswer($index = 0)
473 {
474 if ($index < 0) {
475 return null;
476 }
477 if (count($this->answers) < 1) {
478 return null;
479 }
480 if ($index >= count($this->answers)) {
481 return null;
482 }
483
484 return $this->answers[$index];
485 }
486
495 public function deleteAnswer($index = 0)
496 {
497 if ($index < 0) {
498 return;
499 }
500 if (count($this->answers) < 1) {
501 return;
502 }
503 if ($index >= count($this->answers)) {
504 return;
505 }
506 $answer = $this->answers[$index];
507 if (strlen($answer->getImage())) {
508 $this->deleteImage($answer->getImage());
509 }
510 unset($this->answers[$index]);
511 $this->answers = array_values($this->answers);
512 for ($i = 0; $i < count($this->answers); $i++) {
513 if ($this->answers[$i]->getOrder() > $index) {
514 $this->answers[$i]->setOrder($i);
515 }
516 }
517 }
518
525 public function flushAnswers()
526 {
527 $this->answers = array();
528 }
529
536 public function getMaximumPoints()
537 {
538 $points = 0;
539 foreach ($this->answers as $key => $value) {
540 if ($value->getPoints() > $points) {
541 $points = $value->getPoints();
542 }
543 }
544 return $points;
545 }
546
557 public function calculateReachedPoints($active_id, $pass = null, $authorizedSolution = true, $returndetails = false)
558 {
559 if ($returndetails) {
560 throw new ilTestException('return details not implemented for ' . __METHOD__);
561 }
562
563 global $DIC;
564 $ilDB = $DIC['ilDB'];
565
566 $found_values = array();
567 if (is_null($pass)) {
568 $pass = $this->getSolutionMaxPass($active_id);
569 }
570 $result = $this->getCurrentSolutionResultSet($active_id, $pass, $authorizedSolution);
571 while ($data = $ilDB->fetchAssoc($result)) {
572 if (strcmp($data["value1"], "") != 0) {
573 array_push($found_values, $data["value1"]);
574 }
575 }
576 $points = 0;
577 foreach ($this->answers as $key => $answer) {
578 if (count($found_values) > 0) {
579 if (in_array($key, $found_values)) {
580 $points += $answer->getPoints();
581 }
582 }
583 }
584
585 return $points;
586 }
587
589 {
590 $participantSolution = $previewSession->getParticipantsSolution();
591
592 $points = 0;
593
594 foreach ($this->answers as $key => $answer) {
595 if (is_numeric($participantSolution) && $key == $participantSolution) {
596 $points = $answer->getPoints();
597 }
598 }
599
600 $reachedPoints = $this->deductHintPointsFromReachedPoints($previewSession, $points);
601
602 return $this->ensureNonNegativePoints($reachedPoints);
603 }
604
613 public function saveWorkingData($active_id, $pass = null, $authorized = true)
614 {
615 global $DIC;
616 $ilDB = $DIC['ilDB'];
617 $ilUser = $DIC['ilUser'];
618
619 if (is_null($pass)) {
620 include_once "./Modules/Test/classes/class.ilObjTest.php";
621 $pass = ilObjTest::_getPass($active_id);
622 }
623
624 $entered_values = 0;
625
626 $this->getProcessLocker()->executeUserSolutionUpdateLockOperation(function () use (&$entered_values, $ilDB, $active_id, $pass, $authorized) {
627 $result = $this->getCurrentSolutionResultSet($active_id, $pass, $authorized);
628 $row = $ilDB->fetchAssoc($result);
629 $update = $row["solution_id"];
630
631 if ($update) {
632 if (strlen($_POST["multiple_choice_result"])) {
633 $this->updateCurrentSolution($update, $_POST["multiple_choice_result"], null, $authorized);
634 $entered_values++;
635 } else {
636 $this->removeSolutionRecordById($update);
637 }
638 } else {
639 if (strlen($_POST["multiple_choice_result"])) {
640 $this->saveCurrentSolution($active_id, $pass, $_POST['multiple_choice_result'], null, $authorized);
641 $entered_values++;
642 }
643 }
644 });
645
646 if ($entered_values) {
647 include_once("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
649 assQuestion::logAction($this->lng->txtlng("assessment", "log_user_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
650 }
651 } else {
652 include_once("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
654 assQuestion::logAction($this->lng->txtlng("assessment", "log_user_not_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
655 }
656 }
657
658 return true;
659 }
660
661 protected function savePreviewData(ilAssQuestionPreviewSession $previewSession)
662 {
663 if (strlen($_POST['multiple_choice_result' . $this->getId() . 'ID'])) {
664 $previewSession->setParticipantsSolution($_POST['multiple_choice_result' . $this->getId() . 'ID']);
665 } else {
666 $previewSession->setParticipantsSolution(null);
667 }
668 }
669
670 public function saveAdditionalQuestionDataToDb()
671 {
673 global $DIC;
674 $ilDB = $DIC['ilDB'];
675
676 // save additional data
677 $ilDB->manipulateF(
678 "DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
679 array( "integer" ),
680 array( $this->getId() )
681 );
682
683 $ilDB->manipulateF(
684 "INSERT INTO " . $this->getAdditionalTableName(
685 ) . " (question_fi, shuffle, allow_images, thumb_size) VALUES (%s, %s, %s, %s)",
686 array( "integer", "text", "text", "integer" ),
687 array(
688 $this->getId(),
689 $this->getShuffle(),
690 ($this->isSingleline) ? "0" : "1",
691 (strlen($this->getThumbSize()) == 0) ? null : $this->getThumbSize()
692 )
693 );
694 }
695
701 public function saveAnswerSpecificDataToDb()
702 {
704 global $DIC;
705 $ilDB = $DIC['ilDB'];
706
707 if (!$this->isSingleline) {
709 }
710 // Get all feedback entries
711 $result = $ilDB->queryF(
712 "SELECT * FROM qpl_fb_specific WHERE question_fi = %s",
713 ['integer'],
714 [$this->getId()]
715 );
716 $db_feedback = $ilDB->fetchAll($result);
717
718 // Check if feedback exists and the regular editor is used and not the page editor
719 if (sizeof($db_feedback) >= 1 && $this->getAdditionalContentEditingMode() == 'default'){
720 // Get all existing answer data for question
721 $result = $ilDB->queryF(
722 "SELECT answer_id, aorder FROM qpl_a_sc WHERE question_fi = %s",
723 ['integer'],
724 [$this->getId()]
725 );
726 $db_answers = $ilDB->fetchAll($result);
727
728 // Collect old and new order entries by ids and order to calculate a diff/intersection and remove/update feedback
729 $post_answer_order_for_id = [];
730 foreach ($this->answers as $answer){
731 // Only the first appearance of an id is used
732 if ($answer->getId() !== null && !in_array($answer->getId(), array_keys($post_answer_order_for_id))) {
733 // -1 is happening while import and also if a new multi line answer is generated
734 if ($answer->getId() == -1) {
735 continue;
736 }
737 $post_answer_order_for_id[$answer->getId()] = $answer->getOrder();
738 }
739 }
740
741 // If there is no usable ids from post, it's better to not touch the feedback
742 // This is useful since the import is also using this function or the first creation of a new question in general
743 if (sizeof($post_answer_order_for_id) >= 1) {
744 $db_answer_order_for_id = [];
745 $db_answer_id_for_order = [];
746 foreach ($db_answers as $db_answer){
747 $db_answer_order_for_id[intval($db_answer['answer_id'])] = intval($db_answer['aorder']);
748 $db_answer_id_for_order[intval($db_answer['aorder'])] = intval($db_answer['answer_id']);
749 }
750
751 // Handle feedback
752 // the diff between the already existing answer ids from the Database and the answer ids from post
753 // feedback related to the answer ids should be deleted or in our case not recreated.
754 $db_answer_ids = array_keys($db_answer_order_for_id);
755 $post_answer_ids = array_keys($post_answer_order_for_id);
756 $diff_db_post_answer_ids = array_diff($db_answer_ids, $post_answer_ids);
757 $unused_answer_ids = array_keys($diff_db_post_answer_ids);
758
759 // Delete all feedback in the database
760 $this->feedbackOBJ->deleteSpecificAnswerFeedbacks($this->getId(), false);
761 // Recreate feedback
762 foreach ($db_feedback as $feedback_option) {
763 // skip feedback which answer is deleted
764 if (in_array(intval($feedback_option['answer']), $unused_answer_ids)) {
765 continue;
766 }
767
768 // Reorder feedback
769 $feedback_order_db = intval($feedback_option['answer']);
770 $db_answer_id = $db_answer_id_for_order[$feedback_order_db];
771 // This cuts feedback that currently would have no corresponding answer
772 // This case can happen while copying "broken" questions
773 // Or when saving a question with less answers than feedback
774 if (is_null($db_answer_id) || $db_answer_id < 0) {
775 continue;
776 }
777 $feedback_order_post = $post_answer_order_for_id[$db_answer_id];
778 $feedback_option['answer'] = $feedback_order_post;
779
780 // Recreate remaining feedback in database
781 $next_id = $ilDB->nextId('qpl_fb_specific');
782 $ilDB->manipulateF(
783 "INSERT INTO qpl_fb_specific (feedback_id, question_fi, answer, tstamp, feedback, question)
784 VALUES (%s, %s, %s, %s, %s, %s)",
785 ['integer', 'integer', 'integer', 'integer', 'text', 'integer'],
786 [
787 $next_id,
788 $feedback_option['question_fi'],
789 $feedback_option['answer'],
790 time(),
791 $feedback_option['feedback'],
792 $feedback_option['question']
793 ]
794 );
795 }
796 }
797 }
798
799 // Delete all entries in qpl_a_sc for question
800 $ilDB->manipulateF(
801 "DELETE FROM qpl_a_sc WHERE question_fi = %s",
802 ['integer'],
803 [$this->getId()]
804 );
805
806 // Recreate answers one by one
807 foreach ($this->answers as $key => $value) {
809 $answer_obj = $this->answers[$key];
810 $next_id = $ilDB->nextId('qpl_a_sc');
811 $ilDB->manipulateF(
812 "INSERT INTO qpl_a_sc (answer_id, question_fi, answertext, points, aorder, imagefile, tstamp) VALUES (%s, %s, %s, %s, %s, %s, %s)",
813 ['integer', 'integer', 'text', 'float', 'integer', 'text', 'integer'],
814 [
815 $next_id,
816 $this->getId(),
817 ilRTE::_replaceMediaObjectImageSrc($answer_obj->getAnswertext(), 0),
818 $answer_obj->getPoints(),
819 $answer_obj->getOrder(),
820 $answer_obj->getImage(),
821 time()
822 ]
823 );
824 }
825 $this->rebuildThumbnails();
826 }
827
834 public function getQuestionType()
835 {
836 return "assSingleChoice";
837 }
838
845 public function getAdditionalTableName()
846 {
847 return "qpl_qst_sc";
848 }
849
856 public function getAnswerTableName()
857 {
858 return "qpl_a_sc";
859 }
860
869 public function setImageFile($image_filename, $image_tempfilename = "")
870 {
871 $result = 0;
872 if (!empty($image_tempfilename)) {
873 $image_filename = str_replace(" ", "_", $image_filename);
874 $imagepath = $this->getImagePath();
875 if (!file_exists($imagepath)) {
876 ilUtil::makeDirParents($imagepath);
877 }
878 //if (!move_uploaded_file($image_tempfilename, $imagepath . $image_filename))
879 if (!ilUtil::moveUploadedFile($image_tempfilename, $image_filename, $imagepath . $image_filename)) {
880 $result = 2;
881 } else {
882 include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
883 $mimetype = ilObjMediaObject::getMimeType($imagepath . $image_filename);
884 if (!preg_match("/^image/", $mimetype)) {
885 unlink($imagepath . $image_filename);
886 $result = 1;
887 } else {
888 // create thumbnail file
889 if ($this->isSingleline && ($this->getThumbSize())) {
890 $this->generateThumbForFile($imagepath, $image_filename);
891 }
892 }
893 }
894 }
895 return $result;
896 }
897
904 public function deleteImage($image_filename)
905 {
906 $imagepath = $this->getImagePath();
907 @unlink($imagepath . $image_filename);
908 $thumbpath = $imagepath . $this->getThumbPrefix() . $image_filename;
909 @unlink($thumbpath);
910 }
911
912 public function duplicateImages($question_id, $objectId = null)
913 {
914 global $DIC;
915 $ilLog = $DIC['ilLog'];
916 $imagepath = $this->getImagePath();
917 $imagepath_original = str_replace("/$this->id/images", "/$question_id/images", $imagepath);
918
919 if ((int) $objectId > 0) {
920 $imagepath_original = str_replace("/$this->obj_id/", "/$objectId/", $imagepath_original);
921 }
922
923 foreach ($this->answers as $answer) {
924 $filename = $answer->getImage();
925 if (strlen($filename)) {
926 if (!file_exists($imagepath)) {
927 ilUtil::makeDirParents($imagepath);
928 }
929 if (!@copy($imagepath_original . $filename, $imagepath . $filename)) {
930 $ilLog->write("image could not be duplicated!!!!", $ilLog->ERROR);
931 $ilLog->write("object: " . print_r($this, true), $ilLog->ERROR);
932 }
933 if (@file_exists($imagepath_original . $this->getThumbPrefix() . $filename)) {
934 if (!@copy($imagepath_original . $this->getThumbPrefix() . $filename, $imagepath . $this->getThumbPrefix() . $filename)) {
935 $ilLog->write("image thumbnail could not be duplicated!!!!", $ilLog->ERROR);
936 $ilLog->write("object: " . print_r($this, true), $ilLog->ERROR);
937 }
938 }
939 }
940 }
941 }
942
943 public function copyImages($question_id, $source_questionpool)
944 {
946 global $DIC;
947 $ilLog = $DIC['ilLog'];
948
949 $imagepath = $this->getImagePath();
950 $imagepath_original = str_replace("/$this->id/images", "/$question_id/images", $imagepath);
951 $imagepath_original = str_replace("/$this->obj_id/", "/$source_questionpool/", $imagepath_original);
952 foreach ($this->answers as $answer) {
953 $filename = $answer->getImage();
954 if (strlen($filename)) {
955 if (!file_exists($imagepath)) {
956 ilUtil::makeDirParents($imagepath);
957 }
958
959 if (file_exists($imagepath_original . $filename)) {
960 if (!copy($imagepath_original . $filename, $imagepath . $filename)) {
961 $ilLog->warning(sprintf(
962 "Could not clone source image '%s' to '%s' (srcQuestionId: %s|tgtQuestionId: %s|srcParentObjId: %s|tgtParentObjId: %s)",
963 $imagepath_original . $filename,
964 $imagepath . $filename,
965 $question_id,
966 $this->id,
967 $source_questionpool,
968 $this->obj_id
969 ));
970 }
971 }
972
973 if (file_exists($imagepath_original . $this->getThumbPrefix() . $filename)) {
974 if (!copy($imagepath_original . $this->getThumbPrefix() . $filename, $imagepath . $this->getThumbPrefix() . $filename)) {
975 $ilLog->warning(sprintf(
976 "Could not clone thumbnail source image '%s' to '%s' (srcQuestionId: %s|tgtQuestionId: %s|srcParentObjId: %s|tgtParentObjId: %s)",
977 $imagepath_original . $this->getThumbPrefix() . $filename,
978 $imagepath . $this->getThumbPrefix() . $filename,
979 $question_id,
980 $this->id,
981 $source_questionpool,
982 $this->obj_id
983 ));
984 }
985 }
986 }
987 }
988 }
989
993 protected function syncImages()
994 {
995 global $DIC;
996 $ilLog = $DIC['ilLog'];
997 $question_id = $this->getOriginalId();
998 $imagepath = $this->getImagePath();
999 $imagepath_original = str_replace("/$this->id/images", "/$question_id/images", $imagepath);
1000 ilUtil::delDir($imagepath_original);
1001 foreach ($this->answers as $answer) {
1002 $filename = $answer->getImage();
1003 if (strlen($filename)) {
1004 if (@file_exists($imagepath . $filename)) {
1005 if (!file_exists($imagepath)) {
1006 ilUtil::makeDirParents($imagepath);
1007 }
1008 if (!file_exists($imagepath_original)) {
1009 ilUtil::makeDirParents($imagepath_original);
1010 }
1011 if (!@copy($imagepath . $filename, $imagepath_original . $filename)) {
1012 $ilLog->write("image could not be duplicated!!!!", $ilLog->ERROR);
1013 $ilLog->write("object: " . print_r($this, true), $ilLog->ERROR);
1014 }
1015 }
1016 if (@file_exists($imagepath . $this->getThumbPrefix() . $filename)) {
1017 if (!@copy($imagepath . $this->getThumbPrefix() . $filename, $imagepath_original . $this->getThumbPrefix() . $filename)) {
1018 $ilLog->write("image thumbnail could not be duplicated!!!!", $ilLog->ERROR);
1019 $ilLog->write("object: " . print_r($this, true), $ilLog->ERROR);
1020 }
1021 }
1022 }
1023 }
1024 }
1025
1031 {
1032 $text = parent::getRTETextWithMediaObjects();
1033 foreach ($this->answers as $index => $answer) {
1034 $text .= $this->feedbackOBJ->getSpecificAnswerFeedbackContent($this->getId(), 0, $index);
1035 $answer_obj = $this->answers[$index];
1036 $text .= $answer_obj->getAnswertext();
1037 }
1038 return $text;
1039 }
1040
1044 public function &getAnswers()
1045 {
1046 return $this->answers;
1047 }
1048
1052 public function setExportDetailsXLS($worksheet, $startrow, $active_id, $pass)
1053 {
1054 parent::setExportDetailsXLS($worksheet, $startrow, $active_id, $pass);
1055
1056 $solution = $this->getSolutionValues($active_id, $pass);
1057 $i = 1;
1058 foreach ($this->getAnswers() as $id => $answer) {
1059 $worksheet->setCell($startrow + $i, 0, $answer->getAnswertext());
1060 $worksheet->setBold($worksheet->getColumnCoord(0) . ($startrow + $i));
1061 if (
1062 count($solution) > 0 &&
1063 isset($solution[0]) &&
1064 is_array($solution[0]) &&
1065 strlen($solution[0]['value1']) > 0 && $id == $solution[0]['value1']
1066 ) {
1067 $worksheet->setCell($startrow + $i, 2, 1);
1068 } else {
1069 $worksheet->setCell($startrow + $i, 2, 0);
1070 }
1071 $i++;
1072 }
1073
1074 return $startrow + $i + 1;
1075 }
1076
1077 public function getThumbSize()
1078 {
1079 return $this->thumb_size;
1080 }
1081
1082 public function setThumbSize($a_size)
1083 {
1084 $this->thumb_size = $a_size;
1085 }
1086
1091 {
1092 foreach ($this->getAnswers() as $answer) {
1093 /* @var ASS_AnswerBinaryStateImage $answer */
1094 $answer->setAnswertext($migrator->migrateToLmContent($answer->getAnswertext()));
1095 }
1096 }
1097
1101 public function toJSON()
1102 {
1103 include_once("./Services/RTE/classes/class.ilRTE.php");
1104 $result = array();
1105 $result['id'] = (int) $this->getId();
1106 $result['type'] = (string) $this->getQuestionType();
1107 $result['title'] = (string) $this->getTitle();
1108 $result['question'] = $this->formatSAQuestion($this->getQuestion());
1109 $result['nr_of_tries'] = (int) $this->getNrOfTries();
1110 $result['shuffle'] = (bool) $this->getShuffle();
1111
1112 $result['feedback'] = array(
1113 'onenotcorrect' => $this->formatSAQuestion($this->feedbackOBJ->getGenericFeedbackTestPresentation($this->getId(), false)),
1114 'allcorrect' => $this->formatSAQuestion($this->feedbackOBJ->getGenericFeedbackTestPresentation($this->getId(), true))
1115 );
1116
1117 $answers = array();
1118 $has_image = false;
1119 foreach ($this->getAnswers() as $key => $answer_obj) {
1120 if ((string) $answer_obj->getImage()) {
1121 $has_image = true;
1122 }
1123 array_push($answers, array(
1124 "answertext" => (string) $this->formatSAQuestion($answer_obj->getAnswertext()),
1125 'html_id' => (int) $this->getId() . '_' . $key,
1126 "points" => (float) $answer_obj->getPoints(),
1127 "order" => (int) $answer_obj->getOrder(),
1128 "image" => (string) $answer_obj->getImage(),
1129 "feedback" => $this->formatSAQuestion(
1130 $this->feedbackOBJ->getSpecificAnswerFeedbackExportPresentation($this->getId(), 0, $key)
1131 )
1132 ));
1133 }
1134 $result['answers'] = $answers;
1135 if ($has_image) {
1136 $result['path'] = $this->getImagePathWeb();
1137 $result['thumb'] = $this->getThumbSize();
1138 }
1139
1140 $mobs = ilObjMediaObject::_getMobsOfObject("qpl:html", $this->getId());
1141 $result['mobs'] = $mobs;
1142
1143 return json_encode($result);
1144 }
1145
1146 public function removeAnswerImage($index)
1147 {
1148 $answer = $this->answers[$index];
1149 if (is_object($answer)) {
1150 $this->deleteImage($answer->getImage());
1151 $answer->setImage('');
1152 }
1153 }
1154
1156 {
1157 global $DIC;
1158 $ilUser = $DIC['ilUser'];
1159
1160 $multilineAnswerSetting = $ilUser->getPref("tst_multiline_answers");
1161 if ($multilineAnswerSetting != 1) {
1162 $multilineAnswerSetting = 0;
1163 }
1164 return $multilineAnswerSetting;
1165 }
1166
1167 public function setMultilineAnswerSetting($a_setting = 0)
1168 {
1169 global $DIC;
1170 $ilUser = $DIC['ilUser'];
1171 $ilUser->writePref("tst_multiline_answers", $a_setting);
1172 }
1173
1183 public function setSpecificFeedbackSetting($a_feedback_setting)
1184 {
1185 $this->feedback_setting = $a_feedback_setting;
1186 }
1187
1198 {
1199 if ($this->feedback_setting) {
1201 } else {
1202 return 1;
1203 }
1204 }
1205
1207 {
1208 return 'feedback_correct_sc_mc';
1209 }
1210
1221 public function isAnswered($active_id, $pass = null)
1222 {
1223 $numExistingSolutionRecords = assQuestion::getNumExistingSolutionRecords($active_id, $pass, $this->getId());
1224
1225 return $numExistingSolutionRecords > 0;
1226 }
1227
1238 public static function isObligationPossible($questionId)
1239 {
1240 return true;
1241 }
1242
1251 public function getOperators($expression)
1252 {
1253 require_once "./Modules/TestQuestionPool/classes/class.ilOperatorsExpressionMapping.php";
1255 }
1256
1261 public function getExpressionTypes()
1262 {
1263 return array(
1267 );
1268 }
1269
1278 public function getUserQuestionResult($active_id, $pass)
1279 {
1281 global $DIC;
1282 $ilDB = $DIC['ilDB'];
1283 $result = new ilUserQuestionResult($this, $active_id, $pass);
1284
1285 $maxStep = $this->lookupMaxStep($active_id, $pass);
1286
1287 if ($maxStep !== null) {
1288 $data = $ilDB->queryF(
1289 "SELECT * FROM tst_solutions WHERE active_fi = %s AND pass = %s AND question_fi = %s AND step = %s",
1290 array("integer", "integer", "integer","integer"),
1291 array($active_id, $pass, $this->getId(), $maxStep)
1292 );
1293 } else {
1294 $data = $ilDB->queryF(
1295 "SELECT * FROM tst_solutions WHERE active_fi = %s AND pass = %s AND question_fi = %s",
1296 array("integer", "integer", "integer"),
1297 array($active_id, $pass, $this->getId())
1298 );
1299 }
1300
1301 $row = $ilDB->fetchAssoc($data);
1302
1303 if ($row != null) {
1304 ++$row["value1"];
1305 $result->addKeyValue($row["value1"], $row["value1"]);
1306 }
1307
1308 $points = $this->calculateReachedPoints($active_id, $pass);
1309 $max_points = $this->getMaximumPoints();
1310
1311 $result->setReachedPercentage(($points / $max_points) * 100);
1312
1313 return $result;
1314 }
1315
1324 public function getAvailableAnswerOptions($index = null)
1325 {
1326 if ($index !== null) {
1327 return $this->getAnswer($index);
1328 } else {
1329 return $this->getAnswers();
1330 }
1331 }
1332
1336 protected function afterSyncWithOriginal($origQuestionId, $dupQuestionId, $origParentObjId, $dupParentObjId)
1337 {
1338 parent::afterSyncWithOriginal($origQuestionId, $dupQuestionId, $origParentObjId, $dupParentObjId);
1339
1340 $origImagePath = $this->buildImagePath($origQuestionId, $origParentObjId);
1341 $dupImagePath = $this->buildImagePath($dupQuestionId, $dupParentObjId);
1342
1343 ilUtil::delDir($origImagePath);
1344 if (is_dir($dupImagePath)) {
1345 ilUtil::makeDirParents($origImagePath);
1346 ilUtil::rCopy($dupImagePath, $origImagePath);
1347 }
1348 }
1349}
$result
$filename
Definition: buildRTE.php:89
$_POST["username"]
Class for answers with a binary state indicator.
An exception for terminatinating execution or to throw for unit testing.
Abstract basic class which is to be extended by the concrete assessment question type classes.
getCurrentSolutionResultSet($active_id, $pass, $authorized=true)
Get a restulset for the current user solution for a this question by active_id and pass.
getSolutionValues($active_id, $pass=null, $authorized=true)
Loads solutions of a given user from the database an returns it.
static _getOriginalId($question_id)
Returns the original id of a question.
formatSAQuestion($a_q)
Format self assessment question.
setShuffle($shuffle=true)
Sets the shuffle flag.
setId($id=-1)
Sets the id of the assQuestion object.
setOriginalId($original_id)
setObjId($obj_id=0)
Set the object id of the container object.
getSolutionMaxPass($active_id)
Returns the maximum pass a users question solution.
saveQuestionDataToDb($original_id="")
getId()
Gets the id of the assQuestion object.
saveCurrentSolution($active_id, $pass, $value1, $value2, $authorized=true, $tstamp=null)
getObjId()
Get the object id of the container object.
setTitle($title="")
Sets the title string of the assQuestion object.
getAdditionalContentEditingMode()
getter for additional content editing mode for this question
setOwner($owner="")
Sets the creator/owner ID of the assQuestion object.
setEstimatedWorkingTime($hour=0, $min=0, $sec=0)
Sets the estimated working time of a question from given hour, minute and second.
deductHintPointsFromReachedPoints(ilAssQuestionPreviewSession $previewSession, $reachedPoints)
buildImagePath($questionId, $parentObjectId)
static logAction($logtext="", $active_id="", $question_id="")
Logs an action into the Test&Assessment log.
getImagePath($question_id=null, $object_id=null)
Returns the image path for web accessable images of a question.
updateCurrentSolution($solutionId, $value1, $value2, $authorized=true)
removeSolutionRecordById($solutionId)
static getNumExistingSolutionRecords($activeId, $pass, $questionId)
returns the number of existing solution records for the given test active / pass and given question i...
setAuthor($author="")
Sets the authors name of the assQuestion object.
getShuffle()
Gets the shuffle flag.
setLifecycle(ilAssQuestionLifecycle $lifecycle)
getTitle()
Gets the title string of the assQuestion object.
setPoints($a_points)
Sets the maximum available points for the question.
setComment($comment="")
Sets the comment string of the assQuestion object.
setNrOfTries($a_nr_of_tries)
getQuestion()
Gets the question string of the question object.
setAdditionalContentEditingMode($additinalContentEditingMode)
setter for additional content editing mode for this question
saveToDb($original_id="")
Saves the question to the database.
setQuestion($question="")
Sets the question string of the question object.
getImagePathWeb()
Returns the web image path for web accessable images of a question.
ensureNonNegativePoints($points)
Class for single choice questions.
getMaximumPoints()
Returns the maximum points, a learner can reach answering the question.
loadFromDb($question_id)
Loads a assSingleChoice object from a database.
deleteImage($image_filename)
Deletes an image file.
setOutputType($output_type=OUTPUT_ORDER)
Sets the output type of the assSingleChoice object.
addAnswer( $answertext="", $points=0.0, $order=0, $answerimage="", $answer_id=-1)
Adds a possible answer for a single choice question.
getQuestionType()
Returns the question type of the question.
getAnswerCount()
Returns the number of answers.
toJSON()
Returns a JSON representation of the question.
getRTETextWithMediaObjects()
Collects all text in the question which could contain media objects which were created with the Rich ...
generateThumbForFile($path, $file)
syncImages()
Sync images of a MC question on synchronisation with the original question.
setSpecificFeedbackSetting($a_feedback_setting)
Sets the feedback settings in effect for the question.
getOutputType()
Gets the single choice output type which is either OUTPUT_ORDER (=0) or OUTPUT_RANDOM (=1).
saveWorkingData($active_id, $pass=null, $authorized=true)
Saves the learners input of the question to the database.
isAnswered($active_id, $pass=null)
returns boolean wether the question is answered during test pass or not
getSpecificFeedbackSetting()
Gets the current feedback settings in effect for the question.
getOperators($expression)
Get all available operations for a specific question.
setMultilineAnswerSetting($a_setting=0)
getExpressionTypes()
Get all available expression types for a specific question.
getAvailableAnswerOptions($index=null)
If index is null, the function returns an array with all anwser options Else it returns the specific ...
afterSyncWithOriginal($origQuestionId, $dupQuestionId, $origParentObjId, $dupParentObjId)
{}
& getAnswers()
Returns a reference to the answers array.
isComplete()
Returns true, if a single choice question is complete for use.
deleteAnswer($index=0)
Deletes an answer with a given index.
calculateReachedPoints($active_id, $pass=null, $authorizedSolution=true, $returndetails=false)
Returns the points, a learner has reached answering the question.
savePreviewData(ilAssQuestionPreviewSession $previewSession)
static isObligationPossible($questionId)
returns boolean wether it is possible to set this question type as obligatory or not considering the ...
setImageFile($image_filename, $image_tempfilename="")
Sets the image file and uploads the image to the object's image directory.
copyObject($target_questionpool_id, $title="")
Copies an assSingleChoice object.
setExportDetailsXLS($worksheet, $startrow, $active_id, $pass)
{Creates an Excel worksheet for the detailed cumulated results of this question.object}
lmMigrateQuestionTypeSpecificContent(ilAssSelfAssessmentMigrator $migrator)
__construct( $title="", $comment="", $author="", $owner=-1, $question="", $output_type=OUTPUT_ORDER)
assSingleChoice constructor
flushAnswers()
Deletes all answers.
getAnswer($index=0)
Returns an answer with a given index.
calculateReachedPointsFromPreviewSession(ilAssQuestionPreviewSession $previewSession)
createNewOriginalFromThisDuplicate($targetParentId, $targetQuestionTitle="")
duplicateImages($question_id, $objectId=null)
duplicate($for_test=true, $title="", $author="", $owner="", $testObjId=null)
Duplicates an assSingleChoiceQuestion.
getAdditionalTableName()
Returns the name of the additional question data table in the database.
getAnswerTableName()
Returns the name of the answer table in the database.
static _getLogLanguage()
retrieve the log language for assessment logging
static _enabledAssessmentLogging()
check wether assessment logging is enabled or not
static getMimeType($a_file, $a_external=null)
get mime type for file
static _getMobsOfObject($a_type, $a_id, $a_usage_hist_nr=0, $a_lang="-")
get mobs of object
static _getPass($active_id)
Retrieves the actual pass of a given user for a given test.
static _replaceMediaObjectImageSrc($a_text, $a_direction=0, $nic=IL_INST_ID)
Replaces image source from mob image urls with the mob id or replaces mob id with the correct image s...
Base Exception for all Exceptions relating to Modules/Test.
Class ilUserQuestionResult.
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static convertImage( $a_from, $a_to, $a_target_format="", $a_geometry="", $a_background_color="")
convert image
static rCopy($a_sdir, $a_tdir, $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
static makeDirParents($a_dir)
Create a new directory and all parent directories.
global $DIC
Definition: goto.php:24
$mobs
Definition: imgupload.php:54
$ilUser
Definition: imgupload.php:18
const OUTPUT_ORDER
Class iQuestionCondition.
getUserQuestionResult($active_id, $pass)
Get the user solution for a question by active_id and the test pass.
Interface ilObjAnswerScoringAdjustable.
saveAnswerSpecificDataToDb()
Saves the answer specific records into a question types answer table.
Interface ilObjQuestionScoringAdjustable.
saveAdditionalQuestionDataToDb()
Saves a record to the question types additional data table.
$index
Definition: metadata.php:128
$i
Definition: metadata.php:24
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $ilDB
$data
Definition: storeScorm.php:23