Completed
Push — master ( bd22e5...ae5621 )
by Julito
12:43
created

CourseBuilder::set_tools_to_build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
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 Category;
7
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Announcement;
8
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Asset;
9
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Attendance;
10
use Chamilo\CourseBundle\Component\CourseCopy\Resources\CalendarEvent;
11
use Chamilo\CourseBundle\Component\CourseCopy\Resources\CourseCopyLearnpath;
12
use Chamilo\CourseBundle\Component\CourseCopy\Resources\CourseCopyTestCategory;
13
use Chamilo\CourseBundle\Component\CourseCopy\Resources\CourseDescription;
14
use Chamilo\CourseBundle\Component\CourseCopy\Resources\CourseSession;
15
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Document;
16
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Forum;
17
use Chamilo\CourseBundle\Component\CourseCopy\Resources\ForumCategory;
18
use Chamilo\CourseBundle\Component\CourseCopy\Resources\ForumPost;
19
use Chamilo\CourseBundle\Component\CourseCopy\Resources\ForumTopic;
20
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Glossary;
21
use Chamilo\CourseBundle\Component\CourseCopy\Resources\GradeBookBackup;
22
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Link;
23
use Chamilo\CourseBundle\Component\CourseCopy\Resources\LinkCategory;
24
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Quiz;
25
use Chamilo\CourseBundle\Component\CourseCopy\Resources\QuizQuestion;
26
use Chamilo\CourseBundle\Component\CourseCopy\Resources\QuizQuestionOption;
27
use Chamilo\CourseBundle\Component\CourseCopy\Resources\ScormDocument;
28
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Survey;
29
use Chamilo\CourseBundle\Component\CourseCopy\Resources\SurveyQuestion;
30
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Thematic;
31
use Chamilo\CourseBundle\Component\CourseCopy\Resources\ToolIntro;
32
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Wiki;
33
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Work;
34
use CourseManager;
35
use Database;
36
use Link as LinkManager;
37
use TestCategory;
38
39
/**
40
 * Class CourseBuilder
41
 * Builds a course-object from a Chamilo-course.
42
 *
43
 * @author Bart Mollet <[email protected]>
44
 *
45
 * @package chamilo.backup
46
 */
