Completed
Push — master ( 395485...bbad3a )
by Julito
43:12
created

CourseBuilder::build_quizzes()   B

Complexity

Conditions 8
Paths 30

Size

Total Lines 59
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 43
nc 30
nop 4
dl 0
loc 59
rs 7.132
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 = array(
55
        'announcements',
56
        'attendance',
57
        'course_descriptions',
58
        'documents',
59
        'events',
60
        'forum_category',
61
        'forums',
62
        'forum_topics',
63
        'glossary',
64
        'quizzes',
65
        'test_category',
66
        '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 = array();
79
80
    /**
81
     * Create a new CourseBuilder
82
     * @param string $type
83
     * @param null $course
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 in Chamilo\CourseBundle\Component\CourseCopy\Course.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

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'];
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 in Chamilo\CourseBundle\Component\CourseCopy\Course.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

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 (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 = array();
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 = array()
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 View Code Duplication
            if (!empty($this->course->type) && $this->course->type == 'partial') {
0 ignored issues
show
Bug introduced by
The property type does not seem to exist in Chamilo\CourseBundle\Component\CourseCopy\Course.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

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 View Code Duplication
            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 View Code Duplication
            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 View Code Duplication
            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 = array()
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 = array()
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 View Code Duplication
    public function build_forum_topics(
396
        $session_id = 0,
397
        $courseId = 0,
398
        $with_base_content = false,
399
        $id_list = array()
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 = array()
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
                $this->course->add_resource($link);
492
                $this->course->resources[RESOURCE_LINK][$item['id']]->add_linked_resource(
493
                    RESOURCE_LINKCATEGORY,
494
                    $item['category_id']
495
                );
496
            }
497
        }
498
    }
499
500
    /**
501
     * Build tool intro
502
     * @param int $session_id Internal session ID
503
     * @param int $courseId Internal course ID
504
     * @param bool $with_base_content Whether to include content from the course without session or not
505
     * @param array $id_list If you want to restrict the structure to only the given IDs
506
     */
507 View Code Duplication
    public function build_tool_intro(
508
        $session_id = 0,
509
        $courseId = 0,
510
        $with_base_content = false,
511
        $id_list = array()
512
    ) {
513
        $table = Database:: get_course_table(TABLE_TOOL_INTRO);
514
515
        $sessionCondition = api_get_session_condition(
516
            $session_id,
517
            true,
518
            $with_base_content
519
        );
520
521
        $sql = "SELECT * FROM $table
522
                WHERE c_id = $courseId $sessionCondition";
523
524
        $db_result = Database::query($sql);
525
        while ($obj = Database::fetch_object($db_result)) {
526
            $tool_intro = new ToolIntro($obj->id, $obj->intro_text);
527
            $this->course->add_resource($tool_intro);
528
        }
529
    }
530
531
    /**
532
     * Build a link category
533
     * @param int $id Internal link ID
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
534
     * @param int $courseId Internal course ID
0 ignored issues
show
Bug introduced by
There is no parameter named $courseId. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
535
     * @return int
536
     */
537
    public function build_link_category($category)
538
    {
539
        if (empty($category) || empty($category['category_title'])) {
540
            return 0;
541
        }
542
543
        $linkCategory = new LinkCategory(
544
            $category['id'],
545
            $category['category_title'],
546
            $category['description'],
547
            $category['display_order']
548
        );
549
        $this->course->add_resource($linkCategory);
550
551
        return $category['id'];
552
    }
553
554
    /**
555
     * Build the Quizzes
556
     * @param int $session_id Internal session ID
557
     * @param int $courseId Internal course ID
558
     * @param bool $with_base_content Whether to include content from the course without session or not
559
     * @param array $id_list If you want to restrict the structure to only the given IDs
560
     */
561
    public function build_quizzes(
562
        $session_id = 0,
563
        $courseId = 0,
564
        $with_base_content = false,
565
        $id_list = array()
566
    ) {
567
        $table_qui = Database:: get_course_table(TABLE_QUIZ_TEST);
568
        $table_rel = Database:: get_course_table(TABLE_QUIZ_TEST_QUESTION);
569
        $table_doc = Database:: get_course_table(TABLE_DOCUMENT);
570
571
        if (!empty($courseId) && !empty($session_id)) {
572
            $session_id  = intval($session_id);
573
            if ($with_base_content) {
574
                $session_condition = api_get_session_condition(
575
                    $session_id,
576
                    true,
577
                    true
578
                );
579
            } else {
580
                $session_condition = api_get_session_condition(
581
                    $session_id,
582
                    true
583
                );
584
            }
585
            $sql = "SELECT * FROM $table_qui
586
                    WHERE c_id = $courseId AND active >=0 $session_condition";
587
            //select only quizzes with active = 0 or 1 (not -1 which is for deleted quizzes)
588
        } else {
589
            $sql = "SELECT * FROM $table_qui
590
                    WHERE c_id = $courseId AND active >=0 AND (session_id = 0 OR session_id IS NULL)";
591
            //select only quizzes with active = 0 or 1 (not -1 which is for deleted quizzes)
592
        }
593
594
        $db_result = Database::query($sql);
595
        while ($obj = Database::fetch_object($db_result)) {
596
            if (strlen($obj->sound) > 0) {
597
                $sql = "SELECT id FROM $table_doc
598
                        WHERE c_id = $courseId AND path = '/audio/".$obj->sound."'";
599
                $res = Database::query($sql);
600
                $doc = Database::fetch_object($res);
601
                $obj->sound = $doc->id;
602
            }
603
            $quiz = new Quiz($obj);
604
605
            $sql = 'SELECT * FROM '.$table_rel.'
606
                    WHERE c_id = '.$courseId.' AND exercice_id = '.$obj->id;
607
            $db_result2 = Database::query($sql);
608
            while ($obj2 = Database::fetch_object($db_result2)) {
609
                $quiz->add_question($obj2->question_id, $obj2->question_order);
610
            }
611
            $this->course->add_resource($quiz);
612
        }
613
614
        if (!empty($courseId)) {
615
            $this->build_quiz_questions($courseId);
616
        } else {
617
            $this->build_quiz_questions();
618
        }
619
    }
620
621
    /**
622
     * Build the Quiz-Questions
623
     * @param int $courseId Internal course ID
624
     */
625
    public function build_quiz_questions($courseId = 0)
626
    {
627
        $table_qui = Database :: get_course_table(TABLE_QUIZ_TEST);
628
        $table_rel = Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION);
629
        $table_que = Database :: get_course_table(TABLE_QUIZ_QUESTION);
630
        $table_ans = Database :: get_course_table(TABLE_QUIZ_ANSWER);
631
632
        // Building normal tests.
633
        $sql = "SELECT * FROM $table_que
634
                WHERE c_id = $courseId ";
635
        $result = Database::query($sql);
636
637
        while ($obj = Database::fetch_object($result)) {
638
            // find the question category
639
640
            // @todo : need to be adapted for multi category questions in 1.10
641
            $question_category_id = TestCategory::getCategoryForQuestion(
642
                $obj->id,
643
                $courseId
644
            );
645
646
            // build the backup resource question object
647
            $question = new QuizQuestion(
648
                $obj->id,
649
                $obj->question,
650
                $obj->description,
651
                $obj->ponderation,
652
                $obj->type,
653
                $obj->position,
654
                $obj->picture,
655
                $obj->level,
656
                $obj->extra,
657
                $question_category_id
658
            );
659
660
            $sql = 'SELECT * FROM '.$table_ans.'
661
                    WHERE c_id = '.$courseId.' AND question_id = '.$obj->id;
662
            $db_result2 = Database::query($sql);
663
664
            while ($obj2 = Database::fetch_object($db_result2)) {
665
                $question->add_answer(
666
                    $obj2->id,
667
                    $obj2->answer,
668
                    $obj2->correct,
669
                    $obj2->comment,
670
                    $obj2->ponderation,
671
                    $obj2->position,
672
                    $obj2->hotspot_coordinates,
673
                    $obj2->hotspot_type
674
                );
675
                if ($obj->type == MULTIPLE_ANSWER_TRUE_FALSE) {
676
                    $table_options = Database::get_course_table(
677
                        TABLE_QUIZ_QUESTION_OPTION
678
                    );
679
                    $sql = 'SELECT * FROM '.$table_options.'
680
                            WHERE c_id = '.$courseId.' AND question_id = '.$obj->id;
681
                    $db_result3 = Database::query($sql);
682
                    while ($obj3 = Database::fetch_object($db_result3)) {
683
                        $question_option = new QuizQuestionOption($obj3);
684
                        $question->add_option($question_option);
685
                    }
686
                }
687
            }
688
            $this->course->add_resource($question);
689
        }
690
691
        // Building a fictional test for collecting orphan questions.
692
        // When a course is emptied this option should be activated (true).
693
        $build_orphan_questions = !empty($_POST['recycle_option']);
694
695
        // 1st union gets the orphan questions from deleted exercises
696
        // 2nd union gets the orphan questions from question that were deleted in a exercise.
697
698
        $sql = " (
699
                    SELECT question_id, q.* FROM $table_que q 
700
                    INNER JOIN $table_rel r
701
                    ON (q.c_id = r.c_id AND q.id = r.question_id)
702
                    INNER JOIN $table_qui ex
703
                    ON (ex.id = r.exercice_id AND ex.c_id = r.c_id )
704
                    WHERE ex.c_id = $courseId AND ex.active = '-1'
705
                 )
