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