47
class CourseBuilder
48
{
49
    /** @var Course */
50
    public $course;
51
52
    /* With this array you can filter the tools you want to be parsed by
53
    default all tools are included */
54
    public $tools_to_build = [
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
        'learnpaths',
67
        'links',
68
        'surveys',
69
        'tool_intro',
70
        'thematic',
71
        'wiki',
72
        'works',
73
        'gradebook',
74
    ];
75
76
    /* With this array you can filter wich elements of the tools are going
77
    to be added in the course obj (only works with LPs) */
78
    public $specific_id_list = [];
79
80
    /**
81
     * Create a new CourseBuilder.
82
     *
83
     * @param string $type
84
     * @param null   $course
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $course is correct as it would always require null to be passed?
Loading history...
85
     */
86
    public function __construct($type = '', $course = null)
87
    {
88
        $_course = api_get_course_info();
89
90
        if (!empty($course['official_code'])) {
91
            $_course = $course;
92
        }
93
94
        $this->course = new Course();
95
        $this->course->code = $_course['code'];
96
        $this->course->type = $type;
0 ignored issues
show
Bug introduced by
The property type does not seem to exist on Chamilo\CourseBundle\Component\CourseCopy\Course.
Loading history...
97
        $this->course->path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/';
98
        $this->course->backup_path = api_get_path(SYS_COURSE_PATH).$_course['path'];
0 ignored issues
show
Bug Best Practice introduced by
The property backup_path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
99
        $this->course->encoding = api_get_system_encoding();
100
        $this->course->info = $_course;
0 ignored issues
show
Bug introduced by
The property info does not seem to exist on Chamilo\CourseBundle\Component\CourseCopy\Course.
Loading history...
101
    }
102
103
    /**
104
     * @param array $array
105
     */
106
    public function set_tools_to_build($array)
107
    {
108
        $this->tools_to_build = $array;
109
    }
110
111
    /**
112
     * @param array $array
113
     */
114
    public function set_tools_specific_id_list($array)
115
    {
116
        $this->specific_id_list = $array;
117
    }
118
119
    /**
120
     * Get the created course.
121
     *
122
     * @return course The course
123
     */
124
    public function get_course()
125
    {
126
        return $this->course;
127
    }
128
129
    /**
130
     * Build the course-object.
131
     *
132
     * @param int    $session_id
133
     * @param string $courseCode
134
     * @param bool   $withBaseContent true if you want to get the elements that exists in the course and
135
     *                                in the session, (session_id = 0 or session_id = X)
136
     *
137
     * @return Course The course object structure
138
     */
139
    public function build(
140
        $session_id = 0,
141
        $courseCode = '',
142
        $withBaseContent = false
143
    ) {
144
        $table_properties = Database::get_course_table(TABLE_ITEM_PROPERTY);
145
        $course = api_get_course_info($courseCode);
146
        $courseId = $course['real_id'];
147
148
        foreach ($this->tools_to_build as $tool) {
149
            $function_build = 'build_'.$tool;
150
            $specificIdList = isset($this->specific_id_list[$tool]) ? $this->specific_id_list[$tool] : null;
151
152
            $this->$function_build(
153
                $session_id,
154
                $courseId,
155
                $withBaseContent,
156
                $specificIdList
157
            );
158
        }
159
160
        // Add asset
161
        if ($course['course_image_source'] && basename($course['course_image_source']) != 'course.png') {
162
            // Add course image courses/XXX/course-pic85x85.png
163
            $asset = new Asset(
164
                $course['course_image_source'],
165
                basename($course['course_image_source']),
166
                basename($course['course_image_source'])
167
            );
168
            $this->course->add_resource($asset);
169
170
            $asset = new Asset(
171
                $course['course_image_large_source'],
172
                basename($course['course_image_large_source']),
173
                basename($course['course_image_large_source'])
174
            );
175
            $this->course->add_resource($asset);
176
        }
177
178
        // Once we've built the resources array a bit more, try to get items
179
        //  from the item_property table and order them in the "resources" array
180
        foreach ($this->course->resources as $type => $resources) {
181
            foreach ($resources as $id => $resource) {
182
                $tool = $resource->get_tool();
183
                if ($tool != null) {
184
                    $sql = "SELECT * FROM $table_properties
185
                            WHERE
186
                                c_id = $courseId AND
187
                                tool = '".$tool."' AND
188
                                ref = '".$resource->get_id()."'";
189
                    $res = Database::query($sql);
190
                    $all_properties = [];
191
                    while ($item_property = Database::fetch_array($res)) {
192
                        $all_properties[] = $item_property;
193
                    }
194
                    $this->course->resources[$type][$id]->item_properties = $all_properties;
195
                }
196
            }
197
        }
198
199
        return $this->course;
200
    }
201
202
    /**
203
     * Build the documents.
204
     *
205
     * @param int   $session_id
206
     * @param int   $courseId
207
     * @param bool  $withBaseContent
208
     * @param array $idList
209
     */
210
    public function build_documents(
211
        $session_id = 0,
212
        $courseId = 0,
213
        $withBaseContent = false,
214
        $idList = []
215
    ) {
216
        $table_doc = Database::get_course_table(TABLE_DOCUMENT);
217
        $table_prop = Database::get_course_table(TABLE_ITEM_PROPERTY);
218
219
        // Remove chat_files and shared_folder files
220
        $avoid_paths = " path NOT LIKE '/shared_folder%' AND
221
                         path NOT LIKE '/chat_files%' ";
222
223
        $documentCondition = '';
224
        if (!empty($idList)) {
225
            $idList = array_unique($idList);
226
            $idList = array_map('intval', $idList);
227
            $documentCondition = ' d.iid IN ("'.implode('","', $idList).'") AND ';
228
        }
229
230
        if (!empty($courseId) && !empty($session_id)) {
231
            $session_id = intval($session_id);
232
            if ($withBaseContent) {
233
                $session_condition = api_get_session_condition(
234
                    $session_id,
235
                    true,
236
                    true,
237
                    'd.session_id'
238
                );
239
            } else {
240
                $session_condition = api_get_session_condition(
241
                    $session_id,
242
                    true,
243
                    false,
244
                    'd.session_id'
245
                );
246
            }
247
248
            if (!empty($this->course->type) && $this->course->type == 'partial') {
0 ignored issues
show
Bug introduced by
The property type does not seem to exist on Chamilo\CourseBundle\Component\CourseCopy\Course.
Loading history...
249
                $sql = "SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size
250
                        FROM $table_doc d 
251
                        INNER JOIN $table_prop p
252
                        ON (p.ref = d.id AND d.c_id = p.c_id)
253
                        WHERE
254
                            d.c_id = $courseId AND
255
                            p.c_id = $courseId AND
256
                            tool = '".TOOL_DOCUMENT."' AND
257
                            $documentCondition
258
                            p.visibility != 2 AND
259
                            path NOT LIKE '/images/gallery%' AND
260
                            $avoid_paths
261
                            $session_condition
262
                        ORDER BY path";
263
            } else {
264
                $sql = "SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size
265
                        FROM $table_doc d 
266
                        INNER JOIN $table_prop p
267
                        ON (p.ref = d.id AND d.c_id = p.c_id)
268
                        WHERE
269
                            d.c_id = $courseId AND
270
                            p.c_id = $courseId AND
271
                            tool = '".TOOL_DOCUMENT."' AND
272
                            $documentCondition
273
                            $avoid_paths AND
274
                            p.visibility != 2 $session_condition
275
                        ORDER BY path";
276
            }
277
278
            $db_result = Database::query($sql);
279
            while ($obj = Database::fetch_object($db_result)) {
280
                $doc = new Document(
281
                    $obj->id,
282
                    $obj->path,
283
                    $obj->comment,
284
                    $obj->title,
285
                    $obj->filetype,
286
                    $obj->size
287
                );
288
                $this->course->add_resource($doc);
289
            }
290
        } else {
291
            if (!empty($this->course->type) && $this->course->type == 'partial') {
292
                $sql = "SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size
293
                        FROM $table_doc d 
294
                        INNER JOIN $table_prop p
295
                        ON (p.ref = d.id AND d.c_id = p.c_id)
296
                        WHERE
297
                            d.c_id = $courseId AND
298
                            p.c_id = $courseId AND
299
                            tool = '".TOOL_DOCUMENT."' AND
300
                            $documentCondition
301
                            p.visibility != 2 AND
302
                            path NOT LIKE '/images/gallery%' AND
303
                            $avoid_paths AND
304
                            (d.session_id = 0 OR d.session_id IS NULL)
305
                        ORDER BY path";
306
            } else {
307
                $sql = "SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size
308
                        FROM $table_doc d 
309
                        INNER JOIN $table_prop p
310
                        ON (p.ref = d.id AND d.c_id = p.c_id)
311
                        WHERE
312
                            d.c_id = $courseId AND
313
                            p.c_id = $courseId AND
314
                            tool = '".TOOL_DOCUMENT."' AND
315
                            $documentCondition
316
                            p.visibility != 2 AND
317
                            $avoid_paths AND
318
                            (d.session_id = 0 OR d.session_id IS NULL)
319
                        ORDER BY path";
320
            }
321
322
            $result = Database::query($sql);
323
            while ($obj = Database::fetch_object($result)) {
324
                $doc = new Document(
325
                    $obj->id,
326
                    $obj->path,
327
                    $obj->comment,
328
                    $obj->title,
329
                    $obj->filetype,
330
                    $obj->size
331
                );
332
                $this->course->add_resource($doc);
333
            }
334
        }
335
    }
336
337
    /**
338
     * Build the forums.
339
     *
340
     * @param int   $session_id      Internal session ID
341
     * @param int   $courseId        Internal course ID
342
     * @param bool  $withBaseContent Whether to include content from the course without session or not
343
     * @param array $idList          If you want to restrict the structure to only the given IDs
344
     */
345
    public function build_forums(
346
        $session_id = 0,
347
        $courseId = 0,
348
        $withBaseContent = false,
349
        $idList = []
350
    ) {
351
        $table = Database::get_course_table(TABLE_FORUM);
352
        $sessionCondition = api_get_session_condition(
353
            $session_id,
354
            true,
355
            $withBaseContent
356
        );
357
358
        $idCondition = '';
359
        if (!empty($idList)) {
360
            $idList = array_unique($idList);
361
            $idList = array_map('intval', $idList);
362
            $idCondition = ' AND iid IN ("'.implode('","', $idList).'") ';
363
        }
364
365
        $sql = "SELECT * FROM $table WHERE c_id = $courseId $sessionCondition $idCondition";
366
        $sql .= " ORDER BY forum_title, forum_category";
367
        $db_result = Database::query($sql);
368
        while ($obj = Database::fetch_object($db_result)) {
369
            $forum = new Forum($obj);
370
            $this->course->add_resource($forum);
371
        }
372
    }
373
374
    /**
375
     * Build a forum-category.
376
     *
377
     * @param int   $session_id      Internal session ID
378
     * @param int   $courseId        Internal course ID
379
     * @param bool  $withBaseContent Whether to include content from the course without session or not
380
     * @param array $idList          If you want to restrict the structure to only the given IDs
381
     */
382
    public function build_forum_category(
383
        $session_id = 0,
384
        $courseId = 0,
385
        $withBaseContent = false,
386
        $idList = []
387
    ) {
388
        $table = Database::get_course_table(TABLE_FORUM_CATEGORY);
389
390
        $sessionCondition = api_get_session_condition(
391
            $session_id,
392
            true,
393
            $withBaseContent
394
        );
395
396
        $idCondition = '';
397
        if (!empty($idList)) {
398
            $idList = array_unique($idList);
399
            $idList = array_map('intval', $idList);
400
            $idCondition = ' AND iid IN ("'.implode('","', $idList).'") ';
401
        }
402
403
        $sql = "SELECT * FROM $table
404
                WHERE c_id = $courseId $sessionCondition $idCondition
405
                ORDER BY cat_title";
406
407
        $result = Database::query($sql);
408
        while ($obj = Database::fetch_object($result)) {
409
            $forumCategory = new ForumCategory($obj);
410
            $this->course->add_resource($forumCategory);
411
        }
412
    }
413
414
    /**
415
     * Build the forum-topics.
416
     *
417
     * @param int   $session_id      Internal session ID
418
     * @param int   $courseId        Internal course ID
419
     * @param bool  $withBaseContent Whether to include content from the course without session or not
420
     * @param array $idList          If you want to restrict the structure to only the given IDs
421
     */
422
    public function build_forum_topics(
423
        $session_id = 0,
424
        $courseId = 0,
425
        $withBaseContent = false,
426
        $idList = []
427
    ) {
428
        $table = Database::get_course_table(TABLE_FORUM_THREAD);
429
430
        $sessionCondition = api_get_session_condition(
431
            $session_id,
432
            true,
433
            $withBaseContent
434
        );
435
436
        $idCondition = '';
437
        if (!empty($idList)) {
438
            $idList = array_map('intval', $idList);
439
            $idCondition = ' AND iid IN ("'.implode('","', $idList).'") ';
440
        }
441
442
        $sql = "SELECT * FROM $table WHERE c_id = $courseId
443
                $sessionCondition
444
                $idCondition
445
                ORDER BY thread_title ";
446
        $result = Database::query($sql);
447
448
        while ($obj = Database::fetch_object($result)) {
449
            $forumTopic = new ForumTopic($obj);
450
            $this->course->add_resource($forumTopic);
451
            $this->build_forum_posts($courseId, $obj->thread_id, $obj->forum_id);
452
        }
453
    }
454
455
    /**
456
     * Build the forum-posts
457
     * TODO: All tree structure of posts should be built, attachments for example.
458
     *
459
     * @param int   $courseId  Internal course ID
460
     * @param int   $thread_id Internal thread ID
461
     * @param int   $forum_id  Internal forum ID
462
     * @param array $idList
463
     */
464
    public function build_forum_posts(
465
        $courseId = 0,
466
        $thread_id = null,
467
        $forum_id = null,
468
        $idList = []
469
    ) {
470
        $table = Database::get_course_table(TABLE_FORUM_POST);
471
        $courseId = (int) $courseId;
472
        $sql = "SELECT * FROM $table WHERE c_id = $courseId ";
473
        if (!empty($thread_id) && !empty($forum_id)) {
474
            $forum_id = intval($forum_id);
475
            $thread_id = intval($thread_id);
476
            $sql .= " AND thread_id = $thread_id AND forum_id = $forum_id ";
477
        }
478
479
        if (!empty($idList)) {
480
            $idList = array_map('intval', $idList);
481
            $sql .= ' AND iid IN ("'.implode('","', $idList).'") ';
482
        }
483
484
        $sql .= " ORDER BY post_id ASC LIMIT 1";
485
        $db_result = Database::query($sql);
486
        while ($obj = Database::fetch_object($db_result)) {
487
            $forum_post = new ForumPost($obj);
488
            $this->course->add_resource($forum_post);
489
        }
490
    }
491
492
    /**
493
     * Build the links.
494
     *
495
     * @param int   $session_id      Internal session ID
496
     * @param int   $courseId        Internal course ID
497
     * @param bool  $withBaseContent Whether to include content from the course without session or not
498
     * @param array $idList          If you want to restrict the structure to only the given IDs
499
     */
500
    public function build_links(
501
        $session_id = 0,
502
        $courseId = 0,
503
        $withBaseContent = false,
504
        $idList = []
505
    ) {
506
        $categories = LinkManager::getLinkCategories(
507
            $courseId,
508
            $session_id,
509
            $withBaseContent
510
        );
511
512
        // Adding empty category
513
        $categories[] = ['id' => 0];
514
515
        foreach ($categories as $category) {
516
            $this->build_link_category($category);
517
518
            $links = LinkManager::getLinksPerCategory(
519
                $category['id'],
520
                $courseId,
521
                $session_id,
522
                $withBaseContent
523
            );
524
525
            foreach ($links as $item) {
526
                if (!empty($idList)) {
527
                    if (!in_array($item['id'], $idList)) {
528
                        continue;
529
                    }
530
                }
531
532
                $link = new Link(
533
                    $item['id'],
534
                    $item['title'],
535
                    $item['url'],
536
                    $item['description'],
537
                    $item['category_id'],
538
                    $item['on_homepage']
539
                );
540
                $link->target = $item['target'];
541
                $this->course->add_resource($link);
542
                $this->course->resources[RESOURCE_LINK][$item['id']]->add_linked_resource(
543
                    RESOURCE_LINKCATEGORY,
544
                    $item['category_id']
545
                );
546
            }
547
        }
548
    }
549
550
    /**
551
     * Build tool intro.
552
     *
553
     * @param int   $session_id      Internal session ID
554
     * @param int   $courseId        Internal course ID
555
     * @param bool  $withBaseContent Whether to include content from the course without session or not
556
     * @param array $idList          If you want to restrict the structure to only the given IDs
557
     */
558
    public function build_tool_intro(
559
        $session_id = 0,
560
        $courseId = 0,
561
        $withBaseContent = false,
562
        $idList = []
563
    ) {
564
        $table = Database::get_course_table(TABLE_TOOL_INTRO);
565
566
        $sessionCondition = api_get_session_condition(
567
            $session_id,
568
            true,
569
            $withBaseContent
570
        );
571
572
        $courseId = (int) $courseId;
573
574
        $sql = "SELECT * FROM $table
575
                WHERE c_id = $courseId $sessionCondition";
576
577
        $db_result = Database::query($sql);
578
        while ($obj = Database::fetch_object($db_result)) {
579
            $tool_intro = new ToolIntro($obj->id, $obj->intro_text);
580
            $this->course->add_resource($tool_intro);
581
        }
582
    }
583
584
    /**
585
     * Build a link category.
586
     *
587
     * @param int $id       Internal link ID
588
     * @param int $courseId Internal course ID
589
     *
590
     * @return int
591
     */
592
    public function build_link_category($category)
593
    {
594
        if (empty($category) || empty($category['category_title'])) {
595
            return 0;
596
        }
597
598
        $linkCategory = new LinkCategory(
599
            $category['id'],
600
            $category['category_title'],
601
            $category['description'],
602
            $category['display_order']
603
        );
604
        $this->course->add_resource($linkCategory);
605
606
        return $category['id'];
607
    }
608
609
    /**
610
     * Build the Quizzes.
611
     *
612
     * @param int   $session_id      Internal session ID
613
     * @param int   $courseId        Internal course ID
614
     * @param bool  $withBaseContent Whether to include content from the course without session or not
615
     * @param array $idList          If you want to restrict the structure to only the given IDs
616
     */
617
    public function build_quizzes(
618
        $session_id = 0,
619
        $courseId = 0,
620
        $withBaseContent = false,
621
        $idList = []
622
    ) {
623
        $table_qui = Database::get_course_table(TABLE_QUIZ_TEST);
624
        $table_rel = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
625
        $table_doc = Database::get_course_table(TABLE_DOCUMENT);
626
627
        $courseId = (int) $courseId;
628
        $idCondition = '';
629
        if (!empty($idList)) {
630
            $idList = array_map('intval', $idList);
631
            $idCondition = ' iid IN ("'.implode('","', $idList).'") AND ';
632
        }
633
634
        if (!empty($courseId) && !empty($session_id)) {
635
            $session_id = intval($session_id);
636
            if ($withBaseContent) {
637
                $session_condition = api_get_session_condition(
638
                    $session_id,
639
                    true,
640
                    true
641
                );
642
            } else {
643
                $session_condition = api_get_session_condition(
644
                    $session_id,
645
                    true
646
                );
647
            }
648
649
            // Select only quizzes with active = 0 or 1 (not -1 which is for deleted quizzes)
650
            $sql = "SELECT * FROM $table_qui
651
                    WHERE 
652
                      c_id = $courseId AND 
653
                      $idCondition
654
                      active >=0 
655
                      $session_condition ";
656
        } else {
657
            // Select only quizzes with active = 0 or 1 (not -1 which is for deleted quizzes)
658
            $sql = "SELECT * FROM $table_qui
659
                    WHERE 
660
                      c_id = $courseId AND 
661
                      $idCondition
662
                      active >=0 AND 
663
                      (session_id = 0 OR session_id IS NULL)";
664
        }
665
666
        $sql .= 'ORDER BY title';
667
668
        $db_result = Database::query($sql);
669
        while ($obj = Database::fetch_object($db_result)) {
670
            if (strlen($obj->sound) > 0) {
671
                $sql = "SELECT id FROM $table_doc
672
                        WHERE c_id = $courseId AND path = '/audio/".$obj->sound."'";
673
                $res = Database::query($sql);
674
                $doc = Database::fetch_object($res);
675
                $obj->sound = $doc->id;
676
            }
677
            $quiz = new Quiz($obj);
678
            $sql = 'SELECT * FROM '.$table_rel.'
679
                    WHERE c_id = '.$courseId.' AND exercice_id = '.$obj->id;
680
            $db_result2 = Database::query($sql);
681
            while ($obj2 = Database::fetch_object($db_result2)) {
682
                $quiz->add_question($obj2->question_id, $obj2->question_order);
683
            }
684
            $this->course->add_resource($quiz);
685
        }
686
687
        if (!empty($courseId)) {
688
            $this->build_quiz_questions($courseId);
689
        } else {
690
            $this->build_quiz_questions();
691
        }
692
    }
693
694
    /**
695
     * Build the Quiz-Questions.
696
     *
697
     * @param int $courseId Internal course ID
698
     */
699
    public function build_quiz_questions($courseId = 0)
700
    {
701
        $table_qui = Database::get_course_table(TABLE_QUIZ_TEST);
702
        $table_rel = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
703
        $table_que = Database::get_course_table(TABLE_QUIZ_QUESTION);
704
        $table_ans = Database::get_course_table(TABLE_QUIZ_ANSWER);
705
706
        $courseId = (int) $courseId;
707
708
        // Building normal tests.
709
        $sql = "SELECT * FROM $table_que
710
                WHERE c_id = $courseId ";
711
        $result = Database::query($sql);
712
713
        while ($obj = Database::fetch_object($result)) {
714
            // find the question category
715
            // @todo : need to be adapted for multi category questions in 1.10
716
            $question_category_id = TestCategory::getCategoryForQuestion(
717
                $obj->id,
718
                $courseId
719
            );
720
721
            // build the backup resource question object
722
            $question = new QuizQuestion(
723
                $obj->id,
724
                $obj->question,
725
                $obj->description,
726
                $obj->ponderation,
727
                $obj->type,
728
                $obj->position,
729
                $obj->picture,
730
                $obj->level,
731
                $obj->extra,
732
                $question_category_id
733
            );
734
            $question->addPicture($this);
735
736
            $sql = 'SELECT * FROM '.$table_ans.'
737
                    WHERE c_id = '.$courseId.' AND question_id = '.$obj->id;
738
            $db_result2 = Database::query($sql);
739
740
            while ($obj2 = Database::fetch_object($db_result2)) {
741
                $question->add_answer(
742
                    $obj2->id,
743
                    $obj2->answer,
744
                    $obj2->correct,
745
                    $obj2->comment,
746
                    $obj2->ponderation,
747
                    $obj2->position,
748
                    $obj2->hotspot_coordinates,
749
                    $obj2->hotspot_type
750
                );
751
                if ($obj->type == MULTIPLE_ANSWER_TRUE_FALSE) {
752
                    $table_options = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
753
                    $sql = 'SELECT * FROM '.$table_options.'
754
                            WHERE c_id = '.$courseId.' AND question_id = '.$obj->id;
755
                    $db_result3 = Database::query($sql);
756
                    while ($obj3 = Database::fetch_object($db_result3)) {
757
                        $question_option = new QuizQuestionOption($obj3);
758
                        $question->add_option($question_option);
759
                    }
760
                }
761
            }
762
            $this->course->add_resource($question);
763
        }
764
765
        // Building a fictional test for collecting orphan questions.
766
        // When a course is emptied this option should be activated (true).
767
        $build_orphan_questions = !empty($_POST['recycle_option']);
768
769
        // 1st union gets the orphan questions from deleted exercises
770
        // 2nd union gets the orphan questions from question that were deleted in a exercise.
771
        $sql = " (
772
                    SELECT question_id, q.* FROM $table_que q 
773
                    INNER JOIN $table_rel r
774
                    ON (q.c_id = r.c_id AND q.id = r.question_id)
775
                    INNER JOIN $table_qui ex
776
                    ON (ex.id = r.exercice_id AND ex.c_id = r.c_id )
777
                    WHERE ex.c_id = $courseId AND ex.active = '-1'
778
                 )
779
                 UNION
780
                 (
781
                    SELECT question_id, q.* FROM $table_que q 
782
                    left OUTER JOIN $table_rel r
783
                    ON (q.c_id = r.c_id AND q.id = r.question_id)
784
                    WHERE q.c_id = $courseId AND r.question_id is null
785
                 )
786
                 UNION
787
                 (
788
                    SELECT question_id, q.* FROM $table_que q
789
                    INNER JOIN $table_rel r
790
                    ON (q.c_id = r.c_id AND q.id = r.question_id)
791
                    WHERE r.c_id = $courseId AND (r.exercice_id = '-1' OR r.exercice_id = '0')
792
                 )
793
        ";
794
795
        $result = Database::query($sql);
796
        if (Database::num_rows($result) > 0) {
797
            $build_orphan_questions = true;
798
            $orphanQuestionIds = [];
799
            while ($obj = Database::fetch_object($result)) {
800
                // Orphan questions
801
                if (!empty($obj->question_id)) {
802
                    $obj->id = $obj->question_id;
803
                }
804
805
                // Avoid adding the same question twice
806
                if (!isset($this->course->resources[$obj->id])) {
807
                    // find the question category
808
                    // @todo : need to be adapted for multi category questions in 1.10
809
                    $question_category_id = TestCategory::getCategoryForQuestion($obj->id, $courseId);
810
                    $question = new QuizQuestion(
811
                        $obj->id,
812
                        $obj->question,
813
                        $obj->description,
814
                        $obj->ponderation,
815
                        $obj->type,
816
                        $obj->position,
817
                        $obj->picture,
818
                        $obj->level,
819
                        $obj->extra,
820
                        $question_category_id
821
                    );
822
                    $question->addPicture($this);
823
                    $sql = "SELECT * FROM $table_ans
824
                            WHERE c_id = $courseId AND question_id = ".$obj->id;
825
                    $db_result2 = Database::query($sql);
826
                    if (Database::num_rows($db_result2)) {
827
                        while ($obj2 = Database::fetch_object($db_result2)) {
828
                            $question->add_answer(
829
                                $obj2->id,
830
                                $obj2->answer,
831
                                $obj2->correct,
832
                                $obj2->comment,
833
                                $obj2->ponderation,
834
                                $obj2->position,
835
                                $obj2->hotspot_coordinates,
836
                                $obj2->hotspot_type
837
                            );
838
                        }
839
                        $orphanQuestionIds[] = $obj->id;
840
                    }
841
                    $this->course->add_resource($question);
842
                }
843
            }
844
        }
845
846
        if ($build_orphan_questions) {
847
            $obj = [
848
                'id' => -1,
849
                'title' => get_lang('OrphanQuestions', ''),
850
                'type' => 2,
851
            ];
852
            $newQuiz = new Quiz((object) $obj);
853
            if (!empty($orphanQuestionIds)) {
854
                foreach ($orphanQuestionIds as $index => $orphanId) {
855
                    $order = $index + 1;
856
                    $newQuiz->add_question($orphanId, $order);
857
                }
858
            }
859
            $this->course->add_resource($newQuiz);
860
        }
861
    }