706
                 UNION
707
                 (
708
                    SELECT question_id, q.* FROM $table_que q 
709
                    left OUTER JOIN $table_rel r
710
                    ON (q.c_id = r.c_id AND q.id = r.question_id)
711
                    WHERE q.c_id = $courseId AND r.question_id is null
712
                 )
713
                 UNION
714
                 (
715
                    SELECT question_id, q.* FROM $table_que q
716
                    INNER JOIN $table_rel r
717
                    ON (q.c_id = r.c_id AND q.id = r.question_id)
718
                    WHERE r.c_id = $courseId AND (r.exercice_id = '-1' OR r.exercice_id = '0')
719
                 )
720
        ";
721
722
        $result = Database::query($sql);
723
        if (Database::num_rows($result) > 0) {
724
            $build_orphan_questions = true;
725
            $orphanQuestionIds = array();
726
            while ($obj = Database::fetch_object($result)) {
727
                // Orphan questions
728
                if (!empty($obj->question_id)) {
729
                    $obj->id = $obj->question_id;
730
                }
731
732
                // Avoid adding the same question twice
733
                if (!isset($this->course->resources[$obj->id])) {
734
                    // find the question category
735
                    // @todo : need to be adapted for multi category questions in 1.10
736
                    $question_category_id = TestCategory::getCategoryForQuestion(
737
                        $obj->id,
738
                        $courseId
739
                    );
740
                    $question = new QuizQuestion(
741
                        $obj->id,
742
                        $obj->question,
743
                        $obj->description,
744
                        $obj->ponderation,
745
                        $obj->type,
746
                        $obj->position,
747
                        $obj->picture,
748
                        $obj->level,
749
                        $obj->extra,
750
                        $question_category_id
751
                    );
752
                    $sql = "SELECT * FROM $table_ans
753
                            WHERE c_id = $courseId AND question_id = ".$obj->id;
754
                    $db_result2 = Database::query($sql);
755
                    if (Database::num_rows($db_result2)) {
756 View Code Duplication
                        while ($obj2 = Database::fetch_object($db_result2)) {
757
                            $question->add_answer(
758
                                $obj2->id,
759
                                $obj2->answer,
760
                                $obj2->correct,
761
                                $obj2->comment,
762
                                $obj2->ponderation,
763
                                $obj2->position,
764
                                $obj2->hotspot_coordinates,
765
                                $obj2->hotspot_type
766
                            );
767
                        }
768
                        $orphanQuestionIds[] = $obj->id;
769
                    }
770
                    $this->course->add_resource($question);
771
                }
772
            }
773
        }
