Completed
Push — master ( 441dc8...d4012c )
by Julito
09:56
created

CourseDescription   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 528
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 212
dl 0
loc 528
rs 8.8
c 0
b 0
f 0
wmc 45

27 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A get_descriptions() 0 27 3
A insert() 0 35 4
A get_data_by_description_type() 0 30 4
A get_data_by_id() 0 26 4
A get_max_description_type() 0 22 3
A get_description_data() 0 19 3
A get_id() 0 3 1
A set_title() 0 3 1
A get_content() 0 3 1
A set_description_type() 0 3 1
A get_default_information() 0 13 1
A get_default_description_icon() 0 13 1
A get_description_type() 0 3 1
A set_id() 0 3 1
A set_course_id() 0 3 1
A get_default_question() 0 13 1
A get_progress() 0 3 1
A get_default_description_title() 0 13 1
A update() 0 33 3
A get_session_id() 0 3 1
A set_content() 0 3 1
A get_title() 0 3 1
A set_session_id() 0 3 1
A delete() 0 23 2
A get_default_description_title_editable() 0 13 1
A set_progress() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like CourseDescription often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CourseDescription, and based on these observations, apply Extract Interface, too.

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * This file contains a class used like library provides functions for
6
 * course description tool. It's also used like model to
7
 * course_description_controller (MVC pattern).
8
 *
9
 * @author Christian Fasanando <[email protected]>
10
 *
11
 * @package chamilo.course_description
12
 */
13
14
/**
15
 * Class CourseDescription course descriptions.
16
 *
17
 * @package chamilo.course_description
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
        // Get course code
47
        $course_info = api_get_course_info_by_id($course_id);
48
        if (!empty($course_info)) {
49
            $course_id = $course_info['real_id'];
50
        } else {
51
            return [];
52
        }
53
        $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
54
        $sql = "SELECT * FROM $table
55
                WHERE c_id = $course_id AND session_id = '0'";
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['id']);
61
            $desc_tmp->set_title($row['title']);
62
63
            $desc_tmp->set_content($row['content']);
64
            $desc_tmp->set_session_id($row['session_id']);
65
            $desc_tmp->set_description_type($row['description_type']);
66
            $desc_tmp->set_progress($row['progress']);
67
            $results[] = $desc_tmp;
68
        }
69
70
        return $results;
71
    }
72
73
    /**
74
     * Get all data of course description by session id,
75
     * first you must set session_id property with the object CourseDescription.
76
     *
77
     * @return array
78
     */
79
    public function get_description_data()
80
    {
81
        $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
82
        $condition_session = api_get_session_condition(
83
            $this->session_id,
84
            true,
85
            true
86
        );
87
        $course_id = $this->course_id ?: api_get_course_int_id();
88
        $sql = "SELECT * FROM $table
89
		        WHERE c_id = $course_id $condition_session
90
		        ORDER BY id ";
91
        $rs = Database::query($sql);
92
        $data = [];
93
        while ($description = Database::fetch_array($rs)) {
94
            $data['descriptions'][$description['id']] = $description;
95
        }
96
97
        return $data;
98
    }
99
100
    /**
101
     * Get all data by description and session id,
102
     * first you must set session_id property with the object CourseDescription.
103
     *
104
     * @param int    $description_type Description type
105
     * @param string $courseId         Course code (optional)
106
     * @param int    $session_id       Session id (optional)
107
     *
108
     * @return array List of fields from the descriptions found of the given type
109
     */
110
    public function get_data_by_description_type(
111
        $description_type,
112
        $courseId = null,
113
        $session_id = null
114
    ) {
115
        $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
116
        if (empty($courseId)) {
117
            $courseId = api_get_course_int_id();
118
        }
119
120
        if (!isset($session_id)) {
121
            $session_id = $this->session_id;
122
        }
123
        $condition_session = api_get_session_condition($session_id);
124
        $description_type = intval($description_type);
125
        $sql = "SELECT * FROM $table
126
		        WHERE 
127
		            c_id = $courseId AND 
128
		            description_type = '$description_type' 
129
		            $condition_session ";
130
        $rs = Database::query($sql);
131
        $data = [];
132
        if ($description = Database::fetch_array($rs)) {
133
            $data['description_title'] = $description['title'];
134
            $data['description_content'] = $description['content'];
135
            $data['progress'] = $description['progress'];
136
            $data['id'] = $description['id'];
137
        }
138
139
        return $data;
140
    }