862
863
    /**
864
     * Build the orphan questions.
865
     */
866
    public function build_quiz_orphan_questions()
867
    {
868
        $table_qui = Database::get_course_table(TABLE_QUIZ_TEST);
869
        $table_rel = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
870
        $table_que = Database::get_course_table(TABLE_QUIZ_QUESTION);
871
        $table_ans = Database::get_course_table(TABLE_QUIZ_ANSWER);
872
873
        $courseId = api_get_course_int_id();
874
875
        $sql = 'SELECT *
876
                FROM '.$table_que.' as questions
877
                LEFT JOIN '.$table_rel.' as quizz_questions
878
                ON questions.id=quizz_questions.question_id
879
                LEFT JOIN '.$table_qui.' as exercises
880
                ON quizz_questions.exercice_id = exercises.id
881
                WHERE
882
                    questions.c_id = quizz_questions.c_id AND
883
                    questions.c_id = exercises.c_id AND
884
                    exercises.c_id = '.$courseId.' AND
885
                    (quizz_questions.exercice_id IS NULL OR
886
                    exercises.active = -1)';
887
888
        $db_result = Database::query($sql);
889
        if (Database::num_rows($db_result) > 0) {
890
            // This is the fictional test for collecting orphan questions.
891
            $orphan_questions = new Quiz(
892
                -1,
893
                get_lang('OrphanQuestions', ''),
894
                '',
895
                0,
896
                0,
897
                1,
898
                '',
899
                0
900
            );
901
902
            $this->course->add_resource($orphan_questions);
903
            while ($obj = Database::fetch_object($db_result)) {
904
                $question = new QuizQuestion(
905
                    $obj->id,
906
                    $obj->question,
907
                    $obj->description,
908
                    $obj->ponderation,
909
                    $obj->type,
910
                    $obj->position,
911
                    $obj->picture,
912
                    $obj->level,
913
                    $obj->extra
914
                );
915
                $question->addPicture($this);
916
917
                $sql = 'SELECT * FROM '.$table_ans.' WHERE question_id = '.$obj->id;
918
                $db_result2 = Database::query($sql);
919
                while ($obj2 = Database::fetch_object($db_result2)) {
920
                    $question->add_answer(
921
                        $obj2->id,
922
                        $obj2->answer,
923
                        $obj2->correct,
924
                        $obj2->comment,
925
                        $obj2->ponderation,
926
                        $obj2->position,
927
                        $obj2->hotspot_coordinates,
928
                        $obj2->hotspot_type
929
                    );
930
                }
931
                $this->course->add_resource($question);
932
            }
933
        }
934
    }