774
775
        if ($build_orphan_questions) {
776
            $obj = array(
777
                'id' => -1,
778
                'title' => get_lang('OrphanQuestions', ''),
779
                'type' => 2
780
            );
781
            $newQuiz = new Quiz((object) $obj);
782
            if (!empty($orphanQuestionIds)) {
783
                foreach ($orphanQuestionIds as $index => $orphanId) {
784
                    $order = $index + 1;
785
                    $newQuiz->add_question($orphanId, $order);
786
                }
787
            }
788
            $this->course->add_resource($newQuiz);
789
        }
790
    }
791
792
    /**
793
     * Build the orphan questions
794
     */
795
    public function build_quiz_orphan_questions()
796
    {
797
        $table_qui = Database:: get_course_table(TABLE_QUIZ_TEST);
798
        $table_rel = Database:: get_course_table(TABLE_QUIZ_TEST_QUESTION);
799
        $table_que = Database:: get_course_table(TABLE_QUIZ_QUESTION);
800
        $table_ans = Database:: get_course_table(TABLE_QUIZ_ANSWER);
801
802
        $courseId = api_get_course_int_id();
803
804
        $sql = 'SELECT *
805
                FROM '.$table_que.' as questions
806
                LEFT JOIN '.$table_rel.' as quizz_questions
807
                ON questions.id=quizz_questions.question_id
808
                LEFT JOIN '.$table_qui.' as exercises
809
                ON quizz_questions.exercice_id = exercises.id
810
                WHERE
811
                    questions.c_id = quizz_questions.c_id AND
812
                    questions.c_id = exercises.c_id AND
813
                    exercises.c_id = '.$courseId.' AND
814
                    (quizz_questions.exercice_id IS NULL OR
815
                    exercises.active = -1)';
816
817
        $db_result = Database::query($sql);
818
        if (Database::num_rows($db_result) > 0) {
819
            // This is the fictional test for collecting orphan questions.
820
            $orphan_questions = new Quiz(
821
                -1,
822
                get_lang('OrphanQuestions', ''),
823
                '',
824
                0,
825
                0,
826
                1,
827
                '',
828
                0
829
            );
830
831
            $this->course->add_resource($orphan_questions);
832
            while ($obj = Database::fetch_object($db_result)) {
833
                $question = new QuizQuestion(
834
                    $obj->id,
835
                    $obj->question,
836
                    $obj->description,
837
                    $obj->ponderation,
838
                    $obj->type,
839
                    $obj->position,
840
                    $obj->picture,
841
                    $obj->level,
842
                    $obj->extra
843
                );
844
                $sql = 'SELECT * FROM '.$table_ans.' WHERE question_id = '.$obj->id;
845
                $db_result2 = Database::query($sql);
846 View Code Duplication
                while ($obj2 = Database::fetch_object($db_result2)) {
847
                    $question->add_answer(
848
                        $obj2->id,
849
                        $obj2->answer,
850
                        $obj2->correct,
851
                        $obj2->comment,
852
                        $obj2->ponderation,
853
                        $obj2->position,
854
                        $obj2->hotspot_coordinates,
855
                        $obj2->hotspot_type
856
                    );
857
                }
858
                $this->course->add_resource($question);
859
            }
860
        }
861
    }
862
863
    /**
864
     * Build the test category
865
     * @param int $session_id Internal session ID
866
     * @param int $courseId Internal course ID
867
     * @param bool $with_base_content Whether to include content from the course without session or not
868
     * @param array $id_list If you want to restrict the structure to only the given IDs
869
     * @todo add course session
870
     */
871
    public function build_test_category(
872
        $session_id = 0,
873
        $courseId = 0,
874
        $with_base_content = false,
875
        $id_list = array()
876
    ) {
877
        // get all test category in course
878
        $tab_test_categories_id = TestCategory::getCategoryListInfo(
879
            "id",
880
            $courseId
881
        );
882
        foreach ($tab_test_categories_id as $test_category_id) {
883
            $test_category = new TestCategory();
884
            $test_category = $test_category->getCategory($test_category_id);
885
            $copy_course_test_category = new CourseCopyTestCategory(
886
                $test_category_id,
887
                $test_category->name,
888
                $test_category->description
889
            );
890
            $this->course->add_resource($copy_course_test_category);
891
        }
892
    }
893
894
    /**
895
     * Build the Surveys
896
     * @param int $session_id Internal session ID
897
     * @param int $courseId Internal course ID
898
     * @param bool $with_base_content Whether to include content from the course without session or not
899
     * @param array $id_list If you want to restrict the structure to only the given IDs
900
     */
