Completed
Push — master ( 27e209...a08afa )
by Julito
186:04 queued 150:53
created

CourseBuilder::build_gradebook()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 19
nc 3
nop 3
dl 0
loc 29
rs 8.8571
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 Chamilo\CourseBundle\Component\CourseCopy\Resources\Asset;
7
use Database;
8
use TestCategory;
9
use Category;
10
use CourseManager;
11
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Announcement;
12
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Attendance;
13
use Chamilo\CourseBundle\Component\CourseCopy\Resources\CalendarEvent;
14
use Chamilo\CourseBundle\Component\CourseCopy\Resources\CourseCopyLearnpath;
15
use Chamilo\CourseBundle\Component\CourseCopy\Resources\CourseCopyTestCategory;
16
use Chamilo\CourseBundle\Component\CourseCopy\Resources\CourseDescription;
17
use Chamilo\CourseBundle\Component\CourseCopy\Resources\CourseSession;
18
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Document;
19
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Forum;
20
use Chamilo\CourseBundle\Component\CourseCopy\Resources\ForumCategory;
21
use Chamilo\CourseBundle\Component\CourseCopy\Resources\ForumPost;
22
use Chamilo\CourseBundle\Component\CourseCopy\Resources\ForumTopic;
23
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Glossary;
24
use Chamilo\CourseBundle\Component\CourseCopy\Resources\GradeBookBackup;
25
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Link;
26
use Chamilo\CourseBundle\Component\CourseCopy\Resources\LinkCategory;
27
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Quiz;
28
use Chamilo\CourseBundle\Component\CourseCopy\Resources\QuizQuestion;
29
use Chamilo\CourseBundle\Component\CourseCopy\Resources\QuizQuestionOption;
30
use Chamilo\CourseBundle\Component\CourseCopy\Resources\ScormDocument;
31
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Survey;
32
use Chamilo\CourseBundle\Component\CourseCopy\Resources\SurveyInvitation;
33
use Chamilo\CourseBundle\Component\CourseCopy\Resources\SurveyQuestion;
34
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Thematic;
35
use Chamilo\CourseBundle\Component\CourseCopy\Resources\ToolIntro;
36
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Wiki;
37
use Chamilo\CourseBundle\Component\CourseCopy\Resources\Work;
38
39
use \Link as LinkManager;
40
41
/**
42
 * Class CourseBuilder
43
 * Builds a course-object from a Chamilo-course.
44
 * @author Bart Mollet <[email protected]>
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
     * @param string $type
83
     * @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...
84
     */
85
    public function __construct($type = '', $course = null)
86
    {
87
        $_course = api_get_course_info();
88
89
        if (!empty($course['official_code'])) {
90
            $_course = $course;
91
        }
92
93
        $this->course = new Course();
94
        $this->course->code = $_course['code'];
95
        $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...
96
        $this->course->path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/';
97
        $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...
98
        $this->course->encoding = api_get_system_encoding();
99
        $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...
100
    }
101
102
    /**
103
     * @param array $array
104
     */
105
    public function set_tools_to_build($array)
106
    {
107
        $this->tools_to_build = $array;
108
    }
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
     * @return course The course
122
     */
123
    public function get_course()
124
    {
125
        return $this->course;
126
    }
127
128
    /**
129
     * Build the course-object
130
     *
131
     * @param int      $session_id
132
     * @param string   $courseCode
133
     * @param bool     true if you want to get the elements that exists in the course and
134
     *                 in the session, (session_id = 0 or session_id = X)
135
     * @return Course The course object structure
136
     */
137
    public function build(
138
        $session_id = 0,
139
        $courseCode = '',
140
        $with_base_content = false
141
    ) {
142
        $table_properties = Database::get_course_table(TABLE_ITEM_PROPERTY);
143
        $course = api_get_course_info($courseCode);
144
        $courseId = $course['real_id'];
145
146
        foreach ($this->tools_to_build as $tool) {
147
            $function_build = 'build_'.$tool;
148
            $specificIdList = isset($this->specific_id_list[$tool]) ? $this->specific_id_list[$tool] : null;
149
150
            $this->$function_build(
151
                $session_id,
152
                $courseId,
153
                $with_base_content,
154
                $specificIdList
155
            );
156
        }
157
158
        // Add asset
159
        if ($course['course_image_source'] && basename($course['course_image_source']) != 'course.png') {
160
            // Add course image courses/XXX/course-pic85x85.png
161
            $asset = new Asset(
162
                $course['course_image_source'],
163
                basename($course['course_image_source']),
164
                basename($course['course_image_source'])
165
            );
166
            $this->course->add_resource($asset);
167
168
            $asset = new Asset(
169
                $course['course_image_large_source'],
170
                basename($course['course_image_large_source']),
171
                basename($course['course_image_large_source'])
172
            );
173
            $this->course->add_resource($asset);
174
        }
175
176
        // Once we've built the resources array a bit more, try to get items
177
        //  from the item_property table and order them in the "resources" array
178
        foreach ($this->course->resources as $type => $resources) {
179
            foreach ($resources as $id => $resource) {
180
                $tool = $resource->get_tool();
181
                if ($tool != null) {
182
                    $sql = "SELECT * FROM $table_properties
183
                            WHERE
184
                                c_id = $courseId AND
185
                                tool = '".$tool."' AND
186
                                ref = '".$resource->get_id()."'";
187
                    $res = Database::query($sql);
188
                    $all_properties = [];
189
                    while ($item_property = Database::fetch_array($res)) {
190
                        $all_properties[] = $item_property;
191
                    }
192
                    $this->course->resources[$type][$id]->item_properties = $all_properties;
193
                }
194
            }
195
        }
196
197
        return $this->course;
198
    }
199
200
    /**
201
     * Build the documents
202
     * @param int $session_id
203
     * @param int $courseId
204
     * @param bool $with_base_content
205
     * @param array $id_list
206
     */