935
936
    /**
937
     * Build the test category.
938
     *
939
     * @param int   $sessionId       Internal session ID
940
     * @param int   $courseId        Internal course ID
941
     * @param bool  $withBaseContent Whether to include content from the course without session or not
942
     * @param array $idList          If you want to restrict the structure to only the given IDs
943
     *
944
     * @todo add course session
945
     */
946
    public function build_test_category(
947
        $sessionId = 0,
948
        $courseId = 0,
949
        $withBaseContent = false,
950
        $idList = []
951
    ) {
952
        // get all test category in course
953
        $categories = TestCategory::getCategoryListInfo('', $courseId);
954
        foreach ($categories as $category) {
955
            /** @var TestCategory $category */
956
            $courseCopyTestCategory = new CourseCopyTestCategory(
957
                $category->id,
958
                $category->name,
959
                $category->description
960
            );
961
            $this->course->add_resource($courseCopyTestCategory);
962
        }
963
    }
964
965
    /**
966
     * Build the Surveys.
967
     *
968
     * @param int   $session_id      Internal session ID
969
     * @param int   $courseId        Internal course ID
970
     * @param bool  $withBaseContent Whether to include content from the course without session or not
971
     * @param array $id_list         If you want to restrict the structure to only the given IDs
972
     */
973
    public function build_surveys(
974
        $session_id = 0,
975
        $courseId = 0,
976
        $withBaseContent = false,
977
        $id_list = []
978
    ) {
979
        $table_survey = Database::get_course_table(TABLE_SURVEY);
980
        $table_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
981
982
        $courseId = (int) $courseId;
983
984
        $sessionCondition = api_get_session_condition(
985
            $session_id,
986
            true,
987
            $withBaseContent
988
        );
989
990
        $sql = 'SELECT * FROM '.$table_survey.'
991
                WHERE c_id = '.$courseId.' '.$sessionCondition;
992
        if ($id_list) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id_list of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
993
            $sql .= " AND iid IN (".implode(', ', $id_list).")";
994
        }
