Passed
Push — master ( 4c9a82...f78b85 )
by Julito
12:50 queued 04:06
created

Question::updateLevel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CourseBundle\Entity\CQuizAnswer;
5
6
/**
7
 * Class Question.
8
 *
9
 * This class allows to instantiate an object of type Question
10
 *
11
 * @author Olivier Brouckaert, original author
12
 * @author Patrick Cool, LaTeX support
13
 * @author Julio Montoya <[email protected]> lot of bug fixes
14
 * @author [email protected] - add question categories
15
 *
16
 * @package chamilo.exercise
17
 */
18
abstract class Question
19
{
20
    public $id;
21
    public $iid;
22
    public $question;
23
    public $description;
24
    public $weighting;
25
    public $position;
26
    public $type;
27
    public $level;
28
    public $picture;
29
    public $exerciseList; // array with the list of exercises which this question is in
30
    public $category_list;
31
    public $parent_id;
32
    public $category;
33
    public $isContent;
34
    public $course;
35
    public $feedback;
36
    public static $typePicture = 'new_question.png';
37
    public static $explanationLangVar = '';
38
    public $question_table_class = 'table table-striped';
39
    public $questionTypeWithFeedback;
40
    public $extra;
41
    public $export = false;
42
    public $code;
43
    public static $questionTypes = [
44
        UNIQUE_ANSWER => ['unique_answer.class.php', 'UniqueAnswer'],
45
        MULTIPLE_ANSWER => ['multiple_answer.class.php', 'MultipleAnswer'],
46
        FILL_IN_BLANKS => ['fill_blanks.class.php', 'FillBlanks'],
47
        MATCHING => ['matching.class.php', 'Matching'],
48
        FREE_ANSWER => ['freeanswer.class.php', 'FreeAnswer'],
49
        ORAL_EXPRESSION => ['oral_expression.class.php', 'OralExpression'],
50
        HOT_SPOT => ['hotspot.class.php', 'HotSpot'],
51
        HOT_SPOT_DELINEATION => ['hotspot.class.php', 'HotspotDelineation'],
52
        MULTIPLE_ANSWER_COMBINATION => ['multiple_answer_combination.class.php', 'MultipleAnswerCombination'],
53
        UNIQUE_ANSWER_NO_OPTION => ['unique_answer_no_option.class.php', 'UniqueAnswerNoOption'],
54
        MULTIPLE_ANSWER_TRUE_FALSE => ['multiple_answer_true_false.class.php', 'MultipleAnswerTrueFalse'],
55
        MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY => [
56
            'MultipleAnswerTrueFalseDegreeCertainty.php',
57
            'MultipleAnswerTrueFalseDegreeCertainty',
58
        ],
59
        MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE => [
60
            'multiple_answer_combination_true_false.class.php',
61
            'MultipleAnswerCombinationTrueFalse',
62
        ],
63
        GLOBAL_MULTIPLE_ANSWER => ['global_multiple_answer.class.php', 'GlobalMultipleAnswer'],
64
        CALCULATED_ANSWER => ['calculated_answer.class.php', 'CalculatedAnswer'],
65
        UNIQUE_ANSWER_IMAGE => ['UniqueAnswerImage.php', 'UniqueAnswerImage'],
66
        DRAGGABLE => ['Draggable.php', 'Draggable'],
67
        MATCHING_DRAGGABLE => ['MatchingDraggable.php', 'MatchingDraggable'],
68
        //MEDIA_QUESTION => array('media_question.class.php' , 'MediaQuestion')
69
        ANNOTATION => ['Annotation.php', 'Annotation'],
70
        READING_COMPREHENSION => ['ReadingComprehension.php', 'ReadingComprehension'],
71
    ];
72
73
    /**
74
     * constructor of the class.
75
     *
76
     * @author Olivier Brouckaert
77
     */
78
    public function __construct()
79
    {
80
        $this->id = 0;
81
        $this->iid = 0;
82
        $this->question = '';
83
        $this->description = '';
84
        $this->weighting = 0;
85
        $this->position = 1;
86
        $this->picture = '';
87
        $this->level = 1;
88
        $this->category = 0;
89
        // This variable is used when loading an exercise like an scenario with
90
        // an special hotspot: final_overlap, final_missing, final_excess
91
        $this->extra = '';
92
        $this->exerciseList = [];
93
        $this->course = api_get_course_info();
94
        $this->category_list = [];
95
        $this->parent_id = 0;
96
        // See BT#12611
97
        $this->questionTypeWithFeedback = [
98
            MATCHING,
99
            MATCHING_DRAGGABLE,
100
            DRAGGABLE,
101
            FILL_IN_BLANKS,
102
            FREE_ANSWER,
103
            ORAL_EXPRESSION,
104
            CALCULATED_ANSWER,
105
            ANNOTATION,
106
        ];
107
    }
108
109
    /**
110
     * @return int|null
111
     */
112
    public function getIsContent()
113
    {
114
        $isContent = null;
115
        if (isset($_REQUEST['isContent'])) {
116
            $isContent = intval($_REQUEST['isContent']);
117
        }
118
119
        return $this->isContent = $isContent;
120
    }
121
122
    /**
123
     * Reads question information from the data base.
124
     *
125
     * @param int $id        - question ID
126
     * @param int $course_id
127
     *
128
     * @return Question
129
     *
130
     * @author Olivier Brouckaert
131
     */
132
    public static function read($id, $course_id = null)
133
    {
134
        $id = (int) $id;
135
        if (!empty($course_id)) {
136
            $course_info = api_get_course_info_by_id($course_id);
137
        } else {
138
            $course_info = api_get_course_info();
139
        }
140
141
        $course_id = $course_info['real_id'];
142
143
        if (empty($course_id) || $course_id == -1) {
144
            return false;
145
        }
146
147
        $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
148
        $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
149
150
        $sql = "SELECT *
151
                FROM $TBL_QUESTIONS
152
                WHERE c_id = $course_id AND id = $id ";
153
        $result = Database::query($sql);
154
155
        // if the question has been found
156
        if ($object = Database::fetch_object($result)) {
157
            $objQuestion = self::getInstance($object->type);
158
            if (!empty($objQuestion)) {
159
                $objQuestion->id = (int) $id;
160
                $objQuestion->iid = (int) $object->iid;
161
                $objQuestion->question = $object->question;
162
                $objQuestion->description = $object->description;
163
                $objQuestion->weighting = $object->ponderation;
164
                $objQuestion->position = $object->position;
165
                $objQuestion->type = (int) $object->type;
166
                $objQuestion->picture = $object->picture;
167
                $objQuestion->level = (int) $object->level;
168
                $objQuestion->extra = $object->extra;
169
                $objQuestion->course = $course_info;
170
                $objQuestion->feedback = isset($object->feedback) ? $object->feedback : '';
171
                $objQuestion->category = TestCategory::getCategoryForQuestion($id, $course_id);
172
                $objQuestion->code = isset($object->code) ? $object->code : '';
173
174
                $tblQuiz = Database::get_course_table(TABLE_QUIZ_TEST);
175
                $sql = "SELECT DISTINCT q.exercice_id
176
                        FROM $TBL_EXERCISE_QUESTION q
177
                        INNER JOIN $tblQuiz e
178
                        ON e.c_id = q.c_id AND e.id = q.exercice_id
179
                        WHERE
180
                            q.c_id = $course_id AND
181
                            q.question_id = $id AND
182
                            e.active >= 0";
183
184
                $result = Database::query($sql);
185
186
                // fills the array with the exercises which this question is in
187
                if ($result) {
0 ignored issues
show
introduced by
$result is of type Doctrine\DBAL\Driver\Statement, thus it always evaluated to true.
Loading history...
188
                    while ($obj = Database::fetch_object($result)) {
189
                        $objQuestion->exerciseList[] = $obj->exercice_id;
190
                    }
191
                }
192
193
                return $objQuestion;
194
            }
195
        }
196
197
        // question not found
198
        return false;
199
    }
200
201
    /**
202
     * returns the question ID.
203
     *
204
     * @author Olivier Brouckaert
205
     *
206
     * @return int - question ID
207
     */
208
    public function selectId()
209
    {
210
        return $this->id;
211
    }
212
213
    /**
214
     * returns the question title.
215
     *
216
     * @author Olivier Brouckaert
217
     *
218
     * @return string - question title
219
     */
220
    public function selectTitle()
221
    {
222
        if (!api_get_configuration_value('save_titles_as_html')) {
223
            return $this->question;
224
        }
225
226
        return Display::div($this->question, ['style' => 'display: inline-block;']);
227
    }
228
229
    /**
230
     * @param int $itemNumber
231
     *
232
     * @return string
233
     */
234
    public function getTitleToDisplay($itemNumber)
235
    {
236
        $showQuestionTitleHtml = api_get_configuration_value('save_titles_as_html');
237
        $title = '';
238
        if (api_get_configuration_value('show_question_id')) {
239
            $title .= '<h4>#'.$this->course['code'].'-'.$this->iid.'</h4>';
240
        }
241
242
        $title .= $showQuestionTitleHtml ? '' : '<strong>';
243
        $title .= $itemNumber.'. '.$this->selectTitle();
244
        $title .= $showQuestionTitleHtml ? '' : '</strong>';
245
246
        return Display::div(
247
            $title,
248
            ['class' => 'question_title']
249
        );
250
    }
251
252
    /**
253
     * returns the question description.
254
     *
255
     * @author Olivier Brouckaert
256
     *
257
     * @return string - question description
258
     */
259
    public function selectDescription()
260
    {
261
        return $this->description;
262
    }
263
264
    /**
265
     * returns the question weighting.
266
     *
267
     * @author Olivier Brouckaert
268
     *
269
     * @return int - question weighting
270
     */
271
    public function selectWeighting()
272
    {
273
        return $this->weighting;
274
    }
275
276
    /**
277
     * returns the question position.
278
     *
279
     * @author Olivier Brouckaert
280
     *
281
     * @return int - question position
282
     */
283
    public function selectPosition()
284
    {
285
        return $this->position;
286
    }
287
288
    /**
289
     * returns the answer type.
290
     *
291
     * @author Olivier Brouckaert
292
     *
293
     * @return int - answer type
294
     */
295
    public function selectType()
296
    {
297
        return $this->type;
298
    }
299
300
    /**
301
     * returns the level of the question.
302
     *
303
     * @author Nicolas Raynaud
304
     *
305
     * @return int - level of the question, 0 by default
306
     */
307
    public function getLevel()
308
    {
309
        return $this->level;
310
    }
311
312
    /**
313
     * returns the picture name.
314
     *
315
     * @author Olivier Brouckaert
316
     *
317
     * @return string - picture name
318
     */
319
    public function selectPicture()
320
    {
321
        return $this->picture;
322
    }
323
324
    /**
325
     * @return string
326
     */
327
    public function selectPicturePath()
328
    {
329
        if (!empty($this->picture)) {
330
            return api_get_path(WEB_COURSE_PATH).$this->course['directory'].'/document/images/'.$this->getPictureFilename();
331
        }
332
333
        return '';
334
    }
335
336
    /**
337
     * @return int|string
338
     */
339
    public function getPictureId()
340
    {
341
        // for backward compatibility
342
        // when in field picture we had the filename not the document id
343
        if (preg_match("/quiz-.*/", $this->picture)) {
344
            return DocumentManager::get_document_id(
345
                $this->course,
346
                $this->selectPicturePath(),
347
                api_get_session_id()
348
            );
349
        }
350
351
        return $this->picture;
352
    }
353
354
    /**
355
     * @param int $courseId
356
     * @param int $sessionId
357
     *
358
     * @return string
359
     */
360
    public function getPictureFilename($courseId = 0, $sessionId = 0)
361
    {
362
        $courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId;
363
        $sessionId = empty($sessionId) ? api_get_session_id() : (int) $sessionId;
364
365
        if (empty($courseId)) {
366
            return '';
367
        }
368
        // for backward compatibility
369
        // when in field picture we had the filename not the document id
370
        if (preg_match("/quiz-.*/", $this->picture)) {
371
            return $this->picture;
372
        }
373
374
        $pictureId = $this->getPictureId();
375
        $courseInfo = $this->course;
376
        $documentInfo = DocumentManager::get_document_data_by_id(
377
            $pictureId,
378
            $courseInfo['code'],
379
            false,
380
            $sessionId
381
        );
382
        $documentFilename = '';
383
        if ($documentInfo) {
384
            // document in document/images folder
385
            $documentFilename = pathinfo(
386
                $documentInfo['path'],
387
                PATHINFO_BASENAME
388
            );
389
        }
390
391
        return $documentFilename;
392
    }
393
394
    /**
395
     * returns the array with the exercise ID list.
396
     *
397
     * @author Olivier Brouckaert
398
     *
399
     * @return array - list of exercise ID which the question is in
400
     */
401
    public function selectExerciseList()
402
    {
403
        return $this->exerciseList;
404
    }
405
406
    /**
407
     * returns the number of exercises which this question is in.
408
     *
409
     * @author Olivier Brouckaert
410
     *
411
     * @return int - number of exercises
412
     */
413
    public function selectNbrExercises()
414
    {
415
        return count($this->exerciseList);
416
    }
417
418
    /**
419
     * changes the question title.
420
     *
421
     * @param string $title - question title
422
     *
423
     * @author Olivier Brouckaert
424
     */
425
    public function updateTitle($title)
426
    {
427
        $this->question = $title;
428
    }
429
430
    /**
431
     * @param int $id
432
     */
433
    public function updateParentId($id)
434
    {
435
        $this->parent_id = (int) $id;
436
    }
437
438
    /**
439
     * changes the question description.
440
     *
441
     * @param string $description - question description
442
     *
443
     * @author Olivier Brouckaert
444
     */
445
    public function updateDescription($description)
446
    {
447
        $this->description = $description;
448
    }
449
450
    /**
451
     * changes the question weighting.
452
     *
453
     * @param int $weighting - question weighting
454
     *
455
     * @author Olivier Brouckaert
456
     */
457
    public function updateWeighting($weighting)
458
    {
459
        $this->weighting = $weighting;
460
    }
461
462
    /**
463
     * @param array $category
464
     *
465
     * @author Hubert Borderiou 12-10-2011
466
     */
467
    public function updateCategory($category)
468
    {
469
        $this->category = $category;
470
    }
471
472
    /**
473
     * @param int $value
474
     *
475
     * @author Hubert Borderiou 12-10-2011
476
     */
477
    public function updateScoreAlwaysPositive($value)
478
    {
479
        $this->scoreAlwaysPositive = $value;
480
    }
481
482
    /**
483
     * @param int $value
484
     *
485
     * @author Hubert Borderiou 12-10-2011
486
     */
487
    public function updateUncheckedMayScore($value)
488
    {
489
        $this->uncheckedMayScore = $value;
490
    }
491
492
    /**
493
     * Save category of a question.
494
     *
495
     * A question can have n categories if category is empty,
496
     * then question has no category then delete the category entry
497
     *
498
     * @param array $category_list
499
     *
500
     * @author Julio Montoya - Adding multiple cat support
501
     */
502
    public function saveCategories($category_list)
503
    {
504
        if (!empty($category_list)) {
505
            $this->deleteCategory();
506
            $table = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
507
508
            // update or add category for a question
509
            foreach ($category_list as $category_id) {
510
                $category_id = (int) $category_id;
511
                $question_id = (int) $this->id;
512
                $sql = "SELECT count(*) AS nb
513
                        FROM $table
514
                        WHERE
515
                            category_id = $category_id
516
                            AND question_id = $question_id
517
                            AND c_id=".api_get_course_int_id();
518
                $res = Database::query($sql);
519
                $row = Database::fetch_array($res);
520
                if ($row['nb'] > 0) {
521
                    // DO nothing
522
                } else {
523
                    $sql = "INSERT INTO $table (c_id, question_id, category_id)
524
                            VALUES (".api_get_course_int_id().", $question_id, $category_id)";
525
                    Database::query($sql);
526
                }
527
            }
528
        }
529
    }
530
531
    /**
532
     * in this version, a question can only have 1 category
533
     * if category is 0, then question has no category then delete the category entry.
534
     *
535
     * @param int $categoryId
536
     * @param int $courseId
537
     *
538
     * @return bool
539
     *
540
     * @author Hubert Borderiou 12-10-2011
541
     */
542
    public function saveCategory($categoryId, $courseId = 0)
543
    {
544
        $courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId;
545
546
        if (empty($courseId)) {
547
            return false;
548
        }
549
550
        if ($categoryId <= 0) {
551
            $this->deleteCategory($courseId);
552
        } else {
553
            // update or add category for a question
554
            $table = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
555
            $categoryId = (int) $categoryId;
556
            $question_id = (int) $this->id;
557
            $sql = "SELECT count(*) AS nb FROM $table
558
                    WHERE
559
                        question_id = $question_id AND
560
                        c_id = ".$courseId;
561
            $res = Database::query($sql);
562
            $row = Database::fetch_array($res);
563
            if ($row['nb'] > 0) {
564
                $sql = "UPDATE $table
565
                        SET category_id = $categoryId
566
                        WHERE
567
                            question_id = $question_id AND
568
                            c_id = ".$courseId;
569
                Database::query($sql);
570
            } else {
571
                $sql = "INSERT INTO $table (c_id, question_id, category_id)
572
                        VALUES (".$courseId.", $question_id, $categoryId)";
573
                Database::query($sql);
574
            }
575
576
            return true;
577
        }
578
    }
579
580
    /**
581
     * @author hubert borderiou 12-10-2011
582
     *
583
     * @param int $courseId
584
     *                      delete any category entry for question id
585
     *                      delete the category for question
586
     *
587
     * @return bool
588
     */
589
    public function deleteCategory($courseId = 0)
590
    {
591
        $courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId;
592
        $table = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
593
        $questionId = (int) $this->id;
594
        if (empty($courseId) || empty($questionId)) {
595
            return false;
596
        }
597
        $sql = "DELETE FROM $table
598
                WHERE
599
                    question_id = $questionId AND
600
                    c_id = ".$courseId;
601
        Database::query($sql);
602
603
        return true;
604
    }
605
606
    /**
607
     * changes the question position.
608
     *
609
     * @param int $position - question position
610
     *
611
     * @author Olivier Brouckaert
612
     */
613
    public function updatePosition($position)
614
    {
615
        $this->position = $position;
616
    }
617
618
    /**
619
     * changes the question level.
620
     *
621
     * @param int $level - question level
622
     *
623
     * @author Nicolas Raynaud
624
     */
625
    public function updateLevel($level)
626
    {
627
        $this->level = $level;
628
    }
629
630
    /**
631
     * changes the answer type. If the user changes the type from "unique answer" to "multiple answers"
632
     * (or conversely) answers are not deleted, otherwise yes.
633
     *
634
     * @param int $type - answer type
635
     *
636
     * @author Olivier Brouckaert
637
     */
638
    public function updateType($type)
639
    {
640
        $table = Database::get_course_table(TABLE_QUIZ_ANSWER);
641
        $course_id = $this->course['real_id'];
642
643
        if (empty($course_id)) {
644
            $course_id = api_get_course_int_id();
645
        }
646
        // if we really change the type
647
        if ($type != $this->type) {
648
            // if we don't change from "unique answer" to "multiple answers" (or conversely)
649
            if (!in_array($this->type, [UNIQUE_ANSWER, MULTIPLE_ANSWER]) ||
650
                !in_array($type, [UNIQUE_ANSWER, MULTIPLE_ANSWER])
651
            ) {
652
                // removes old answers
653
                $sql = "DELETE FROM $table
654
                        WHERE c_id = $course_id AND question_id = ".intval($this->id);
655
                Database::query($sql);
656
            }
657
658
            $this->type = $type;
659
        }
660
    }
661
662
    /**
663
     * Get default hot spot folder in documents.
664
     *
665
     * @param array $courseInfo
666
     *
667
     * @return string
668
     */
669
    public function getHotSpotFolderInCourse($courseInfo = [])
670
    {
671
        $courseInfo = empty($courseInfo) ? $this->course : $courseInfo;
672
673
        if (empty($courseInfo) || empty($courseInfo['directory'])) {
674
            // Stop everything if course is not set.
675
            api_not_allowed();
676
        }
677
678
        $pictureAbsolutePath = api_get_path(SYS_COURSE_PATH).$courseInfo['directory'].'/document/images/';
679
        $picturePath = basename($pictureAbsolutePath);
680
681
        if (!is_dir($picturePath)) {
682
            create_unexisting_directory(
683
                $courseInfo,
684
                api_get_user_id(),
685
                0,
686
                0,
687
                0,
688
                dirname($pictureAbsolutePath),
689
                '/'.$picturePath,
690
                $picturePath,
691
                '',
692
                false,
693
                false
694
            );
695
        }
696
697
        return $pictureAbsolutePath;
698
    }
699
700
    /**
701
     * adds a picture to the question.
702
     *
703
     * @param string $picture - temporary path of the picture to upload
704
     *
705
     * @return bool - true if uploaded, otherwise false
706
     *
707
     * @author Olivier Brouckaert
708
     */
709
    public function uploadPicture($picture)
710
    {
711
        $picturePath = $this->getHotSpotFolderInCourse();
712
713
        // if the question has got an ID
714
        if ($this->id) {
715
            $pictureFilename = self::generatePictureName();
716
            $img = new Image($picture);
717
            $img->send_image($picturePath.'/'.$pictureFilename, -1, 'jpg');
718
            $document = DocumentManager::addDocument(
719
                $this->course,
720
                '/images/'.$pictureFilename,
721
                'file',
722
                filesize($picturePath.'/'.$pictureFilename),
723
                $pictureFilename
724
            );
725
726
            if ($document) {
727
                $this->picture = $document->getId();
728
729
                if (!file_exists($picturePath.'/'.$pictureFilename)) {
730
                    return false;
731
                }
732
733
                $this->resizePicture('width', 800);
734
735
                return true;
736
            }
737
        }
738
739
        return false;
740
    }
741
742
    /**
743
     * return the name for image use in hotspot question
744
     * to be unique, name is quiz-[utc unix timestamp].jpg.
745
     *
746
     * @param string $prefix
747
     * @param string $extension
748
     *
749
     * @return string
750
     */
751
    public function generatePictureName($prefix = 'quiz-', $extension = 'jpg')
752
    {
753
        // image name is quiz-xxx.jpg in folder images/
754
        $utcTime = time();
755
756
        return $prefix.$utcTime.'.'.$extension;
757
    }
758
759
    /**
760
     * deletes the picture.
761
     *
762
     * @author Olivier Brouckaert
763
     *
764
     * @return bool - true if removed, otherwise false
765
     */
766
    public function removePicture()
767
    {
768
        $picturePath = $this->getHotSpotFolderInCourse();
769
770
        // if the question has got an ID and if the picture exists
771
        if ($this->id) {
772
            $picture = $this->picture;
773
            $this->picture = '';
774
775
            return @unlink($picturePath.'/'.$picture) ? true : false;
776
        }
777
778
        return false;
779
    }
780
781
    /**
782
     * Exports a picture to another question.
783
     *
784
     * @author Olivier Brouckaert
785
     *
786
     * @param int   $questionId - ID of the target question
787
     * @param array $courseInfo destination course info
788
     *
789
     * @return bool - true if copied, otherwise false
790
     */
791
    public function exportPicture($questionId, $courseInfo)
792
    {
793
        if (empty($questionId) || empty($courseInfo)) {
794
            return false;
795
        }
796
797
        $course_id = $courseInfo['real_id'];
798
        $destination_path = $this->getHotSpotFolderInCourse($courseInfo);
799
800
        if (empty($destination_path)) {
801
            return false;
802
        }
803
804
        $source_path = $this->getHotSpotFolderInCourse();
805
806
        // if the question has got an ID and if the picture exists
807
        if (!$this->id || empty($this->picture)) {
808
            return false;
809
        }
810
811
        $sourcePictureName = $this->getPictureFilename($course_id);
812
        $picture = $this->generatePictureName();
813
        $result = false;
814
        if (file_exists($source_path.'/'.$sourcePictureName)) {
815
            // for backward compatibility
816
            $result = copy(
817
                $source_path.'/'.$sourcePictureName,
818
                $destination_path.'/'.$picture
819
            );
820
        } else {
821
            $imageInfo = DocumentManager::get_document_data_by_id(
822
                $this->picture,
823
                $courseInfo['code']
824
            );
825
            if (file_exists($imageInfo['absolute_path'])) {
826
                $result = @copy(
827
                    $imageInfo['absolute_path'],
828
                    $destination_path.'/'.$picture
829
                );
830
            }
831
        }
832
833
        // If copy was correct then add to the database
834
        if (!$result) {
835
            return false;
836
        }
837
838
        $table = Database::get_course_table(TABLE_QUIZ_QUESTION);
839
        $sql = "UPDATE $table SET
840
                picture = '".Database::escape_string($picture)."'
841
                WHERE c_id = $course_id AND id='".intval($questionId)."'";
842
        Database::query($sql);
843
844
        $documentId = DocumentManager::addDocument(
845
            $courseInfo,
846
            '/images/'.$picture,
847
            'file',
848
            filesize($destination_path.'/'.$picture),
849
            $picture
850
        );
851
852
        if (!$documentId) {
853
            return false;
854
        }
855
856
        return true;
857
    }
858
859
    /**
860
     * Saves the picture coming from POST into a temporary file
861
     * Temporary pictures are used when we don't want to save a picture right after a form submission.
862
     * For example, if we first show a confirmation box.
863
     *
864
     * @author Olivier Brouckaert
865
     *
866
     * @param string $picture     - temporary path of the picture to move
867
     * @param string $pictureName - Name of the picture
868
     */
869
    public function setTmpPicture($picture, $pictureName)
870
    {
871
        $picturePath = $this->getHotSpotFolderInCourse();
872
        $pictureName = explode('.', $pictureName);
873
        $Extension = $pictureName[sizeof($pictureName) - 1];
874
875
        // saves the picture into a temporary file
876
        @move_uploaded_file($picture, $picturePath.'/tmp.'.$Extension);
877
    }
878
879
    /**
880
     * Set title.
881
     *
882
     * @param string $title
883
     */
884
    public function setTitle($title)
885
    {
886
        $this->question = $title;
887
    }
888
889
    /**
890
     * Sets extra info.
891
     *
892
     * @param string $extra
893
     */
894
    public function setExtra($extra)
895
    {
896
        $this->extra = $extra;
897
    }
898
899
    /**
900
     * updates the question in the data base
901
     * if an exercise ID is provided, we add that exercise ID into the exercise list.
902
     *
903
     * @author Olivier Brouckaert
904
     *
905
     * @param Exercise $exercise
906
     */
907
    public function save($exercise)
908
    {
909
        $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
910
        $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
911
        $em = Database::getManager();
912
        $exerciseId = $exercise->id;
913
914
        $id = $this->id;
915
        $question = $this->question;
916
        $description = $this->description;
917
        $weighting = $this->weighting;
918
        $position = $this->position;
919
        $type = $this->type;
920
        $picture = $this->picture;
921
        $level = $this->level;
922
        $extra = $this->extra;
923
        $c_id = $this->course['real_id'];
924
        $categoryId = $this->category;
925
926
        // question already exists
927
        if (!empty($id)) {
928
            $params = [
929
                'question' => $question,
930
                'description' => $description,
931
                'ponderation' => $weighting,
932
                'position' => $position,
933
                'type' => $type,
934
                'picture' => $picture,
935
                'extra' => $extra,
936
                'level' => $level,
937
            ];
938
            if ($exercise->questionFeedbackEnabled) {
939
                $params['feedback'] = $this->feedback;
940
            }
941
            Database::update(
942
                $TBL_QUESTIONS,
943
                $params,
944
                ['c_id = ? AND id = ?' => [$c_id, $id]]
945
            );
946
947
            Event::addEvent(
948
                LOG_QUESTION_UPDATED,
949
                LOG_QUESTION_ID,
950
                $this->iid
951
            );
952
            $this->saveCategory($categoryId);
953
954
            if (!empty($exerciseId)) {
955
                api_item_property_update(
956
                    $this->course,
957
                    TOOL_QUIZ,
958
                    $id,
959
                    'QuizQuestionUpdated',
960
                    api_get_user_id()
961
                );
962
            }
963
            if (api_get_setting('search_enabled') == 'true') {
964
                if ($exerciseId != 0) {
965
                    $this->search_engine_edit($exerciseId);
966
                } else {
967
                    /**
968
                     * actually there is *not* an user interface for
969
                     * creating questions without a relation with an exercise.
970
                     */
971
                }
972
            }
973
        } else {
974
            // creates a new question
975
            $sql = "SELECT max(position)
976
                    FROM $TBL_QUESTIONS as question,
977
                    $TBL_EXERCISE_QUESTION as test_question
978
                    WHERE
979
                        question.id = test_question.question_id AND
980
                        test_question.exercice_id = ".intval($exerciseId)." AND
981
                        question.c_id = $c_id AND
982
                        test_question.c_id = $c_id ";
983
            $result = Database::query($sql);
984
            $current_position = Database::result($result, 0, 0);
985
            $this->updatePosition($current_position + 1);
986
            $position = $this->position;
987
988
            $params = [
989
                'c_id' => $c_id,
990
                'question' => $question,
991
                'description' => $description,
992
                'ponderation' => $weighting,
993
                'position' => $position,
994
                'type' => $type,
995
                'picture' => $picture,
996
                'extra' => $extra,
997
                'level' => $level,
998
            ];
999
1000
            if ($exercise->questionFeedbackEnabled) {
1001
                $params['feedback'] = $this->feedback;
1002
            }
1003
            $this->id = Database::insert($TBL_QUESTIONS, $params);
1004
1005
            if ($this->id) {
1006
                $sql = "UPDATE $TBL_QUESTIONS SET id = iid WHERE iid = {$this->id}";
1007
                Database::query($sql);
1008
1009
                Event::addEvent(
1010
                    LOG_QUESTION_CREATED,
1011
                    LOG_QUESTION_ID,
1012
                    $this->id
1013
                );
1014
1015
                api_item_property_update(
1016
                    $this->course,
1017
                    TOOL_QUIZ,
1018
                    $this->id,
1019
                    'QuizQuestionAdded',
1020
                    api_get_user_id()
1021
                );
1022
1023
                // If hotspot, create first answer
1024
                if ($type == HOT_SPOT || $type == HOT_SPOT_ORDER) {
1025
                    $quizAnswer = new CQuizAnswer();
1026
                    $quizAnswer
1027
                        ->setCId($c_id)
1028
                        ->setQuestionId($this->id)
1029
                        ->setAnswer('')
1030
                        ->setPonderation(10)
1031
                        ->setPosition(1)
1032
                        ->setHotspotCoordinates('0;0|0|0')
1033
                        ->setHotspotType('square');
1034
1035
                    $em->persist($quizAnswer);
1036
                    $em->flush();
1037
1038
                    $id = $quizAnswer->getIid();
1039
1040
                    if ($id) {
1041
                        $quizAnswer
1042
                            ->setId($id)
1043
                            ->setIdAuto($id);
1044
1045
                        $em->merge($quizAnswer);
1046
                        $em->flush();
1047
                    }
1048
                }
1049
1050
                if ($type == HOT_SPOT_DELINEATION) {
1051
                    $quizAnswer = new CQuizAnswer();
1052
                    $quizAnswer
1053
                        ->setCId($c_id)
1054
                        ->setQuestionId($this->id)
1055
                        ->setAnswer('')
1056
                        ->setPonderation(10)
1057
                        ->setPosition(1)
1058
                        ->setHotspotCoordinates('0;0|0|0')
1059
                        ->setHotspotType('delineation');
1060
1061
                    $em->persist($quizAnswer);
1062
                    $em->flush();
1063
1064
                    $id = $quizAnswer->getIid();
1065
1066
                    if ($id) {
1067
                        $quizAnswer
1068
                            ->setId($id)
1069
                            ->setIdAuto($id);
1070
1071
                        $em->merge($quizAnswer);
1072
                        $em->flush();
1073
                    }
1074
                }
1075
1076
                if (api_get_setting('search_enabled') == 'true') {
1077
                    if ($exerciseId != 0) {
1078
                        $this->search_engine_edit($exerciseId, true);
1079
                    } else {
1080
                        /**
1081
                         * actually there is *not* an user interface for
1082
                         * creating questions without a relation with an exercise.
1083
                         */
1084
                    }
1085
                }
1086
            }
1087
        }
1088
1089
        // if the question is created in an exercise
1090
        if ($exerciseId) {
1091
            // adds the exercise into the exercise list of this question
1092
            $this->addToList($exerciseId, true);
1093
        }
1094
    }
1095
1096
    public function search_engine_edit(
1097
        $exerciseId,
1098
        $addQs = false,
1099
        $rmQs = false
1100
    ) {
1101
        // update search engine and its values table if enabled
1102
        if (api_get_setting('search_enabled') == 'true' && extension_loaded('xapian')) {
1103
            $course_id = api_get_course_id();
1104
            // get search_did
1105
            $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
1106
            if ($addQs || $rmQs) {
1107
                //there's only one row per question on normal db and one document per question on search engine db
1108
                $sql = 'SELECT * FROM %s
1109
                    WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_second_level=%s LIMIT 1';
1110
                $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
1111
            } else {
1112
                $sql = 'SELECT * FROM %s
1113
                    WHERE course_code=\'%s\' AND tool_id=\'%s\'
1114
                    AND ref_id_high_level=%s AND ref_id_second_level=%s LIMIT 1';
1115
                $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $exerciseId, $this->id);
1116
            }
1117
            $res = Database::query($sql);
1118
1119
            if (Database::num_rows($res) > 0 || $addQs) {
1120
                $di = new ChamiloIndexer();
1121
                if ($addQs) {
1122
                    $question_exercises = [(int) $exerciseId];
1123
                } else {
1124
                    $question_exercises = [];
1125
                }
1126
                isset($_POST['language']) ? $lang = Database::escape_string($_POST['language']) : $lang = 'english';
1127
                $di->connectDb(null, null, $lang);
1128
1129
                // retrieve others exercise ids
1130
                $se_ref = Database::fetch_array($res);
1131
                $se_doc = $di->get_document((int) $se_ref['search_did']);
1132
                if ($se_doc !== false) {
1133
                    if (($se_doc_data = $di->get_document_data($se_doc)) !== false) {
1134
                        $se_doc_data = unserialize($se_doc_data);
1135
                        if (isset($se_doc_data[SE_DATA]['type']) &&
1136
                            $se_doc_data[SE_DATA]['type'] == SE_DOCTYPE_EXERCISE_QUESTION
1137
                        ) {
1138
                            if (isset($se_doc_data[SE_DATA]['exercise_ids']) &&
1139
                                is_array($se_doc_data[SE_DATA]['exercise_ids'])
1140
                            ) {
1141
                                foreach ($se_doc_data[SE_DATA]['exercise_ids'] as $old_value) {
1142
                                    if (!in_array($old_value, $question_exercises)) {
1143
                                        $question_exercises[] = $old_value;
1144
                                    }
1145
                                }
1146
                            }
1147
                        }
1148
                    }
1149
                }
1150
                if ($rmQs) {
1151
                    while (($key = array_search($exerciseId, $question_exercises)) !== false) {
1152
                        unset($question_exercises[$key]);
1153
                    }
1154
                }
1155
1156
                // build the chunk to index
1157
                $ic_slide = new IndexableChunk();
1158
                $ic_slide->addValue("title", $this->question);
1159
                $ic_slide->addCourseId($course_id);
1160
                $ic_slide->addToolId(TOOL_QUIZ);
1161
                $xapian_data = [
1162
                    SE_COURSE_ID => $course_id,
1163
                    SE_TOOL_ID => TOOL_QUIZ,
1164
                    SE_DATA => [
1165
                        'type' => SE_DOCTYPE_EXERCISE_QUESTION,
1166
                        'exercise_ids' => $question_exercises,
1167
                        'question_id' => (int) $this->id,
1168
                    ],
1169
                    SE_USER => (int) api_get_user_id(),
1170
                ];
1171
                $ic_slide->xapian_data = serialize($xapian_data);
1172
                $ic_slide->addValue("content", $this->description);
1173
1174
                //TODO: index answers, see also form validation on question_admin.inc.php
1175
1176
                $di->remove_document($se_ref['search_did']);
1177
                $di->addChunk($ic_slide);
1178
1179
                //index and return search engine document id
1180
                if (!empty($question_exercises)) { // if empty there is nothing to index
1181
                    $did = $di->index();
1182
                    unset($di);
1183
                }
1184
                if ($did || $rmQs) {
1185
                    // save it to db
1186
                    if ($addQs || $rmQs) {
1187
                        $sql = "DELETE FROM %s
1188
                            WHERE course_code = '%s' AND tool_id = '%s' AND ref_id_second_level = '%s'";
1189
                        $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
1190
                    } else {
1191
                        $sql = "DELETE FROM %S
1192
                            WHERE
1193
                                course_code = '%s'
1194
                                AND tool_id = '%s'
1195
                                AND tool_id = '%s'
1196
                                AND ref_id_high_level = '%s'
1197
                                AND ref_id_second_level = '%s'";
1198
                        $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $exerciseId, $this->id);
1199
                    }
1200
                    Database::query($sql);
1201
                    if ($rmQs) {
1202
                        if (!empty($question_exercises)) {
1203
                            $sql = "INSERT INTO %s (
1204
                                    id, course_code, tool_id, ref_id_high_level, ref_id_second_level, search_did
1205
                                )
1206
                                VALUES (
1207
                                    NULL, '%s', '%s', %s, %s, %s
1208
                                )";
1209
                            $sql = sprintf(
1210
                                $sql,
1211
                                $tbl_se_ref,
1212
                                $course_id,
1213
                                TOOL_QUIZ,
1214
                                array_shift($question_exercises),
1215
                                $this->id,
1216
                                $did
1217
                            );
1218
                            Database::query($sql);
1219
                        }
1220
                    } else {
1221
                        $sql = "INSERT INTO %s (
1222
                                id, course_code, tool_id, ref_id_high_level, ref_id_second_level, search_did
1223
                            )
1224
                            VALUES (
1225
                                NULL , '%s', '%s', %s, %s, %s
1226
                            )";
1227
                        $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $exerciseId, $this->id, $did);
1228
                        Database::query($sql);
1229
                    }
