| Conditions | 29 |
| Paths | 2048 |
| Total Lines | 114 |
| Code Lines | 64 |
| Lines | 40 |
| Ratio | 35.09 % |
| 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 |
||
| 65 | public static function createBackup($course) |
||
| 66 | { |
||
| 67 | CourseArchiver::cleanBackupDir(); |
||
| 68 | CourseArchiver::createBackupDir(); |
||
| 69 | |||
| 70 | $perm_dirs = api_get_permissions_for_new_directories(); |
||
| 71 | $backupDirectory = self::getBackupDir(); |
||
| 72 | |||
| 73 | // Create a temp directory |
||
| 74 | $backup_dir = $backupDirectory . 'CourseArchiver_' . api_get_unique_id() . '/'; |
||
| 75 | |||
| 76 | // All course-information will be stored in course_info.dat |
||
| 77 | $course_info_file = $backup_dir . 'course_info.dat'; |
||
| 78 | |||
| 79 | $user = api_get_user_info(); |
||
| 80 | $date = new \DateTime(api_get_local_time()); |
||
| 81 | $zipFileName = $user['user_id'] . '_' . $course->code . '_' . $date->format('Ymd-His') . '.zip'; |
||
| 82 | $zipFilePath = $backupDirectory. $zipFileName; |
||
| 83 | |||
| 84 | $php_errormsg = ''; |
||
| 85 | $res = @mkdir($backup_dir, $perm_dirs); |
||
| 86 | View Code Duplication | if ($res === false) { |
|
| 87 | //TODO set and handle an error message telling the user to review the permissions on the archive directory |
||
| 88 | 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); |
||
|
|
|||
| 89 | } |
||
| 90 | // Write the course-object to the file |
||
| 91 | $fp = @fopen($course_info_file, 'w'); |
||
| 92 | View Code Duplication | if ($fp === false) { |
|
| 93 | 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); |
||
| 94 | } |
||
| 95 | |||
| 96 | $res = @fwrite($fp, base64_encode(serialize($course))); |
||
| 97 | View Code Duplication | if ($res === false) { |
|
| 98 | 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); |
||
| 99 | } |
||
| 100 | |||
| 101 | $res = @fclose($fp); |
||
| 102 | View Code Duplication | if ($res === false) { |
|
| 103 | 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); |
||
| 104 | } |
||
| 105 | |||
| 106 | // Copy all documents to the temp-dir |
||
| 107 | if (isset($course->resources[RESOURCE_DOCUMENT]) && is_array($course->resources[RESOURCE_DOCUMENT])) { |
||
| 108 | foreach ($course->resources[RESOURCE_DOCUMENT] as $document) { |
||
| 109 | if ($document->file_type == DOCUMENT) { |
||
| 110 | $doc_dir = $backup_dir . $document->path; |
||
| 111 | @mkdir(dirname($doc_dir), $perm_dirs, true); |
||
| 112 | View Code Duplication | if (file_exists($course->path . $document->path)) { |
|
| 113 | copy($course->path . $document->path, $doc_dir); |
||
| 114 | } |
||
| 115 | } else { |
||
| 116 | @mkdir($backup_dir . $document->path, $perm_dirs, true); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | // Copy all scorm documents to the temp-dir |
||
| 122 | if (isset($course->resources[RESOURCE_SCORM]) && is_array($course->resources[RESOURCE_SCORM])) { |
||
| 123 | foreach ($course->resources[RESOURCE_SCORM] as $document) { |
||
| 124 | $doc_dir = dirname($backup_dir . $document->path); |
||
| 125 | @mkdir($doc_dir, $perm_dirs, true); |
||
| 126 | copyDirTo($course->path . $document->path, $doc_dir, false); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | // Copy calendar attachments. |
||
| 131 | |||
| 132 | View Code Duplication | if (isset($course->resources[RESOURCE_EVENT]) && is_array($course->resources[RESOURCE_EVENT])) { |
|
| 133 | $doc_dir = dirname($backup_dir . '/upload/calendar/'); |
||
| 134 | @mkdir($doc_dir, $perm_dirs, true); |
||
| 135 | copyDirTo($course->path . 'upload/calendar/', $doc_dir, false); |
||
| 136 | } |
||
| 137 | |||
| 138 | // Copy Learning path author image. |
||
| 139 | View Code Duplication | if (isset($course->resources[RESOURCE_LEARNPATH]) && is_array($course->resources[RESOURCE_LEARNPATH])) { |
|
| 140 | $doc_dir = dirname($backup_dir . '/upload/learning_path/'); |
||
| 141 | @mkdir($doc_dir, $perm_dirs, true); |
||
| 142 | copyDirTo($course->path . 'upload/learning_path/', $doc_dir, false); |
||
| 143 | } |
||
| 144 | |||
| 145 | // Copy announcements attachments. |
||
| 146 | View Code Duplication | if (isset($course->resources[RESOURCE_ANNOUNCEMENT]) && is_array($course->resources[RESOURCE_ANNOUNCEMENT])) { |
|
| 147 | $doc_dir = dirname($backup_dir . '/upload/announcements/'); |
||
| 148 | @mkdir($doc_dir, $perm_dirs, true); |
||
| 149 | copyDirTo($course->path . 'upload/announcements/', $doc_dir, false); |
||
| 150 | } |
||
| 151 | |||
| 152 | // Copy work folders (only folders) |
||
| 153 | View Code Duplication | if (isset($course->resources[RESOURCE_WORK]) && is_array($course->resources[RESOURCE_WORK])) { |
|
| 154 | $doc_dir = dirname($backup_dir . '/upload/work/'); |
||
| 155 | @mkdir($doc_dir, $perm_dirs, true); |
||
| 156 | // @todo: adjust to only create subdirs, but not copy files |
||
| 157 | copyDirTo($course->path . 'upload/work/', $doc_dir, false); |
||
| 158 | } |
||
| 159 | |||
| 160 | if (isset($course->resources[RESOURCE_ASSET]) && is_array($course->resources[RESOURCE_ASSET])) { |
||
| 161 | /** @var Asset $asset */ |
||
| 162 | foreach ($course->resources[RESOURCE_ASSET] as $asset) { |
||
| 163 | $doc_dir = $backup_dir . $asset->path; |
||
| 164 | @mkdir(dirname($doc_dir), $perm_dirs, true); |
||
| 165 | View Code Duplication | if (file_exists($course->path . $asset->path)) { |
|
| 166 | copy($course->path . $asset->path, $doc_dir); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | // Zip the course-contents |
||
| 172 | $zip = new \PclZip($zipFilePath); |
||
| 173 | $zip->create($backup_dir, PCLZIP_OPT_REMOVE_PATH, $backup_dir); |
||
| 174 | |||
| 175 | // Remove the temp-dir. |
||
| 176 | rmdirr($backup_dir); |
||
| 177 | return $zipFileName; |
||
| 178 | } |
||
| 179 | |||
| 316 |