/
exercise.class.php
executable file
·10878 lines (9924 loc) · 432 KB
/
exercise.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Entity\GradebookLink;
use Chamilo\CoreBundle\Entity\TrackEExerciseConfirmation;
use Chamilo\CoreBundle\Entity\TrackEHotspot;
use Chamilo\CourseBundle\Entity\CExerciseCategory;
use ChamiloSession as Session;
use Doctrine\DBAL\Types\Type;
/**
* Class Exercise.
*
* Allows to instantiate an object of type Exercise
*
* @todo use getters and setters correctly
*
* @author Olivier Brouckaert
* @author Julio Montoya Cleaning exercises
* Modified by Hubert Borderiou #294
*/
class Exercise
{
const PAGINATION_ITEMS_PER_PAGE = 20;
public $iId;
public $id;
public $name;
public $title;
public $exercise;
public $description;
public $sound;
public $type; //ALL_ON_ONE_PAGE or ONE_PER_PAGE
public $random;
public $random_answers;
public $active;
public $timeLimit;
public $attempts;
public $feedback_type;
public $end_time;
public $start_time;
public $questionList; // array with the list of this exercise's questions
/* including question list of the media */
public $questionListUncompressed;
public $results_disabled;
public $expired_time;
public $course;
public $course_id;
public $propagate_neg;
public $saveCorrectAnswers;
public $review_answers;
public $randomByCat;
public $text_when_finished;
public $display_category_name;
public $pass_percentage;
public $edit_exercise_in_lp = false;
public $is_gradebook_locked = false;
public $exercise_was_added_in_lp = false;
public $lpList = [];
public $force_edit_exercise_in_lp = false;
public $categories;
public $categories_grouping = true;
public $endButton = 0;
public $categoryWithQuestionList;
public $mediaList;
public $loadQuestionAJAX = false;
// Notification send to the teacher.
public $emailNotificationTemplate = null;
// Notification send to the student.
public $emailNotificationTemplateToUser = null;
public $countQuestions = 0;
public $fastEdition = false;
public $modelType = 1;
public $questionSelectionType = EX_Q_SELECTION_ORDERED;
public $hideQuestionTitle = 0;
public $scoreTypeModel = 0;
public $categoryMinusOne = true; // Shows the category -1: See BT#6540
public $globalCategoryId = null;
public $onSuccessMessage = null;
public $onFailedMessage = null;
public $emailAlert;
public $notifyUserByEmail = '';
public $sessionId = 0;
public $questionFeedbackEnabled = false;
public $questionTypeWithFeedback;
public $showPreviousButton;
public $notifications;
public $export = false;
public $autolaunch;
public $exerciseCategoryId;
public $pageResultConfiguration;
public $preventBackwards;
public $currentQuestion;
public $hideComment;
public $hideNoAnswer;
public $hideExpectedAnswer;
/**
* Constructor of the class.
*
* @param int $courseId
*
* @author Olivier Brouckaert
*/
public function __construct($courseId = 0)
{
$this->iId = 0;
$this->id = 0;
$this->exercise = '';
$this->description = '';
$this->sound = '';
$this->type = ALL_ON_ONE_PAGE;
$this->random = 0;
$this->random_answers = 0;
$this->active = 1;
$this->questionList = [];
$this->timeLimit = 0;
$this->end_time = '';
$this->start_time = '';
$this->results_disabled = 1;
$this->expired_time = 0;
$this->propagate_neg = 0;
$this->saveCorrectAnswers = 0;
$this->review_answers = false;
$this->randomByCat = 0;
$this->text_when_finished = '';
$this->display_category_name = 0;
$this->pass_percentage = 0;
$this->modelType = 1;
$this->questionSelectionType = EX_Q_SELECTION_ORDERED;
$this->endButton = 0;
$this->scoreTypeModel = 0;
$this->globalCategoryId = null;
$this->notifications = [];
$this->exerciseCategoryId = null;
$this->pageResultConfiguration;
$this->preventBackwards = 0;
$this->hideComment = false;
$this->hideNoAnswer = false;
$this->hideExpectedAnswer = false;
if (!empty($courseId)) {
$courseInfo = api_get_course_info_by_id($courseId);
} else {
$courseInfo = api_get_course_info();
}
$this->course_id = $courseInfo['real_id'];
$this->course = $courseInfo;
$this->sessionId = api_get_session_id();
// ALTER TABLE c_quiz_question ADD COLUMN feedback text;
$this->questionFeedbackEnabled = api_get_configuration_value('allow_quiz_question_feedback');
$this->showPreviousButton = true;
}
/**
* Reads exercise information from the data base.
*
* @author Olivier Brouckaert
*
* @param int $id - exercise Id
* @param bool $parseQuestionList
*
* @return bool - true if exercise exists, otherwise false
*/
public function read($id, $parseQuestionList = true)
{
$table = Database::get_course_table(TABLE_QUIZ_TEST);
$id = (int) $id;
if (empty($this->course_id)) {
return false;
}
$sql = "SELECT * FROM $table
WHERE c_id = ".$this->course_id." AND id = ".$id;
$result = Database::query($sql);
// if the exercise has been found
if ($object = Database::fetch_object($result)) {
$this->iId = $object->iid;
$this->id = $id;
$this->exercise = $object->title;
$this->name = $object->title;
$this->title = $object->title;
$this->description = $object->description;
$this->sound = $object->sound;
$this->type = $object->type;
if (empty($this->type)) {
$this->type = ONE_PER_PAGE;
}
$this->random = $object->random;
$this->random_answers = $object->random_answers;
$this->active = $object->active;
$this->results_disabled = $object->results_disabled;
$this->attempts = $object->max_attempt;
$this->feedback_type = $object->feedback_type;
$this->sessionId = $object->session_id;
$this->propagate_neg = $object->propagate_neg;
$this->saveCorrectAnswers = $object->save_correct_answers;
$this->randomByCat = $object->random_by_category;
$this->text_when_finished = $object->text_when_finished;
$this->display_category_name = $object->display_category_name;
$this->pass_percentage = $object->pass_percentage;
$this->is_gradebook_locked = api_resource_is_locked_by_gradebook($id, LINK_EXERCISE);
$this->review_answers = (isset($object->review_answers) && $object->review_answers == 1) ? true : false;
$this->globalCategoryId = isset($object->global_category_id) ? $object->global_category_id : null;
$this->questionSelectionType = isset($object->question_selection_type) ? (int) $object->question_selection_type : null;
$this->hideQuestionTitle = isset($object->hide_question_title) ? (int) $object->hide_question_title : 0;
$this->autolaunch = isset($object->autolaunch) ? (int) $object->autolaunch : 0;
$this->exerciseCategoryId = isset($object->exercise_category_id) ? (int) $object->exercise_category_id : null;
$this->preventBackwards = isset($object->prevent_backwards) ? (int) $object->prevent_backwards : 0;
$this->exercise_was_added_in_lp = false;
$this->lpList = [];
$this->notifications = [];
if (!empty($object->notifications)) {
$this->notifications = explode(',', $object->notifications);
}
if (!empty($object->page_result_configuration)) {
$this->pageResultConfiguration = $object->page_result_configuration;
}
if (isset($object->show_previous_button)) {
$this->showPreviousButton = $object->show_previous_button == 1 ? true : false;
}
$list = self::getLpListFromExercise($id, $this->course_id);
if (!empty($list)) {
$this->exercise_was_added_in_lp = true;
$this->lpList = $list;
}
$this->force_edit_exercise_in_lp = api_get_configuration_value('force_edit_exercise_in_lp');
$this->edit_exercise_in_lp = true;
if ($this->exercise_was_added_in_lp) {
$this->edit_exercise_in_lp = $this->force_edit_exercise_in_lp == true;
}
if (!empty($object->end_time)) {
$this->end_time = $object->end_time;
}
if (!empty($object->start_time)) {
$this->start_time = $object->start_time;
}
// Control time
$this->expired_time = $object->expired_time;
// Checking if question_order is correctly set
if ($parseQuestionList) {
$this->setQuestionList(true);
}
//overload questions list with recorded questions list
//load questions only for exercises of type 'one question per page'
//this is needed only is there is no questions
// @todo not sure were in the code this is used somebody mess with the exercise tool
// @todo don't know who add that config and why $_configuration['live_exercise_tracking']
/*global $_configuration, $questionList;
if ($this->type == ONE_PER_PAGE && $_SERVER['REQUEST_METHOD'] != 'POST'
&& defined('QUESTION_LIST_ALREADY_LOGGED') &&
isset($_configuration['live_exercise_tracking']) && $_configuration['live_exercise_tracking']
) {
$this->questionList = $questionList;
}*/
return true;
}
return false;
}
/**
* @return string
*/
public function getCutTitle()
{
$title = $this->getUnformattedTitle();
return cut($title, EXERCISE_MAX_NAME_SIZE);
}
/**
* returns the exercise ID.
*
* @author Olivier Brouckaert
*
* @return int - exercise ID
*/
public function selectId()
{
return $this->id;
}
/**
* returns the exercise title.
*
* @author Olivier Brouckaert
*
* @param bool $unformattedText Optional. Get the title without HTML tags
*
* @return string - exercise title
*/
public function selectTitle($unformattedText = false)
{
if ($unformattedText) {
return $this->getUnformattedTitle();
}
return $this->exercise;
}
/**
* returns the number of attempts setted.
*
* @return int - exercise attempts
*/
public function selectAttempts()
{
return $this->attempts;
}
/**
* Returns the number of FeedbackType
* 0: Feedback , 1: DirectFeedback, 2: NoFeedback.
*
* @return int - exercise attempts
*/
public function getFeedbackType()
{
return (int) $this->feedback_type;
}
/**
* returns the time limit.
*
* @return int
*/
public function selectTimeLimit()
{
return $this->timeLimit;
}
/**
* returns the exercise description.
*
* @author Olivier Brouckaert
*
* @return string - exercise description
*/
public function selectDescription()
{
return $this->description;
}
/**
* returns the exercise sound file.
*
* @author Olivier Brouckaert
*
* @return string - exercise description
*/
public function selectSound()
{
return $this->sound;
}
/**
* returns the exercise type.
*
* @author Olivier Brouckaert
*
* @return int - exercise type
*/
public function selectType()
{
return $this->type;
}
/**
* @return int
*/
public function getModelType()
{
return $this->modelType;
}
/**
* @return int
*/
public function selectEndButton()
{
return $this->endButton;
}
/**
* @author hubert borderiou 30-11-11
*
* @return int : do we display the question category name for students
*/
public function selectDisplayCategoryName()
{
return $this->display_category_name;
}
/**
* @return int
*/
public function selectPassPercentage()
{
return $this->pass_percentage;
}
/**
* Modify object to update the switch display_category_name.
*
* @author hubert borderiou 30-11-11
*
* @param int $value is an integer 0 or 1
*/
public function updateDisplayCategoryName($value)
{
$this->display_category_name = $value;
}
/**
* @author hubert borderiou 28-11-11
*
* @return string html text : the text to display ay the end of the test
*/
public function getTextWhenFinished()
{
return $this->text_when_finished;
}
/**
* @param string $text
*
* @author hubert borderiou 28-11-11
*/
public function updateTextWhenFinished($text)
{
$this->text_when_finished = $text;
}
/**
* return 1 or 2 if randomByCat.
*
* @author hubert borderiou
*
* @return int - quiz random by category
*/
public function getRandomByCategory()
{
return $this->randomByCat;
}
/**
* return 0 if no random by cat
* return 1 if random by cat, categories shuffled
* return 2 if random by cat, categories sorted by alphabetic order.
*
* @author hubert borderiou
*
* @return int - quiz random by category
*/
public function isRandomByCat()
{
$res = EXERCISE_CATEGORY_RANDOM_DISABLED;
if ($this->randomByCat == EXERCISE_CATEGORY_RANDOM_SHUFFLED) {
$res = EXERCISE_CATEGORY_RANDOM_SHUFFLED;
} elseif ($this->randomByCat == EXERCISE_CATEGORY_RANDOM_ORDERED) {
$res = EXERCISE_CATEGORY_RANDOM_ORDERED;
}
return $res;
}
/**
* return nothing
* update randomByCat value for object.
*
* @param int $random
*
* @author hubert borderiou
*/
public function updateRandomByCat($random)
{
$this->randomByCat = EXERCISE_CATEGORY_RANDOM_DISABLED;
if (in_array(
$random,
[
EXERCISE_CATEGORY_RANDOM_SHUFFLED,
EXERCISE_CATEGORY_RANDOM_ORDERED,
EXERCISE_CATEGORY_RANDOM_DISABLED,
]
)) {
$this->randomByCat = $random;
}
}
/**
* Tells if questions are selected randomly, and if so returns the draws.
*
* @author Carlos Vargas
*
* @return int - results disabled exercise
*/
public function selectResultsDisabled()
{
return $this->results_disabled;
}
/**
* tells if questions are selected randomly, and if so returns the draws.
*
* @author Olivier Brouckaert
*
* @return bool
*/
public function isRandom()
{
$isRandom = false;
// "-1" means all questions will be random
if ($this->random > 0 || $this->random == -1) {
$isRandom = true;
}
return $isRandom;
}
/**
* returns random answers status.
*
* @author Juan Carlos Rana
*/
public function getRandomAnswers()
{
return $this->random_answers;
}
/**
* Same as isRandom() but has a name applied to values different than 0 or 1.
*
* @return int
*/
public function getShuffle()
{
return $this->random;
}
/**
* returns the exercise status (1 = enabled ; 0 = disabled).
*
* @author Olivier Brouckaert
*
* @return int - 1 if enabled, otherwise 0
*/
public function selectStatus()
{
return $this->active;
}
/**
* If false the question list will be managed as always if true
* the question will be filtered
* depending of the exercise settings (table c_quiz_rel_category).
*
* @param bool $status active or inactive grouping
*/
public function setCategoriesGrouping($status)
{
$this->categories_grouping = (bool) $status;
}
/**
* @return int
*/
public function getHideQuestionTitle()
{
return $this->hideQuestionTitle;
}
/**
* @param $value
*/
public function setHideQuestionTitle($value)
{
$this->hideQuestionTitle = (int) $value;
}
/**
* @return int
*/
public function getScoreTypeModel()
{
return $this->scoreTypeModel;
}
/**
* @param int $value
*/
public function setScoreTypeModel($value)
{
$this->scoreTypeModel = (int) $value;
}
/**
* @return int
*/
public function getGlobalCategoryId()
{
return $this->globalCategoryId;
}
/**
* @param int $value
*/
public function setGlobalCategoryId($value)
{
if (is_array($value) && isset($value[0])) {
$value = $value[0];
}
$this->globalCategoryId = (int) $value;
}
/**
* @param int $start
* @param int $limit
* @param int $sidx
* @param string $sord
* @param array $whereCondition
* @param array $extraFields
*
* @return array
*/
public function getQuestionListPagination(
$start,
$limit,
$sidx,
$sord,
$whereCondition = [],
$extraFields = []
) {
if (!empty($this->id)) {
$category_list = TestCategory::getListOfCategoriesNameForTest(
$this->id,
false
);
$TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
$TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
$sql = "SELECT q.iid
FROM $TBL_EXERCICE_QUESTION e
INNER JOIN $TBL_QUESTIONS q
ON (e.question_id = q.id AND e.c_id = ".$this->course_id." )
WHERE e.exercice_id = '".$this->id."' ";
$orderCondition = ' ORDER BY question_order ';
if (!empty($sidx) && !empty($sord)) {
if ('question' === $sidx) {
if (in_array(strtolower($sord), ['desc', 'asc'])) {
$orderCondition = " ORDER BY q.$sidx $sord";
}
}
}
$sql .= $orderCondition;
$limitCondition = null;
if (isset($start) && isset($limit)) {
$start = (int) $start;
$limit = (int) $limit;
$limitCondition = " LIMIT $start, $limit";
}
$sql .= $limitCondition;
$result = Database::query($sql);
$questions = [];
if (Database::num_rows($result)) {
if (!empty($extraFields)) {
$extraFieldValue = new ExtraFieldValue('question');
}
while ($question = Database::fetch_array($result, 'ASSOC')) {
/** @var Question $objQuestionTmp */
$objQuestionTmp = Question::read($question['iid']);
$category_labels = TestCategory::return_category_labels(
$objQuestionTmp->category_list,
$category_list
);
if (empty($category_labels)) {
$category_labels = '-';
}
// Question type
$typeImg = $objQuestionTmp->getTypePicture();
$typeExpl = $objQuestionTmp->getExplanation();
$question_media = null;
if (!empty($objQuestionTmp->parent_id)) {
$objQuestionMedia = Question::read($objQuestionTmp->parent_id);
$question_media = Question::getMediaLabel($objQuestionMedia->question);
}
$questionType = Display::tag(
'div',
Display::return_icon($typeImg, $typeExpl, [], ICON_SIZE_MEDIUM).$question_media
);
$question = [
'id' => $question['iid'],
'question' => $objQuestionTmp->selectTitle(),
'type' => $questionType,
'category' => Display::tag(
'div',
'<a href="#" style="padding:0px; margin:0px;">'.$category_labels.'</a>'
),
'score' => $objQuestionTmp->selectWeighting(),
'level' => $objQuestionTmp->level,
];
if (!empty($extraFields)) {
foreach ($extraFields as $extraField) {
$value = $extraFieldValue->get_values_by_handler_and_field_id(
$question['id'],
$extraField['id']
);
$stringValue = null;
if ($value) {
$stringValue = $value['field_value'];
}
$question[$extraField['field_variable']] = $stringValue;
}
}
$questions[] = $question;
}
}
return $questions;
}
}
/**
* Get question count per exercise from DB (any special treatment).
*
* @return int
*/
public function getQuestionCount()
{
$TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
$TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
$sql = "SELECT count(q.id) as count
FROM $TBL_EXERCICE_QUESTION e
INNER JOIN $TBL_QUESTIONS q
ON (e.question_id = q.id AND e.c_id = q.c_id)
WHERE
e.c_id = {$this->course_id} AND
e.exercice_id = ".$this->id;
$result = Database::query($sql);
$count = 0;
if (Database::num_rows($result)) {
$row = Database::fetch_array($result);
$count = (int) $row['count'];
}
return $count;
}
/**
* @return array
*/
public function getQuestionOrderedListByName()
{
if (empty($this->course_id) || empty($this->id)) {
return [];
}
$exerciseQuestionTable = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
$questionTable = Database::get_course_table(TABLE_QUIZ_QUESTION);
// Getting question list from the order (question list drag n drop interface ).
$sql = "SELECT e.question_id
FROM $exerciseQuestionTable e
INNER JOIN $questionTable q
ON (e.question_id= q.id AND e.c_id = q.c_id)
WHERE
e.c_id = {$this->course_id} AND
e.exercice_id = '".$this->id."'
ORDER BY q.question";
$result = Database::query($sql);
$list = [];
if (Database::num_rows($result)) {
$list = Database::store_result($result, 'ASSOC');
}
return $list;
}
/**
* Selecting question list depending in the exercise-category
* relationship (category table in exercise settings).
*
* @param array $question_list
* @param int $questionSelectionType
*
* @return array
*/
public function getQuestionListWithCategoryListFilteredByCategorySettings(
$question_list,
$questionSelectionType
) {
$result = [
'question_list' => [],
'category_with_questions_list' => [],
];
// Order/random categories
$cat = new TestCategory();
// Setting category order.
switch ($questionSelectionType) {
case EX_Q_SELECTION_ORDERED: // 1
case EX_Q_SELECTION_RANDOM: // 2
// This options are not allowed here.
break;
case EX_Q_SELECTION_CATEGORIES_ORDERED_QUESTIONS_ORDERED: // 3
$categoriesAddedInExercise = $cat->getCategoryExerciseTree(
$this,
$this->course['real_id'],
'title ASC',
false,
true
);
$questions_by_category = TestCategory::getQuestionsByCat(
$this->id,
$question_list,
$categoriesAddedInExercise
);
$question_list = $this->pickQuestionsPerCategory(
$categoriesAddedInExercise,
$question_list,
$questions_by_category,
true,
false
);
break;
case EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_ORDERED: // 4
case EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_ORDERED_NO_GROUPED: // 7
$categoriesAddedInExercise = $cat->getCategoryExerciseTree(
$this,
$this->course['real_id'],
null,
true,
true
);
$questions_by_category = TestCategory::getQuestionsByCat(
$this->id,
$question_list,
$categoriesAddedInExercise
);
$question_list = $this->pickQuestionsPerCategory(
$categoriesAddedInExercise,
$question_list,
$questions_by_category,
true,
false
);
break;
case EX_Q_SELECTION_CATEGORIES_ORDERED_QUESTIONS_RANDOM: // 5
$categoriesAddedInExercise = $cat->getCategoryExerciseTree(
$this,
$this->course['real_id'],
'title ASC',
false,
true
);
$questions_by_category = TestCategory::getQuestionsByCat(
$this->id,
$question_list,
$categoriesAddedInExercise
);
$questionsByCategoryMandatory = [];
if (EX_Q_SELECTION_CATEGORIES_ORDERED_QUESTIONS_RANDOM == $this->getQuestionSelectionType() &&
api_get_configuration_value('allow_mandatory_question_in_category')
) {
$questionsByCategoryMandatory = TestCategory::getQuestionsByCat(
$this->id,
$question_list,
$categoriesAddedInExercise,
true
);
}
$question_list = $this->pickQuestionsPerCategory(
$categoriesAddedInExercise,
$question_list,
$questions_by_category,
true,
true,
$questionsByCategoryMandatory
);
break;
case EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_RANDOM: // 6
case EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_RANDOM_NO_GROUPED:
$categoriesAddedInExercise = $cat->getCategoryExerciseTree(
$this,
$this->course['real_id'],
null,
true,
true
);
$questions_by_category = TestCategory::getQuestionsByCat(
$this->id,
$question_list,
$categoriesAddedInExercise
);
$question_list = $this->pickQuestionsPerCategory(
$categoriesAddedInExercise,
$question_list,
$questions_by_category,
true,
true
);
break;
case EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_ORDERED_NO_GROUPED: // 7
break;
case EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_RANDOM_NO_GROUPED: // 8
break;
case EX_Q_SELECTION_CATEGORIES_ORDERED_BY_PARENT_QUESTIONS_ORDERED: // 9
$categoriesAddedInExercise = $cat->getCategoryExerciseTree(
$this,
$this->course['real_id'],
'root ASC, lft ASC',
false,
true
);
$questions_by_category = TestCategory::getQuestionsByCat(
$this->id,
$question_list,
$categoriesAddedInExercise
);
$question_list = $this->pickQuestionsPerCategory(
$categoriesAddedInExercise,
$question_list,
$questions_by_category,
true,
false
);
break;
case EX_Q_SELECTION_CATEGORIES_ORDERED_BY_PARENT_QUESTIONS_RANDOM: // 10
$categoriesAddedInExercise = $cat->getCategoryExerciseTree(
$this,
$this->course['real_id'],
'root, lft ASC',
false,
true
);
$questions_by_category = TestCategory::getQuestionsByCat(
$this->id,
$question_list,
$categoriesAddedInExercise
);
$question_list = $this->pickQuestionsPerCategory(
$categoriesAddedInExercise,
$question_list,
$questions_by_category,
true,
true
);
break;
}
$result['question_list'] = isset($question_list) ? $question_list : [];
$result['category_with_questions_list'] = isset($questions_by_category) ? $questions_by_category : [];
$parentsLoaded = [];
// Adding category info in the category list with question list:
if (!empty($questions_by_category)) {
$newCategoryList = [];
$em = Database::getManager();
$repo = $em->getRepository('ChamiloCourseBundle:CQuizCategory');
foreach ($questions_by_category as $categoryId => $questionList) {
$cat = new TestCategory();
$cat = $cat->getCategory($categoryId);
if ($cat) {
$cat = (array) $cat;
$cat['iid'] = $cat['id'];
}
$categoryParentInfo = null;
// Parent is not set no loop here
if (isset($cat['parent_id']) && !empty($cat['parent_id'])) {
/** @var \Chamilo\CourseBundle\Entity\CQuizCategory $categoryEntity */
if (!isset($parentsLoaded[$cat['parent_id']])) {