1230
                }
1231
            }
1232
        }
1233
    }
1234
1235
    /**
1236
     * adds an exercise into the exercise list.
1237
     *
1238
     * @author Olivier Brouckaert
1239
     *
1240
     * @param int  $exerciseId - exercise ID
1241
     * @param bool $fromSave   - from $this->save() or not
1242
     */
1243
    public function addToList($exerciseId, $fromSave = false)
1244
    {
1245
        $exerciseRelQuestionTable = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
1246
        $id = $this->id;
1247
        // checks if the exercise ID is not in the list
1248
        if (!in_array($exerciseId, $this->exerciseList)) {
1249
            $this->exerciseList[] = $exerciseId;
1250
            $new_exercise = new Exercise();
1251
            $new_exercise->read($exerciseId);
1252
            $count = $new_exercise->selectNbrQuestions();
1253
            $count++;
1254
            $sql = "INSERT INTO $exerciseRelQuestionTable (c_id, question_id, exercice_id, question_order)
1255
                    VALUES ({$this->course['real_id']}, ".intval($id).", ".intval($exerciseId).", '$count')";
1256
            Database::query($sql);
1257
1258
            // we do not want to reindex if we had just saved adnd indexed the question
1259
            if (!$fromSave) {
1260
                $this->search_engine_edit($exerciseId, true);
1261
            }
1262
        }
1263
    }
