Passed
Push — master ( 2b8881...05b235 )
by Yannick
13:59 queued 03:52
created

CourseRecycler::recycle_learnpath_categories()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 12
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CourseBundle\Component\CourseCopy;
5
6
use Database;
7
use TestCategory;
8
9
/**
10
 * Class to delete items from a Chamilo-course.
11
 *
12
 * @author Bart Mollet <[email protected]>
13
 *
14
 * @package chamilo.backup
15
 */
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)
31
    {
32
        $this->course = $course;
33
        $this->course_info = api_get_course_info($this->course->code);
0 ignored issues
show
Bug Best Practice introduced by
The property course_info does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34
        $this->course_id = $this->course_info['real_id'];
0 ignored issues
show
Bug Best Practice introduced by
The property course_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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();
0 ignored issues
show
Bug introduced by
The method recycle_learnpath_categories() does not exist on Chamilo\CourseBundle\Com...urseCopy\CourseRecycler. Did you maybe mean recycle()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
        $this->/** @scrutinizer ignore-call */ 
70
               recycle_learnpath_categories();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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()
136
    {
137
        if ($this->course->has_resources(RESOURCE_WIKI)) {
138
            $table_wiki = Database::get_course_table(TABLE_WIKI);
139
            $table_wiki_conf = Database::get_course_table(TABLE_WIKI_CONF);
140
            $pages = [];
141
            foreach ($this->course->resources[RESOURCE_WIKI] as $resource) {
142
                $pages[] = $resource->page_id;
143
            }
144
145
            $wiki_ids = implode(',', array_filter(array_keys($this->course->resources[RESOURCE_WIKI])));
146
            if (!empty($wiki_ids)) {
147
                $page_ids = implode(',', $pages);
148
149
                $sql = "DELETE FROM ".$table_wiki."
150
                        WHERE c_id = ".$this->course_id." AND id IN(".$wiki_ids.")";
151
                Database::query($sql);
152
153
                $sql = "DELETE FROM ".$table_wiki_conf."
154
                        WHERE c_id = ".$this->course_id." AND page_id IN(".$page_ids.")";
155
                Database::query($sql);
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()
196
    {
197
        $table_category = Database::get_course_table(TABLE_FORUM_CATEGORY);
198
        $table_forum = Database::get_course_table(TABLE_FORUM);
199
        $table_thread = Database::get_course_table(TABLE_FORUM_THREAD);
200
        $table_post = Database::get_course_table(TABLE_FORUM_POST);
201
        $table_attachment = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
202
        $table_notification = Database::get_course_table(TABLE_FORUM_NOTIFICATION);
203
        $table_mail_queue = Database::get_course_table(TABLE_FORUM_MAIL_QUEUE);
204
        $table_thread_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY);
205
        $table_thread_qualify_log = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY_LOG);
206
207
        if ($this->type === 'full_backup') {
208
            $sql = "DELETE FROM ".$table_category." WHERE c_id = ".$this->course_id;
209
            Database::query($sql);
210
            $sql = "DELETE FROM ".$table_forum." WHERE c_id = ".$this->course_id;
211
            Database::query($sql);
212
            $sql = "DELETE FROM ".$table_thread." WHERE c_id = ".$this->course_id;
213
            Database::query($sql);
214
            $sql = "DELETE FROM ".$table_post." WHERE c_id = ".$this->course_id;
215
            Database::query($sql);
216
            $sql = "DELETE FROM ".$table_attachment." WHERE c_id = ".$this->course_id;
217
            Database::query($sql);
218
            $sql = "DELETE FROM ".$table_notification." WHERE c_id = ".$this->course_id;
219
            Database::query($sql);
220
            $sql = "DELETE FROM ".$table_mail_queue." WHERE c_id = ".$this->course_id;
221
            Database::query($sql);
222
            $sql = "DELETE FROM ".$table_thread_qualify." WHERE c_id = ".$this->course_id;
223
            Database::query($sql);
224
            $sql = "DELETE FROM ".$table_thread_qualify_log." WHERE c_id = ".$this->course_id;
225
            Database::query($sql);
226
            $sql = "DELETE FROM ".$table_thread_qualify_log." WHERE c_id = ".$this->course_id;
227
            Database::query($sql);
228
        }
229
230
        if ($this->course->has_resources(RESOURCE_FORUMCATEGORY)) {
231
            $forum_ids = implode(',', array_filter(array_keys($this->course->resources[RESOURCE_FORUMCATEGORY])));
232
            if (!empty($forum_ids)) {
233
                $sql = "DELETE FROM ".$table_category."
234
                        WHERE c_id = ".$this->course_id." AND cat_id IN(".$forum_ids.");";
235
                Database::query($sql);
236
            }
237
        }
238
239
        if ($this->course->has_resources(RESOURCE_FORUM)) {
240
            $forum_ids = implode(',', array_filter(array_keys($this->course->resources[RESOURCE_FORUM])));
241
242
            if (empty($forum_ids)) {
243
                return false;
244
            }
245
246
            $sql = "DELETE FROM $table_attachment USING $table_attachment
247
                    INNER JOIN $table_post
248
                    WHERE   ".$table_post.".c_id = ".$this->course_id." AND
249
                            ".$table_attachment.".c_id = ".$this->course_id." AND
250
                            ".$table_attachment.".post_id = ".$table_post.".post_id".
251
                " AND ".$table_post.".forum_id IN(".$forum_ids.");";
252
            Database::query($sql);
253
254
            $sql = "DELETE FROM ".$table_mail_queue." USING ".$table_mail_queue." INNER JOIN $table_post
255
                    WHERE
256
                        ".$table_post.".c_id = ".$this->course_id." AND
257
                        ".$table_mail_queue.".c_id = ".$this->course_id." AND
258
                        ".$table_mail_queue.".post_id = ".$table_post.".post_id AND
259
                        ".$table_post.".forum_id IN(".$forum_ids.");";
260
            Database::query($sql);
261
262
            // Just in case, deleting in the same table using thread_id as record-linker.
263
            $sql = "DELETE FROM $table_mail_queue
264
                    USING ".$table_mail_queue." INNER JOIN $table_thread
265
                    WHERE
266
                        $table_mail_queue.c_id = ".$this->course_id." AND
267
                        $table_thread.c_id = ".$this->course_id." AND
268
                        $table_mail_queue.thread_id = ".$table_thread.".thread_id AND
269
                        $table_thread.forum_id IN(".$forum_ids.");";
270
            Database::query($sql);
271
272
            $sql = "DELETE FROM $table_thread_qualify
273
                    USING $table_thread_qualify INNER JOIN $table_thread
274
                    WHERE
275
                        $table_thread_qualify.c_id = ".$this->course_id." AND
276
                        $table_thread.c_id = ".$this->course_id." AND
277
                        $table_thread_qualify.thread_id = $table_thread.thread_id AND
278
                        $table_thread.forum_id IN(".$forum_ids.");";
279
            Database::query($sql);
280
281
            $sql = "DELETE FROM ".$table_thread_qualify_log.
282
                " USING ".$table_thread_qualify_log." INNER JOIN ".$table_thread.
283
                " WHERE
284
                    $table_thread_qualify_log.c_id = ".$this->course_id." AND
285
                    $table_thread.c_id = ".$this->course_id." AND
286
                    ".$table_thread_qualify_log.".thread_id = ".$table_thread.".thread_id AND
287
                    ".$table_thread.".forum_id IN(".$forum_ids.");";
288
            Database::query($sql);
289
290
            $sql = "DELETE FROM ".$table_notification."
291
                    WHERE c_id = ".$this->course_id." AND forum_id IN(".$forum_ids.")";
292
            Database::query($sql);
293
294
            $sql = "DELETE FROM ".$table_post."
295
                    WHERE c_id = ".$this->course_id." AND forum_id IN(".$forum_ids.")";
296
            Database::query($sql);
297
298
            $sql = "DELETE FROM ".$table_thread."
299
                    WHERE c_id = ".$this->course_id." AND forum_id IN(".$forum_ids.")";
300
            Database::query($sql);
301
302
            $sql = "DELETE FROM ".$table_forum."
303
                    WHERE c_id = ".$this->course_id." AND forum_id IN(".$forum_ids.")";
304
            Database::query($sql);
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()
516
    {
517
        if ($this->course->has_resources(RESOURCE_SURVEY)) {
518
            $table_survey = Database::get_course_table(TABLE_SURVEY);
519
            $table_survey_q = Database::get_course_table(TABLE_SURVEY_QUESTION);
520
            $table_survey_q_o = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
521
            $table_survey_a = Database::get_course_Table(TABLE_SURVEY_ANSWER);
522
            $table_survey_i = Database::get_course_table(TABLE_SURVEY_INVITATION);
523
            $sql = "DELETE FROM $table_survey_i
524
                    WHERE c_id = ".$this->course_id;
525
            Database::query($sql);
526
527
            $ids = implode(',', array_filter(array_keys($this->course->resources[RESOURCE_SURVEY])));
528
            if (!empty($ids)) {
529
                $sql = "DELETE FROM $table_survey_a
530
                        WHERE c_id = ".$this->course_id."  AND survey_id IN(".$ids.")";
531
                Database::query($sql);
532
533
                $sql = "DELETE FROM $table_survey_q_o
534
                        WHERE c_id = ".$this->course_id."  AND survey_id IN(".$ids.")";
535
                Database::query($sql);
536
537
                $sql = "DELETE FROM $table_survey_q
538
                        WHERE c_id = ".$this->course_id."  AND survey_id IN(".$ids.")";
539
                Database::query($sql);
540
541
                $sql = "DELETE FROM $table_survey
542
                        WHERE c_id = ".$this->course_id."  AND survey_id IN(".$ids.")";
543
                Database::query($sql);
544
            }
545
        }
546
    }
547
548
    /**
549
     * Recycle learning paths.
550
     */
551
    public function recycle_learnpaths()
552
    {
553
        if ($this->course->has_resources(RESOURCE_LEARNPATH)) {
554
            $table_main = Database::get_course_table(TABLE_LP_MAIN);
555
            $table_item = Database::get_course_table(TABLE_LP_ITEM);
556
            $table_view = Database::get_course_table(TABLE_LP_VIEW);
557
            $table_iv = Database::get_course_table(TABLE_LP_ITEM_VIEW);
558
            $table_iv_int = Database::get_course_table(TABLE_LP_IV_INTERACTION);
559
            $table_tool = Database::get_course_table(TABLE_TOOL_LIST);
560
561
            foreach ($this->course->resources[RESOURCE_LEARNPATH] as $id => $learnpath) {
562
                // See task #875.
563
                if ($learnpath->lp_type == 2) {
564
                    // This is a learning path of SCORM type.
565
                    // A sanity check for avoiding removal of the parent folder scorm/
566
                    if (trim($learnpath->path) != '') {
567
                        // when $learnpath->path value is incorrect for some reason.
568
                        // The directory trat contains files of the SCORM package is to be deleted.
569
                        $scorm_package_dir = realpath($this->course->path.'scorm/'.$learnpath->path);
570
                        rmdirr($scorm_package_dir);
571
                    }
572
                }
573
574
                //remove links from course homepage
575
                $sql = "DELETE FROM $table_tool
576
                        WHERE
577
                            c_id = ".$this->course_id." AND
578
                            link LIKE '%lp_controller.php%lp_id=$id%' AND
579
                            image='scormbuilder.gif'";
580
                Database::query($sql);
581
                //remove elements from lp_* tables (from bottom-up)
582
                // by removing interactions, then item_view, then views and items, then paths
583
                $sql_items = "SELECT id FROM $table_item
584
                              WHERE c_id = ".$this->course_id."  AND lp_id=$id";
585
                $res_items = Database::query($sql_items);
586
                while ($row_item = Database::fetch_array($res_items)) {
587
                    //get item views
588
                    $sql_iv = "SELECT id FROM $table_iv
589
                               WHERE  c_id = ".$this->course_id."  AND lp_item_id=".$row_item['id'];
590
                    $res_iv = Database::query($sql_iv);
591
                    while ($row_iv = Database::fetch_array($res_iv)) {
592
                        //delete interactions
593
                        $sql_iv_int_del = "DELETE FROM $table_iv_int
594
                                           WHERE c_id = ".$this->course_id."  AND lp_iv_id = ".$row_iv['id'];
595
                        Database::query($sql_iv_int_del);
596
                    }
597
                    //delete item views
598
                    $sql_iv_del = "DELETE FROM $table_iv
599
                                   WHERE c_id = ".$this->course_id."  AND lp_item_id=".$row_item['id'];
600
                    Database::query($sql_iv_del);
601
                }
602
                //delete items
603
                $sql_items_del = "DELETE FROM $table_item WHERE c_id = ".$this->course_id."  AND lp_id=$id";
604
                Database::query($sql_items_del);
605
                //delete views
606
                $sql_views_del = "DELETE FROM $table_view WHERE c_id = ".$this->course_id."  AND lp_id=$id";
607
                Database::query($sql_views_del);
608
                //delete lps
609
                $sql_del = "DELETE FROM $table_main WHERE c_id = ".$this->course_id."  AND id = $id";
610
                Database::query($sql_del);
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)
0 ignored issues
show
Unused Code introduced by
The parameter $session_id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

654
    public function recycle_thematic(/** @scrutinizer ignore-unused */ $session_id = 0)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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'],
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $thematic_advance does not seem to be defined for all execution paths leading up to this point.
Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $session_id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

719
    public function recycle_attendance(/** @scrutinizer ignore-unused */ $session_id = 0)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
720
    {
721
        if ($this->course->has_resources(RESOURCE_ATTENDANCE)) {
722
            $table_attendance = Database::get_course_table(TABLE_ATTENDANCE);
723
            $table_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
724
725
            $resources = $this->course->resources;
726
            foreach ($resources[RESOURCE_ATTENDANCE] as $last_id => $obj) {
727
                if (is_numeric($last_id)) {
728
                    foreach ($obj->attendance_calendar as $attendance_calendar) {
729
                        $cond = ['id = ? AND c_id = ? ' => [$attendance_calendar['id'], $this->course_id]];
730
                        Database::delete($table_attendance_calendar, $cond);
731
                    }
732
                    $cond = ['id = ? AND c_id = ?' => [$last_id, $this->course_id]];
733
                    Database::delete($table_attendance, $cond);
734
                    api_item_property_update(
735
                        $this->course_info,
736
                        TOOL_ATTENDANCE,
737
                        $last_id,
738
                        'AttendanceDeleted',
739
                        api_get_user_id()
740
                    );
741
                }
742
            }
743
        }
744
    }
745
746
    /**
747
     * Recycle Works.
748
     */
749
    public function recycle_work($session_id = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $session_id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

749
    public function recycle_work(/** @scrutinizer ignore-unused */ $session_id = 0)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
750
    {
751
        if ($this->course->has_resources(RESOURCE_WORK)) {
752
            $table_work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
753
            $table_work_assignment = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
754
755
            $resources = $this->course->resources;
756
            foreach ($resources[RESOURCE_WORK] as $last_id => $obj) {
757
                if (is_numeric($last_id)) {
758
                    $cond = ['publication_id = ? AND c_id = ? ' => [$last_id, $this->course_id]];
759
                    Database::delete($table_work_assignment, $cond);
760
                    // The following also deletes student tasks
761
                    $cond = ['parent_id = ? AND c_id = ?' => [$last_id, $this->course_id]];
762
                    Database::delete($table_work, $cond);
763
                    // Finally, delete the main task registry
764
                    $cond = ['id = ? AND c_id = ?' => [$last_id, $this->course_id]];
765
                    Database::delete($table_work, $cond);
766
                    api_item_property_update(
767
                        $this->course_info,
768
                        TOOL_STUDENTPUBLICATION,
769
                        $last_id,
770
                        'StudentPublicationDeleted',
771
                        api_get_user_id()
772
                    );
773
                }
774
            }
775
        }
776
    }
777
}
778