901
    public function build_surveys(
902
        $session_id = 0,
903
        $courseId = 0,
904
        $with_base_content = false,
905
        $id_list = array()
906
    ) {
907
        $table_survey = Database:: get_course_table(TABLE_SURVEY);
908
        $table_question = Database:: get_course_table(TABLE_SURVEY_QUESTION);
909
910
        $sessionCondition = api_get_session_condition(
911
            $session_id,
912
            true,
913
            $with_base_content
914
        );
915
916
        $sql = 'SELECT * FROM '.$table_survey.'
917
                WHERE c_id = '.$courseId.' '.$sessionCondition;
918
        $db_result = Database::query($sql);
919
        while ($obj = Database::fetch_object($db_result)) {
920
            $survey = new Survey(
921
                $obj->survey_id, $obj->code, $obj->title,
922
                $obj->subtitle, $obj->author, $obj->lang,
923
                $obj->avail_from, $obj->avail_till, $obj->is_shared,
924
                $obj->template, $obj->intro, $obj->surveythanks,
925
                $obj->creation_date, $obj->invited, $obj->answered,
926
                $obj->invite_mail, $obj->reminder_mail
927
            );
928
            $sql = 'SELECT * FROM '.$table_question.'
929
                    WHERE c_id = '.$courseId.' AND survey_id = '.$obj->survey_id;
930
            $db_result2 = Database::query($sql);
931
            while ($obj2 = Database::fetch_object($db_result2)) {
932
                $survey->add_question($obj2->question_id);
933
            }
934
            $this->course->add_resource($survey);
935
        }
936
        $this->build_survey_questions($courseId);
937
    }
938
939
    /**
940
     * Build the Survey Questions
941
     * @param int $courseId Internal course ID
942
     */
943
    public function build_survey_questions($courseId)
944
    {
945
        $table_que = Database :: get_course_table(TABLE_SURVEY_QUESTION);
946
        $table_opt = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
947
948
        $sql = 'SELECT * FROM '.$table_que.' WHERE c_id = '.$courseId.'  ';
949
        $db_result = Database::query($sql);
950
        while ($obj = Database::fetch_object($db_result)) {
951
            $question = new SurveyQuestion(
952
                $obj->question_id,
953
                $obj->survey_id,
954
                $obj->survey_question,
955
                $obj->survey_question_comment,
956
                $obj->type,
957
                $obj->display,
958
                $obj->sort,
959
                $obj->shared_question_id,
960
                $obj->max_value
961
            );
962
            $sql = 'SELECT * FROM '.$table_opt.'
963
                    WHERE c_id = '.$courseId.' AND question_id = '.$obj->question_id;
964
            $db_result2 = Database::query($sql);
965
            while ($obj2 = Database::fetch_object($db_result2)) {
966
                $question->add_answer($obj2->option_text, $obj2->sort);
967
            }
968
            $this->course->add_resource($question);
969
        }
970
    }
971
972
    /**
973
     * Build the announcements
974
     * @param int $session_id Internal session ID
975
     * @param int $courseId Internal course ID
976
     * @param bool $with_base_content Whether to include content from the course without session or not
977
     * @param array $id_list If you want to restrict the structure to only the given IDs
978
     */
979 View Code Duplication
    public function build_announcements(
980
        $session_id = 0,
981
        $courseId = 0,
982
        $with_base_content = false,
983
        $id_list = array()
984
    ) {
985
        $table = Database:: get_course_table(TABLE_ANNOUNCEMENT);
986
987
        $sessionCondition = api_get_session_condition(
988
            $session_id,
989
            true,
990
            $with_base_content
991
        );
992
993
        $sql = 'SELECT * FROM '.$table.'
994
                WHERE c_id = '.$courseId.' '.$sessionCondition;
995
        $db_result = Database::query($sql);
996
        $table_attachment = Database:: get_course_table(
997
            TABLE_ANNOUNCEMENT_ATTACHMENT
998
        );
999
        while ($obj = Database::fetch_object($db_result)) {
1000
            if (empty($obj->id)) {
1001
                continue;
1002
            }
1003
            $sql = 'SELECT path, comment, filename, size
1004
                    FROM '.$table_attachment.'
1005
                    WHERE c_id = '.$courseId.' AND announcement_id = '.$obj->id.'';
1006
            $result = Database::query($sql);
1007
            $attachment_obj = Database::fetch_object($result);
1008
            $att_path = $att_filename = $att_size = $atth_comment = '';
1009
1010
            if (!empty($attachment_obj)) {
1011
                $att_path = $attachment_obj->path;
1012
                $att_filename = $attachment_obj->filename;
1013
                $att_size = $attachment_obj->size;
1014
                $atth_comment = $attachment_obj->comment;
1015
            }
1016
1017
            $announcement = new Announcement(
1018
                $obj->id,
1019
                $obj->title,
1020
                $obj->content,
1021
                $obj->end_date,
1022
                $obj->display_order,
1023
                $obj->email_sent,
1024
                $att_path,
1025
                $att_filename,
1026
                $att_size,
1027
                $atth_comment
1028
            );
1029
            $this->course->add_resource($announcement);
1030
        }
1031
    }
1032
1033
    /**
1034
     * Build the events
1035
     * @param int $session_id Internal session ID
1036
     * @param int $courseId Internal course ID
1037
     * @param bool $with_base_content Whether to include content from the course without session or not
1038
     * @param array $id_list If you want to restrict the structure to only the given IDs
1039
     */
