| Conditions | 46 |
| Paths | > 20000 |
| Total Lines | 364 |
| Code Lines | 241 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 = $questionsValues['qtype']; |
||
| 271 | $questionType = $this->matchMoodleChamiloQuestionTypes($questionsValues); |
||
| 272 | $questionInstance = Question::getInstance($questionType); |
||
| 273 | |||
| 274 | if (empty($questionInstance)) { |
||
| 275 | continue; |
||
| 276 | } |
||
| 277 | if ($debug) { |
||
| 278 | error_log('question: '.$question['questionid']); |
||
| 279 | } |
||
| 280 | |||
| 281 | $questionInstance->updateTitle($questionsValues['name']); |
||
| 282 | $questionText = $questionsValues['questiontext']; |
||
| 283 | |||
| 284 | // Replace the path from @@PLUGINFILE@@ to a correct chamilo path |
||
| 285 | $questionText = str_replace( |
||
| 286 | '@@PLUGINFILE@@', |
||
| 287 | '/courses/'.$courseInfo['path'].'/document/moodle', |
||
| 288 | $questionText |
||
| 289 | ); |
||
| 290 | |||
| 291 | if ($importedFiles) { |
||
| 292 | $this->fixPathInText($importedFiles, $questionText); |
||
| 293 | } |
||
| 294 | |||
| 295 | $questionInstance->updateDescription($questionText); |
||
| 296 | $questionInstance->updateLevel(1); |
||
| 297 | $questionInstance->updateCategory(0); |
||
| 298 | |||
| 299 | //Save normal question if NOT media |
||
| 300 | if ($questionInstance->type != MEDIA_QUESTION) { |
||
| 301 | $questionInstance->save($exercise); |
||
| 302 | // modify the exercise |
||
| 303 | $exercise->addToList($questionInstance->id); |
||
| 304 | $exercise->update_question_positions(); |
||
| 305 | } |
||
| 306 | |||
| 307 | $questionList = $moduleValues['question_instances'][$index]['plugin_qtype_'.$qType.'_question']; |
||
| 308 | $currentQuestion = $moduleValues['question_instances'][$index]; |
||
| 309 | |||
| 310 | $this->processAnswers( |
||
| 311 | $exercise, |
||
| 312 | $questionList, |
||
| 313 | $qType, |
||
| 314 | $questionInstance, |
||
| 315 | $currentQuestion, |
||
| 316 | $importedFiles |
||
| 317 | ); |
||
| 318 | } |
||
| 319 | break; |
||
| 320 | case 'resource': |
||
| 321 | // Read the current resource module xml. |
||
| 322 | $moduleDir = $currentItem['directory']; |
||
| 323 | $moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml'); |
||
| 324 | $filesXml = @file_get_contents($destinationDir.'/files.xml'); |
||
| 325 | $moduleValues = $this->readResourceModule($moduleXml); |
||
| 326 | $mainFileModuleValues = $this->readMainFilesXml( |
||
| 327 | $filesXml, |
||
| 328 | $moduleValues['contextid'] |
||
| 329 | ); |
||
| 330 | $fileInfo = array_merge($moduleValues, $mainFileModuleValues, $currentItem); |
||
| 331 | $currentResourceFilePath = $destinationDir.'/files/'; |
||
| 332 | $dirs = new RecursiveDirectoryIterator($currentResourceFilePath); |
||
| 333 | foreach (new RecursiveIteratorIterator($dirs) as $file) { |
||
| 334 | if (!is_file($file) || false === strpos($file, $fileInfo['contenthash'])) { |
||
| 335 | continue; |
||
| 336 | } |
||
| 337 | |||
| 338 | $files = []; |
||
| 339 | $files['file']['name'] = $fileInfo['filename']; |
||
| 340 | $files['file']['tmp_name'] = $file->getPathname(); |
||
| 341 | $files['file']['type'] = $fileInfo['mimetype']; |
||
| 342 | $files['file']['error'] = 0; |
||
| 343 | $files['file']['size'] = $fileInfo['filesize']; |
||
| 344 | $files['file']['from_file'] = true; |
||
| 345 | $files['file']['move_file'] = true; |
||
| 346 | $_POST['language'] = $courseInfo['language']; |
||
| 347 | $_POST['moodle_import'] = true; |
||
| 348 | |||
| 349 | DocumentManager::upload_document( |
||
| 350 | $files, |
||
| 351 | '/moodle', |
||
| 352 | $fileInfo['title'], |
||
| 353 | '', |
||
| 354 | null, |
||
| 355 | null, |
||
| 356 | true, |
||
| 357 | true |
||
| 358 | ); |
||
| 359 | } |
||
| 360 | |||
| 361 | break; |
||
| 362 | case 'url': |
||
| 363 | // Read the current url module xml. |
||
| 364 | $moduleDir = $currentItem['directory']; |
||
| 365 | $moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml'); |
||
| 366 | $moduleValues = $this->readUrlModule($moduleXml); |
||
| 367 | $_POST['title'] = $moduleValues['name']; |
||
| 368 | $_POST['url'] = $moduleValues['externalurl']; |
||
| 369 | $_POST['description'] = $moduleValues['intro']; |
||
| 370 | $_POST['category_id'] = 0; |
||
| 371 | $_POST['target'] = '_blank'; |
||
| 372 | |||
| 373 | Link::addlinkcategory('link'); |
||
| 374 | break; |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | if ($debug) { |
||
| 379 | error_log('Finish'); |
||
| 380 | } |
||
| 381 | |||
| 382 | removeDir($destinationDir); |
||
| 383 | unlink($filePath); |
||
| 384 | |||
| 385 | return true; |
||
| 386 | } |
||
| 1192 |