141
142
    /**
143
     * @param int    $id
144
     * @param string $course_code
145
     * @param int    $session_id
146
     *
147
     * @return array
148
     */
149
    public function get_data_by_id($id, $course_code = '', $session_id = null)
150
    {
151
        $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
152
        $course_id = api_get_course_int_id();
153
154
        if (!isset($session_id)) {
155
            $session_id = $this->session_id;
156
        }
157
        $condition_session = api_get_session_condition($session_id);
158
        if (!empty($course_code)) {
159
            $course_info = api_get_course_info($course_code);
160
            $course_id = $course_info['real_id'];
161
        }
162
        $id = intval($id);
163
        $sql = "SELECT * FROM $table
164
		        WHERE c_id = $course_id AND id='$id' $condition_session ";
165
        $rs = Database::query($sql);
166
        $data = [];
167
        if ($description = Database::fetch_array($rs)) {
168
            $data['description_type'] = $description['description_type'];
169
            $data['description_title'] = $description['title'];
170
            $data['description_content'] = $description['content'];
171
            $data['progress'] = $description['progress'];
172
        }
173
174
        return $data;
175
    }
176
177
    /**
178
     * Get maximum description type by session id,
179
     * first you must set session_id properties with the object CourseDescription.
180
     *
181
     * @return int maximum description time adding one
182
     */
183
    public function get_max_description_type()
184
    {
185
        $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
186
        $course_id = api_get_course_int_id();
187
188
        $sql = "SELECT MAX(description_type) as MAX
189
                FROM $table
190
		        WHERE c_id = $course_id AND session_id='".$this->session_id."'";
191
        $rs = Database::query($sql);
192
        $max = Database::fetch_array($rs);
193
194
        if ($max['MAX'] >= 8) {
195
            $description_type = 8;
196
        } else {
197
            $description_type = $max['MAX'] + 1;
198
        }
199
200
        if ($description_type < ADD_BLOCK) {
201
            $description_type = ADD_BLOCK;
202
        }
203
204
        return $description_type;
205
    }
206
207
    /**
208
     * Insert a description to the course_description table,
209
     * first you must set description_type, title, content, progress and
210
     * session_id properties with the object CourseDescription.
211
     *
212
     * @return int affected rows
213
     */
214
    public function insert()
215
    {
216
        if (empty($this->course_id)) {
217
            $course_id = api_get_course_int_id();
218
        } else {
219
            $course_id = $this->course_id;
220
        }
221
        $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
222
223
        $params = [
224
            'c_id' => $course_id,
225
            'description_type' => $this->description_type,
226
            'title' => $this->title,
227
            'content' => $this->content,
228
            'progress' => intval($this->progress),
229
            'session_id' => $this->session_id,
230
        ];
231
232
        $last_id = Database::insert($table, $params);
233
234
        if ($last_id > 0) {
235
            $sql = "UPDATE $table SET id = iid WHERE iid = $last_id";
236
            Database::query($sql);
237
238
            // insert into item_property
239
            api_item_property_update(
240
                api_get_course_info(),
241
                TOOL_COURSE_DESCRIPTION,
242
                $last_id,
0 ignored issues
show
Bug introduced by
It seems like $last_id can also be of type false; however, parameter $item_id of api_item_property_update() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

242
                /** @scrutinizer ignore-type */ $last_id,
Loading history...
243
                'CourseDescriptionAdded',
244
                api_get_user_id()
245
            );
246
        }
247
248
        return $last_id > 0 ? 1 : 0;
249
    }