995
        $db_result = Database::query($sql);
996
        while ($obj = Database::fetch_object($db_result)) {
997
            $survey = new Survey(
998
                $obj->survey_id,
999
                $obj->code,
1000
                $obj->title,
1001
                $obj->subtitle,
1002
                $obj->author,
1003
                $obj->lang,
1004
                $obj->avail_from,
1005
                $obj->avail_till,
1006
                $obj->is_shared,
1007
                $obj->template,
1008
                $obj->intro,
1009
                $obj->surveythanks,
1010
                $obj->creation_date,
1011
                $obj->invited,
1012
                $obj->answered,
1013
                $obj->invite_mail,
1014
                $obj->reminder_mail
1015
            );
1016
            $sql = 'SELECT * FROM '.$table_question.'
1017
                    WHERE c_id = '.$courseId.' AND survey_id = '.$obj->survey_id;
1018
            $db_result2 = Database::query($sql);
1019
            while ($obj2 = Database::fetch_object($db_result2)) {
1020
                $survey->add_question($obj2->question_id);
1021
            }
1022
            $this->course->add_resource($survey);
1023
        }
1024
        $this->build_survey_questions($courseId);
1025
    }
1026
1027
    /**
1028
     * Build the Survey Questions.
1029
     *
1030
     * @param int $courseId Internal course ID
1031
     */
1032
    public function build_survey_questions($courseId)
1033
    {
1034
        $table_que = Database::get_course_table(TABLE_SURVEY_QUESTION);
1035
        $table_opt = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
1036
1037
        $courseId = (int) $courseId;
1038
1039
        $sql = 'SELECT * FROM '.$table_que.' WHERE c_id = '.$courseId.'  ';
1040
        $db_result = Database::query($sql);
1041
        $is_required = 0;
1042
        while ($obj = Database::fetch_object($db_result)) {
1043
            if (api_get_configuration_value('allow_required_survey_questions')) {
1044
                if (isset($obj->is_required)) {
1045
                    $is_required = $obj->is_required;
1046
                }
1047
            }
1048
            $question = new SurveyQuestion(
1049
                $obj->question_id,
1050
                $obj->survey_id,
1051
                $obj->survey_question,
1052
                $obj->survey_question_comment,
1053
                $obj->type,
1054
                $obj->display,
1055
                $obj->sort,
1056
                $obj->shared_question_id,
1057
                $obj->max_value,
1058
                $is_required
1059
            );
1060
            $sql = 'SELECT * FROM '.$table_opt.'
1061
                    WHERE c_id = '.$courseId.' AND question_id = '.$obj->question_id;
1062
            $db_result2 = Database::query($sql);
1063
            while ($obj2 = Database::fetch_object($db_result2)) {
1064
                $question->add_answer($obj2->option_text, $obj2->sort);
1065
            }
1066
            $this->course->add_resource($question);
1067
        }
1068
    }
1069
1070
    /**
1071
     * Build the announcements.
1072
     *
1073
     * @param int   $session_id      Internal session ID
1074
     * @param int   $courseId        Internal course ID
1075
     * @param bool  $withBaseContent Whether to include content from the course without session or not
1076
     * @param array $id_list         If you want to restrict the structure to only the given IDs
1077
     */
1078
    public function build_announcements(
1079
        $session_id = 0,
1080
        $courseId = 0,
1081
        $withBaseContent = false,
1082
        $id_list = []
1083
    ) {
1084
        $table = Database::get_course_table(TABLE_ANNOUNCEMENT);
1085
1086
        $sessionCondition = api_get_session_condition(
1087
            $session_id,
1088
            true,
1089
            $withBaseContent
1090
        );
1091
1092
        $courseId = (int) $courseId;
1093
1094
        $sql = 'SELECT * FROM '.$table.'
1095
                WHERE c_id = '.$courseId.' '.$sessionCondition;
1096
        $db_result = Database::query($sql);
1097
        $table_attachment = Database::get_course_table(
1098
            TABLE_ANNOUNCEMENT_ATTACHMENT
1099
        );
1100
        while ($obj = Database::fetch_object($db_result)) {
1101
            if (empty($obj->id)) {
1102
                continue;
1103
            }
1104
            $sql = 'SELECT path, comment, filename, size
1105
                    FROM '.$table_attachment.'
1106
                    WHERE c_id = '.$courseId.' AND announcement_id = '.$obj->id.'';
1107
            $result = Database::query($sql);
1108
            $attachment_obj = Database::fetch_object($result);
1109
            $att_path = $att_filename = $att_size = $atth_comment = '';
1110
1111
            if (!empty($attachment_obj)) {
1112
                $att_path = $attachment_obj->path;
1113
                $att_filename = $attachment_obj->filename;
1114
                $att_size = $attachment_obj->size;
1115
                $atth_comment = $attachment_obj->comment;
1116
            }
1117
1118
            $announcement = new Announcement(
1119
                $obj->id,
1120
                $obj->title,
1121
                $obj->content,
1122
                $obj->end_date,
1123
                $obj->display_order,
1124
                $obj->email_sent,
1125
                $att_path,
1126
                $att_filename,
1127
                $att_size,
1128
                $atth_comment
1129
            );
1130
            $this->course->add_resource($announcement);
1131
        }
1132
    }