207
    public function build_documents(
208
        $session_id = 0,
209
        $courseId = 0,
210
        $with_base_content = false,
211
        $id_list = []
212
    ) {
213
        $table_doc = Database::get_course_table(TABLE_DOCUMENT);
214
        $table_prop = Database::get_course_table(TABLE_ITEM_PROPERTY);
215
216
        // Remove chat_files and shared_folder files
217
        $avoid_paths = " path NOT LIKE '/shared_folder%' AND
218
                         path NOT LIKE '/chat_files%' ";
219
220
        if (!empty($courseId) && !empty($session_id)) {
221
            $session_id = intval($session_id);
222
            if ($with_base_content) {
223
                $session_condition = api_get_session_condition(
224
                    $session_id,
225
                    true,
226
                    true,
227
                    'd.session_id'
228
                );
229
            } else {
230
                $session_condition = api_get_session_condition(
231
                    $session_id,
232
                    true,
233
                    false,
234
                    'd.session_id'
235
                );
236
            }
237
238
            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...
239
                $sql = "SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size
240
                        FROM $table_doc d 
241
                        INNER JOIN $table_prop p
242
                        ON (p.ref = d.id AND d.c_id = p.c_id)
243
                        WHERE
244
                            d.c_id = $courseId AND
245
                            p.c_id = $courseId AND
246
                            tool = '".TOOL_DOCUMENT."' AND
247
                            p.visibility != 2 AND
248
                            path NOT LIKE '/images/gallery%' AND
249
                            $avoid_paths
250
                            $session_condition
251
                        ORDER BY path";
252
            } else {
253
                $sql = "SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size
254
                        FROM $table_doc d 
255
                        INNER JOIN $table_prop p
256
                        ON (p.ref = d.id AND d.c_id = p.c_id)
257
                        WHERE
258
                            d.c_id = $courseId AND
259
                            p.c_id = $courseId AND
260
                            tool = '".TOOL_DOCUMENT."' AND
261
                            $avoid_paths AND
262
                            p.visibility != 2 $session_condition
263
                        ORDER BY path";
264
            }
265
266
            $db_result = Database::query($sql);
267
            while ($obj = Database::fetch_object($db_result)) {
268
                $doc = new Document(
269
                    $obj->id,
270
                    $obj->path,
271
                    $obj->comment,
272
                    $obj->title,
273
                    $obj->filetype,
274
                    $obj->size
275
                );
276
                $this->course->add_resource($doc);
277
            }
278
        } else {
279
            if (!empty($this->course->type) && $this->course->type == 'partial') {
280
                $sql = "SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size
281
                        FROM $table_doc d 
282
                        INNER JOIN $table_prop p
283
                        ON (p.ref = d.id AND d.c_id = p.c_id)
284
                        WHERE
285
                            d.c_id = $courseId AND
286
                            p.c_id = $courseId AND
287
                            tool = '".TOOL_DOCUMENT."' AND
288
                            p.visibility != 2 AND
289
                            path NOT LIKE '/images/gallery%' AND
290
                            $avoid_paths AND
291
                            (d.session_id = 0 OR d.session_id IS NULL)
292
                        ORDER BY path";
293
            } else {
294
                $sql = "SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size
295
                        FROM $table_doc d 
296
                        INNER JOIN $table_prop p
297
                        ON (p.ref = d.id AND d.c_id = p.c_id)
298
                        WHERE
299
                            d.c_id = $courseId AND
300
                            p.c_id = $courseId AND
301
                            tool = '".TOOL_DOCUMENT."' AND
302
                            p.visibility != 2 AND
303
                            $avoid_paths AND
304
                            (d.session_id = 0 OR d.session_id IS NULL)
305
                        ORDER BY path";
306
            }
307
308
            $db_result = Database::query($sql);
309
            while ($obj = Database::fetch_object($db_result)) {
310
                $doc = new Document(
311
                    $obj->id,
312
                    $obj->path,
313
                    $obj->comment,
314
                    $obj->title,
315
                    $obj->filetype,
316
                    $obj->size
317
                );
318
                $this->course->add_resource($doc);
319
            }
320
        }
321
    }
322
323
    /**
324
     * Build the forums
325
     * @param int $session_id Internal session ID
326
     * @param int $courseId Internal course ID
327
     * @param bool $with_base_content Whether to include content from the course without session or not
328
     * @param array $id_list If you want to restrict the structure to only the given IDs
329
     * @return void
330
     */
331
    public function build_forums(
332
        $session_id = 0,
333
        $courseId = 0,
334
        $with_base_content = false,
335
        $id_list = []
336
    ) {
337
        $table = Database::get_course_table(TABLE_FORUM);
338
339
        $sessionCondition = api_get_session_condition(
340
            $session_id,
341
            true,
342
            $with_base_content
343
        );
344
345
        $sql = "SELECT * FROM $table WHERE c_id = $courseId $sessionCondition";
346
        $sql .= " ORDER BY forum_title, forum_category";
347
        $db_result = Database::query($sql);
348
        while ($obj = Database::fetch_object($db_result)) {
349
            $forum = new Forum($obj);
350
            $this->course->add_resource($forum);
351
        }
352
    }
353
354
    /**
355
     * Build a forum-category
356
     * @param int $session_id Internal session ID
357
     * @param int $courseId Internal course ID
358
     * @param bool $with_base_content Whether to include content from the course without session or not
359
     * @param array $id_list If you want to restrict the structure to only the given IDs
360
     * @return void
361
     */
362
    public function build_forum_category(
363
        $session_id = 0,
364
        $courseId = 0,
365
        $with_base_content = false,
366
        $id_list = []
367
    ) {
368
        $table = Database::get_course_table(TABLE_FORUM_CATEGORY);
369
370
        $sessionCondition = api_get_session_condition(
371
            $session_id,
372
            true,
373
            $with_base_content
374
        );
375
376
        $sql = "SELECT * FROM $table
377
                WHERE c_id = $courseId $sessionCondition
378
                ORDER BY cat_title";
379
380
        $result = Database::query($sql);
381
        while ($obj = Database::fetch_object($result)) {
382
            $forumCategory = new ForumCategory($obj);
383
            $this->course->add_resource($forumCategory);
384
        }
385
    }
386
387
    /**
388
     * Build the forum-topics
389
     * @param int $session_id Internal session ID
390
     * @param int $courseId Internal course ID
391
     * @param bool $with_base_content Whether to include content from the course without session or not
392
     * @param array $id_list If you want to restrict the structure to only the given IDs
393
     * @return void
394
     */
395
    public function build_forum_topics(
396
        $session_id = 0,
397
        $courseId = 0,
398
        $with_base_content = false,
399
        $id_list = []
400
    ) {
401
        $table = Database::get_course_table(TABLE_FORUM_THREAD);
402
403
        $sessionCondition = api_get_session_condition(
404
            $session_id,
405
            true,
406
            $with_base_content
407
        );
408
409
        $sql = "SELECT * FROM $table WHERE c_id = $courseId
410
                $sessionCondition
411
                ORDER BY thread_title ";
412
        $result = Database::query($sql);
413
414
        while ($obj = Database::fetch_object($result)) {
415
            $forum_topic = new ForumTopic($obj);
416
            $this->course->add_resource($forum_topic);
417
            $this->build_forum_posts($courseId, $obj->thread_id, $obj->forum_id, true);
418
        }
419
    }
420
421
    /**
422
     * Build the forum-posts
423
     * TODO: All tree structure of posts should be built, attachments for example.
424
     * @param int $courseId Internal course ID
425
     * @param int $thread_id Internal thread ID
426
     * @param int $forum_id Internal forum ID
427
     * @param bool $only_first_post Whether to only copy the first post or not
428
     */
429
    public function build_forum_posts(
430
        $courseId = 0,
431
        $thread_id = null,
432
        $forum_id = null,
433
        $only_first_post = false
434
    ) {
435
        $table = Database::get_course_table(TABLE_FORUM_POST);
436
        $sql = "SELECT * FROM $table WHERE c_id = $courseId ";
437
        if (!empty($thread_id) && !empty($forum_id)) {
438
            $forum_id = intval($forum_id);
439
            $thread_id = intval($thread_id);
440
            $sql .= " AND thread_id = $thread_id AND forum_id = $forum_id ";
441
        }
442
        $sql .= " ORDER BY post_id ASC LIMIT 1";
443
        $db_result = Database::query($sql);
444
        while ($obj = Database::fetch_object($db_result)) {
445
            $forum_post = new ForumPost($obj);
446
            $this->course->add_resource($forum_post);
447
        }
448
    }