1040 View Code Duplication
    public function build_events(
1041
        $session_id = 0,
1042
        $courseId = 0,
1043
        $with_base_content = false,
1044
        $id_list = array()
1045
    ) {
1046
        $table = Database:: get_course_table(TABLE_AGENDA);
1047
1048
        $sessionCondition = api_get_session_condition(
1049
            $session_id,
1050
            true,
1051
            $with_base_content
1052
        );
1053
1054
        $sql = 'SELECT * FROM '.$table.'
1055
                WHERE c_id = '.$courseId.' '.$sessionCondition;
1056
        $db_result = Database::query($sql);
1057
        while ($obj = Database::fetch_object($db_result)) {
1058
            $table_attachment = Database:: get_course_table(
1059
                TABLE_AGENDA_ATTACHMENT
1060
            );
1061
            $sql = 'SELECT path, comment, filename, size
1062
                    FROM '.$table_attachment.'
1063
                    WHERE c_id = '.$courseId.' AND agenda_id = '.$obj->id.'';
1064
            $result = Database::query($sql);
1065
1066
            $attachment_obj = Database::fetch_object($result);
1067
            $att_path = $att_filename = $att_size = $atth_comment = '';
1068
            if (!empty($attachment_obj)) {
1069
                $att_path = $attachment_obj->path;
1070
                $att_filename = $attachment_obj->filename;
1071
                $att_size = $attachment_obj->size;
1072
                $atth_comment = $attachment_obj->comment;
1073
            }
1074
            $event = new CalendarEvent(
1075
                $obj->id,
1076
                $obj->title,
1077
                $obj->content,
1078
                $obj->start_date,
1079
                $obj->end_date,
1080
                $att_path,
1081
                $att_filename,
1082
                $att_size,
1083
                $atth_comment,
1084
                $obj->all_day
1085
            );
1086
            $this->course->add_resource($event);
1087
        }
1088
    }
1089
1090
    /**
1091
     * Build the course-descriptions
1092
     * @param int $session_id Internal session ID
1093
     * @param int $courseId Internal course ID
1094
     * @param bool $with_base_content Whether to include content from the course without session or not
1095
     * @param array $id_list If you want to restrict the structure to only the given IDs
1096
     */
1097
    public function build_course_descriptions(
1098
        $session_id = 0,
1099
        $courseId = 0,
1100
        $with_base_content = false,
1101
        $id_list = array()
1102
    ) {
1103
        $table = Database:: get_course_table(TABLE_COURSE_DESCRIPTION);
1104
1105 View Code Duplication
        if (!empty($session_id) && !empty($courseId)) {
1106
            $session_id = intval($session_id);
1107
            if ($with_base_content) {
1108
                $session_condition = api_get_session_condition(
1109
                    $session_id,
1110
                    true,
1111
                    true
1112
                );
1113
            } else {
1114
                $session_condition = api_get_session_condition(
1115
                    $session_id,
1116
                    true
1117
                );
1118
            }
1119
            $sql = 'SELECT * FROM '.$table.'
1120
                    WHERE c_id = '.$courseId.' '.$session_condition;
1121
        } else {
1122
            $table = Database:: get_course_table(TABLE_COURSE_DESCRIPTION);
1123
            $sql = 'SELECT * FROM '.$table.'
1124
                    WHERE c_id = '.$courseId.'  AND session_id = 0';
1125
        }
1126
1127
        $db_result = Database::query($sql);
1128 View Code Duplication
        while ($obj = Database::fetch_object($db_result)) {
1129
            $cd = new CourseDescription(
1130
                $obj->id,
1131
                $obj->title,
1132
                $obj->content,
1133
                $obj->description_type
1134
            );
1135
            $this->course->add_resource($cd);
1136
        }
1137
    }
1138
1139
    /**
1140
     * Build the learnpaths
1141
     * @param int $session_id Internal session ID
1142
     * @param int $courseId Internal course ID
1143
     * @param bool $with_base_content Whether to include content from the course without session or not
1144
     * @param array $id_list If you want to restrict the structure to only the given IDs
1145
     */
