Passed
Push — master ( 3ef090...0b35a3 )
by Julito
12:21
created

CQuiz::getRandom()   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 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CourseBundle\Entity;
6
7
use Chamilo\CoreBundle\Entity\AbstractResource;
8
use Chamilo\CoreBundle\Entity\Course;
9
use Chamilo\CoreBundle\Entity\ResourceInterface;
10
use Chamilo\CourseBundle\Traits\ShowCourseResourcesInSessionTrait;
11
use Doctrine\Common\Collections\ArrayCollection;
12
use Doctrine\ORM\Event\LifecycleEventArgs;
13
use Doctrine\ORM\Mapping as ORM;
14
use Symfony\Component\Validator\Constraints as Assert;
15
16
/**
17
 * CQuiz.
18
 *
19
 * @ORM\Table(
20
 *  name="c_quiz",
21
 *  indexes={
22
 *      @ORM\Index(name="course", columns={"c_id"}),
23
 *      @ORM\Index(name="session_id", columns={"session_id"})
24
 *  }
25
 * )
26
 * @ORM\Entity
27
 */
28
class CQuiz extends AbstractResource implements ResourceInterface
29
{
30
    use ShowCourseResourcesInSessionTrait;
31
32
    /**
33
     * @var int
34
     *
35
     * @ORM\Column(name="iid", type="integer")
36
     * @ORM\Id
37
     * @ORM\GeneratedValue
38
     */
39
    protected $iid;
40
41
    /**
42
     * @var int
43
     *
44
     * @ORM\Column(name="c_id", type="integer")
45
     */
46
    protected $cId;
47
48
    /**
49
     * @var string
50
     * @Assert\NotBlank()
51
     * @ORM\Column(name="title", type="text", nullable=false)
52
     */
53
    protected $title;
54
55
    /**
56
     * @var string
57
     *
58
     * @ORM\Column(name="description", type="text", nullable=true)
59
     */
60
    protected $description;
61
62
    /**
63
     * @var string
64
     *
65
     * @ORM\Column(name="sound", type="string", length=255, nullable=true)
66
     */
67
    protected $sound;
68
69
    /**
70
     * @var int
71
     *
72
     * @ORM\Column(name="type", type="integer", nullable=false)
73
     */
74
    protected $type;
75
76
    /**
77
     * @var int
78
     *
79
     * @ORM\Column(name="random", type="integer", nullable=false)
80
     */
81
    protected $random;
82
83
    /**
84
     * @var bool
85
     *
86
     * @ORM\Column(name="random_answers", type="boolean", nullable=false)
87
     */
88
    protected $randomAnswers;
89
90
    /**
91
     * @var bool
92
     *
93
     * @ORM\Column(name="active", type="boolean", nullable=false)
94
     */
95
    protected $active;
96
97
    /**
98
     * @var int
99
     *
100
     * @ORM\Column(name="results_disabled", type="integer", nullable=false)
101
     */
102
    protected $resultsDisabled;
103
104
    /**
105
     * @var string
106
     *
107
     * @ORM\Column(name="access_condition", type="text", nullable=true)
108
     */
109
    protected $accessCondition;
110
111
    /**
112
     * @var int
113
     *
114
     * @ORM\Column(name="max_attempt", type="integer", nullable=false)
115
     */
116
    protected $maxAttempt;
117
118
    /**
119
     * @var \DateTime
120
     *
121
     * @ORM\Column(name="start_time", type="datetime", nullable=true)
122
     */
123
    protected $startTime;
124
125
    /**
126
     * @var \DateTime
127
     *
128
     * @ORM\Column(name="end_time", type="datetime", nullable=true)
129
     */
130
    protected $endTime;
131
132
    /**
133
     * @var int
134
     *
135
     * @ORM\Column(name="feedback_type", type="integer", nullable=false)
136
     */
137
    protected $feedbackType;
138
139
    /**
140
     * @var int
141
     *
142
     * @ORM\Column(name="expired_time", type="integer", nullable=false)
143
     */
144
    protected $expiredTime;
145
146
    /**
147
     * @var int
148
     *
149
     * @ORM\Column(name="session_id", type="integer", nullable=true)
150
     */
151
    protected $sessionId;
152
153
    /**
154
     * @var int
155
     *
156
     * @ORM\Column(name="propagate_neg", type="integer", nullable=false)
157
     */
158
    protected $propagateNeg;
159
160
    /**
161
     * @var int
162
     *
163
     * @ORm\Column(name="save_correct_answers", type="integer", nullable=true)
164
     */
165
    protected $saveCorrectAnswers;
166
167
    /**
168
     * @var int
169
     *
170
     * @ORM\Column(name="review_answers", type="integer", nullable=false)
171
     */
172
    protected $reviewAnswers;
173
174
    /**
175
     * @var int
176
     *
177
     * @ORM\Column(name="random_by_category", type="integer", nullable=false)
178
     */
179
    protected $randomByCategory;
180
181
    /**
182
     * @var string
183
     *
184
     * @ORM\Column(name="text_when_finished", type="text", nullable=true)
185
     */
186
    protected $textWhenFinished;
187
188
    /**
189
     * @var int
190
     *
191
     * @ORM\Column(name="display_category_name", type="integer", nullable=false)
192
     */
193
    protected $displayCategoryName;
194
195
    /**
196
     * @var int
197
     *
198
     * @ORM\Column(name="pass_percentage", type="integer", nullable=true)
199
     */
200
    protected $passPercentage;
201
202
    /**
203
     * @var int
204
     *
205
     * @ORM\Column(name="prevent_backwards", type="integer", nullable=false, options={"default":0})
206
     */
207
    protected $preventBackwards;
208
209
    /**
210
     * @var int
211
     *
212
     * @ORM\Column(name="question_selection_type", type="integer", nullable=true)
213
     */
214
    protected $questionSelectionType;
215
216
    /**
217
     * @var bool
218
     *
219
     * @ORM\Column(name="hide_question_title", type="boolean", nullable=true)
220
     */
221
    protected $hideQuestionTitle;
222
223
    /**
224
     * @var CExerciseCategory
225
     *
226
     * @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CExerciseCategory", cascade={"persist"})
227
     * @ORM\JoinColumn(name="exercise_category_id", referencedColumnName="id", onDelete="SET NULL")
228
     */
229
    protected $exerciseCategory;
230
231
    /**
232
     * @var bool
233
     *
234
     * @ORM\Column(name="show_previous_button", type="boolean", nullable=true, options={"default":1})
235
     */
236
    protected $showPreviousButton;
237
238
    /**
239
     * @var string
240
     *
241
     * @ORM\Column(name="notifications", type="string", length=255, nullable=true)
242
     */
243
    protected $notifications;
244
245
    /**
246
     * @var bool
247
     *
248
     * @ORM\Column(name="autolaunch", type="boolean", nullable=true, options={"default":0})
249
     */
250
    protected $autoLaunch;
251
252
    /**
253
     * @var int
254
     *
255
     * @ORM\Column(name="page_result_configuration", type="array", nullable=true)
256
     */
257
    protected $pageResultConfiguration;
258
259
    /**
260
     * @var CQuizRelQuestion[]|ArrayCollection
261
     *
262
     * @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CQuizRelQuestion", mappedBy="quiz"))
263
     */
264
    protected $quizRelQuestions;
265
266
    /**
267
     * CQuiz constructor.
268
     */
269
    public function __construct()
270
    {
271
        $this->hideQuestionTitle = false;
272
        $this->type = ONE_PER_PAGE;
273
        $this->showPreviousButton = true;
274
        $this->notifications = '';
275
        $this->autoLaunch = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $autoLaunch was declared of type boolean, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
276
        $this->preventBackwards = 0;
277
        $this->random = 0;
278
        $this->randomAnswers = false;
279
        $this->active = true;
280
        $this->resultsDisabled = 0;
281
        $this->maxAttempt = 1;
282
        $this->feedbackType = 0;
283
        $this->expiredTime = 0;
284
        $this->propagateNeg = 0;
285
        $this->saveCorrectAnswers = false;
0 ignored issues
show
Documentation Bug introduced by
The property $saveCorrectAnswers was declared of type integer, but false is of type false. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
286
        $this->reviewAnswers = 0;
287
        $this->randomByCategory = 0;
288
        $this->displayCategoryName = 0;
289
        $this->quizRelQuestions = new ArrayCollection();
290
    }
291
292
    public function __toString(): string
293
    {
294
        return $this->getTitle();
295
    }
296
297
    /**
298
     * Set title.
299
     *
300
     * @param string $title
301
     *
302
     * @return CQuiz
303
     */
304
    public function setTitle($title)
305
    {
306
        $this->title = $title;
307
308
        return $this;
309
    }
310
311
    /**
312
     * Get title.
313
     *
314
     * @return string
315
     */
316
    public function getTitle()
317
    {
318
        return (string) $this->title;
319
    }
320
321
    /**
322
     * Set description.
323
     *
324
     * @param string $description
325
     *
326
     * @return CQuiz
327
     */
328
    public function setDescription($description)
329
    {
330
        $this->description = $description;
331
332
        return $this;
333
    }
334
335
    /**
336
     * Get description.
337
     *
338
     * @return string
339
     */
340
    public function getDescription()
341
    {
342
        return $this->description;
343
    }
344
345
    /**
346
     * Set sound.
347
     *
348
     * @param string $sound
349
     *
350
     * @return CQuiz
351
     */
352
    public function setSound($sound)
353
    {
354
        $this->sound = $sound;
355
356
        return $this;
357
    }
358
359
    /**
360
     * Get sound.
361
     *
362
     * @return string
363
     */
364
    public function getSound()
365
    {
366
        return $this->sound;
367
    }
368
369
    /**
370
     * Set type.
371
     */
372
    public function setType(int $type): self
373
    {
374
        $this->type = $type;
375
376
        return $this;
377
    }
378
379
    /**
380
     * Get type.
381
     */
382
    public function getType(): int
383
    {
384
        return $this->type;
385
    }
386
387
    /**
388
     * Set random.
389
     *
390
     * @param int $random
391
     *
392
     * @return CQuiz
393
     */
394
    public function setRandom($random)
395
    {
396
        $this->random = $random;
397
398
        return $this;
399
    }
400
401
    /**
402
     * Get random.
403
     *
404
     * @return int
405
     */
406
    public function getRandom()
407
    {
408
        return $this->random;
409
    }
410
411
    /**
412
     * Set randomAnswers.
413
     *
414
     * @param bool $randomAnswers
415
     *
416
     * @return CQuiz
417
     */
418
    public function setRandomAnswers($randomAnswers)
419
    {
420
        $this->randomAnswers = $randomAnswers;
421
422
        return $this;
423
    }
424
425
    /**
426
     * Get randomAnswers.
427
     *
428
     * @return bool
429
     */
430
    public function getRandomAnswers()
431
    {
432
        return $this->randomAnswers;
433
    }
434
435
    /**
436
     * Set active.
437
     *
438
     * @param bool $active
439
     *
440
     * @return CQuiz
441
     */
442
    public function setActive($active)
443
    {
444
        $this->active = $active;
445
446
        return $this;
447
    }
448
449
    /**
450
     * Get active.
451
     *
452
     * @return bool
453
     */
454
    public function getActive()
455
    {
456
        return $this->active;
457
    }
458
459
    /**
460
     * Set resultsDisabled.
461
     *
462
     * @param int $resultsDisabled
463
     *
464
     * @return CQuiz
465
     */
466
    public function setResultsDisabled($resultsDisabled)
467
    {
468
        $this->resultsDisabled = $resultsDisabled;
469
470
        return $this;
471
    }
472
473
    /**
474
     * Get resultsDisabled.
475
     *
476
     * @return int
477
     */
478
    public function getResultsDisabled()
479
    {
480
        return $this->resultsDisabled;
481
    }
482
483
    /**
484
     * Set accessCondition.
485
     *
486
     * @param string $accessCondition
487
     *
488
     * @return CQuiz
489
     */
490
    public function setAccessCondition($accessCondition)
491
    {
492
        $this->accessCondition = $accessCondition;
493
494
        return $this;
495
    }
496
497
    /**
498
     * Get accessCondition.
499
     *
500
     * @return string
501
     */
502
    public function getAccessCondition()
503
    {
504
        return $this->accessCondition;
505
    }
506
507
    /**
508
     * Set maxAttempt.
509
     *
510
     * @param int $maxAttempt
511
     *
512
     * @return CQuiz
513
     */
514
    public function setMaxAttempt($maxAttempt)
515
    {
516
        $this->maxAttempt = $maxAttempt;
517
518
        return $this;
519
    }
520
521
    /**
522
     * Get maxAttempt.
523
     *
524
     * @return int
525
     */
526
    public function getMaxAttempt()
527
    {
528
        return $this->maxAttempt;
529
    }
530
531
    /**
532
     * Set startTime.
533
     *
534
     * @param \DateTime $startTime
535
     *
536
     * @return CQuiz
537
     */
538
    public function setStartTime($startTime)
539
    {
540
        $this->startTime = $startTime;
541
542
        return $this;
543
    }
544
545
    /**
546
     * Get startTime.
547
     *
548
     * @return \DateTime
549
     */
550
    public function getStartTime()
551
    {
552
        return $this->startTime;
553
    }
554
555
    /**
556
     * Set endTime.
557
     *
558
     * @param \DateTime $endTime
559
     *
560
     * @return CQuiz
561
     */
562
    public function setEndTime($endTime)
563
    {
564
        $this->endTime = $endTime;
565
566
        return $this;
567
    }
568
569
    /**
570
     * Get endTime.
571
     *
572
     * @return \DateTime
573
     */
574
    public function getEndTime()
575
    {
576
        return $this->endTime;
577
    }
578
579
    /**
580
     * Set feedbackType.
581
     *
582
     * @param int $feedbackType
583
     *
584
     * @return CQuiz
585
     */
586
    public function setFeedbackType($feedbackType)
587
    {
588
        $this->feedbackType = $feedbackType;
589
590
        return $this;
591
    }
592
593
    /**
594
     * Get feedbackType.
595
     *
596
     * @return int
597
     */
598
    public function getFeedbackType()
599
    {
600
        return $this->feedbackType;
601
    }
602
603
    /**
604
     * Set expiredTime.
605
     *
606
     * @param int $expiredTime
607
     *
608
     * @return CQuiz
609
     */
610
    public function setExpiredTime($expiredTime)
611
    {
612
        $this->expiredTime = $expiredTime;
613
614
        return $this;
615
    }
616
617
    /**
618
     * Get expiredTime.
619
     *
620
     * @return int
621
     */
622
    public function getExpiredTime()
623
    {
624
        return $this->expiredTime;
625
    }
626
627
    /**
628
     * Set sessionId.
629
     *
630
     * @param int $sessionId
631
     *
632
     * @return CQuiz
633
     */
634
    public function setSessionId($sessionId)
635
    {
636
        $this->sessionId = $sessionId;
637
638
        return $this;
639
    }
640
641
    /**
642
     * Get sessionId.
643
     *
644
     * @return int
645
     */
646
    public function getSessionId()
647
    {
648
        return $this->sessionId;
649
    }
650
651
    /**
652
     * Set propagateNeg.
653
     *
654
     * @param int $propagateNeg
655
     *
656
     * @return CQuiz
657
     */
658
    public function setPropagateNeg($propagateNeg)
659
    {
660
        $this->propagateNeg = $propagateNeg;
661
662
        return $this;
663
    }
664
665
    /**
666
     * Get propagateNeg.
667
     *
668
     * @return int
669
     */
670
    public function getPropagateNeg()
671
    {
672
        return $this->propagateNeg;
673
    }
674
675
    /**
676
     * @param int $saveCorrectAnswers
677
     *
678
     * @return CQuiz
679
     */
680
    public function setSaveCorrectAnswers($saveCorrectAnswers)
681
    {
682
        $this->saveCorrectAnswers = $saveCorrectAnswers;
683
684
        return $this;
685
    }
686
687
    /**
688
     * @return int
689
     */
690
    public function getSaveCorrectAnswers()
691
    {
692
        return $this->saveCorrectAnswers;
693
    }
694
695
    /**
696
     * Set reviewAnswers.
697
     *
698
     * @param int $reviewAnswers
699
     *
700
     * @return CQuiz
701
     */
702
    public function setReviewAnswers($reviewAnswers)
703
    {
704
        $this->reviewAnswers = $reviewAnswers;
705
706
        return $this;
707
    }
708
709
    /**
710
     * Get reviewAnswers.
711
     *
712
     * @return int
713
     */
714
    public function getReviewAnswers()
715
    {
716
        return $this->reviewAnswers;
717
    }
718
719
    /**
720
     * Set randomByCategory.
721
     *
722
     * @param int $randomByCategory
723
     *
724
     * @return CQuiz
725
     */
726
    public function setRandomByCategory($randomByCategory)
727
    {
728
        $this->randomByCategory = $randomByCategory;
729
730
        return $this;
731
    }
732
733
    /**
734
     * Get randomByCategory.
735
     *
736
     * @return int
737
     */
738
    public function getRandomByCategory()
739
    {
740
        return $this->randomByCategory;
741
    }
742
743
    /**
744
     * Set textWhenFinished.
745
     *
746
     * @param string $textWhenFinished
747
     *
748
     * @return CQuiz
749
     */
750
    public function setTextWhenFinished($textWhenFinished)
751
    {
752
        $this->textWhenFinished = $textWhenFinished;
753
754
        return $this;
755
    }
756
757
    /**
758
     * Get textWhenFinished.
759
     *
760
     * @return string
761
     */
762
    public function getTextWhenFinished()
763
    {
764
        return $this->textWhenFinished;
765
    }
766
767
    /**
768
     * Set displayCategoryName.
769
     *
770
     * @param int $displayCategoryName
771
     *
772
     * @return CQuiz
773
     */
774
    public function setDisplayCategoryName($displayCategoryName)
775
    {
776
        $this->displayCategoryName = $displayCategoryName;
777
778
        return $this;
779
    }
780
781
    /**
782
     * Get displayCategoryName.
783
     *
784
     * @return int
785
     */
786
    public function getDisplayCategoryName()
787
    {
788
        return $this->displayCategoryName;
789
    }
790
791
    /**
792
     * Set passPercentage.
793
     *
794
     * @param int $passPercentage
795
     *
796
     * @return CQuiz
797
     */
798
    public function setPassPercentage($passPercentage)
799
    {
800
        $this->passPercentage = $passPercentage;
801
802
        return $this;
803
    }
804
805
    /**
806
     * Get passPercentage.
807
     *
808
     * @return int
809
     */
810
    public function getPassPercentage()
811
    {
812
        return $this->passPercentage;
813
    }
814
815
    /**
816
     * Set cId.
817
     *
818
     * @param int $cId
819
     *
820
     * @return CQuiz
821
     */
822
    public function setCId($cId)
823
    {
824
        $this->cId = $cId;
825
826
        return $this;
827
    }
828
829
    /**
830
     * Get cId.
831
     *
832
     * @return int
833
     */
834
    public function getCId()
835
    {
836
        return $this->cId;
837
    }
838
839
    /**
840
     * @return CExerciseCategory
841
     */
842
    public function getExerciseCategory(): ?CExerciseCategory
843
    {
844
        return $this->exerciseCategory;
845
    }
846
847
    public function setExerciseCategory(CExerciseCategory $exerciseCategory): self
848
    {
849
        $this->exerciseCategory = $exerciseCategory;
850
851
        return $this;
852
    }
853
854
    /**
855
     * @return int
856
     */
857
    public function getQuestionSelectionType()
858
    {
859
        return $this->questionSelectionType;
860
    }
861
862
    /**
863
     * @param int $questionSelectionType
864
     *
865
     * @return CQuiz
866
     */
867
    public function setQuestionSelectionType($questionSelectionType)
868
    {
869
        $this->questionSelectionType = $questionSelectionType;
870
871
        return $this;
872
    }
873
874
    /**
875
     * @return bool
876
     */
877
    public function isHideQuestionTitle()
878
    {
879
        return $this->hideQuestionTitle;
880
    }
881
882
    /**
883
     * @param bool $hideQuestionTitle
884
     *
885
     * @return CQuiz
886
     */
887
    public function setHideQuestionTitle($hideQuestionTitle)
888
    {
889
        $this->hideQuestionTitle = $hideQuestionTitle;
890
891
        return $this;
892
    }
893
894
    public function isShowPreviousButton(): bool
895
    {
896
        return $this->showPreviousButton;
897
    }
898
899
    public function setShowPreviousButton(bool $showPreviousButton): self
900
    {
901
        $this->showPreviousButton = $showPreviousButton;
902
903
        return $this;
904
    }
905
906
    public function getNotifications(): string
907
    {
908
        return $this->notifications;
909
    }
910
911
    public function setNotifications(string $notifications): self
912
    {
913
        $this->notifications = $notifications;
914
915
        return $this;
916
    }
917
918
    /**
919
     * @return int
920
     */
921
    public function getIid()
922
    {
923
        return $this->iid;
924
    }
925
926
    public function getPreventBackwards(): int
927
    {
928
        return $this->preventBackwards;
929
    }
930
931
    public function setPreventBackwards(int $preventBackwards): self
932
    {
933
        $this->preventBackwards = $preventBackwards;
934
935
        return $this;
936
    }
937
938
    public function isAutoLaunch(): bool
939
    {
940
        return $this->autoLaunch;
941
    }
942
943
    public function setAutoLaunch(bool $autoLaunch): self
944
    {
945
        $this->autoLaunch = $autoLaunch;
946
947
        return $this;
948
    }
949
950
    public function getPageResultConfiguration(): int
951
    {
952
        return $this->pageResultConfiguration;
953
    }
954
955
    public function setPageResultConfiguration(int $pageResultConfiguration): self
956
    {
957
        $this->pageResultConfiguration = $pageResultConfiguration;
958
959
        return $this;
960
    }
961
962
    /**
963
     * @ORM\PostPersist()
964
     */
965
    public function postPersist(LifecycleEventArgs $args)
966
    {
967
    }
968
969
    /**
970
     * Returns the sum of question's ponderation
971
     */
972
    public function getMaxScore(): int
973
    {
974
        $maxScore = 0;
975
        foreach ($this->quizRelQuestions as $relQuestion) {
976
            $maxScore += $relQuestion->getQuestion()->getPonderation();
977
        }
978
979
        return $maxScore;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $maxScore could return the type double which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
980
    }
981
982
    /**
983
     * Resource identifier.
984
     */
985
    public function getResourceIdentifier(): int
986
    {
987
        return $this->getIid();
988
    }
989
990
    public function getResourceName(): string
991
    {
992
        return $this->getTitle();
993
    }
994
995
    public function setResourceName(string $name): self
996
    {
997
        return $this->setTitle($name);
998
    }
999
}
1000