Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CourseRestorer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CourseRestorer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class CourseRestorer |
||
| 40 | { |
||
| 41 | /** |
||
| 42 | * The course-object |
||
| 43 | */ |
||
| 44 | public $course; |
||
| 45 | public $destination_course_info; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * What to do with files with same name (FILE_SKIP, FILE_RENAME or |
||
| 49 | * FILE_OVERWRITE) |
||
| 50 | */ |
||
| 51 | public $file_option; |
||
| 52 | public $set_tools_invisible_by_default; |
||
| 53 | public $skip_content; |
||
| 54 | public $tools_to_restore = array( |
||
| 55 | 'announcements', |
||
| 56 | 'attendance', |
||
| 57 | 'course_descriptions', |
||
| 58 | 'documents', |
||
| 59 | 'events', |
||
| 60 | 'forum_category', |
||
| 61 | 'forums', |
||
| 62 | // 'forum_topics', |
||
| 63 | 'glossary', |
||
| 64 | 'quizzes', |
||
| 65 | 'test_category', |
||
| 66 | 'links', |
||
| 67 | 'learnpaths', |
||
| 68 | 'surveys', |
||
| 69 | //'scorm_documents', ?? |
||
| 70 | 'tool_intro', |
||
| 71 | 'thematic', |
||
| 72 | 'wiki', |
||
| 73 | 'works', |
||
| 74 | 'gradebook', |
||
| 75 | ); |
||
| 76 | |||
| 77 | /** Setting per tool */ |
||
| 78 | public $tool_copy_settings = array(); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * If true adds the text "copy" in the title of an item (only for LPs right now) |
||
| 82 | * |
||
| 83 | **/ |
||
| 84 | public $add_text_in_items = false; |
||
| 85 | public $destination_course_id; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * CourseRestorer constructor. |
||
| 89 | * @param Course $course |
||
| 90 | */ |
||
| 91 | public function __construct($course) |
||
| 92 | { |
||
| 93 | $this->course = $course; |
||
| 94 | $course_info = api_get_course_info($this->course->code); |
||
| 95 | if (!empty($course_info)) { |
||
| 96 | $this->course_origin_id = $course_info['real_id']; |
||
| 97 | } else { |
||
| 98 | $this->course_origin_id = null; |
||
| 99 | } |
||
| 100 | $this->file_option = FILE_RENAME; |
||
| 101 | $this->set_tools_invisible_by_default = false; |
||
| 102 | $this->skip_content = array(); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Set the file-option |
||
| 107 | * @param int $option (optional) What to do with files with same name |
||
| 108 | * FILE_SKIP, FILE_RENAME or FILE_OVERWRITE |
||
| 109 | */ |
||
| 110 | public function set_file_option($option = FILE_OVERWRITE) |
||
| 111 | { |
||
| 112 | $this->file_option = $option; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param string $status |
||
| 117 | */ |
||
| 118 | public function set_add_text_in_items($status) |
||
| 119 | { |
||
| 120 | $this->add_text_in_items = $status; |
||
|
|
|||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param array $array |
||
| 125 | */ |
||
| 126 | public function set_tool_copy_settings($array) |
||
| 127 | { |
||
| 128 | $this->tool_copy_settings = $array; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Restore a course. |
||
| 133 | * |
||
| 134 | * @param string $destination_course_code code of the Chamilo-course in |
||
| 135 | * @param int $session_id |
||
| 136 | * @param bool $update_course_settings Course settings are going to be restore? |
||
| 137 | * @param bool $respect_base_content |
||
| 138 | * @return false|null |
||
| 139 | */ |
||
| 140 | public function restore( |
||
| 141 | $destination_course_code = '', |
||
| 142 | $session_id = 0, |
||
| 143 | $update_course_settings = false, |
||
| 144 | $respect_base_content = false |
||
| 145 | ) { |
||
| 146 | if ($destination_course_code == '') { |
||
| 147 | $course_info = api_get_course_info(); |
||
| 148 | $this->destination_course_info = $course_info; |
||
| 149 | $this->course->destination_path = $course_info['path']; |
||
| 150 | } else { |
||
| 151 | $course_info = api_get_course_info($destination_course_code); |
||
| 152 | $this->destination_course_info = $course_info; |
||
| 153 | $this->course->destination_path = $course_info['path']; |
||
| 154 | } |
||
| 155 | $this->destination_course_id = $course_info['real_id']; |
||
| 156 | |||
| 157 | //Getting first teacher (for the forums) |
||
| 158 | $teacher_list = CourseManager::get_teacher_list_from_course_code( |
||
| 159 | $course_info['code'] |
||
| 160 | ); |
||
| 161 | $this->first_teacher_id = api_get_user_id(); |
||
| 162 | |||
| 163 | if (!empty($teacher_list)) { |
||
| 164 | foreach ($teacher_list as $teacher) { |
||
| 165 | $this->first_teacher_id = $teacher['user_id']; |
||
| 166 | break; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | if (empty($this->course)) { |
||
| 171 | return false; |
||
| 172 | } |
||
| 173 | |||
| 174 | // Source platform encoding - reading/detection |
||
| 175 | // The correspondent data field has been added as of version 1.8.6.1 |
||
| 176 | |||
| 177 | if (empty($this->course->encoding)) { |
||
| 178 | // The archive has been created by a system which is prior to 1.8.6.1 version. |
||
| 179 | // In this case we have to detect the encoding. |
||
| 180 | $sample_text = $this->course->get_sample_text()."\n"; |
||
| 181 | // Let us exclude ASCII lines, probably they are English texts. |
||
| 182 | $sample_text = explode("\n", $sample_text); |
||
| 183 | foreach ($sample_text as $key => &$line) { |
||
| 184 | if (api_is_valid_ascii($line)) { |
||
| 185 | unset($sample_text[$key]); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | $sample_text = join("\n", $sample_text); |
||
| 189 | $this->course->encoding = api_detect_encoding($sample_text, $course_info['language']); |
||
| 190 | } |
||
| 191 | |||
| 192 | // Encoding conversion of the course, if it is needed. |
||
| 193 | $this->course->to_system_encoding(); |
||
| 194 | |||
| 195 | foreach ($this->tools_to_restore as $tool) { |
||
| 196 | $function_build = 'restore_'.$tool; |
||
| 197 | $this->$function_build($session_id, $respect_base_content, $destination_course_code); |
||
| 198 | } |
||
| 199 | |||
| 200 | if ($update_course_settings) { |
||
| 201 | $this->restore_course_settings($destination_course_code); |
||
| 202 | } |
||
| 203 | |||
| 204 | // Restore the item properties |
||
| 205 | $table = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 206 | |||
| 207 | foreach ($this->course->resources as $type => $resources) { |
||
| 208 | if (is_array($resources)) { |
||
| 209 | foreach ($resources as $id => $resource) { |
||
| 210 | if (isset($resource->item_properties)) { |
||
| 211 | foreach ($resource->item_properties as $property) { |
||
| 212 | // First check if there isn't already a record for this resource |
||
| 213 | $sql = "SELECT * FROM $table |
||
| 214 | WHERE |
||
| 215 | c_id = ".$this->destination_course_id." AND |
||
| 216 | tool = '".$property['tool']."' AND |
||
| 217 | ref = '".$resource->destination_id."'"; |
||
| 218 | |||
| 219 | $params = []; |
||
| 220 | if (!empty($session_id)) { |
||
| 221 | $params['session_id'] = intval($session_id); |
||
| 222 | } |
||
| 223 | |||
| 224 | $res = Database::query($sql); |
||
| 225 | if (Database::num_rows($res) == 0) { |
||
| 226 | /* The to_group_id and to_user_id are set to default |
||
| 227 | values as users/groups possibly not exist in |
||
| 228 | the target course*/ |
||
| 229 | |||
| 230 | $params['c_id'] = $this->destination_course_id; |
||
| 231 | $params['tool'] = self::DBUTF8( |
||
| 232 | $property['tool'] |
||
| 233 | ); |
||
| 234 | $property['insert_user_id'] = $this->checkUserId($property['insert_user_id']); |
||
| 235 | |||
| 236 | $params['insert_user_id'] = self::DBUTF8( |
||
| 237 | $property['insert_user_id'] |
||
| 238 | ); |
||
| 239 | $params['insert_date'] = self::DBUTF8( |
||
| 240 | $property['insert_date'] |
||
| 241 | ); |
||
| 242 | $params['lastedit_date'] = self::DBUTF8( |
||
| 243 | $property['lastedit_date'] |
||
| 244 | ); |
||
| 245 | $params['ref'] = $resource->destination_id; |
||
| 246 | $params['lastedit_type'] = self::DBUTF8( |
||
| 247 | $property['lastedit_type'] |
||
| 248 | ); |
||
| 249 | $params['lastedit_user_id'] = self::DBUTF8( |
||
| 250 | $property['lastedit_user_id'] |
||
| 251 | ); |
||
| 252 | $params['visibility'] = self::DBUTF8( |
||
| 253 | $property['visibility'] |
||
| 254 | ); |
||
| 255 | $params['start_visible'] = self::DBUTF8( |
||
| 256 | $property['start_visible'] |
||
| 257 | ); |
||
| 258 | $params['end_visible'] = self::DBUTF8( |
||
| 259 | $property['end_visible'] |
||
| 260 | ); |
||
| 261 | |||
| 262 | $property['to_user_id'] = $this->checkUserId($property['to_user_id'], true); |
||
| 263 | |||
| 264 | $params['to_user_id'] = self::DBUTF8( |
||
| 265 | $property['to_user_id'] |
||
| 266 | ); |
||
| 267 | //$params['to_group_id'] = 'NULL'; |
||
| 268 | |||
| 269 | $id = Database::insert($table, $params); |
||
| 270 | if ($id) { |
||
| 271 | $sql = "UPDATE $table SET id = iid WHERE iid = $id"; |
||
| 272 | Database::query($sql); |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 | } |
||
| 277 | } |
||
| 278 | } |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Restore only harmless course settings: |
||
| 284 | * course_language, visibility, department_name,department_url, |
||
| 285 | * subscribe, unsubscribe ,category_code |
||
| 286 | * |
||
| 287 | * @param string $destination_course_code |
||
| 288 | */ |
||
| 289 | public function restore_course_settings($destination_course_code) |
||
| 290 | { |
||
| 291 | $origin_course_info = api_get_course_info($destination_course_code); |
||
| 292 | $course_info = $this->course->info; |
||
| 293 | $params['course_language'] = $course_info['language']; |
||
| 294 | $params['visibility'] = $course_info['visibility']; |
||
| 295 | $params['department_name'] = $course_info['department_name']; |
||
| 296 | $params['department_url'] = $course_info['department_url']; |
||
| 297 | |||
| 298 | $params['category_code'] = $course_info['categoryCode']; |
||
| 299 | $params['subscribe'] = $course_info['subscribe_allowed']; |
||
| 300 | $params['unsubscribe'] = $course_info['unsubscribe']; |
||
| 301 | CourseManager::update_attributes($origin_course_info['real_id'], $params); |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Restore documents |
||
| 306 | * |
||
| 307 | * @param int $session_id |
||
| 308 | * @param bool $respect_base_content |
||
| 309 | * @param string $destination_course_code |
||
| 310 | */ |
||
| 311 | public function restore_documents($session_id = 0, $respect_base_content = false, $destination_course_code = '') |
||
| 312 | { |
||
| 313 | $course_info = api_get_course_info($destination_course_code); |
||
| 314 | |||
| 315 | if ($this->course->has_resources(RESOURCE_DOCUMENT)) { |
||
| 316 | $table = Database :: get_course_table(TABLE_DOCUMENT); |
||
| 317 | $resources = $this->course->resources; |
||
| 318 | $path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/'; |
||
| 319 | |||
| 320 | foreach ($resources[RESOURCE_DOCUMENT] as $id => $document) { |
||
| 321 | |||
| 322 | if (empty($document->item_properties[0]['id_session'])) { |
||
| 323 | $my_session_id = 0; |
||
| 324 | } else { |
||
| 325 | $my_session_id = $session_id; |
||
| 326 | } |
||
| 327 | |||
| 328 | if ($document->file_type == FOLDER) { |
||
| 329 | $visibility = $document->item_properties[0]['visibility']; |
||
| 330 | $new = substr($document->path, 8); |
||
| 331 | |||
| 332 | $folderList = explode('/', $new); |
||
| 333 | $tempFolder = ''; |
||
| 334 | |||
| 335 | // Check if the parent path exists. |
||
| 336 | foreach ($folderList as $folder) { |
||
| 337 | $folderToCreate = $tempFolder.$folder; |
||
| 338 | $sysFolderPath = $path.'document'.$folderToCreate; |
||
| 339 | $tempFolder .= $folder.'/'; |
||
| 340 | |||
| 341 | if (empty($folderToCreate)) { |
||
| 342 | continue; |
||
| 343 | } |
||
| 344 | |||
| 345 | $title = $document->title; |
||
| 346 | if (empty($title)) { |
||
| 347 | $title = basename($sysFolderPath); |
||
| 348 | } |
||
| 349 | |||
| 350 | // File doesn't exist in file system. |
||
| 351 | if (!is_dir($sysFolderPath)) { |
||
| 352 | // Creating directory |
||
| 353 | create_unexisting_directory( |
||
| 354 | $course_info, |
||
| 355 | api_get_user_id(), |
||
| 356 | $my_session_id, |
||
| 357 | 0, |
||
| 358 | 0, |
||
| 359 | $path.'document', |
||
| 360 | $folderToCreate, |
||
| 361 | $title, |
||
| 362 | $visibility |
||
| 363 | ); |
||
| 364 | } else { |
||
| 365 | // File exist in file system. |
||
| 366 | $documentData = DocumentManager::get_document_id( |
||
| 367 | $course_info, |
||
| 368 | $folderToCreate, |
||
| 369 | $my_session_id |
||
| 370 | ); |
||
| 371 | |||
| 372 | if (empty($documentData)) { |
||
| 373 | /* This means the folder exists in the |
||
| 374 | filesystem but not in the DB, trying to fix it */ |
||
| 375 | add_document( |
||
| 376 | $course_info, |
||
| 377 | $folderToCreate, |
||
| 378 | 'folder', |
||
| 379 | 0, |
||
| 380 | $title, |
||
| 381 | null, |
||
| 382 | null, |
||
| 383 | false, |
||
| 384 | null, |
||
| 385 | $my_session_id |
||
| 386 | ); |
||
| 387 | } |
||
| 388 | } |
||
| 389 | } |
||
| 390 | } elseif ($document->file_type == DOCUMENT) { |
||
| 391 | //Checking if folder exists in the database otherwise we created it |
||
| 392 | $dir_to_create = dirname($document->path); |
||
| 393 | |||
| 394 | if (!empty($dir_to_create) && $dir_to_create != 'document' && $dir_to_create != '/') { |
||
| 395 | if (is_dir($path.dirname($document->path))) { |
||
| 396 | $sql = "SELECT id FROM $table |
||
| 397 | WHERE |
||
| 398 | c_id = ".$this->destination_course_id." AND |
||
| 399 | path = '/".self::DBUTF8escapestring(substr(dirname($document->path), 9))."'"; |
||
| 400 | $res = Database::query($sql); |
||
| 401 | if (Database::num_rows($res) == 0) { |
||
| 402 | //continue; |
||
| 403 | $visibility = $document->item_properties[0]['visibility']; |
||
| 404 | $new = '/'.substr(dirname($document->path), 9); |
||
| 405 | $title = $document->title; |
||
| 406 | if (empty($title)) { |
||
| 407 | $title = str_replace('/', '', $new); |
||
| 408 | } |
||
| 409 | |||
| 410 | // This code fixes the possibility for a file without a directory entry to be |
||
| 411 | $document_id = add_document( |
||
| 412 | $course_info, |
||
| 413 | $new, |
||
| 414 | 'folder', |
||
| 415 | 0, |
||
| 416 | $title, |
||
| 417 | null, |
||
| 418 | null, |
||
| 419 | false |
||
| 420 | ); |
||
| 421 | |||
| 422 | $itemProperty = isset($document->item_properties[0]) ? $document->item_properties[0] : ''; |
||
| 423 | $insertUserId = isset($itemProperty['insert_user_id']) ? $itemProperty['insert_user_id'] : api_get_user_id(); |
||
| 424 | $toGroupId = isset($itemProperty['to_group_id']) ? $itemProperty['to_group_id'] : 0; |
||
| 425 | $toUserId = isset($itemProperty['to_user_id']) ? $itemProperty['to_user_id'] : null; |
||
| 426 | |||
| 427 | $insertUserId = $this->checkUserId($insertUserId); |
||
| 428 | $toUserId = $this->checkUserId($toUserId, true); |
||
| 429 | |||
| 430 | api_item_property_update( |
||
| 431 | $course_info, |
||
| 432 | TOOL_DOCUMENT, |
||
| 433 | $document_id, |
||
| 434 | 'FolderCreated', |
||
| 435 | $insertUserId, |
||
| 436 | $toGroupId, |
||
| 437 | $toUserId, |
||
| 438 | null, |
||
| 439 | null, |
||
| 440 | $my_session_id |
||
| 441 | ); |
||
| 442 | } |
||
| 443 | } |
||
| 444 | } |
||
| 445 | |||
| 446 | if (file_exists($path.$document->path)) { |
||
| 447 | switch ($this->file_option) { |
||
| 448 | case FILE_OVERWRITE: |
||
| 449 | $origin_path = $this->course->backup_path.'/'.$document->path; |
||
| 450 | |||
| 451 | if (file_exists($origin_path)) { |
||
| 452 | copy($origin_path, $path.$document->path); |
||
| 453 | $sql = "SELECT id FROM $table |
||
| 454 | WHERE |
||
| 455 | c_id = ".$this->destination_course_id." AND |
||
| 456 | path = '/".self::DBUTF8escapestring(substr($document->path, 9))."'"; |
||
| 457 | |||
| 458 | $res = Database::query($sql); |
||
| 459 | $count = Database::num_rows($res); |
||
| 460 | |||
| 461 | if ($count == 0) { |
||
| 462 | $params = [ |
||
| 463 | 'path' => "/".self::DBUTF8(substr($document->path, 9)), |
||
| 464 | 'c_id' => $this->destination_course_id, |
||
| 465 | 'comment'=> self::DBUTF8($document->comment), |
||
| 466 | 'title' => self::DBUTF8($document->title), |
||
| 467 | 'filetype' => self::DBUTF8($document->file_type), |
||
| 468 | 'size' => self::DBUTF8($document->size), |
||
| 469 | 'session_id' => $my_session_id, |
||
| 470 | ]; |
||
| 471 | |||
| 472 | $document_id = Database::insert($table, $params); |
||
| 473 | |||
| 474 | if ($document_id) { |
||
| 475 | $sql = "UPDATE $table SET id = iid WHERE iid = $document_id"; |
||
| 476 | Database::query($sql); |
||
| 477 | } |
||
| 478 | $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $document_id; |
||
| 479 | |||
| 480 | $itemProperty = isset($document->item_properties[0]) ? $document->item_properties[0] : ''; |
||
| 481 | $insertUserId = isset($itemProperty['insert_user_id']) ? $itemProperty['insert_user_id'] : api_get_user_id(); |
||
| 482 | $toGroupId = isset($itemProperty['to_group_id']) ? $itemProperty['to_group_id'] : 0; |
||
| 483 | $toUserId = isset($itemProperty['to_user_id']) ? $itemProperty['to_user_id'] : null; |
||
| 484 | |||
| 485 | $insertUserId = $this->checkUserId($insertUserId); |
||
| 486 | $toUserId = $this->checkUserId($toUserId, true); |
||
| 487 | |||
| 488 | api_item_property_update( |
||
| 489 | $course_info, |
||
| 490 | TOOL_DOCUMENT, |
||
| 491 | $document_id, |
||
| 492 | 'DocumentAdded', |
||
| 493 | $insertUserId, |
||
| 494 | $toGroupId, |
||
| 495 | $toUserId, |
||
| 496 | null, |
||
| 497 | null, |
||
| 498 | $my_session_id |
||
| 499 | ); |
||
| 500 | } else { |
||
| 501 | $obj = Database::fetch_object($res); |
||
| 502 | $document_id = $obj->id; |
||
| 503 | $params = [ |
||
| 504 | 'path' => "/".self::DBUTF8(substr($document->path, 9)), |
||
| 505 | 'c_id' => $this->destination_course_id, |
||
| 506 | 'comment'=> self::DBUTF8($document->comment), |
||
| 507 | 'title' => self::DBUTF8($document->title), |
||
| 508 | 'filetype' => self::DBUTF8($document->file_type), |
||
| 509 | 'size' => self::DBUTF8($document->size), |
||
| 510 | 'session_id' => $my_session_id, |
||
| 511 | ]; |
||
| 512 | |||
| 513 | Database::update( |
||
| 514 | $table, |
||
| 515 | $params, |
||
| 516 | [ |
||
| 517 | 'c_id = ? AND path = ?' => [ |
||
| 518 | $this->destination_course_id, |
||
| 519 | "/".self::DBUTF8escapestring(substr($document->path, 9)), |
||
| 520 | ], |
||
| 521 | ] |
||
| 522 | ); |
||
| 523 | |||
| 524 | $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $obj->id; |
||
| 525 | |||
| 526 | $itemProperty = isset($document->item_properties[0]) ? $document->item_properties[0] : ''; |
||
| 527 | $insertUserId = isset($itemProperty['insert_user_id']) ? $itemProperty['insert_user_id'] : api_get_user_id(); |
||
| 528 | $toGroupId = isset($itemProperty['to_group_id']) ? $itemProperty['to_group_id'] : 0; |
||
| 529 | $toUserId = isset($itemProperty['to_user_id']) ? $itemProperty['to_user_id'] : null; |
||
| 530 | |||
| 531 | $insertUserId = $this->checkUserId($insertUserId); |
||
| 532 | $toUserId = $this->checkUserId($toUserId, true); |
||
| 533 | |||
| 534 | api_item_property_update( |
||
| 535 | $course_info, |
||
| 536 | TOOL_DOCUMENT, |
||
| 537 | $obj->id, |
||
| 538 | 'default', |
||
| 539 | $insertUserId, |
||
| 540 | $toGroupId, |
||
| 541 | $toUserId, |
||
| 542 | null, |
||
| 543 | null, |
||
| 544 | $my_session_id |
||
| 545 | ); |
||
| 546 | } |
||
| 547 | |||
| 548 | // Replace old course code with the new destination code |
||
| 549 | |||
| 550 | $file_info = pathinfo($path.$document->path); |
||
| 551 | |||
| 552 | if (in_array($file_info['extension'], array('html', 'htm'))) { |
||
| 553 | $content = file_get_contents($path.$document->path); |
||
| 554 | if (UTF8_CONVERT) $content = utf8_encode($content); |
||
| 555 | $content = DocumentManager::replace_urls_inside_content_html_from_copy_course( |
||
| 556 | $content, |
||
| 557 | $this->course->code, |
||
| 558 | $this->course->destination_path, |
||
| 559 | $this->course->backup_path, |
||
| 560 | $this->course->info['path'] |
||
| 561 | ); |
||
| 562 | file_put_contents($path.$document->path,$content); |
||
| 563 | } |
||
| 564 | |||
| 565 | $params = [ |
||
| 566 | 'comment'=> self::DBUTF8($document->comment), |
||
| 567 | 'title' => self::DBUTF8($document->title), |
||
| 568 | 'size' => self::DBUTF8($document->size), |
||
| 569 | ]; |
||
| 570 | Database::update( |
||
| 571 | $table, |
||
| 572 | $params, |
||
| 573 | [ |
||
| 574 | 'c_id = ? AND id = ?' => [ |
||
| 575 | $this->destination_course_id, |
||
| 576 | $document_id, |
||
| 577 | ], |
||
| 578 | ] |
||
| 579 | ); |
||
| 580 | } |
||
| 581 | |||
| 582 | break; |
||
| 583 | case FILE_SKIP: |
||
| 584 | $sql = "SELECT id FROM $table |
||
| 585 | WHERE |
||
| 586 | c_id = ".$this->destination_course_id." AND |
||
| 587 | path='/".self::DBUTF8escapestring(substr($document->path, 9))."'"; |
||
| 588 | $res = Database::query($sql); |
||
| 589 | $obj = Database::fetch_object($res); |
||
| 590 | $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $obj->id; |
||
| 591 | break; |
||
| 592 | case FILE_RENAME: |
||
| 593 | $i = 1; |
||
| 594 | $ext = explode('.', basename($document->path)); |
||
| 595 | View Code Duplication | if (count($ext) > 1) { |
|
| 596 | $ext = array_pop($ext); |
||
| 597 | $file_name_no_ext = substr($document->path, 0, - (strlen($ext) + 1)); |
||
| 598 | $ext = '.'.$ext; |
||
| 599 | } else { |
||
| 600 | $ext = ''; |
||
| 601 | $file_name_no_ext = $document->path; |
||
| 602 | } |
||
| 603 | $new_file_name = $file_name_no_ext.'_'.$i.$ext; |
||
| 604 | $file_exists = file_exists($path.$new_file_name); |
||
| 605 | View Code Duplication | while ($file_exists) { |
|
| 606 | $i ++; |
||
| 607 | $new_file_name = $file_name_no_ext.'_'.$i.$ext; |
||
| 608 | $file_exists = file_exists($path.$new_file_name); |
||
| 609 | } |
||
| 610 | |||
| 611 | if (!empty($session_id)) { |
||
| 612 | |||
| 613 | $document_path = explode('/',$document->path,3); |
||
| 614 | $course_path = $path; |
||
| 615 | $orig_base_folder = $document_path[1]; |
||
| 616 | $orig_base_path = $course_path.$document_path[0].'/'.$document_path[1]; |
||
| 617 | |||
| 618 | if (is_dir($orig_base_path)) { |
||
| 619 | |||
| 620 | $new_base_foldername = $orig_base_folder; |
||
| 621 | $new_base_path = $orig_base_path; |
||
| 622 | |||
| 623 | if ($_SESSION['orig_base_foldername'] != $new_base_foldername) { |
||
| 624 | unset($_SESSION['new_base_foldername']); |
||
| 625 | unset($_SESSION['orig_base_foldername']); |
||
| 626 | unset($_SESSION['new_base_path']); |
||
| 627 | } |
||
| 628 | |||
| 629 | $folder_exists = file_exists($new_base_path); |
||
| 630 | if ($folder_exists) { |
||
| 631 | $_SESSION['orig_base_foldername'] = $new_base_foldername; // e.g: carpeta1 in session |
||
| 632 | $x = ''; |
||
| 633 | while ($folder_exists) { |
||
| 634 | $x = $x + 1; |
||
| 635 | $new_base_foldername = $document_path[1].'_'.$x; |
||
| 636 | $new_base_path = $orig_base_path.'_'.$x; |
||
| 637 | if ($_SESSION['new_base_foldername'] == $new_base_foldername) { |
||
| 638 | break; |
||
| 639 | } |
||
| 640 | $folder_exists = file_exists($new_base_path); |
||
| 641 | } |
||
| 642 | $_SESSION['new_base_foldername'] = $new_base_foldername; |
||
| 643 | $_SESSION['new_base_path'] = $new_base_path; |
||
| 644 | } |
||
| 645 | |||
| 646 | if (isset($_SESSION['new_base_foldername']) && isset($_SESSION['new_base_path'])) { |
||
| 647 | $new_base_foldername = $_SESSION['new_base_foldername']; |
||
| 648 | $new_base_path = $_SESSION['new_base_path']; |
||
| 649 | } |
||
| 650 | |||
| 651 | $dest_document_path = $new_base_path.'/'.$document_path[2]; // e.g: "/var/www/wiener/courses/CURSO4/document/carpeta1_1/subcarpeta1/collaborative.png" |
||
| 652 | $basedir_dest_path = dirname($dest_document_path); // e.g: "/var/www/wiener/courses/CURSO4/document/carpeta1_1/subcarpeta1" |
||
| 653 | $base_path_document = $course_path.$document_path[0]; // e.g: "/var/www/wiener/courses/CURSO4/document" |
||
| 654 | $path_title = '/'.$new_base_foldername.'/'.$document_path[2]; |
||
| 655 | |||
| 656 | copy_folder_course_session( |
||
| 657 | $basedir_dest_path, |
||
| 658 | $base_path_document, |
||
| 659 | $session_id, |
||
| 660 | $course_info, |
||
| 661 | $document, |
||
| 662 | $this->course_origin_id |
||
| 663 | ); |
||
| 664 | |||
| 665 | if (file_exists($course_path.$document->path)) { |
||
| 666 | copy($course_path.$document->path, $dest_document_path); |
||
| 667 | } |
||
| 668 | |||
| 669 | //Replace old course code with the new destination code see BT#1985 |
||
| 670 | if (file_exists($dest_document_path)) { |
||
| 671 | $file_info = pathinfo($dest_document_path); |
||
| 672 | if (in_array($file_info['extension'], array('html','htm'))) { |
||
| 673 | $content = file_get_contents($dest_document_path); |
||
| 674 | if (UTF8_CONVERT) { |
||
| 675 | $content = utf8_encode($content); |
||
| 676 | } |
||
| 677 | $content = DocumentManager::replace_urls_inside_content_html_from_copy_course( |
||
| 678 | $content, |
||
| 679 | $this->course->code, |
||
| 680 | $this->course->destination_path, |
||
| 681 | $this->course->backup_path, |
||
| 682 | $this->course->info['path'] |
||
| 683 | ); |
||
| 684 | file_put_contents($dest_document_path, $content); |
||
| 685 | } |
||
| 686 | } |
||
| 687 | |||
| 688 | $params = [ |
||
| 689 | 'path' => self::DBUTF8($path_title), |
||
| 690 | 'c_id' => $this->destination_course_id, |
||
| 691 | 'comment'=> self::DBUTF8($document->comment), |
||
| 692 | 'title' => self::DBUTF8(basename($path_title)), |
||
| 693 | 'filetype' => self::DBUTF8($document->file_type), |
||
| 694 | 'size' => self::DBUTF8($document->size), |
||
| 695 | 'session_id' => $my_session_id, |
||
| 696 | ]; |
||
| 697 | |||
| 698 | $document_id = Database::insert($table, $params); |
||
| 699 | |||
| 700 | if ($document_id) { |
||
| 701 | $sql = "UPDATE $table SET id = iid WHERE iid = $document_id"; |
||
| 702 | Database::query($sql); |
||
| 703 | } |
||
| 704 | |||
| 705 | $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $document_id; |
||
| 706 | |||
| 707 | $itemProperty = isset($document->item_properties[0]) ? $document->item_properties[0] : ''; |
||
| 708 | $insertUserId = isset($itemProperty['insert_user_id']) ? $itemProperty['insert_user_id'] : api_get_user_id(); |
||
| 709 | $toGroupId = isset($itemProperty['to_group_id']) ? $itemProperty['to_group_id'] : 0; |
||
| 710 | $toUserId = isset($itemProperty['to_user_id']) ? $itemProperty['to_user_id'] : null; |
||
| 711 | |||
| 712 | $insertUserId = $this->checkUserId($insertUserId); |
||
| 713 | $toUserId = $this->checkUserId($toUserId, true); |
||
| 714 | |||
| 715 | api_item_property_update( |
||
| 716 | $course_info, |
||
| 717 | TOOL_DOCUMENT, |
||
| 718 | $document_id, |
||
| 719 | 'DocumentAdded', |
||
| 720 | $insertUserId, |
||
| 721 | $toGroupId, |
||
| 722 | $toUserId, |
||
| 723 | null, |
||
| 724 | null, |
||
| 725 | $my_session_id |
||
| 726 | ); |
||
| 727 | } else { |
||
| 728 | if (file_exists($path.$document->path)) { |
||
| 729 | copy($path.$document->path, $path.$new_file_name); |
||
| 730 | } |
||
| 731 | //Replace old course code with the new destination code see BT#1985 |
||
| 732 | View Code Duplication | if (file_exists($path.$new_file_name)) { |
|
| 733 | $file_info = pathinfo($path.$new_file_name); |
||
| 734 | if (in_array($file_info['extension'], array('html','htm'))) { |
||
| 735 | $content = file_get_contents($path.$new_file_name); |
||
| 736 | if (UTF8_CONVERT) { |
||
| 737 | $content = utf8_encode($content); |
||
| 738 | } |
||
| 739 | $content = DocumentManager::replace_urls_inside_content_html_from_copy_course( |
||
| 740 | $content, |
||
| 741 | $this->course->code, |
||
| 742 | $this->course->destination_path, |
||
| 743 | $this->course->backup_path, |
||
| 744 | $this->course->info['path'] |
||
| 745 | ); |
||
| 746 | file_put_contents($path.$new_file_name, $content); |
||
| 747 | } |
||
| 748 | } |
||
| 749 | |||
| 750 | $params = [ |
||
| 751 | 'path' => "/".self::DBUTF8escapestring(substr($new_file_name, 9)), |
||
| 752 | 'c_id' => $this->destination_course_id, |
||
| 753 | 'comment'=> self::DBUTF8($document->comment), |
||
| 754 | 'title' => self::DBUTF8($document->title), |
||
| 755 | 'filetype' => self::DBUTF8($document->file_type), |
||
| 756 | 'size' => self::DBUTF8($document->size), |
||
| 757 | 'session_id' => $my_session_id, |
||
| 758 | ]; |
||
| 759 | |||
| 760 | $document_id = Database::insert($table, $params); |
||
| 761 | |||
| 762 | if ($document_id) { |
||
| 763 | $sql = "UPDATE $table SET id = iid WHERE iid = $document_id"; |
||
| 764 | Database::query($sql); |
||
| 765 | |||
| 766 | $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $document_id; |
||
| 767 | |||
| 768 | $itemProperty = isset($document->item_properties[0]) ? $document->item_properties[0] : ''; |
||
| 769 | $insertUserId = isset($itemProperty['insert_user_id']) ? $itemProperty['insert_user_id'] : api_get_user_id(); |
||
| 770 | $toGroupId = isset($itemProperty['to_group_id']) ? $itemProperty['to_group_id'] : 0; |
||
| 771 | $toUserId = isset($itemProperty['to_user_id']) ? $itemProperty['to_user_id'] : null; |
||
| 772 | |||
| 773 | $insertUserId = $this->checkUserId($insertUserId); |
||
| 774 | $toUserId = $this->checkUserId($toUserId, true); |
||
| 775 | |||
| 776 | api_item_property_update( |
||
| 777 | $course_info, |
||
| 778 | TOOL_DOCUMENT, |
||
| 779 | $document_id, |
||
| 780 | 'DocumentAdded', |
||
| 781 | $insertUserId, |
||
| 782 | $toGroupId, |
||
| 783 | $toUserId, |
||
| 784 | null, |
||
| 785 | null, |
||
| 786 | $my_session_id |
||
| 787 | ); |
||
| 788 | } |
||
| 789 | } |
||
| 790 | } else { |
||
| 791 | |||
| 792 | copy($this->course->backup_path.'/'.$document->path, $path.$new_file_name); |
||
| 793 | |||
| 794 | //Replace old course code with the new destination code see BT#1985 |
||
| 795 | View Code Duplication | if (file_exists($path.$new_file_name)) { |
|
| 796 | $file_info = pathinfo($path.$new_file_name); |
||
| 797 | if (in_array($file_info['extension'], array('html','htm'))) { |
||
| 798 | $content = file_get_contents($path.$new_file_name); |
||
| 799 | if (UTF8_CONVERT) { |
||
| 800 | $content = utf8_encode($content); |
||
| 801 | } |
||
| 802 | $content = DocumentManager::replace_urls_inside_content_html_from_copy_course( |
||
| 803 | $content, |
||
| 804 | $this->course->code, |
||
| 805 | $this->course->destination_path, |
||
| 806 | $this->course->backup_path, |
||
| 807 | $this->course->info['path'] |
||
| 808 | ); |
||
| 809 | file_put_contents($path.$new_file_name, $content); |
||
| 810 | } |
||
| 811 | } |
||
| 812 | |||
| 813 | $params = [ |
||
| 814 | 'c_id' => $this->destination_course_id, |
||
| 815 | 'path' => "/".self::DBUTF8escapestring(substr($new_file_name, 9)), |
||
| 816 | 'comment'=> self::DBUTF8($document->comment), |
||
| 817 | 'title' => self::DBUTF8($document->title), |
||
| 818 | 'filetype' => self::DBUTF8($document->file_type), |
||
| 819 | 'size' => self::DBUTF8($document->size), |
||
| 820 | 'session_id' => $my_session_id, |
||
| 821 | ]; |
||
| 822 | |||
| 823 | $document_id = Database::insert($table, $params); |
||
| 824 | |||
| 825 | if ($document_id) { |
||
| 826 | $sql = "UPDATE $table SET id = iid WHERE iid = $document_id"; |
||
| 827 | Database::query($sql); |
||
| 828 | } |
||
| 829 | |||
| 830 | $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $document_id; |
||
| 831 | |||
| 832 | $itemProperty = isset($document->item_properties[0]) ? $document->item_properties[0] : ''; |
||
| 833 | $insertUserId = isset($itemProperty['insert_user_id']) ? $itemProperty['insert_user_id'] : api_get_user_id(); |
||
| 834 | $toGroupId = isset($itemProperty['to_group_id']) ? $itemProperty['to_group_id'] : 0; |
||
| 835 | $toUserId = isset($itemProperty['to_user_id']) ? $itemProperty['to_user_id'] : null; |
||
| 836 | |||
| 837 | $insertUserId = $this->checkUserId($insertUserId); |
||
| 838 | $toUserId = $this->checkUserId($toUserId, true); |
||
| 839 | |||
| 840 | api_item_property_update( |
||
| 841 | $course_info, |
||
| 842 | TOOL_DOCUMENT, |
||
| 843 | $document_id, |
||
| 844 | 'DocumentAdded', |
||
| 845 | $insertUserId, |
||
| 846 | $toGroupId, |
||
| 847 | $toUserId, |
||
| 848 | null, |
||
| 849 | null, |
||
| 850 | $my_session_id |
||
| 851 | ); |
||
| 852 | } |
||
| 853 | break; |
||
| 854 | |||
| 855 | } // end switch |
||
| 856 | } else { |
||
| 857 | // end if file exists |
||
| 858 | //make sure the source file actually exists |
||
| 859 | if (is_file($this->course->backup_path.'/'.$document->path) && |
||
| 860 | is_readable($this->course->backup_path.'/'.$document->path) && |
||
| 861 | is_dir(dirname($path.$document->path)) && |
||
| 862 | is_writeable(dirname($path.$document->path)) |
||
| 863 | ) { |
||
| 864 | //echo 'Copying'; |
||
| 865 | copy($this->course->backup_path.'/'.$document->path, $path.$document->path); |
||
| 866 | |||
| 867 | //Replace old course code with the new destination code see BT#1985 |
||
| 868 | if (file_exists($path.$document->path)) { |
||
| 869 | $file_info = pathinfo($path.$document->path); |
||
| 870 | if (in_array($file_info['extension'], array('html','htm'))) { |
||
| 871 | $content = file_get_contents($path.$document->path); |
||
| 872 | if (UTF8_CONVERT) { |
||
| 873 | $content = utf8_encode($content); |
||
| 874 | } |
||
| 875 | $content = DocumentManager::replace_urls_inside_content_html_from_copy_course( |
||
| 876 | $content, |
||
| 877 | $this->course->code, |
||
| 878 | $this->course->destination_path, |
||
| 879 | $this->course->backup_path, |
||
| 880 | $this->course->info['path'] |
||
| 881 | ); |
||
| 882 | file_put_contents($path.$document->path, $content); |
||
| 883 | } |
||
| 884 | } |
||
| 885 | |||
| 886 | $params = [ |
||
| 887 | 'c_id' => $this->destination_course_id, |
||
| 888 | 'path' => "/".self::DBUTF8(substr($document->path, 9)), |
||
| 889 | 'comment'=> self::DBUTF8($document->comment), |
||
| 890 | 'title' => self::DBUTF8($document->title), |
||
| 891 | 'filetype' => self::DBUTF8($document->file_type), |
||
| 892 | 'size' => self::DBUTF8($document->size), |
||
| 893 | 'session_id' => $my_session_id, |
||
| 894 | ]; |
||
| 895 | |||
| 896 | $document_id = Database::insert($table, $params); |
||
| 897 | |||
| 898 | if ($document_id) { |
||
| 899 | $sql = "UPDATE $table SET id = iid WHERE iid = $document_id"; |
||
| 900 | Database::query($sql); |
||
| 901 | } |
||
| 902 | |||
| 903 | $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $document_id; |
||
| 904 | |||
| 905 | $itemProperty = isset($document->item_properties[0]) ? $document->item_properties[0] : ''; |
||
| 906 | $insertUserId = isset($itemProperty['insert_user_id']) ? $itemProperty['insert_user_id'] : api_get_user_id(); |
||
| 907 | $toGroupId = isset($itemProperty['to_group_id']) ? $itemProperty['to_group_id'] : 0; |
||
| 908 | $toUserId = isset($itemProperty['to_user_id']) ? $itemProperty['to_user_id'] : null; |
||
| 909 | |||
| 910 | $insertUserId = $this->checkUserId($insertUserId); |
||
| 911 | $toUserId = $this->checkUserId($toUserId, true); |
||
| 912 | |||
| 913 | api_item_property_update( |
||
| 914 | $course_info, |
||
| 915 | TOOL_DOCUMENT, |
||
| 916 | $document_id, |
||
| 917 | 'DocumentAdded', |
||
| 918 | $insertUserId, |
||
| 919 | $toGroupId, |
||
| 920 | $toUserId, |
||
| 921 | null, |
||
| 922 | null, |
||
| 923 | $my_session_id |
||
| 924 | ); |
||
| 925 | } else { |
||
| 926 | if (is_file($this->course->backup_path.'/'.$document->path) && |
||
| 927 | is_readable($this->course->backup_path.'/'.$document->path) |
||
| 928 | ) { |
||
| 929 | error_log('Course copy generated an ignoreable error while trying to copy '.$this->course->backup_path.'/'.$document->path.': file not found'); |
||
| 930 | } |
||
| 931 | View Code Duplication | if (!is_dir(dirname($path.$document->path))) { |
|
| 932 | error_log('Course copy generated an ignoreable error while trying to copy to '.dirname($path.$document->path).': directory not found'); |
||
| 933 | } |
||
| 934 | View Code Duplication | if (!is_writeable(dirname($path.$document->path))) { |
|
| 935 | error_log('Course copy generated an ignoreable error while trying to copy to '.dirname($path.$document->path).': directory not writeable'); |
||
| 936 | } |
||
| 937 | } |
||
| 938 | } // end file doesn't exist |
||
| 939 | } |
||
| 940 | } // end for each |
||
| 941 | |||
| 942 | // Delete sessions for the copy the new folder in session |
||
| 943 | unset($_SESSION['new_base_foldername']); |
||
| 944 | unset($_SESSION['orig_base_foldername']); |
||
| 945 | unset($_SESSION['new_base_path']); |
||
| 946 | } |
||
| 947 | } |
||
| 948 | |||
| 949 | /** |
||
| 950 | * Restore scorm documents |
||
| 951 | * TODO @TODO check that the restore function with renaming doesn't break the scorm structure! |
||
| 952 | * see #7029 |
||
| 953 | */ |
||
| 954 | public function restore_scorm_documents() |
||
| 1026 | |||
| 1027 | /** |
||
| 1028 | * Restore forums |
||
| 1029 | */ |
||
| 1030 | public function restore_forums($sessionId = 0) |
||
| 1031 | { |
||
| 1032 | if ($this->course->has_resources(RESOURCE_FORUM)) { |
||
| 1033 | $sessionId = intval($sessionId); |
||
| 1034 | $table_forum = Database::get_course_table(TABLE_FORUM); |
||
| 1035 | $resources = $this->course->resources; |
||
| 1036 | foreach ($resources[RESOURCE_FORUM] as $id => $forum) { |
||
| 1037 | $params = (array)$forum->obj; |
||
| 1038 | $cat_id = ''; |
||
| 1039 | if (isset($this->course->resources[RESOURCE_FORUMCATEGORY]) && |
||
| 1040 | isset($this->course->resources[RESOURCE_FORUMCATEGORY][$params['forum_category']])) { |
||
| 1041 | if ($this->course->resources[RESOURCE_FORUMCATEGORY][$params['forum_category']]->destination_id == -1) { |
||
| 1042 | $cat_id = $this->restore_forum_category( |
||
| 1043 | $params['forum_category'], |
||
| 1044 | $sessionId |
||
| 1045 | ); |
||
| 1046 | } else { |
||
| 1047 | $cat_id = $this->course->resources[RESOURCE_FORUMCATEGORY][$params['forum_category']]->destination_id; |
||
| 1048 | } |
||
| 1049 | } |
||
| 1050 | |||
| 1051 | $params = self::DBUTF8_array($params); |
||
| 1052 | $params['c_id'] = $this->destination_course_id; |
||
| 1053 | $params['forum_category'] = $cat_id; |
||
| 1054 | $params['session_id'] = $sessionId; |
||
| 1055 | |||
| 1056 | unset($params['forum_id']); |
||
| 1057 | unset($params['iid']); |
||
| 1058 | |||
| 1059 | $params['forum_comment'] = DocumentManager::replace_urls_inside_content_html_from_copy_course( |
||
| 1060 | $params['forum_comment'], |
||
| 1061 | $this->course->code, |
||
| 1062 | $this->course->destination_path, |
||
| 1063 | $this->course->backup_path, |
||
| 1064 | $this->course->info['path'] |
||
| 1065 | ); |
||
| 1066 | |||
| 1067 | if (!empty($params['forum_image'])) { |
||
| 1068 | $original_forum_image = $this->course->path.'upload/forum/images/'.$params['forum_image']; |
||
| 1069 | if (file_exists($original_forum_image)) { |
||
| 1070 | $new_forum_image = api_get_path(SYS_COURSE_PATH).$this->destination_course_info['path'].'/upload/forum/images/'.$params['forum_image']; |
||
| 1071 | @copy($original_forum_image, $new_forum_image); |
||
| 1072 | } |
||
| 1073 | } |
||
| 1074 | |||
| 1075 | $new_id = Database::insert($table_forum, $params); |
||
| 1076 | |||
| 1077 | if ($new_id) { |
||
| 1078 | $sql = "UPDATE $table_forum SET forum_id = iid WHERE iid = $new_id"; |
||
| 1079 | Database::query($sql); |
||
| 1080 | } |
||
| 1081 | |||
| 1082 | $this->course->resources[RESOURCE_FORUM][$id]->destination_id = $new_id; |
||
| 1083 | |||
| 1084 | $forum_topics = 0; |
||
| 1085 | if (is_array($this->course->resources[RESOURCE_FORUMTOPIC])) { |
||
| 1086 | foreach ($this->course->resources[RESOURCE_FORUMTOPIC] as $topic_id => $topic) { |
||
| 1087 | if ($topic->obj->forum_id == $id) { |
||
| 1088 | $this->restore_topic($topic_id, $new_id, $sessionId); |
||
| 1089 | $forum_topics ++; |
||
| 1090 | } |
||
| 1091 | } |
||
| 1092 | } |
||
| 1093 | if ($forum_topics > 0) { |
||
| 1094 | $sql = "UPDATE ".$table_forum." SET forum_threads = ".$forum_topics." |
||
| 1095 | WHERE c_id = {$this->destination_course_id} AND forum_id = ".(int)$new_id; |
||
| 1096 | Database::query($sql); |
||
| 1097 | } |
||
| 1098 | } |
||
| 1099 | } |
||
| 1100 | } |
||
| 1101 | |||
| 1102 | /** |
||
| 1103 | * Restore forum-categories |
||
| 1104 | */ |
||
| 1105 | public function restore_forum_category($my_id = null, $sessionId = 0) |
||
| 1106 | { |
||
| 1107 | $forum_cat_table = Database :: get_course_table(TABLE_FORUM_CATEGORY); |
||
| 1108 | $resources = $this->course->resources; |
||
| 1109 | if (!empty($resources[RESOURCE_FORUMCATEGORY])) { |
||
| 1110 | foreach ($resources[RESOURCE_FORUMCATEGORY] as $id => $forum_cat) { |
||
| 1111 | if (!empty($my_id)) { |
||
| 1112 | if ($my_id != $id) { |
||
| 1113 | continue; |
||
| 1114 | } |
||
| 1115 | } |
||
| 1116 | if ($forum_cat && !$forum_cat->is_restored()) { |
||
| 1117 | /*$title = $forum_cat->obj->cat_title; |
||
| 1118 | if (!empty($title)) { |
||
| 1119 | if (!preg_match('/.*\((.+)\)$/', $title, $matches)) { |
||
| 1120 | // This is for avoiding repetitive adding of training code after several backup/restore cycles. |
||
| 1121 | if ($matches[1] != $this->course->code) { |
||
| 1122 | $title = $title.' ('.$this->course->code.')'; |
||
| 1123 | } |
||
| 1124 | } |
||
| 1125 | }*/ |
||
| 1126 | $params = (array) $forum_cat->obj; |
||
| 1127 | $params['c_id'] = $this->destination_course_id; |
||
| 1128 | $params['cat_comment'] = DocumentManager::replace_urls_inside_content_html_from_copy_course( |
||
| 1129 | $params['cat_comment'], |
||
| 1130 | $this->course->code, |
||
| 1131 | $this->course->destination_path, |
||
| 1132 | $this->course->backup_path, |
||
| 1133 | $this->course->info['path'] |
||
| 1134 | ); |
||
| 1135 | $params['session_id'] = intval($sessionId); |
||
| 1136 | |||
| 1137 | unset($params['cat_id']); |
||
| 1138 | unset($params['iid']); |
||
| 1139 | |||
| 1140 | $params = self::DBUTF8_array($params); |
||
| 1141 | $new_id = Database::insert($forum_cat_table, $params); |
||
| 1142 | |||
| 1143 | if ($new_id) { |
||
| 1144 | $sql = "UPDATE $forum_cat_table SET cat_id = iid WHERE iid = $new_id"; |
||
| 1145 | Database::query($sql); |
||
| 1146 | } |
||
| 1147 | |||
| 1148 | $this->course->resources[RESOURCE_FORUMCATEGORY][$id]->destination_id = $new_id; |
||
| 1149 | if (!empty($my_id)) { |
||
| 1150 | return $new_id; |
||
| 1151 | } |
||
| 1152 | } |
||
| 1153 | } |
||
| 1154 | } |
||
| 1155 | } |
||
| 1156 | |||
| 1157 | /** |
||
| 1158 | * Restore a forum-topic |
||
| 1159 | * @param false|string $forum_id |
||
| 1160 | */ |
||
| 1161 | public function restore_topic($thread_id, $forum_id, $sessionId = 0) |
||
| 1162 | { |
||
| 1163 | $table = Database :: get_course_table(TABLE_FORUM_THREAD); |
||
| 1164 | $topic = $this->course->resources[RESOURCE_FORUMTOPIC][$thread_id]; |
||
| 1165 | |||
| 1166 | $params = (array)$topic->obj; |
||
| 1167 | $params = self::DBUTF8_array($params); |
||
| 1168 | $params['c_id'] = $this->destination_course_id; |
||
| 1169 | $params['forum_id'] = $forum_id; |
||
| 1170 | $params['thread_poster_id'] = $this->first_teacher_id; |
||
| 1171 | $params['thread_date'] = api_get_utc_datetime(); |
||
| 1172 | $params['thread_close_date'] = '0000-00-00 00:00:00'; |
||
| 1173 | $params['thread_last_post'] = 0; |
||
| 1174 | $params['thread_replies'] = 0; |
||
| 1175 | $params['thread_views'] = 0; |
||
| 1176 | $params['session_id'] = intval($sessionId); |
||
| 1177 | unset($params['thread_id']); |
||
| 1178 | unset($params['iid']); |
||
| 1179 | |||
| 1180 | $new_id = Database::insert($table, $params); |
||
| 1181 | |||
| 1182 | if ($new_id) { |
||
| 1183 | $sql = "UPDATE $table SET thread_id = iid WHERE iid = $new_id"; |
||
| 1184 | Database::query($sql); |
||
| 1185 | } |
||
| 1186 | |||
| 1187 | api_item_property_update( |
||
| 1188 | $this->destination_course_info, |
||
| 1189 | TOOL_FORUM_THREAD, |
||
| 1190 | $new_id, |
||
| 1191 | 'ThreadAdded', |
||
| 1192 | api_get_user_id(), |
||
| 1193 | 0, |
||
| 1194 | 0, |
||
| 1195 | null, |
||
| 1196 | null, |
||
| 1197 | $sessionId |
||
| 1198 | ); |
||
| 1199 | |||
| 1200 | $this->course->resources[RESOURCE_FORUMTOPIC][$thread_id]->destination_id = $new_id; |
||
| 1201 | |||
| 1202 | $topic_replies = -1; |
||
| 1203 | |||
| 1204 | foreach ($this->course->resources[RESOURCE_FORUMPOST] as $post_id => $post) { |
||
| 1205 | if ($post->obj->thread_id == $thread_id) { |
||
| 1206 | $topic_replies++; |
||
| 1207 | $this->restore_post($post_id, $new_id, $forum_id, $sessionId); |
||
| 1208 | } |
||
| 1209 | } |
||
| 1210 | return $new_id; |
||
| 1211 | } |
||
| 1212 | |||
| 1213 | /** |
||
| 1214 | * Restore a forum-post |
||
| 1215 | * @TODO Restore tree-structure of posts. For example: attachments to posts. |
||
| 1216 | * @param false|string $topic_id |
||
| 1217 | */ |
||
| 1218 | public function restore_post($id, $topic_id, $forum_id, $sessionId = 0) |
||
| 1219 | { |
||
| 1220 | $table_post = Database :: get_course_table(TABLE_FORUM_POST); |
||
| 1221 | $post = $this->course->resources[RESOURCE_FORUMPOST][$id]; |
||
| 1222 | $params = (array) $post->obj; |
||
| 1223 | $params['c_id'] = $this->destination_course_id; |
||
| 1224 | $params['forum_id'] = $forum_id; |
||
| 1225 | $params['thread_id'] = $topic_id; |
||
| 1226 | $params['poster_id'] = $this->first_teacher_id; |
||
| 1227 | $params['post_date'] = api_get_utc_datetime(); |
||
| 1228 | unset($params['post_id']); |
||
| 1229 | unset($params['iid']); |
||
| 1230 | $params['post_text'] = DocumentManager::replace_urls_inside_content_html_from_copy_course( |
||
| 1231 | $params['post_text'], |
||
| 1232 | $this->course->code, |
||
| 1233 | $this->course->destination_path, |
||
| 1234 | $this->course->backup_path, |
||
| 1235 | $this->course->info['path'] |
||
| 1236 | ); |
||
| 1237 | $new_id = Database::insert($table_post, $params); |
||
| 1238 | |||
| 1239 | if ($new_id) { |
||
| 1240 | $sql = "UPDATE $table_post SET post_id = iid WHERE iid = $new_id"; |
||
| 1241 | Database::query($sql); |
||
| 1242 | } |
||
| 1243 | |||
| 1244 | api_item_property_update( |
||
| 1245 | $this->destination_course_info, |
||
| 1246 | TOOL_FORUM_POST, |
||
| 1247 | $new_id, |
||
| 1248 | 'PostAdded', |
||
| 1249 | api_get_user_id(), |
||
| 1250 | 0, |
||
| 1251 | 0, |
||
| 1252 | null, |
||
| 1253 | null, |
||
| 1254 | $sessionId |
||
| 1255 | ); |
||
| 1256 | $this->course->resources[RESOURCE_FORUMPOST][$id]->destination_id = $new_id; |
||
| 1257 | |||
| 1258 | return $new_id; |
||
| 1259 | } |
||
| 1260 | |||
| 1261 | /** |
||
| 1262 | * Restore links |
||
| 1263 | */ |
||
| 1264 | public function restore_links($session_id = 0) |
||
| 1265 | { |
||
| 1266 | if ($this->course->has_resources(RESOURCE_LINK)) { |
||
| 1267 | $link_table = Database :: get_course_table(TABLE_LINK); |
||
| 1268 | $resources = $this->course->resources; |
||
| 1269 | |||
| 1270 | foreach ($resources[RESOURCE_LINK] as $id => $link) { |
||
| 1271 | $cat_id = $this->restore_link_category( |
||
| 1272 | $link->category_id, |
||
| 1273 | $session_id |
||
| 1274 | ); |
||
| 1275 | $sql = "SELECT MAX(display_order) |
||
| 1276 | FROM $link_table |
||
| 1277 | WHERE |
||
| 1278 | c_id = ".$this->destination_course_id." AND |
||
| 1279 | category_id='" . intval($cat_id). "'"; |
||
| 1280 | $result = Database::query($sql); |
||
| 1281 | list($max_order) = Database::fetch_array($result); |
||
| 1282 | |||
| 1283 | $params = []; |
||
| 1284 | if (!empty($session_id)) { |
||
| 1285 | $params['session_id'] = $session_id; |
||
| 1286 | } |
||
| 1287 | |||
| 1288 | $params['c_id'] = $this->destination_course_id; |
||
| 1289 | $params['url'] = self::DBUTF8($link->url); |
||
| 1290 | $params['title'] = self::DBUTF8($link->title); |
||
| 1291 | $params['description'] = self::DBUTF8($link->description); |
||
| 1292 | $params['category_id'] = $cat_id; |
||
| 1293 | $params['on_homepage'] = $link->on_homepage; |
||
| 1294 | $params['display_order'] = $max_order+1; |
||
| 1295 | |||
| 1296 | $id = Database::insert($link_table, $params); |
||
| 1297 | |||
| 1298 | View Code Duplication | if ($id) { |
|
| 1299 | $sql = "UPDATE $link_table SET id = iid WHERE iid = $id"; |
||
| 1300 | Database::query($sql); |
||
| 1301 | |||
| 1302 | api_item_property_update( |
||
| 1303 | $this->destination_course_info, |
||
| 1304 | TOOL_LINK, |
||
| 1305 | $id, |
||
| 1306 | 'LinkAdded', |
||
| 1307 | api_get_user_id() |
||
| 1308 | ); |
||
| 1309 | |||
| 1310 | if (!isset($this->course->resources[RESOURCE_LINK][$id])) { |
||
| 1311 | $this->course->resources[RESOURCE_LINK][$id] = new stdClass(); |
||
| 1312 | } |
||
| 1313 | $this->course->resources[RESOURCE_LINK][$id]->destination_id = $id; |
||
| 1314 | } |
||
| 1315 | } |
||
| 1316 | } |
||
| 1317 | } |
||
| 1318 | |||
| 1319 | /** |
||
| 1320 | * Restore a link-category |
||
| 1321 | */ |
||
| 1322 | public function restore_link_category($id, $session_id = 0) |
||
| 1323 | { |
||
| 1324 | $params = []; |
||
| 1325 | if (!empty($session_id)) { |
||
| 1326 | $params['session_id'] = $session_id; |
||
| 1327 | } |
||
| 1328 | |||
| 1329 | if ($id == 0) { |
||
| 1330 | return 0; |
||
| 1331 | } |
||
| 1332 | $link_cat_table = Database :: get_course_table(TABLE_LINK_CATEGORY); |
||
| 1333 | $resources = $this->course->resources; |
||
| 1334 | $link_cat = $resources[RESOURCE_LINKCATEGORY][$id]; |
||
| 1335 | if (is_object($link_cat) && !$link_cat->is_restored()) { |
||
| 1336 | $sql = "SELECT MAX(display_order) FROM $link_cat_table |
||
| 1337 | WHERE c_id = ".$this->destination_course_id." "; |
||
| 1338 | $result=Database::query($sql); |
||
| 1339 | list($orderMax)=Database::fetch_array($result,'NUM'); |
||
| 1340 | $display_order=$orderMax+1; |
||
| 1341 | |||
| 1342 | $params['c_id'] = $this->destination_course_id; |
||
| 1343 | $params['category_title'] = self::DBUTF8($link_cat->title); |
||
| 1344 | $params['description'] = self::DBUTF8($link_cat->description); |
||
| 1345 | $params['display_order'] = $display_order; |
||
| 1346 | |||
| 1347 | $new_id = Database::insert($link_cat_table, $params); |
||
| 1348 | |||
| 1349 | if ($new_id) { |
||
| 1350 | $sql = "UPDATE $link_cat_table SET id = iid WHERE iid = $new_id"; |
||
| 1351 | Database::query($sql); |
||
| 1352 | api_set_default_visibility($new_id, TOOL_LINK_CATEGORY); |
||
| 1353 | } |
||
| 1354 | |||
| 1355 | $this->course->resources[RESOURCE_LINKCATEGORY][$id]->destination_id = $new_id; |
||
| 1356 | return $new_id; |
||
| 1357 | } |
||
| 1358 | return $this->course->resources[RESOURCE_LINKCATEGORY][$id]->destination_id; |
||
| 1359 | } |
||
| 1360 | |||
| 1361 | /** |
||
| 1362 | * Restore tool intro |
||
| 1363 | */ |
||
| 1364 | public function restore_tool_intro($sessionId = 0) |
||
| 1365 | { |
||
| 1366 | if ($this->course->has_resources(RESOURCE_TOOL_INTRO)) { |
||
| 1367 | $sessionId = intval($sessionId); |
||
| 1368 | $tool_intro_table = Database :: get_course_table(TABLE_TOOL_INTRO); |
||
| 1369 | $resources = $this->course->resources; |
||
| 1370 | foreach ($resources[RESOURCE_TOOL_INTRO] as $id => $tool_intro) { |
||
| 1371 | $sql = "DELETE FROM ".$tool_intro_table." |
||
| 1372 | WHERE |
||
| 1373 | c_id = ".$this->destination_course_id." AND |
||
| 1374 | id = '".self::DBUTF8escapestring($tool_intro->id)."'"; |
||
| 1375 | Database::query($sql); |
||
| 1376 | |||
| 1377 | $tool_intro->intro_text = DocumentManager::replace_urls_inside_content_html_from_copy_course( |
||
| 1378 | $tool_intro->intro_text, |
||
| 1379 | $this->course->code, |
||
| 1380 | $this->course->destination_path, |
||
| 1381 | $this->course->backup_path, |
||
| 1382 | $this->course->info['path'] |
||
| 1383 | ); |
||
| 1384 | |||
| 1385 | $params = [ |
||
| 1386 | 'c_id' => $this->destination_course_id, |
||
| 1387 | 'id' => self::DBUTF8($tool_intro->id), |
||
| 1388 | 'intro_text' => self::DBUTF8($tool_intro->intro_text), |
||
| 1389 | 'session_id' => $sessionId, |
||
| 1390 | ]; |
||
| 1391 | |||
| 1392 | $id = Database::insert($tool_intro_table, $params); |
||
| 1393 | if ($id) { |
||
| 1394 | // id is a varchar not an int |
||
| 1395 | /*$sql = "UPDATE $tool_intro_table SET id = iid WHERE iid = $id"; |
||
| 1396 | Database::query($sql);*/ |
||
| 1397 | |||
| 1398 | if (!isset($this->course->resources[RESOURCE_TOOL_INTRO][$id])) { |
||
| 1399 | $this->course->resources[RESOURCE_TOOL_INTRO][$id] = new stdClass(); |
||
| 1400 | } |
||
| 1401 | |||
| 1402 | $this->course->resources[RESOURCE_TOOL_INTRO][$id]->destination_id = $id; |
||
| 1403 | } |
||
| 1404 | } |
||
| 1405 | } |
||
| 1406 | } |
||
| 1407 | |||
| 1408 | /** |
||
| 1409 | * Restore events |
||
| 1410 | */ |
||
| 1411 | View Code Duplication | public function restore_events($sessionId = 0) |
|
| 1518 | |||
| 1519 | /** |
||
| 1520 | * Restore course-description |
||
| 1521 | */ |
||
| 1522 | public function restore_course_descriptions($session_id = 0) |
||
| 1523 | { |
||
| 1524 | if ($this->course->has_resources(RESOURCE_COURSEDESCRIPTION)) { |
||
| 1525 | $table = Database :: get_course_table(TABLE_COURSE_DESCRIPTION); |
||
| 1526 | $resources = $this->course->resources; |
||
| 1527 | foreach ($resources[RESOURCE_COURSEDESCRIPTION] as $id => $cd) { |
||
| 1528 | $courseDescription = (array) $cd; |
||
| 1529 | |||
| 1530 | $content = isset($courseDescription['content']) ? $courseDescription['content'] : ''; |
||
| 1531 | $descriptionType = isset($courseDescription['description_type']) ? $courseDescription['description_type'] : ''; |
||
| 1532 | $title = isset($courseDescription['title']) ? $courseDescription['title'] : ''; |
||
| 1565 | |||
| 1566 | /** |
||
| 1567 | * Restore announcements |
||
| 1568 | */ |
||
| 1569 | View Code Duplication | public function restore_announcements($sessionId = 0) |
|
| 1692 | |||
| 1693 | /** |
||
| 1694 | * Restore Quiz |
||
| 1695 | * @param int $session_id |
||
| 1696 | * @param bool $respect_base_content |
||
| 1697 | */ |
||
| 1698 | public function restore_quizzes( |
||
| 1816 | |||
| 1817 | /** |
||
| 1818 | * Restore quiz-questions |
||
| 1819 | * @params int question id |
||
| 1820 | */ |
||
| 1821 | public function restore_quiz_question($id) |
||
| 2119 | |||
| 2120 | /** |
||
| 2121 | * @todo : add session id when used for session |
||
| 2122 | */ |
||
| 2123 | public function restore_test_category($session_id, $respect_base_content, $destination_course_code) |
||
| 2190 | |||
| 2191 | /** |
||
| 2192 | * Restore surveys |
||
| 2193 | * @param int $sessionId Optional. The session id |
||
| 2194 | */ |
||
| 2195 | public function restore_surveys($sessionId = 0) |
||
| 2369 | |||
| 2370 | /** |
||
| 2371 | * Check availability of a survey code |
||
| 2372 | * @param string $survey_code |
||
| 2373 | */ |
||
| 2374 | View Code Duplication | public function is_survey_code_available($survey_code) |
|
| 2384 | |||
| 2385 | /** |
||
| 2386 | * Restore survey-questions |
||
| 2387 | * @param string $survey_id |
||
| 2388 | */ |
||
| 2389 | public function restore_survey_question($id, $survey_id) |
||
| 2461 | |||
| 2462 | /** |
||
| 2463 | * Restoring learning paths |
||
| 2464 | * @param int $session_id |
||
| 2465 | * @param bool|false $respect_base_content |
||
| 2466 | */ |
||
| 2467 | public function restore_learnpaths($session_id = 0, $respect_base_content = false) |
||
| 2747 | |||
| 2748 | /** |
||
| 2749 | * Restore works |
||
| 2750 | * @deprecated use restore_works |
||
| 2751 | * |
||
| 2752 | */ |
||
| 2753 | public function restore_student_publication($sessionId = 0) |
||
| 2838 | |||
| 2839 | /** |
||
| 2840 | * copy all directory and sub directory |
||
| 2841 | * @param string The path origin |
||
| 2842 | * @param string The path destination |
||
| 2843 | * @param boolean Option Overwrite |
||
| 2844 | * @param string $source |
||
| 2845 | * @param string $dest |
||
| 2846 | * @return void() |
||
| 2847 | * @deprecated |
||
| 2848 | */ |
||
| 2849 | public function allow_create_all_directory($source, $dest, $overwrite = false) |
||
| 2873 | |||
| 2874 | /** |
||
| 2875 | * Gets the new ID of one specific tool item from the tool name and the old ID |
||
| 2876 | * @param string Tool name |
||
| 2877 | * @param integer Old ID |
||
| 2878 | * @return integer New ID |
||
| 2879 | */ |
||
| 2880 | public function get_new_id($tool, $ref) |
||
| 2902 | |||
| 2903 | /** |
||
| 2904 | * Restore glossary |
||
| 2905 | */ |
||
| 2906 | public function restore_glossary($session_id = 0) |
||
| 2956 | |||
| 2957 | /** |
||
| 2958 | * @param int $session_id |
||
| 2959 | */ |
||
| 2960 | public function restore_wiki($session_id = 0) |
||
| 3030 | |||
| 3031 | /** |
||
| 3032 | * Restore Thematics |
||
| 3033 | * @param int $session_id |
||
| 3034 | */ |
||
| 3035 | public function restore_thematic($session_id = 0) |
||
| 3124 | |||
| 3125 | /** |
||
| 3126 | * Restore Attendance |
||
| 3127 | * @param int $session_id |
||
| 3128 | */ |
||
| 3129 | public function restore_attendance($session_id = 0) |
||
| 3185 | |||
| 3186 | /** |
||
| 3187 | * Restore Works |
||
| 3188 | * @param int $sessionId |
||
| 3189 | */ |
||
| 3190 | public function restore_works($sessionId = 0) |
||
| 3285 | |||
| 3286 | /** |
||
| 3287 | * Restore Works |
||
| 3288 | * @param int $sessionId |
||
| 3289 | */ |
||
| 3290 | public function restore_gradebook($sessionId = 0) |
||
| 3319 | |||
| 3320 | /** |
||
| 3321 | * @param string $str |
||
| 3322 | * @return string |
||
| 3323 | */ |
||
| 3324 | public function DBUTF8($str) |
||
| 3331 | |||
| 3332 | /** |
||
| 3333 | * @param string $str |
||
| 3334 | * @return string |
||
| 3335 | */ |
||
| 3336 | public function DBUTF8escapestring($str) |
||
| 3343 | |||
| 3344 | /** |
||
| 3345 | * @param array $array |
||
| 3346 | * @return mixed |
||
| 3347 | */ |
||
| 3348 | public function DBUTF8_array($array) |
||
| 3359 | |||
| 3360 | /** |
||
| 3361 | * Check if user exist otherwise use current user |
||
| 3362 | * @param int $userId |
||
| 3363 | * @param bool $returnNull |
||
| 3364 | * |
||
| 3365 | * @return int |
||
| 3366 | */ |
||
| 3367 | private function checkUserId($userId, $returnNull = false) { |
||
| 3382 | } |
||
| 3383 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.