1146
    public function build_learnpaths(
1147
        $session_id = 0,
1148
        $courseId = 0,
1149
        $with_base_content = false,
1150
        $id_list = array()
1151
    ) {
1152
        $table_main = Database::get_course_table(TABLE_LP_MAIN);
1153
        $table_item = Database::get_course_table(TABLE_LP_ITEM);
1154
        $table_tool = Database::get_course_table(TABLE_TOOL_LIST);
1155
1156 View Code Duplication
        if (!empty($session_id) && !empty($courseId)) {
1157
            $session_id = intval($session_id);
1158
            if ($with_base_content) {
1159
                $session_condition = api_get_session_condition(
1160
                    $session_id,
1161
                    true,
1162
                    true
1163
                );
1164
            } else {
1165
                $session_condition = api_get_session_condition(
1166
                    $session_id,
1167
                    true
1168
                );
1169
            }
1170
            $sql = 'SELECT * FROM '.$table_main.'
1171
                    WHERE c_id = '.$courseId.'  '.$session_condition;
1172
        } else {
1173
            $sql = 'SELECT * FROM '.$table_main.'
1174
                    WHERE c_id = '.$courseId.' AND (session_id = 0 OR session_id IS NULL)';
1175
        }
1176
1177 View Code Duplication
        if (!empty($id_list)) {
1178
            $id_list = array_map('intval', $id_list);
1179
            $sql .= " AND id IN (".implode(', ', $id_list).") ";
1180
        }
1181
1182
        $db_result = Database::query($sql);
1183
        if ($db_result) {
1184
            while ($obj = Database::fetch_object($db_result)) {
1185
                $items = array();
1186
                $sql = "SELECT * FROM ".$table_item."
1187
                        WHERE c_id = '$courseId' AND lp_id = ".$obj->id;
1188
                $db_items = Database::query($sql);
1189
                while ($obj_item = Database::fetch_object($db_items)) {
1190
                    $item['id'] = $obj_item->id;
1191
                    $item['item_type'] = $obj_item->item_type;
1192
                    $item['ref'] = $obj_item->ref;
1193
                    $item['title'] = $obj_item->title;
1194
                    $item['description'] = $obj_item->description;
1195
                    $item['path'] = $obj_item->path;
1196
                    $item['min_score'] = $obj_item->min_score;
1197
                    $item['max_score'] = $obj_item->max_score;
1198
                    $item['mastery_score'] = $obj_item->mastery_score;
1199
                    $item['parent_item_id'] = $obj_item->parent_item_id;
1200
                    $item['previous_item_id'] = $obj_item->previous_item_id;
1201
                    $item['next_item_id'] = $obj_item->next_item_id;
1202
                    $item['display_order'] = $obj_item->display_order;
1203
                    $item['prerequisite'] = $obj_item->prerequisite;
1204
                    $item['parameters'] = $obj_item->parameters;
1205
                    $item['launch_data'] = $obj_item->launch_data;
1206
                    $item['audio'] = $obj_item->audio;
1207
                    $items[] = $item;
1208
                }
1209
1210
                $sql = "SELECT id FROM $table_tool
1211
                        WHERE
1212
                            c_id = $courseId AND
1213
                            (link LIKE '%lp_controller.php%lp_id=".$obj->id."%' AND image='scormbuilder.gif') AND
1214
                            visibility = '1' ";
1215
                $db_tool = Database::query($sql);
1216
1217
                if (Database::num_rows($db_tool)) {
1218
                    $visibility = '1';
1219
                } else {
1220
                    $visibility = '0';
1221
                }
1222
1223
                $lp = new CourseCopyLearnpath(
1224
                    $obj->id,
1225
                    $obj->lp_type,
1226
                    $obj->name,
1227
                    $obj->path,
1228
                    $obj->ref,
1229
                    $obj->description,
1230
                    $obj->content_local,
1231
                    $obj->default_encoding,
1232
                    $obj->default_view_mod,
1233
                    $obj->prevent_reinit,
1234
                    $obj->force_commit,
1235
                    $obj->content_maker,
1236
                    $obj->display_order,
1237
                    $obj->js_lib,
1238
                    $obj->content_license,
1239
                    $obj->debug,
1240
                    $visibility,
1241
                    $obj->author,
1242
                    $obj->preview_image,
1243
                    $obj->use_max_score,
1244
                    $obj->autolaunch,
1245
                    $obj->created_on,
1246
                    $obj->modified_on,
1247
                    $obj->publicated_on,
1248
                    $obj->expired_on,
1249
                    $obj->session_id,
1250
                    $items
1251
                );
1252
                $this->course->add_resource($lp);
1253
            }
1254
        }
1255
1256
        // Save scorm directory (previously build_scorm_documents())
1257
        $i = 1;
1258
        if ($dir = @opendir($this->course->backup_path.'/scorm')) {
1259
            while ($file = readdir($dir)) {
1260
                if (is_dir($this->course->backup_path.'/scorm/'.$file) &&
1261
                    !in_array($file, array('.', '..'))
1262
                ) {
1263
                    $doc = new ScormDocument($i++, '/'.$file, $file);
1264
                    $this->course->add_resource($doc);
1265
                }
1266
            }
1267
            closedir($dir);
1268
        }
1269
    }
1270
1271
    /**
1272
     * Build the glossaries
1273
     * @param int $session_id Internal session ID
1274
     * @param int $courseId Internal course ID
1275
     * @param bool $with_base_content Whether to include content from the course without session or not
1276
     * @param array $id_list If you want to restrict the structure to only the given IDs
1277
     */
1278
    public function build_glossary(
1279
        $session_id = 0,
1280
        $courseId = 0,
1281
        $with_base_content = false,
1282
        $id_list = array()
1283
    ) {
1284
        $table_glossary = Database :: get_course_table(TABLE_GLOSSARY);
1285
1286
        if (!empty($session_id) && !empty($courseId)) {
1287
            $session_id = intval($session_id);
1288
            if ($with_base_content) {
1289
                $session_condition = api_get_session_condition(
1290
                    $session_id,
1291
                    true,
1292
                    true
1293
                );
1294
            } else {
1295
                $session_condition = api_get_session_condition(
1296
                    $session_id,
1297
                    true
1298
                );
1299
            }
1300
            //@todo check this queries are the same ...
1301 View Code Duplication
            if (!empty($this->course->type) && $this->course->type == 'partial') {
0 ignored issues
show
Bug introduced by
The property type does not seem to exist in Chamilo\CourseBundle\Component\CourseCopy\Course.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1302
                $sql = 'SELECT * FROM '.$table_glossary.' g
1303
                        WHERE g.c_id = '.$courseId.' '.$session_condition;
1304
            } else {
1305
                $sql = 'SELECT * FROM '.$table_glossary.' g
1306
                        WHERE g.c_id = '.$courseId.' '.$session_condition;
1307
            }
1308
        } else {
1309
            $table_glossary = Database:: get_course_table(TABLE_GLOSSARY);
1310
            //@todo check this queries are the same ... ayayay
1311 View Code Duplication
            if (!empty($this->course->type) && $this->course->type == 'partial') {
1312
                $sql = 'SELECT * FROM '.$table_glossary.' g
1313
                        WHERE g.c_id = '.$courseId.' AND (session_id = 0 OR session_id IS NULL)';
1314
            } else {
1315
                $sql = 'SELECT * FROM '.$table_glossary.' g
1316
                        WHERE g.c_id = '.$courseId.' AND (session_id = 0 OR session_id IS NULL)';
1317
            }
1318
        }
1319
        $db_result = Database::query($sql);
1320
        while ($obj = Database::fetch_object($db_result)) {
1321
            $doc = new Glossary(
1322
                $obj->glossary_id,
1323
                $obj->name,
1324
                $obj->description,
1325
                $obj->display_order
1326
            );
1327
            $this->course->add_resource($doc);
1328
        }
1329
    }
