| Total Complexity | 55 |
| Total Lines | 286 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Cc13Convert 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 Cc13Convert, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 4 | class Cc13Convert |
||
| 5 | { |
||
| 6 | public static function convert($packagedir, $outdir, $objCourse) |
||
| 7 | { |
||
| 8 | $dir = realpath($packagedir); |
||
| 9 | if (empty($dir)) { |
||
| 10 | throw new InvalidArgumentException('Directory does not exist!'); |
||
| 11 | } |
||
| 12 | $odir = realpath($outdir); |
||
| 13 | if (empty($odir)) { |
||
| 14 | throw new InvalidArgumentException('Directory does not exist!'); |
||
| 15 | } |
||
| 16 | |||
| 17 | if (!empty($objCourse)) { |
||
| 18 | //Initialize the manifest metadata class |
||
| 19 | $meta = new CcMetadataManifest(); |
||
| 20 | |||
| 21 | //Package metadata |
||
| 22 | $metageneral = new CcMetadataGeneral(); |
||
| 23 | $metageneral->setLanguage($objCourse->info['language']); |
||
| 24 | $metageneral->setTitle($objCourse->info['title'], $objCourse->info['language']); |
||
| 25 | $metageneral->setDescription('', $objCourse->info['language']); |
||
| 26 | $metageneral->setCatalog('category'); |
||
| 27 | $metageneral->setEntry($objCourse->info['categoryName']); |
||
| 28 | $meta->addMetadataGeneral($metageneral); |
||
| 29 | |||
| 30 | // Create the manifest |
||
| 31 | $manifest = new CcManifest(); |
||
| 32 | |||
| 33 | $manifest->addMetadataManifest($meta); |
||
| 34 | |||
| 35 | $organization = null; |
||
| 36 | |||
| 37 | //Get the course structure - this will be transformed into organization |
||
| 38 | //Step 1 - Get the list and order of sections/topics |
||
| 39 | |||
| 40 | $count = 1; |
||
| 41 | $sections = []; |
||
| 42 | $resources = $objCourse->resources; |
||
| 43 | |||
| 44 | // We check the quiz sections |
||
| 45 | if (isset($resources['quiz'])) { |
||
| 46 | $quizSections = self::getItemSections($resources['quiz'], 'quiz', $count, $objCourse->info['code'], $resources['Exercise_Question']); |
||
| 47 | $sections = array_merge($sections, $quizSections); |
||
| 48 | } |
||
| 49 | |||
| 50 | // We check the document sections |
||
| 51 | if (isset($resources['document'])) { |
||
| 52 | $documentSections = self::getItemSections($resources['document'], 'document', $count, $objCourse->info['code']); |
||
| 53 | $sections = array_merge($sections, $documentSections); |
||
| 54 | } |
||
| 55 | |||
| 56 | // We check the wiki sections |
||
| 57 | if (isset($resources['wiki'])) { |
||
| 58 | $wikiSections = self::getItemSections($resources['wiki'], 'wiki', $count, $objCourse->info['code']); |
||
| 59 | $sections = array_merge($sections, $wikiSections); |
||
| 60 | } |
||
| 61 | |||
| 62 | // We check the forum sections |
||
| 63 | if (isset($resources['forum'])) { |
||
| 64 | $forumSections = self::getItemSections($resources['forum'], 'forum', $count, $objCourse->info['code'], $resources['Forum_Category']); |
||
| 65 | $sections = array_merge($sections, $forumSections); |
||
| 66 | } |
||
| 67 | |||
| 68 | // We check the link sections |
||
| 69 | if (isset($resources['link'])) { |
||
| 70 | $linkSections = self::getItemSections($resources['link'], 'link', $count, $objCourse->info['code'], $resources['Link_Category']); |
||
| 71 | $sections = array_merge($sections, $linkSections); |
||
| 72 | } |
||
| 73 | |||
| 74 | //organization title |
||
| 75 | $organization = new CcOrganization(); |
||
| 76 | foreach ($sections as $sectionid => $values) { |
||
| 77 | $item = new CcItem(); |
||
| 78 | $item->title = $values[0]; |
||
| 79 | self::processSequence($item, $manifest, $values[1], $dir, $odir); |
||
| 80 | $organization->addItem($item); |
||
| 81 | } |
||
| 82 | $manifest->putNodes(); |
||
| 83 | |||
| 84 | if (!empty($organization)) { |
||
| 85 | $manifest->addNewOrganization($organization); |
||
| 86 | } |
||
| 87 | |||
| 88 | $manifestpath = $outdir.DIRECTORY_SEPARATOR.'imsmanifest.xml'; |
||
| 89 | $saved = $manifest->saveTo($manifestpath); |
||
| 90 | |||
| 91 | return $saved; |
||
| 92 | } |
||
| 93 | |||
| 94 | return false; |
||
| 95 | } |
||
| 96 | |||
| 97 | protected static function getItemSections($itemData, $itemType, &$count, $courseCode, $itmesExtraData = null) |
||
| 98 | { |
||
| 99 | $sections = []; |
||
| 100 | switch ($itemType) { |
||
| 101 | case 'quiz': |
||
| 102 | case 'document': |
||
| 103 | case 'wiki': |
||
| 104 | $convertType = $itemType; |
||
| 105 | if ($itemType == 'wiki') { |
||
| 106 | $convertType = 'Page'; |
||
| 107 | } |
||
| 108 | $sectionid = $count; |
||
| 109 | $sectiontitle = ucfirst($itemType); |
||
| 110 | $sequence = self::getSequence($itemData, 0, $convertType, $courseCode, $itmesExtraData); |
||
| 111 | $sections[$sectionid] = [$sectiontitle, $sequence]; |
||
| 112 | $count++; |
||
| 113 | break; |
||
| 114 | case 'link': |
||
| 115 | $links = self::getSequence($itemData, null, $itemType); |
||
| 116 | foreach ($links as $categoryId => $sequence) { |
||
| 117 | $sectionid = $count; |
||
| 118 | if (isset($itmesExtraData[$categoryId])) { |
||
| 119 | $sectiontitle = $itmesExtraData[$categoryId]->title; |
||
| 120 | } else { |
||
| 121 | $sectiontitle = 'General'; |
||
| 122 | } |
||
| 123 | $sections[$sectionid] = [$sectiontitle, $sequence]; |
||
| 124 | $count++; |
||
| 125 | } |
||
| 126 | break; |
||
| 127 | case 'forum': |
||
| 128 | if (isset($itmesExtraData)) { |
||
| 129 | foreach ($itmesExtraData as $fcategory) { |
||
| 130 | if (isset($fcategory->obj)) { |
||
| 131 | $objCategory = $fcategory->obj; |
||
| 132 | $sectionid = $count; |
||
| 133 | $sectiontitle = $objCategory->cat_title; |
||
| 134 | $sequence = self::getSequence($itemData, $objCategory->iid, $itemType); |
||
| 135 | $sections[$sectionid] = [$sectiontitle, $sequence]; |
||
| 136 | $count++; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | break; |
||
| 141 | } |
||
| 142 | |||
| 143 | return $sections; |
||
| 144 | } |
||
| 145 | |||
| 146 | protected static function getSequence($objItems, $categoryId = null, $itemType = null, $coursecode = null, $itemQuestions = null) |
||
| 147 | { |
||
| 148 | $sequences = []; |
||
| 149 | switch ($itemType) { |
||
| 150 | case 'quiz': |
||
| 151 | $sequence = []; |
||
| 152 | foreach ($objItems as $objItem) { |
||
| 153 | if ($categoryId === 0) { |
||
| 154 | $questions = []; |
||
| 155 | foreach ($objItem->obj->question_ids as $questionId) { |
||
| 156 | if (isset($itemQuestions[$questionId])) { |
||
| 157 | $questions[$questionId] = $itemQuestions[$questionId]; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | $sequence[$categoryId][$objItem->obj->iid] = [ |
||
| 161 | 'title' => $objItem->obj->title, |
||
| 162 | 'comment' => $objItem->obj->description, |
||
| 163 | 'cc_type' => 'quiz', |
||
| 164 | 'source_id' => $objItem->obj->iid, |
||
| 165 | 'questions' => $questions, |
||
| 166 | 'max_attempt' => $objItem->obj->max_attempt, |
||
| 167 | 'expired_time' => $objItem->obj->expired_time, |
||
| 168 | 'pass_percentage' => $objItem->obj->pass_percentage, |
||
| 169 | 'random_answers' => $objItem->obj->random_answers, |
||
| 170 | 'course_code' => $coursecode, |
||
| 171 | ]; |
||
| 172 | } |
||
| 173 | } |
||
| 174 | $sequences = $sequence[$categoryId]; |
||
| 175 | break; |
||
| 176 | case 'document': |
||
| 177 | $sequence = []; |
||
| 178 | foreach ($objItems as $objItem) { |
||
| 179 | if ($categoryId === 0) { |
||
| 180 | $sequence[$categoryId][$objItem->source_id] = [ |
||
| 181 | 'title' => $objItem->title, |
||
| 182 | 'comment' => $objItem->comment, |
||
| 183 | 'cc_type' => ($objItem->file_type == 'folder' ? 'folder' : 'resource'), |
||
| 184 | 'source_id' => $objItem->source_id, |
||
| 185 | 'path' => $objItem->path, |
||
| 186 | 'file_type' => $objItem->file_type, |
||
| 187 | 'course_code' => $coursecode, |
||
| 188 | ]; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | $sequences = $sequence[$categoryId]; |
||
| 192 | break; |
||
| 193 | case 'forum': |
||
| 194 | foreach ($objItems as $objItem) { |
||
| 195 | if ($categoryId == $objItem->obj->forum_category) { |
||
| 196 | $sequence[$categoryId][$objItem->obj->forum_id] = [ |
||
| 197 | 'title' => $objItem->obj->forum_title, |
||
| 198 | 'comment' => $objItem->obj->forum_comment, |
||
| 199 | 'cc_type' => 'forum', |
||
| 200 | 'source_id' => $objItem->obj->iid, |
||
| 201 | ]; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | $sequences = $sequence[$categoryId]; |
||
|
|
|||
| 205 | break; |
||
| 206 | case 'page': |
||
| 207 | foreach ($objItems as $objItem) { |
||
| 208 | if ($categoryId === 0) { |
||
| 209 | $sequence[$categoryId][$objItem->page_id] = [ |
||
| 210 | 'title' => $objItem->title, |
||
| 211 | 'comment' => $objItem->content, |
||
| 212 | 'cc_type' => 'page', |
||
| 213 | 'source_id' => $objItem->page_id, |
||
| 214 | 'reflink' => $objItem->reflink, |
||
| 215 | ]; |
||
| 216 | } |
||
| 217 | } |
||
| 218 | $sequences = $sequence[$categoryId]; |
||
| 219 | break; |
||
| 220 | case 'link': |
||
| 221 | if (!isset($categoryId)) { |
||
| 222 | $categories = []; |
||
| 223 | foreach ($objItems as $objItem) { |
||
| 224 | $categories[$objItem->category_id] = self::getSequence($objItems, $objItem->category_id, $itemType); |
||
| 225 | } |
||
| 226 | $sequences = $categories; |
||
| 227 | } else { |
||
| 228 | foreach ($objItems as $objItem) { |
||
| 229 | if ($categoryId == $objItem->category_id) { |
||
| 230 | $sequence[$categoryId][$objItem->source_id] = [ |
||
| 231 | 'title' => $objItem->title, |
||
| 232 | 'comment' => $objItem->description, |
||
| 233 | 'cc_type' => 'url', |
||
| 234 | 'source_id' => $objItem->source_id, |
||
| 235 | 'url' => $objItem->url, |
||
| 236 | 'target' => $objItem->target, |
||
| 237 | ]; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | $sequences = $sequence[$categoryId]; |
||
| 241 | } |
||
| 242 | break; |
||
| 243 | } |
||
| 244 | |||
| 245 | return $sequences; |
||
| 246 | } |
||
| 247 | |||
| 248 | protected static function processSequence(CcIItem &$item, CcIManifest &$manifest, array $sequence, $packageroot, $outdir) |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | protected static function itemIndenter(CcIItem &$item, $level = 0) |
||
| 290 | } |
||
| 291 | } |
||
| 292 |