Conditions | 24 |
Paths | 739 |
Total Lines | 211 |
Code Lines | 142 |
Lines | 0 |
Ratio | 0 % |
Changes | 8 | ||
Bugs | 1 | Features | 2 |
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 'forum': |
||
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); // <-- 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 | |||
110 | // Match shuffle question with chamilo |
||
111 | switch ($moduleValues['shufflequestions']) { |
||
112 | case '0': |
||
113 | $exercise->setRandom(0); |
||
114 | break; |
||
115 | case '1': |
||
116 | $exercise->setRandom(-1); |
||
117 | break; |
||
118 | default: |
||
119 | $exercise->setRandom(0); |
||
120 | } |
||
121 | $exercise->updateRandomAnswers($moduleValues['shuffleanswers']); |
||
122 | // @todo divide to minutes |
||
123 | $exercise->updateExpiredTime($moduleValues['timelimit']); |
||
124 | |||
125 | if ($moduleValues['questionsperpage'] == 1) { |
||
126 | $exercise->updateType(2); |
||
127 | } else { |
||
128 | $exercise->updateType(1); |
||
129 | } |
||
130 | |||
131 | // Create the new Quiz |
||
132 | $exercise->save(); |
||
133 | |||
134 | // Ok, we got the Quiz and create it, now its time to add the Questions |
||
135 | foreach ($moduleValues['question_instances'] as $index => $question) { |
||
136 | $questionsValues = $this->readMainQuestionsXml($questionsXml, $question['questionid']); |
||
137 | $moduleValues['question_instances'][$index] = $questionsValues; |
||
138 | // Set Question Type from Moodle XML element <qtype> |
||
139 | $qType = $moduleValues['question_instances'][$index]['qtype']; |
||
140 | // Add the matched chamilo question type to the array |
||
141 | $moduleValues['question_instances'][$index]['chamilo_qtype'] = $this->matchMoodleChamiloQuestionTypes($qType); |
||
142 | $questionInstance = Question::getInstance($moduleValues['question_instances'][$index]['chamilo_qtype']); |
||
143 | if ($questionInstance) { |
||
144 | $questionInstance->updateTitle($moduleValues['question_instances'][$index]['name']); |
||
145 | $questionInstance->updateDescription($moduleValues['question_instances'][$index]['questiontext']); |
||
146 | $questionInstance->updateLevel(1); |
||
147 | $questionInstance->updateCategory(0); |
||
148 | |||
149 | //Save normal question if NOT media |
||
150 | if ($questionInstance->type != MEDIA_QUESTION) { |
||
151 | $questionInstance->save($exercise->id); |
||
152 | |||
153 | // modify the exercise |
||
154 | $exercise->addToList($questionInstance->id); |
||
155 | $exercise->update_question_positions(); |
||
156 | } |
||
157 | |||
158 | $questionList = $moduleValues['question_instances'][$index]['plugin_qtype_'.$qType.'_question']; |
||
159 | $currentQuestion = $moduleValues['question_instances'][$index]; |
||
160 | |||
161 | $result = $this->processAnswers($questionList, $qType, $questionInstance, $currentQuestion); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | break; |
||
166 | case 'resource': |
||
167 | // Read the current resource module xml. |
||
168 | $moduleDir = $currentItem['directory']; |
||
169 | $moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml'); |
||
170 | $filesXml = @file_get_contents($destinationDir.'/files.xml'); |
||
171 | $moduleValues = $this->readResourceModule($moduleXml); |
||
172 | $mainFileModuleValues = $this->readMainFilesXml($filesXml, $moduleValues['contextid']); |
||
173 | $fileInfo = array_merge($moduleValues, $mainFileModuleValues, $currentItem); |
||
174 | $documentPath = $coursePath.'document/'; |
||
175 | $currentResourceFilePath = $destinationDir.'/files/'; |
||
176 | $dirs = new RecursiveDirectoryIterator($currentResourceFilePath); |
||
177 | foreach (new RecursiveIteratorIterator($dirs) as $file) { |
||
178 | if (is_file($file) && strpos($file, $fileInfo['contenthash']) !== false) { |
||
179 | $files = []; |
||
180 | $files['file']['name'] = $fileInfo['filename']; |
||
181 | $files['file']['tmp_name'] = $file->getPathname(); |
||
182 | $files['file']['type'] = $fileInfo['mimetype']; |
||
183 | $files['file']['error'] = 0; |
||
184 | $files['file']['size'] = $fileInfo['filesize']; |
||
185 | $files['file']['from_file'] = true; |
||
186 | $files['file']['move_file'] = true; |
||
187 | $_POST['language'] = $courseInfo['language']; |
||
188 | $_POST['moodle_import'] = true; |
||
189 | |||
190 | DocumentManager::upload_document( |
||
191 | $files, |
||
192 | '/', |
||
193 | $fileInfo['title'], |
||
194 | '', |
||
195 | null, |
||
196 | null, |
||
197 | true, |
||
198 | true |
||
199 | ); |
||
200 | } |
||
201 | } |
||
202 | |||
203 | break; |
||
204 | case 'url': |
||
205 | // Read the current url module xml. |
||
206 | $moduleDir = $currentItem['directory']; |
||
207 | $moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml'); |
||
208 | $moduleValues = $this->readUrlModule($moduleXml); |
||
209 | $_POST['title'] = $moduleValues['name']; |
||
210 | $_POST['url'] = $moduleValues['externalurl']; |
||
211 | $_POST['description'] = $moduleValues['intro']; |
||
212 | $_POST['category_id'] = 0; |
||
213 | $_POST['target'] = '_blank'; |
||
214 | |||
215 | Link::addlinkcategory("link"); |
||
216 | break; |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | } else { |
||
221 | return false; |
||
222 | } |
||
223 | } else { |
||
224 | return false; |
||
225 | } |
||
226 | |||
227 | removeDir($destinationDir); |
||
228 | return $packageContent[$mainFileKey]; |
||
229 | } |
||
230 | |||
733 | } |
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.