| Conditions | 17 |
| Paths | 130 |
| Total Lines | 102 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 22 | public function export(int $activityId, string $exportDir, int $moduleId, int $sectionId): void |
||
| 23 | { |
||
| 24 | // Build destination folder |
||
| 25 | $baseDir = rtrim($exportDir, '/').'/chamilo/quiz/quiz_'.$moduleId; |
||
| 26 | $this->ensureDir($baseDir); |
||
| 27 | |||
| 28 | // Resolve quiz bag (accept constant or string key) |
||
| 29 | $quizBag = |
||
| 30 | $this->course->resources[\defined('RESOURCE_QUIZ') ? RESOURCE_QUIZ : 'quiz'] ?? |
||
| 31 | $this->course->resources['quiz'] ?? []; |
||
| 32 | |||
| 33 | if (empty($quizBag[$activityId])) { |
||
| 34 | @error_log('[QuizMetaExport] WARN quiz not found in resources: id='.$activityId); |
||
|
|
|||
| 35 | return; |
||
| 36 | } |
||
| 37 | |||
| 38 | // Unwrap quiz wrapper → raw payload already prepared by build_quizzes() |
||
| 39 | $quizWrap = $quizBag[$activityId]; |
||
| 40 | $quizObj = $this->unwrap($quizWrap); // stdClass payload |
||
| 41 | $quizArr = $this->toArray($quizObj); // array payload for JSON |
||
| 42 | |||
| 43 | // Keep minimal context references (section/module) for traceability |
||
| 44 | $quizArr['_context'] = [ |
||
| 45 | 'module_id' => (int) $moduleId, |
||
| 46 | 'section_id' => (int) $sectionId, |
||
| 47 | ]; |
||
| 48 | |||
| 49 | // Persist quiz.json |
||
| 50 | $this->writeJson($baseDir.'/quiz.json', ['quiz' => $quizArr]); |
||
| 51 | |||
| 52 | // Collect questions for this quiz (preserve order if available) |
||
| 53 | $questionIds = []; |
||
| 54 | $orders = []; |
||
| 55 | |||
| 56 | if (isset($quizArr['question_ids']) && \is_array($quizArr['question_ids'])) { |
||
| 57 | $questionIds = array_map('intval', $quizArr['question_ids']); |
||
| 58 | } |
||
| 59 | if (isset($quizArr['question_orders']) && \is_array($quizArr['question_orders'])) { |
||
| 60 | $orders = array_map('intval', $quizArr['question_orders']); |
||
| 61 | } |
||
| 62 | |||
| 63 | $qBag = |
||
| 64 | $this->course->resources[\defined('RESOURCE_QUIZQUESTION') ? RESOURCE_QUIZQUESTION : 'Exercise_Question'] |
||
| 65 | ?? $this->course->resources['Exercise_Question'] |
||
| 66 | ?? []; |
||
| 67 | |||
| 68 | // Build ordered questions array (raw, with their nested answers) |
||
| 69 | $questions = []; |
||
| 70 | $answersFlat = []; |
||
| 71 | $orderMap = []; |
||
| 72 | |||
| 73 | // If we have question_orders aligned with question_ids, build an order map |
||
| 74 | if (!empty($questionIds) && !empty($orders) && \count($questionIds) === \count($orders)) { |
||
| 75 | foreach ($questionIds as $idx => $qid) { |
||
| 76 | $orderMap[(int) $qid] = (int) $orders[$idx]; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | foreach ($questionIds as $qid) { |
||
| 81 | if (!isset($qBag[$qid])) { |
||
| 82 | continue; |
||
| 83 | } |
||
| 84 | $qWrap = $qBag[$qid]; |
||
| 85 | $qObj = $this->unwrap($qWrap); // stdClass payload from build_quiz_questions() |
||
| 86 | $qArr = $this->toArray($qObj); |
||
| 87 | |||
| 88 | // Attach quiz reference |
||
| 89 | $qArr['_links']['quiz_id'] = (int) $activityId; |
||
| 90 | |||
| 91 | // Optional: attach explicit question_id for clarity |
||
| 92 | $qArr['id'] = $qArr['id'] ?? (int) ($qWrap->source_id ?? $qid); |
||
| 93 | |||
| 94 | // Flatten answers to a standalone list (still keep them nested in question) |
||
| 95 | $answers = []; |
||
| 96 | if (isset($qArr['answers']) && \is_array($qArr['answers'])) { |
||
| 97 | foreach ($qArr['answers'] as $ans) { |
||
| 98 | $answers[] = $ans; |
||
| 99 | |||
| 100 | $answersFlat[] = [ |
||
| 101 | 'quiz_id' => (int) $activityId, |
||
| 102 | 'question_id' => (int) $qArr['id'], |
||
| 103 | // Persist raw answer data verbatim |
||
| 104 | 'data' => $ans, |
||
| 105 | ]; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | // Preserve original order if available; fallback to question "position" |
||
| 110 | $qArr['_order'] = $orderMap[$qid] ?? (int) ($qArr['position'] ?? 0); |
||
| 111 | $questions[] = $qArr; |
||
| 112 | } |
||
| 113 | |||
| 114 | // Sort questions by _order asc (stable) |
||
| 115 | usort($questions, static function (array $a, array $b): int { |
||
| 116 | return ($a['_order'] ?? 0) <=> ($b['_order'] ?? 0); |
||
| 117 | }); |
||
| 118 | |||
| 119 | // Persist questions.json (full raw) |
||
| 120 | $this->writeJson($baseDir.'/questions.json', ['questions' => $questions]); |
||
| 121 | |||
| 122 | // Persist answers.json (flat list) |
||
| 123 | $this->writeJson($baseDir.'/answers.json', ['answers' => $answersFlat]); |
||
| 124 | } |
||
| 172 |
If you suppress an error, we recommend checking for the error condition explicitly: