Total Complexity | 79 |
Total Lines | 654 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like CQuiz often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CQuiz, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | #[ORM\Table(name: 'c_quiz')] |
||
27 | #[ORM\Entity(repositoryClass: CQuizRepository::class)] |
||
28 | class CQuiz extends AbstractResource implements ResourceInterface, ResourceShowCourseResourcesInSessionInterface, Stringable |
||
29 | { |
||
30 | public const ALL_ON_ONE_PAGE = 1; |
||
31 | public const ONE_PER_PAGE = 2; |
||
32 | |||
33 | #[Groups(['track_e_exercise:read'])] |
||
34 | #[ORM\Column(name: 'iid', type: 'integer')] |
||
35 | #[ORM\Id] |
||
36 | #[ORM\GeneratedValue] |
||
37 | protected ?int $iid = null; |
||
38 | |||
39 | #[Assert\NotBlank] |
||
40 | #[ORM\Column(name: 'title', type: 'text', nullable: false)] |
||
41 | protected string $title; |
||
42 | |||
43 | #[ORM\Column(name: 'description', type: 'text', nullable: true)] |
||
44 | protected ?string $description = null; |
||
45 | |||
46 | #[ORM\Column(name: 'sound', type: 'string', length: 255, nullable: true)] |
||
47 | protected ?string $sound = null; |
||
48 | |||
49 | #[ORM\Column(name: 'type', type: 'integer', nullable: false)] |
||
50 | protected int $type; |
||
51 | |||
52 | #[ORM\Column(name: 'random', type: 'integer', nullable: false)] |
||
53 | protected int $random; |
||
54 | |||
55 | #[ORM\Column(name: 'random_answers', type: 'boolean', nullable: false)] |
||
56 | protected bool $randomAnswers; |
||
57 | |||
58 | #[ORM\Column(name: 'active', type: 'integer', nullable: false)] |
||
59 | protected int $active; |
||
60 | |||
61 | #[ORM\Column(name: 'results_disabled', type: 'integer', nullable: false)] |
||
62 | protected int $resultsDisabled; |
||
63 | |||
64 | #[ORM\Column(name: 'access_condition', type: 'text', nullable: true)] |
||
65 | protected ?string $accessCondition = null; |
||
66 | |||
67 | #[ORM\Column(name: 'max_attempt', type: 'integer', nullable: false)] |
||
68 | protected int $maxAttempt; |
||
69 | |||
70 | #[ORM\Column(name: 'start_time', type: 'datetime', nullable: true)] |
||
71 | protected ?DateTime $startTime = null; |
||
72 | |||
73 | #[ORM\Column(name: 'end_time', type: 'datetime', nullable: true)] |
||
74 | protected ?DateTime $endTime = null; |
||
75 | |||
76 | #[ORM\Column(name: 'feedback_type', type: 'integer', nullable: false)] |
||
77 | protected int $feedbackType; |
||
78 | |||
79 | #[ORM\Column(name: 'expired_time', type: 'integer', nullable: false)] |
||
80 | protected int $expiredTime; |
||
81 | |||
82 | #[ORM\Column(name: 'propagate_neg', type: 'integer', nullable: false)] |
||
83 | protected int $propagateNeg; |
||
84 | |||
85 | #[ORM\Column(name: 'save_correct_answers', type: 'integer', nullable: true)] |
||
86 | protected ?int $saveCorrectAnswers; |
||
87 | |||
88 | #[ORM\Column(name: 'review_answers', type: 'integer', nullable: false)] |
||
89 | protected int $reviewAnswers; |
||
90 | |||
91 | #[ORM\Column(name: 'random_by_category', type: 'integer', nullable: false)] |
||
92 | protected int $randomByCategory; |
||
93 | |||
94 | #[ORM\Column(name: 'text_when_finished', type: 'text', nullable: true)] |
||
95 | protected ?string $textWhenFinished = null; |
||
96 | |||
97 | #[ORM\Column(name: 'text_when_finished_failure', type: 'text', nullable: true)] |
||
98 | protected ?string $textWhenFinishedFailure = null; |
||
99 | |||
100 | #[ORM\Column(name: 'display_category_name', type: 'integer', nullable: false)] |
||
101 | protected int $displayCategoryName; |
||
102 | |||
103 | #[ORM\Column(name: 'pass_percentage', type: 'integer', nullable: true)] |
||
104 | protected ?int $passPercentage = null; |
||
105 | |||
106 | #[ORM\Column(name: 'prevent_backwards', type: 'integer', nullable: false, options: ['default' => 0])] |
||
107 | protected int $preventBackwards; |
||
108 | |||
109 | #[ORM\Column(name: 'question_selection_type', type: 'integer', nullable: true)] |
||
110 | protected ?int $questionSelectionType = null; |
||
111 | |||
112 | #[ORM\Column(name: 'hide_question_number', type: 'integer', nullable: false, options: ['default' => 0])] |
||
113 | protected int $hideQuestionNumber; |
||
114 | |||
115 | #[ORM\Column(name: 'hide_question_title', type: 'boolean', nullable: false)] |
||
116 | protected bool $hideQuestionTitle; |
||
117 | |||
118 | #[ORM\ManyToOne(targetEntity: CQuizCategory::class, cascade: ['persist'])] |
||
119 | #[ORM\JoinColumn(name: 'quiz_category_id', referencedColumnName: 'id', onDelete: 'SET NULL')] |
||
120 | protected ?CQuizCategory $quizCategory = null; |
||
121 | |||
122 | #[ORM\Column(name: 'show_previous_button', type: 'boolean', nullable: false, options: ['default' => 1])] |
||
123 | protected bool $showPreviousButton; |
||
124 | |||
125 | #[ORM\Column(name: 'notifications', type: 'string', length: 255, nullable: true)] |
||
126 | protected ?string $notifications; |
||
127 | |||
128 | #[ORM\Column(name: 'autolaunch', type: 'boolean', nullable: true, options: ['default' => 0])] |
||
129 | protected ?bool $autoLaunch; |
||
130 | |||
131 | #[ORM\Column(name: 'hide_attempts_table', type: 'boolean', nullable: false, options: ['default' => 0])] |
||
132 | protected bool $hideAttemptsTable; |
||
133 | |||
134 | #[ORM\Column(name: 'page_result_configuration', type: 'array')] |
||
135 | protected array $pageResultConfiguration = []; |
||
136 | |||
137 | /** |
||
138 | * @var Collection<int, CQuizRelQuestion> |
||
139 | */ |
||
140 | #[ORM\OneToMany(mappedBy: 'quiz', targetEntity: CQuizRelQuestion::class, cascade: ['persist'], orphanRemoval: true)] |
||
141 | protected Collection $questions; |
||
142 | |||
143 | /** |
||
144 | * @var Collection<int, CQuizRelQuestionCategory> |
||
145 | */ |
||
146 | #[ORM\OneToMany(mappedBy: 'quiz', targetEntity: CQuizRelQuestionCategory::class, cascade: ['persist'])] |
||
147 | protected Collection $questionsCategories; |
||
148 | |||
149 | /** |
||
150 | * @var Collection<int, TrackEExercise> |
||
151 | */ |
||
152 | #[ORM\OneToMany(mappedBy: 'quiz', targetEntity: TrackEExercise::class)] |
||
153 | protected Collection $attempts; |
||
154 | |||
155 | public function __construct() |
||
156 | { |
||
157 | $this->questions = new ArrayCollection(); |
||
158 | $this->questionsCategories = new ArrayCollection(); |
||
159 | $this->hideQuestionTitle = false; |
||
160 | $this->hideQuestionNumber = 0; |
||
161 | $this->type = self::ONE_PER_PAGE; |
||
162 | $this->showPreviousButton = true; |
||
163 | $this->notifications = ''; |
||
164 | $this->autoLaunch = false; |
||
165 | $this->preventBackwards = 0; |
||
166 | $this->random = 0; |
||
167 | $this->randomAnswers = false; |
||
168 | $this->active = 1; |
||
169 | $this->resultsDisabled = 0; |
||
170 | $this->maxAttempt = 1; |
||
171 | $this->feedbackType = 0; |
||
172 | $this->expiredTime = 0; |
||
173 | $this->propagateNeg = 0; |
||
174 | $this->saveCorrectAnswers = 0; |
||
175 | $this->reviewAnswers = 0; |
||
176 | $this->randomByCategory = 0; |
||
177 | $this->displayCategoryName = 0; |
||
178 | $this->hideAttemptsTable = false; |
||
179 | $this->pageResultConfiguration = []; |
||
180 | $this->attempts = new ArrayCollection(); |
||
181 | $this->hideAttemptsTable = false; |
||
182 | } |
||
183 | |||
184 | public function __toString(): string |
||
185 | { |
||
186 | return $this->getTitle(); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @return ArrayCollection<int, CQuizRelQuestion> |
||
191 | */ |
||
192 | public function getQuestions(): Collection |
||
193 | { |
||
194 | return $this->questions instanceof ArrayCollection ? |
||
195 | $this->questions : |
||
196 | new ArrayCollection($this->questions->toArray()); |
||
197 | } |
||
198 | |||
199 | public function setTitle(string $title): self |
||
200 | { |
||
201 | $this->title = $title; |
||
202 | |||
203 | return $this; |
||
204 | } |
||
205 | |||
206 | public function getTitle(): string |
||
207 | { |
||
208 | return $this->title; |
||
209 | } |
||
210 | |||
211 | public function setDescription(string $description): self |
||
212 | { |
||
213 | $this->description = $description; |
||
214 | |||
215 | return $this; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Get description. |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | public function getDescription() |
||
224 | { |
||
225 | return $this->description; |
||
226 | } |
||
227 | |||
228 | public function setSound(string $sound): self |
||
229 | { |
||
230 | $this->sound = $sound; |
||
231 | |||
232 | return $this; |
||
233 | } |
||
234 | |||
235 | public function getSound(): ?string |
||
236 | { |
||
237 | return $this->sound; |
||
238 | } |
||
239 | |||
240 | public function setType(int $type): self |
||
241 | { |
||
242 | $this->type = $type; |
||
243 | |||
244 | return $this; |
||
245 | } |
||
246 | |||
247 | public function getType(): int |
||
248 | { |
||
249 | return $this->type; |
||
250 | } |
||
251 | |||
252 | public function setRandom(int $random): self |
||
253 | { |
||
254 | $this->random = $random; |
||
255 | |||
256 | return $this; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Get random. |
||
261 | * |
||
262 | * @return int |
||
263 | */ |
||
264 | public function getRandom() |
||
265 | { |
||
266 | return $this->random; |
||
267 | } |
||
268 | |||
269 | public function setRandomAnswers(bool $randomAnswers): self |
||
270 | { |
||
271 | $this->randomAnswers = $randomAnswers; |
||
272 | |||
273 | return $this; |
||
274 | } |
||
275 | |||
276 | public function getRandomAnswers(): bool |
||
277 | { |
||
278 | return $this->randomAnswers; |
||
279 | } |
||
280 | |||
281 | public function setActive(int $active): self |
||
282 | { |
||
283 | $this->active = $active; |
||
284 | |||
285 | return $this; |
||
286 | } |
||
287 | |||
288 | public function getActive(): int |
||
289 | { |
||
290 | return $this->active; |
||
291 | } |
||
292 | |||
293 | public function setResultsDisabled(int $resultsDisabled): self |
||
294 | { |
||
295 | $this->resultsDisabled = $resultsDisabled; |
||
296 | |||
297 | return $this; |
||
298 | } |
||
299 | |||
300 | public function getResultsDisabled(): int |
||
301 | { |
||
302 | return $this->resultsDisabled; |
||
303 | } |
||
304 | |||
305 | public function setAccessCondition(string $accessCondition): self |
||
306 | { |
||
307 | $this->accessCondition = $accessCondition; |
||
308 | |||
309 | return $this; |
||
310 | } |
||
311 | |||
312 | public function getAccessCondition(): ?string |
||
313 | { |
||
314 | return $this->accessCondition; |
||
315 | } |
||
316 | |||
317 | public function setMaxAttempt(int $maxAttempt): self |
||
318 | { |
||
319 | $this->maxAttempt = $maxAttempt; |
||
320 | |||
321 | return $this; |
||
322 | } |
||
323 | |||
324 | public function getMaxAttempt(): int |
||
325 | { |
||
326 | return $this->maxAttempt; |
||
327 | } |
||
328 | |||
329 | public function setStartTime(?DateTime $startTime): self |
||
330 | { |
||
331 | $this->startTime = $startTime; |
||
332 | |||
333 | return $this; |
||
334 | } |
||
335 | |||
336 | public function getStartTime(): ?DateTime |
||
337 | { |
||
338 | return $this->startTime; |
||
339 | } |
||
340 | |||
341 | public function setEndTime(?DateTime $endTime): self |
||
342 | { |
||
343 | $this->endTime = $endTime; |
||
344 | |||
345 | return $this; |
||
346 | } |
||
347 | |||
348 | public function getEndTime(): ?DateTime |
||
349 | { |
||
350 | return $this->endTime; |
||
351 | } |
||
352 | |||
353 | public function setFeedbackType(int $feedbackType): self |
||
354 | { |
||
355 | $this->feedbackType = $feedbackType; |
||
356 | |||
357 | return $this; |
||
358 | } |
||
359 | |||
360 | public function getFeedbackType(): int |
||
361 | { |
||
362 | return $this->feedbackType; |
||
363 | } |
||
364 | |||
365 | public function setExpiredTime(int $expiredTime): self |
||
366 | { |
||
367 | $this->expiredTime = $expiredTime; |
||
368 | |||
369 | return $this; |
||
370 | } |
||
371 | |||
372 | public function getExpiredTime(): int |
||
373 | { |
||
374 | return $this->expiredTime; |
||
375 | } |
||
376 | |||
377 | public function setPropagateNeg(int $propagateNeg): self |
||
378 | { |
||
379 | $this->propagateNeg = $propagateNeg; |
||
380 | |||
381 | return $this; |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * Get propagateNeg. |
||
386 | */ |
||
387 | public function getPropagateNeg(): int |
||
388 | { |
||
389 | return $this->propagateNeg; |
||
390 | } |
||
391 | |||
392 | public function setSaveCorrectAnswers(int $saveCorrectAnswers): self |
||
393 | { |
||
394 | $this->saveCorrectAnswers = $saveCorrectAnswers; |
||
395 | |||
396 | return $this; |
||
397 | } |
||
398 | |||
399 | public function getSaveCorrectAnswers(): ?int |
||
400 | { |
||
401 | return $this->saveCorrectAnswers; |
||
402 | } |
||
403 | |||
404 | public function setReviewAnswers(int $reviewAnswers): self |
||
405 | { |
||
406 | $this->reviewAnswers = $reviewAnswers; |
||
407 | |||
408 | return $this; |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Get reviewAnswers. |
||
413 | */ |
||
414 | public function getReviewAnswers(): int |
||
415 | { |
||
416 | return $this->reviewAnswers; |
||
417 | } |
||
418 | |||
419 | public function setRandomByCategory(int $randomByCategory): self |
||
420 | { |
||
421 | $this->randomByCategory = $randomByCategory; |
||
422 | |||
423 | return $this; |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * Get randomByCategory. |
||
428 | */ |
||
429 | public function getRandomByCategory(): int |
||
430 | { |
||
431 | return $this->randomByCategory; |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * Set text to display to user when they succeed to the test or, when no pass percentage has been set, when the |
||
436 | * test is finished. |
||
437 | */ |
||
438 | public function setTextWhenFinished(string $textWhenFinished): self |
||
439 | { |
||
440 | $this->textWhenFinished = $textWhenFinished; |
||
441 | |||
442 | return $this; |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Get text to display to user when they succeed to the test or, when no pass percentage has been set, when the |
||
447 | * test is finished. |
||
448 | */ |
||
449 | public function getTextWhenFinished(): ?string |
||
450 | { |
||
451 | return $this->textWhenFinished; |
||
452 | } |
||
453 | |||
454 | /** |
||
455 | * Set text to display to user when they fail to the test (when pass percentage has been set). |
||
456 | */ |
||
457 | public function setTextWhenFinishedFailure(?string $textWhenFinished): self |
||
458 | { |
||
459 | $this->textWhenFinishedFailure = $textWhenFinished; |
||
460 | |||
461 | return $this; |
||
462 | } |
||
463 | |||
464 | /** |
||
465 | * Get text to display to user when they fail to the test (when pass percentage has been set). |
||
466 | */ |
||
467 | public function getTextWhenFinishedFailure(): ?string |
||
468 | { |
||
469 | if (empty($this->textWhenFinishedFailure)) { |
||
470 | return ''; |
||
471 | } |
||
472 | |||
473 | return $this->textWhenFinishedFailure; |
||
474 | } |
||
475 | |||
476 | public function setDisplayCategoryName(int $displayCategoryName): self |
||
477 | { |
||
478 | $this->displayCategoryName = $displayCategoryName; |
||
479 | |||
480 | return $this; |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * Get displayCategoryName. |
||
485 | */ |
||
486 | public function getDisplayCategoryName(): int |
||
487 | { |
||
488 | return $this->displayCategoryName; |
||
489 | } |
||
490 | |||
491 | public function setPassPercentage(int $passPercentage): self |
||
492 | { |
||
493 | $this->passPercentage = $passPercentage; |
||
494 | |||
495 | return $this; |
||
496 | } |
||
497 | |||
498 | /** |
||
499 | * Get passPercentage. |
||
500 | */ |
||
501 | public function getPassPercentage(): ?int |
||
502 | { |
||
503 | return $this->passPercentage; |
||
504 | } |
||
505 | |||
506 | public function getQuizCategory(): ?CQuizCategory |
||
507 | { |
||
508 | return $this->quizCategory; |
||
509 | } |
||
510 | |||
511 | public function setQuizCategory(CQuizCategory $quizCategory): self |
||
512 | { |
||
513 | $this->quizCategory = $quizCategory; |
||
514 | |||
515 | return $this; |
||
516 | } |
||
517 | |||
518 | public function getQuestionSelectionType(): ?int |
||
519 | { |
||
520 | return $this->questionSelectionType; |
||
521 | } |
||
522 | |||
523 | public function setQuestionSelectionType(int $questionSelectionType): self |
||
524 | { |
||
525 | $this->questionSelectionType = $questionSelectionType; |
||
526 | |||
527 | return $this; |
||
528 | } |
||
529 | |||
530 | public function isHideQuestionTitle(): bool |
||
531 | { |
||
532 | return $this->hideQuestionTitle; |
||
533 | } |
||
534 | |||
535 | public function setHideQuestionTitle(bool $hideQuestionTitle): self |
||
536 | { |
||
537 | $this->hideQuestionTitle = $hideQuestionTitle; |
||
538 | |||
539 | return $this; |
||
540 | } |
||
541 | |||
542 | public function isShowPreviousButton(): bool |
||
543 | { |
||
544 | return $this->showPreviousButton; |
||
545 | } |
||
546 | |||
547 | public function setShowPreviousButton(bool $showPreviousButton): self |
||
548 | { |
||
549 | $this->showPreviousButton = $showPreviousButton; |
||
550 | |||
551 | return $this; |
||
552 | } |
||
553 | |||
554 | public function getNotifications(): string |
||
555 | { |
||
556 | return $this->notifications; |
||
|
|||
557 | } |
||
558 | |||
559 | public function setNotifications(string $notifications): self |
||
560 | { |
||
561 | $this->notifications = $notifications; |
||
562 | |||
563 | return $this; |
||
564 | } |
||
565 | |||
566 | public function getIid(): ?int |
||
567 | { |
||
568 | return $this->iid; |
||
569 | } |
||
570 | |||
571 | public function getPreventBackwards(): int |
||
572 | { |
||
573 | return $this->preventBackwards; |
||
574 | } |
||
575 | |||
576 | public function setPreventBackwards(int $preventBackwards): self |
||
577 | { |
||
578 | $this->preventBackwards = $preventBackwards; |
||
579 | |||
580 | return $this; |
||
581 | } |
||
582 | |||
583 | public function isAutoLaunch(): bool |
||
584 | { |
||
585 | return $this->autoLaunch; |
||
586 | } |
||
587 | |||
588 | public function setAutoLaunch(bool $autoLaunch): self |
||
589 | { |
||
590 | $this->autoLaunch = $autoLaunch; |
||
591 | |||
592 | return $this; |
||
593 | } |
||
594 | |||
595 | public function getPageResultConfiguration(): array |
||
596 | { |
||
597 | return $this->pageResultConfiguration; |
||
598 | } |
||
599 | |||
600 | public function setPageResultConfiguration(array $pageResultConfiguration): self |
||
601 | { |
||
602 | $this->pageResultConfiguration = $pageResultConfiguration; |
||
603 | |||
604 | return $this; |
||
605 | } |
||
606 | |||
607 | /** |
||
608 | * Returns the sum of question's ponderation. |
||
609 | */ |
||
610 | public function getMaxScore(): int |
||
611 | { |
||
612 | $maxScore = 0; |
||
613 | foreach ($this->questions as $relQuestion) { |
||
614 | $maxScore += $relQuestion->getQuestion()->getPonderation(); |
||
615 | } |
||
616 | |||
617 | return $maxScore; |
||
618 | } |
||
619 | |||
620 | public function getAutoLaunch(): ?bool |
||
621 | { |
||
622 | return $this->autoLaunch; |
||
623 | } |
||
624 | |||
625 | public function getHideQuestionNumber(): ?int |
||
626 | { |
||
627 | return $this->hideQuestionNumber; |
||
628 | } |
||
629 | |||
630 | public function setHideQuestionNumber(int $hideQuestionNumber): self |
||
631 | { |
||
632 | $this->hideQuestionNumber = $hideQuestionNumber; |
||
633 | |||
634 | return $this; |
||
635 | } |
||
636 | |||
637 | public function isHideAttemptsTable(): bool |
||
640 | } |
||
641 | |||
642 | public function setHideAttemptsTable(bool $hideAttemptsTable): self |
||
643 | { |
||
644 | $this->hideAttemptsTable = $hideAttemptsTable; |
||
645 | |||
646 | return $this; |
||
647 | } |
||
648 | |||
649 | /** |
||
650 | * @return Collection<int, CQuizRelQuestionCategory> |
||
651 | */ |
||
652 | public function getQuestionsCategories(): Collection |
||
653 | { |
||
654 | return $this->questionsCategories instanceof ArrayCollection ? |
||
655 | $this->questionsCategories : |
||
656 | new ArrayCollection($this->questionsCategories->toArray()); |
||
657 | } |
||
658 | |||
659 | public function getResourceIdentifier(): int|Uuid |
||
662 | } |
||
663 | |||
664 | public function getResourceName(): string |
||
665 | { |
||
666 | return $this->getTitle(); |
||
667 | } |
||
668 | |||
669 | public function setResourceName(string $name): self |
||
672 | } |
||
673 | |||
674 | /** |
||
675 | * @return Collection<int, TrackEExercise> |
||
676 | */ |
||
677 | public function getAttempts(): Collection |
||
678 | { |
||
679 | return $this->attempts; |
||
680 | } |
||
681 | } |
||
682 |