449
450
    /**
451
     * Build the links
452
     * @param int $session_id Internal session ID
453
     * @param int $courseId Internal course ID
454
     * @param bool $with_base_content Whether to include content from the course without session or not
455
     * @param array $id_list If you want to restrict the structure to only the given IDs
456
     */
457
    public function build_links(
458
        $session_id = 0,
459
        $courseId = 0,
460
        $with_base_content = false,
461
        $id_list = []
462
    ) {
463
        $categories = LinkManager::getLinkCategories(
464
            $courseId,
465
            $session_id,
466
            $with_base_content
467
        );
468
469
        // Adding empty category
470
        $categories[] = ['id' => 0];
471
472
        foreach ($categories as $category) {
473
            $this->build_link_category($category);
474
475
            $links = LinkManager::getLinksPerCategory(
476
                $category['id'],
477
                $courseId,
478
                $session_id,
479
                $with_base_content
480
            );
481
482
            foreach ($links as $item) {
483
                $link = new Link(
484
                    $item['id'],
485
                    $item['title'],
486
                    $item['url'],
487
                    $item['description'],
488
                    $item['category_id'],
489
                    $item['on_homepage']
490
                );
491
                $link->target = $item['target'];
492
                $this->course->add_resource($link);
493
                $this->course->resources[RESOURCE_LINK][$item['id']]->add_linked_resource(
494
                    RESOURCE_LINKCATEGORY,
495
                    $item['category_id']
496
                );
497
            }
498
        }
499
    }
500
501
    /**
502
     * Build tool intro
503
     * @param int $session_id Internal session ID
504
     * @param int $courseId Internal course ID
505
     * @param bool $with_base_content Whether to include content from the course without session or not
506
     * @param array $id_list If you want to restrict the structure to only the given IDs
507
     */
508
    public function build_tool_intro(
509
        $session_id = 0,
510
        $courseId = 0,
511
        $with_base_content = false,
512
        $id_list = []
513
    ) {
514
        $table = Database::get_course_table(TABLE_TOOL_INTRO);
515
516
        $sessionCondition = api_get_session_condition(
517
            $session_id,
518
            true,
519
            $with_base_content
520
        );
521
522
        $sql = "SELECT * FROM $table
523
                WHERE c_id = $courseId $sessionCondition";
524
525
        $db_result = Database::query($sql);
526
        while ($obj = Database::fetch_object($db_result)) {
527
            $tool_intro = new ToolIntro($obj->id, $obj->intro_text);
528
            $this->course->add_resource($tool_intro);
529
        }
530
    }
531
532
    /**
533
     * Build a link category
534
     * @param int $id Internal link ID
535
     * @param int $courseId Internal course ID
536
     * @return int
537
     */
538
    public function build_link_category($category)
539
    {
540
        if (empty($category) || empty($category['category_title'])) {
541
            return 0;
542
        }
543
544
        $linkCategory = new LinkCategory(
545
            $category['id'],
546
            $category['category_title'],
547
            $category['description'],
548
            $category['display_order']
549
        );
550
        $this->course->add_resource($linkCategory);
551
552
        return $category['id'];
553
    }
554
555
    /**
556
     * Build the Quizzes
557
     * @param int $session_id Internal session ID
558
     * @param int $courseId Internal course ID
559
     * @param bool $with_base_content Whether to include content from the course without session or not
560
     * @param array $idList If you want to restrict the structure to only the given IDs
561
     */
562
    public function build_quizzes(
563
        $session_id = 0,
564
        $courseId = 0,
565
        $with_base_content = false,
566
        $idList = []
567
    ) {
568
        $table_qui = Database::get_course_table(TABLE_QUIZ_TEST);
569
        $table_rel = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
570
        $table_doc = Database::get_course_table(TABLE_DOCUMENT);
571
572
        if (!empty($courseId) && !empty($session_id)) {
573
            $session_id = intval($session_id);
574
            if ($with_base_content) {
575
                $session_condition = api_get_session_condition(
576
                    $session_id,
577
                    true,
578
                    true
579
                );
580
            } else {
581
                $session_condition = api_get_session_condition(
582
                    $session_id,
583
                    true
584
                );
585
            }
586
            $sql = "SELECT * FROM $table_qui
587
                    WHERE c_id = $courseId AND active >=0 $session_condition";
588
            //select only quizzes with active = 0 or 1 (not -1 which is for deleted quizzes)
589
        } else {
590
            $sql = "SELECT * FROM $table_qui
591
                    WHERE c_id = $courseId AND active >=0 AND (session_id = 0 OR session_id IS NULL)";
592
            //select only quizzes with active = 0 or 1 (not -1 which is for deleted quizzes)
593
        }
594
595
        $sql .= 'ORDER BY title';
596
597
        $db_result = Database::query($sql);
598
        while ($obj = Database::fetch_object($db_result)) {
599
            if (strlen($obj->sound) > 0) {
600
                $sql = "SELECT id FROM $table_doc
601
                        WHERE c_id = $courseId AND path = '/audio/".$obj->sound."'";
602
                $res = Database::query($sql);
603
                $doc = Database::fetch_object($res);
604
                $obj->sound = $doc->id;
605
            }
606
            $quiz = new Quiz($obj);
607
608
            $sql = 'SELECT * FROM '.$table_rel.'
609
                    WHERE c_id = '.$courseId.' AND exercice_id = '.$obj->id;
610
            $db_result2 = Database::query($sql);
611
            while ($obj2 = Database::fetch_object($db_result2)) {
612
                $quiz->add_question($obj2->question_id, $obj2->question_order);
613
            }
614
            $this->course->add_resource($quiz);
615
        }
616
617
        if (!empty($courseId)) {
618
            $this->build_quiz_questions($courseId);
619
        } else {
620
            $this->build_quiz_questions();
621
        }
622
    }
623
624
    /**
625
     * Build the Quiz-Questions
626
     * @param int $courseId Internal course ID
627
     */
628
    public function build_quiz_questions($courseId = 0)
