Passed
Push — master ( 4ca170...784f70 )
by Julito
12:58
created

Thematic::thematic_advance_save()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 88
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 57
nc 3
nop 0
dl 0
loc 88
rs 8.9381
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
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Entity\Resource\ResourceLink;
6
use Chamilo\CoreBundle\Framework\Container;
7
use Chamilo\CourseBundle\Entity\CThematic;
8
use Chamilo\CourseBundle\Entity\CThematicAdvance;
9
use Chamilo\CourseBundle\Entity\CThematicPlan;
10
11
/**
12
 * Provides functions for thematic option inside attendance tool.
13
 * It's also used like model to thematic_controller (MVC pattern)
14
 * Thematic class can be used to instanciate objects or as a library for thematic control.
15
 *
16
 * @author Christian Fasanando <[email protected]>
17
 * @author Julio Montoya <[email protected]> SQL fixes
18
 */
19
class Thematic
20
{
21
    private $session_id;
22
    private $thematic_id;
23
    private $thematic_title;
24
    private $thematic_content;
25
    private $thematic_plan_id;
0 ignored issues
show
introduced by
The private property $thematic_plan_id is not used, and could be removed.
Loading history...
26
    private $thematic_plan_title;
27
    private $thematic_plan_description;
28
    private $thematic_plan_description_type;
29
    private $thematic_advance_id;
30
    private $attendance_id;
31
    private $thematic_advance_content;
32
    private $start_date;
33
    private $duration;
34
    private $course_int_id;
35
36
    /**
37
     * Constructor.
38
     */
39
    public function __construct()
40
    {
41
        $this->course_int_id = api_get_course_int_id();
42
    }
43
44
    /**
45
     * Get the total number of thematic inside current course and current session.
46
     *
47
     * @see SortableTable#get_total_number_of_items()
48
     */
49
    public function get_number_of_thematics()
50
    {
51
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
52
        $condition_session = '';
53
        if (!api_get_session_id()) {
54
            $condition_session = api_get_session_condition(0);
55
        }
56
        $course_id = api_get_course_int_id();
57
        $sql = "SELECT COUNT(id) AS total_number_of_items
58
                FROM $tbl_thematic
59
                WHERE c_id = $course_id AND active = 1 $condition_session ";
60
        $res = Database::query($sql);
61
        $obj = Database::fetch_object($res);
62
63
        return $obj->total_number_of_items;
64
    }
65
66
    /**
67
     * Get the thematics to display on the current page (fill the sortable-table).
68
     *
69
     * @param   int     offset of first user to recover
70
     * @param   int     Number of users to get
71
     * @param   int     Column to sort on
72
     * @param   string  Order (ASC,DESC)
73
     *
74
     * @return array
75
     *
76
     * @see SortableTable#get_table_data($from)
77
     */
78
    public function get_thematic_data($from, $number_of_items, $column, $direction)
79
    {
80
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
81
        $condition_session = '';
82
        if (!api_get_session_id()) {
83
            $condition_session = api_get_session_condition(0);
84
        }
85
        $column = intval($column);
86
        $from = intval($from);
87
        $number_of_items = intval($number_of_items);
88
89
        if (!in_array($direction, ['ASC', 'DESC'])) {
90
            $direction = 'ASC';
91
        }
92
93
        $course_id = api_get_course_int_id();
94
95
        $sql = "SELECT id AS col0, title AS col1, display_order AS col2, session_id
96
                FROM $tbl_thematic
97
                WHERE c_id = $course_id AND active = 1 $condition_session
98
                ORDER BY col2
99
                LIMIT $from,$number_of_items ";
100
        $res = Database::query($sql);
101
102
        $thematics = [];
103
        $user_info = api_get_user_info(api_get_user_id());
104
        while ($thematic = Database::fetch_row($res)) {
105
            $session_star = '';
106
            if (api_get_session_id() == $thematic[3]) {
107
                $session_star = api_get_session_image(api_get_session_id(), $user_info['status']);
108
            }
109
            $thematic[1] = '<a href="index.php?'.api_get_cidreq().'&action=thematic_details&thematic_id='.$thematic[0].'">'.
110
                Security::remove_XSS($thematic[1], STUDENT).$session_star.'</a>';
111
            if (api_is_allowed_to_edit(null, true)) {
112
                $actions = '';
113
114
                if (api_get_session_id()) {
115
                    if (api_get_session_id() == $thematic[3]) {
116
                        $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_plan_list&thematic_id='.$thematic[0].'">'.
117
                            Display::return_icon('lesson_plan.png', get_lang('Thematic plan'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
118
                        $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_list&thematic_id='.$thematic[0].'">'.
119
                            Display::return_icon('lesson_plan_calendar.png', get_lang('Thematic advance'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
120
121
                        $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_edit&thematic_id='.$thematic[0].'">'.
122
                            Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).'</a>';
123
                        $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('Are you sure you want to delete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=thematic_delete&thematic_id='.$thematic[0].'">'.
124
                            Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
125
                    } else {
126
                        $actions .= Display::return_icon(
127
                            'lesson_plan_na.png',
128
                            get_lang('Thematic plan'),
129
                            '',
130
                            ICON_SIZE_SMALL
131
                        ).'&nbsp;';
132
                        $actions .= Display::return_icon(
133
                            'lesson_plan_calendar_na.png',
134
                            get_lang('Thematic advance'),
135
                            '',
136
                            ICON_SIZE_SMALL
137
                        ).'&nbsp;';
138
                        $actions .= Display::return_icon('edit_na.png', get_lang('Edit'), '', ICON_SIZE_SMALL);
139
                        $actions .= Display::return_icon(
140
                            'delete_na.png',
141
                            get_lang('Delete'),
142
                            '',
143
                            ICON_SIZE_SMALL
144
                        ).'&nbsp;';
145
                        $actions .= Display::url(
146
                            Display::return_icon('cd.gif', get_lang('Copy')),
147
                            'index.php?'.api_get_cidreq().'&action=thematic_copy&thematic_id='.$thematic[0]
148
                        );
149
                    }
150
                } else {
151
                    $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_plan_list&thematic_id='.$thematic[0].'">'.
152
                        Display::return_icon('lesson_plan.png', get_lang('Thematic plan'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
153
                    $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_list&thematic_id='.$thematic[0].'">'.
154
                        Display::return_icon('lesson_plan_calendar.png', get_lang('Thematic advance'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
155
156
                    if ($thematic[2] > 1) {
157
                        $actions .= '<a href="'.api_get_self().'?action=moveup&'.api_get_cidreq().'&thematic_id='.$thematic[0].'">'.
158
                            Display::return_icon('up.png', get_lang('Up'), '', ICON_SIZE_SMALL).'</a>';
159
                    } else {
160
                        $actions .= Display::return_icon('up_na.png', '&nbsp;', '', ICON_SIZE_SMALL);
161
                    }
162
                    if ($thematic[2] < self::get_max_thematic_item()) {
0 ignored issues
show
Bug Best Practice introduced by
The method Thematic::get_max_thematic_item() is not static, but was called statically. ( Ignorable by Annotation )

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

162
                    if ($thematic[2] < self::/** @scrutinizer ignore-call */ get_max_thematic_item()) {
Loading history...
163
                        $actions .= '<a href="'.api_get_self().'?action=movedown&a'.api_get_cidreq().'&thematic_id='.$thematic[0].'">'.
164
                            Display::return_icon('down.png', get_lang('down'), '', ICON_SIZE_SMALL).'</a>';
165
                    } else {
166
                        $actions .= Display::return_icon('down_na.png', '&nbsp;', '', ICON_SIZE_SMALL);
167
                    }
168
                    $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_edit&thematic_id='.$thematic[0].'">'.
169
                        Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).'</a>';
170
                    $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('Are you sure you want to delete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=thematic_delete&thematic_id='.$thematic[0].'">'.
171
                        Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
172
                }
173
                $thematics[] = [$thematic[0], $thematic[1], $actions];
174
            }
175
        }
176
177
        return $thematics;
178
    }
179
180
    /**
181
     * Get the maximum display order of the thematic item.
182
     *
183
     * @param bool $use_session
184
     *
185
     * @return int Maximum display order
186
     */
187
    public function get_max_thematic_item($use_session = true)
188
    {
189
        // Database table definition
190
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
191
        $session_id = api_get_session_id();
192
        if ($use_session) {
193
            $condition_session = api_get_session_condition($session_id);
194
        } else {
195
            $condition_session = '';
196
        }
197
        $course_id = api_get_course_int_id();
198
        $sql = "SELECT MAX(display_order)
199
                FROM $tbl_thematic
200
                WHERE c_id = $course_id AND active = 1 $condition_session";
201
        $rs = Database::query($sql);
202
        $row = Database::fetch_array($rs);
203
204
        return $row[0];
205
    }
206
207
    /**
208
     * Move a thematic.
209
     *
210
     * @param string $direction   (up, down)
211
     * @param int    $thematic_id
212
     */
213
    public function move_thematic($direction, $thematic_id)
214
    {
215
        // Database table definition
216
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
217
218
        // sort direction
219
        if ('up' == $direction) {
220
            $sortorder = 'DESC';
221
        } else {
222
            $sortorder = 'ASC';
223
        }
224
        $course_id = api_get_course_int_id();
225
        $session_id = api_get_session_id();
226
        $condition_session = api_get_session_condition($session_id);
227
228
        $sql = "SELECT id, display_order
229
                FROM $tbl_thematic
230
                WHERE c_id = $course_id AND active = 1 $condition_session
231
                ORDER BY display_order $sortorder";
232
        $res = Database::query($sql);
233
        $found = false;
234
235
        // Variable definition
236
        $current_id = 0;
237
        $next_id = 0;
238
        while ($row = Database::fetch_array($res)) {
239
            if ($found && empty($next_id)) {
240
                $next_id = intval($row['id']);
241
                $next_display_order = intval($row['display_order']);
242
            }
243
244
            if ($row['id'] == $thematic_id) {
245
                $current_id = intval($thematic_id);
246
                $current_display_order = intval($row['display_order']);
247
                $found = true;
248
            }
249
        }
250
251
        // get last done thematic advance before move thematic list
252
        $last_done_thematic_advance = $this->get_last_done_thematic_advance();
253
254
        if (!empty($next_display_order) && !empty($current_id)) {
255
            $sql = "UPDATE $tbl_thematic SET display_order = $next_display_order
256
                    WHERE c_id = $course_id AND id = $current_id ";
257
            Database::query($sql);
258
        }
259
        if (!empty($current_display_order) && !empty($next_id)) {
260
            $sql = "UPDATE $tbl_thematic SET
261
                    display_order = $current_display_order
262
                    WHERE c_id = $course_id AND id = $next_id ";
263
            Database::query($sql);
264
        }
265
266
        // update done advances with de current thematic list
267
        $this->update_done_thematic_advances($last_done_thematic_advance);
268
    }
269
270
    /**
271
     * Get thematic list.
272
     *
273
     * @param int    $thematic_id Thematic id (optional), get list by id
274
     * @param string $course_code
275
     * @param int    $session_id
276
     *
277
     * @return array Thematic data
278
     */
279
    public static function get_thematic_list(
280
        $thematic_id = null,
281
        $course_code = null,
282
        $session_id = null
283
    ) {
284
        // set current course and session
285
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
286
        $course_info = api_get_course_info($course_code);
287
        $course_id = $course_info['real_id'];
288
289
        if (isset($session_id)) {
290
            $session_id = intval($session_id);
291
        } else {
292
            $session_id = api_get_session_id();
293
        }
294
295
        $data = [];
296
        if (isset($thematic_id)) {
297
            $thematic_id = intval($thematic_id);
298
            $condition = " WHERE id = $thematic_id AND active = 1 ";
299
        } else {
300
            if (empty($session_id)) {
301
                $condition_session = api_get_session_condition(0);
302
            } else {
303
                $condition_session = api_get_session_condition($session_id, true, true);
304
            }
305
            $condition = " WHERE active = 1 $condition_session ";
306
        }
307
        $sql = "SELECT *
308
                FROM $tbl_thematic $condition AND c_id = $course_id
309
                ORDER BY display_order ";
310
311
        $res = Database::query($sql);
312
        if (Database::num_rows($res) > 0) {
313
            if (!empty($thematic_id)) {
314
                $data = Database::fetch_array($res, 'ASSOC');
315
            } else {
316
                while ($row = Database::fetch_array($res, 'ASSOC')) {
317
                    $data[$row['id']] = $row;
318
                }
319
            }
320
        }
321
322
        return $data;
323
    }
324
325
    /**
326
     * Insert or update a thematic.
327
     *
328
     * @return int last thematic id
329
     */
330
    public function thematic_save()
331
    {
332
        // definition database table
333
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
334
335
        // protect data
336
        $id = intval($this->thematic_id);
337
        $title = $this->thematic_title;
338
        $content = $this->thematic_content;
339
        $session_id = intval($this->session_id);
340
341
        // get the maximum display order of all the glossary items
342
        $max_thematic_item = $this->get_max_thematic_item(false);
343
344
        $repo = Container::getThematicRepository();
345
        $em = $repo->getEntityManager();
346
347
        if (empty($id)) {
348
            $thematic = new CThematic();
349
            $thematic
350
                ->setTitle($title)
351
                ->setContent($content)
352
                ->setActive(1)
353
                ->setCId($this->course_int_id)
354
                ->setDisplayOrder($max_thematic_item + 1)
355
                ->setSessionId($session_id)
356
            ;
357
358
            $em->persist($thematic);
359
360
            $repo->addResourceToCourse(
361
                $thematic,
362
                ResourceLink::VISIBILITY_PUBLISHED,
363
                api_get_user_entity(api_get_user_id()),
364
                api_get_course_entity(),
365
                api_get_session_entity(),
366
                api_get_group_entity()
367
            );
368
369
            $em->flush();
370
371
            // insert
372
            /*$params = [
373
                'c_id' => $this->course_int_id,
374
                'active' => 1,
375
                'display_order' => intval($max_thematic_item) + 1,
376
                'session_id' => $session_id,
377
            ];*/
378
            $last_id = $thematic->getIid();
379
            if ($last_id) {
380
                $sql = "UPDATE $tbl_thematic SET id = iid WHERE iid = $last_id";
381
                Database::query($sql);
382
                /*api_item_property_update(
383
                    $_course,
384
                    'thematic',
385
                    $last_id,
386
                    'ThematicAdded',
387
                    $user_id
388
                );*/
389
            }
390
        } else {
391
            $thematic = $repo->find($id);
392
            if ($thematic) {
393
                $thematic
394
                    ->setTitle($title)
395
                    ->setContent($content)
396
                ;
397
398
                $em->persist($thematic);
399
                $em->flush();
400
            }
401
402
            // Update
403
            /*$params = [
404
                'title' => $title,
405
                'content' => $content,
406
                'session_id' => $session_id,
407
            ];
408
409
            Database::update(
410
                $tbl_thematic,
411
                $params,
412
                ['id  = ? AND c_id = ?' => [$id, $this->course_int_id]]
413
            );
414
415
            $last_id = $id;
416
417
            // save inside item property table
418
            api_item_property_update(
419
                $_course,
420
                'thematic',
421
                $last_id,
422
                'ThematicUpdated',
423
                $user_id
424
            );*/
425
        }
426
427
        return $thematic;
428
    }
429
430
    /**
431
     * Delete logically (set active field to 0) a thematic.
432
     *
433
     * @param int|array One or many thematic ids
434
     *
435
     * @return int Affected rows
436
     */
437
    public function delete($thematic_id)
438
    {
439
        $_course = api_get_course_info();
440
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
441
        $affected_rows = 0;
442
        $user_id = api_get_user_id();
443
        $course_id = api_get_course_int_id();
444
445
        if (is_array($thematic_id)) {
446
            foreach ($thematic_id as $id) {
447
                $id = intval($id);
448
                $sql = "UPDATE $tbl_thematic SET active = 0
449
                        WHERE c_id = $course_id AND id = $id";
450
                $result = Database::query($sql);
451
                $affected_rows += Database::affected_rows($result);
452
                if (!empty($affected_rows)) {
453
                    // update row item property table
454
                    /*api_item_property_update(
455
                        $_course,
456
                        'thematic',
457
                        $id,
458
                        'ThematicDeleted',
459
                        $user_id
460
                    );*/
461
                }
462
            }
463
        } else {
464
            $thematic_id = intval($thematic_id);
465
            $sql = "UPDATE $tbl_thematic SET active = 0
466
                    WHERE c_id = $course_id AND id = $thematic_id";
467
            $result = Database::query($sql);
468
            $affected_rows = Database::affected_rows($result);
469
            if (!empty($affected_rows)) {
470
                // update row item property table
471
                /*api_item_property_update(
472
                    $_course,
473
                    'thematic',
474
                    $thematic_id,
475
                    'ThematicDeleted',
476
                    $user_id
477
                );*/
478
            }
479
        }
480
481
        return $affected_rows;
482
    }
483
484
    /**
485
     * @param int $thematic_id
486
     */
487
    public function copy($thematic_id)
488
    {
489
        $thematic = self::get_thematic_list($thematic_id, api_get_course_id(), 0);
490
        $thematic_copy = new Thematic();
491
        $thematic_copy->set_thematic_attributes(
492
            '',
493
            $thematic['title'].' - '.get_lang('Copy'),
494
            $thematic['content'],
495
            api_get_session_id()
496
        );
497
498
        $new_thematic_id = $thematic_copy->thematic_save();
499
        if (!empty($new_thematic_id)) {
500
            $thematic_advanced = self::get_thematic_advance_by_thematic_id($thematic_id);
0 ignored issues
show
Bug Best Practice introduced by
The method Thematic::get_thematic_advance_by_thematic_id() is not static, but was called statically. ( Ignorable by Annotation )

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

500
            /** @scrutinizer ignore-call */ 
501
            $thematic_advanced = self::get_thematic_advance_by_thematic_id($thematic_id);
Loading history...
501
            if (!empty($thematic_advanced)) {
502
                foreach ($thematic_advanced as $item) {
503
                    $thematic = new Thematic();
504
                    $thematic->set_thematic_advance_attributes(
505
                        0,
506
                        $new_thematic_id,
507
                        0,
508
                        $item['content'],
509
                        $item['start_date'],
510
                        $item['duration']
511
                    );
512
                    $thematic->thematic_advance_save();
513
                }
514
            }
515
            $thematic_plan = self::get_thematic_plan_data($thematic_id);
0 ignored issues
show
Bug Best Practice introduced by
The method Thematic::get_thematic_plan_data() is not static, but was called statically. ( Ignorable by Annotation )

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

515
            /** @scrutinizer ignore-call */ 
516
            $thematic_plan = self::get_thematic_plan_data($thematic_id);
Loading history...
516
            if (!empty($thematic_plan)) {
517
                foreach ($thematic_plan as $item) {
518
                    $thematic = new Thematic();
519
                    $thematic->set_thematic_plan_attributes(
520
                        $new_thematic_id,
521
                        $item['title'],
522
                        $item['description'],
523
                        $item['description_type']
524
                    );
525
                    $thematic->thematic_plan_save();
526
                }
527
            }
528
        }
529
    }
530
531
    /**
532
     * Get the total number of thematic advance inside current course.
533
     *
534
     * @see SortableTable#get_total_number_of_items()
535
     */
536
    public static function get_number_of_thematic_advances()
537
    {
538
        global $thematic_id;
539
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
540
        $course_id = api_get_course_int_id();
541
        $thematic_id = (int) $thematic_id;
542
543
        $sql = "SELECT COUNT(id) AS total_number_of_items
544
                FROM $table
545
                WHERE c_id = $course_id AND thematic_id = $thematic_id ";
546
        $res = Database::query($sql);
547
        $obj = Database::fetch_object($res);
548
549
        return $obj->total_number_of_items;
550
    }
551
552
    /**
553
     * Get the thematic advances to display on the current page (fill the sortable-table).
554
     *
555
     * @param   int     offset of first user to recover
556
     * @param   int     Number of users to get
557
     * @param   int     Column to sort on
558
     * @param   string  Order (ASC,DESC)
559
     *
560
     * @return array
561
     *
562
     * @see SortableTable#get_table_data($from)
563
     */
564
    public static function get_thematic_advance_data($from, $number_of_items, $column, $direction)
565
    {
566
        global $thematic_id;
567
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
568
        $column = intval($column);
569
        $from = intval($from);
570
        $number_of_items = intval($number_of_items);
571
        if (!in_array($direction, ['ASC', 'DESC'])) {
572
            $direction = 'ASC';
573
        }
574
        $data = [];
575
        $course_id = api_get_course_int_id();
576
        $thematic_id = (int) $thematic_id;
577
        if (api_is_allowed_to_edit(null, true)) {
578
            $sql = "SELECT id AS col0, start_date AS col1, duration AS col2, content AS col3
579
                    FROM $table
580
                    WHERE c_id = $course_id AND thematic_id = $thematic_id
581
                    ORDER BY col$column $direction
582
                    LIMIT $from,$number_of_items ";
583
584
            $list = api_get_item_property_by_tool(
585
                'thematic_advance',
586
                api_get_course_id(),
587
                api_get_session_id()
588
            );
589
590
            $elements = [];
591
            foreach ($list as $value) {
592
                $elements[] = $value['ref'];
593
            }
594
595
            $res = Database::query($sql);
596
            $i = 1;
597
            while ($thematic_advance = Database::fetch_row($res)) {
598
                if (in_array($thematic_advance[0], $elements)) {
599
                    $thematic_advance[1] = api_get_local_time($thematic_advance[1]);
600
                    $thematic_advance[1] = api_format_date($thematic_advance[1], DATE_TIME_FORMAT_LONG);
601
                    $actions = '';
602
                    $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_edit&thematic_id='.$thematic_id.'&thematic_advance_id='.$thematic_advance[0].'">'.
603
                        Display::return_icon('edit.png', get_lang('Edit'), '', 22).'</a>';
604
                    $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('Are you sure you want to delete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=thematic_advance_delete&thematic_id='.$thematic_id.'&thematic_advance_id='.$thematic_advance[0].'">'.
605
                        Display::return_icon('delete.png', get_lang('Delete'), '', 22).'</a></center>';
606
                    $data[] = [$i, $thematic_advance[1], $thematic_advance[2], $thematic_advance[3], $actions];
607
                    ++$i;
608
                }
609
            }
610
        }
611
612
        return $data;
613
    }
614
615
    /**
616
     * get thematic advance data by thematic id.
617
     *
618
     * @param int    $thematic_id
619
     * @param string $course_code Course code (optional)
620
     *
621
     * @return array data
622
     */
623
    public function get_thematic_advance_by_thematic_id($thematic_id, $course_code = null)
624
    {
625
        $course_info = api_get_course_info($course_code);
626
        $course_id = $course_info['real_id'];
627
628
        $repo = Container::getThematicAdvanceRepository();
629
630
        $courseEntity = api_get_course_entity($course_id);
631
        $sessionEntity = api_get_session_entity(api_get_session_id());
632
633
        $qb = $repo->getResourcesByCourse($courseEntity, $sessionEntity);
634
635
        $qb->andWhere($qb->expr()->eq('resource.thematic', $thematic_id));
636
637
        return $qb->getQuery()->getResult();
638
639
        // set current course
640
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
0 ignored issues
show
Unused Code introduced by
$table = Database::get_c...TABLE_THEMATIC_ADVANCE) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
641
        $thematic_id = (int) $thematic_id;
642
        $data = [];
643
        $sql = "SELECT * FROM $table
644
                WHERE c_id = $course_id AND thematic_id = $thematic_id ";
645
646
        $elements = [];
647
        $list = api_get_item_property_by_tool(
648
            'thematic_advance',
649
            $course_info['code'],
650
            api_get_session_id()
651
        );
652
        foreach ($list as $value) {
653
            $elements[] = $value['ref'];
654
        }
655
656
        $res = Database::query($sql);
657
        if (Database::num_rows($res) > 0) {
658
            while ($row = Database::fetch_array($res, 'ASSOC')) {
659
                if (in_array($row['id'], $elements)) {
660
                    $data[] = $row;
661
                }
662
            }
663
        }
664
665
        return $data;
666
    }
667
668
    public function getThematicAdvance(
669
        $id
670
    ) {
671
        $repo = Container::getThematicAdvanceRepository();
672
673
        $courseEntity = api_get_course_entity($courseId);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $courseId seems to be never defined.
Loading history...
674
        $sessionEntity = null;
675
        if ($sessionId) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sessionId seems to be never defined.
Loading history...
676
            $sessionEntity = api_get_session_entity($sessionId);
677
            /*$list = api_get_item_property_by_tool(
678
                'thematic_advance',
679
                $course_info['code'],
680
                api_get_session_id()
681
            );
682
            foreach ($list as $value) {
683
                $elements[$value['ref']] = $value;
684
            }*/
685
        }
686
687
        return $repo->find($id);
688
    }
689
690
    /**
691
     * Get thematic advance list.
692
     *
693
     * @param int    $thematic_advance_id Thematic advance id (optional), get data by thematic advance list
694
     * @param string $course_code         Course code (optional)
695
     * @param bool   $force_session_id    Force to have a session id
696
     *
697
     * @return array $data
698
     */
699
    public function get_thematic_advance_list(
700
        $thematic_advance_id = null,
701
        $course_code = null,
702
        $force_session_id = false
703
    ) {
704
        $course_info = api_get_course_info($course_code);
705
        $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
706
        $data = [];
707
        $condition = '';
708
        $thematic_advance_id = (int) $thematic_advance_id;
709
710
        if (!empty($thematic_advance_id)) {
711
            $condition = " AND a.id = $thematic_advance_id ";
712
        }
713
714
        $course_id = $course_info['real_id'];
715
716
        $sql = "SELECT * FROM $tbl_thematic_advance a
717
                WHERE c_id = $course_id $condition
718
                ORDER BY start_date ";
719
720
        $repo = Container::getThematicAdvanceRepository();
721
722
        $courseEntity = api_get_course_entity($course_id);
723
        $sessionEntity = null;
724
        $elements = [];
725
        if ($force_session_id) {
726
            $sessionEntity = api_get_session_entity(api_get_session_id());
727
            /*$list = api_get_item_property_by_tool(
728
                'thematic_advance',
729
                $course_info['code'],
730
                api_get_session_id()
731
            );
732
            foreach ($list as $value) {
733
                $elements[$value['ref']] = $value;
734
            }*/
735
        }
736
737
        $qb = $repo->getResourcesByCourse($courseEntity, $sessionEntity);
738
739
        return $qb->getQuery()->getResult();
740
741
        $res = Database::query($sql);
0 ignored issues
show
Unused Code introduced by
$res = Database::query($sql) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
742
        if (Database::num_rows($res) > 0) {
743
            if (!empty($thematic_advance_id)) {
744
                $data = Database::fetch_array($res);
745
            } else {
746
                // group all data group by thematic id
747
                $tmp = [];
748
                while ($row = Database::fetch_array($res, 'ASSOC')) {
749
                    $tmp[] = $row['thematic_id'];
750
                    if (in_array($row['thematic_id'], $tmp)) {
751
                        if ($force_session_id) {
752
                            if (in_array($row['id'], array_keys($elements))) {
753
                                $row['session_id'] = $elements[$row['id']]['session_id'];
754
                                $data[$row['thematic_id']][$row['id']] = $row;
755
                            }
756
                        } else {
757
                            $data[$row['thematic_id']][$row['id']] = $row;
758
                        }
759
                    }
760
                }
761
            }
762
        }
763
764
        return $data;
765
    }
766
767
    /**
768
     * insert or update a thematic advance.
769
     *
770
     * @todo problem
771
     *
772
     * @return int last thematic advance id
773
     */
774
    public function thematic_advance_save()
775
    {
776
        $_course = api_get_course_info();
777
        // definition database table
778
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
779
780
        // protect data
781
        $id = intval($this->thematic_advance_id);
782
        $thematic_id = intval($this->thematic_id);
783
        $attendance_id = intval($this->attendance_id);
784
        $content = $this->thematic_advance_content;
785
        $start_date = $this->start_date;
786
        $duration = intval($this->duration);
787
        $user_id = api_get_user_id();
788
789
        $repo = Container::getThematicAdvanceRepository();
790
        $em = $repo->getEntityManager();
791
792
        $advance = $repo->find($id);
793
794
        $last_id = null;
795
        if (null === $advance) {
796
            $repoThematic = Container::getThematicRepository();
797
            $thematic = $repoThematic->find($thematic_id);
798
            $attendanceRepo = Container::getAttendanceRepository();
799
            $attendance = $attendanceRepo->find($attendance_id);
800
801
            $advance = new CThematicAdvance();
802
            $advance
803
                ->setCId($this->course_int_id)
804
                ->setContent($content)
805
                ->setThematic($thematic)
806
                ->setAttendance($attendance)
807
                ->setStartDate(api_get_utc_datetime($start_date, true, true))
808
                ->setDuration($duration)
809
            ;
810
811
            $em->persist($advance);
812
813
            $repo->addResourceToCourse(
814
                $advance,
815
                ResourceLink::VISIBILITY_PUBLISHED,
816
                api_get_user_entity(api_get_user_id()),
817
                api_get_course_entity(),
818
                api_get_session_entity(),
819
                api_get_group_entity()
820
            );
821
            $em->flush();
822
823
            $last_id = $advance->getIid();
824
825
            if ($last_id) {
826
                $sql = "UPDATE $table SET id = iid WHERE iid = $last_id";
827
                Database::query($sql);
828
829
                /*api_item_property_update(
830
                    $_course,
831
                    'thematic_advance',
832
                    $last_id,
833
                    'ThematicAdvanceAdded',
834
                    $user_id
835
                );*/
836
            }
837
        } else {
838
            $params = [
839
                'thematic_id' => $thematic_id,
840
                'attendance_id' => $attendance_id,
841
                'content' => $content,
842
                'start_date' => api_get_utc_datetime($start_date),
843
                'duration' => $duration,
844
            ];
845
846
            Database::update(
847
                $table,
848
                $params,
849
                ['id = ? AND c_id = ?' => [$id, $this->course_int_id]]
850
            );
851
852
            api_item_property_update(
853
                $_course,
854
                'thematic_advance',
855
                $id,
856
                'ThematicAdvanceUpdated',
857
                $user_id
858
            );
859
        }
860
861
        return $last_id;
862
    }
863
864
    /**
865
     * delete  thematic advance.
866
     *
867
     * @param int $id Thematic advance id
868
     *
869
     * @return int Affected rows
870
     */
871
    public function thematic_advance_destroy($id)
872
    {
873
        $_course = api_get_course_info();
874
        $course_id = api_get_course_int_id();
875
876
        // definition database table
877
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
878
879
        // protect data
880
        $id = intval($id);
881
        $user_id = api_get_user_id();
882
883
        $sql = "DELETE FROM $table
884
                WHERE c_id = $course_id AND id = $id ";
885
        $result = Database::query($sql);
886
        $affected_rows = Database::affected_rows($result);
887
        if ($affected_rows) {
888
            api_item_property_update(
889
                $_course,
890
                'thematic_advance',
891
                $id,
892
                'ThematicAdvanceDeleted',
893
                $user_id
894
            );
895
        }
896
897
        return $affected_rows;
898
    }
899
900
    /**
901
     * get thematic plan data.
902
     *
903
     * @param int Thematic id (optional), get data by thematic id
904
     * @param int Thematic plan description type (optional), get data by description type
905
     *
906
     * @return array Thematic plan data
907
     */
908
    public function get_thematic_plan_data($thematic_id = null, $description_type = null)
909
    {
910
        // definition database table
911
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
912
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
913
        $course_id = api_get_course_int_id();
914
915
        $repo = Container::getThematicPlanRepository();
916
917
        $courseEntity = api_get_course_entity();
918
        $sessionEntity = api_get_session_entity(api_get_session_id());
919
920
        $qb = $repo->getResourcesByCourse($courseEntity, $sessionEntity);
921
922
        $result = $qb->getQuery()->getResult();
923
        //var_dump(count($result));
924
925
        $data = [];
926
        $condition = '';
927
        //var_dump($thematic_id, $description_type);
928
        if (!empty($thematic_id)) {
929
            $qb->andWhere($qb->expr()->eq('resource.thematic', $thematic_id));
930
931
            //$thematic_id = intval($thematic_id);
932
            //$condition .= " AND thematic_id = $thematic_id ";
933
        }
934
        if (!empty($description_type)) {
935
            $qb->andWhere($qb->expr()->eq('resource.descriptionType', $description_type));
936
            //$condition .= " AND description_type = $description_type ";
937
        }
938
939
        return $qb->getQuery()->getResult();
940
941
        /*$items_from_course = api_get_item_property_by_tool(
942
            'thematic_plan',
943
            api_get_course_id(),
944
            0
945
        );
946
        $items_from_session = api_get_item_property_by_tool(
947
            'thematic_plan',
948
            api_get_course_id(),
949
            api_get_session_id()
950
        );*/
951
952
        $thematic_plan_complete_list = [];
0 ignored issues
show
Unused Code introduced by
$thematic_plan_complete_list = array() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
953
        $thematic_plan_id_list = [];
954
955
        /*if (!empty($items_from_course)) {
956
            foreach ($items_from_course as $item) {
957
                $thematic_plan_id_list[] = $item['ref'];
958
                $thematic_plan_complete_list[$item['ref']] = $item;
959
            }
960
        }
961
962
        if (!empty($items_from_session)) {
963
            foreach ($items_from_session as $item) {
964
                $thematic_plan_id_list[] = $item['ref'];
965
                $thematic_plan_complete_list[$item['ref']] = $item;
966
            }
967
        }*/
968
969
        if (!empty($thematic_plan_id_list)) {
970
            $sql = "SELECT
971
                        tp.id, thematic_id, tp.title, description, description_type, t.session_id
972
                    FROM $tbl_thematic_plan tp
973
                    INNER JOIN $tbl_thematic t
974
                    ON (t.id = tp.thematic_id AND t.c_id = tp.c_id)
975
                    WHERE
976
                        t.c_id = $course_id AND
977
                        tp.c_id = $course_id
978
                        $condition AND
979
                        tp.id IN (".implode(', ', $thematic_plan_id_list).') ';
980
981
            $rs = Database::query($sql);
982
983
            if (Database::num_rows($rs)) {
984
                if (!isset($thematic_id) && !isset($description_type)) {
985
                    // group all data group by thematic id
986
                    $tmp = [];
987
                    while ($row = Database::fetch_array($rs, 'ASSOC')) {
988
                        $tmp[] = $row['thematic_id'];
989
                        if (in_array($row['thematic_id'], $tmp)) {
990
                            $row['session_id'] = $thematic_plan_complete_list[$row['id']];
991
                            $data[$row['thematic_id']][$row['description_type']] = $row;
992
                        }
993
                    }
994
                } else {
995
                    while ($row = Database::fetch_array($rs, 'ASSOC')) {
996
                        $row['session_id'] = $thematic_plan_complete_list[$row['id']];
997
                        $data[] = $row;
998
                    }
999
                }
1000
            }
1001
        }
1002
1003
        return $data;
1004
    }
1005
1006
    /**
1007
     * insert or update a thematic plan.
1008
     *
1009
     * @return int affected rows
1010
     */
1011
    public function thematic_plan_save()
1012
    {
1013
        $_course = api_get_course_info();
1014
        // definition database table
1015
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
1016
1017
        // protect data
1018
        $thematic_id = intval($this->thematic_id);
1019
        $title = $this->thematic_plan_title;
1020
        $description = $this->thematic_plan_description;
1021
        $description_type = intval($this->thematic_plan_description_type);
1022
        $user_id = api_get_user_id();
1023
        $course_id = api_get_course_int_id();
1024
1025
        /*$list = api_get_item_property_by_tool(
1026
            'thematic_plan',
1027
            api_get_course_id(),
1028
            api_get_session_id()
1029
        );
1030
1031
        $elements_to_show = [];
1032
        foreach ($list as $value) {
1033
            $elements_to_show[] = $value['ref'];
1034
        }
1035
        $condition = '';
1036
        if (!empty($elements_to_show)) {
1037
            $condition = 'AND id IN ('.implode(',', $elements_to_show).') ';
1038
        }*/
1039
1040
        $repo = Container::getThematicPlanRepository();
1041
1042
        $criteria = [
1043
            'cId' => $course_id,
1044
            'thematic' => $thematic_id,
1045
            'descriptionType' => $description_type,
1046
        ];
1047
        $em = $repo->getEntityManager();
1048
        /** @var CThematicPlan $plan */
1049
        $plan = $repo->findOneBy($criteria);
1050
1051
        // check thematic plan type already exists
1052
        /*$sql = "SELECT id FROM $tbl_thematic_plan
1053
                WHERE
1054
                    c_id = $course_id AND
1055
                    thematic_id = $thematic_id AND
1056
                    description_type = '$description_type'";
1057
        $rs = Database::query($sql);*/
1058
        if ($plan) {
0 ignored issues
show
introduced by
$plan is of type Chamilo\CourseBundle\Entity\CThematicPlan, thus it always evaluated to true.
Loading history...
1059
            $plan
1060
                ->setTitle($title)
1061
                ->setDescription($description)
1062
            ;
1063
            $em->persist($plan);
1064
            $em->flush();
1065
1066
            // update
1067
            /*$params = [
1068
                'title' => $title,
1069
                'description' => $description,
1070
            ];
1071
            Database::update(
1072
                $tbl_thematic_plan,
1073
                $params,
1074
                ['c_id = ? AND id = ?' => [$course_id, $thematic_plan_id]]
1075
            );
1076
1077
            api_item_property_update(
1078
                $_course,
1079
                'thematic_plan',
1080
                $thematic_plan_id,
1081
                'ThematicPlanUpdated',
1082
                $user_id
1083
            );*/
1084
        } else {
1085
            $thematic = Container::getThematicRepository()->find($thematic_id);
1086
            $plan = new CThematicPlan();
1087
            $plan
1088
                ->setTitle($title)
1089
                ->setDescription($description)
1090
                ->setCId($this->course_int_id)
1091
                ->setThematic($thematic)
1092
                ->setDescriptionType($description_type)
1093
            ;
1094
1095
            $em->persist($plan);
1096
1097
            $repo->addResourceToCourse(
1098
                $plan,
1099
                ResourceLink::VISIBILITY_PUBLISHED,
1100
                api_get_user_entity(api_get_user_id()),
1101
                api_get_course_entity(),
1102
                api_get_session_entity(),
1103
                api_get_group_entity()
1104
            );
1105
1106
            $em->flush();
1107
1108
            if ($plan && $plan->getIid()) {
1109
                $id = $plan->getIid();
1110
                $sql = "UPDATE $tbl_thematic_plan SET id = iid WHERE iid = $id";
1111
                Database::query($sql);
1112
                /*
1113
                api_item_property_update(
1114
                    $_course,
1115
                    'thematic_plan',
1116
                    $last_id,
1117
                    'ThematicPlanAdded',
1118
                    $user_id
1119
                );*/
1120
            }
1121
        }
1122
1123
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type integer.
Loading history...
1124
    }
1125
1126
    /**
1127
     * Delete a thematic plan description.
1128
     *
1129
     * @param int $thematic_id      Thematic id
1130
     * @param int $description_type Description type
1131
     *
1132
     * @return int Affected rows
1133
     */
1134
    public function thematic_plan_destroy($thematic_id, $description_type)
1135
    {
1136
        $_course = api_get_course_info();
1137
        // definition database table
1138
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
1139
1140
        // protect data
1141
        $thematic_id = intval($thematic_id);
1142
        $description_type = intval($description_type);
1143
        $user_id = api_get_user_id();
1144
        $course_info = api_get_course_info();
1145
        $course_id = $course_info['real_id'];
1146
1147
        // get thematic plan id
1148
        $thematic_plan_data = $this->get_thematic_plan_data($thematic_id, $description_type);
1149
        $thematic_plan_id = $thematic_plan_data[0]['id'];
1150
1151
        // delete
1152
        $sql = "DELETE FROM $tbl_thematic_plan
1153
                WHERE
1154
                    c_id = $course_id AND
1155
                    thematic_id = $thematic_id AND
1156
                    description_type = $description_type ";
1157
        $result = Database::query($sql);
1158
        $affected_rows = Database::affected_rows($result);
1159
        if ($affected_rows) {
1160
            api_item_property_update(
1161
                $_course,
1162
                'thematic_plan',
1163
                $thematic_plan_id,
1164
                'ThematicPlanDeleted',
1165
                $user_id
1166
            );
1167
        }
1168
1169
        return $affected_rows;
1170
    }
1171
1172
    /**
1173
     * Get next description type for a new thematic plan description (option 'others').
1174
     *
1175
     * @param int $thematic_id Thematic id
1176
     *
1177
     * @return int New Description type
1178
     */
1179
    public function get_next_description_type($thematic_id)
1180
    {
1181
        // definition database table
1182
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
1183
1184
        // protect data
1185
        $thematic_id = intval($thematic_id);
1186
        $course_id = api_get_course_int_id();
1187
1188
        $sql = "SELECT MAX(description_type) as max
1189
                FROM $tbl_thematic_plan
1190
                WHERE
1191
                    c_id = $course_id AND
1192
                    thematic_id = $thematic_id AND
1193
                    description_type >= ".ADD_THEMATIC_PLAN;
1194
        $rs = Database::query($sql);
1195
        $row = Database::fetch_array($rs);
1196
        $last_description_type = $row['max'];
1197
1198
        if (isset($last_description_type)) {
1199
            $next_description_type = $last_description_type + 1;
1200
        } else {
1201
            $next_description_type = ADD_THEMATIC_PLAN;
1202
        }
1203
1204
        return $next_description_type;
1205
    }
1206
1207
    /**
1208
     * update done thematic advances from thematic details interface.
1209
     *
1210
     * @param int $thematic_advance_id
1211
     *
1212
     * @return int Affected rows
1213
     */
1214
    public function update_done_thematic_advances($thematic_advance_id)
1215
    {
1216
        $_course = api_get_course_info();
1217
        $thematic_data = self::get_thematic_list(null, api_get_course_id());
1218
        $thematic_advance_data = $this->get_thematic_advance_list(
1219
            null,
1220
            api_get_course_id(),
1221
            true
1222
        );
1223
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
1224
1225
        $affected_rows = 0;
1226
        $user_id = api_get_user_id();
1227
1228
        $all = [];
1229
        if (!empty($thematic_data)) {
1230
            foreach ($thematic_data as $thematic) {
1231
                $thematic_id = $thematic['id'];
1232
                if (!empty($thematic_advance_data[$thematic['id']])) {
1233
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1234
                        $all[] = $thematic_advance['id'];
1235
                    }
1236
                }
1237
            }
1238
        }
1239
        $error = null;
1240
        $a_thematic_advance_ids = [];
1241
        $course_id = api_get_course_int_id();
1242
        $sessionId = api_get_session_id();
1243
1244
        if (!empty($thematic_data)) {
1245
            foreach ($thematic_data as $thematic) {
1246
                $my_affected_rows = 0;
1247
                $thematic_id = $thematic['id'];
1248
                if (!empty($thematic_advance_data[$thematic['id']])) {
1249
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1250
                        $item_info = api_get_item_property_info(
1251
                            api_get_course_int_id(),
1252
                            'thematic_advance',
1253
                            $thematic_advance['id'],
1254
                            $sessionId
1255
                        );
1256
1257
                        if ($item_info['session_id'] == $sessionId) {
1258
                            $a_thematic_advance_ids[] = $thematic_advance['id'];
1259
                            // update done thematic for previous advances ((done_advance = 1))
1260
                            $upd = "UPDATE $table SET
1261
                                    done_advance = 1
1262
                                    WHERE c_id = $course_id AND id = ".$thematic_advance['id'].' ';
1263
                            $result = Database::query($upd);
1264
                            $my_affected_rows = Database::affected_rows($result);
1265
                            $affected_rows += $my_affected_rows;
1266
                            //if ($my_affected_rows) {
1267
                            api_item_property_update(
1268
                                $_course,
1269
                                'thematic_advance',
1270
                                $thematic_advance['id'],
1271
                                'ThematicAdvanceDone',
1272
                                $user_id
1273
                            );
1274
                            //}
1275
                            if ($thematic_advance['id'] == $thematic_advance_id) {
1276
                                break 2;
1277
                            }
1278
                        }
1279
                    }
1280
                }
1281
            }
1282
        }
1283
1284
        // Update done thematic for others advances (done_advance = 0)
1285
        if (!empty($a_thematic_advance_ids) && count($a_thematic_advance_ids) > 0) {
1286
            $diff = array_diff($all, $a_thematic_advance_ids);
1287
            if (!empty($diff)) {
1288
                $upd = "UPDATE $table SET done_advance = 0
1289
                        WHERE c_id = $course_id AND id IN(".implode(',', $diff).') ';
1290
                Database::query($upd);
1291
            }
1292
1293
            // update item_property
1294
            $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
1295
            $sql = "SELECT ref FROM $tbl_item_property
1296
                    WHERE
1297
                        c_id = $course_id AND
1298
                        tool='thematic_advance' AND
1299
                        lastedit_type='ThematicAdvanceDone' AND
1300
                        session_id = $sessionId ";
1301
            // get all thematic advance done
1302
            $rs_thematic_done = Database::query($sql);
1303
            if (Database::num_rows($rs_thematic_done) > 0) {
1304
                while ($row_thematic_done = Database::fetch_array($rs_thematic_done)) {
1305
                    $ref = $row_thematic_done['ref'];
1306
                    if (in_array($ref, $a_thematic_advance_ids)) {
1307
                        continue;
1308
                    }
1309
                    // update items
1310
                    $sql = "UPDATE $tbl_item_property SET
1311
                                lastedit_date='".api_get_utc_datetime()."',
1312
                                lastedit_type='ThematicAdvanceUpdated',
1313
                                lastedit_user_id = $user_id
1314
                            WHERE
1315
                                c_id = $course_id AND
1316
                                tool='thematic_advance' AND
1317
                                ref=$ref AND
1318
                                session_id = $sessionId  ";
1319
                    Database::query($sql);
1320
                }
1321
            }
1322
        }
1323
1324
        return $affected_rows;
1325
    }
1326
1327
    /**
1328
     * Get last done thematic advance from thematic details interface.
1329
     *
1330
     * @return int Last done thematic advance id
1331
     */
1332
    public function get_last_done_thematic_advance()
1333
    {
1334
        $thematic_data = self::get_thematic_list();
1335
        $thematic_advance_data = $this->get_thematic_advance_list(
1336
            null,
1337
            api_get_course_id(),
1338
            true
1339
        );
1340
1341
        $a_thematic_advance_ids = [];
1342
        $last_done_advance_id = 0;
1343
        if (!empty($thematic_data)) {
1344
            foreach ($thematic_data as $thematic) {
1345
                if (!empty($thematic_advance_data[$thematic['id']])) {
1346
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1347
                        if (1 == $thematic_advance['done_advance']) {
1348
                            $a_thematic_advance_ids[] = $thematic_advance['id'];
1349
                        }
1350
                    }
1351
                }
1352
            }
1353
        }
1354
        if (!empty($a_thematic_advance_ids)) {
1355
            $last_done_advance_id = array_pop($a_thematic_advance_ids);
1356
            $last_done_advance_id = intval($last_done_advance_id);
1357
        }
1358
1359
        return $last_done_advance_id;
1360
    }
1361
1362
    /**
1363
     * Get next thematic advance not done from thematic details interface.
1364
     *
1365
     * @param   int Offset (if you want to get an item that is not directly the next)
1366
     *
1367
     * @return int next thematic advance not done
1368
     */
1369
    public function get_next_thematic_advance_not_done($offset = 1)
1370
    {
1371
        $thematic_data = self::get_thematic_list();
1372
        $thematic_advance_data = $this->get_thematic_advance_list();
1373
        $a_thematic_advance_ids = [];
1374
        $next_advance_not_done = 0;
1375
        if (!empty($thematic_data)) {
1376
            foreach ($thematic_data as $thematic) {
1377
                if (!empty($thematic_advance_data[$thematic['id']])) {
1378
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1379
                        if (0 == $thematic_advance['done_advance']) {
1380
                            $a_thematic_advance_ids[] = $thematic_advance['id'];
1381
                        }
1382
                    }
1383
                }
1384
            }
1385
        }
1386
1387
        if (!empty($a_thematic_advance_ids)) {
1388
            for ($i = 0; $i < $offset; ++$i) {
1389
                $next_advance_not_done = array_shift($a_thematic_advance_ids);
1390
            }
1391
            $next_advance_not_done = intval($next_advance_not_done);
1392
        }
1393
1394
        return $next_advance_not_done;
1395
    }
1396
1397
    /**
1398
     * Get total average of thematic advances.
1399
     *
1400
     * @param string $course_code (optional)
1401
     * @param int    $session_id  (optional)
1402
     *
1403
     * @return float Average of thematic advances
1404
     */
1405
    public function get_total_average_of_thematic_advances($course_code = null, $session_id = null)
0 ignored issues
show
Unused Code introduced by
The parameter $session_id is not used and could be removed. ( Ignorable by Annotation )

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

1405
    public function get_total_average_of_thematic_advances($course_code = null, /** @scrutinizer ignore-unused */ $session_id = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1406
    {
1407
        if (empty($course_code)) {
1408
            $course_code = api_get_course_id();
1409
        }
1410
        if (api_get_session_id()) {
1411
            $thematic_data = self::get_thematic_list(null, $course_code);
1412
        } else {
1413
            $thematic_data = self::get_thematic_list(null, $course_code, 0);
1414
        }
1415
        $new_thematic_data = [];
1416
        if (!empty($thematic_data)) {
1417
            foreach ($thematic_data as $item) {
1418
                $new_thematic_data[] = $item;
1419
            }
1420
            $thematic_data = $new_thematic_data;
1421
        }
1422
1423
        $a_average_of_advances_by_thematic = [];
1424
        $total_average = 0;
1425
        if (!empty($thematic_data)) {
1426
            foreach ($thematic_data as $thematic) {
1427
                $thematic_id = $thematic['id'];
1428
                $a_average_of_advances_by_thematic[$thematic_id] = $this->get_average_of_advances_by_thematic(
1429
                    $thematic_id,
1430
                    $course_code
1431
                );
1432
            }
1433
        }
1434
1435
        // calculate total average
1436
        if (!empty($a_average_of_advances_by_thematic)) {
1437
            $count_tematics = count($thematic_data);
1438
            $score = array_sum($a_average_of_advances_by_thematic);
1439
            $total_average = round(($score * 100) / ($count_tematics * 100));
1440
        }
1441
1442
        return $total_average;
1443
    }
1444
1445
    /**
1446
     * Get average of advances by thematic.
1447
     *
1448
     * @param int Thematic id
1449
     * @param string $course_code
1450
     *
1451
     * @return float Average of thematic advances
1452
     */
1453
    public function get_average_of_advances_by_thematic($thematic_id, $course_code = null)
1454
    {
1455
        $thematic_advance_data = $this->get_thematic_advance_by_thematic_id($thematic_id, $course_code);
1456
        $average = 0;
1457
        if (!empty($thematic_advance_data)) {
1458
            // get all done advances by thematic
1459
            $advances = [];
1460
            $count_done_advances = 0;
1461
            /** @var CThematicAdvance $thematic_advance */
1462
            foreach ($thematic_advance_data as $thematic_advance) {
1463
1464
                if (1 == $thematic_advance->getDoneAdvance()) {
1465
                    $count_done_advances++;
1466
                }
1467
                $advances[] = $thematic_advance->getDoneAdvance();
1468
            }
1469
            // calculate average by thematic
1470
            $count_total_advances = count($advances);
1471
            $average = round(($count_done_advances * 100) / $count_total_advances);
1472
        }
1473
1474
        return $average;
1475
    }
1476
1477
    /**
1478
     * set attributes for fields of thematic table.
1479
     *
1480
     * @param    int        Thematic id
1481
     * @param    string    Thematic title
1482
     * @param    string    Thematic content
1483
     * @param    int        Session id
1484
     */
1485
    public function set_thematic_attributes($id = null, $title = '', $content = '', $session_id = 0)
1486
    {
1487
        $this->thematic_id = $id;
1488
        $this->thematic_title = $title;
1489
        $this->thematic_content = $content;
1490
        $this->session_id = $session_id;
1491
    }
1492
1493
    /**
1494
     * set attributes for fields of thematic_plan table.
1495
     *
1496
     * @param    int        Thematic id
1497
     * @param    string    Thematic plan title
1498
     * @param    string    Thematic plan description
1499
     * @param    int        Thematic plan description type
1500
     */
1501
    public function set_thematic_plan_attributes(
1502
        $thematic_id = 0,
1503
        $title = '',
1504
        $description = '',
1505
        $description_type = 0
1506
    ) {
1507
        $this->thematic_id = $thematic_id;
1508
        $this->thematic_plan_title = $title;
1509
        $this->thematic_plan_description = $description;
1510
        $this->thematic_plan_description_type = $description_type;
1511
    }
1512
1513
    /**
1514
     * set attributes for fields of thematic_advance table.
1515
     *
1516
     * @param int $id Thematic advance id
1517
     * @param    int        Thematic id
1518
     * @param    int        Attendance id
1519
     * @param    string    Content
1520
     * @param    string    Date and time
1521
     * @param    int        Duration in hours
1522
     */
1523
    public function set_thematic_advance_attributes(
1524
        $id = null,
1525
        $thematic_id = 0,
1526
        $attendance_id = 0,
1527
        $content = '',
1528
        $start_date = null,
1529
        $duration = 0
1530
    ) {
1531
        $this->thematic_advance_id = $id;
1532
        $this->thematic_id = $thematic_id;
1533
        $this->attendance_id = $attendance_id;
1534
        $this->thematic_advance_content = $content;
1535
        $this->start_date = $start_date;
1536
        $this->duration = $duration;
1537
    }
1538
1539
    /**
1540
     * set thematic id.
1541
     *
1542
     * @param    int     Thematic id
1543
     */
1544
    public function set_thematic_id($thematic_id)
1545
    {
1546
        $this->thematic_id = $thematic_id;
1547
    }
1548
1549
    /**
1550
     * get thematic id.
1551
     *
1552
     * @return int
1553
     */
1554
    public function get_thematic_id()
1555
    {
1556
        return $this->thematic_id;
1557
    }
1558
1559
    /**
1560
     * Get thematic plan titles by default.
1561
     *
1562
     * @return array
1563
     */
1564
    public function get_default_thematic_plan_title()
1565
    {
1566
        $default_thematic_plan_titles = [];
1567
        $default_thematic_plan_titles[1] = get_lang('Objectives');
1568
        $default_thematic_plan_titles[2] = get_lang('Skills to acquire');
1569
        $default_thematic_plan_titles[3] = get_lang('Methodology');
1570
        $default_thematic_plan_titles[4] = get_lang('Infrastructure');
1571
        $default_thematic_plan_titles[5] = get_lang('Assessment');
1572
        $default_thematic_plan_titles[6] = get_lang('Others');
1573
1574
        return $default_thematic_plan_titles;
1575
    }
1576
1577
    /**
1578
     * Get thematic plan icons by default.
1579
     *
1580
     * @return array
1581
     */
1582
    public function get_default_thematic_plan_icon()
1583
    {
1584
        $default_thematic_plan_icon = [];
1585
        $default_thematic_plan_icon[1] = 'icons/32/objective.png';
1586
        $default_thematic_plan_icon[2] = 'icons/32/skills.png';
1587
        $default_thematic_plan_icon[3] = 'icons/32/strategy.png';
1588
        $default_thematic_plan_icon[4] = 'icons/32/laptop.png';
1589
        $default_thematic_plan_icon[5] = 'icons/32/assessment.png';
1590
        $default_thematic_plan_icon[6] = 'icons/32/wizard.png';
1591
1592
        return $default_thematic_plan_icon;
1593
    }
1594
1595
    /**
1596
     * Get questions by default for help.
1597
     *
1598
     * @return array
1599
     */
1600
    public function get_default_question()
1601
    {
1602
        $question = [];
1603
        $question[1] = get_lang('What should the end results be when the learner has completed the course? What are the activities performed during the course?');
1604
        $question[2] = get_lang('Skills to acquireQuestions');
1605
        $question[3] = get_lang('What methods and activities help achieve the objectives of the course?  What would the schedule be?');
1606
        $question[4] = get_lang('What infrastructure is necessary to achieve the goals of this topic normally?');
1607
        $question[5] = get_lang('How will learners be assessed? Are there strategies to develop in order to master the topic?');
1608
1609
        return $question;
1610
    }
1611
}
1612