1264
1265
    /**
1266
     * removes an exercise from the exercise list.
1267
     *
1268
     * @author Olivier Brouckaert
1269
     *
1270
     * @param int $exerciseId - exercise ID
1271
     *
1272
     * @return bool - true if removed, otherwise false
1273
     */
1274
    public function removeFromList($exerciseId)
1275
    {
1276
        $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
1277
        $id = $this->id;
1278
1279
        // searches the position of the exercise ID in the list
1280
        $pos = array_search($exerciseId, $this->exerciseList);
1281
        $course_id = api_get_course_int_id();
1282
1283
        // exercise not found
1284
        if ($pos === false) {
1285
            return false;
1286
        } else {
1287
            // deletes the position in the array containing the wanted exercise ID
1288
            unset($this->exerciseList[$pos]);
1289
            //update order of other elements
1290
            $sql = "SELECT question_order
1291
                    FROM $TBL_EXERCISE_QUESTION
1292
                    WHERE
1293
                        c_id = $course_id
1294
                        AND question_id = ".intval($id)."
1295
                        AND exercice_id = ".intval($exerciseId);
1296
            $res = Database::query($sql);
1297
            if (Database::num_rows($res) > 0) {
1298
                $row = Database::fetch_array($res);
1299
                if (!empty($row['question_order'])) {
1300
                    $sql = "UPDATE $TBL_EXERCISE_QUESTION
1301
                        SET question_order = question_order-1
1302
                        WHERE
1303
                            c_id = $course_id
1304
                            AND exercice_id = ".intval($exerciseId)."
1305
                            AND question_order > ".$row['question_order'];
1306
                    Database::query($sql);
1307
                }
1308
            }
1309
1310
            $sql = "DELETE FROM $TBL_EXERCISE_QUESTION
1311
                    WHERE
1312
                        c_id = $course_id
1313
                        AND question_id = ".intval($id)."
1314
                        AND exercice_id = ".intval($exerciseId);
1315
            Database::query($sql);
1316
1317
            return true;
1318
        }
1319
    }