629
    {
630
        $table_qui = Database::get_course_table(TABLE_QUIZ_TEST);
631
        $table_rel = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
632
        $table_que = Database::get_course_table(TABLE_QUIZ_QUESTION);
633
        $table_ans = Database::get_course_table(TABLE_QUIZ_ANSWER);
634
635
        // Building normal tests.
636
        $sql = "SELECT * FROM $table_que
637
                WHERE c_id = $courseId ";
638
        $result = Database::query($sql);
639
640
        while ($obj = Database::fetch_object($result)) {
641
            // find the question category
642
            // @todo : need to be adapted for multi category questions in 1.10
643
            $question_category_id = TestCategory::getCategoryForQuestion(
644
                $obj->id,
645
                $courseId
646
            );
647
648
            // build the backup resource question object
649
            $question = new QuizQuestion(
650
                $obj->id,
651
                $obj->question,
652
                $obj->description,
653
                $obj->ponderation,
654
                $obj->type,
655
                $obj->position,
656
                $obj->picture,
657
                $obj->level,
658
                $obj->extra,
659
                $question_category_id
660
            );
661
662
            $sql = 'SELECT * FROM '.$table_ans.'
663
                    WHERE c_id = '.$courseId.' AND question_id = '.$obj->id;
664
            $db_result2 = Database::query($sql);
665
666
            while ($obj2 = Database::fetch_object($db_result2)) {
667
                $question->add_answer(
668
                    $obj2->id,
669
                    $obj2->answer,
670
                    $obj2->correct,
671
                    $obj2->comment,
672
                    $obj2->ponderation,
673
                    $obj2->position,
674
                    $obj2->hotspot_coordinates,
675
                    $obj2->hotspot_type
676
                );
677
                if ($obj->type == MULTIPLE_ANSWER_TRUE_FALSE) {
678
                    $table_options = Database::get_course_table(
679
                        TABLE_QUIZ_QUESTION_OPTION
680
                    );
681
                    $sql = 'SELECT * FROM '.$table_options.'
682
                            WHERE c_id = '.$courseId.' AND question_id = '.$obj->id;
683
                    $db_result3 = Database::query($sql);
684
                    while ($obj3 = Database::fetch_object($db_result3)) {
685
                        $question_option = new QuizQuestionOption($obj3);
686
                        $question->add_option($question_option);
687
                    }
688
                }
689
            }
690
            $this->course->add_resource($question);
691
        }
692
693
        // Building a fictional test for collecting orphan questions.
694
        // When a course is emptied this option should be activated (true).
695
        $build_orphan_questions = !empty($_POST['recycle_option']);
696
697
        // 1st union gets the orphan questions from deleted exercises
698
        // 2nd union gets the orphan questions from question that were deleted in a exercise.
699
700
        $sql = " (
701
                    SELECT question_id, q.* FROM $table_que q 
702
                    INNER JOIN $table_rel r
703
                    ON (q.c_id = r.c_id AND q.id = r.question_id)
704
                    INNER JOIN $table_qui ex
705
                    ON (ex.id = r.exercice_id AND ex.c_id = r.c_id )
706
                    WHERE ex.c_id = $courseId AND ex.active = '-1'
707
                 )
708
                 UNION
709
                 (
710
                    SELECT question_id, q.* FROM $table_que q 
711
                    left OUTER JOIN $table_rel r
712
                    ON (q.c_id = r.c_id AND q.id = r.question_id)
713
                    WHERE q.c_id = $courseId AND r.question_id is null
714
                 )
715
                 UNION
716
                 (
717
                    SELECT question_id, q.* FROM $table_que q
718
                    INNER JOIN $table_rel r
719
                    ON (q.c_id = r.c_id AND q.id = r.question_id)
720
                    WHERE r.c_id = $courseId AND (r.exercice_id = '-1' OR r.exercice_id = '0')
721
                 )
722
        ";
723
724
        $result = Database::query($sql);
725
        if (Database::num_rows($result) > 0) {
726
            $build_orphan_questions = true;
727
            $orphanQuestionIds = [];
728
            while ($obj = Database::fetch_object($result)) {
729
                // Orphan questions
730
                if (!empty($obj->question_id)) {
731
                    $obj->id = $obj->question_id;
732
                }
733
734
                // Avoid adding the same question twice
735
                if (!isset($this->course->resources[$obj->id])) {
736
                    // find the question category
737
                    // @todo : need to be adapted for multi category questions in 1.10
738
                    $question_category_id = TestCategory::getCategoryForQuestion(
739
                        $obj->id,
740
                        $courseId
741
                    );
742
                    $question = new QuizQuestion(
743
                        $obj->id,
744
                        $obj->question,
745
                        $obj->description,
746
                        $obj->ponderation,
747
                        $obj->type,
748
                        $obj->position,
749
                        $obj->picture,
750
                        $obj->level,
751
                        $obj->extra,
752
                        $question_category_id
753
                    );
754
                    $sql = "SELECT * FROM $table_ans
755
                            WHERE c_id = $courseId AND question_id = ".$obj->id;
756
                    $db_result2 = Database::query($sql);
757
                    if (Database::num_rows($db_result2)) {
758
                        while ($obj2 = Database::fetch_object($db_result2)) {
759
                            $question->add_answer(
760
                                $obj2->id,
761
                                $obj2->answer,
762
                                $obj2->correct,
763
                                $obj2->comment,
764
                                $obj2->ponderation,
765
                                $obj2->position,
766
                                $obj2->hotspot_coordinates,
767
                                $obj2->hotspot_type
768
                            );
769
                        }
770
                        $orphanQuestionIds[] = $obj->id;
771
                    }
772
                    $this->course->add_resource($question);
773
                }
774
            }
775
        }
776
777
        if ($build_orphan_questions) {
778
            $obj = [
779
                'id' => -1,
780
                'title' => get_lang('OrphanQuestions', ''),
781
                'type' => 2,
782
            ];
783
            $newQuiz = new Quiz((object) $obj);
784
            if (!empty($orphanQuestionIds)) {
785
                foreach ($orphanQuestionIds as $index => $orphanId) {
786
                    $order = $index + 1;
787
                    $newQuiz->add_question($orphanId, $order);
788
                }
789
            }
790
            $this->course->add_resource($newQuiz);
791
        }
792
    }
793
794
    /**
795
     * Build the orphan questions
796
     */
797
    public function build_quiz_orphan_questions()
798
    {
799
        $table_qui = Database::get_course_table(TABLE_QUIZ_TEST);
800
        $table_rel = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
801
        $table_que = Database::get_course_table(TABLE_QUIZ_QUESTION);
802
        $table_ans = Database::get_course_table(TABLE_QUIZ_ANSWER);
803
804
        $courseId = api_get_course_int_id();
805
806
        $sql = 'SELECT *
807
                FROM '.$table_que.' as questions
808
                LEFT JOIN '.$table_rel.' as quizz_questions
809
                ON questions.id=quizz_questions.question_id
810
                LEFT JOIN '.$table_qui.' as exercises
811
                ON quizz_questions.exercice_id = exercises.id
812
                WHERE
813
                    questions.c_id = quizz_questions.c_id AND
814
                    questions.c_id = exercises.c_id AND
815
                    exercises.c_id = '.$courseId.' AND
816
                    (quizz_questions.exercice_id IS NULL OR
817
                    exercises.active = -1)';
818
819
        $db_result = Database::query($sql);
820
        if (Database::num_rows($db_result) > 0) {
821
            // This is the fictional test for collecting orphan questions.
822
            $orphan_questions = new Quiz(
823
                -1,
824
                get_lang('OrphanQuestions', ''),
825
                '',
826
                0,
827
                0,
828
                1,
829
                '',
830
                0
831
            );
832
833
            $this->course->add_resource($orphan_questions);
834
            while ($obj = Database::fetch_object($db_result)) {
835
                $question = new QuizQuestion(
836
                    $obj->id,
837
                    $obj->question,
838
                    $obj->description,
839
                    $obj->ponderation,
840
                    $obj->type,
841
                    $obj->position,
842
                    $obj->picture,
843
                    $obj->level,
844
                    $obj->extra
845
                );
846
                $sql = 'SELECT * FROM '.$table_ans.' WHERE question_id = '.$obj->id;
847
                $db_result2 = Database::query($sql);
848
                while ($obj2 = Database::fetch_object($db_result2)) {
849
                    $question->add_answer(
850
                        $obj2->id,
851
                        $obj2->answer,
852
                        $obj2->correct,
853
                        $obj2->comment,
854
                        $obj2->ponderation,
855
                        $obj2->position,
856
                        $obj2->hotspot_coordinates,
857
                        $obj2->hotspot_type
858
                    );
859
                }
860
                $this->course->add_resource($question);
861
            }
862
        }
863
    }
