Total Complexity | 76 |
Total Lines | 756 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CourseRecycler 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.
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 CourseRecycler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class CourseRecycler |
||
17 | { |
||
18 | /** |
||
19 | * A course-object with the items to delete. |
||
20 | */ |
||
21 | public $course; |
||
22 | public $type; |
||
23 | |||
24 | /** |
||
25 | * Create a new CourseRecycler. |
||
26 | * |
||
27 | * @param course $course The course-object which contains the items to |
||
28 | * delete |
||
29 | */ |
||
30 | public function __construct($course) |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Delete all items from the course. |
||
39 | * This deletes all items in the course-object from the current Chamilo- |
||
40 | * course. |
||
41 | * |
||
42 | * @param string $backupType 'full_backup' or 'select_items' |
||
43 | * |
||
44 | * @return bool |
||
45 | * |
||
46 | * @assert (null) === false |
||
47 | */ |
||
48 | public function recycle($backupType) |
||
49 | { |
||
50 | if (empty($backupType)) { |
||
51 | return false; |
||
52 | } |
||
53 | |||
54 | $table_tool_intro = Database::get_course_table(TABLE_TOOL_INTRO); |
||
55 | $table_item_properties = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
56 | |||
57 | $this->type = $backupType; |
||
58 | $this->recycle_links(); |
||
59 | $this->recycle_link_categories(); |
||
60 | $this->recycle_events(); |
||
61 | $this->recycle_announcements(); |
||
62 | $this->recycle_documents(); |
||
63 | $this->recycle_forums(); |
||
64 | $this->recycle_forum_categories(); |
||
65 | $this->recycle_quizzes(); |
||
66 | $this->recycle_test_category(); |
||
67 | $this->recycle_surveys(); |
||
68 | $this->recycle_learnpaths(); |
||
69 | $this->recycle_learnpath_categories(); |
||
70 | $this->recycle_cours_description(); |
||
71 | $this->recycle_wiki(); |
||
72 | $this->recycle_glossary(); |
||
73 | $this->recycle_thematic(); |
||
74 | $this->recycle_attendance(); |
||
75 | $this->recycle_work(); |
||
76 | |||
77 | foreach ($this->course->resources as $type => $resources) { |
||
78 | foreach ($resources as $id => $resource) { |
||
79 | if (is_numeric($id)) { |
||
80 | $sql = "DELETE FROM $table_item_properties |
||
81 | WHERE c_id = ".$this->course_id." AND tool ='".$resource->get_tool()."' AND ref=".$id; |
||
82 | Database::query($sql); |
||
83 | } elseif ($type == RESOURCE_TOOL_INTRO) { |
||
84 | $sql = "DELETE FROM $table_tool_intro |
||
85 | WHERE c_id = ".$this->course_id." AND id='$id'"; |
||
86 | Database::query($sql); |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | |||
91 | if ($backupType === 'full_backup') { |
||
92 | \CourseManager::deleteCoursePicture($this->course_info['code']); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Delete documents. |
||
98 | */ |
||
99 | public function recycle_documents() |
||
100 | { |
||
101 | $table = Database::get_course_table(TABLE_DOCUMENT); |
||
102 | $tableItemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
103 | |||
104 | if ($this->type === 'full_backup') { |
||
105 | $sql = "DELETE FROM $tableItemProperty |
||
106 | WHERE |
||
107 | c_id = ".$this->course_id." AND |
||
108 | tool = '".TOOL_DOCUMENT."'"; |
||
109 | Database::query($sql); |
||
110 | |||
111 | $sql = "DELETE FROM $table WHERE c_id = ".$this->course_id; |
||
112 | Database::query($sql); |
||
113 | |||
114 | // Delete all content in the documents. |
||
115 | rmdirr($this->course->backup_path.'/document', true); |
||
116 | } else { |
||
117 | if ($this->course->has_resources(RESOURCE_DOCUMENT)) { |
||
118 | foreach ($this->course->resources[RESOURCE_DOCUMENT] as $document) { |
||
119 | rmdirr($this->course->backup_path.'/'.$document->path); |
||
120 | } |
||
121 | |||
122 | $ids = implode(',', array_filter(array_keys($this->course->resources[RESOURCE_DOCUMENT]))); |
||
123 | if (!empty($ids)) { |
||
124 | $sql = "DELETE FROM $table |
||
125 | WHERE c_id = ".$this->course_id." AND id IN(".$ids.")"; |
||
126 | Database::query($sql); |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Delete wiki. |
||
134 | */ |
||
135 | public function recycle_wiki() |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Delete glossary. |
||
162 | */ |
||
163 | public function recycle_glossary() |
||
164 | { |
||
165 | if ($this->course->has_resources(RESOURCE_GLOSSARY)) { |
||
166 | $table = Database::get_course_table(TABLE_GLOSSARY); |
||
167 | $ids = implode(',', array_filter(array_keys($this->course->resources[RESOURCE_GLOSSARY]))); |
||
168 | if (!empty($ids)) { |
||
169 | $sql = "DELETE FROM $table |
||
170 | WHERE c_id = ".$this->course_id." AND glossary_id IN(".$ids.")"; |
||
171 | Database::query($sql); |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Delete links. |
||
178 | */ |
||
179 | public function recycle_links() |
||
180 | { |
||
181 | if ($this->course->has_resources(RESOURCE_LINK)) { |
||
182 | $table = Database::get_course_table(TABLE_LINK); |
||
183 | $ids = implode(',', array_filter(array_keys($this->course->resources[RESOURCE_LINK]))); |
||
184 | if (!empty($ids)) { |
||
185 | $sql = "DELETE FROM $table |
||
186 | WHERE c_id = ".$this->course_id." AND id IN(".$ids.")"; |
||
187 | Database::query($sql); |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Delete forums. |
||
194 | */ |
||
195 | public function recycle_forums() |
||
305 | } |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Deletes all forum-categories without forum from the current course. |
||
310 | * Categories with forums in it are dealt with by recycle_forums() |
||
311 | * This requires a check on the status of the forum item in c_item_property |
||
312 | */ |
||
313 | public function recycle_forum_categories() |
||
314 | { |
||
315 | $forumTable = Database::get_course_table(TABLE_FORUM); |
||
316 | $forumCategoryTable = Database::get_course_table(TABLE_FORUM_CATEGORY); |
||
317 | $itemPropertyTable = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
318 | $courseId = $this->course_id; |
||
319 | // c_forum_forum.forum_category points to c_forum_category.cat_id and |
||
320 | // has to be queried *with* the c_id to ensure a match |
||
321 | $subQuery = "SELECT distinct(f.forum_category) as categoryId |
||
322 | FROM $forumTable f, $itemPropertyTable i |
||
323 | WHERE |
||
324 | f.c_id = $courseId AND |
||
325 | i.c_id = f.c_id AND |
||
326 | i.tool = 'forum' AND |
||
327 | f.iid = i.ref AND |
||
328 | i.visibility = 1"; |
||
329 | $sql = "DELETE FROM $forumCategoryTable |
||
330 | WHERE c_id = $courseId AND cat_id NOT IN ($subQuery)"; |
||
331 | Database::query($sql); |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Deletes all empty link-categories (=without links) from current course. |
||
336 | * Links are already dealt with by recycle_links() but if recycle is called |
||
337 | * on categories and not on link, then non-empty categories will survive |
||
338 | * the recycling. |
||
339 | */ |
||
340 | public function recycle_link_categories() |
||
341 | { |
||
342 | $linkCategoryTable = Database::get_course_table(TABLE_LINK_CATEGORY); |
||
343 | $linkTable = Database::get_course_table(TABLE_LINK); |
||
344 | $itemPropertyTable = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
345 | $courseId = $this->course_id; |
||
346 | // c_link.category_id points to c_link_category.id and |
||
347 | // has to be queried *with* the c_id to ensure a match |
||
348 | $subQuery = "SELECT distinct(l.category_id) as categoryId |
||
349 | FROM $linkTable l, $itemPropertyTable i |
||
350 | WHERE |
||
351 | l.c_id = $courseId AND |
||
352 | i.c_id = l.c_id AND |
||
353 | i.tool = 'link' AND |
||
354 | l.iid = i.ref AND |
||
355 | i.visibility = 1"; |
||
356 | $sql = "DELETE FROM $linkCategoryTable |
||
357 | WHERE c_id = $courseId AND id NOT IN ($subQuery)"; |
||
358 | Database::query($sql); |
||
359 | |||
360 | } |
||
361 | |||
362 | /** |
||
363 | * Delete events. |
||
364 | */ |
||
365 | public function recycle_events() |
||
366 | { |
||
367 | if ($this->course->has_resources(RESOURCE_EVENT)) { |
||
368 | $table = Database::get_course_table(TABLE_AGENDA); |
||
369 | $table_attachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT); |
||
370 | |||
371 | $ids = implode(',', array_filter(array_keys($this->course->resources[RESOURCE_EVENT]))); |
||
372 | if (!empty($ids)) { |
||
373 | $sql = "DELETE FROM ".$table." |
||
374 | WHERE c_id = ".$this->course_id." AND id IN(".$ids.")"; |
||
375 | Database::query($sql); |
||
376 | |||
377 | $sql = "DELETE FROM ".$table_attachment." |
||
378 | WHERE c_id = ".$this->course_id." AND agenda_id IN(".$ids.")"; |
||
379 | Database::query($sql); |
||
380 | } |
||
381 | } |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * Delete announcements. |
||
386 | */ |
||
387 | public function recycle_announcements() |
||
388 | { |
||
389 | if ($this->course->has_resources(RESOURCE_ANNOUNCEMENT)) { |
||
390 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
391 | $table_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); |
||
392 | |||
393 | $ids = implode(',', array_filter(array_keys($this->course->resources[RESOURCE_ANNOUNCEMENT]))); |
||
394 | if (!empty($ids)) { |
||
395 | $sql = "DELETE FROM ".$table." |
||
396 | WHERE c_id = ".$this->course_id." AND id IN(".$ids.")"; |
||
397 | Database::query($sql); |
||
398 | |||
399 | $sql = "DELETE FROM ".$table_attachment." |
||
400 | WHERE c_id = ".$this->course_id." AND announcement_id IN(".$ids.")"; |
||
401 | Database::query($sql); |
||
402 | } |
||
403 | } |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * Recycle quizzes - doesn't remove the questions and their answers, |
||
408 | * as they might still be used later. |
||
409 | */ |
||
410 | public function recycle_quizzes() |
||
411 | { |
||
412 | if ($this->course->has_resources(RESOURCE_QUIZ)) { |
||
413 | $table_qui_que = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
414 | $table_qui_ans = Database::get_course_table(TABLE_QUIZ_ANSWER); |
||
415 | $table_qui = Database::get_course_table(TABLE_QUIZ_TEST); |
||
416 | $table_rel = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
417 | $table_qui_que_opt = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION); |
||
418 | $table_qui_que_cat = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY); |
||
419 | $table_qui_que_rel_cat = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY); |
||
420 | |||
421 | $ids = array_keys($this->course->resources[RESOURCE_QUIZ]); |
||
422 | // If the value "-1" is in the ids of elements (questions) to |
||
423 | // be deleted, then consider all orphan questions should be deleted |
||
424 | // This value is set in CourseBuilder::quiz_build_questions() |
||
425 | $delete_orphan_questions = in_array(-1, $ids); |
||
426 | $ids = implode(',', $ids); |
||
427 | |||
428 | if (!empty($ids)) { |
||
429 | // Deletion of the tests first. Questions in these tests are |
||
430 | // not deleted and become orphan at this point |
||
431 | $sql = "DELETE FROM ".$table_qui." |
||
432 | WHERE c_id = ".$this->course_id." AND id IN(".$ids.")"; |
||
433 | Database::query($sql); |
||
434 | $sql = "DELETE FROM ".$table_rel." |
||
435 | WHERE c_id = ".$this->course_id." AND exercice_id IN(".$ids.")"; |
||
436 | Database::query($sql); |
||
437 | } |
||
438 | |||
439 | // Identifying again and deletion of the orphan questions, if it was desired. |
||
440 | if ($delete_orphan_questions) { |
||
441 | // If this query was ever too slow, there is an alternative here: |
||
442 | // https://github.com/beeznest/chamilo-lms-icpna/commit/a38eab725402188dffff50245ee068d79bcef779 |
||
443 | $sql = " ( |
||
444 | SELECT q.id, ex.c_id FROM $table_qui_que q |
||
445 | INNER JOIN $table_rel r |
||
446 | ON (q.c_id = r.c_id AND q.id = r.question_id) |
||
447 | |||
448 | INNER JOIN $table_qui ex |
||
449 | ON (ex.id = r.exercice_id AND ex.c_id = r.c_id) |
||
450 | WHERE ex.c_id = ".$this->course_id." AND (ex.active = '-1' OR ex.id = '-1') |
||
451 | ) |
||
452 | UNION |
||
453 | ( |
||
454 | SELECT q.id, r.c_id FROM $table_qui_que q |
||
455 | LEFT OUTER JOIN $table_rel r |
||
456 | ON (q.c_id = r.c_id AND q.id = r.question_id) |
||
457 | WHERE q.c_id = ".$this->course_id." AND r.question_id is null |
||
458 | ) |
||
459 | UNION |
||
460 | ( |
||
461 | SELECT q.id, r.c_id FROM $table_qui_que q |
||
462 | INNER JOIN $table_rel r |
||
463 | ON (q.c_id = r.c_id AND q.id = r.question_id) |
||
464 | WHERE r.c_id = ".$this->course_id." AND (r.exercice_id = '-1' OR r.exercice_id = '0') |
||
465 | )"; |
||
466 | $db_result = Database::query($sql); |
||
467 | if (Database::num_rows($db_result) > 0) { |
||
468 | $orphan_ids = []; |
||
469 | while ($obj = Database::fetch_object($db_result)) { |
||
470 | $orphan_ids[] = $obj->id; |
||
471 | } |
||
472 | $orphan_ids = implode(',', $orphan_ids); |
||
473 | $sql = "DELETE FROM ".$table_rel." |
||
474 | WHERE c_id = ".$this->course_id." AND question_id IN(".$orphan_ids.")"; |
||
475 | Database::query($sql); |
||
476 | $sql = "DELETE FROM ".$table_qui_ans." |
||
477 | WHERE c_id = ".$this->course_id." AND question_id IN(".$orphan_ids.")"; |
||
478 | Database::query($sql); |
||
479 | $sql = "DELETE FROM ".$table_qui_que." |
||
480 | WHERE c_id = ".$this->course_id." AND id IN(".$orphan_ids.")"; |
||
481 | Database::query($sql); |
||
482 | } |
||
483 | // Also delete questions categories and options |
||
484 | $sql = "DELETE FROM $table_qui_que_rel_cat WHERE c_id = ".$this->course_id; |
||
485 | Database::query($sql); |
||
486 | $sql = "DELETE FROM $table_qui_que_cat WHERE c_id = ".$this->course_id; |
||
487 | Database::query($sql); |
||
488 | $sql = "DELETE FROM $table_qui_que_opt WHERE c_id = ".$this->course_id; |
||
489 | Database::query($sql); |
||
490 | } |
||
491 | |||
492 | // Quizzes previously deleted are, in fact, kept with a status |
||
493 | // (active field) of "-1". Delete those, now. |
||
494 | $sql = "DELETE FROM ".$table_qui." WHERE c_id = ".$this->course_id." AND active = -1"; |
||
495 | Database::query($sql); |
||
496 | } |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * Recycle tests categories. |
||
501 | */ |
||
502 | public function recycle_test_category() |
||
503 | { |
||
504 | if (isset($this->course->resources[RESOURCE_TEST_CATEGORY])) { |
||
505 | foreach ($this->course->resources[RESOURCE_TEST_CATEGORY] as $tab_test_cat) { |
||
506 | $obj_cat = new TestCategory(); |
||
507 | $obj_cat->removeCategory($tab_test_cat->source_id); |
||
508 | } |
||
509 | } |
||
510 | } |
||
511 | |||
512 | /** |
||
513 | * Recycle surveys - removes everything. |
||
514 | */ |
||
515 | public function recycle_surveys() |
||
544 | } |
||
545 | } |
||
546 | } |
||
547 | |||
548 | /** |
||
549 | * Recycle learning paths. |
||
550 | */ |
||
551 | public function recycle_learnpaths() |
||
611 | } |
||
612 | } |
||
613 | } |
||
614 | |||
615 | /** |
||
616 | * Recycle selected learning path categories and dissociate learning paths |
||
617 | * that are associated with it. |
||
618 | */ |
||
619 | public function recycle_learnpath_categories() |
||
620 | { |
||
621 | $learningPathTable = Database::get_course_table(TABLE_LP_MAIN); |
||
622 | $learningPathCategoryTable = Database::get_course_table(TABLE_LP_CATEGORY); |
||
623 | foreach ($this->course->resources[RESOURCE_LEARNPATH_CATEGORY] as $id => $learnpathCategory) { |
||
624 | $categoryId = $learnpathCategory->object->getId(); |
||
625 | // Dissociate learning paths from categories that will be deleted |
||
626 | $sql = "UPDATE $learningPathTable SET category_id = 0 WHERE category_id = ".$categoryId; |
||
627 | Database::query($sql); |
||
628 | $sql = "DELETE FROM $learningPathCategoryTable WHERE iid = ".$categoryId; |
||
629 | error_log($sql); |
||
630 | Database::query($sql); |
||
631 | } |
||
632 | |||
633 | } |
||
634 | |||
635 | /** |
||
636 | * Delete course description. |
||
637 | */ |
||
638 | public function recycle_cours_description() |
||
639 | { |
||
640 | if ($this->course->has_resources(RESOURCE_COURSEDESCRIPTION)) { |
||
641 | $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION); |
||
642 | $ids = implode(',', array_filter(array_keys($this->course->resources[RESOURCE_COURSEDESCRIPTION]))); |
||
643 | if (!empty($ids)) { |
||
644 | $sql = "DELETE FROM $table |
||
645 | WHERE c_id = ".$this->course_id." AND id IN(".$ids.")"; |
||
646 | Database::query($sql); |
||
647 | } |
||
648 | } |
||
649 | } |
||
650 | |||
651 | /** |
||
652 | * Recycle Thematics. |
||
653 | */ |
||
654 | public function recycle_thematic($session_id = 0) |
||
655 | { |
||
656 | if ($this->course->has_resources(RESOURCE_THEMATIC)) { |
||
657 | $table_thematic = Database::get_course_table(TABLE_THEMATIC); |
||
658 | $table_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE); |
||
659 | $table_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN); |
||
660 | |||
661 | $resources = $this->course->resources; |
||
662 | foreach ($resources[RESOURCE_THEMATIC] as $last_id => $thematic) { |
||
663 | if (is_numeric($last_id)) { |
||
664 | foreach ($thematic->thematic_advance_list as $thematic_advance) { |
||
665 | $cond = [ |
||
666 | 'id = ? AND c_id = ?' => [ |
||
667 | $thematic_advance['id'], |
||
668 | $this->course_id, |
||
669 | ], |
||
670 | ]; |
||
671 | api_item_property_update( |
||
672 | $this->course_info, |
||
673 | 'thematic_advance', |
||
674 | $thematic_advance['id'], |
||
675 | 'ThematicAdvanceDeleted', |
||
676 | api_get_user_id() |
||
677 | ); |
||
678 | Database::delete($table_thematic_advance, $cond); |
||
679 | } |
||
680 | |||
681 | foreach ($thematic->thematic_plan_list as $thematic_plan) { |
||
682 | $cond = [ |
||
683 | 'id = ? AND c_id = ?' => [ |
||
684 | $thematic_plan['id'], |
||
685 | $this->course_id, |
||
686 | ], |
||
687 | ]; |
||
688 | api_item_property_update( |
||
689 | $this->course_info, |
||
690 | 'thematic_plan', |
||
691 | $thematic_advance['id'], |
||
692 | 'ThematicPlanDeleted', |
||
693 | api_get_user_id() |
||
694 | ); |
||
695 | Database::delete($table_thematic_plan, $cond); |
||
696 | } |
||
697 | $cond = [ |
||
698 | 'id = ? AND c_id = ?' => [ |
||
699 | $last_id, |
||
700 | $this->course_id, |
||
701 | ], |
||
702 | ]; |
||
703 | api_item_property_update( |
||
704 | $this->course_info, |
||
705 | 'thematic', |
||
706 | $last_id, |
||
707 | 'ThematicDeleted', |
||
708 | api_get_user_id() |
||
709 | ); |
||
710 | Database::delete($table_thematic, $cond); |
||
711 | } |
||
712 | } |
||
713 | } |
||
714 | } |
||
715 | |||
716 | /** |
||
717 | * Recycle Attendances. |
||
718 | */ |
||
719 | public function recycle_attendance($session_id = 0) |
||
740 | ); |
||
741 | } |
||
742 | } |
||
743 | } |
||
744 | } |
||
745 | |||
746 | /** |
||
747 | * Recycle Works. |
||
748 | */ |
||
749 | public function recycle_work($session_id = 0) |
||
772 | ); |
||
773 | } |
||
774 | } |
||
778 |