1320
1321
    /**
1322
     * Deletes a question from the database
1323
     * the parameter tells if the question is removed from all exercises (value = 0),
1324
     * or just from one exercise (value = exercise ID).
1325
     *
1326
     * @author Olivier Brouckaert
1327
     *
1328
     * @param int $deleteFromEx - exercise ID if the question is only removed from one exercise
1329
     */
1330
    public function delete($deleteFromEx = 0)
1331
    {
1332
        $course_id = api_get_course_int_id();
1333
1334
        $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
1335
        $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
1336
        $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
1337
        $TBL_QUIZ_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
1338
1339
        $id = (int) $this->id;
1340
1341
        // if the question must be removed from all exercises
1342
        if (!$deleteFromEx) {
1343
            //update the question_order of each question to avoid inconsistencies
1344
            $sql = "SELECT exercice_id, question_order 
1345
                    FROM $TBL_EXERCISE_QUESTION
1346
                    WHERE c_id = $course_id AND question_id = ".intval($id);
1347
1348
            $res = Database::query($sql);
1349
            if (Database::num_rows($res) > 0) {
1350
                while ($row = Database::fetch_array($res)) {
1351
                    if (!empty($row['question_order'])) {
1352
                        $sql = "UPDATE $TBL_EXERCISE_QUESTION
1353
                                SET question_order = question_order-1
1354
                                WHERE
1355
                                    c_id = $course_id AND 
1356
                                    exercice_id = ".intval($row['exercice_id'])." AND 
1357
                                    question_order > ".$row['question_order'];
1358
                        Database::query($sql);
1359
                    }
1360
                }
1361
            }
1362
1363
            $sql = "DELETE FROM $TBL_EXERCISE_QUESTION
1364
                    WHERE c_id = $course_id AND question_id = ".$id;
1365
            Database::query($sql);
1366
1367
            $sql = "DELETE FROM $TBL_QUESTIONS
1368
                    WHERE c_id = $course_id AND id = ".$id;
1369
            Database::query($sql);
1370
1371
            $sql = "DELETE FROM $TBL_REPONSES
1372
                    WHERE c_id = $course_id AND question_id = ".$id;
1373
            Database::query($sql);
1374
1375
            // remove the category of this question in the question_rel_category table
1376
            $sql = "DELETE FROM $TBL_QUIZ_QUESTION_REL_CATEGORY
1377
                    WHERE 
1378
                        c_id = $course_id AND 
1379
                        question_id = ".$id;
1380
            Database::query($sql);
1381
1382
            api_item_property_update(
1383
                $this->course,
1384
                TOOL_QUIZ,
1385
                $id,
1386
                'QuizQuestionDeleted',
1387
                api_get_user_id()
1388
            );
1389
            $this->removePicture();
1390
        } else {
1391
            // just removes the exercise from the list
1392
            $this->removeFromList($deleteFromEx);
1393
            if (api_get_setting('search_enabled') == 'true' && extension_loaded('xapian')) {
1394
                // disassociate question with this exercise
1395
                $this->search_engine_edit($deleteFromEx, false, true);
1396
            }
1397
1398
            api_item_property_update(
1399
                $this->course,
1400
                TOOL_QUIZ,
1401
                $id,
1402
                'QuizQuestionDeleted',
1403
                api_get_user_id()
1404
            );
1405
        }
1406
    }