1133
1134
    /**
1135
     * Build the events.
1136
     *
1137
     * @param int   $session_id      Internal session ID
1138
     * @param int   $courseId        Internal course ID
1139
     * @param bool  $withBaseContent Whether to include content from the course without session or not
1140
     * @param array $id_list         If you want to restrict the structure to only the given IDs
1141
     */
1142
    public function build_events(
1143
        $session_id = 0,
1144
        $courseId = 0,
1145
        $withBaseContent = false,
1146
        $id_list = []
1147
    ) {
1148
        $table = Database::get_course_table(TABLE_AGENDA);
1149
1150
        $sessionCondition = api_get_session_condition(
1151
            $session_id,
1152
            true,
1153
            $withBaseContent
1154
        );
1155
1156
        $courseId = (int) $courseId;
1157
1158
        $sql = 'SELECT * FROM '.$table.'
1159
                WHERE c_id = '.$courseId.' '.$sessionCondition;
1160
        $db_result = Database::query($sql);
1161
        while ($obj = Database::fetch_object($db_result)) {
1162
            $table_attachment = Database::get_course_table(
1163
                TABLE_AGENDA_ATTACHMENT
1164
            );
1165
            $sql = 'SELECT path, comment, filename, size
1166
                    FROM '.$table_attachment.'
1167
                    WHERE c_id = '.$courseId.' AND agenda_id = '.$obj->id.'';
1168
            $result = Database::query($sql);
1169
1170
            $attachment_obj = Database::fetch_object($result);
1171
            $att_path = $att_filename = $att_size = $atth_comment = '';
1172
            if (!empty($attachment_obj)) {
1173
                $att_path = $attachment_obj->path;
1174
                $att_filename = $attachment_obj->filename;
1175
                $att_size = $attachment_obj->size;
1176
                $atth_comment = $attachment_obj->comment;
1177
            }
1178
            $event = new CalendarEvent(
1179
                $obj->id,
1180
                $obj->title,
1181
                $obj->content,
1182
                $obj->start_date,
1183
                $obj->end_date,
1184
                $att_path,
1185
                $att_filename,
1186
                $att_size,
1187
                $atth_comment,
1188
                $obj->all_day
1189
            );
1190
            $this->course->add_resource($event);
1191
        }
1192
    }
1193
1194
    /**
1195
     * Build the course-descriptions.
1196
     *
1197
     * @param int   $session_id      Internal session ID
1198
     * @param int   $courseId        Internal course ID
1199
     * @param bool  $withBaseContent Whether to include content from the course without session or not
1200
     * @param array $id_list         If you want to restrict the structure to only the given IDs
1201
     */
1202
    public function build_course_descriptions(
1203
        $session_id = 0,
1204
        $courseId = 0,
1205
        $withBaseContent = false,
1206
        $id_list = []
1207
    ) {
1208
        $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
1209
        $courseId = (int) $courseId;
1210
1211
        if (!empty($session_id) && !empty($courseId)) {
1212
            $session_id = intval($session_id);
1213
            if ($withBaseContent) {
1214
                $session_condition = api_get_session_condition(
1215
                    $session_id,
1216
                    true,
1217
                    true
1218
                );
1219
            } else {
1220
                $session_condition = api_get_session_condition(
1221
                    $session_id,
1222
                    true
1223
                );
1224
            }
1225
            $sql = 'SELECT * FROM '.$table.'
1226
                    WHERE c_id = '.$courseId.' '.$session_condition;
1227
        } else {
1228
            $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
1229
            $sql = 'SELECT * FROM '.$table.'
1230
                    WHERE c_id = '.$courseId.'  AND session_id = 0';
1231
        }
1232
1233
        $db_result = Database::query($sql);
1234
        while ($obj = Database::fetch_object($db_result)) {
1235
            $cd = new CourseDescription(
1236
                $obj->id,
1237
                $obj->title,
1238
                $obj->content,
1239
                $obj->description_type
1240
            );
1241
            $this->course->add_resource($cd);
1242
        }
1243
    }
1244
1245
    /**
1246
     * Build the learnpaths.
1247
     *
1248
     * @param int   $session_id      Internal session ID
1249
     * @param int   $courseId        Internal course ID
1250
     * @param bool  $withBaseContent Whether to include content from the course without session or not
1251
     * @param array $id_list         If you want to restrict the structure to only the given IDs
1252
     * @param bool  $addScormFolder
1253
     */
1254
    public function build_learnpaths(
1255
        $session_id = 0,
1256
        $courseId = 0,
1257
        $withBaseContent = false,
1258
        $id_list = [],
1259
        $addScormFolder = true
1260
    ) {
1261
        $table_main = Database::get_course_table(TABLE_LP_MAIN);
1262
        $table_item = Database::get_course_table(TABLE_LP_ITEM);
1263
        $table_tool = Database::get_course_table(TABLE_TOOL_LIST);
1264
1265
        $courseId = (int) $courseId;
1266
1267
        if (!empty($session_id) && !empty($courseId)) {
1268
            $session_id = intval($session_id);
1269
            if ($withBaseContent) {
1270
                $session_condition = api_get_session_condition(
1271
                    $session_id,
1272
                    true,
1273
                    true
1274
                );
1275
            } else {
1276
                $session_condition = api_get_session_condition(
1277
                    $session_id,
1278
                    true
1279
                );
1280
            }
1281
            $sql = 'SELECT * FROM '.$table_main.'
1282
                    WHERE c_id = '.$courseId.'  '.$session_condition;
1283
        } else {
1284
            $sql = 'SELECT * FROM '.$table_main.'
1285
                    WHERE c_id = '.$courseId.' AND (session_id = 0 OR session_id IS NULL)';
1286
        }
1287
1288
        if (!empty($id_list)) {
1289
            $id_list = array_map('intval', $id_list);
1290
            $sql .= " AND id IN (".implode(', ', $id_list).") ";
1291
        }
1292
1293
        $result = Database::query($sql);
1294
        if ($result) {
0 ignored issues
show
introduced by
$result is of type Doctrine\DBAL\Driver\Statement, thus it always evaluated to true.
Loading history...
1295
            while ($obj = Database::fetch_object($result)) {
1296
                $items = [];
1297
                $sql = "SELECT * FROM ".$table_item."
1298
                        WHERE c_id = '$courseId' AND lp_id = ".$obj->id;
1299
                $db_items = Database::query($sql);
1300
                while ($obj_item = Database::fetch_object($db_items)) {
1301
                    $item['id'] = $obj_item->id;
1302
                    $item['item_type'] = $obj_item->item_type;
1303
                    $item['ref'] = $obj_item->ref;
1304
                    $item['title'] = $obj_item->title;
1305
                    $item['description'] = $obj_item->description;
1306
                    $item['path'] = $obj_item->path;
1307
                    $item['min_score'] = $obj_item->min_score;
1308
                    $item['max_score'] = $obj_item->max_score;
1309
                    $item['mastery_score'] = $obj_item->mastery_score;
1310
                    $item['parent_item_id'] = $obj_item->parent_item_id;
1311
                    $item['previous_item_id'] = $obj_item->previous_item_id;
1312
                    $item['next_item_id'] = $obj_item->next_item_id;
1313
                    $item['display_order'] = $obj_item->display_order;
1314
                    $item['prerequisite'] = $obj_item->prerequisite;
1315
                    $item['parameters'] = $obj_item->parameters;
1316
                    $item['launch_data'] = $obj_item->launch_data;
1317
                    $item['audio'] = $obj_item->audio;
1318
                    $items[] = $item;
1319
                }
1320
1321
                $sql = "SELECT id FROM $table_tool
1322
                        WHERE
1323
                            c_id = $courseId AND
1324
                            (link LIKE '%lp_controller.php%lp_id=".$obj->id."%' AND image='scormbuilder.gif') AND
1325
                            visibility = '1' ";
1326
                $db_tool = Database::query($sql);
1327
                $visibility = '0';
1328
                if (Database::num_rows($db_tool)) {
1329
                    $visibility = '1';
1330
                }
1331
1332
                $lp = new CourseCopyLearnpath(
1333
                    $obj->id,
1334
                    $obj->lp_type,
1335
                    $obj->name,
1336
                    $obj->path,
1337
                    $obj->ref,
1338
                    $obj->description,
1339
                    $obj->content_local,
1340
                    $obj->default_encoding,
1341
                    $obj->default_view_mod,
1342
                    $obj->prevent_reinit,
1343
                    $obj->force_commit,
1344
                    $obj->content_maker,
1345
                    $obj->display_order,
1346
                    $obj->js_lib,
1347
                    $obj->content_license,
1348
                    $obj->debug,
1349
                    $visibility,
1350
                    $obj->author,
1351
                    $obj->preview_image,
1352
                    $obj->use_max_score,
1353
                    $obj->autolaunch,
1354
                    $obj->created_on,
1355
                    $obj->modified_on,
1356
                    $obj->publicated_on,
1357
                    $obj->expired_on,
1358
                    $obj->session_id,
1359
                    $items
1360
                );
1361
1362
                $this->course->add_resource($lp);
1363
1364
                if (!empty($obj->preview_image)) {
1365
                    // Add LP teacher image
1366
                    $asset = new Asset(
1367
                        $obj->preview_image,
1368
                        '/upload/learning_path/images/'.$obj->preview_image,
1369
                        '/upload/learning_path/images/'.$obj->preview_image
1370
                    );
1371
                    $this->course->add_resource($asset);
1372
                }
1373
            }
1374
        }
1375
1376
        // Save scorm directory (previously build_scorm_documents())
1377
        if ($addScormFolder) {
1378
            $i = 1;
1379
            if ($dir = @opendir($this->course->backup_path.'/scorm')) {
1380
                while ($file = readdir($dir)) {
1381
                    if (is_dir($this->course->backup_path.'/scorm/'.$file) &&
1382
                        !in_array($file, ['.', '..'])
1383
                    ) {
1384
                        $doc = new ScormDocument($i++, '/'.$file, $file);
1385
                        $this->course->add_resource($doc);
1386
                    }
1387
                }
1388
                closedir($dir);
1389
            }
1390
        }
1391
    }