864
865
    /**
866
     * Build the test category
867
     * @param int $sessionId Internal session ID
868
     * @param int $courseId Internal course ID
869
     * @param bool $withBaseContent Whether to include content from the course without session or not
870
     * @param array $idList If you want to restrict the structure to only the given IDs
871
     * @todo add course session
872
     */
873
    public function build_test_category(
874
        $sessionId = 0,
875
        $courseId = 0,
876
        $withBaseContent = false,
877
        $idList = []
878
    ) {
879
        // get all test category in course
880
        $categories = TestCategory::getCategoryListInfo('', $courseId);
881
        foreach ($categories as $category) {
882
            /** @var TestCategory $category */
883
            $courseCopyTestCategory = new CourseCopyTestCategory(
884
                $category->id,
885
                $category->name,
886
                $category->description
887
            );
888
            $this->course->add_resource($courseCopyTestCategory);
889
        }
890
    }
891
892
    /**
893
     * Build the Surveys
894
     * @param int $session_id Internal session ID
895
     * @param int $courseId Internal course ID
896
     * @param bool $with_base_content Whether to include content from the course without session or not
897
     * @param array $id_list If you want to restrict the structure to only the given IDs
898
     */
899
    public function build_surveys(
900
        $session_id = 0,
901
        $courseId = 0,
902
        $with_base_content = false,
903
        $id_list = []
904
    ) {
905
        $table_survey = Database::get_course_table(TABLE_SURVEY);
906
        $table_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
907
908
        $sessionCondition = api_get_session_condition(
909
            $session_id,
910
            true,
911
            $with_base_content
912
        );
913
914
        $sql = 'SELECT * FROM '.$table_survey.'
915
                WHERE c_id = '.$courseId.' '.$sessionCondition;
916
        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...
917
            $sql .= " AND iid IN (".implode(', ', $id_list).")";
918
        }
919
        $db_result = Database::query($sql);
920
        while ($obj = Database::fetch_object($db_result)) {
921
            $survey = new Survey(
922
                $obj->survey_id,
923
                $obj->code,
924
                $obj->title,
925
                $obj->subtitle,
926
                $obj->author,
927
                $obj->lang,
928
                $obj->avail_from,
929
                $obj->avail_till,
930
                $obj->is_shared,
931
                $obj->template,
932
                $obj->intro,
933
                $obj->surveythanks,
934
                $obj->creation_date,
935
                $obj->invited,
936
                $obj->answered,
937
                $obj->invite_mail,
938
                $obj->reminder_mail
939
            );
940
            $sql = 'SELECT * FROM '.$table_question.'
941
                    WHERE c_id = '.$courseId.' AND survey_id = '.$obj->survey_id;
942
            $db_result2 = Database::query($sql);
943
            while ($obj2 = Database::fetch_object($db_result2)) {
944
                $survey->add_question($obj2->question_id);
945
            }
946
            $this->course->add_resource($survey);
947
        }
948
        $this->build_survey_questions($courseId);
949
    }
950
951
    /**
952
     * Build the Survey Questions
953
     * @param int $courseId Internal course ID
954
     */
955
    public function build_survey_questions($courseId)
956
    {
957
        $table_que = Database::get_course_table(TABLE_SURVEY_QUESTION);
958
        $table_opt = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
959
960
        $sql = 'SELECT * FROM '.$table_que.' WHERE c_id = '.$courseId.'  ';
961
        $db_result = Database::query($sql);
962
        $is_required = 0;
963
        while ($obj = Database::fetch_object($db_result)) {
964
            if (api_get_configuration_value('allow_required_survey_questions')) {
965
                if (isset($obj->is_required)) {
966
                    $is_required = $obj->is_required;
967
                }
968
            }
969
            $question = new SurveyQuestion(
970
                $obj->question_id,
971
                $obj->survey_id,
972
                $obj->survey_question,
973
                $obj->survey_question_comment,
974
                $obj->type,
975
                $obj->display,
976
                $obj->sort,
977
                $obj->shared_question_id,
978
                $obj->max_value,
979
                $is_required
980
            );
981
            $sql = 'SELECT * FROM '.$table_opt.'
982
                    WHERE c_id = '.$courseId.' AND question_id = '.$obj->question_id;
983
            $db_result2 = Database::query($sql);
984
            while ($obj2 = Database::fetch_object($db_result2)) {
985
                $question->add_answer($obj2->option_text, $obj2->sort);
986
            }
987
            $this->course->add_resource($question);
988
        }
989
    }
990
991
    /**
992
     * Build the announcements
993
     * @param int $session_id Internal session ID
994
     * @param int $courseId Internal course ID
995
     * @param bool $with_base_content Whether to include content from the course without session or not
996
     * @param array $id_list If you want to restrict the structure to only the given IDs
997
     */
998
    public function build_announcements(
999
        $session_id = 0,
1000
        $courseId = 0,
1001
        $with_base_content = false,
1002
        $id_list = []
1003
    ) {
1004
        $table = Database::get_course_table(TABLE_ANNOUNCEMENT);
1005
1006
        $sessionCondition = api_get_session_condition(
1007
            $session_id,
1008
            true,
1009
            $with_base_content
1010
        );
1011
1012
        $sql = 'SELECT * FROM '.$table.'
1013
                WHERE c_id = '.$courseId.' '.$sessionCondition;
1014
        $db_result = Database::query($sql);
1015
        $table_attachment = Database::get_course_table(
1016
            TABLE_ANNOUNCEMENT_ATTACHMENT
1017
        );
1018
        while ($obj = Database::fetch_object($db_result)) {
1019
            if (empty($obj->id)) {
1020
                continue;
1021
            }
1022
            $sql = 'SELECT path, comment, filename, size
1023
                    FROM '.$table_attachment.'
1024
                    WHERE c_id = '.$courseId.' AND announcement_id = '.$obj->id.'';
1025
            $result = Database::query($sql);
1026
            $attachment_obj = Database::fetch_object($result);
1027
            $att_path = $att_filename = $att_size = $atth_comment = '';
1028
1029
            if (!empty($attachment_obj)) {
1030
                $att_path = $attachment_obj->path;
1031
                $att_filename = $attachment_obj->filename;
1032
                $att_size = $attachment_obj->size;
1033
                $atth_comment = $attachment_obj->comment;
1034
            }
1035
1036
            $announcement = new Announcement(
1037
                $obj->id,
1038
                $obj->title,
1039
                $obj->content,
1040
                $obj->end_date,
1041
                $obj->display_order,
1042
                $obj->email_sent,
1043
                $att_path,
1044
                $att_filename,
1045
                $att_size,
1046
                $atth_comment
1047
            );
1048
            $this->course->add_resource($announcement);
1049
        }
1050
    }