1407
1408
    /**
1409
     * Duplicates the question.
1410
     *
1411
     * @author Olivier Brouckaert
1412
     *
1413
     * @param array $courseInfo Course info of the destination course
1414
     *
1415
     * @return false|string ID of the new question
1416
     */
1417
    public function duplicate($courseInfo = [])
1418
    {
1419
        $courseInfo = empty($courseInfo) ? $this->course : $courseInfo;
1420
1421
        if (empty($courseInfo)) {
1422
            return false;
1423
        }
1424
        $questionTable = Database::get_course_table(TABLE_QUIZ_QUESTION);
1425
        $TBL_QUESTION_OPTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
1426
1427
        $question = $this->question;
1428
        $description = $this->description;
1429
        $weighting = $this->weighting;
1430
        $position = $this->position;
1431
        $type = $this->type;
1432
        $level = (int) $this->level;
1433
        $extra = $this->extra;
1434
1435
        // Using the same method used in the course copy to transform URLs
1436
        if ($this->course['id'] != $courseInfo['id']) {
1437
            $description = DocumentManager::replaceUrlWithNewCourseCode(
1438
                $description,
1439
                $this->course['code'],
1440
                $courseInfo['id']
1441
            );
1442
            $question = DocumentManager::replaceUrlWithNewCourseCode(
1443
                $question,
1444
                $this->course['code'],
1445
                $courseInfo['id']
1446
            );
1447
        }
1448
1449
        $course_id = $courseInfo['real_id'];
1450
1451
        // Read the source options
1452
        $options = self::readQuestionOption($this->id, $this->course['real_id']);
1453
1454
        // Inserting in the new course db / or the same course db
1455
        $params = [
1456
            'c_id' => $course_id,
1457
            'question' => $question,
1458
            'description' => $description,
1459
            'ponderation' => $weighting,
1460
            'position' => $position,
1461
            'type' => $type,
1462
            'level' => $level,
1463
            'extra' => $extra,
1464
        ];
1465
        $newQuestionId = Database::insert($questionTable, $params);
1466
1467
        if ($newQuestionId) {
1468
            $sql = "UPDATE $questionTable 
1469
                    SET id = iid
1470
                    WHERE iid = $newQuestionId";
1471
            Database::query($sql);
1472
1473
            if (!empty($options)) {
1474
                // Saving the quiz_options
1475
                foreach ($options as $item) {
1476
                    $item['question_id'] = $newQuestionId;
1477
                    $item['c_id'] = $course_id;
1478
                    unset($item['id']);
1479
                    unset($item['iid']);
1480
                    $id = Database::insert($TBL_QUESTION_OPTIONS, $item);
1481
                    if ($id) {
1482
                        $sql = "UPDATE $TBL_QUESTION_OPTIONS 
1483
                                SET id = iid
1484
                                WHERE iid = $id";
1485
                        Database::query($sql);
1486
                    }
1487
                }
1488
            }
1489
1490
            // Duplicates the picture of the hotspot
1491
            $this->exportPicture($newQuestionId, $courseInfo);
1492
        }
1493
1494
        return $newQuestionId;
1495
    }
1496
1497
    /**
1498
     * @return string
1499
     */
1500
    public function get_question_type_name()
1501
    {
1502
        $key = self::$questionTypes[$this->type];
1503
1504
        return get_lang($key[1]);
1505
    }
1506
1507
    /**
1508
     * @param string $type
1509
     */
1510
    public static function get_question_type($type)
1511
    {
1512
        if ($type == ORAL_EXPRESSION && api_get_setting('enable_record_audio') !== 'true') {
1513
            return null;
1514
        }
1515
1516
        return self::$questionTypes[$type];
1517
    }
1518
1519
    /**
1520
     * @return array
1521
     */
1522
    public static function get_question_type_list()
1523
    {
1524
        if (api_get_setting('enable_record_audio') !== 'true') {
1525
            self::$questionTypes[ORAL_EXPRESSION] = null;
1526
            unset(self::$questionTypes[ORAL_EXPRESSION]);
1527
        }
1528
        if (api_get_setting('enable_quiz_scenario') !== 'true') {
1529
            self::$questionTypes[HOT_SPOT_DELINEATION] = null;
1530
            unset(self::$questionTypes[HOT_SPOT_DELINEATION]);
1531
        }
1532
1533
        return self::$questionTypes;
1534
    }
1535
1536
    /**
1537
     * Returns an instance of the class corresponding to the type.
1538
     *
1539
     * @param int $type the type of the question
1540
     *
1541
     * @return $this instance of a Question subclass (or of Questionc class by default)
1542
     */
1543
    public static function getInstance($type)
1544
    {
1545
        if (!is_null($type)) {
1546
            list($fileName, $className) = self::get_question_type($type);
1547
            if (!empty($fileName)) {
1548
                if (class_exists($className)) {
1549
                    return new $className();
1550
                } else {
1551
                    echo 'Can\'t instanciate class '.$className.' of type '.$type;
1552
                }
1553
            }
1554
        }
1555
1556
        return null;
1557
    }
1558
1559
    /**
1560
     * Creates the form to create / edit a question
1561
     * A subclass can redefine this function to add fields...
1562
     *
1563
     * @param FormValidator $form
1564
     * @param Exercise      $exercise
1565
     */
1566
    public function createForm(&$form, $exercise)
1567
    {
1568
        echo '<style>
1569
                .media { display:none;}
1570
            </style>';
1571
1572
        // question name
1573
        if (api_get_configuration_value('save_titles_as_html')) {
1574
            $editorConfig = ['ToolbarSet' => 'Minimal'];
1575
            $form->addHtmlEditor(
1576
                'questionName',
1577
                get_lang('Question'),
1578
                false,
1579
                false,
1580
                $editorConfig,
1581
                true
1582
            );
1583
        } else {
1584
            $form->addElement('text', 'questionName', get_lang('Question'));
1585
        }
1586
1587
        $form->addRule('questionName', get_lang('GiveQuestion'), 'required');
1588
1589
        // default content
1590
        $isContent = isset($_REQUEST['isContent']) ? intval($_REQUEST['isContent']) : null;
1591
1592
        // Question type
1593
        $answerType = isset($_REQUEST['answerType']) ? intval($_REQUEST['answerType']) : null;
1594
        $form->addElement('hidden', 'answerType', $answerType);
1595
1596
        // html editor
1597
        $editorConfig = [
1598
            'ToolbarSet' => 'TestQuestionDescription',
1599
            'Height' => '150',
1600
        ];
1601
1602
        if (!api_is_allowed_to_edit(null, true)) {
1603
            $editorConfig['UserStatus'] = 'student';
1604
        }
1605
1606
        $form->addButtonAdvancedSettings('advanced_params');
1607
        $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
1608
        $form->addHtmlEditor(
1609
            'questionDescription',
1610
            get_lang('QuestionDescription'),
1611
            false,
1612
            false,
1613
            $editorConfig
1614
        );
1615
1616
        // hidden values
1617
        $my_id = isset($_REQUEST['myid']) ? intval($_REQUEST['myid']) : null;
1618
        $form->addElement('hidden', 'myid', $my_id);
1619
1620
        if ($this->type != MEDIA_QUESTION) {
1621
            // Advanced parameters
1622
            $select_level = self::get_default_levels();
1623
            $form->addElement(
1624
                'select',
1625
                'questionLevel',
1626
                get_lang('Difficulty'),
1627
                $select_level
1628
            );
1629
1630
            // Categories
1631
            $tabCat = TestCategory::getCategoriesIdAndName();
1632
            $form->addElement(
1633
                'select',
1634
                'questionCategory',
1635
                get_lang('Category'),
1636
                $tabCat
1637
            );
1638
1639
            global $text;
1640
1641
            switch ($this->type) {
1642
                case UNIQUE_ANSWER:
1643
                    $buttonGroup = [];
1644
                    $buttonGroup[] = $form->addButtonSave(
1645
                        $text,
1646
                        'submitQuestion',
1647
                        true
1648
                    );
1649
                    $buttonGroup[] = $form->addButton(
1650
                        'convertAnswer',
1651
                        get_lang('ConvertToMultipleAnswer'),
1652
                        'dot-circle-o',
1653
                        'default',
1654
                        null,
1655
                        null,
1656
                        null,
1657
                        true
1658
                    );
1659
                    $form->addGroup($buttonGroup);
1660
                    break;
1661
                case MULTIPLE_ANSWER:
1662
                    $buttonGroup = [];
1663
                    $buttonGroup[] = $form->addButtonSave(
1664
                        $text,
1665
                        'submitQuestion',
1666
                        true
1667
                    );
1668
                    $buttonGroup[] = $form->addButton(
1669
                        'convertAnswer',
1670
                        get_lang('ConvertToUniqueAnswer'),
1671
                        'check-square-o',
1672
                        'default',
1673
                        null,
1674
                        null,
1675
                        null,
1676
                        true
1677
                    );
1678
                    $form->addGroup($buttonGroup);
1679
                    break;
1680
            }
1681
            //Medias
1682
            //$course_medias = self::prepare_course_media_select(api_get_course_int_id());
1683
            //$form->addElement('select', 'parent_id', get_lang('AttachToMedia'), $course_medias);
1684
        }
1685
1686
        $form->addElement('html', '</div>');
1687
1688
        if (!isset($_GET['fromExercise'])) {
1689
            switch ($answerType) {
1690
                case 1:
1691
                    $this->question = get_lang('DefaultUniqueQuestion');
1692
                    break;
1693
                case 2:
1694
                    $this->question = get_lang('DefaultMultipleQuestion');
1695
                    break;
1696
                case 3:
1697
                    $this->question = get_lang('DefaultFillBlankQuestion');
1698
                    break;
1699
                case 4:
1700
                    $this->question = get_lang('DefaultMathingQuestion');
1701
                    break;
1702
                case 5:
1703
                    $this->question = get_lang('DefaultOpenQuestion');
1704
                    break;
1705
                case 9:
1706
                    $this->question = get_lang('DefaultMultipleQuestion');
1707
                    break;
1708
            }
1709
        }
1710
1711
        if (!is_null($exercise)) {
1712
            if ($exercise->questionFeedbackEnabled && $this->showFeedback($exercise)) {
1713
                $form->addTextarea('feedback', get_lang('FeedbackIfNotCorrect'));
1714
            }
1715
        }
1716
1717
        // default values
1718
        $defaults = [];
1719
        $defaults['questionName'] = $this->question;
1720
        $defaults['questionDescription'] = $this->description;
1721
        $defaults['questionLevel'] = $this->level;
1722
        $defaults['questionCategory'] = $this->category;
1723
        $defaults['feedback'] = $this->feedback;
1724
1725
        // Came from he question pool
1726
        if (isset($_GET['fromExercise'])) {
1727
            $form->setDefaults($defaults);
1728
        }
1729
1730
        if (!empty($_REQUEST['myid'])) {
1731
            $form->setDefaults($defaults);
1732
        } else {
1733
            if ($isContent == 1) {
1734
                $form->setDefaults($defaults);
1735
            }
1736
        }
1737
    }
1738
1739
    /**
1740
     * function which process the creation of questions.
1741
     *
1742
     * @param FormValidator $form
1743
     * @param Exercise      $exercise
1744
     */
1745
    public function processCreation($form, $exercise)
1746
    {
1747
        $this->updateTitle($form->getSubmitValue('questionName'));
1748
        $this->updateDescription($form->getSubmitValue('questionDescription'));
1749
        $this->updateLevel($form->getSubmitValue('questionLevel'));
1750
        $this->updateCategory($form->getSubmitValue('questionCategory'));
1751
        $this->setFeedback($form->getSubmitValue('feedback'));
1752
1753
        //Save normal question if NOT media
1754
        if ($this->type != MEDIA_QUESTION) {
1755
            $this->save($exercise);
1756
1757
            $exercise->addToList($this->id);
1758
            $exercise->update_question_positions();
1759
        }
1760
    }
1761
1762
    /**
1763
     * abstract function which creates the form to create / edit the answers of the question.
1764
     *
1765
     * @param FormValidator $form
1766
     */
1767
    abstract public function createAnswersForm($form);
1768
1769
    /**
1770
     * abstract function which process the creation of answers.
1771
     *
1772
     * @param FormValidator $form
1773
     * @param Exercise      $exercise
1774
     */
1775
    abstract public function processAnswersCreation($form, $exercise);
1776
1777
    /**
1778
     * Displays the menu of question types.
1779
     *
1780
     * @param Exercise $objExercise
1781
     */
1782
    public static function displayTypeMenu($objExercise)
