| Conditions | 14 |
| Paths | 128 |
| Total Lines | 103 |
| Code Lines | 59 |
| 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 |
||
| 37 | public function exportAll(string $exportDir): int |
||
| 38 | { |
||
| 39 | $baseDir = rtrim($exportDir, '/').'/chamilo/learnpath'; |
||
| 40 | $this->ensureDir($baseDir); |
||
| 41 | |||
| 42 | // Resolve resources bag defensively |
||
| 43 | $res = is_array($this->course->resources ?? null) ? $this->course->resources : []; |
||
| 44 | |||
| 45 | // ---- Categories (optional but recommended) ---- |
||
| 46 | $catBag = |
||
| 47 | ($res[\defined('RESOURCE_LEARNPATH_CATEGORY') ? RESOURCE_LEARNPATH_CATEGORY : 'learnpath_category'] ?? null) |
||
| 48 | ?? ($res['learnpath_category'] ?? []) |
||
| 49 | ; |
||
| 50 | |||
| 51 | $categories = []; |
||
| 52 | if (is_array($catBag)) { |
||
| 53 | foreach ($catBag as $cid => $cwrap) { |
||
| 54 | $cobj = $this->unwrapIfObject($cwrap); |
||
| 55 | $carr = $this->toArray($cobj); |
||
| 56 | // Normalize minimal shape (id, title) if present |
||
| 57 | $categories[] = [ |
||
| 58 | 'id' => (int) ($carr['id'] ?? $cid), |
||
| 59 | 'title' => (string) ($carr['title'] ?? ($carr['name'] ?? '')), |
||
| 60 | 'raw' => $carr, // keep full raw payload as well |
||
| 61 | ]; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | $this->writeJson($baseDir.'/categories.json', ['categories' => $categories]); |
||
| 65 | |||
| 66 | // Build a map id→title for quick lookup |
||
| 67 | $catTitle = []; |
||
| 68 | foreach ($categories as $c) { |
||
| 69 | $catTitle[(int) $c['id']] = (string) $c['title']; |
||
| 70 | } |
||
| 71 | |||
| 72 | // ---- Learnpaths ---- |
||
| 73 | $lpBag = |
||
| 74 | ($res[\defined('RESOURCE_LEARNPATH') ? RESOURCE_LEARNPATH : 'learnpath'] ?? null) |
||
| 75 | ?? ($res['learnpath'] ?? []) |
||
| 76 | ; |
||
| 77 | |||
| 78 | if (!is_array($lpBag) || empty($lpBag)) { |
||
| 79 | @error_log('[LearnpathMetaExport] No learnpaths present in resources; skipping.'); |
||
|
|
|||
| 80 | // still return 0 after writing (possibly empty) categories.json |
||
| 81 | $this->exportScormIndexIfAny($res, $baseDir); |
||
| 82 | $this->writeJson($baseDir.'/index.json', ['learnpaths' => []]); |
||
| 83 | return 0; |
||
| 84 | } |
||
| 85 | |||
| 86 | $index = []; |
||
| 87 | $count = 0; |
||
| 88 | |||
| 89 | foreach ($lpBag as $lpId => $lpWrap) { |
||
| 90 | $lpObj = $this->unwrapIfObject($lpWrap); // stdClass payload from builder |
||
| 91 | $lpArr = $this->toArray($lpObj); // full raw payload |
||
| 92 | |||
| 93 | $lpDir = $baseDir.'/lp_'.((int) $lpArr['id'] ?: (int) $lpId); |
||
| 94 | $this->ensureDir($lpDir); |
||
| 95 | |||
| 96 | // Resolve category label if possible |
||
| 97 | $cid = (int) ($lpArr['category_id'] ?? 0); |
||
| 98 | $lpArr['_context'] = [ |
||
| 99 | 'lp_id' => (int) ($lpArr['id'] ?? $lpId), |
||
| 100 | 'lp_type' => (int) ($lpArr['lp_type'] ?? 0), // 1=LP,2=SCORM,3=AICC |
||
| 101 | 'category_id' => $cid, |
||
| 102 | 'category_name' => $catTitle[$cid] ?? null, |
||
| 103 | ]; |
||
| 104 | |||
| 105 | // Persist learnpath.json (complete raw payload + _context) |
||
| 106 | $this->writeJson($lpDir.'/learnpath.json', ['learnpath' => $lpArr]); |
||
| 107 | |||
| 108 | // Persist items.json as a separate, ordered list (if provided) |
||
| 109 | $items = []; |
||
| 110 | if (isset($lpArr['items']) && is_array($lpArr['items'])) { |
||
| 111 | $items = $lpArr['items']; |
||
| 112 | // Stable sort by display_order if present, otherwise keep builder order |
||
| 113 | usort($items, static function (array $a, array $b): int { |
||
| 114 | return (int) ($a['display_order'] ?? 0) <=> (int) ($b['display_order'] ?? 0); |
||
| 115 | }); |
||
| 116 | } |
||
| 117 | $this->writeJson($lpDir.'/items.json', ['items' => $items]); |
||
| 118 | |||
| 119 | // Add to index |
||
| 120 | $index[] = [ |
||
| 121 | 'id' => (int) ($lpArr['id'] ?? $lpId), |
||
| 122 | 'title' => (string) ($lpArr['title'] ?? ''), |
||
| 123 | 'lp_type' => (int) ($lpArr['lp_type'] ?? 0), |
||
| 124 | 'category_id' => $cid, |
||
| 125 | 'category_name' => $catTitle[$cid] ?? null, |
||
| 126 | 'dir' => 'lp_'.((int) $lpArr['id'] ?: (int) $lpId), |
||
| 127 | ]; |
||
| 128 | |||
| 129 | $count++; |
||
| 130 | } |
||
| 131 | |||
| 132 | // Persist learnpaths index |
||
| 133 | $this->writeJson($baseDir.'/index.json', ['learnpaths' => $index]); |
||
| 134 | |||
| 135 | // Optional SCORM index (if present in resources) |
||
| 136 | $this->exportScormIndexIfAny($res, $baseDir); |
||
| 137 | |||
| 138 | @error_log('[LearnpathMetaExport] Exported learnpaths='.$count.' categories='.count($categories)); |
||
| 139 | return $count; |
||
| 140 | } |
||
| 204 |
If you suppress an error, we recommend checking for the error condition explicitly: