Completed
Push — master ( 784f70...96b7ea )
by Julito
10:23
created

Thematic::get_thematic_advance_data()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 28
c 0
b 0
f 0
nc 4
nop 5
dl 0
loc 48
rs 9.472
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 string $course_code
274
     * @param int    $session_id
275
     *
276
     * @return array Thematic data
277
     */
278
    public static function get_thematic_list($course_code = null, $session_id = null)
279
    {
280
        // set current course and session
281
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
282
        $course_info = api_get_course_info($course_code);
283
        $course_id = $course_info['real_id'];
284
285
        if (!empty($session_id)) {
286
            $session_id = (int) $session_id;
287
        } else {
288
            $session_id = api_get_session_id();
289
        }
290
291
        $data = [];
292
        if (empty($session_id)) {
293
            $condition_session = api_get_session_condition(0);
294
        } else {
295
            $condition_session = api_get_session_condition($session_id, true, true);
296
        }
297
        $condition = " WHERE active = 1 $condition_session ";
298
299
        $sql = "SELECT *
300
                FROM $tbl_thematic $condition AND c_id = $course_id
301
                ORDER BY display_order ";
302
303
        $res = Database::query($sql);
304
        if (Database::num_rows($res) > 0) {
305
            $repo = Container::getThematicRepository();
306
            while ($row = Database::fetch_array($res, 'ASSOC')) {
307
                $entity = $repo->find($row['iid']);
308
                $data[$row['iid']] = $entity;
309
            }
310
        }
311
312
        return $data;
313
    }
314
315
    /**
316
     * Insert or update a thematic.
317
     *
318
     * @return CThematic
319
     */
320
    public function thematic_save()
321
    {
322
        // definition database table
323
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
324
325
        // protect data
326
        $id = intval($this->thematic_id);
327
        $title = $this->thematic_title;
328
        $content = $this->thematic_content;
329
        $session_id = intval($this->session_id);
330
331
        // get the maximum display order of all the glossary items
332
        $max_thematic_item = $this->get_max_thematic_item(false);
333
334
        $repo = Container::getThematicRepository();
335
        $em = $repo->getEntityManager();
336
337
        if (empty($id)) {
338
            $thematic = new CThematic();
339
            $thematic
340
                ->setTitle($title)
341
                ->setContent($content)
342
                ->setActive(1)
343
                ->setCId($this->course_int_id)
344
                ->setDisplayOrder($max_thematic_item + 1)
345
                ->setSessionId($session_id)
346
            ;
347
348
            $em->persist($thematic);
349
350
            $repo->addResourceToCourse(
351
                $thematic,
352
                ResourceLink::VISIBILITY_PUBLISHED,
353
                api_get_user_entity(api_get_user_id()),
354
                api_get_course_entity(),
355
                api_get_session_entity(),
356
                api_get_group_entity()
357
            );
358
359
            $em->flush();
360
361
            // insert
362
            /*$params = [
363
                'c_id' => $this->course_int_id,
364
                'active' => 1,
365
                'display_order' => intval($max_thematic_item) + 1,
366
                'session_id' => $session_id,
367
            ];*/
368
            $last_id = $thematic->getIid();
369
            if ($last_id) {
370
                $sql = "UPDATE $tbl_thematic SET id = iid WHERE iid = $last_id";
371
                Database::query($sql);
372
                /*api_item_property_update(
373
                    $_course,
374
                    'thematic',
375
                    $last_id,
376
                    'ThematicAdded',
377
                    $user_id
378
                );*/
379
            }
380
        } else {
381
            $thematic = $repo->find($id);
382
            if ($thematic) {
383
                $thematic
384
                    ->setTitle($title)
385
                    ->setContent($content)
386
                ;
387
388
                $em->persist($thematic);
389
                $em->flush();
390
            }
391
392
            // Update
393
            /*$params = [
394
                'title' => $title,
395
                'content' => $content,
396
                'session_id' => $session_id,
397
            ];
398
399
            Database::update(
400
                $tbl_thematic,
401
                $params,
402
                ['id  = ? AND c_id = ?' => [$id, $this->course_int_id]]
403
            );
404
405
            $last_id = $id;
406
407
            // save inside item property table
408
            api_item_property_update(
409
                $_course,
410
                'thematic',
411
                $last_id,
412
                'ThematicUpdated',
413
                $user_id
414
            );*/
415
        }
416
417
        return $thematic;
418
    }
419
420
    /**
421
     * Delete logically (set active field to 0) a thematic.
422
     *
423
     * @param int|array One or many thematic ids
424
     *
425
     * @return int Affected rows
426
     */
427
    public function delete($thematic_id)
428
    {
429
        $_course = api_get_course_info();
430
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
431
        $affected_rows = 0;
432
        $user_id = api_get_user_id();
433
        $course_id = api_get_course_int_id();
434
435
        if (is_array($thematic_id)) {
436
            foreach ($thematic_id as $id) {
437
                $id = intval($id);
438
                $sql = "UPDATE $tbl_thematic SET active = 0
439
                        WHERE c_id = $course_id AND id = $id";
440
                $result = Database::query($sql);
441
                $affected_rows += Database::affected_rows($result);
442
                if (!empty($affected_rows)) {
443
                    // update row item property table
444
                    /*api_item_property_update(
445
                        $_course,
446
                        'thematic',
447
                        $id,
448
                        'ThematicDeleted',
449
                        $user_id
450
                    );*/
451
                }
452
            }
453
        } else {
454
            $thematic_id = intval($thematic_id);
455
            $sql = "UPDATE $tbl_thematic SET active = 0
456
                    WHERE c_id = $course_id AND id = $thematic_id";
457
            $result = Database::query($sql);
458
            $affected_rows = Database::affected_rows($result);
459
            if (!empty($affected_rows)) {
460
                // update row item property table
461
                /*api_item_property_update(
462
                    $_course,
463
                    'thematic',
464
                    $thematic_id,
465
                    'ThematicDeleted',
466
                    $user_id
467
                );*/
468
            }
469
        }
470
471
        return $affected_rows;
472
    }
473
474
    /**
475
     * @param int $thematicId
476
     */
477
    public function copy($thematicId)
478
    {
479
        $repo = Container::getThematicRepository();
480
        /** @var CThematic $thematic */
481
        $thematic = $repo->find($thematicId);
482
        if (null === $thematic) {
483
            return false;
484
        }
485
486
        $thematicManager = new Thematic();
487
        $thematicManager->set_thematic_attributes(
488
            '',
489
            $thematic->getTitle().' - '.get_lang('Copy'),
490
            $thematic->getContent(),
491
            api_get_session_id()
492
        );
493
        $new_thematic_id = $thematicManager->thematic_save();
494
495
        if (!empty($new_thematic_id)) {
496
            $thematic_advanced = $thematic->getAdvances();
497
            if (!empty($thematic_advanced)) {
498
                foreach ($thematic_advanced as $item) {
499
                    $thematic = new Thematic();
500
                    $thematic->set_thematic_advance_attributes(
501
                        0,
502
                        $new_thematic_id,
503
                        0,
504
                        $item->getContent(),
505
                        $item->getStartDate()->format('Y-m-d H:i:s'),
506
                        $item->getDuration()
507
                    );
508
                    $thematic->thematic_advance_save();
509
                }
510
            }
511
            $thematic_plan = $thematic->getPlans();
0 ignored issues
show
Bug introduced by
The method getPlans() does not exist on Thematic. ( Ignorable by Annotation )

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

511
            /** @scrutinizer ignore-call */ 
512
            $thematic_plan = $thematic->getPlans();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
512
            if (!empty($thematic_plan)) {
513
                foreach ($thematic_plan as $item) {
514
                    $thematic = new Thematic();
515
                    $thematic->set_thematic_plan_attributes(
516
                        $new_thematic_id,
517
                        $item->getTitle(),
518
                        $item->getDescription(),
519
                        $item->getDescriptionType()
520
                    );
521
                    $thematic->thematic_plan_save();
522
                }
523
            }
524
        }
525
    }
526
527
    /**
528
     * Get the total number of thematic advance inside current course.
529
     *
530
     * @see SortableTable#get_total_number_of_items()
531
     */
532
    public static function get_number_of_thematic_advances()
533
    {
534
        global $thematic_id;
535
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
536
        $course_id = api_get_course_int_id();
537
        $thematic_id = (int) $thematic_id;
538
539
        $sql = "SELECT COUNT(id) AS total_number_of_items
540
                FROM $table
541
                WHERE c_id = $course_id AND thematic_id = $thematic_id ";
542
        $res = Database::query($sql);
543
        $obj = Database::fetch_object($res);
544
545
        return $obj->total_number_of_items;
546
    }
547
548
    /**
549
     * Get the thematic advances to display on the current page (fill the sortable-table).
550
     *
551
     * @param   int     offset of first user to recover
552
     * @param   int     Number of users to get
553
     * @param   int     Column to sort on
554
     * @param   string  Order (ASC,DESC)
555
     *
556
     * @return array
557
     *
558
     * @see SortableTable#get_table_data($from)
559
     */
560
    public static function get_thematic_advance_data($from, $number_of_items, $column, $direction, $params = [])
561
    {
562
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
563
        $column = (int) $column;
564
        $from = (int) $from;
565
        $number_of_items = (int) $number_of_items;
566
        if (!in_array($direction, ['ASC', 'DESC'])) {
567
            $direction = 'ASC';
568
        }
569
        $data = [];
570
        $course_id = api_get_course_int_id();
571
        $thematic_id = (int) $params['thematic_id'];
572
        if (api_is_allowed_to_edit(null, true)) {
573
            $sql = "SELECT id AS col0, start_date AS col1, duration AS col2, content AS col3
574
                    FROM $table
575
                    WHERE c_id = $course_id AND thematic_id = $thematic_id
576
                    ORDER BY col$column $direction
577
                    LIMIT $from,$number_of_items ";
578
579
            /*$list = api_get_item_property_by_tool(
580
                'thematic_advance',
581
                api_get_course_id(),
582
                api_get_session_id()
583
            );*/
584
585
            /*$elements = [];
586
            foreach ($list as $value) {
587
                $elements[] = $value['ref'];
588
            }*/
589
590
            $res = Database::query($sql);
591
            $i = 1;
592
            while ($thematic_advance = Database::fetch_row($res)) {
593
                //if (in_array($thematic_advance[0], $elements)) {
594
                    $thematic_advance[1] = api_get_local_time($thematic_advance[1]);
595
                    $thematic_advance[1] = api_format_date($thematic_advance[1], DATE_TIME_FORMAT_LONG);
596
                    $actions = '';
597
                    $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_edit&thematic_id='.$thematic_id.'&thematic_advance_id='.$thematic_advance[0].'">'.
598
                        Display::return_icon('edit.png', get_lang('Edit'), '', 22).'</a>';
599
                    $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].'">'.
600
                        Display::return_icon('delete.png', get_lang('Delete'), '', 22).'</a></center>';
601
                    $data[] = [$i, $thematic_advance[1], $thematic_advance[2], $thematic_advance[3], $actions];
602
                    ++$i;
603
               // }
604
            }
605
        }
606
607
        return $data;
608
    }
609
610
    /**
611
     * get thematic advance data by thematic id.
612
     *
613
     * @param int    $thematic_id
614
     * @param string $course_code Course code (optional)
615
     *
616
     * @return array data
617
     */
618
    public function get_thematic_advance_by_thematic_id($thematic_id, $course_code = null)
619
    {
620
        $course_info = api_get_course_info($course_code);
621
        $course_id = $course_info['real_id'];
622
623
        $repo = Container::getThematicAdvanceRepository();
624
625
        $courseEntity = api_get_course_entity($course_id);
626
        $sessionEntity = api_get_session_entity(api_get_session_id());
627
628
        $qb = $repo->getResourcesByCourse($courseEntity, $sessionEntity);
629
630
        $qb->andWhere($qb->expr()->eq('resource.thematic', $thematic_id));
631
632
        return $qb->getQuery()->getResult();
633
634
        // set current course
635
        $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...
636
        $thematic_id = (int) $thematic_id;
637
        $data = [];
638
        $sql = "SELECT * FROM $table
639
                WHERE c_id = $course_id AND thematic_id = $thematic_id ";
640
641
        $elements = [];
642
        $list = api_get_item_property_by_tool(
643
            'thematic_advance',
644
            $course_info['code'],
645
            api_get_session_id()
646
        );
647
        foreach ($list as $value) {
648
            $elements[] = $value['ref'];
649
        }
650
651
        $res = Database::query($sql);
652
        if (Database::num_rows($res) > 0) {
653
            while ($row = Database::fetch_array($res, 'ASSOC')) {
654
                if (in_array($row['id'], $elements)) {
655
                    $data[] = $row;
656
                }
657
            }
658
        }
659
660
        return $data;
661
    }
662
663
    public function getThematicAdvance($id)
664
    {
665
        $repo = Container::getThematicAdvanceRepository();
666
667
        $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...
668
        $sessionEntity = null;
669
        if ($sessionId) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sessionId seems to be never defined.
Loading history...
670
            $sessionEntity = api_get_session_entity($sessionId);
671
            /*$list = api_get_item_property_by_tool(
672
                'thematic_advance',
673
                $course_info['code'],
674
                api_get_session_id()
675
            );
676
            foreach ($list as $value) {
677
                $elements[$value['ref']] = $value;
678
            }*/
679
        }
680
681
        return $repo->find($id);
682
    }
683
684
    /**
685
     * Get thematic advance list.
686
     *
687
     * @param int    $thematic_advance_id Thematic advance id (optional), get data by thematic advance list
688
     * @param string $course_code         Course code (optional)
689
     * @param bool   $force_session_id    Force to have a session id
690
     *
691
     * @return array $data
692
     */
693
    public function get_thematic_advance_list(
694
        $thematic_advance_id = null,
695
        $course_code = null,
696
        $force_session_id = false
697
    ) {
698
        $course_info = api_get_course_info($course_code);
699
        $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
700
        $data = [];
701
        $condition = '';
702
        $thematic_advance_id = (int) $thematic_advance_id;
703
704
        if (!empty($thematic_advance_id)) {
705
            $condition = " AND a.id = $thematic_advance_id ";
706
        }
707
708
        $course_id = $course_info['real_id'];
709
710
        $sql = "SELECT * FROM $tbl_thematic_advance a
711
                WHERE c_id = $course_id $condition
712
                ORDER BY start_date ";
713
714
        $repo = Container::getThematicAdvanceRepository();
715
716
        $courseEntity = api_get_course_entity($course_id);
717
        $sessionEntity = null;
718
        $elements = [];
719
        if ($force_session_id) {
720
            $sessionEntity = api_get_session_entity(api_get_session_id());
721
            /*$list = api_get_item_property_by_tool(
722
                'thematic_advance',
723
                $course_info['code'],
724
                api_get_session_id()
725
            );
726
            foreach ($list as $value) {
727
                $elements[$value['ref']] = $value;
728
            }*/
729
        }
730
731
        $qb = $repo->getResourcesByCourse($courseEntity, $sessionEntity);
732
        $qb->orderBy('resource.startDate', 'DESC');
733
734
        return $qb->getQuery()->getResult();
735
736
        $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...
737
        if (Database::num_rows($res) > 0) {
738
            if (!empty($thematic_advance_id)) {
739
                $data = Database::fetch_array($res);
740
            } else {
741
                // group all data group by thematic id
742
                $tmp = [];
743
                while ($row = Database::fetch_array($res, 'ASSOC')) {
744
                    $tmp[] = $row['thematic_id'];
745
                    if (in_array($row['thematic_id'], $tmp)) {
746
                        if ($force_session_id) {
747
                            if (in_array($row['id'], array_keys($elements))) {
748
                                $row['session_id'] = $elements[$row['id']]['session_id'];
749
                                $data[$row['thematic_id']][$row['id']] = $row;
750
                            }
751
                        } else {
752
                            $data[$row['thematic_id']][$row['id']] = $row;
753
                        }
754
                    }
755
                }
756
            }
757
        }
758
759
        return $data;
760
    }
761
762
    /**
763
     * insert or update a thematic advance.
764
     *
765
     * @todo problem
766
     *
767
     * @return int last thematic advance id
768
     */
769
    public function thematic_advance_save()
770
    {
771
        $_course = api_get_course_info();
772
        // definition database table
773
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
774
775
        // protect data
776
        $id = intval($this->thematic_advance_id);
777
        $thematic_id = intval($this->thematic_id);
778
        $attendance_id = intval($this->attendance_id);
779
        $content = $this->thematic_advance_content;
780
        $start_date = $this->start_date;
781
        $duration = intval($this->duration);
782
        $user_id = api_get_user_id();
783
784
        $repo = Container::getThematicAdvanceRepository();
785
        $em = $repo->getEntityManager();
786
787
        /** @var CThematicAdvance $advance */
788
        $advance = $repo->find($id);
789
790
        $repoThematic = Container::getThematicRepository();
791
        $thematic = $repoThematic->find($thematic_id);
792
        $attendanceRepo = Container::getAttendanceRepository();
793
        $attendance = $attendanceRepo->find($attendance_id);
794
795
        $last_id = null;
796
        if (null === $advance) {
797
            $advance = new CThematicAdvance();
798
            $advance
799
                ->setCId($this->course_int_id)
800
                ->setContent($content)
801
                //->setThematic($thematic)
802
                //->setAttendance($attendance)
803
                ->setStartDate(api_get_utc_datetime($start_date, true, true))
804
                ->setDuration($duration)
805
            ;
806
807
            if ($thematic) {
808
                $advance                    ->setThematic($thematic);
809
            }
810
811
            if ($attendance) {
0 ignored issues
show
introduced by
$attendance is of type Chamilo\CoreBundle\Entit...ource\ResourceInterface, thus it always evaluated to true.
Loading history...
812
                $advance                    ->setAttendance($attendance);
813
            }
814
815
816
817
            $em->persist($advance);
818
819
            $repo->addResourceToCourse(
820
                $advance,
821
                ResourceLink::VISIBILITY_PUBLISHED,
822
                api_get_user_entity(api_get_user_id()),
823
                api_get_course_entity(),
824
                api_get_session_entity(),
825
                api_get_group_entity()
826
            );
827
            $em->flush();
828
829
            $last_id = $advance->getIid();
830
831
            if ($last_id) {
832
                $sql = "UPDATE $table SET id = iid WHERE iid = $last_id";
833
                Database::query($sql);
834
835
                /*api_item_property_update(
836
                    $_course,
837
                    'thematic_advance',
838
                    $last_id,
839
                    'ThematicAdvanceAdded',
840
                    $user_id
841
                );*/
842
            }
843
        } else {
844
            $advance
845
                ->setCId($this->course_int_id)
846
                ->setContent($content)
847
                ->setStartDate(api_get_utc_datetime($start_date, true, true))
848
                ->setDuration($duration)
849
            ;
850
851
            if ($thematic) {
852
                $advance                    ->setThematic($thematic);
853
            }
854
855
            if ($attendance) {
0 ignored issues
show
introduced by
$attendance is of type Chamilo\CoreBundle\Entit...ource\ResourceInterface, thus it always evaluated to true.
Loading history...
856
                $advance                    ->setAttendance($attendance);
857
            }
858
            $em->persist($advance);
859
            $em->flush();
860
            /*
861
            Database::update(
862
                $table,
863
                $params,
864
                ['id = ? AND c_id = ?' => [$id, $this->course_int_id]]
865
            );
866
867
            api_item_property_update(
868
                $_course,
869
                'thematic_advance',
870
                $id,
871
                'ThematicAdvanceUpdated',
872
                $user_id
873
            );*/
874
        }
875
876
        return $last_id;
877
    }
878
879
    /**
880
     * delete  thematic advance.
881
     *
882
     * @param int $id Thematic advance id
883
     *
884
     * @return int Affected rows
885
     */
886
    public function thematic_advance_destroy($id)
887
    {
888
        $repo = Container::getThematicAdvanceRepository();
889
        $advance = $repo->find($id);
890
891
        if ($advance) {
892
            $repo->getEntityManager()->remove($advance);
893
            $repo->getEntityManager()->flush();
894
        }
895
896
        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...
897
898
899
        $_course = api_get_course_info();
0 ignored issues
show
Unused Code introduced by
$_course = api_get_course_info() 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...
900
        $course_id = api_get_course_int_id();
901
902
        // definition database table
903
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
904
905
        // protect data
906
        $id = intval($id);
907
        $user_id = api_get_user_id();
908
909
        $sql = "DELETE FROM $table
910
                WHERE c_id = $course_id AND id = $id ";
911
        $result = Database::query($sql);
912
        $affected_rows = Database::affected_rows($result);
913
914
        if ($affected_rows) {
915
            api_item_property_update(
916
                $_course,
917
                'thematic_advance',
918
                $id,
919
                'ThematicAdvanceDeleted',
920
                $user_id
921
            );
922
        }
923
924
        return $affected_rows;
925
    }
926
927
    /**
928
     * get thematic plan data.
929
     *
930
     * @param int Thematic id (optional), get data by thematic id
931
     * @param int Thematic plan description type (optional), get data by description type
932
     *
933
     * @deprecated
934
     *
935
     * @return array Thematic plan data
936
     */
937
    public function get_thematic_plan_data($thematic_id = null, $description_type = null)