1783
    {
1784
        $feedback_type = $objExercise->feedback_type;
1785
        $exerciseId = $objExercise->id;
1786
1787
        // 1. by default we show all the question types
1788
        $question_type_custom_list = self::get_question_type_list();
1789
1790
        if (!isset($feedback_type)) {
1791
            $feedback_type = 0;
1792
        }
1793
1794
        if ($feedback_type == 1) {
1795
            //2. but if it is a feedback DIRECT we only show the UNIQUE_ANSWER type that is currently available
1796
            $question_type_custom_list = [
1797
                UNIQUE_ANSWER => self::$questionTypes[UNIQUE_ANSWER],
1798
                HOT_SPOT_DELINEATION => self::$questionTypes[HOT_SPOT_DELINEATION],
1799
            ];
1800
        } else {
1801
            unset($question_type_custom_list[HOT_SPOT_DELINEATION]);
1802
        }
1803
1804
        echo '<div class="card card-exercise">';
1805
        echo '<div class="card-body">';
1806
        echo '<ul class="list-exercise">';
1807
        foreach ($question_type_custom_list as $i => $a_type) {
1808
            // @todo remove require_once classes are already loaded using composer
1809
            // include the class of the type
1810
            require_once $a_type[0];
1811
            // get the picture of the type and the langvar which describes it
1812
            $img = $explanation = '';
1813
            eval('$img = '.$a_type[1].'::$typePicture;');
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
1814
            eval('$explanation = get_lang('.$a_type[1].'::$explanationLangVar);');
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
1815
            echo '<li>';
1816
            echo '<div class="icon">';
1817
            $icon = '<a href="admin.php?'.api_get_cidreq().'&newQuestion=yes&answerType='.$i.'">'.
1818
                Display::return_icon($img, $explanation, null, ICON_SIZE_BIG).'</a>';
1819
1820
            if ($objExercise->force_edit_exercise_in_lp === false) {
1821
                if ($objExercise->exercise_was_added_in_lp == true) {
1822
                    $img = pathinfo($img);
1823
                    $img = $img['filename'].'_na.'.$img['extension'];
1824
                    $icon = Display::return_icon($img, $explanation, null, ICON_SIZE_BIG);
1825
                }
1826
            }
1827
1828
            echo $icon;
1829
            echo '</div>';
1830
            echo '</li>';
1831
        }
1832
1833
        echo '<li>';
1834
        echo '<div class="content">';
1835
        if ($objExercise->exercise_was_added_in_lp == true) {
1836
            echo Display::return_icon(
1837
                'database_na.png',
1838
                get_lang('GetExistingQuestion'),
1839
                null,
1840
                ICON_SIZE_BIG
1841
            );
1842
        } else {
1843
            if ($feedback_type == 1) {
1844
                echo $url = "<a href=\"question_pool.php?".api_get_cidreq()."&type=1&fromExercise=$exerciseId\">";
1845
            } else {
1846
                echo $url = '<a href="question_pool.php?'.api_get_cidreq().'&fromExercise='.$exerciseId.'">';
1847
            }
1848
            echo Display::return_icon(
1849
                'database.png',
1850
                get_lang('GetExistingQuestion'),
1851
                null,
1852
                ICON_SIZE_BIG
1853
            );
1854
        }
1855
        echo '</a>';
1856
        echo '</div></li>';
1857
        echo '</ul>';
1858
        echo '</div>';
1859
        echo '</div>';
1860
    }
1861
1862
    /**
1863
     * @param int    $question_id
1864
     * @param string $name
1865
     * @param int    $course_id
1866
     * @param int    $position
1867
     *
1868
     * @return false|string
1869
     */
1870
    public static function saveQuestionOption($question_id, $name, $course_id, $position = 0)
1871
    {
1872
        $table = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
1873
        $params['question_id'] = (int) $question_id;
1874
        $params['name'] = $name;
1875
        $params['position'] = $position;
1876
        $params['c_id'] = $course_id;
1877
        $result = self::readQuestionOption($question_id, $course_id);
1878
        $last_id = Database::insert($table, $params);
1879
        if ($last_id) {
1880
            $sql = "UPDATE $table SET id = iid WHERE iid = $last_id";
1881
            Database::query($sql);
1882
        }
1883
1884
        return $last_id;
1885
    }
1886
1887
    /**
1888
     * @param int $question_id
1889
     * @param int $course_id
1890
     */
1891
    public static function deleteAllQuestionOptions($question_id, $course_id)
1892
    {
1893
        $table = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
1894
        Database::delete(
1895
            $table,
1896
            [
1897
                'c_id = ? AND question_id = ?' => [
1898
                    $course_id,
1899
                    $question_id,
1900
                ],
1901
            ]
1902
        );
1903
    }
1904
1905
    /**
1906
     * @param int   $id
1907
     * @param array $params
1908
     * @param int   $course_id
1909
     *
1910
     * @return bool|int
1911
     */
1912
    public static function updateQuestionOption($id, $params, $course_id)
1913
    {
1914
        $table = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
1915
        $result = Database::update(
1916
            $table,
1917
            $params,
1918
            ['c_id = ? AND id = ?' => [$course_id, $id]]
1919
        );
1920
1921
        return $result;
1922
    }
1923
1924
    /**
1925
     * @param int $question_id
1926
     * @param int $course_id
1927
     *
1928
     * @return array
1929
     */
1930
    public static function readQuestionOption($question_id, $course_id)
1931
    {
1932
        $table = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
1933
        $result = Database::select(
1934
            '*',
1935
            $table,
1936
            [
1937
                'where' => [
1938
                    'c_id = ? AND question_id = ?' => [
1939
                        $course_id,
1940
                        $question_id,
1941
                    ],
1942
                ],
1943
                'order' => 'id ASC',
1944
            ]
1945
        );
1946
1947
        return $result;
1948
    }
1949
1950
    /**
1951
     * Shows question title an description.
1952
     *
1953
     * @param Exercise $exercise
1954
     * @param int      $counter
1955
     * @param array    $score
1956
     *
1957
     * @return string HTML string with the header of the question (before the answers table)
1958
     */
1959
    public function return_header($exercise, $counter = null, $score = [])
1960
    {
1961
        $counterLabel = '';
1962
        if (!empty($counter)) {
1963
            $counterLabel = (int) $counter;
1964
        }
1965
        $score_label = get_lang('Wrong');
1966
        $class = 'error';
1967
        if (isset($score['pass']) && $score['pass'] == true) {
1968
            $score_label = get_lang('Correct');
1969
            $class = 'success';
1970
        }
1971
1972
        if (in_array($this->type, [FREE_ANSWER, ORAL_EXPRESSION, ANNOTATION])) {
1973
            $score['revised'] = isset($score['revised']) ? $score['revised'] : false;
1974
            if ($score['revised'] == true) {
1975
                $score_label = get_lang('Revised');
1976
                $class = '';
1977
            } else {
1978
                $score_label = get_lang('NotRevised');
1979
                $class = 'warning';
1980
                if (isset($score['weight'])) {
1981
                    $weight = float_format($score['weight'], 1);
1982
                    $score['result'] = ' ? / '.$weight;
1983
                }
1984
                $model = ExerciseLib::getCourseScoreModel();
1985
                if (!empty($model)) {
1986
                    $score['result'] = ' ? ';
1987
                }
1988
1989
                $hide = api_get_configuration_value('hide_free_question_score');
1990
                if ($hide === true) {
1991
                    $score['result'] = '-';
1992
                }
1993
            }
1994
        }
1995
1996
        // display question category, if any
1997
        $header = '';
1998
        if ($exercise->display_category_name) {
1999
            $header = TestCategory::returnCategoryAndTitle($this->id);
2000
        }
2001
        $show_media = '';
2002
        if ($show_media) {
2003
            $header .= $this->show_media_content();
2004
        }
2005
        $scoreCurrent = [
2006
            'used' => isset($score['score']) ? $score['score'] : '',
2007
            'missing' => isset($score['weight']) ? $score['weight'] : '',
2008
        ];
2009
        $header .= Display::page_subheader2($counterLabel.'. '.$this->question);
2010
2011
        // dont display score for certainty degree questions
2012
        if ($this->type != MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
2013
            if (isset($score['result'])) {
2014
                $header .= $exercise->getQuestionRibbon($class, $score_label, $score['result'], $scoreCurrent);
2015
            }
2016
        }
2017
2018
        if ($this->type != READING_COMPREHENSION) {
2019
            // Do not show the description (the text to read) if the question is of type READING_COMPREHENSION
2020
            $header .= Display::div(
2021
                $this->description,
2022
                ['class' => 'question_description']
2023
            );
2024
        } else {
2025
            if ($score['pass'] == true) {
2026
                $message = Display::div(
2027
                    sprintf(
2028
                        get_lang('ReadingQuestionCongratsSpeedXReachedForYWords'),
2029
                        ReadingComprehension::$speeds[$this->level],
2030
                        $this->getWordsCount()
2031
                    )
2032
                );
2033
            } else {
2034
                $message = Display::div(
2035
                    sprintf(
2036
                        get_lang('ReadingQuestionCongratsSpeedXNotReachedForYWords'),
2037
                        ReadingComprehension::$speeds[$this->level],
2038
                        $this->getWordsCount()
2039
                    )
2040
                );
2041
            }
2042
            $header .= $message.'<br />';
2043
        }
2044
2045
        if (isset($score['pass']) && $score['pass'] === false) {
2046
            if ($this->showFeedback($exercise)) {
2047
                $header .= $this->returnFormatFeedback();
2048
            }
2049
        }
2050
2051
        return $header;
2052
    }
2053
2054
    /**
2055
     * Create a question from a set of parameters.
2056
     *
2057
     * @param   int     Quiz ID
2058
     * @param   string  Question name
2059
     * @param   int     Maximum result for the question
2060
     * @param   int     Type of question (see constants at beginning of question.class.php)
2061
     * @param   int     Question level/category
2062
     * @param string $quiz_id
2063
     */
2064
    public function create_question(
2065
        $quiz_id,
2066
        $question_name,
2067
        $question_description = '',
2068
        $max_score = 0,
2069
        $type = 1,
2070
        $level = 1
2071
    ) {
2072
        $course_id = api_get_course_int_id();
2073
        $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
2074
        $tbl_quiz_rel_question = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
2075
2076
        $quiz_id = intval($quiz_id);
2077
        $max_score = (float) $max_score;
2078
        $type = intval($type);
2079
        $level = intval($level);
2080
2081
        // Get the max position
2082
        $sql = "SELECT max(position) as max_position
2083
                FROM $tbl_quiz_question q 
2084
                INNER JOIN $tbl_quiz_rel_question r
2085
                ON
2086
                    q.id = r.question_id AND
2087
                    exercice_id = $quiz_id AND
2088
                    q.c_id = $course_id AND
2089
                    r.c_id = $course_id";
2090
        $rs_max = Database::query($sql);
2091
        $row_max = Database::fetch_object($rs_max);
2092
        $max_position = $row_max->max_position + 1;
2093
2094
        $params = [
2095
            'c_id' => $course_id,
2096
            'question' => $question_name,
2097
            'description' => $question_description,
2098
            'ponderation' => $max_score,
2099
            'position' => $max_position,
2100
            'type' => $type,
2101
            'level' => $level,
2102
        ];
2103
        $question_id = Database::insert($tbl_quiz_question, $params);
2104
2105
        if ($question_id) {
2106
            $sql = "UPDATE $tbl_quiz_question  
2107
                    SET id = iid WHERE iid = $question_id";
2108
            Database::query($sql);
2109
2110
            // Get the max question_order
2111
            $sql = "SELECT max(question_order) as max_order
2112
                    FROM $tbl_quiz_rel_question
2113
                    WHERE c_id = $course_id AND exercice_id = $quiz_id ";
2114
            $rs_max_order = Database::query($sql);
2115
            $row_max_order = Database::fetch_object($rs_max_order);
2116
            $max_order = $row_max_order->max_order + 1;
2117
            // Attach questions to quiz
2118
            $sql = "INSERT INTO $tbl_quiz_rel_question (c_id, question_id, exercice_id, question_order)
2119
                    VALUES($course_id, $question_id, $quiz_id, $max_order)";
2120
            Database::query($sql);
2121
        }
2122
2123
        return $question_id;
2124
    }
