ILIAS  release_7 Revision v7.30-3-g800a261c036
class.assMultipleChoice.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
28{
36 public $answers;
37
47
51
53 protected $thumb_size;
54
58 protected $selectionLimit;
59
64 {
65 $this->isSingleline = $isSingleline;
66 }
67
71 public function getIsSingleline()
72 {
74 }
75
79 public function setLastChange($lastChange)
80 {
81 $this->lastChange = $lastChange;
82 }
83
87 public function getLastChange()
88 {
89 return $this->lastChange;
90 }
91
106 public function __construct(
107 $title = "",
108 $comment = "",
109 $author = "",
110 $owner = -1,
111 $question = "",
113 ) {
115 $this->output_type = $output_type;
116 $this->thumb_size = 150;
117 $this->answers = array();
118 $this->shuffle = 1;
119 $this->selectionLimit = null;
120 $this->feedback_setting = 0;
121 }
122
126 public function getSelectionLimit()
127 {
129 }
130
135 {
136 $this->selectionLimit = $selectionLimit;
137 }
138
145 public function isComplete()
146 {
147 if (strlen($this->title) and ($this->author) and ($this->question) and (count($this->answers)) and ($this->getMaximumPoints() > 0)) {
148 return true;
149 } else {
150 return false;
151 }
152 }
153
159 public function saveToDb($original_id = "")
160 {
164
165 $this->ensureNoInvalidObligation($this->getId());
166 parent::saveToDb($original_id);
167 }
168
172 protected function rebuildThumbnails()
173 {
174 if ($this->isSingleline && ($this->getThumbSize())) {
175 foreach ($this->getAnswers() as $answer) {
176 if (strlen($answer->getImage())) {
177 $this->generateThumbForFile($this->getImagePath(), $answer->getImage());
178 }
179 }
180 }
181 }
182
186 public function getThumbPrefix()
187 {
188 return "thumb.";
189 }
190
195 protected function generateThumbForFile($path, $file)
196 {
197 $filename = $path . $file;
198 if (@file_exists($filename)) {
199 $thumbpath = $path . $this->getThumbPrefix() . $file;
200 $path_info = @pathinfo($filename);
201 $ext = "";
202 switch (strtoupper($path_info['extension'])) {
203 case 'PNG':
204 $ext = 'PNG';
205 break;
206 case 'GIF':
207 $ext = 'GIF';
208 break;
209 default:
210 $ext = 'JPEG';
211 break;
212 }
213 ilUtil::convertImage($filename, $thumbpath, $ext, $this->getThumbSize());
214 }
215 }
216
222 public function loadFromDb($question_id)
223 {
224 global $DIC;
225 $ilDB = $DIC['ilDB'];
226 $hasimages = 0;
227
228 $result = $ilDB->queryF(
229 "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",
230 array("integer"),
231 array($question_id)
232 );
233 if ($result->numRows() == 1) {
234 $data = $ilDB->fetchAssoc($result);
235 $this->setId($question_id);
236 $this->setObjId($data["obj_fi"]);
237 $this->setTitle($data["title"]);
238 $this->setNrOfTries($data['nr_of_tries']);
239 $this->setComment($data["description"]);
240 $this->setOriginalId($data["original_id"]);
241 $this->setAuthor($data["author"]);
242 $this->setPoints($data["points"]);
243 $this->setOwner($data["owner"]);
244 include_once("./Services/RTE/classes/class.ilRTE.php");
245 $this->setQuestion(ilRTE::_replaceMediaObjectImageSrc($data["question_text"], 1));
246 $shuffle = (is_null($data['shuffle'])) ? true : $data['shuffle'];
247 $this->setShuffle($shuffle);
248 $this->setEstimatedWorkingTime(substr($data["working_time"], 0, 2), substr($data["working_time"], 3, 2), substr($data["working_time"], 6, 2));
249 $this->setThumbSize($data['thumb_size']);
250 $this->isSingleline = ($data['allow_images']) ? false : true;
251 $this->lastChange = $data['tstamp'];
252 $this->setSelectionLimit((int) $data['selection_limit'] > 0 ? (int) $data['selection_limit'] : null);
253 $this->feedback_setting = $data['feedback_setting'];
254
255 try {
256 $this->setLifecycle(ilAssQuestionLifecycle::getInstance($data['lifecycle']));
259 }
260
261 try {
262 $this->setAdditionalContentEditingMode($data['add_cont_edit_mode']);
264 }
265 }
266
267 $result = $ilDB->queryF(
268 "SELECT * FROM qpl_a_mc WHERE question_fi = %s ORDER BY aorder ASC",
269 array('integer'),
270 array($question_id)
271 );
272 include_once "./Modules/TestQuestionPool/classes/class.assAnswerMultipleResponseImage.php";
273 if ($result->numRows() > 0) {
274 while ($data = $ilDB->fetchAssoc($result)) {
275 $imagefilename = $this->getImagePath() . $data["imagefile"];
276 if (!@file_exists($imagefilename)) {
277 $data["imagefile"] = "";
278 }
279 include_once("./Services/RTE/classes/class.ilRTE.php");
280 $data["answertext"] = ilRTE::_replaceMediaObjectImageSrc($data["answertext"], 1);
281 array_push($this->answers, new ASS_AnswerMultipleResponseImage($data["answertext"], $data["points"], $data["aorder"], $data["points_unchecked"], $data["imagefile"], $data["answer_id"]));
282 }
283 }
284
285 parent::loadFromDb($question_id);
286 }
287
291 public function duplicate($for_test = true, $title = "", $author = "", $owner = "", $testObjId = null)
292 {
293 if ($this->id <= 0) {
294 // The question has not been saved. It cannot be duplicated
295 return;
296 }
297 // duplicate the question in database
298 $this_id = $this->getId();
299 $thisObjId = $this->getObjId();
300
301 $clone = $this;
302 include_once("./Modules/TestQuestionPool/classes/class.assQuestion.php");
304 $clone->id = -1;
305
306 if ((int) $testObjId > 0) {
307 $clone->setObjId($testObjId);
308 }
309
310 if ($title) {
311 $clone->setTitle($title);
312 }
313
314 if ($author) {
315 $clone->setAuthor($author);
316 }
317 if ($owner) {
318 $clone->setOwner($owner);
319 }
320
321 if ($for_test) {
322 $clone->saveToDb($original_id);
323 } else {
324 $clone->saveToDb();
325 }
326
327 // copy question page content
328 $clone->copyPageOfQuestion($this_id);
329 // copy XHTML media objects
330 $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
331 // duplicate the images
332 $clone->duplicateImages($this_id, $thisObjId);
333
334 $clone->onDuplicate($thisObjId, $this_id, $clone->getObjId(), $clone->getId());
335
336 return $clone->id;
337 }
338
342 public function copyObject($target_questionpool_id, $title = "")
343 {
344 if ($this->id <= 0) {
345 // The question has not been saved. It cannot be duplicated
346 return;
347 }
348 // duplicate the question in database
349 $clone = $this;
350 include_once("./Modules/TestQuestionPool/classes/class.assQuestion.php");
352 $clone->id = -1;
353 $source_questionpool_id = $this->getObjId();
354 $clone->setObjId($target_questionpool_id);
355 if ($title) {
356 $clone->setTitle($title);
357 }
358 $clone->saveToDb();
359 // copy question page content
360 $clone->copyPageOfQuestion($original_id);
361 // copy XHTML media objects
362 $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
363 // duplicate the image
364 $clone->copyImages($original_id, $source_questionpool_id);
365
366 $clone->onCopy($source_questionpool_id, $original_id, $clone->getObjId(), $clone->getId());
367
368 return $clone->id;
369 }
370
371 public function createNewOriginalFromThisDuplicate($targetParentId, $targetQuestionTitle = "")
372 {
373 if ($this->id <= 0) {
374 // The question has not been saved. It cannot be duplicated
375 return;
376 }
377
378 include_once("./Modules/TestQuestionPool/classes/class.assQuestion.php");
379
380 $sourceQuestionId = $this->id;
381 $sourceParentId = $this->getObjId();
382
383 // duplicate the question in database
384 $clone = $this;
385 $clone->id = -1;
386
387 $clone->setObjId($targetParentId);
388
389 if ($targetQuestionTitle) {
390 $clone->setTitle($targetQuestionTitle);
391 }
392
393 $clone->saveToDb();
394 // copy question page content
395 $clone->copyPageOfQuestion($sourceQuestionId);
396 // copy XHTML media objects
397 $clone->copyXHTMLMediaObjectsOfQuestion($sourceQuestionId);
398 // duplicate the image
399 $clone->copyImages($sourceQuestionId, $sourceParentId);
400
401 $clone->onCopy($sourceParentId, $sourceQuestionId, $clone->getObjId(), $clone->getId());
402
403 return $clone->id;
404 }
405
412 public function getOutputType()
413 {
414 return $this->output_type;
415 }
416
425 {
426 $this->output_type = $output_type;
427 }
428
443 public function addAnswer(
444 $answertext = "",
445 $points = 0.0,
446 $points_unchecked = 0.0,
447 $order = 0,
448 $answerimage = "",
449 $answer_id = -1
450 ) {
451 include_once "./Modules/TestQuestionPool/classes/class.assAnswerMultipleResponseImage.php";
452 $answertext = $this->getHtmlQuestionContentPurifier()->purify($answertext);
453 if (array_key_exists($order, $this->answers)) {
454 // insert answer
455 $answer = new ASS_AnswerMultipleResponseImage($answertext, $points, $order, $points_unchecked, $answerimage, $answer_id);
456 $newchoices = array();
457 for ($i = 0; $i < $order; $i++) {
458 array_push($newchoices, $this->answers[$i]);
459 }
460 array_push($newchoices, $answer);
461 for ($i = $order; $i < count($this->answers); $i++) {
462 $changed = $this->answers[$i];
463 $changed->setOrder($i + 1);
464 array_push($newchoices, $changed);
465 }
466 $this->answers = $newchoices;
467 } else {
468 // add answer
469 $answer = new ASS_AnswerMultipleResponseImage($answertext, $points, count($this->answers), $points_unchecked, $answerimage, $answer_id);
470 array_push($this->answers, $answer);
471 }
472 }
473
480 public function getAnswerCount()
481 {
482 return count($this->answers);
483 }
484
493 public function getAnswer($index = 0)
494 {
495 if ($index < 0) {
496 return null;
497 }
498 if (count($this->answers) < 1) {
499 return null;
500 }
501 if ($index >= count($this->answers)) {
502 return null;
503 }
504
505 return $this->answers[$index];
506 }
507
515 public function deleteAnswer($index = 0)
516 {
517 if ($index < 0) {
518 return;
519 }
520 if (count($this->answers) < 1) {
521 return;
522 }
523 if ($index >= count($this->answers)) {
524 return;
525 }
526 $answer = $this->answers[$index];
527 if (strlen($answer->getImage())) {
528 $this->deleteImage($answer->getImage());
529 }
530 unset($this->answers[$index]);
531 $this->answers = array_values($this->answers);
532 for ($i = 0; $i < count($this->answers); $i++) {
533 if ($this->answers[$i]->getOrder() > $index) {
534 $this->answers[$i]->setOrder($i);
535 }
536 }
537 }
538
544 public function flushAnswers()
545 {
546 $this->answers = array();
547 }
548
554 public function getMaximumPoints()
555 {
556 $points = 0;
557 $allpoints = 0;
558 foreach ($this->answers as $key => $value) {
559 if ($value->getPoints() > $value->getPointsUnchecked()) {
560 $allpoints += $value->getPoints();
561 } else {
562 $allpoints += $value->getPointsUnchecked();
563 }
564 }
565 return $allpoints;
566 }
567
579 public function calculateReachedPoints($active_id, $pass = null, $authorizedSolution = true, $returndetails = false)
580 {
581 if ($returndetails) {
582 throw new ilTestException('return details not implemented for ' . __METHOD__);
583 }
584
585 global $DIC;
586 $ilDB = $DIC['ilDB'];
587
588 $found_values = array();
589 if (is_null($pass)) {
590 $pass = $this->getSolutionMaxPass($active_id);
591 }
592 $result = $this->getCurrentSolutionResultSet($active_id, $pass, $authorizedSolution);
593 while ($data = $ilDB->fetchAssoc($result)) {
594 if (strcmp($data["value1"], "") != 0) {
595 array_push($found_values, $data["value1"]);
596 }
597 }
598
599 $points = $this->calculateReachedPointsForSolution($found_values, $active_id);
600
601 return $points;
602 }
603
604 public function validateSolutionSubmit()
605 {
606 $submit = $this->getSolutionSubmit();
607
608 if ($this->getSelectionLimit()) {
609 if (count($submit) > $this->getSelectionLimit()) {
610 $failureMsg = sprintf(
611 $this->lng->txt('ass_mc_sel_lim_exhausted_hint'),
612 $this->getSelectionLimit(),
613 $this->getAnswerCount()
614 );
615
616 ilUtil::sendFailure($failureMsg, true);
617 return false;
618 }
619 }
620
621 return true;
622 }
623
624 protected function isForcedEmptySolution($solutionSubmit)
625 {
626 if (!count($solutionSubmit) && !empty($_POST['tst_force_form_diff_input'])) {
627 return true;
628 }
629
630 return false;
631 }
632
641 public function saveWorkingData($active_id, $pass = null, $authorized = true)
642 {
644 global $DIC;
645 $ilDB = $DIC['ilDB'];
646
647 if (is_null($pass)) {
648 include_once "./Modules/Test/classes/class.ilObjTest.php";
649 $pass = ilObjTest::_getPass($active_id);
650 }
651
652 $entered_values = 0;
653
654 $this->getProcessLocker()->executeUserSolutionUpdateLockOperation(function () use (&$entered_values, $active_id, $pass, $authorized) {
655 $this->removeCurrentSolution($active_id, $pass, $authorized);
656
657 $solutionSubmit = $this->getSolutionSubmit();
658
659 foreach ($solutionSubmit as $value) {
660 if (strlen($value)) {
661 $this->saveCurrentSolution($active_id, $pass, $value, null, $authorized);
662 $entered_values++;
663 }
664 }
665
666 // fau: testNav - write a dummy entry for the evil mc questions with "None of the above" checked
667 if ($this->isForcedEmptySolution($solutionSubmit)) {
668 $this->saveCurrentSolution($active_id, $pass, 'mc_none_above', null, $authorized);
669 $entered_values++;
670 }
671 // fau.
672 });
673
674 if ($entered_values) {
675 include_once("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
677 assQuestion::logAction($this->lng->txtlng("assessment", "log_user_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
678 }
679 } else {
680 include_once("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
682 assQuestion::logAction($this->lng->txtlng("assessment", "log_user_not_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
683 }
684 }
685
686 return true;
687 }
688
689 public function saveAdditionalQuestionDataToDb()
690 {
692 global $DIC;
693 $ilDB = $DIC['ilDB'];
694 $oldthumbsize = 0;
695 if ($this->isSingleline && ($this->getThumbSize())) {
696 // get old thumbnail size
697 $result = $ilDB->queryF(
698 "SELECT thumb_size FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
699 ['integer'],
700 [$this->getId()]
701 );
702 if ($result->numRows() == 1) {
703 $data = $ilDB->fetchAssoc($result);
704 $oldthumbsize = $data['thumb_size'];
705 }
706 }
707
708 if (!$this->isSingleline) {
710 }
711
712 // save additional data
713 $ilDB->replace(
714 $this->getAdditionalTableName(),
715 [
716 'shuffle' => array('text', $this->getShuffle()),
717 'allow_images' => array('text', $this->isSingleline ? 0 : 1),
718 'thumb_size' => array('integer', strlen($this->getThumbSize()) ? $this->getThumbSize() : null),
719 'selection_limit' => array('integer', $this->getSelectionLimit()),
720 'feedback_setting' => array('integer', $this->getSpecificFeedbackSetting())
721 ],
722 ['question_fi' => array('integer', $this->getId())]
723 );
724 }
725
731 public function saveAnswerSpecificDataToDb()
732 {
734 global $DIC;
735 $ilDB = $DIC['ilDB'];
736
737 // Get all feedback entries
738 $result = $ilDB->queryF(
739 "SELECT * FROM qpl_fb_specific WHERE question_fi = %s",
740 ['integer'],
741 [$this->getId()]
742 );
743 $db_feedback = $ilDB->fetchAll($result);
744
745 // Check if feedback exists and the regular editor is used and not the page editor
746 if (sizeof($db_feedback) >= 1 && $this->getAdditionalContentEditingMode() == 'default'){
747 // Get all existing answer data for question
748 $result = $ilDB->queryF(
749 "SELECT answer_id, aorder FROM qpl_a_mc WHERE question_fi = %s",
750 ['integer'],
751 [$this->getId()]
752 );
753 $db_answers = $ilDB->fetchAll($result);
754
755 // Collect old and new order entries by ids and order to calculate a diff/intersection and remove/update feedback
756 $post_answer_order_for_id = [];
757 foreach ($this->answers as $answer){
758 // Only the first appearance of an id is used
759 if ($answer->getId() !== null && !in_array($answer->getId(), array_keys($post_answer_order_for_id))) {
760 // -1 is happening while import and also if a new multi line answer is generated
761 if ($answer->getId() == -1) {
762 continue;
763 }
764 $post_answer_order_for_id[$answer->getId()] = $answer->getOrder();
765 }
766 }
767
768 // If there is no usable ids from post, it's better to not touch the feedback
769 // This is useful since the import is also using this function or the first creation of a new question in general
770 if (sizeof($post_answer_order_for_id) >= 1) {
771 $db_answer_order_for_id = [];
772 $db_answer_id_for_order = [];
773 foreach ($db_answers as $db_answer){
774 $db_answer_order_for_id[intval($db_answer['answer_id'])] = intval($db_answer['aorder']);
775 $db_answer_id_for_order[intval($db_answer['aorder'])] = intval($db_answer['answer_id']);
776 }
777
778 // Handle feedback
779 // the diff between the already existing answer ids from the Database and the answer ids from post
780 // feedback related to the answer ids should be deleted or in our case not recreated.
781 $db_answer_ids = array_keys($db_answer_order_for_id);
782 $post_answer_ids = array_keys($post_answer_order_for_id);
783 $diff_db_post_answer_ids = array_diff($db_answer_ids, $post_answer_ids);
784 $unused_answer_ids = array_keys($diff_db_post_answer_ids);
785
786 // Delete all feedback in the database
787 $this->feedbackOBJ->deleteSpecificAnswerFeedbacks($this->getId(), false);
788 // Recreate feedback
789 foreach ($db_feedback as $feedback_option) {
790 // skip feedback which answer is deleted
791 if (in_array(intval($feedback_option['answer']), $unused_answer_ids)) {
792 continue;
793 }
794
795 // Reorder feedback
796 $feedback_order_db = intval($feedback_option['answer']);
797 $db_answer_id = $db_answer_id_for_order[$feedback_order_db];
798 // This cuts feedback that currently would have no corresponding answer
799 // This case can happen while copying "broken" questions
800 // Or when saving a question with less answers than feedback
801 if (is_null($db_answer_id) || $db_answer_id < 0) {
802 continue;
803 }
804 $feedback_order_post = $post_answer_order_for_id[$db_answer_id];
805 $feedback_option['answer'] = $feedback_order_post;
806
807 // Recreate remaining feedback in database
808 $next_id = $ilDB->nextId('qpl_fb_specific');
809 $ilDB->manipulateF(
810 "INSERT INTO qpl_fb_specific (feedback_id, question_fi, answer, tstamp, feedback, question)
811 VALUES (%s, %s, %s, %s, %s, %s)",
812 ['integer', 'integer', 'integer', 'integer', 'text', 'integer'],
813 [
814 $next_id,
815 $feedback_option['question_fi'],
816 $feedback_option['answer'],
817 time(),
818 $feedback_option['feedback'],
819 $feedback_option['question']
820 ]
821 );
822 }
823 }
824 }
825
826 // Delete all entries in qpl_a_mc for question
827 $ilDB->manipulateF(
828 "DELETE FROM qpl_a_mc WHERE question_fi = %s",
829 ['integer'],
830 [$this->getId()]
831 );
832
833 // Recreate answers one by one
834 foreach ($this->answers as $key => $value) {
835 $answer_obj = $this->answers[$key];
836 $next_id = $ilDB->nextId('qpl_a_mc');
837 $ilDB->manipulateF(
838 "INSERT INTO qpl_a_mc (answer_id, question_fi, answertext, points, points_unchecked, aorder, imagefile, tstamp)
839 VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
840 ['integer', 'integer', 'text', 'float', 'float', 'integer', 'text', 'integer'],
841 [
842 $next_id,
843 $this->getId(),
844 ilRTE::_replaceMediaObjectImageSrc($answer_obj->getAnswertext(), 0),
845 $answer_obj->getPoints(),
846 $answer_obj->getPointsUnchecked(),
847 $answer_obj->getOrder(),
848 $answer_obj->getImage(),
849 time()
850 ]
851 );
852 }
853 $this->rebuildThumbnails();
854 }
855
856 public function syncWithOriginal()
857 {
858 if ($this->getOriginalId()) {
859 $this->syncImages();
860 parent::syncWithOriginal();
861 }
862 }
863
869 public function getQuestionType()
870 {
871 return "assMultipleChoice";
872 }
873
879 public function getAdditionalTableName()
880 {
881 return "qpl_qst_mc";
882 }
883
889 public function getAnswerTableName()
890 {
891 return "qpl_a_mc";
892 }
893
901 public function setImageFile($image_filename, $image_tempfilename = "")
902 {
903 $result = 0;
904 if (!empty($image_tempfilename)) {
905 $image_filename = str_replace(" ", "_", $image_filename);
906 $imagepath = $this->getImagePath();
907 if (!file_exists($imagepath)) {
908 ilUtil::makeDirParents($imagepath);
909 }
910 if (!ilUtil::moveUploadedFile($image_tempfilename, $image_filename, $imagepath . $image_filename)) {
911 $result = 2;
912 } else {
913 include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
914 $mimetype = ilObjMediaObject::getMimeType($imagepath . $image_filename);
915 if (!preg_match("/^image/", $mimetype)) {
916 unlink($imagepath . $image_filename);
917 $result = 1;
918 } else {
919 // create thumbnail file
920 if ($this->isSingleline && ($this->getThumbSize())) {
921 $this->generateThumbForFile($imagepath, $image_filename);
922 }
923 }
924 }
925 }
926 return $result;
927 }
928
934 protected function deleteImage($image_filename)
935 {
936 $imagepath = $this->getImagePath();
937 @unlink($imagepath . $image_filename);
938 $thumbpath = $imagepath . $this->getThumbPrefix() . $image_filename;
939 @unlink($thumbpath);
940 }
941
942 public function duplicateImages($question_id, $objectId = null)
943 {
945 global $DIC;
946 $ilLog = $DIC['ilLog'];
947
948 $imagepath = $this->getImagePath();
949 $imagepath_original = str_replace("/$this->id/images", "/$question_id/images", $imagepath);
950
951 if ((int) $objectId > 0) {
952 $imagepath_original = str_replace("/$this->obj_id/", "/$objectId/", $imagepath_original);
953 }
954
955 foreach ($this->answers as $answer) {
956 $filename = $answer->getImage();
957 if (strlen($filename)) {
958 if (!file_exists($imagepath)) {
959 ilUtil::makeDirParents($imagepath);
960 }
961
962 if (file_exists($imagepath_original . $filename)) {
963 if (!copy($imagepath_original . $filename, $imagepath . $filename)) {
964 $ilLog->warning(sprintf(
965 "Could not clone source image '%s' to '%s' (srcQuestionId: %s|tgtQuestionId: %s|srcParentObjId: %s|tgtParentObjId: %s)",
966 $imagepath_original . $filename,
967 $imagepath . $filename,
968 $question_id,
969 $this->id,
970 $objectId,
971 $this->obj_id
972 ));
973 }
974 }
975
976 if (file_exists($imagepath_original . $this->getThumbPrefix() . $filename)) {
977 if (!copy($imagepath_original . $this->getThumbPrefix() . $filename, $imagepath . $this->getThumbPrefix() . $filename)) {
978 $ilLog->warning(sprintf(
979 "Could not clone thumbnail source image '%s' to '%s' (srcQuestionId: %s|tgtQuestionId: %s|srcParentObjId: %s|tgtParentObjId: %s)",
980 $imagepath_original . $this->getThumbPrefix() . $filename,
981 $imagepath . $this->getThumbPrefix() . $filename,
982 $question_id,
983 $this->id,
984 $objectId,
985 $this->obj_id
986 ));
987 }
988 }
989 }
990 }
991 }
992
993 public function copyImages($question_id, $source_questionpool)
994 {
995 global $DIC;
996 $ilLog = $DIC['ilLog'];
997 $imagepath = $this->getImagePath();
998 $imagepath_original = str_replace("/$this->id/images", "/$question_id/images", $imagepath);
999 $imagepath_original = str_replace("/$this->obj_id/", "/$source_questionpool/", $imagepath_original);
1000 foreach ($this->answers as $answer) {
1001 $filename = $answer->getImage();
1002 if (strlen($filename)) {
1003 if (!file_exists($imagepath)) {
1004 ilUtil::makeDirParents($imagepath);
1005 }
1006 if (!@copy($imagepath_original . $filename, $imagepath . $filename)) {
1007 $ilLog->write("image could not be duplicated!!!!", $ilLog->ERROR);
1008 $ilLog->write("object: " . print_r($this, true), $ilLog->ERROR);
1009 }
1010 if (@file_exists($imagepath_original . $this->getThumbPrefix() . $filename)) {
1011 if (!@copy($imagepath_original . $this->getThumbPrefix() . $filename, $imagepath . $this->getThumbPrefix() . $filename)) {
1012 $ilLog->write("image thumbnail could not be duplicated!!!!", $ilLog->ERROR);
1013 $ilLog->write("object: " . print_r($this, true), $ilLog->ERROR);
1014 }
1015 }
1016 }
1017 }
1018 }
1019
1023 protected function syncImages()
1024 {
1025 global $DIC;
1026 $ilLog = $DIC['ilLog'];
1027 $imagepath = $this->getImagePath();
1028 $question_id = $this->getOriginalId();
1029 $originalObjId = parent::lookupParentObjId($this->getOriginalId());
1030 $imagepath_original = $this->getImagePath($question_id, $originalObjId);
1031
1032 ilUtil::delDir($imagepath_original);
1033 foreach ($this->answers as $answer) {
1034 $filename = $answer->getImage();
1035 if (strlen($filename)) {
1036 if (@file_exists($imagepath . $filename)) {
1037 if (!file_exists($imagepath)) {
1038 ilUtil::makeDirParents($imagepath);
1039 }
1040 if (!file_exists($imagepath_original)) {
1041 ilUtil::makeDirParents($imagepath_original);
1042 }
1043 if (!@copy($imagepath . $filename, $imagepath_original . $filename)) {
1044 $ilLog->write("image could not be duplicated!!!!", $ilLog->ERROR);
1045 $ilLog->write("object: " . print_r($this, true), $ilLog->ERROR);
1046 }
1047 }
1048 if (@file_exists($imagepath . $this->getThumbPrefix() . $filename)) {
1049 if (!@copy($imagepath . $this->getThumbPrefix() . $filename, $imagepath_original . $this->getThumbPrefix() . $filename)) {
1050 $ilLog->write("image thumbnail could not be duplicated!!!!", $ilLog->ERROR);
1051 $ilLog->write("object: " . print_r($this, true), $ilLog->ERROR);
1052 }
1053 }
1054 }
1055 }
1056 }
1057
1062 {
1063 $text = parent::getRTETextWithMediaObjects();
1064 foreach ($this->answers as $index => $answer) {
1065 $text .= $this->feedbackOBJ->getSpecificAnswerFeedbackContent($this->getId(), 0, $index);
1066 $answer_obj = $this->answers[$index];
1067 $text .= $answer_obj->getAnswertext();
1068 }
1069 return $text;
1070 }
1071
1075 public function &getAnswers()
1076 {
1077 return $this->answers;
1078 }
1079
1083 public function setExportDetailsXLS($worksheet, $startrow, $active_id, $pass)
1084 {
1085 parent::setExportDetailsXLS($worksheet, $startrow, $active_id, $pass);
1086
1087 $solution = $this->getSolutionValues($active_id, $pass);
1088
1089 $i = 1;
1090 foreach ($this->getAnswers() as $id => $answer) {
1091 $worksheet->setCell($startrow + $i, 0, $answer->getAnswertext());
1092 $worksheet->setBold($worksheet->getColumnCoord(0) . ($startrow + $i));
1093 $checked = false;
1094 foreach ($solution as $solutionvalue) {
1095 if ($id == $solutionvalue["value1"]) {
1096 $checked = true;
1097 }
1098 }
1099 if ($checked) {
1100 $worksheet->setCell($startrow + $i, 2, 1);
1101 } else {
1102 $worksheet->setCell($startrow + $i, 2, 0);
1103 }
1104 $i++;
1105 }
1106
1107 return $startrow + $i + 1;
1108 }
1109
1110 public function getThumbSize()
1111 {
1112 return $this->thumb_size;
1113 }
1114
1115 public function setThumbSize($a_size)
1116 {
1117 $this->thumb_size = $a_size;
1118 }
1119
1124 {
1125 foreach ($this->getAnswers() as $answer) {
1126 /* @var ASS_AnswerBinaryStateImage $answer */
1127 $answer->setAnswertext($migrator->migrateToLmContent($answer->getAnswertext()));
1128 }
1129 }
1130
1134 public function toJSON()
1135 {
1136 require_once './Services/RTE/classes/class.ilRTE.php';
1137 $result = array();
1138 $result['id'] = (int) $this->getId();
1139 $result['type'] = (string) $this->getQuestionType();
1140 $result['title'] = (string) $this->getTitle();
1141 $result['question'] = $this->formatSAQuestion($this->getQuestion());
1142 $result['nr_of_tries'] = (int) $this->getNrOfTries();
1143 $result['shuffle'] = (bool) $this->getShuffle();
1144 $result['selection_limit'] = (int) $this->getSelectionLimit();
1145 $result['feedback'] = array(
1146 'onenotcorrect' => $this->formatSAQuestion($this->feedbackOBJ->getGenericFeedbackTestPresentation($this->getId(), false)),
1147 'allcorrect' => $this->formatSAQuestion($this->feedbackOBJ->getGenericFeedbackTestPresentation($this->getId(), true))
1148 );
1149
1150 $answers = array();
1151 $has_image = false;
1152 foreach ($this->getAnswers() as $key => $answer_obj) {
1153 if ((string) $answer_obj->getImage()) {
1154 $has_image = true;
1155 }
1156 array_push($answers, array(
1157 "answertext" => (string) $this->formatSAQuestion($answer_obj->getAnswertext()),
1158 "points_checked" => (float) $answer_obj->getPointsChecked(),
1159 "points_unchecked" => (float) $answer_obj->getPointsUnchecked(),
1160 "order" => (int) $answer_obj->getOrder(),
1161 "image" => (string) $answer_obj->getImage(),
1162 "feedback" => $this->formatSAQuestion(
1163 $this->feedbackOBJ->getSpecificAnswerFeedbackExportPresentation($this->getId(), 0, $key)
1164 )
1165 ));
1166 }
1167 $result['answers'] = $answers;
1168
1169 if ($has_image) {
1170 $result['path'] = $this->getImagePathWeb();
1171 $result['thumb'] = $this->getThumbSize();
1172 }
1173
1174 $mobs = ilObjMediaObject::_getMobsOfObject("qpl:html", $this->getId());
1175 $result['mobs'] = $mobs;
1176
1177 return json_encode($result);
1178 }
1179
1180 public function removeAnswerImage($index)
1181 {
1182 $answer = $this->answers[$index];
1183 if (is_object($answer)) {
1184 $this->deleteImage($answer->getImage());
1185 $answer->setImage('');
1186 }
1187 }
1188
1190 {
1191 global $DIC;
1192 $ilUser = $DIC['ilUser'];
1193
1194 $multilineAnswerSetting = $ilUser->getPref("tst_multiline_answers");
1195 if ($multilineAnswerSetting != 1) {
1196 $multilineAnswerSetting = 0;
1197 }
1198 return $multilineAnswerSetting;
1199 }
1200
1201 public function setMultilineAnswerSetting($a_setting = 0)
1202 {
1203 global $DIC;
1204 $ilUser = $DIC['ilUser'];
1205 $ilUser->writePref("tst_multiline_answers", $a_setting);
1206 }
1207
1217 public function setSpecificFeedbackSetting($a_feedback_setting)
1218 {
1219 $this->feedback_setting = $a_feedback_setting;
1220 }
1221
1232 {
1233 if ($this->feedback_setting) {
1235 } else {
1236 return 1;
1237 }
1238 }
1239
1241 {
1242 return 'feedback_correct_sc_mc';
1243 }
1244
1256 public function isAnswered($active_id, $pass = null)
1257 {
1258 $numExistingSolutionRecords = assQuestion::getNumExistingSolutionRecords($active_id, $pass, $this->getId());
1259
1260 return $numExistingSolutionRecords > 0;
1261 }
1262
1274 public static function isObligationPossible($questionId)
1275 {
1277 global $DIC;
1278 $ilDB = $DIC['ilDB'];
1279
1280 $query = "
1281 SELECT SUM(points) points_for_checked_answers
1282 FROM qpl_a_mc
1283 WHERE question_fi = %s AND points > 0
1284 ";
1285
1286 $res = $ilDB->queryF($query, array('integer'), array($questionId));
1287
1288 $row = $ilDB->fetchAssoc($res);
1289
1290 return $row['points_for_checked_answers'] > 0;
1291 }
1292
1301 public function ensureNoInvalidObligation($questionId)
1302 {
1304 global $DIC;
1305 $ilDB = $DIC['ilDB'];
1306
1307 $query = "
1308 SELECT SUM(qpl_a_mc.points) points_for_checked_answers,
1309 test_question_id
1310
1311 FROM tst_test_question
1312
1313 INNER JOIN qpl_a_mc
1314 ON qpl_a_mc.question_fi = tst_test_question.question_fi
1315
1316 WHERE tst_test_question.question_fi = %s
1317 AND tst_test_question.obligatory = 1
1318
1319 GROUP BY test_question_id
1320 ";
1321
1322 $res = $ilDB->queryF($query, array('integer'), array($questionId));
1323
1324 $updateTestQuestionIds = array();
1325
1326 while ($row = $ilDB->fetchAssoc($res)) {
1327 if ($row['points_for_checked_answers'] <= 0) {
1328 $updateTestQuestionIds[] = $row['test_question_id'];
1329 }
1330 }
1331
1332 if (count($updateTestQuestionIds)) {
1333 $test_question_id__IN__updateTestQuestionIds = $ilDB->in(
1334 'test_question_id',
1335 $updateTestQuestionIds,
1336 false,
1337 'integer'
1338 );
1339
1340 $query = "
1341 UPDATE tst_test_question
1342 SET obligatory = 0
1343 WHERE $test_question_id__IN__updateTestQuestionIds
1344 ";
1345
1346 $ilDB->manipulate($query);
1347 }
1348 }
1349
1353 protected function getSolutionSubmit()
1354 {
1355 $solutionSubmit = array();
1356 foreach ($_POST as $key => $value) {
1357 if (preg_match("/^multiple_choice_result_(\d+)/", $key)) {
1358 if (strlen($value)) {
1359 $solutionSubmit[] = $value;
1360 }
1361 }
1362 }
1363 return $solutionSubmit;
1364 }
1365
1371 protected function calculateReachedPointsForSolution($found_values, $active_id = 0)
1372 {
1373 $points = 0;
1374 foreach ($this->answers as $key => $answer) {
1375 if (in_array($key, $found_values)) {
1376 $points += $answer->getPoints();
1377 } else {
1378 $points += $answer->getPointsUnchecked();
1379 }
1380 }
1381 if ($active_id) {
1382 include_once "./Modules/Test/classes/class.ilObjTest.php";
1383 $mc_scoring = ilObjTest::_getMCScoring($active_id);
1384 if (($mc_scoring == 0) && (count($found_values) == 0)) {
1385 $points = 0;
1386 }
1387 }
1388 return $points;
1389 }
1390
1399 public function getOperators($expression)
1400 {
1401 require_once "./Modules/TestQuestionPool/classes/class.ilOperatorsExpressionMapping.php";
1403 }
1404
1409 public function getExpressionTypes()
1410 {
1411 return array(
1416 );
1417 }
1418
1427 public function getUserQuestionResult($active_id, $pass)
1428 {
1430 global $DIC;
1431 $ilDB = $DIC['ilDB'];
1432 $result = new ilUserQuestionResult($this, $active_id, $pass);
1433
1434 $maxStep = $this->lookupMaxStep($active_id, $pass);
1435
1436 if ($maxStep !== null) {
1437 $data = $ilDB->queryF(
1438 "SELECT value1+1 as value1 FROM tst_solutions WHERE active_fi = %s AND pass = %s AND question_fi = %s AND step = %s",
1439 array("integer", "integer", "integer","integer"),
1440 array($active_id, $pass, $this->getId(), $maxStep)
1441 );
1442 } else {
1443 $data = $ilDB->queryF(
1444 "SELECT value1+1 as value1 FROM tst_solutions WHERE active_fi = %s AND pass = %s AND question_fi = %s",
1445 array("integer", "integer", "integer"),
1446 array($active_id, $pass, $this->getId())
1447 );
1448 }
1449
1450 while ($row = $ilDB->fetchAssoc($data)) {
1451 $result->addKeyValue($row["value1"], $row["value1"]);
1452 }
1453
1454 $points = $this->calculateReachedPoints($active_id, $pass);
1455 $max_points = $this->getMaximumPoints();
1456
1457 $result->setReachedPercentage(($points / $max_points) * 100);
1458
1459 return $result;
1460 }
1461
1470 public function getAvailableAnswerOptions($index = null)
1471 {
1472 if ($index !== null) {
1473 return $this->getAnswer($index);
1474 } else {
1475 return $this->getAnswers();
1476 }
1477 }
1478
1479 protected function buildTestPresentationConfig()
1480 {
1481 $config = parent::buildTestPresentationConfig();
1482 $config->setUseUnchangedAnswerLabel($this->lng->txt('tst_mc_label_none_above'));
1483 return $config;
1484 }
1485}
$result
$filename
Definition: buildRTE.php:89
$_POST["username"]
ASS_AnswerBinaryStateImage is a class for answers with a binary state indicator (checked/unchecked,...
An exception for terminatinating execution or to throw for unit testing.
Class for multiple choice tests.
buildTestPresentationConfig()
build basic test question configuration instance
syncImages()
Sync images of a MC question on synchronisation with the original question.
toJSON()
Returns a JSON representation of the question.
getSpecificFeedbackSetting()
Gets the current feedback settings in effect for the question.
setExportDetailsXLS($worksheet, $startrow, $active_id, $pass)
{Creates an Excel worksheet for the detailed cumulated results of this question.object}
copyImages($question_id, $source_questionpool)
saveToDb($original_id="")
Saves a assMultipleChoice object to a database.
loadFromDb($question_id)
Loads a assMultipleChoice object from a database.
duplicate($for_test=true, $title="", $author="", $owner="", $testObjId=null)
Duplicates an assMultipleChoiceQuestion.
getMaximumPoints()
Returns the maximum points, a learner can reach answering the question.
getAnswerCount()
Returns the number of answers.
getAnswerTableName()
Returns the name of the answer table in the database.
isForcedEmptySolution($solutionSubmit)
getAdditionalTableName()
Returns the name of the additional question data table in the database.
setSpecificFeedbackSetting($a_feedback_setting)
Sets the feedback settings in effect for the question.
getAvailableAnswerOptions($index=null)
If index is null, the function returns an array with all anwser options Else it returns the specific ...
setSelectionLimit($selectionLimit)
calculateReachedPointsForSolution($found_values, $active_id=0)
addAnswer( $answertext="", $points=0.0, $points_unchecked=0.0, $order=0, $answerimage="", $answer_id=-1)
Adds a possible answer for a multiple choice question.
getOutputType()
Gets the multiple choice output type which is either OUTPUT_ORDER (=0) or OUTPUT_RANDOM (=1).
copyObject($target_questionpool_id, $title="")
Copies an assMultipleChoice object.
createNewOriginalFromThisDuplicate($targetParentId, $targetQuestionTitle="")
lmMigrateQuestionTypeSpecificContent(ilAssSelfAssessmentMigrator $migrator)
setMultilineAnswerSetting($a_setting=0)
isComplete()
Returns true, if a multiple choice question is complete for use.
flushAnswers()
Deletes all answers.
deleteAnswer($index=0)
Deletes an answer with a given index.
getQuestionType()
Returns the question type of the question.
deleteImage($image_filename)
Deletes an image file.
setImageFile($image_filename, $image_tempfilename="")
Sets the image file and uploads the image to the object's image directory.
__construct( $title="", $comment="", $author="", $owner=-1, $question="", $output_type=OUTPUT_ORDER)
assMultipleChoice constructor
setIsSingleline($isSingleline)
setOutputType($output_type=OUTPUT_ORDER)
Sets the output type of the assMultipleChoice object.
calculateReachedPoints($active_id, $pass=null, $authorizedSolution=true, $returndetails=false)
Returns the points, a learner has reached answering the question.
isAnswered($active_id, $pass=null)
returns boolean wether the question is answered during test pass or not
getAnswer($index=0)
Returns an answer with a given index.
generateThumbForFile($path, $file)
getExpressionTypes()
Get all available expression types for a specific question.
getRTETextWithMediaObjects()
Collects all text in the question which could contain media objects which were created with the Rich ...
rebuildThumbnails()
Rebuild the thumbnail images with a new thumbnail size.
& getAnswers()
Returns a reference to the answers array.
getOperators($expression)
Get all available operations for a specific question.
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.
static isObligationPossible($questionId)
returns boolean wether it is possible to set this question type as obligatory or not considering the ...
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.
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.
removeCurrentSolution($active_id, $pass, $authorized=true)
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.
saveWorkingData($active_id, $pass=null, $authorized=true)
Saves the learners input of the question to the database.
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
setQuestion($question="")
Sets the question string of the question object.
getImagePathWeb()
Returns the web image path for web accessable images of a question.
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 _getMCScoring($active_id)
Gets the scoring type for multiple choice questions.
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 sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
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
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68
$i
Definition: metadata.php:24
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$query
foreach($_POST as $key=> $value) $res
global $ilDB
$data
Definition: storeScorm.php:23