1330
1331
    /*
1332
     * Build session course by jhon
1333
     */
1334
    public function build_session_course()
1335
    {
1336
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
1337
        $tbl_session_course = Database::get_main_table(
1338
            TABLE_MAIN_SESSION_COURSE
1339
        );
1340
        $list_course = CourseManager::get_course_list();
1341
        $list = array();
1342
        foreach ($list_course as $_course) {
1343
            $this->course = new Course();
1344
            $this->course->code = $_course['code'];
1345
            $this->course->type = 'partial';
0 ignored issues
show
Bug introduced by
The property type does not seem to exist in Chamilo\CourseBundle\Component\CourseCopy\Course.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1346
            $this->course->path = api_get_path(SYS_COURSE_PATH).$_course['directory'].'/';
1347
            $this->course->backup_path = api_get_path(SYS_COURSE_PATH).$_course['directory'];
1348
            $this->course->encoding = api_get_system_encoding(); //current platform encoding
1349
            $courseId = $_course['real_id'];
1350
            $sql_session = "SELECT s.id, name, c_id
1351
                FROM $tbl_session_course sc
1352
                INNER JOIN $tbl_session s
1353
                ON sc.session_id = s.id
1354
                WHERE sc.c_id = '$courseId' ";
1355
            $query_session = Database::query($sql_session);
1356
            while ($rows_session = Database::fetch_assoc($query_session)) {
1357
                $session = new CourseSession(
1358
                    $rows_session['id'],
1359
                    $rows_session['name']
1360
                );
1361
                $this->course->add_resource($session);
1362
            }
1363
            $list[] = $this->course;
1364
        }
1365
1366
        return $list;
1367
    }
1368
1369
    /**
1370
     * @param int $session_id Internal session ID
1371
     * @param int $courseId Internal course ID
1372
     * @param bool $with_base_content Whether to include content from the course without session or not
1373
     * @param array $id_list If you want to restrict the structure to only the given IDs
1374
     */
1375
    public function build_wiki(
1376
        $session_id = 0,
1377
        $courseId = 0,
1378
        $with_base_content = false,
1379
        $id_list = array()
1380
    ) {
1381
        $tbl_wiki = Database::get_course_table(TABLE_WIKI);
1382
1383 View Code Duplication
        if (!empty($session_id) && !empty($courseId)) {
1384
            $session_id = intval($session_id);
1385
            if ($with_base_content) {
1386
                $session_condition = api_get_session_condition(
1387
                    $session_id,
1388
                    true,
1389
                    true
1390
                );
1391
            } else {
1392
                $session_condition = api_get_session_condition(
1393
                    $session_id,
1394
                    true
1395
                );
1396
            }
1397
            $sql = 'SELECT * FROM '.$tbl_wiki.'
1398
                    WHERE c_id = '.$courseId.' '.$session_condition;
1399
        } else {
1400
            $tbl_wiki = Database::get_course_table(TABLE_WIKI);
1401
            $sql = 'SELECT * FROM '.$tbl_wiki.'
1402
                    WHERE c_id = '.$courseId.' AND (session_id = 0 OR session_id IS NULL)';
1403
        }
1404
        $db_result = Database::query($sql);
1405
        while ($obj = Database::fetch_object($db_result)) {
1406
            $wiki = new Wiki(
1407
                $obj->id,
1408
                $obj->page_id,
1409
                $obj->reflink,
1410
                $obj->title,
1411
                $obj->content,
1412
                $obj->user_id,
1413
                $obj->group_id,
1414
                $obj->dtime,
1415
                $obj->progress,
1416
                $obj->version
1417
            );
1418
            $this->course->add_resource($wiki);
1419
        }
1420
    }
1421
1422
    /**
1423
     * Build the Surveys
1424
     * @param int $session_id Internal session ID
1425
     * @param int $courseId Internal course ID
1426
     * @param bool $with_base_content Whether to include content from the course without session or not
1427
     * @param array $id_list If you want to restrict the structure to only the given IDs
1428
     */
