| Conditions | 32 |
| Paths | 2048 |
| Total Lines | 134 |
| Code Lines | 78 |
| 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 |
||
| 69 | public static function createBackup($course) |
||
| 70 | { |
||
| 71 | self::cleanBackupDir(); |
||
| 72 | self::createBackupDir(); |
||
| 73 | |||
| 74 | $perm_dirs = api_get_permissions_for_new_directories(); |
||
| 75 | $backupDirectory = self::getBackupDir(); |
||
| 76 | |||
| 77 | // Create a temp directory |
||
| 78 | $backup_dir = $backupDirectory.'CourseArchiver_'.api_get_unique_id().'/'; |
||
| 79 | |||
| 80 | // All course-information will be stored in course_info.dat |
||
| 81 | $course_info_file = $backup_dir.'course_info.dat'; |
||
| 82 | |||
| 83 | $user = api_get_user_info(); |
||
| 84 | $date = new \DateTime(api_get_local_time()); |
||
| 85 | $zipFileName = $user['user_id'].'_'.$course->code.'_'.$date->format('Ymd-His').'.zip'; |
||
| 86 | $zipFilePath = $backupDirectory.$zipFileName; |
||
| 87 | |||
| 88 | $php_errormsg = ''; |
||
| 89 | $res = @mkdir($backup_dir, $perm_dirs); |
||
| 90 | if ($res === false) { |
||
| 91 | //TODO set and handle an error message telling the user to review the permissions on the archive directory |
||
| 92 | error_log(__FILE__.' line '.__LINE__.': '.(ini_get('track_errors') != false ? $php_errormsg : 'error not recorded because track_errors is off in your php.ini').' - This error, occuring because your archive directory will not let this script write data into it, will prevent courses backups to be created', 0); |
||
|
|
|||
| 93 | } |
||
| 94 | // Write the course-object to the file |
||
| 95 | $fp = @fopen($course_info_file, 'w'); |
||
| 96 | if ($fp === false) { |
||
| 97 | error_log(__FILE__.' line '.__LINE__.': '.(ini_get('track_errors') != false ? $php_errormsg : 'error not recorded because track_errors is off in your php.ini'), 0); |
||
| 98 | } |
||
| 99 | |||
| 100 | $res = @fwrite($fp, base64_encode(serialize($course))); |
||
| 101 | if ($res === false) { |
||
| 102 | error_log(__FILE__.' line '.__LINE__.': '.(ini_get('track_errors') != false ? $php_errormsg : 'error not recorded because track_errors is off in your php.ini'), 0); |
||
| 103 | } |
||
| 104 | |||
| 105 | $res = @fclose($fp); |
||
| 106 | if ($res === false) { |
||
| 107 | error_log(__FILE__.' line '.__LINE__.': '.(ini_get('track_errors') != false ? $php_errormsg : 'error not recorded because track_errors is off in your php.ini'), 0); |
||
| 108 | } |
||
| 109 | |||
| 110 | // Copy all documents to the temp-dir |
||
| 111 | if (isset($course->resources[RESOURCE_DOCUMENT]) && is_array($course->resources[RESOURCE_DOCUMENT])) { |
||
| 112 | $webEditorCss = api_get_path(WEB_CSS_PATH).'editor.css'; |
||
| 113 | /** @var Document $document */ |
||
| 114 | foreach ($course->resources[RESOURCE_DOCUMENT] as $document) { |
||
| 115 | if ($document->file_type == DOCUMENT) { |
||
| 116 | $doc_dir = $backup_dir.$document->path; |
||
| 117 | @mkdir(dirname($doc_dir), $perm_dirs, true); |
||
| 118 | if (file_exists($course->path.$document->path)) { |
||
| 119 | copy($course->path.$document->path, $doc_dir); |
||
| 120 | // Check if is html or htm |
||
| 121 | $extension = pathinfo(basename($document->path), PATHINFO_EXTENSION); |
||
| 122 | switch ($extension) { |
||
| 123 | case 'html': |
||
| 124 | case 'htm': |
||
| 125 | $contents = file_get_contents($doc_dir); |
||
| 126 | $contents = str_replace( |
||
| 127 | $webEditorCss, |
||
| 128 | '{{css_editor}}', |
||
| 129 | $contents |
||
| 130 | ); |
||
| 131 | file_put_contents($doc_dir, $contents); |
||
| 132 | break; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } else { |
||
| 136 | @mkdir($backup_dir.$document->path, $perm_dirs, true); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | // Copy all scorm documents to the temp-dir |
||
| 142 | if (isset($course->resources[RESOURCE_SCORM]) && is_array($course->resources[RESOURCE_SCORM])) { |
||
| 143 | foreach ($course->resources[RESOURCE_SCORM] as $document) { |
||
| 144 | copyDirTo($course->path.$document->path, $backup_dir.$document->path, false); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | // Copy calendar attachments. |
||
| 149 | if (isset($course->resources[RESOURCE_EVENT]) && is_array($course->resources[RESOURCE_EVENT])) { |
||
| 150 | $doc_dir = dirname($backup_dir.'/upload/calendar/'); |
||
| 151 | @mkdir($doc_dir, $perm_dirs, true); |
||
| 152 | copyDirTo($course->path.'upload/calendar/', $doc_dir, false); |
||
| 153 | } |
||
| 154 | |||
| 155 | // Copy Learning path author image. |
||
| 156 | if (isset($course->resources[RESOURCE_LEARNPATH]) && is_array($course->resources[RESOURCE_LEARNPATH])) { |
||
| 157 | $doc_dir = dirname($backup_dir.'/upload/learning_path/'); |
||
| 158 | @mkdir($doc_dir, $perm_dirs, true); |
||
| 159 | copyDirTo($course->path.'upload/learning_path/', $doc_dir, false); |
||
| 160 | } |
||
| 161 | |||
| 162 | // Copy announcements attachments. |
||
| 163 | if (isset($course->resources[RESOURCE_ANNOUNCEMENT]) && is_array($course->resources[RESOURCE_ANNOUNCEMENT])) { |
||
| 164 | $doc_dir = dirname($backup_dir.'/upload/announcements/'); |
||
| 165 | @mkdir($doc_dir, $perm_dirs, true); |
||
| 166 | copyDirTo($course->path.'upload/announcements/', $doc_dir, false); |
||
| 167 | } |
||
| 168 | |||
| 169 | // Copy work folders (only folders) |
||
| 170 | if (isset($course->resources[RESOURCE_WORK]) && is_array($course->resources[RESOURCE_WORK])) { |
||
| 171 | $doc_dir = $backup_dir.'work'; |
||
| 172 | @mkdir($doc_dir, $perm_dirs, true); |
||
| 173 | copyDirWithoutFilesTo($course->path.'work/', $doc_dir); |
||
| 174 | } |
||
| 175 | |||
| 176 | if (isset($course->resources[RESOURCE_ASSET]) && is_array($course->resources[RESOURCE_ASSET])) { |
||
| 177 | /** @var Asset $asset */ |
||
| 178 | foreach ($course->resources[RESOURCE_ASSET] as $asset) { |
||
| 179 | $doc_dir = $backup_dir.$asset->path; |
||
| 180 | @mkdir(dirname($doc_dir), $perm_dirs, true); |
||
| 181 | $assetPath = $course->path.$asset->path; |
||
| 182 | |||
| 183 | if (!file_exists($assetPath)) { |
||
| 184 | continue; |
||
| 185 | } |
||
| 186 | |||
| 187 | if (is_dir($course->path.$asset->path)) { |
||
| 188 | copyDirTo($course->path.$asset->path, $doc_dir, false); |
||
| 189 | continue; |
||
| 190 | } |
||
| 191 | copy($course->path.$asset->path, $doc_dir); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | // Zip the course-contents |
||
| 196 | $zip = new \PclZip($zipFilePath); |
||
| 197 | $zip->create($backup_dir, PCLZIP_OPT_REMOVE_PATH, $backup_dir); |
||
| 198 | |||
| 199 | // Remove the temp-dir. |
||
| 200 | rmdirr($backup_dir); |
||
| 201 | |||
| 202 | return $zipFileName; |
||
| 203 | } |
||
| 362 |