250
251
    /**
252
     * Update a description, first you must set description_type, title, content, progress
253
     * and session_id properties with the object CourseDescription.
254
     *
255
     * @return int affected rows
256
     */
257
    public function update()
258
    {
259
        $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
260
        $params = [
261
            'title' => $this->title,
262
            'content' => $this->content,
263
            'progress' => intval($this->progress),
264
        ];
265
266
        Database::update(
267
            $table,
268
            $params,
269
            [
270
                'id = ? AND session_id = ? AND c_id = ?' => [
271
                    $this->id,
272
                    $this->session_id,
273
                    $this->course_id ? $this->course_id : api_get_course_int_id(),
274
                ],
275
            ]
276
        );
277
278
        if ($this->id > 0) {
279
            // Insert into item_property
280
            api_item_property_update(
281
                api_get_course_info(),
282
                TOOL_COURSE_DESCRIPTION,
283
                $this->id,
284
                'CourseDescriptionUpdated',
285
                api_get_user_id()
286
            );
287
        }
288
289
        return 1;
290
    }
291
292
    /**
293
     * Delete a description, first you must set description_type and session_id
294
     * properties with the object CourseDescription.
295
     *
296
     * @return int affected rows
297
     */
298
    public function delete()
299
    {
300
        $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
301
        $course_id = api_get_course_int_id();
302
        $sql = "DELETE FROM $table
303
			 	WHERE
304
			 	    c_id = $course_id AND
305
			 	    id = '".intval($this->id)."' AND
306
			 	    session_id = '".intval($this->session_id)."'";
307
        $result = Database::query($sql);
308
        $affected_rows = Database::affected_rows($result);
309
        if ($this->id > 0) {
310
            //insert into item_property
311
            api_item_property_update(
312
                api_get_course_info(),
313
                TOOL_COURSE_DESCRIPTION,
314
                $this->id,
315
                'CourseDescriptionDeleted',
316
                api_get_user_id()
317
            );
318
        }
319
320
        return $affected_rows;
321
    }
322
323
    /**
324
     * Get description titles by default.
325
     *
326
     * @return array
327
     */
328
    public function get_default_description_title()
329
    {
330
        $default_description_titles = [];
331
        $default_description_titles[1] = get_lang('GeneralDescription');
332
        $default_description_titles[2] = get_lang('Objectives');
333
        $default_description_titles[3] = get_lang('Topics');
334
        $default_description_titles[4] = get_lang('Methodology');
335
        $default_description_titles[5] = get_lang('CourseMaterial');
336
        $default_description_titles[6] = get_lang('HumanAndTechnicalResources');
337
        $default_description_titles[7] = get_lang('Assessment');
338
        $default_description_titles[8] = get_lang('Other');
339
340
        return $default_description_titles;
341
    }
342
343
    /**
344
     * Get description titles editable by default.
345
     *
346
     * @return array
347
     */
348
    public function get_default_description_title_editable()
349
    {
350
        $default_description_title_editable = [];
351
        $default_description_title_editable[1] = true;
352
        $default_description_title_editable[2] = true;
353
        $default_description_title_editable[3] = true;
354
        $default_description_title_editable[4] = true;
355
        $default_description_title_editable[5] = true;
356
        $default_description_title_editable[6] = true;
357
        $default_description_title_editable[7] = true;
358
        //$default_description_title_editable[8] = true;
359
360
        return $default_description_title_editable;
361
    }
362
363
    /**
364
     * Get description icons by default.
365
     *
366
     * @return array
367
     */
368
    public function get_default_description_icon()
369
    {
370
        $default_description_icon = [];
371
        $default_description_icon[1] = 'info.png';
372
        $default_description_icon[2] = 'objective.png';
373
        $default_description_icon[3] = 'topics.png';
374
        $default_description_icon[4] = 'strategy.png';
375
        $default_description_icon[5] = 'laptop.png';
376
        $default_description_icon[6] = 'teacher.png';
377
        $default_description_icon[7] = 'assessment.png';
378
        $default_description_icon[8] = 'wizard.png';
379
380
        return $default_description_icon;
381
    }
