| Conditions | 22 |
| Paths | 13 |
| Total Lines | 201 |
| Code Lines | 132 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| 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 |
||
| 19 | public function readMoodleFile($uploadedFile) |
||
| 20 | { |
||
| 21 | $file = $uploadedFile['tmp_name']; |
||
| 22 | |||
| 23 | if (is_file($file) && is_readable($file)) { |
||
| 24 | $package = new PclZip($file); |
||
| 25 | $packageContent = $package->listContent(); |
||
| 26 | $mainFileKey = 0; |
||
| 27 | foreach ($packageContent as $index => $value) { |
||
| 28 | if ($value['filename'] == 'moodle_backup.xml') { |
||
| 29 | $mainFileKey = $index; |
||
| 30 | break; |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | if (!$mainFileKey) { |
||
| 35 | Display::addFlash(Display::return_message(get_lang('FailedToImportThisIsNotAMoodleFile'), 'error')); |
||
| 36 | } |
||
| 37 | |||
| 38 | $folder = api_get_unique_id(); |
||
| 39 | $destinationDir = api_get_path(SYS_ARCHIVE_PATH).$folder; |
||
| 40 | $coursePath = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/'; |
||
| 41 | $courseInfo = api_get_course_info(); |
||
| 42 | |||
| 43 | mkdir($destinationDir, api_get_permissions_for_new_directories(), true); |
||
| 44 | |||
| 45 | $package->extract( |
||
| 46 | PCLZIP_OPT_PATH, |
||
| 47 | $destinationDir |
||
| 48 | ); |
||
| 49 | |||
| 50 | $xml = @file_get_contents($destinationDir.'/moodle_backup.xml'); |
||
| 51 | |||
| 52 | $doc = new DOMDocument(); |
||
| 53 | $res = @$doc->loadXML($xml); |
||
| 54 | if ($res) { |
||
| 55 | $activities = $doc->getElementsByTagName('activity'); |
||
| 56 | foreach ($activities as $activity) { |
||
| 57 | if ($activity->childNodes->length) { |
||
| 58 | $currentItem = []; |
||
| 59 | |||
| 60 | foreach($activity->childNodes as $item) { |
||
| 61 | $currentItem[$item->nodeName] = $item->nodeValue; |
||
| 62 | } |
||
| 63 | |||
| 64 | $moduleName = isset($currentItem['modulename']) ? $currentItem['modulename'] : false; |
||
| 65 | switch ($moduleName) { |
||
| 66 | case 'forum1': |
||
| 67 | require_once '../forum/forumfunction.inc.php'; |
||
| 68 | $catForumValues = []; |
||
| 69 | |||
| 70 | // Read the current forum module xml. |
||
| 71 | $moduleDir = $currentItem['directory']; |
||
| 72 | $moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml'); |
||
| 73 | $moduleValues = $this->readForumModule($moduleXml); |
||
| 74 | |||
| 75 | // Create a Forum category based on Moodle forum type. |
||
| 76 | $catForumValues['forum_category_title'] = $moduleValues['type']; |
||
| 77 | $catForumValues['forum_category_comment'] = ''; |
||
| 78 | $catId = store_forumcategory($catForumValues); |
||
| 79 | $forumValues = []; |
||
| 80 | $forumValues['forum_title'] = $moduleValues['name']; |
||
| 81 | $forumValues['forum_image'] = ''; |
||
| 82 | $forumValues['forum_comment'] = $moduleValues['intro']; |
||
| 83 | $forumValues['forum_category'] = $catId; |
||
| 84 | |||
| 85 | store_forum($forumValues); |
||
| 86 | break; |
||
| 87 | case 'quiz': |
||
|
|
|||
| 88 | |||
| 89 | // Read the current quiz module xml. |
||
| 90 | // The quiz case is the very complicate process of all the import. |
||
| 91 | // Please if you want to review the script, try to see the readingXML functions. |
||
| 92 | // The readingXML functions in this clases do all the mayor work here. |
||
| 93 | |||
| 94 | $moduleDir = $currentItem['directory']; |
||
| 95 | $moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml'); |
||
| 96 | $questionsXml = @file_get_contents($destinationDir.'/questions.xml'); |
||
| 97 | $moduleValues = $this->readQuizModule($moduleXml); |
||
| 98 | |||
| 99 | // At this point we got all the prepared resources from Moodle file |
||
| 100 | // $moduleValues variable contains all the necesary info to the quiz import |
||
| 101 | // var_dump($moduleValues['question_instances']); // <-- uncomment this to see the final array |
||
| 102 | |||
| 103 | // Lets do this ... |
||
| 104 | $exercise = new Exercise(); |
||
| 105 | $exercise->updateTitle(Exercise::format_title_variable($moduleValues['name'])); |
||
| 106 | $exercise->updateDescription($moduleValues['intro']); |
||
| 107 | $exercise->updateAttempts($moduleValues['attempts_number']); |
||
| 108 | $exercise->updateFeedbackType(0); |
||
| 109 | $exercise->setRandom(intval($moduleValues['shufflequestions']) + 1); |
||
| 110 | $exercise->updateRandomAnswers($moduleValues['shuffleanswers']); |
||
| 111 | $exercise->updateExpiredTime($moduleValues['timelimit']); |
||
| 112 | |||
| 113 | if ($moduleValues['questionsperpage'] == 1) { |
||
| 114 | $exercise->updateType(2); |
||
| 115 | } else { |
||
| 116 | $exercise->updateType(1); |
||
| 117 | } |
||
| 118 | |||
| 119 | // Create the new Quiz |
||
| 120 | $exercise->save(); |
||
| 121 | |||
| 122 | // Ok, we got the Quiz and create it, now its time to add the Questions |
||
| 123 | foreach ($moduleValues['question_instances'] as $index => $question) { |
||
| 124 | $questionsValues = $this->readMainQuestionsXml($questionsXml, $question['questionid']); |
||
| 125 | $moduleValues['question_instances'][$index] = $questionsValues; |
||
| 126 | // Set Question Type |
||
| 127 | $qType = $moduleValues['question_instances'][$index]['qtype']; |
||
| 128 | // Add the matched chamilo question type to the array |
||
| 129 | $moduleValues['question_instances'][$index]['chamilo_qtype'] = $this->matchMoodleChamiloQuestionTypes($qType); |
||
| 130 | $questionInstance = Question::getInstance($moduleValues['question_instances'][$index]['chamilo_qtype']); |
||
| 131 | $questionInstance->updateTitle($moduleValues['question_instances'][$index]['name']); |
||
| 132 | $questionInstance->updateDescription($moduleValues['question_instances'][$index]['questiontext']); |
||
| 133 | $questionInstance->updateLevel(1); |
||
| 134 | $questionInstance->updateCategory(0); |
||
| 135 | |||
| 136 | //Save normal question if NOT media |
||
| 137 | if ($questionInstance->type != MEDIA_QUESTION) { |
||
| 138 | $questionInstance->save($exercise->id); |
||
| 139 | |||
| 140 | // modify the exercise |
||
| 141 | $exercise->addToList($questionInstance->id); |
||
| 142 | $exercise->update_question_positions(); |
||
| 143 | } |
||
| 144 | |||
| 145 | $objAnswer = new Answer($questionInstance->id); |
||
| 146 | |||
| 147 | foreach ($moduleValues['question_instances'][$index]['plugin_qtype_'.$qType.'_question'] as $slot => $answer) { |
||
| 148 | $questionWeighting = $this->processUniqueAnswer($objAnswer, $answer, $slot + 1); |
||
| 149 | } |
||
| 150 | |||
| 151 | // saves the answers into the data base |
||
| 152 | $objAnswer->save(); |
||
| 153 | |||
| 154 | // sets the total weighting of the question |
||
| 155 | $questionInstance->updateWeighting($questionWeighting); |
||
| 156 | $questionInstance->save(); |
||
| 157 | } |
||
| 158 | |||
| 159 | break; |
||
| 160 | case 'resource1': |
||
| 161 | // Read the current resource module xml. |
||
| 162 | $moduleDir = $currentItem['directory']; |
||
| 163 | $moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml'); |
||
| 164 | $filesXml = @file_get_contents($destinationDir.'/files.xml'); |
||
| 165 | $moduleValues = $this->readResourceModule($moduleXml); |
||
| 166 | $mainFileModuleValues = $this->readMainFilesXml($filesXml, $moduleValues['contextid']); |
||
| 167 | $fileInfo = array_merge($moduleValues, $mainFileModuleValues, $currentItem); |
||
| 168 | $documentPath = $coursePath.'document/'; |
||
| 169 | $currentResourceFilePath = $destinationDir.'/files/'; |
||
| 170 | $dirs = new RecursiveDirectoryIterator($currentResourceFilePath); |
||
| 171 | foreach(new RecursiveIteratorIterator($dirs) as $file) { |
||
| 172 | if (is_file($file) && strpos($file, $fileInfo['contenthash']) !== false) { |
||
| 173 | $files = []; |
||
| 174 | $files['file']['name'] = $fileInfo['filename']; |
||
| 175 | $files['file']['tmp_name'] = $file->getPathname(); |
||
| 176 | $files['file']['type'] = $fileInfo['mimetype']; |
||
| 177 | $files['file']['error'] = 0; |
||
| 178 | $files['file']['size'] = $fileInfo['filesize']; |
||
| 179 | $files['file']['from_file'] = true; |
||
| 180 | $files['file']['move_file'] = true; |
||
| 181 | $_POST['language'] = $courseInfo['language']; |
||
| 182 | $_POST['moodle_import'] = true; |
||
| 183 | |||
| 184 | DocumentManager::upload_document( |
||
| 185 | $files, |
||
| 186 | '/', |
||
| 187 | $fileInfo['title'], |
||
| 188 | '', |
||
| 189 | null, |
||
| 190 | null, |
||
| 191 | true, |
||
| 192 | true |
||
| 193 | ); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | break; |
||
| 198 | case 'url1': |
||
| 199 | // Read the current url module xml. |
||
| 200 | $moduleDir = $currentItem['directory']; |
||
| 201 | $moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml'); |
||
| 202 | $moduleValues = $this->readUrlModule($moduleXml); |
||
| 203 | $_POST['title'] = $moduleValues['name']; |
||
| 204 | $_POST['url'] = $moduleValues['externalurl']; |
||
| 205 | $_POST['description'] = $moduleValues['intro']; |
||
| 206 | $_POST['category_id'] = 0; |
||
| 207 | $_POST['target'] = '_blank'; |
||
| 208 | |||
| 209 | Link::addlinkcategory("link"); |
||
| 210 | break; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | } |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | removeDir($destinationDir); |
||
| 218 | return $packageContent[$mainFileKey]; |
||
| 219 | } |
||
| 220 | |||
| 575 | } |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.