1392
1393
    /**
1394
     * Build the glossaries.
1395
     *
1396
     * @param int   $session_id      Internal session ID
1397
     * @param int   $courseId        Internal course ID
1398
     * @param bool  $withBaseContent Whether to include content from the course without session or not
1399
     * @param array $id_list         If you want to restrict the structure to only the given IDs
1400
     */
1401
    public function build_glossary(
1402
        $session_id = 0,
1403
        $courseId = 0,
1404
        $withBaseContent = false,
1405
        $id_list = []
1406
    ) {
1407
        $table_glossary = Database::get_course_table(TABLE_GLOSSARY);
1408
1409
        $courseId = (int) $courseId;
1410
1411
        if (!empty($session_id) && !empty($courseId)) {
1412
            $session_id = intval($session_id);
1413
            if ($withBaseContent) {
1414
                $session_condition = api_get_session_condition(
1415
                    $session_id,
1416
                    true,
1417
                    true
1418
                );
1419
            } else {
1420
                $session_condition = api_get_session_condition(
1421
                    $session_id,
1422
                    true
1423
                );
1424
            }
1425
1426
            //@todo check this queries are the same ...
1427
            if (!empty($this->course->type) && $this->course->type == 'partial') {
0 ignored issues
show
Bug introduced by
The property type does not seem to exist on Chamilo\CourseBundle\Component\CourseCopy\Course.
Loading history...
1428
                $sql = 'SELECT * FROM '.$table_glossary.' g
1429
                        WHERE g.c_id = '.$courseId.' '.$session_condition;
1430
            } else {
1431
                $sql = 'SELECT * FROM '.$table_glossary.' g
1432
                        WHERE g.c_id = '.$courseId.' '.$session_condition;
1433
            }
1434
        } else {
1435
            $table_glossary = Database::get_course_table(TABLE_GLOSSARY);
1436
            //@todo check this queries are the same ... ayayay
1437
            if (!empty($this->course->type) && $this->course->type == 'partial') {
1438
                $sql = 'SELECT * FROM '.$table_glossary.' g
1439
                        WHERE g.c_id = '.$courseId.' AND (session_id = 0 OR session_id IS NULL)';
1440
            } else {
1441
                $sql = 'SELECT * FROM '.$table_glossary.' g
1442
                        WHERE g.c_id = '.$courseId.' AND (session_id = 0 OR session_id IS NULL)';
1443
            }
1444
        }
1445
        $db_result = Database::query($sql);
1446
        while ($obj = Database::fetch_object($db_result)) {
1447
            $doc = new Glossary(
1448
                $obj->glossary_id,
1449
                $obj->name,
1450
                $obj->description,
1451
                $obj->display_order
1452
            );
1453
            $this->course->add_resource($doc);
1454
        }
1455
    }
1456
1457
    /*
1458
     * Build session course by jhon
1459
     */
1460
    public function build_session_course()
1461
    {
1462
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
1463
        $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
1464
        $list_course = CourseManager::get_course_list();
1465
        $list = [];
1466
        foreach ($list_course as $_course) {
1467
            $this->course = new Course();
1468
            $this->course->code = $_course['code'];
1469
            $this->course->type = 'partial';
0 ignored issues
show
Bug introduced by
The property type does not seem to exist on Chamilo\CourseBundle\Component\CourseCopy\Course.
Loading history...
1470
            $this->course->path = api_get_path(SYS_COURSE_PATH).$_course['directory'].'/';
1471
            $this->course->backup_path = api_get_path(SYS_COURSE_PATH).$_course['directory'];
0 ignored issues
show
Bug Best Practice introduced by
The property backup_path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
1472
            $this->course->encoding = api_get_system_encoding(); //current platform encoding
1473
            $courseId = $_course['real_id'];
1474
            $sql = "SELECT s.id, name, c_id
1475
                    FROM $tbl_session_course sc
1476
                    INNER JOIN $tbl_session s
1477
                    ON sc.session_id = s.id
1478
                    WHERE sc.c_id = '$courseId' ";
1479
            $query_session = Database::query($sql);
1480
            while ($rows_session = Database::fetch_assoc($query_session)) {
1481
                $session = new CourseSession(
1482
                    $rows_session['id'],
1483
                    $rows_session['name']
1484
                );
1485
                $this->course->add_resource($session);
1486
            }
1487
            $list[] = $this->course;
1488
        }
1489
1490
        return $list;
1491
    }
1492
1493
    /**
1494
     * @param int   $session_id      Internal session ID
1495
     * @param int   $courseId        Internal course ID
1496
     * @param bool  $withBaseContent Whether to include content from the course without session or not
1497
     * @param array $id_list         If you want to restrict the structure to only the given IDs
1498
     */
1499
    public function build_wiki(
1500
        $session_id = 0,
1501
        $courseId = 0,
1502
        $withBaseContent = false,
1503
        $id_list = []
1504
    ) {
1505
        $tbl_wiki = Database::get_course_table(TABLE_WIKI);
1506
        $courseId = (int) $courseId;
1507
1508
        if (!empty($session_id) && !empty($courseId)) {
1509
            $session_id = intval($session_id);
1510
            if ($withBaseContent) {
1511
                $session_condition = api_get_session_condition(
1512
                    $session_id,
1513
                    true,
1514
                    true
1515
                );
1516
            } else {
1517
                $session_condition = api_get_session_condition(
1518
                    $session_id,
1519
                    true
1520
                );
1521
            }
1522
            $sql = 'SELECT * FROM '.$tbl_wiki.'
1523
                    WHERE c_id = '.$courseId.' '.$session_condition;
1524
        } else {
1525
            $tbl_wiki = Database::get_course_table(TABLE_WIKI);
1526
            $sql = 'SELECT * FROM '.$tbl_wiki.'
1527
                    WHERE c_id = '.$courseId.' AND (session_id = 0 OR session_id IS NULL)';
1528
        }
1529
        $db_result = Database::query($sql);
1530
        while ($obj = Database::fetch_object($db_result)) {
1531
            $wiki = new Wiki(
1532
                $obj->id,
1533
                $obj->page_id,
1534
                $obj->reflink,
1535
                $obj->title,
1536
                $obj->content,
1537
                $obj->user_id,
1538
                $obj->group_id,
1539
                $obj->dtime,
1540
                $obj->progress,
1541
                $obj->version
1542
            );
1543
            $this->course->add_resource($wiki);
1544
        }
1545
    }
