Conditions | 46 |
Paths | > 20000 |
Total Lines | 368 |
Code Lines | 243 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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 import($uploadedFile) |
||
23 | { |
||
24 | $debug = false; |
||
25 | if (UPLOAD_ERR_OK !== $uploadedFile['error']) { |
||
26 | throw new Exception(get_lang('UploadError')); |
||
27 | } |
||
28 | |||
29 | $cachePath = api_get_path(SYS_ARCHIVE_PATH); |
||
30 | $tempPath = $uploadedFile['tmp_name']; |
||
31 | $nameParts = explode('.', $uploadedFile['name']); |
||
32 | $extension = array_pop($nameParts); |
||
33 | $name = basename($tempPath).".$extension"; |
||
34 | |||
35 | if (!move_uploaded_file($tempPath, api_get_path(SYS_ARCHIVE_PATH).$name)) { |
||
36 | throw new Exception(get_lang('UploadError')); |
||
37 | } |
||
38 | |||
39 | $filePath = $cachePath.$name; |
||
40 | if (!is_readable($filePath)) { |
||
41 | throw new Exception(get_lang('UploadError')); |
||
42 | } |
||
43 | |||
44 | $mimeType = mime_content_type($filePath); |
||
45 | $folder = api_get_unique_id(); |
||
46 | $destinationDir = api_get_path(SYS_ARCHIVE_PATH).$folder; |
||
47 | |||
48 | mkdir($destinationDir, api_get_permissions_for_new_directories(), true); |
||
49 | |||
50 | switch ($mimeType) { |
||
51 | case 'application/gzip': |
||
52 | case 'application/x-gzip': |
||
53 | $backUpFile = new PharData($filePath); |
||
54 | |||
55 | if (false === $backUpFile->extractTo($destinationDir)) { |
||
56 | throw new Exception(get_lang('ErrorImportingFile')); |
||
57 | } |
||
58 | |||
59 | if (!file_exists($destinationDir.'/moodle_backup.xml')) { |
||
60 | throw new Exception(get_lang('FailedToImportThisIsNotAMoodleFile')); |
||
61 | } |
||
62 | |||
63 | break; |
||
64 | case 'application/zip': |
||
65 | $package = new PclZip($filePath); |
||
66 | $mainFileKey = 0; |
||
67 | $packageContent = $package->listContent(); |
||
68 | |||
69 | if (!empty($packageContent)) { |
||
70 | foreach ($packageContent as $index => $value) { |
||
71 | if ($value['filename'] === 'moodle_backup.xml') { |
||
72 | $mainFileKey = $index; |
||
73 | break; |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | |||
78 | if (!$mainFileKey) { |
||
79 | throw new Exception(get_lang('FailedToImportThisIsNotAMoodleFile')); |
||
80 | } |
||
81 | |||
82 | $package->extract(PCLZIP_OPT_PATH, $destinationDir); |
||
83 | |||
84 | break; |
||
85 | } |
||
86 | |||
87 | $courseInfo = api_get_course_info(); |
||
88 | $sessionId = api_get_session_id(); |
||
89 | $groupId = api_get_group_id(); |
||
90 | $documentPath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document'; |
||
91 | |||
92 | create_unexisting_directory( |
||
93 | $courseInfo, |
||
94 | api_get_user_id(), |
||
95 | $sessionId, |
||
96 | $groupId, |
||
97 | null, |
||
98 | $documentPath, |
||
99 | '/moodle', |
||
100 | 'Moodle Docs', |
||
101 | 0 |
||
102 | ); |
||
103 | |||
104 | // This process will upload all question resource files |
||
105 | $filesXml = @file_get_contents($destinationDir.'/files.xml'); |
||
106 | $mainFileModuleValues = $this->getAllQuestionFiles($filesXml); |
||
107 | $currentResourceFilePath = $destinationDir.'/files/'; |
||
108 | $importedFiles = []; |
||
109 | if ($debug) { |
||
110 | error_log('loading files'); |
||
111 | } |
||
112 | |||
113 | $_POST['moodle_import'] = true; |
||
114 | $_POST['language'] = $courseInfo['language']; |
||
115 | |||
116 | foreach ($mainFileModuleValues as $fileInfo) { |
||
117 | $dirs = new RecursiveDirectoryIterator($currentResourceFilePath); |
||
118 | foreach (new RecursiveIteratorIterator($dirs) as $file) { |
||
119 | if (!is_file($file) || false === strpos($file, $fileInfo['contenthash'])) { |
||
120 | continue; |
||
121 | } |
||
122 | |||
123 | if (isset($importedFiles[$fileInfo['filename']])) { |
||
124 | continue; |
||
125 | } |
||
126 | |||
127 | if ($debug) { |
||
128 | error_log($fileInfo['filename']); |
||
129 | } |
||
130 | $files = []; |
||
131 | $files['file']['name'] = $fileInfo['filename']; |
||
132 | $files['file']['tmp_name'] = $file->getPathname(); |
||
133 | $files['file']['type'] = $fileInfo['mimetype']; |
||
134 | $files['file']['error'] = 0; |
||
135 | $files['file']['size'] = $fileInfo['filesize']; |
||
136 | $files['file']['from_file'] = true; |
||
137 | $files['file']['move_file'] = true; |
||
138 | |||
139 | $title = isset($fileInfo['title']) ? $fileInfo['title'] : pathinfo($fileInfo['filename'], PATHINFO_FILENAME); |
||
140 | |||
141 | $data = DocumentManager::upload_document( |
||
142 | $files, |
||
143 | '/moodle', |
||
144 | $title, |
||
145 | '', |
||
146 | null, |
||
147 | 'overwrite', |
||
148 | true, |
||
149 | true, |
||
150 | 'file', |
||
151 | false |
||
152 | ); |
||
153 | |||
154 | if ($data) { |
||
155 | $importedFiles[$fileInfo['filename']] = basename($data['path']); |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | |||
160 | $xml = @file_get_contents($destinationDir.'/moodle_backup.xml'); |
||
161 | $doc = new DOMDocument(); |
||
162 | $res = @$doc->loadXML($xml); |
||
163 | |||
164 | if (empty($res)) { |
||
165 | removeDir($destinationDir); |
||
166 | unlink($filePath); |
||
167 | |||
168 | throw new Exception(get_lang('FailedToImportThisIsNotAMoodleFile')); |
||
169 | } |
||
170 | $activities = $doc->getElementsByTagName('activity'); |
||
171 | |||
172 | require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'; |
||
173 | |||
174 | if ($debug) { |
||
175 | error_log('Loading activities: '.count($activities)); |
||
176 | } |
||
177 | |||
178 | foreach ($activities as $activity) { |
||
179 | if (empty($activity->childNodes->length)) { |
||
180 | continue; |
||
181 | } |
||
182 | |||
183 | $currentItem = []; |
||
184 | foreach ($activity->childNodes as $item) { |
||
185 | $currentItem[$item->nodeName] = $item->nodeValue; |
||
186 | } |
||
187 | |||
188 | $moduleName = isset($currentItem['modulename']) ? $currentItem['modulename'] : false; |
||
189 | if ($debug) { |
||
190 | error_log('moduleName: '.$moduleName); |
||
191 | } |
||
192 | |||
193 | switch ($moduleName) { |
||
194 | case 'forum': |
||
195 | $catForumValues = []; |
||
196 | // Read the current forum module xml. |
||
197 | $moduleDir = $currentItem['directory']; |
||
198 | $moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml'); |
||
199 | $moduleValues = $this->readForumModule($moduleXml); |
||
200 | |||
201 | // Create a Forum category based on Moodle forum type. |
||
202 | $catForumValues['forum_category_title'] = $moduleValues['type']; |
||
203 | $catForumValues['forum_category_comment'] = ''; |
||
204 | $catId = store_forumcategory( |
||
205 | $catForumValues, |
||
206 | $courseInfo, |
||
207 | false |
||
208 | ); |
||
209 | $forumValues = []; |
||
210 | $forumValues['forum_title'] = $moduleValues['name']; |
||
211 | $forumValues['forum_image'] = ''; |
||
212 | $forumValues['forum_comment'] = $moduleValues['intro']; |
||
213 | $forumValues['forum_category'] = $catId; |
||
214 | $forumValues['moderated'] = 0; |
||
215 | |||
216 | store_forum($forumValues, $courseInfo); |
||
217 | break; |
||
218 | case 'quiz': |
||
219 | // Read the current quiz module xml. |
||
220 | // The quiz case is the very complicate process of all the import. |
||
221 | // Please if you want to review the script, try to see the readingXML functions. |
||
222 | // The readingXML functions in this clases do all the mayor work here. |
||
223 | |||
224 | $moduleDir = $currentItem['directory']; |
||
225 | $moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml'); |
||
226 | $questionsXml = @file_get_contents($destinationDir.'/questions.xml'); |
||
227 | $moduleValues = $this->readQuizModule($moduleXml); |
||
228 | |||
229 | // At this point we got all the prepared resources from Moodle file |
||
230 | // $moduleValues variable contains all the necesary info to the quiz import |
||
231 | // var_dump($moduleValues); // <-- uncomment this to see the final array |
||
232 | |||
233 | $exercise = new Exercise($courseInfo['real_id']); |
||
234 | if ($debug) { |
||
235 | error_log('quiz:'.$moduleValues['name']); |
||
236 | } |
||
237 | |||
238 | $title = Exercise::format_title_variable($moduleValues['name']); |
||
239 | $exercise->updateTitle($title); |
||
240 | $exercise->updateDescription($moduleValues['intro']); |
||
241 | $exercise->updateAttempts($moduleValues['attempts_number']); |
||
242 | $exercise->updateFeedbackType(0); |
||
243 | |||
244 | // Match shuffle question with chamilo |
||
245 | if (isset($moduleValues['shufflequestions']) && |
||
246 | (int) $moduleValues['shufflequestions'] === 1 |
||
247 | ) { |
||
248 | $exercise->setRandom(-1); |
||
249 | } else { |
||
250 | $exercise->setRandom(0); |
||
251 | } |
||
252 | $exercise->updateRandomAnswers(!empty($moduleValues['shuffleanswers'])); |
||
253 | // @todo divide to minutes |
||
254 | $exercise->updateExpiredTime((int) $moduleValues['timelimit']); |
||
255 | |||
256 | if ($moduleValues['questionsperpage'] == 1) { |
||
257 | $exercise->updateType(2); |
||
258 | } else { |
||
259 | $exercise->updateType(1); |
||
260 | } |
||
261 | |||
262 | // Create the new Quiz |
||
263 | $exercise->save(); |
||
264 | |||
265 | // Ok, we got the Quiz and create it, now its time to add the Questions |
||
266 | foreach ($moduleValues['question_instances'] as $index => $question) { |
||
267 | $questionsValues = $this->readMainQuestionsXml($questionsXml, $question['questionid']); |
||
268 | $moduleValues['question_instances'][$index] = $questionsValues; |
||
269 | // Set Question Type from Moodle XML element <qtype> |
||
270 | $qType = $moduleValues['question_instances'][$index]['qtype']; |
||
271 | // Add the matched chamilo question type to the array |
||
272 | $moduleValues['question_instances'][$index]['chamilo_qtype'] = |
||
273 | $this->matchMoodleChamiloQuestionTypes($qType); |
||
274 | $questionInstance = Question::getInstance( |
||
275 | $moduleValues['question_instances'][$index]['chamilo_qtype'] |
||
276 | ); |
||
277 | |||
278 | if (empty($questionInstance)) { |
||
279 | continue; |
||
280 | } |
||
281 | if ($debug) { |
||
282 | error_log('question: '.$question['questionid']); |
||
283 | } |
||
284 | |||
285 | $questionInstance->updateTitle($moduleValues['question_instances'][$index]['name']); |
||
286 | $questionText = $moduleValues['question_instances'][$index]['questiontext']; |
||
287 | |||
288 | // Replace the path from @@PLUGINFILE@@ to a correct chamilo path |
||
289 | $questionText = str_replace( |
||
290 | '@@PLUGINFILE@@', |
||
291 | '/courses/'.$courseInfo['path'].'/document/moodle', |
||
292 | $questionText |
||
293 | ); |
||
294 | |||
295 | if ($importedFiles) { |
||
296 | $this->fixPathInText($importedFiles, $questionText); |
||
297 | } |
||
298 | |||
299 | $questionInstance->updateDescription($questionText); |
||
300 | $questionInstance->updateLevel(1); |
||
301 | $questionInstance->updateCategory(0); |
||
302 | |||
303 | //Save normal question if NOT media |
||
304 | if ($questionInstance->type != MEDIA_QUESTION) { |
||
305 | $questionInstance->save($exercise); |
||
306 | // modify the exercise |
||
307 | $exercise->addToList($questionInstance->id); |
||
308 | $exercise->update_question_positions(); |
||
309 | } |
||
310 | |||
311 | $questionList = $moduleValues['question_instances'][$index]['plugin_qtype_'.$qType.'_question']; |
||
312 | $currentQuestion = $moduleValues['question_instances'][$index]; |
||
313 | |||
314 | $this->processAnswers( |
||
315 | $exercise, |
||
316 | $questionList, |
||
317 | $qType, |
||
318 | $questionInstance, |
||
319 | $currentQuestion, |
||
320 | $importedFiles |
||
321 | ); |
||
322 | } |
||
323 | break; |
||
324 | case 'resource': |
||
325 | // Read the current resource module xml. |
||
326 | $moduleDir = $currentItem['directory']; |
||
327 | $moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml'); |
||
328 | $filesXml = @file_get_contents($destinationDir.'/files.xml'); |
||
329 | $moduleValues = $this->readResourceModule($moduleXml); |
||
330 | $mainFileModuleValues = $this->readMainFilesXml( |
||
331 | $filesXml, |
||
332 | $moduleValues['contextid'] |
||
333 | ); |
||
334 | $fileInfo = array_merge($moduleValues, $mainFileModuleValues, $currentItem); |
||
335 | $currentResourceFilePath = $destinationDir.'/files/'; |
||
336 | $dirs = new RecursiveDirectoryIterator($currentResourceFilePath); |
||
337 | foreach (new RecursiveIteratorIterator($dirs) as $file) { |
||
338 | if (!is_file($file) || false === strpos($file, $fileInfo['contenthash'])) { |
||
339 | continue; |
||
340 | } |
||
341 | |||
342 | $files = []; |
||
343 | $files['file']['name'] = $fileInfo['filename']; |
||
344 | $files['file']['tmp_name'] = $file->getPathname(); |
||
345 | $files['file']['type'] = $fileInfo['mimetype']; |
||
346 | $files['file']['error'] = 0; |
||
347 | $files['file']['size'] = $fileInfo['filesize']; |
||
348 | $files['file']['from_file'] = true; |
||
349 | $files['file']['move_file'] = true; |
||
350 | $_POST['language'] = $courseInfo['language']; |
||
351 | $_POST['moodle_import'] = true; |
||
352 | |||
353 | DocumentManager::upload_document( |
||
354 | $files, |
||
355 | '/moodle', |
||
356 | $fileInfo['title'], |
||
357 | '', |
||
358 | null, |
||
359 | null, |
||
360 | true, |
||
361 | true |
||
362 | ); |
||
363 | } |
||
364 | |||
365 | break; |
||
366 | case 'url': |
||
367 | // Read the current url module xml. |
||
368 | $moduleDir = $currentItem['directory']; |
||
369 | $moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml'); |
||
370 | $moduleValues = $this->readUrlModule($moduleXml); |
||
371 | $_POST['title'] = $moduleValues['name']; |
||
372 | $_POST['url'] = $moduleValues['externalurl']; |
||
373 | $_POST['description'] = $moduleValues['intro']; |
||
374 | $_POST['category_id'] = 0; |
||
375 | $_POST['target'] = '_blank'; |
||
376 | |||
377 | Link::addlinkcategory('link'); |
||
378 | break; |
||
379 | } |
||
380 | } |
||
381 | |||
382 | if ($debug) { |
||
383 | error_log('Finish'); |
||
384 | } |
||
385 | |||
386 | removeDir($destinationDir); |
||
387 | unlink($filePath); |
||
388 | |||
389 | return true; |
||
390 | } |
||
1188 |