2125
2126
    /**
2127
     * @return array the image filename of the question type
2128
     */
2129
    public function get_type_icon_html()
2130
    {
2131
        $type = $this->selectType();
2132
        $tabQuestionList = self::get_question_type_list(); // [0]=file to include [1]=type name
2133
2134
        require_once $tabQuestionList[$type][0];
2135
2136
        $img = $explanation = null;
2137
        eval('$img = '.$tabQuestionList[$type][1].'::$typePicture;');
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
2138
        eval('$explanation = get_lang('.$tabQuestionList[$type][1].'::$explanationLangVar);');
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
2139
2140
        return [$img, $explanation];
2141
    }
2142
2143
    /**
2144
     * Get course medias.
2145
     *
2146
     * @param int course id
2147
     * @param int $course_id
2148
     *
2149
     * @return array
2150
     */
2151
    public static function get_course_medias(
2152
        $course_id,
2153
        $start = 0,
2154
        $limit = 100,
2155
        $sidx = "question",
2156
        $sord = "ASC",
2157
        $where_condition = []
2158
    ) {
2159
        $table_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
2160
        $default_where = [
2161
            'c_id = ? AND parent_id = 0 AND type = ?' => [
2162
                $course_id,
2163
                MEDIA_QUESTION,
2164
            ],
2165
        ];
2166
        $result = Database::select(
2167
            '*',
2168
            $table_question,
2169
            [
2170
                'limit' => " $start, $limit",
2171
                'where' => $default_where,
2172
                'order' => "$sidx $sord",
2173
            ]
2174
        );
2175
2176
        return $result;
2177
    }
2178
2179
    /**
2180
     * Get count course medias.
2181
     *
2182
     * @param int course id
2183
     *
2184
     * @return int
2185
     */
2186
    public static function get_count_course_medias($course_id)
2187
    {
2188
        $table_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
2189
        $result = Database::select(
2190
            'count(*) as count',
2191
            $table_question,
2192
            [
2193
                'where' => [
2194
                    'c_id = ? AND parent_id = 0 AND type = ?' => [
2195
                        $course_id,
2196
                        MEDIA_QUESTION,
2197
                    ],
2198
                ],
2199
            ],
2200
            'first'
2201
        );
2202
2203
        if ($result && isset($result['count'])) {
2204
            return $result['count'];
2205
        }
2206
2207
        return 0;
2208
    }
2209
2210
    /**
2211
     * @param int $course_id
2212
     *
2213
     * @return array
2214
     */
2215
    public static function prepare_course_media_select($course_id)
2216
    {
2217
        $medias = self::get_course_medias($course_id);
2218
        $media_list = [];
2219
        $media_list[0] = get_lang('NoMedia');
2220
2221
        if (!empty($medias)) {
2222
            foreach ($medias as $media) {
2223
                $media_list[$media['id']] = empty($media['question']) ? get_lang('Untitled') : $media['question'];
2224
            }
2225
        }
2226
2227
        return $media_list;
2228
    }
2229
2230
    /**
2231
     * @return array
2232
     */
2233
    public static function get_default_levels()
2234
    {
2235
        $levels = [
2236
            1 => 1,
2237
            2 => 2,
2238
            3 => 3,
2239
            4 => 4,
2240
            5 => 5,
2241
        ];
2242
2243
        return $levels;
2244
    }
2245
2246
    /**
2247
     * @return string
2248
     */
2249
    public function show_media_content()
2250
    {
2251
        $html = '';
2252
        if ($this->parent_id != 0) {
2253
            $parent_question = self::read($this->parent_id);
2254
            $html = $parent_question->show_media_content();
2255
        } else {
2256
            $html .= Display::page_subheader($this->selectTitle());
2257
            $html .= $this->selectDescription();
2258
        }
2259
2260
        return $html;
2261
    }
2262
2263
    /**
2264
     * Swap between unique and multiple type answers.
2265
     *
2266
     * @return UniqueAnswer|MultipleAnswer
2267
     */
2268
    public function swapSimpleAnswerTypes()
2269
    {
2270
        $oppositeAnswers = [
2271
            UNIQUE_ANSWER => MULTIPLE_ANSWER,
2272
            MULTIPLE_ANSWER => UNIQUE_ANSWER,
2273
        ];
2274
        $this->type = $oppositeAnswers[$this->type];
2275
        Database::update(
2276
            Database::get_course_table(TABLE_QUIZ_QUESTION),
2277
            ['type' => $this->type],
2278
            ['c_id = ? AND id = ?' => [$this->course['real_id'], $this->id]]
2279
        );
2280
        $answerClasses = [
2281
            UNIQUE_ANSWER => 'UniqueAnswer',
2282
            MULTIPLE_ANSWER => 'MultipleAnswer',
2283
        ];
2284
        $swappedAnswer = new $answerClasses[$this->type]();
2285
        foreach ($this as $key => $value) {
2286
            $swappedAnswer->$key = $value;
2287
        }
2288
2289
        return $swappedAnswer;
2290
    }
2291
2292
    /**
2293
     * @param array $score
2294
     *
2295
     * @return bool
2296
     */
2297
    public function isQuestionWaitingReview($score)
2298
    {
2299
        $isReview = false;
2300
        if (!empty($score)) {
2301
            if (!empty($score['comments']) || $score['score'] > 0) {
2302
                $isReview = true;
2303
            }
2304
        }
2305
2306
        return $isReview;
2307
    }
2308
2309
    /**
2310
     * @param string $value
2311
     */
2312
    public function setFeedback($value)
2313
    {
2314
        $this->feedback = $value;
2315
    }
2316
2317
    /**
2318
     * @param Exercise $exercise
2319
     *
2320
     * @return bool
2321
     */
2322
    public function showFeedback($exercise)
2323
    {
2324
        return
2325
            in_array($this->type, $this->questionTypeWithFeedback) &&
2326
            $exercise->feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM;
2327
    }
2328
2329
    /**
2330
     * @return string
2331
     */
2332
    public function returnFormatFeedback()
2333
    {
2334
        return '<br />'.Display::return_message($this->feedback, 'normal', false);
2335
    }
2336
2337
    /**
2338
     * Check if this question exists in another exercise.
2339
     *
2340
     * @throws \Doctrine\ORM\Query\QueryException
2341
     *
2342
     * @return mixed
2343
     */
2344
    public function existsInAnotherExercises()
2345
    {
2346
        $em = Database::getManager();
2347
2348
        $count = $em
2349
            ->createQuery('
2350
                SELECT COUNT(qq.iid) FROM ChamiloCourseBundle:CQuizRelQuestion qq
2351
                WHERE qq.questionId = :id
2352
            ')
2353
            ->setParameters(['id' => (int) $this->id])
2354
            ->getSingleScalarResult();
2355
2356
        return $count > 1;
2357
    }
2358
2359
    /**
2360
     * Resizes a picture || Warning!: can only be called after uploadPicture,
2361
     * or if picture is already available in object.
2362
     *
2363
     * @param string $Dimension - Resizing happens proportional according to given dimension: height|width|any
2364
     * @param int    $Max       - Maximum size
2365
     *
2366
     * @return bool|null - true if success, false if failed
2367
     *
2368
     * @author Toon Keppens
2369
     */
2370
    private function resizePicture($Dimension, $Max)
2371
    {
2372
        // if the question has an ID
2373
        if (!$this->id) {
2374
            return false;
2375
        }
2376
2377
        $picturePath = $this->getHotSpotFolderInCourse().'/'.$this->getPictureFilename();
2378
2379
        // Get dimensions from current image.
2380
        $my_image = new Image($picturePath);
2381
2382
        $current_image_size = $my_image->get_image_size();
2383
        $current_width = $current_image_size['width'];
2384
        $current_height = $current_image_size['height'];
2385
2386
        if ($current_width < $Max && $current_height < $Max) {
2387
            return true;
2388
        } elseif ($current_height == '') {
2389
            return false;
2390
        }
2391
2392
        // Resize according to height.
2393
        if ($Dimension == "height") {
2394
            $resize_scale = $current_height / $Max;
2395
            $new_width = ceil($current_width / $resize_scale);
2396
        }
2397
2398
        // Resize according to width
2399
        if ($Dimension == "width") {
2400
            $new_width = $Max;
2401
        }
2402
2403
        // Resize according to height or width, both should not be larger than $Max after resizing.
2404
        if ($Dimension == "any") {
2405
            if ($current_height > $current_width || $current_height == $current_width) {
2406
                $resize_scale = $current_height / $Max;
2407
                $new_width = ceil($current_width / $resize_scale);
2408
            }
2409
            if ($current_height < $current_width) {
2410
                $new_width = $Max;
2411
            }
2412
        }
2413
2414
        $my_image->resize($new_width);
2415
        $result = $my_image->send_image($picturePath);
2416
2417
        if ($result) {
2418
            return true;
2419
        }
2420
2421
        return false;
2422
    }
2423
}
2424