938
    {
939
        // definition database table
940
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
941
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
942
        $course_id = api_get_course_int_id();
943
944
        $repo = Container::getThematicPlanRepository();
945
946
        $courseEntity = api_get_course_entity();
947
        $sessionEntity = api_get_session_entity(api_get_session_id());
948
949
        $qb = $repo->getResourcesByCourse($courseEntity, $sessionEntity);
950
951
        $result = $qb->getQuery()->getResult();
952
        //var_dump(count($result));
953
954
        $data = [];
955
        $condition = '';
956
        //var_dump($thematic_id, $description_type);
957
        if (!empty($thematic_id)) {
958
            $qb->andWhere($qb->expr()->eq('resource.thematic', $thematic_id));
959
960
            //$thematic_id = intval($thematic_id);
961
            //$condition .= " AND thematic_id = $thematic_id ";
962
        }
963
        if (!empty($description_type)) {
964
            $qb->andWhere($qb->expr()->eq('resource.descriptionType', $description_type));
965
            //$condition .= " AND description_type = $description_type ";
966
        }
967
968
        return $qb->getQuery()->getResult();
969
970
        /*$items_from_course = api_get_item_property_by_tool(
971
            'thematic_plan',
972
            api_get_course_id(),
973
            0
974
        );
975
        $items_from_session = api_get_item_property_by_tool(
976
            'thematic_plan',
977
            api_get_course_id(),
978
            api_get_session_id()
979
        );*/
980
981
        $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...
982
        $thematic_plan_id_list = [];
983
984
        /*if (!empty($items_from_course)) {
985
            foreach ($items_from_course as $item) {
986
                $thematic_plan_id_list[] = $item['ref'];
987
                $thematic_plan_complete_list[$item['ref']] = $item;
988
            }
989
        }
990
991
        if (!empty($items_from_session)) {
992
            foreach ($items_from_session as $item) {
993
                $thematic_plan_id_list[] = $item['ref'];
994
                $thematic_plan_complete_list[$item['ref']] = $item;
995
            }
996
        }*/
997
998
        if (!empty($thematic_plan_id_list)) {
999
            $sql = "SELECT
1000
                        tp.id, thematic_id, tp.title, description, description_type, t.session_id
1001
                    FROM $tbl_thematic_plan tp
1002
                    INNER JOIN $tbl_thematic t
1003
                    ON (t.id = tp.thematic_id AND t.c_id = tp.c_id)
1004
                    WHERE
1005
                        t.c_id = $course_id AND
1006
                        tp.c_id = $course_id
1007
                        $condition AND
1008
                        tp.id IN (".implode(', ', $thematic_plan_id_list).') ';
1009
1010
            $rs = Database::query($sql);
1011
1012
            if (Database::num_rows($rs)) {
1013
                if (!isset($thematic_id) && !isset($description_type)) {
1014
                    // group all data group by thematic id
1015
                    $tmp = [];
1016
                    while ($row = Database::fetch_array($rs, 'ASSOC')) {
1017
                        $tmp[] = $row['thematic_id'];
1018
                        if (in_array($row['thematic_id'], $tmp)) {
1019
                            $row['session_id'] = $thematic_plan_complete_list[$row['id']];
1020
                            $data[$row['thematic_id']][$row['description_type']] = $row;
1021
                        }
1022
                    }
1023
                } else {
1024
                    while ($row = Database::fetch_array($rs, 'ASSOC')) {
1025
                        $row['session_id'] = $thematic_plan_complete_list[$row['id']];
1026
                        $data[] = $row;
1027
                    }
1028
                }
1029
            }
1030
        }
1031
1032
        return $data;
1033
    }
1034
1035
    /**
1036
     * insert or update a thematic plan.
1037
     *
1038
     * @return int affected rows
1039
     */
1040
    public function thematic_plan_save()
1041
    {
1042
        $_course = api_get_course_info();
1043
        // definition database table
1044
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
1045
1046
        // protect data
1047
        $thematic_id = intval($this->thematic_id);
1048
        $title = $this->thematic_plan_title;
1049
        $description = $this->thematic_plan_description;
1050
        $description_type = intval($this->thematic_plan_description_type);
1051
        $user_id = api_get_user_id();
1052
        $course_id = api_get_course_int_id();
1053
1054
        /*$list = api_get_item_property_by_tool(
1055
            'thematic_plan',
1056
            api_get_course_id(),
1057
            api_get_session_id()
1058
        );
1059
1060
        $elements_to_show = [];
1061
        foreach ($list as $value) {
1062
            $elements_to_show[] = $value['ref'];
1063
        }
1064
        $condition = '';
1065
        if (!empty($elements_to_show)) {
1066
            $condition = 'AND id IN ('.implode(',', $elements_to_show).') ';
1067
        }*/
1068
1069
        $repo = Container::getThematicPlanRepository();
1070
1071
        $criteria = [
1072
            'cId' => $course_id,
1073
            'thematic' => $thematic_id,
1074
            'descriptionType' => $description_type,
1075
        ];
1076
        $em = $repo->getEntityManager();
1077
        /** @var CThematicPlan $plan */
1078
        $plan = $repo->findOneBy($criteria);
1079
1080
        // check thematic plan type already exists
1081
        /*$sql = "SELECT id FROM $tbl_thematic_plan
1082
                WHERE
1083
                    c_id = $course_id AND
1084
                    thematic_id = $thematic_id AND
1085
                    description_type = '$description_type'";
1086
        $rs = Database::query($sql);*/
1087
        if ($plan) {
0 ignored issues
show
introduced by
$plan is of type Chamilo\CourseBundle\Entity\CThematicPlan, thus it always evaluated to true.
Loading history...
1088
            $plan
1089
                ->setTitle($title)
1090
                ->setDescription($description)
1091
            ;
1092
            $em->persist($plan);
1093
            $em->flush();
1094
1095
            // update
1096
            /*$params = [
1097
                'title' => $title,
1098
                'description' => $description,
1099
            ];
1100
            Database::update(
1101
                $tbl_thematic_plan,
1102
                $params,
1103
                ['c_id = ? AND id = ?' => [$course_id, $thematic_plan_id]]
1104
            );
1105
1106
            api_item_property_update(
1107
                $_course,
1108
                'thematic_plan',
1109
                $thematic_plan_id,
1110
                'ThematicPlanUpdated',
1111
                $user_id
1112
            );*/
1113
        } else {
1114
            $thematic = Container::getThematicRepository()->find($thematic_id);
1115
            $plan = new CThematicPlan();
1116
            $plan
1117
                ->setTitle($title)
1118
                ->setDescription($description)
1119
                ->setCId($this->course_int_id)
1120
                ->setThematic($thematic)
1121
                ->setDescriptionType($description_type)
1122
            ;
1123
1124
            $em->persist($plan);
1125
1126
            $repo->addResourceToCourse(
1127
                $plan,
1128
                ResourceLink::VISIBILITY_PUBLISHED,
1129
                api_get_user_entity(api_get_user_id()),
1130
                api_get_course_entity(),
1131
                api_get_session_entity(),
1132
                api_get_group_entity()
1133
            );
1134
1135
            $em->flush();
1136
1137
            if ($plan && $plan->getIid()) {
1138
                $id = $plan->getIid();
1139
                $sql = "UPDATE $tbl_thematic_plan SET id = iid WHERE iid = $id";
1140
                Database::query($sql);
1141
                /*
1142
                api_item_property_update(
1143
                    $_course,
1144
                    'thematic_plan',
1145
                    $last_id,
1146
                    'ThematicPlanAdded',
1147
                    $user_id
1148
                );*/
1149
            }
1150
        }
1151
1152
        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...
1153
    }
1154
1155
    /**
1156
     * Delete a thematic plan description.
1157
     *
1158
     * @param int $thematic_id      Thematic id
1159
     * @param int $description_type Description type
1160
     *
1161
     * @return int Affected rows
1162
     */
1163
    public function thematic_plan_destroy($thematic_id, $descriptionType)
1164
    {
1165
        $repo = Container::getThematicRepository();
1166
1167
        /** @var CThematic $thematic */
1168
        $thematic = $repo->find($thematic_id);
1169
1170
        foreach ($thematic->getPlans() as $plan) {
1171
            if ($descriptionType == $plan->getDescriptionType()) {
1172
                $thematic->getPlans()->removeElement($plan);
1173
            }
1174
        }
1175
        $repo->getEntityManager()->persist($thematic);
1176
        $repo->getEntityManager()->flush();
1177
1178
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type integer.
Loading history...
1179
1180
        $_course = api_get_course_info();
0 ignored issues
show
Unused Code introduced by
$_course = api_get_course_info() 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...
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
        $description_type = intval($description_type);
1187
        $user_id = api_get_user_id();
1188
        $course_info = api_get_course_info();
1189
        $course_id = $course_info['real_id'];
1190
1191
        // get thematic plan id
1192
        $thematic_plan_data = $this->get_thematic_plan_data($thematic_id, $description_type);
1193
        $thematic_plan_id = $thematic_plan_data[0]['id'];
1194
1195
        // delete
1196
        $sql = "DELETE FROM $tbl_thematic_plan
1197
                WHERE
1198
                    c_id = $course_id AND
1199
                    thematic_id = $thematic_id AND
1200
                    description_type = $description_type ";
1201
        $result = Database::query($sql);
1202
        $affected_rows = Database::affected_rows($result);
1203
        if ($affected_rows) {
1204
            /*api_item_property_update(
1205
                $_course,
1206
                'thematic_plan',
1207
                $thematic_plan_id,
1208
                'ThematicPlanDeleted',
1209
                $user_id
1210
            );*/
1211
        }
1212
1213
        return $affected_rows;
1214
    }
1215
1216
    /**
1217
     * Get next description type for a new thematic plan description (option 'others').
1218
     *
1219
     * @param int $thematic_id Thematic id
1220
     *
1221
     * @return int New Description type
1222
     */
1223
    public function get_next_description_type($thematic_id)
1224
    {
1225
        // definition database table
1226
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
1227
1228
        // protect data
1229
        $thematic_id = intval($thematic_id);
1230
        $course_id = api_get_course_int_id();
1231
1232
        $sql = "SELECT MAX(description_type) as max
1233
                FROM $tbl_thematic_plan
1234
                WHERE
1235
                    c_id = $course_id AND
1236
                    thematic_id = $thematic_id AND
1237
                    description_type >= ".ADD_THEMATIC_PLAN;
1238
        $rs = Database::query($sql);
1239
        $row = Database::fetch_array($rs);
1240
        $last_description_type = $row['max'];
1241
1242
        if (isset($last_description_type)) {
1243
            $next_description_type = $last_description_type + 1;
1244
        } else {
1245
            $next_description_type = ADD_THEMATIC_PLAN;
1246
        }
1247
1248
        return $next_description_type;
1249
    }
1250
1251
    /**
1252
     * update done thematic advances from thematic details interface.
1253
     *
1254
     * @return int Affected rows
1255
     */
1256
    public function update_done_thematic_advances($advanceId)
1257
    {
1258
        $repo = Container::getThematicRepository();
1259
        $em = $repo->getEntityManager();
1260
1261
        $list = self::get_thematic_list( api_get_course_id());
1262
        $ordered = [];
1263
1264
        foreach ($list as $thematic) {
1265
            $done = true;
1266
            foreach ($thematic->getAdvances() as $advance) {
1267
                $ordered[] = $advance;
1268
                /*if ($advanceId === $advance->getIid()) {
1269
                    $done = false;
1270
                }*/
1271
                $advance->setDoneAdvance($done);
1272
            }
1273
1274
        }
1275
1276
        $done = true;
1277
        foreach ($ordered as $advance) {
1278
            if ($advanceId === $advance->getIid()) {
1279
                $done = false;
1280
                $advance->setDoneAdvance(true);
1281
                $em->persist($advance);
1282
                continue;
1283
            }
1284
1285
            $advance->setDoneAdvance($done);
1286
            $em->persist($advance);
1287
1288
        }
1289
1290
1291
        $em->flush();
1292
1293
        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...
1294
1295
        $_course = api_get_course_info();
0 ignored issues
show
Unused Code introduced by
$_course = api_get_course_info() 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...
1296
        $thematic_data = self::get_thematic_list( api_get_course_id());
1297
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
1298
1299
        $affected_rows = 0;
1300
        $user_id = api_get_user_id();
1301
1302
        /*$all = [];
1303
        if (!empty($thematic_data)) {
1304
            foreach ($thematic_data as $thematic) {
1305
                $thematic_id = $thematic['id'];
1306
                if (!empty($thematic_advance_data[$thematic['id']])) {
1307
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1308
                        $all[] = $thematic_advance['id'];
1309
                    }
1310
                }
1311
            }
1312
        }*/
1313
        $error = null;
1314
        $a_thematic_advance_ids = [];
1315
        $course_id = api_get_course_int_id();
1316
        $sessionId = api_get_session_id();
1317
1318
        /*if (!empty($thematic_data)) {
1319
            foreach ($thematic_data as $thematic) {
1320
                $my_affected_rows = 0;
1321
                $thematic_id = $thematic['id'];
1322
                if (!empty($thematic_advance_data[$thematic['id']])) {
1323
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1324
                        $item_info = api_get_item_property_info(
1325
                            api_get_course_int_id(),
1326
                            'thematic_advance',
1327
                            $thematic_advance['id'],
1328
                            $sessionId
1329
                        );
1330
1331
                        if ($item_info['session_id'] == $sessionId) {
1332
                            $a_thematic_advance_ids[] = $thematic_advance['id'];
1333
                            // update done thematic for previous advances ((done_advance = 1))
1334
                            $upd = "UPDATE $table SET
1335
                                    done_advance = 1
1336
                                    WHERE c_id = $course_id AND id = ".$thematic_advance['id'].' ';
1337
                            $result = Database::query($upd);
1338
                            $my_affected_rows = Database::affected_rows($result);
1339
                            $affected_rows += $my_affected_rows;
1340
                            //if ($my_affected_rows) {
1341
                            api_item_property_update(
1342
                                $_course,
1343
                                'thematic_advance',
1344
                                $thematic_advance['id'],
1345
                                'ThematicAdvanceDone',
1346
                                $user_id
1347
                            );
1348
                            //}
1349
                            if ($thematic_advance['id'] == $thematic_advance_id) {
1350
                                break 2;
1351
                            }
1352
                        }
1353
                    }
1354
                }
1355
            }
1356
        }*/
1357
1358
        // Update done thematic for others advances (done_advance = 0)
1359
        if (!empty($a_thematic_advance_ids) && count($a_thematic_advance_ids) > 0) {
1360
            $diff = array_diff($all, $a_thematic_advance_ids);
1361
            if (!empty($diff)) {
1362
                $upd = "UPDATE $table SET done_advance = 0
1363
                        WHERE c_id = $course_id AND id IN(".implode(',', $diff).') ';
1364
                Database::query($upd);
1365
            }
1366
1367
            // update item_property
1368
            $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
1369
            $sql = "SELECT ref FROM $tbl_item_property
1370
                    WHERE
1371
                        c_id = $course_id AND
1372
                        tool='thematic_advance' AND
1373
                        lastedit_type='ThematicAdvanceDone' AND
1374
                        session_id = $sessionId ";
1375
            // get all thematic advance done
1376
            $rs_thematic_done = Database::query($sql);
1377
            if (Database::num_rows($rs_thematic_done) > 0) {
1378
                while ($row_thematic_done = Database::fetch_array($rs_thematic_done)) {
1379
                    $ref = $row_thematic_done['ref'];
1380
                    if (in_array($ref, $a_thematic_advance_ids)) {
1381
                        continue;
1382
                    }
1383
                    // update items
1384
                    $sql = "UPDATE $tbl_item_property SET
1385
                                lastedit_date='".api_get_utc_datetime()."',
1386
                                lastedit_type='ThematicAdvanceUpdated',
1387
                                lastedit_user_id = $user_id
1388
                            WHERE
1389
                                c_id = $course_id AND
1390
                                tool='thematic_advance' AND
1391
                                ref=$ref AND
1392
                                session_id = $sessionId  ";
1393
                    Database::query($sql);
1394
                }
1395
            }
1396
        }
1397
1398
        return $affected_rows;
1399
    }
1400
1401
    /**
1402
     * Get last done thematic advance from thematic details interface.
1403
     *
1404
     * @return int Last done thematic advance id
1405
     */
1406
    public function get_last_done_thematic_advance()
1407
    {
1408
        $thematic_data = self::get_thematic_list();
1409
1410
        $a_thematic_advance_ids = [];
1411
        $last_done_advance_id = 0;
1412
        if (!empty($thematic_data)) {
1413
            /** @var CThematic $thematic */
1414
            foreach ($thematic_data as $thematic) {
1415
                $id = $thematic->getIid();
1416
                if ($thematic->getAdvances()->count()) {
1417
                    foreach ($thematic->getAdvances() as $thematic_advance) {
1418
                        if (1 == $thematic_advance->getDoneAdvance()) {
1419
                            $a_thematic_advance_ids[] = $thematic_advance->getIid();
1420
                        }
1421
                    }
1422
                }
1423
            }
1424
        }
1425
        if (!empty($a_thematic_advance_ids)) {
1426
            $last_done_advance_id = array_pop($a_thematic_advance_ids);
1427
            $last_done_advance_id = intval($last_done_advance_id);
1428
        }
1429
1430
        return $last_done_advance_id;
1431
    }
1432
1433
    /**
1434
     * Get next thematic advance not done from thematic details interface.
1435
     *
1436
     * @param   int Offset (if you want to get an item that is not directly the next)
1437
     *
1438
     * @return int next thematic advance not done
1439
     */
1440
    public function get_next_thematic_advance_not_done($offset = 1)
1441
    {
1442
        $thematic_data = self::get_thematic_list();
1443
        $thematic_advance_data = $this->get_thematic_advance_list();
1444
        $a_thematic_advance_ids = [];
1445
        $next_advance_not_done = 0;
1446
        if (!empty($thematic_data)) {
1447
            foreach ($thematic_data as $thematic) {
1448
                if (!empty($thematic_advance_data[$thematic['id']])) {
1449
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1450
                        if (0 == $thematic_advance['done_advance']) {
1451
                            $a_thematic_advance_ids[] = $thematic_advance['id'];
1452
                        }
1453
                    }
1454
                }
1455
            }
1456
        }
1457
1458
        if (!empty($a_thematic_advance_ids)) {
1459
            for ($i = 0; $i < $offset; ++$i) {
1460
                $next_advance_not_done = array_shift($a_thematic_advance_ids);
1461
            }
1462
            $next_advance_not_done = intval($next_advance_not_done);
1463
        }
1464
1465
        return $next_advance_not_done;
1466
    }
1467
1468
    /**
1469
     * Get total average of thematic advances.
1470
     *
1471
     * @param string $course_code (optional)
1472
     * @param int    $session_id  (optional)
1473
     *
1474
     * @return float Average of thematic advances
1475
     */
1476
    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

1476
    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...
1477
    {
1478
        if (empty($course_code)) {
1479
            $course_code = api_get_course_id();
1480
        }
1481
        if (api_get_session_id()) {
1482
            $thematic_data = self::get_thematic_list( $course_code);
1483
        } else {
1484
            $thematic_data = self::get_thematic_list( $course_code, 0);
1485
        }
1486
1487
        $a_average_of_advances_by_thematic = [];
1488
        $total_average = 0;
1489
        if (!empty($thematic_data)) {
1490
            /** @var CThematic $thematic */
1491
            foreach ($thematic_data as $thematic) {
1492
                $thematic_id = $thematic->getIid();
1493
                $a_average_of_advances_by_thematic[$thematic_id] = $this->get_average_of_advances_by_thematic(
1494
                    $thematic,
1495
                    $course_code
1496
                );
1497
            }
1498
        }
1499
1500
        // calculate total average
1501
        if (!empty($a_average_of_advances_by_thematic)) {
1502
            $count_tematics = count($thematic_data);
1503
            $score = array_sum($a_average_of_advances_by_thematic);
1504
            $total_average = round(($score * 100) / ($count_tematics * 100));
1505
        }
1506
1507
        return $total_average;
1508
    }
1509
1510
    /**
1511
     * Get average of advances by thematic.
1512
     *
1513
     * @param CThematic $thematic
1514
     * @param string $course_code
1515
     *
1516
     * @return float Average of thematic advances
1517
     */
1518
    public function get_average_of_advances_by_thematic($thematic, $course_code = null)
0 ignored issues
show
Unused Code introduced by
The parameter $course_code 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

1518
    public function get_average_of_advances_by_thematic($thematic, /** @scrutinizer ignore-unused */ $course_code = 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...
1519
    {
1520
        $advances = $thematic->getAdvances();
1521
        $average = 0;
1522
        if ($advances->count()) {
1523
            // get all done advances by thematic
1524
            $count = 0;
1525
            /** @var CThematicAdvance $thematic_advance */
1526
            foreach ($advances as $thematic_advance) {
1527
                if ($thematic_advance->getDoneAdvance()) {
1528
                    $count++;
1529
                }
1530
            }
1531
1532
            // calculate average by thematic
1533
            $average = round(($count * 100) / count($advances));
1534
        }
1535
1536
        return $average;
1537
    }
1538
1539
    /**
1540
     * set attributes for fields of thematic table.
1541
     *
1542
     * @param    int        Thematic id
1543
     * @param    string    Thematic title
1544
     * @param    string    Thematic content
1545
     * @param    int        Session id
1546
     */
1547
    public function set_thematic_attributes($id = null, $title = '', $content = '', $session_id = 0)
1548
    {
1549
        $this->thematic_id = $id;
1550
        $this->thematic_title = $title;
1551
        $this->thematic_content = $content;
1552
        $this->session_id = $session_id;
1553
    }
1554
1555
    /**
1556
     * set attributes for fields of thematic_plan table.
1557
     *
1558
     * @param    int        Thematic id
1559
     * @param    string    Thematic plan title
1560
     * @param    string    Thematic plan description
1561
     * @param    int        Thematic plan description type
1562
     */
1563
    public function set_thematic_plan_attributes(
1564
        $thematic_id = 0,
1565
        $title = '',
1566
        $description = '',
1567
        $description_type = 0
1568
    ) {
1569
        $this->thematic_id = $thematic_id;
1570
        $this->thematic_plan_title = $title;
1571
        $this->thematic_plan_description = $description;
1572
        $this->thematic_plan_description_type = $description_type;
1573
    }
1574
1575
    /**
1576
     * set attributes for fields of thematic_advance table.
1577
     *
1578
     * @param int $id Thematic advance id
1579
     * @param    int        Thematic id
1580
     * @param    int        Attendance id
1581
     * @param    string    Content
1582
     * @param    string    Date and time
1583
     * @param    int        Duration in hours
1584
     */
1585
    public function set_thematic_advance_attributes(
1586
        $id = null,
1587
        $thematic_id = 0,
1588
        $attendance_id = 0,
1589
        $content = '',
1590
        $start_date = null,
1591
        $duration = 0
1592
    ) {
1593
        $this->thematic_advance_id = $id;
1594
        $this->thematic_id = $thematic_id;
1595
        $this->attendance_id = $attendance_id;
1596
        $this->thematic_advance_content = $content;
1597
        $this->start_date = $start_date;
1598
        $this->duration = $duration;
1599
    }
1600
1601
    /**
1602
     * set thematic id.
1603
     *
1604
     * @param    int     Thematic id
1605
     */
1606
    public function set_thematic_id($thematic_id)
1607
    {
1608
        $this->thematic_id = $thematic_id;
1609
    }
1610
1611
    /**
1612
     * get thematic id.
1613
     *
1614
     * @return int
1615
     */
1616
    public function get_thematic_id()
1617
    {
1618
        return $this->thematic_id;
1619
    }
1620
1621
    /**
1622
     * Get thematic plan titles by default.
1623
     *
1624
     * @return array
1625
     */
1626
    public function get_default_thematic_plan_title()
1627
    {
1628
        $default_thematic_plan_titles = [];
1629
        $default_thematic_plan_titles[1] = get_lang('Objectives');
1630
        $default_thematic_plan_titles[2] = get_lang('Skills to acquire');
1631
        $default_thematic_plan_titles[3] = get_lang('Methodology');
1632
        $default_thematic_plan_titles[4] = get_lang('Infrastructure');
1633
        $default_thematic_plan_titles[5] = get_lang('Assessment');
1634
        $default_thematic_plan_titles[6] = get_lang('Others');
1635
1636
        return $default_thematic_plan_titles;
1637
    }
1638
1639
    /**
1640
     * Get thematic plan icons by default.
1641
     *
1642
     * @return array
1643
     */
1644
    public function get_default_thematic_plan_icon()
1645
    {
1646
        $default_thematic_plan_icon = [];
1647
        $default_thematic_plan_icon[1] = 'icons/32/objective.png';
1648
        $default_thematic_plan_icon[2] = 'icons/32/skills.png';
1649
        $default_thematic_plan_icon[3] = 'icons/32/strategy.png';
1650
        $default_thematic_plan_icon[4] = 'icons/32/laptop.png';
1651
        $default_thematic_plan_icon[5] = 'icons/32/assessment.png';
1652
        $default_thematic_plan_icon[6] = 'icons/32/wizard.png';
1653
1654
        return $default_thematic_plan_icon;
1655
    }
1656
1657
    /**
1658
     * Get questions by default for help.
1659
     *
1660
     * @return array
1661
     */
1662
    public function get_default_question()
1663
    {
1664
        $question = [];
1665
        $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?');
1666
        $question[2] = get_lang('Skills to acquireQuestions');
1667
        $question[3] = get_lang('What methods and activities help achieve the objectives of the course?  What would the schedule be?');
1668
        $question[4] = get_lang('What infrastructure is necessary to achieve the goals of this topic normally?');
1669
        $question[5] = get_lang('How will learners be assessed? Are there strategies to develop in order to master the topic?');
1670
1671
        return $question;
1672
    }
1673
}
1674