382
383
    /**
384
     * Get questions by default for help.
385
     *
386
     * @return array
387
     */
388
    public function get_default_question()
389
    {
390
        $question = [];
391
        $question[1] = get_lang('GeneralDescriptionQuestions');
392
        $question[2] = get_lang('ObjectivesQuestions');
393
        $question[3] = get_lang('TopicsQuestions');
394
        $question[4] = get_lang('MethodologyQuestions');
395
        $question[5] = get_lang('CourseMaterialQuestions');
396
        $question[6] = get_lang('HumanAndTechnicalResourcesQuestions');
397
        $question[7] = get_lang('AssessmentQuestions');
398
        //$question[8]= get_lang('ThematicAdvanceQuestions');
399
400
        return $question;
401
    }
402
403
    /**
404
     * Get informations by default for help.
405
     *
406
     * @return array
407
     */
408
    public function get_default_information()
409
    {
410
        $information = [];
411
        $information[1] = get_lang('GeneralDescriptionInformation');
412
        $information[2] = get_lang('ObjectivesInformation');
413
        $information[3] = get_lang('TopicsInformation');
414
        $information[4] = get_lang('MethodologyInformation');
415
        $information[5] = get_lang('CourseMaterialInformation');
416
        $information[6] = get_lang('HumanAndTechnicalResourcesInformation');
417
        $information[7] = get_lang('AssessmentInformation');
418
        //$information[8]= get_lang('ThematicAdvanceInformation');
419
420
        return $information;
421
    }
422
423
    /**
424
     * Set description id.
425
     */
426
    public function set_id($id)
427
    {
428
        $this->id = $id;
429
    }
430
431
    /**
432
     * Set description's course id.
433
     *
434
     * @param int $id Course ID
435
     */
436
    public function set_course_id($id)
437
    {
438
        $this->course_id = intval($id);
439
    }
440
441
    /**
442
     * Set description title.
443
     *
444
     * @param string $title
445
     */
446
    public function set_title($title)
447
    {
448
        $this->title = $title;
449
    }
450
451
    /**
452
     * Set description content.
453
     *
454
     * @param string $content
455
     */
456
    public function set_content($content)
457
    {
458
        $this->content = $content;
459
    }
460
461
    /**
462
     * Set description session id.
463
     *
464
     * @param int $session_id
465
     */
466
    public function set_session_id($session_id)
467
    {
468
        $this->session_id = $session_id;
469
    }
470
471
    /**
472
     * Set description type.
473
     */
474
    public function set_description_type($description_type)
475
    {
476
        $this->description_type = $description_type;
477
    }
478
479
    /**
480
     * Set progress of a description.
481
     *
482
     * @param string $progress
483
     */
484
    public function set_progress($progress)
485
    {
486
        $this->progress = $progress;
487
    }
488
489
    /**
490
     * get description id.
491
     *
492
     * @return int
493
     */
494
    public function get_id()
495
    {
496
        return $this->id;
497
    }
498
499
    /**
500
     * get description title.
501
     *
502
     * @return string
503
     */
504
    public function get_title()
505
    {
506
        return $this->title;
507
    }
508
509
    /**
510
     * get description content.
511
     *
512
     * @return string
513
     */
514
    public function get_content()
515
    {
516
        return $this->content;
517
    }
518
519
    /**
520
     * get session id.
521
     *
522
     * @return int
523
     */
524
    public function get_session_id()
525
    {
526
        return $this->session_id;
527
    }
528
529
    /**
530
     * get description type.
531
     *
532
     * @return int
533
     */
534
    public function get_description_type()
535
    {
536
        return $this->description_type;
537
    }
538
539
    /**
540
     * get progress of a description.
541
     *
542
     * @return int
543
     */
544
    public function get_progress()
545
    {
546
        return $this->progress;
547
    }
548
}
549