1546
1547
    /**
1548
     * Build the Surveys.
1549
     *
1550
     * @param int   $session_id      Internal session ID
1551
     * @param int   $courseId        Internal course ID
1552
     * @param bool  $withBaseContent Whether to include content from the course without session or not
1553
     * @param array $id_list         If you want to restrict the structure to only the given IDs
1554
     */
1555
    public function build_thematic(
1556
        $session_id = 0,
1557
        $courseId = 0,
1558
        $withBaseContent = false,
1559
        $id_list = []
1560
    ) {
1561
        $table_thematic = Database::get_course_table(TABLE_THEMATIC);
1562
        $table_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
1563
        $table_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
1564
        $courseId = (int) $courseId;
1565
1566
        $courseInfo = api_get_course_info_by_id($courseId);
1567
        $session_id = intval($session_id);
1568
        if ($withBaseContent) {
1569
            $session_condition = api_get_session_condition(
1570
                $session_id,
1571
                true,
1572
                true
1573
            );
1574
        } else {
1575
            $session_condition = api_get_session_condition($session_id, true);
1576
        }
1577
1578
        $sql = "SELECT * FROM $table_thematic
1579
                WHERE c_id = $courseId $session_condition ";
1580
        $db_result = Database::query($sql);
1581
        while ($row = Database::fetch_array($db_result, 'ASSOC')) {
1582
            $thematic = new Thematic($row);
1583
            $sql = 'SELECT * FROM '.$table_thematic_advance.'
1584
                    WHERE c_id = '.$courseId.' AND thematic_id = '.$row['id'];
1585
1586
            $result = Database::query($sql);
1587
            while ($sub_row = Database::fetch_array($result, 'ASSOC')) {
1588
                $thematic->addThematicAdvance($sub_row);
1589
            }
1590
1591
            $items = api_get_item_property_by_tool(
1592
                'thematic_plan',
1593
                $courseInfo['code'],
1594
                $session_id
1595
            );
1596
1597
            $thematic_plan_id_list = [];
1598
            if (!empty($items)) {
1599
                foreach ($items as $item) {
1600
                    $thematic_plan_id_list[] = $item['ref'];
1601
                }
1602
            }
1603
            if (count($thematic_plan_id_list) > 0) {
1604
                $sql = "SELECT tp.*
1605
                        FROM $table_thematic_plan tp
1606
                            INNER JOIN $table_thematic t ON (t.id=tp.thematic_id)
1607
                        WHERE
1608
                            t.c_id = $courseId AND
1609
                            tp.c_id = $courseId AND
1610
                            thematic_id = {$row['id']}  AND
1611
                            tp.id IN (".implode(', ', $thematic_plan_id_list).") ";
1612
1613
                $result = Database::query($sql);
1614
                while ($sub_row = Database::fetch_array($result, 'ASSOC')) {
1615
                    $thematic->addThematicPlan($sub_row);
1616
                }
1617
            }
1618
            $this->course->add_resource($thematic);
1619
        }
1620
    }
1621
1622
    /**
1623
     * Build the attendances.
1624
     *
1625
     * @param int   $session_id      Internal session ID
1626
     * @param int   $courseId        Internal course ID
1627
     * @param bool  $withBaseContent Whether to include content from the course without session or not
1628
     * @param array $id_list         If you want to restrict the structure to only the given IDs
1629
     */
1630
    public function build_attendance(
1631
        $session_id = 0,
1632
        $courseId = 0,
1633
        $withBaseContent = false,
1634
        $id_list = []
1635
    ) {
1636
        $table_attendance = Database::get_course_table(TABLE_ATTENDANCE);
1637
        $table_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
1638
        $sessionCondition = api_get_session_condition($session_id, true, $withBaseContent);
1639
        $courseId = (int) $courseId;
1640
1641
        $sql = 'SELECT * FROM '.$table_attendance.'
1642
                WHERE c_id = '.$courseId.' '.$sessionCondition;
1643
        $db_result = Database::query($sql);
1644
        while ($row = Database::fetch_array($db_result, 'ASSOC')) {
1645
            $obj = new Attendance($row);
1646
            $sql = 'SELECT * FROM '.$table_attendance_calendar.'
1647
                    WHERE c_id = '.$courseId.' AND attendance_id = '.$row['id'];
1648
1649
            $result = Database::query($sql);
1650
            while ($sub_row = Database::fetch_array($result, 'ASSOC')) {
1651
                $obj->add_attendance_calendar($sub_row);
1652
            }
1653
            $this->course->add_resource($obj);
1654
        }
1655
    }
1656
1657
    /**
1658
     * Build the works (or "student publications", or "assignments").
1659
     *
1660
     * @param int   $session_id      Internal session ID
1661
     * @param int   $courseId        Internal course ID
1662
     * @param bool  $withBaseContent Whether to include content from the course without session or not
1663
     * @param array $idList          If you want to restrict the structure to only the given IDs
1664
     */
1665
    public function build_works(
1666
        $session_id = 0,
1667
        $courseId = 0,
1668
        $withBaseContent = false,
1669
        $idList = []
1670
    ) {
1671
        $table_work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
1672
        $sessionCondition = api_get_session_condition(
1673
            $session_id,
1674
            true,
1675
            $withBaseContent
1676
        );
1677
        $courseId = (int) $courseId;
1678
1679
        $idCondition = '';
1680
        if (!empty($idList)) {
1681
            $idList = array_map('intval', $idList);
1682
            $idCondition = ' AND iid IN ("'.implode('","', $idList).'") ';
1683
        }
1684
1685
        $sql = "SELECT * FROM $table_work
1686
                WHERE
1687
                    c_id = $courseId                    
1688
                    $sessionCondition AND                    
1689
                    filetype = 'folder' AND
1690
                    parent_id = 0 AND
1691
                    active = 1
1692
                    $idCondition
1693
                ";
1694
        $result = Database::query($sql);
1695
        while ($row = Database::fetch_array($result, 'ASSOC')) {
1696
            $obj = new Work($row);
1697
            $this->course->add_resource($obj);
1698
        }
1699
    }
1700
1701
    /**
1702
     * @param int  $session_id
1703
     * @param int  $courseId
1704
     * @param bool $withBaseContent
1705
     */
1706
    public function build_gradebook(
1707
        $session_id = 0,
1708
        $courseId = 0,
1709
        $withBaseContent = false
1710
    ) {
1711
        $courseInfo = api_get_course_info_by_id($courseId);
1712
        $courseCode = $courseInfo['code'];
1713
        $cats = Category:: load(
1714
            null,
1715
            null,
1716
            $courseCode,
1717
            null,
1718
            null,
1719
            $session_id
1720
        );
1721
1722
        if (!empty($cats)) {
1723
            /** @var Category $cat */
1724
            foreach ($cats as $cat) {
1725
                $cat->evaluations = $cat->get_evaluations(null, false);
1726
                $cat->links = $cat->get_links(null, false);
1727
                $cat->subCategories = $cat->get_subcategories(
1728
                    null,
1729
                    $courseCode,
1730
                    $session_id
1731
                );
1732
            }
1733
            $obj = new GradeBookBackup($cats);
1734
            $this->course->add_resource($obj);
1735
        }
1736
    }
1737
}
1738