Passed
Push — master ( 7ade69...469b77 )
by Julito
11:15
created

CourseDescription::get_data_by_id()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 8
nop 3
dl 0
loc 27
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
/**
6
 * This file contains a class used like library provides functions for
7
 * course description tool. It's also used like model to
8
 * course_description_controller (MVC pattern).
9
 *
10
 * @author Christian Fasanando <[email protected]>
11
 */
12
13
use Chamilo\CoreBundle\Framework\Container;
14
use Chamilo\CourseBundle\Entity\CCourseDescription;
15
16
/**
17
 * Class CourseDescription course descriptions.
18
 */
19
class CourseDescription
20
{
21
    private $id;
22
    private $course_id;
23
    private $title;
24
    private $content;
25
    private $session_id;
26
    private $description_type;
27
    private $progress;
28
29
    /**
30
     * Constructor.
31
     */
32
    public function __construct()
33
    {
34
    }
35
36
    /**
37
     * Returns an array of objects of type CourseDescription corresponding to
38
     * a specific course, without session ids (session id = 0).
39
     *
40
     * @param int $course_id
41
     *
42
     * @return array Array of CourseDescriptions
43
     */
44
    public static function get_descriptions($course_id)
45
    {
46
        $course_id = (int) $course_id;
47
        // Get course code
48
        $course_info = api_get_course_info_by_id($course_id);
49
        if (!empty($course_info)) {
50
            $course_id = $course_info['real_id'];
51
        } else {
52
            return [];
53
        }
54
        $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
55
        $sql = "SELECT * FROM $table";
56
        $sql_result = Database::query($sql);
57
        $results = [];
58
        while ($row = Database::fetch_array($sql_result)) {
59
            $desc_tmp = new CourseDescription();
60
            $desc_tmp->set_id($row['iid']);
61
            $desc_tmp->set_title($row['title']);
62
            $desc_tmp->set_content($row['content']);
63
            $desc_tmp->set_session_id($row['session_id']);
64
            $desc_tmp->set_description_type($row['description_type']);
65
            $desc_tmp->set_progress($row['progress']);
66
            $results[] = $desc_tmp;
67
        }
68
69
        return $results;
70
    }
71
72
    /**
73
     * Get all data of course description by session id,
74
     * first you must set session_id property with the object CourseDescription.
75
     *
76
     * @return CCourseDescription[]
77
     */
78
    public function get_description_data()
79
    {
80
        $repo = Container::getCourseDescriptionRepository();
81
        $course_id = $this->course_id ?: api_get_course_int_id();
82
83
        $course = api_get_course_entity($course_id);
84
        $session = api_get_session_entity($this->session_id);
85
86
        $qb = $repo->getResourcesByCourse($course, $session);
87
88
        return $qb->getQuery()->getResult();
89
90
        /*$table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
91
        $condition_session = api_get_session_condition(
92
            $this->session_id,
93
            true,
94
            true
95
        );
96
97
98
        if (empty($course_id)) {
99
            return [];
100
        }
101
102
        $sql = "SELECT * FROM $table
103
		        ORDER BY iid ";
104
        $rs = Database::query($sql);
105
        $data = [];
106
        while ($description = Database::fetch_array($rs)) {
107
            $data['descriptions'][$description['iid']] = $description;
108
        }
109
110
        return $data;*/
111
    }
112
113
    /**
114
     * Get all data by description and session id,
115
     * first you must set session_id property with the object CourseDescription.
116
     *
117
     * @param int    $description_type Description type
118
     * @param string $courseId         Course code (optional)
119
     * @param int    $session_id       Session id (optional)
120
     *
121
     * @return array List of fields from the descriptions found of the given type
122
     */
123
    public function get_data_by_description_type(
124
        $description_type,
125
        $courseId = null,
126
        $session_id = null
127
    ) {
128
        $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
129
        $courseId = (int) $courseId;
130
131
        if (empty($courseId)) {
132
            $courseId = api_get_course_int_id();
133
        }
134
135
        if (!isset($session_id)) {
136
            $session_id = $this->session_id;
137
        }
138
        $condition_session = api_get_session_condition($session_id);
139
        $description_type = (int) $description_type;
140
141
        $sql = "SELECT * FROM $table
142
		        WHERE
143
		            description_type = '$description_type'
144
		             ";
145
        $rs = Database::query($sql);
146
        $data = [];
147
        if ($description = Database::fetch_array($rs)) {
148
            $data['description_title'] = $description['title'];
149
            $data['description_content'] = $description['content'];
150
            $data['progress'] = $description['progress'];
151
            $data['iid'] = $description['iid'];
152
        }
153
154
        return $data;
155
    }
156
157
    /**
158
     * @param int    $id
159
     * @param string $course_code
160
     * @param int    $session_id
161
     *
162
     * @return array
163
     */
164
    public function get_data_by_id($id, $course_code = '', $session_id = null)
165
    {
166
        $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
167
        $course_id = api_get_course_int_id();
168
        $id = (int) $id;
169
170
        if (!isset($session_id)) {
171
            $session_id = $this->session_id;
172
        }
173
        $condition_session = api_get_session_condition($session_id);
174
        if (!empty($course_code)) {
175
            $course_info = api_get_course_info($course_code);
176
            $course_id = $course_info['real_id'];
177
        }
178
179
        $sql = "SELECT * FROM $table
180
		        WHERE  iid='$id'  ";
181
        $rs = Database::query($sql);
182
        $data = [];
183
        if ($description = Database::fetch_array($rs)) {
184
            $data['description_type'] = $description['description_type'];
185
            $data['description_title'] = $description['title'];
186
            $data['description_content'] = $description['content'];
187
            $data['progress'] = $description['progress'];
188
        }
189
190
        return $data;
191
    }
192
193
    /**
194
     * Get maximum description type by session id,
195
     * first you must set session_id properties with the object CourseDescription.
196
     *
197
     * @return int maximum description time adding one
198
     */
199
    public function get_max_description_type()
200
    {
201
        $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
202
        $course_id = api_get_course_int_id();
203
204
        $sql = "SELECT MAX(description_type) as MAX
205
                FROM $table
206
		        ";
207
        $rs = Database::query($sql);
208
        $max = Database::fetch_array($rs);
209
210
        if ($max['MAX'] >= 8) {
211
            $description_type = 8;
212
        } else {
213
            $description_type = $max['MAX'] + 1;
214
        }
215
216
        if ($description_type < ADD_BLOCK) {
217
            $description_type = ADD_BLOCK;
218
        }
219
220
        return $description_type;
221
    }
222
223
    /**
224
     * Insert a description to the course_description table,
225
     * first you must set description_type, title, content, progress and
226
     * session_id properties with the object CourseDescription.
227
     *
228
     * @return int affected rows
229
     */
230
    public function insert()
231
    {
232
        if (empty($this->course_id)) {
233
            $course_id = api_get_course_int_id();
234
        } else {
235
            $course_id = $this->course_id;
236
        }
237
238
        $courseDescription = new CCourseDescription();
239
        $courseDescription
240
            ->setTitle($this->title)
241
            ->setContent($this->content)
242
            ->setProgress($this->progress)
243
            ->setDescriptionType($this->description_type)
244
        ;
245
246
        $course = api_get_course_entity($course_id);
247
        $session = api_get_session_entity($this->session_id);
248
        $courseDescription->setParent($course);
249
        $courseDescription->addCourseLink($course, $session);
250
251
        $repo = Container::getCourseDescriptionRepository();
252
        $repo->getEntityManager()->persist($courseDescription);
253
        $repo->getEntityManager()->flush();
254
255
        /*$table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
256
        $params = [
257
            'description_type' => $this->description_type,
258
            'progress' => intval($this->progress),
259
            'session_id' => $this->session_id,
260
        ];
261
262
        $last_id = Database::insert($table, $params);
263
264
        if ($last_id > 0) {
265
            // insert into item_property
266
            api_item_property_update(
267
                api_get_course_info(),
268
                TOOL_COURSE_DESCRIPTION,
269
                $last_id,
270
                'CourseDescriptionAdded',
271
                api_get_user_id()
272
            );
273
        }
274
275
        return $last_id > 0 ? 1 : 0;*/
276
277
        return true;
278
    }
279
280
    /**
281
     * Update a description, first you must set description_type, title, content, progress
282
     * and session_id properties with the object CourseDescription.
283
     *
284
     * @return int affected rows
285
     */
286
    public function update()
287
    {
288
        $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
289
        $params = [
290
            'title' => $this->title,
291
            'content' => $this->content,
292
            'progress' => intval($this->progress),
293
        ];
294
295
        Database::update(
296
            $table,
297
            $params,
298
            [
299
                'id = ? AND session_id = ? AND c_id = ?' => [
300
                    $this->id,
301
                    $this->session_id,
302
                    $this->course_id ? $this->course_id : api_get_course_int_id(),
303
                ],
304
            ]
305
        );
306
307
        if ($this->id > 0) {
308
            // Insert into item_property
309
            api_item_property_update(
0 ignored issues
show
Bug introduced by
The function api_item_property_update was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

309
            /** @scrutinizer ignore-call */ 
310
            api_item_property_update(
Loading history...
310
                api_get_course_info(),
311
                TOOL_COURSE_DESCRIPTION,
312
                $this->id,
313
                'CourseDescriptionUpdated',
314
                api_get_user_id()
315
            );
316
        }
317
318
        return 1;
319
    }
320
321
    /**
322
     * Delete a description, first you must set description_type and session_id
323
     * properties with the object CourseDescription.
324
     *
325
     * @return int affected rows
326
     */
327
    public function delete()
328
    {
329
        $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
330
        $course_id = api_get_course_int_id();
331
        $sql = "DELETE FROM $table
332
			 	WHERE
333
			 	    c_id = $course_id AND
334
			 	    id = '".intval($this->id)."' AND
335
			 	    session_id = '".intval($this->session_id)."'";
336
        $result = Database::query($sql);
337
        $affected_rows = Database::affected_rows($result);
338
        if ($this->id > 0) {
339
            //insert into item_property
340
            api_item_property_update(
0 ignored issues
show
Bug introduced by
The function api_item_property_update was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

340
            /** @scrutinizer ignore-call */ 
341
            api_item_property_update(
Loading history...
341
                api_get_course_info(),
342
                TOOL_COURSE_DESCRIPTION,
343
                $this->id,
344
                'CourseDescriptionDeleted',
345
                api_get_user_id()
346
            );
347
        }
348
349
        return $affected_rows;
350
    }
351
352
    /**
353
     * Get description titles by default.
354
     *
355
     * @return array
356
     */
357
    public function get_default_description_title()
358
    {
359
        $default_description_titles = [];
360
        $default_description_titles[1] = get_lang('Description');
361
        $default_description_titles[2] = get_lang('Objectives');
362
        $default_description_titles[3] = get_lang('Topics');
363
        $default_description_titles[4] = get_lang('Methodology');
364
        $default_description_titles[5] = get_lang('Course material');
365
        $default_description_titles[6] = get_lang('Resources');
366
        $default_description_titles[7] = get_lang('Assessment');
367
        $default_description_titles[8] = get_lang('Other');
368
369
        return $default_description_titles;
370
    }
371
372
    /**
373
     * Get description titles editable by default.
374
     *
375
     * @return array
376
     */
377
    public function get_default_description_title_editable()
378
    {
379
        $default_description_title_editable = [];
380
        $default_description_title_editable[1] = true;
381
        $default_description_title_editable[2] = true;
382
        $default_description_title_editable[3] = true;
383
        $default_description_title_editable[4] = true;
384
        $default_description_title_editable[5] = true;
385
        $default_description_title_editable[6] = true;
386
        $default_description_title_editable[7] = true;
387
        //$default_description_title_editable[8] = true;
388
389
        return $default_description_title_editable;
390
    }
391
392
    /**
393
     * Get description icons by default.
394
     *
395
     * @return array
396
     */
397
    public function get_default_description_icon()
398
    {
399
        $default_description_icon = [];
400
        $default_description_icon[1] = 'info.png';
401
        $default_description_icon[2] = 'objective.png';
402
        $default_description_icon[3] = 'topics.png';
403
        $default_description_icon[4] = 'strategy.png';
404
        $default_description_icon[5] = 'laptop.png';
405
        $default_description_icon[6] = 'teacher.png';
406
        $default_description_icon[7] = 'assessment.png';
407
        $default_description_icon[8] = 'wizard.png';
408
409
        return $default_description_icon;
410
    }
411
412
    /**
413
     * Get questions by default for help.
414
     *
415
     * @return array
416
     */
417
    public function get_default_question()
418
    {
419
        $question = [];
420
        $question[1] = get_lang('DescriptionQuestions');
421
        $question[2] = get_lang('What should the end results be when the learner has completed the course? What are the activities performed during the course?');
422
        $question[3] = get_lang('How does the course progress? Where should the learner pay special care? Are there identifiable problems in understanding different areas? How much time should one dedicate to the different areas of the course?');
423
        $question[4] = get_lang('What methods and activities help achieve the objectives of the course?  What would the schedule be?');
424
        $question[5] = get_lang('Course materialQuestions');
425
        $question[6] = get_lang('ResourcesQuestions');
426
        $question[7] = get_lang('How will learners be assessed? Are there strategies to develop in order to master the topic?');
427
        //$question[8]= get_lang('What is the current progress you have reached with your learners inside your course? How much do you think is remaining in comparison to the complete program?');
428
429
        return $question;
430
    }
431
432
    /**
433
     * Get informations by default for help.
434
     *
435
     * @return array
436
     */
437
    public function get_default_information()
438
    {
439
        $information = [];
440
        $information[1] = get_lang('DescriptionInformation');
441
        $information[2] = get_lang('What are the objectives of the course (competences, skills, outcomes)?');
442
        $information[3] = get_lang('List of topics included in the training. Importance of each topic. Level of difficulty. Structure and inter-dependence of the different parts.');
443
        $information[4] = get_lang('Presentation of the activities (conference, papers, group research, labs...).');
444
        $information[5] = get_lang('Course materialInformation');
445
        $information[6] = get_lang('ResourcesInformation');
446
        $information[7] = get_lang('Criteria for skills acquisition.');
447
        //$information[8]= get_lang('The thematic advance tool allows you to organize your course through time.');
448
449
        return $information;
450
    }
451
452
    /**
453
     * Set description id.
454
     */
455
    public function set_id($id)
456
    {
457
        $this->id = $id;
458
    }
459
460
    /**
461
     * Set description's course id.
462
     *
463
     * @param int $id Course ID
464
     */
465
    public function set_course_id($id)
466
    {
467
        $this->course_id = intval($id);
468
    }
469
470
    /**
471
     * Set description title.
472
     *
473
     * @param string $title
474
     */
475
    public function set_title($title)
476
    {
477
        $this->title = $title;
478
    }
479
480
    /**
481
     * Set description content.
482
     *
483
     * @param string $content
484
     */
485
    public function set_content($content)
486
    {
487
        $this->content = $content;
488
    }
489
490
    /**
491
     * Set description session id.
492
     *
493
     * @param int $session_id
494
     */
495
    public function set_session_id($session_id)
496
    {
497
        $this->session_id = $session_id;
498
    }
499
500
    /**
501
     * Set description type.
502
     */
503
    public function set_description_type($description_type)
504
    {
505
        $this->description_type = $description_type;
506
    }
507
508
    /**
509
     * Set progress of a description.
510
     *
511
     * @param string $progress
512
     */
513
    public function set_progress($progress)
514
    {
515
        $this->progress = $progress;
516
    }
517
518
    /**
519
     * get description id.
520
     *
521
     * @return int
522
     */
523
    public function get_id()
524
    {
525
        return $this->id;
526
    }
527
528
    /**
529
     * get description title.
530
     *
531
     * @return string
532
     */
533
    public function get_title()
534
    {
535
        return $this->title;
536
    }
537
538
    /**
539
     * get description content.
540
     *
541
     * @return string
542
     */
543
    public function get_content()
544
    {
545
        return $this->content;
546
    }
547
548
    /**
549
     * get session id.
550
     *
551
     * @return int
552
     */
553
    public function get_session_id()
554
    {
555
        return $this->session_id;
556
    }
557
558
    /**
559
     * get description type.
560
     *
561
     * @return int
562
     */
563
    public function get_description_type()
564
    {
565
        return $this->description_type;
566
    }
567
568
    /**
569
     * get progress of a description.
570
     *
571
     * @return int
572
     */
573
    public function get_progress()
574
    {
575
        return $this->progress;
576
    }
577
}
578