1051
1052
    /**
1053
     * Build the events
1054
     * @param int $session_id Internal session ID
1055
     * @param int $courseId Internal course ID
1056
     * @param bool $with_base_content Whether to include content from the course without session or not
1057
     * @param array $id_list If you want to restrict the structure to only the given IDs
1058
     */
1059
    public function build_events(
1060
        $session_id = 0,
1061
        $courseId = 0,
1062
        $with_base_content = false,
1063
        $id_list = []
1064
    ) {
1065
        $table = Database::get_course_table(TABLE_AGENDA);
1066
1067
        $sessionCondition = api_get_session_condition(
1068
            $session_id,
1069
            true,
1070
            $with_base_content
1071
        );
1072
1073
        $sql = 'SELECT * FROM '.$table.'
1074
                WHERE c_id = '.$courseId.' '.$sessionCondition;
1075
        $db_result = Database::query($sql);
1076
        while ($obj = Database::fetch_object($db_result)) {
1077
            $table_attachment = Database::get_course_table(
1078
                TABLE_AGENDA_ATTACHMENT
1079
            );
1080
            $sql = 'SELECT path, comment, filename, size
1081
                    FROM '.$table_attachment.'
1082
                    WHERE c_id = '.$courseId.' AND agenda_id = '.$obj->id.'';
1083
            $result = Database::query($sql);
1084
1085
            $attachment_obj = Database::fetch_object($result);
1086
            $att_path = $att_filename = $att_size = $atth_comment = '';
1087
            if (!empty($attachment_obj)) {
1088
                $att_path = $attachment_obj->path;
1089
                $att_filename = $attachment_obj->filename;
1090
                $att_size = $attachment_obj->size;
1091
                $atth_comment = $attachment_obj->comment;
1092
            }
1093
            $event = new CalendarEvent(
1094
                $obj->id,
1095
                $obj->title,
1096
                $obj->content,
1097
                $obj->start_date,
1098
                $obj->end_date,
1099
                $att_path,
1100
                $att_filename,
1101
                $att_size,
1102
                $atth_comment,
1103
                $obj->all_day
1104
            );
1105
            $this->course->add_resource($event);
1106
        }
1107
    }
1108
1109
    /**
1110
     * Build the course-descriptions
1111
     * @param int $session_id Internal session ID
1112
     * @param int $courseId Internal course ID
1113
     * @param bool $with_base_content Whether to include content from the course without session or not
1114
     * @param array $id_list If you want to restrict the structure to only the given IDs
1115
     */
1116
    public function build_course_descriptions(
1117
        $session_id = 0,
1118
        $courseId = 0,
1119
        $with_base_content = false,
1120
        $id_list = []
1121
    ) {
1122
        $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
1123
1124
        if (!empty($session_id) && !empty($courseId)) {
1125
            $session_id = intval($session_id);
1126
            if ($with_base_content) {
1127
                $session_condition = api_get_session_condition(
1128
                    $session_id,
1129
                    true,
1130
                    true
1131
                );
1132
            } else {
1133
                $session_condition = api_get_session_condition(
1134
                    $session_id,
1135
                    true
1136
                );
1137
            }
1138
            $sql = 'SELECT * FROM '.$table.'
1139
                    WHERE c_id = '.$courseId.' '.$session_condition;
1140
        } else {
1141
            $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
1142
            $sql = 'SELECT * FROM '.$table.'
1143
                    WHERE c_id = '.$courseId.'  AND session_id = 0';
1144
        }
1145
1146
        $db_result = Database::query($sql);
1147
        while ($obj = Database::fetch_object($db_result)) {
1148
            $cd = new CourseDescription(
1149
                $obj->id,
1150
                $obj->title,
1151
                $obj->content,
1152
                $obj->description_type
1153
            );
1154
            $this->course->add_resource($cd);
1155
        }
1156
    }
1157
1158
    /**
1159
     * Build the learnpaths
1160
     * @param int $session_id Internal session ID
1161
     * @param int $courseId Internal course ID
1162
     * @param bool $with_base_content Whether to include content from the course without session or not
1163
     * @param array $id_list If you want to restrict the structure to only the given IDs
1164
     */
1165
    public function build_learnpaths(
1166
        $session_id = 0,
1167
        $courseId = 0,
1168
        $with_base_content = false,
1169
        $id_list = []
1170
    ) {
1171
        $table_main = Database::get_course_table(TABLE_LP_MAIN);
1172
        $table_item = Database::get_course_table(TABLE_LP_ITEM);
1173
        $table_tool = Database::get_course_table(TABLE_TOOL_LIST);
1174
1175
        if (!empty($session_id) && !empty($courseId)) {
1176
            $session_id = intval($session_id);
1177
            if ($with_base_content) {
1178
                $session_condition = api_get_session_condition(
1179
                    $session_id,
1180
                    true,
1181
                    true
1182
                );
1183
            } else {
1184
                $session_condition = api_get_session_condition(
1185
                    $session_id,
1186
                    true
1187
                );
1188
            }
1189
            $sql = 'SELECT * FROM '.$table_main.'
1190
                    WHERE c_id = '.$courseId.'  '.$session_condition;
1191
        } else {
1192
            $sql = 'SELECT * FROM '.$table_main.'
1193
                    WHERE c_id = '.$courseId.' AND (session_id = 0 OR session_id IS NULL)';
1194
        }
1195
1196
        if (!empty($id_list)) {
1197
            $id_list = array_map('intval', $id_list);
1198
            $sql .= " AND id IN (".implode(', ', $id_list).") ";
1199
        }
1200
1201
        $db_result = Database::query($sql);
1202
        if ($db_result) {
1203
            while ($obj = Database::fetch_object($db_result)) {
1204
                $items = [];
1205
                $sql = "SELECT * FROM ".$table_item."
1206
                        WHERE c_id = '$courseId' AND lp_id = ".$obj->id;
1207
                $db_items = Database::query($sql);
1208
                while ($obj_item = Database::fetch_object($db_items)) {
1209
                    $item['id'] = $obj_item->id;
1210
                    $item['item_type'] = $obj_item->item_type;
1211
                    $item['ref'] = $obj_item->ref;
1212
                    $item['title'] = $obj_item->title;
1213
                    $item['description'] = $obj_item->description;
1214
                    $item['path'] = $obj_item->path;
1215
                    $item['min_score'] = $obj_item->min_score;
1216
                    $item['max_score'] = $obj_item->max_score;
1217
                    $item['mastery_score'] = $obj_item->mastery_score;
1218
                    $item['parent_item_id'] = $obj_item->parent_item_id;
1219
                    $item['previous_item_id'] = $obj_item->previous_item_id;
1220
                    $item['next_item_id'] = $obj_item->next_item_id;
1221
                    $item['display_order'] = $obj_item->display_order;
1222
                    $item['prerequisite'] = $obj_item->prerequisite;
1223
                    $item['parameters'] = $obj_item->parameters;
1224
                    $item['launch_data'] = $obj_item->launch_data;
1225
                    $item['audio'] = $obj_item->audio;
1226
                    $items[] = $item;
1227
                }
1228
1229
                $sql = "SELECT id FROM $table_tool
1230
                        WHERE
1231
                            c_id = $courseId AND
1232
                            (link LIKE '%lp_controller.php%lp_id=".$obj->id."%' AND image='scormbuilder.gif') AND
1233
                            visibility = '1' ";
1234
                $db_tool = Database::query($sql);
1235
1236
                if (Database::num_rows($db_tool)) {
1237
                    $visibility = '1';
1238
                } else {
1239
                    $visibility = '0';
1240
                }
1241
1242
                $lp = new CourseCopyLearnpath(
1243
                    $obj->id,
1244
                    $obj->lp_type,
1245
                    $obj->name,
1246
                    $obj->path,
1247
                    $obj->ref,
1248
                    $obj->description,
1249
                    $obj->content_local,
1250
                    $obj->default_encoding,
1251
                    $obj->default_view_mod,
1252
                    $obj->prevent_reinit,
1253
                    $obj->force_commit,
1254
                    $obj->content_maker,
1255
                    $obj->display_order,
1256
                    $obj->js_lib,
1257
                    $obj->content_license,
1258
                    $obj->debug,
1259
                    $visibility,
1260
                    $obj->author,
1261
                    $obj->preview_image,
1262
                    $obj->use_max_score,
1263
                    $obj->autolaunch,
1264
                    $obj->created_on,
1265
                    $obj->modified_on,
1266
                    $obj->publicated_on,
1267
                    $obj->expired_on,
1268
                    $obj->session_id,
1269
                    $items
1270
                );
1271
                $this->course->add_resource($lp);
1272
            }
1273
        }
1274
1275
        // Save scorm directory (previously build_scorm_documents())
1276
        $i = 1;
1277
        if ($dir = @opendir($this->course->backup_path.'/scorm')) {
1278
            while ($file = readdir($dir)) {
1279
                if (is_dir($this->course->backup_path.'/scorm/'.$file) &&
1280
                    !in_array($file, ['.', '..'])
1281
                ) {
1282
                    $doc = new ScormDocument($i++, '/'.$file, $file);
1283
                    $this->course->add_resource($doc);
1284
                }
1285
            }
1286
            closedir($dir);
1287
        }
1288
    }
1289
1290
    /**
1291
     * Build the glossaries
1292
     * @param int $session_id Internal session ID
1293
     * @param int $courseId Internal course ID
1294
     * @param bool $with_base_content Whether to include content from the course without session or not
1295
     * @param array $id_list If you want to restrict the structure to only the given IDs
1296
     */
1297
    public function build_glossary(
1298
        $session_id = 0,
1299
        $courseId = 0,
1300
        $with_base_content = false,
1301
        $id_list = []
1302
    ) {
1303
        $table_glossary = Database::get_course_table(TABLE_GLOSSARY);
1304
1305
        if (!empty($session_id) && !empty($courseId)) {
1306
            $session_id = intval($session_id);
1307
            if ($with_base_content) {
1308
                $session_condition = api_get_session_condition(
1309
                    $session_id,
1310
                    true,
1311
                    true
1312
                );
1313
            } else {
1314
                $session_condition = api_get_session_condition(
1315
                    $session_id,
1316
                    true
1317
                );
1318
            }
1319
            //@todo check this queries are the same ...
1320
            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...
1321
                $sql = 'SELECT * FROM '.$table_glossary.' g
1322
                        WHERE g.c_id = '.$courseId.' '.$session_condition;
1323
            } else {
1324
                $sql = 'SELECT * FROM '.$table_glossary.' g
1325
                        WHERE g.c_id = '.$courseId.' '.$session_condition;
1326
            }
1327
        } else {
1328
            $table_glossary = Database::get_course_table(TABLE_GLOSSARY);
1329
            //@todo check this queries are the same ... ayayay
1330
            if (!empty($this->course->type) && $this->course->type == 'partial') {
1331
                $sql = 'SELECT * FROM '.$table_glossary.' g
1332
                        WHERE g.c_id = '.$courseId.' AND (session_id = 0 OR session_id IS NULL)';
1333
            } else {
1334
                $sql = 'SELECT * FROM '.$table_glossary.' g
1335
                        WHERE g.c_id = '.$courseId.' AND (session_id = 0 OR session_id IS NULL)';
1336
            }
1337
        }
1338
        $db_result = Database::query($sql);
1339
        while ($obj = Database::fetch_object($db_result)) {
1340
            $doc = new Glossary(
1341
                $obj->glossary_id,
1342
                $obj->name,
1343
                $obj->description,
1344
                $obj->display_order
1345
            );
1346
            $this->course->add_resource($doc);
1347
        }
1348
    }
1349
1350
    /*
1351
     * Build session course by jhon
1352
     */
1353
    public function build_session_course()
1354
    {
1355
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
1356
        $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
1357
        $list_course = CourseManager::get_course_list();
1358
        $list = [];
1359
        foreach ($list_course as $_course) {
1360
            $this->course = new Course();
1361
            $this->course->code = $_course['code'];
1362
            $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...
1363
            $this->course->path = api_get_path(SYS_COURSE_PATH).$_course['directory'].'/';
1364
            $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...
1365
            $this->course->encoding = api_get_system_encoding(); //current platform encoding
1366
            $courseId = $_course['real_id'];
1367
            $sql = "SELECT s.id, name, c_id
1368
                    FROM $tbl_session_course sc
1369
                    INNER JOIN $tbl_session s
1370
                    ON sc.session_id = s.id
1371
                    WHERE sc.c_id = '$courseId' ";
1372
            $query_session = Database::query($sql);
1373
            while ($rows_session = Database::fetch_assoc($query_session)) {
1374
                $session = new CourseSession(
1375
                    $rows_session['id'],
1376
                    $rows_session['name']
1377
                );
1378
                $this->course->add_resource($session);
1379
            }
1380
            $list[] = $this->course;
1381
        }
1382
1383
        return $list;
1384
    }
1385
1386
    /**
1387
     * @param int $session_id Internal session ID
1388
     * @param int $courseId Internal course ID
1389
     * @param bool $with_base_content Whether to include content from the course without session or not
1390
     * @param array $id_list If you want to restrict the structure to only the given IDs
1391
     */
1392
    public function build_wiki(
1393
        $session_id = 0,
1394
        $courseId = 0,
1395
        $with_base_content = false,
1396
        $id_list = []
1397
    ) {
1398
        $tbl_wiki = Database::get_course_table(TABLE_WIKI);
1399
1400
        if (!empty($session_id) && !empty($courseId)) {
1401
            $session_id = intval($session_id);
1402
            if ($with_base_content) {
1403
                $session_condition = api_get_session_condition(
1404
                    $session_id,
1405
                    true,
1406
                    true
1407
                );
1408
            } else {
1409
                $session_condition = api_get_session_condition(
1410
                    $session_id,
1411
                    true
1412
                );
1413
            }
1414
            $sql = 'SELECT * FROM '.$tbl_wiki.'
1415
                    WHERE c_id = '.$courseId.' '.$session_condition;
1416
        } else {
1417
            $tbl_wiki = Database::get_course_table(TABLE_WIKI);
1418
            $sql = 'SELECT * FROM '.$tbl_wiki.'
1419
                    WHERE c_id = '.$courseId.' AND (session_id = 0 OR session_id IS NULL)';
1420
        }
1421
        $db_result = Database::query($sql);
1422
        while ($obj = Database::fetch_object($db_result)) {
1423
            $wiki = new Wiki(
1424
                $obj->id,
1425
                $obj->page_id,
1426
                $obj->reflink,
1427
                $obj->title,
1428
                $obj->content,
1429
                $obj->user_id,
1430
                $obj->group_id,
1431
                $obj->dtime,
1432
                $obj->progress,
1433
                $obj->version
1434
            );
1435
            $this->course->add_resource($wiki);
1436
        }
1437
    }
1438
1439
    /**
1440
     * Build the Surveys
1441
     * @param int $session_id Internal session ID
1442
     * @param int $courseId Internal course ID
1443
     * @param bool $with_base_content Whether to include content from the course without session or not
1444
     * @param array $id_list If you want to restrict the structure to only the given IDs
1445
     */
1446
    public function build_thematic(
1447
        $session_id = 0,
1448
        $courseId = 0,
1449
        $with_base_content = false,
1450
        $id_list = []
1451
    ) {
1452
        $table_thematic = Database::get_course_table(TABLE_THEMATIC);
1453
        $table_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
1454
        $table_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
1455
1456
        $courseInfo = api_get_course_info_by_id($courseId);
1457
        $session_id = intval($session_id);
1458
        if ($with_base_content) {
1459
            $session_condition = api_get_session_condition(
1460
                $session_id,
1461
                true,
1462
                true
1463
            );
1464
        } else {
1465
            $session_condition = api_get_session_condition($session_id, true);
1466
        }
1467
1468
        $sql = "SELECT * FROM $table_thematic
1469
                WHERE c_id = $courseId $session_condition ";
1470
        $db_result = Database::query($sql);
1471
        while ($row = Database::fetch_array($db_result, 'ASSOC')) {
1472
            $thematic = new Thematic($row);
1473
            $sql = 'SELECT * FROM '.$table_thematic_advance.'
1474
                    WHERE c_id = '.$courseId.' AND thematic_id = '.$row['id'];
1475
1476
            $result = Database::query($sql);
1477
            while ($sub_row = Database::fetch_array($result, 'ASSOC')) {
1478
                $thematic->addThematicAdvance($sub_row);
1479
            }
1480
1481
            $items = api_get_item_property_by_tool(
1482
                'thematic_plan',
1483
                $courseInfo['code'],
1484
                $session_id
1485
            );
1486
1487
            $thematic_plan_id_list = [];
1488
            if (!empty($items)) {
1489
                foreach ($items as $item) {
1490
                    $thematic_plan_id_list[] = $item['ref'];
1491
                }
1492
            }
1493
            if (count($thematic_plan_id_list) > 0) {
1494
                $sql = "SELECT tp.*
1495
                        FROM $table_thematic_plan tp
1496
                            INNER JOIN $table_thematic t ON (t.id=tp.thematic_id)
1497
                        WHERE
1498
                            t.c_id = $courseId AND
1499
                            tp.c_id = $courseId AND
1500
                            thematic_id = {$row['id']}  AND
1501
                            tp.id IN (".implode(', ', $thematic_plan_id_list).") ";
1502
1503
                $result = Database::query($sql);
1504
                while ($sub_row = Database::fetch_array($result, 'ASSOC')) {
1505
                    $thematic->addThematicPlan($sub_row);
1506
                }
1507
            }
1508
            $this->course->add_resource($thematic);
1509
        }
1510
    }
1511
1512
    /**
1513
     * Build the attendances
1514
     * @param int $session_id Internal session ID
1515
     * @param int $courseId Internal course ID
1516
     * @param bool $with_base_content Whether to include content from the course without session or not
1517
     * @param array $id_list If you want to restrict the structure to only the given IDs
1518
     */
1519
    public function build_attendance(
1520
        $session_id = 0,
1521
        $courseId = 0,
1522
        $with_base_content = false,
1523
        $id_list = []
1524
    ) {
1525
        $table_attendance = Database::get_course_table(TABLE_ATTENDANCE);
1526
        $table_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
1527
1528
        $sessionCondition = api_get_session_condition($session_id, true, $with_base_content);
1529
1530
        $sql = 'SELECT * FROM '.$table_attendance.'
1531
                WHERE c_id = '.$courseId.' '.$sessionCondition;
1532
        $db_result = Database::query($sql);
1533
        while ($row = Database::fetch_array($db_result, 'ASSOC')) {
1534
            $obj = new Attendance($row);
1535
            $sql = 'SELECT * FROM '.$table_attendance_calendar.'
1536
                    WHERE c_id = '.$courseId.' AND attendance_id = '.$row['id'];
1537
1538
            $result = Database::query($sql);
1539
            while ($sub_row = Database::fetch_array($result, 'ASSOC')) {
1540
                $obj->add_attendance_calendar($sub_row);
1541
            }
1542
            $this->course->add_resource($obj);
1543
        }
1544
    }
1545
1546
    /**
1547
     * Build the works (or "student publications", or "assignments")
1548
     * @param int $session_id Internal session ID
1549
     * @param int $courseId Internal course ID
1550
     * @param bool $with_base_content Whether to include content from the course without session or not
1551
     * @param array $id_list If you want to restrict the structure to only the given IDs
1552
     */
1553
    public function build_works(
1554
        $session_id = 0,
1555
        $courseId = 0,
1556
        $with_base_content = false,
1557
        $id_list = []
1558
    ) {
1559
        $table_work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
1560
        $sessionCondition = api_get_session_condition(
1561
            $session_id,
1562
            true,
1563
            $with_base_content
1564
        );
1565
1566
        $sql = "SELECT * FROM $table_work
1567
                WHERE
1568
                    c_id = $courseId
1569
                    $sessionCondition AND
1570
                    filetype = 'folder' AND
1571
                    parent_id = 0 AND
1572
                    active = 1";
1573
        $db_result = Database::query($sql);
1574
        while ($row = Database::fetch_array($db_result, 'ASSOC')) {
1575
            $obj = new Work($row);
1576
            $this->course->add_resource($obj);
1577
        }
1578
    }
1579
1580
    /**
1581
     * @param int $session_id
1582
     * @param int $courseId
1583
     * @param bool $with_base_content
1584
     */
1585
    public function build_gradebook(
1586
        $session_id = 0,
1587
        $courseId = 0,
1588
        $with_base_content = false
1589
    ) {
1590
        $courseInfo = api_get_course_info_by_id($courseId);
1591
        $courseCode = $courseInfo['code'];
1592
        $cats = Category:: load(
1593
            null,
1594
            null,
1595
            $courseCode,
1596
            null,
1597
            null,
1598
            $session_id
1599
        );
1600
1601
        if (!empty($cats)) {
1602
            /** @var Category $cat */
1603
            foreach ($cats as $cat) {
1604
                $cat->evaluations = $cat->get_evaluations(null, false);
1605
                $cat->links = $cat->get_links(null, false);
1606
                $cat->subCategories = $cat->get_subcategories(
1607
                    null,
1608
                    $courseCode,
1609
                    $session_id
1610
                );
1611
            }
1612
            $obj = new GradeBookBackup($cats);
1613
            $this->course->add_resource($obj);
1614
        }
1615
    }
1616
}
1617