1429
    public function build_thematic(
1430
        $session_id = 0,
1431
        $courseId = 0,
1432
        $with_base_content = false,
1433
        $id_list = array()
1434
    ) {
1435
        $table_thematic = Database :: get_course_table(TABLE_THEMATIC);
1436
        $table_thematic_advance = Database :: get_course_table(TABLE_THEMATIC_ADVANCE);
1437
        $table_thematic_plan = Database :: get_course_table(TABLE_THEMATIC_PLAN);
1438
1439
        $session_id = intval($session_id);
1440
        if ($with_base_content) {
1441
            $session_condition = api_get_session_condition(
1442
                $session_id,
1443
                true,
1444
                true
1445
            );
1446
        } else {
1447
            $session_condition = api_get_session_condition($session_id, true);
1448
        }
1449
1450
        $sql = "SELECT * FROM $table_thematic
1451
                WHERE c_id = $courseId $session_condition ";
1452
        $db_result = Database::query($sql);
1453
        while ($row = Database::fetch_array($db_result, 'ASSOC')) {
1454
            $thematic = new Thematic($row);
1455
            $sql = 'SELECT * FROM '.$table_thematic_advance.'
1456
                    WHERE c_id = '.$courseId.' AND thematic_id = '.$row['id'];
1457
1458
            $result = Database::query($sql);
1459
            while ($sub_row = Database::fetch_array($result, 'ASSOC')) {
1460
                $thematic->add_thematic_advance($sub_row);
1461
            }
1462
1463
            $items = api_get_item_property_by_tool(
1464
                'thematic_plan',
1465
                api_get_course_id(),
1466
                $session_id
1467
            );
1468
1469
            $thematic_plan_id_list = array();
1470
            if (!empty($items)) {
1471
                foreach ($items as $item) {
1472
                    $thematic_plan_id_list[] = $item['ref'];
1473
                    //$thematic_plan_complete_list[$item['ref']] = $item;
1474
                }
1475
            }
1476
            if (count($thematic_plan_id_list) > 0) {
1477
                $sql = "SELECT tp.*
1478
                        FROM $table_thematic_plan tp
1479
                            INNER JOIN $table_thematic t ON (t.id=tp.thematic_id)
1480
                        WHERE
1481
                            t.c_id = $courseId AND
1482
                            tp.c_id = $courseId AND
1483
                            thematic_id = {$row['id']}  AND
1484
                            tp.id IN (".implode(', ', $thematic_plan_id_list).") ";
1485
1486
                $result = Database::query($sql);
1487
                while ($sub_row = Database::fetch_array($result, 'ASSOC')) {
1488
                    $thematic->add_thematic_plan($sub_row);
1489
                }
1490
            }
1491
            $this->course->add_resource($thematic);
1492
        }
1493
    }
1494
1495
    /**
1496
     * Build the attendances
1497
     * @param int $session_id Internal session ID
1498
     * @param int $courseId Internal course ID
1499
     * @param bool $with_base_content Whether to include content from the course without session or not
1500
     * @param array $id_list If you want to restrict the structure to only the given IDs
1501
     */
1502
    public function build_attendance(
1503
        $session_id = 0,
1504
        $courseId = 0,
1505
        $with_base_content = false,
1506
        $id_list = array()
1507
    ) {
1508
        $table_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
1509
        $table_attendance_calendar = Database :: get_course_table(TABLE_ATTENDANCE_CALENDAR);
1510
1511
        $sessionCondition = api_get_session_condition($session_id, true, $with_base_content);
1512
1513
        $sql = 'SELECT * FROM '.$table_attendance.'
1514
                WHERE c_id = '.$courseId.' '.$sessionCondition;
1515
        $db_result = Database::query($sql);
1516
        while ($row = Database::fetch_array($db_result, 'ASSOC')) {
1517
            $obj = new Attendance($row);
1518
            $sql = 'SELECT * FROM '.$table_attendance_calendar.'
1519
                    WHERE c_id = '.$courseId.' AND attendance_id = '.$row['id'];
1520
1521
            $result = Database::query($sql);
1522
            while ($sub_row = Database::fetch_array($result, 'ASSOC')) {
1523
                $obj->add_attendance_calendar($sub_row);
1524
            }
1525
            $this->course->add_resource($obj);
1526
        }
1527
    }
1528
1529
    /**
1530
     * Build the works (or "student publications", or "assignments")
1531
     * @param int $session_id Internal session ID
1532
     * @param int $courseId Internal course ID
1533
     * @param bool $with_base_content Whether to include content from the course without session or not
1534
     * @param array $id_list If you want to restrict the structure to only the given IDs
1535
     */
1536
    public function build_works(
1537
        $session_id = 0,
1538
        $courseId = 0,
1539
        $with_base_content = false,
1540
        $id_list = array()
1541
    ) {
1542
        $table_work = Database:: get_course_table(TABLE_STUDENT_PUBLICATION);
1543
        $sessionCondition = api_get_session_condition(
1544
            $session_id,
1545
            true,
1546
            $with_base_content
1547
        );
1548
1549
        $sql = "SELECT * FROM $table_work
1550
                WHERE
1551
                    c_id = $courseId
1552
                    $sessionCondition AND
1553
                    filetype = 'folder' AND
1554
                    parent_id = 0 AND
1555
                    active = 1";
1556
        $db_result = Database::query($sql);
1557
        while ($row = Database::fetch_array($db_result, 'ASSOC')) {
1558
            $obj = new Work($row);
1559
            $this->course->add_resource($obj);
1560
        }
1561
    }
1562
1563
    /**
1564
     * @param int $session_id
1565
     * @param int $courseId
1566
     * @param bool $with_base_content
1567
     */
1568
    public function build_gradebook(
1569
        $session_id = 0,
1570
        $courseId = 0,
1571
        $with_base_content = false
1572
    ) {
1573
        $courseInfo = api_get_course_info_by_id($courseId);
1574
        $courseCode = $courseInfo['code'];
1575
        $cats = Category:: load(
1576
            null,
1577
            null,
1578
            $courseCode,
1579
            null,
1580
            null,
1581
            $session_id
1582
        );
1583
1584
        $obj = new GradeBookBackup($cats);
1585
        $this->course->add_resource($obj);
1586
    }
1587
}
1588