Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

main/inc/lib/thematic.lib.php (1 issue)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Provides functions for thematic option inside attendance tool.
6
 * It's also used like model to thematic_controller (MVC pattern)
7
 * Thematic class can be used to instanciate objects or as a library for thematic control.
8
 *
9
 * @author Christian Fasanando <[email protected]>
10
 * @author Julio Montoya <[email protected]> SQL fixes
11
 */
12
class Thematic
13
{
14
    private $session_id;
15
    private $thematic_id;
16
    private $thematic_title;
17
    private $thematic_content;
18
    private $thematic_plan_id;
19
    private $thematic_plan_title;
20
    private $thematic_plan_description;
21
    private $thematic_plan_description_type;
22
    private $thematic_advance_id;
23
    private $attendance_id;
24
    private $thematic_advance_content;
25
    private $start_date;
26
    private $duration;
27
    private $course_int_id;
28
29
    /**
30
     * Constructor.
31
     */
32
    public function __construct()
33
    {
34
        $this->course_int_id = api_get_course_int_id();
35
    }
36
37
    /**
38
     * Get the total number of thematic inside current course and current session.
39
     *
40
     * @see SortableTable#get_total_number_of_items()
41
     */
42
    public function get_number_of_thematics()
43
    {
44
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
45
        $condition_session = '';
46
        if (!api_get_session_id()) {
47
            $condition_session = api_get_session_condition(0);
48
        }
49
        $course_id = api_get_course_int_id();
50
        $sql = "SELECT COUNT(id) AS total_number_of_items
51
                FROM $tbl_thematic
52
                WHERE c_id = $course_id AND active = 1 $condition_session ";
53
        $res = Database::query($sql);
54
        $obj = Database::fetch_object($res);
55
56
        return $obj->total_number_of_items;
57
    }
58
59
    /**
60
     * Get the thematics to display on the current page (fill the sortable-table).
61
     *
62
     * @param   int     offset of first user to recover
63
     * @param   int     Number of users to get
64
     * @param   int     Column to sort on
65
     * @param   string  Order (ASC,DESC)
66
     *
67
     * @return array
68
     *
69
     * @see SortableTable#get_table_data($from)
70
     */
71
    public function get_thematic_data($from, $number_of_items, $column, $direction)
72
    {
73
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
74
        $condition_session = '';
75
        if (!api_get_session_id()) {
76
            $condition_session = api_get_session_condition(0);
77
        }
78
        $column = intval($column);
79
        $from = intval($from);
80
        $number_of_items = intval($number_of_items);
81
82
        if (!in_array($direction, ['ASC', 'DESC'])) {
83
            $direction = 'ASC';
84
        }
85
86
        $course_id = api_get_course_int_id();
87
88
        $sql = "SELECT id AS col0, title AS col1, display_order AS col2, session_id
89
                FROM $tbl_thematic
90
                WHERE c_id = $course_id AND active = 1 $condition_session
91
                ORDER BY col2
92
                LIMIT $from,$number_of_items ";
93
        $res = Database::query($sql);
94
95
        $thematics = [];
96
        $user_info = api_get_user_info(api_get_user_id());
97
        while ($thematic = Database::fetch_row($res)) {
98
            $session_star = '';
99
            if (api_get_session_id() == $thematic[3]) {
100
                $session_star = api_get_session_image(api_get_session_id(), $user_info['status']);
101
            }
102
            $thematic[1] = '<a href="index.php?'.api_get_cidreq().'&action=thematic_details&thematic_id='.$thematic[0].'">'.
103
                Security::remove_XSS($thematic[1], STUDENT).$session_star.'</a>';
104
            if (api_is_allowed_to_edit(null, true)) {
105
                $actions = '';
106
107
                if (api_get_session_id()) {
108
                    if (api_get_session_id() == $thematic[3]) {
109
                        $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_plan_list&thematic_id='.$thematic[0].'">'.
110
                            Display::return_icon('lesson_plan.png', get_lang('ThematicPlan'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
111
                        $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_list&thematic_id='.$thematic[0].'">'.
112
                            Display::return_icon('lesson_plan_calendar.png', get_lang('ThematicAdvance'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
113
114
                        $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_edit&thematic_id='.$thematic[0].'">'.
115
                            Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).'</a>';
116
                        $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToDelete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=thematic_delete&thematic_id='.$thematic[0].'">'.
117
                            Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
118
                    } else {
119
                        $actions .= Display::return_icon(
120
                            'lesson_plan_na.png',
121
                            get_lang('ThematicPlan'),
122
                            '',
123
                            ICON_SIZE_SMALL
124
                        ).'&nbsp;';
125
                        $actions .= Display::return_icon(
126
                            'lesson_plan_calendar_na.png',
127
                            get_lang('ThematicAdvance'),
128
                            '',
129
                            ICON_SIZE_SMALL
130
                        ).'&nbsp;';
131
                        $actions .= Display::return_icon('edit_na.png', get_lang('Edit'), '', ICON_SIZE_SMALL);
132
                        $actions .= Display::return_icon(
133
                            'delete_na.png',
134
                            get_lang('Delete'),
135
                            '',
136
                            ICON_SIZE_SMALL
137
                        ).'&nbsp;';
138
                        $actions .= Display::url(
139
                            Display::return_icon('cd.gif', get_lang('Copy')),
140
                            'index.php?'.api_get_cidreq().'&action=thematic_copy&thematic_id='.$thematic[0]
141
                        );
142
                    }
143
                } else {
144
                    $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_plan_list&thematic_id='.$thematic[0].'">'.
145
                        Display::return_icon('lesson_plan.png', get_lang('ThematicPlan'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
146
                    $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_list&thematic_id='.$thematic[0].'">'.
147
                        Display::return_icon('lesson_plan_calendar.png', get_lang('ThematicAdvance'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
148
149
                    if ($thematic[2] > 1) {
150
                        $actions .= '<a href="'.api_get_self().'?action=moveup&'.api_get_cidreq().'&thematic_id='.$thematic[0].'">'.
151
                            Display::return_icon('up.png', get_lang('Up'), '', ICON_SIZE_SMALL).'</a>';
152
                    } else {
153
                        $actions .= Display::return_icon('up_na.png', '&nbsp;', '', ICON_SIZE_SMALL);
154
                    }
155
                    if ($thematic[2] < self::get_max_thematic_item()) {
156
                        $actions .= '<a href="'.api_get_self().'?action=movedown&a'.api_get_cidreq().'&thematic_id='.$thematic[0].'">'.
157
                            Display::return_icon('down.png', get_lang('Down'), '', ICON_SIZE_SMALL).'</a>';
158
                    } else {
159
                        $actions .= Display::return_icon('down_na.png', '&nbsp;', '', ICON_SIZE_SMALL);
160
                    }
161
                    $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_edit&thematic_id='.$thematic[0].'">'.
162
                        Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).'</a>';
163
                    $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToDelete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=thematic_delete&thematic_id='.$thematic[0].'">'.
164
                        Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
165
                }
166
                $thematics[] = [$thematic[0], $thematic[1], $actions];
167
            }
168
        }
169
170
        return $thematics;
171
    }
172
173
    /**
174
     * Get the maximum display order of the thematic item.
175
     *
176
     * @param bool $use_session
177
     *
178
     * @return int Maximum display order
179
     */
180
    public function get_max_thematic_item($use_session = true)
181
    {
182
        // Database table definition
183
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
184
        $session_id = api_get_session_id();
185
        if ($use_session) {
186
            $condition_session = api_get_session_condition($session_id);
187
        } else {
188
            $condition_session = '';
189
        }
190
        $course_id = api_get_course_int_id();
191
        $sql = "SELECT MAX(display_order)
192
                FROM $tbl_thematic
193
                WHERE c_id = $course_id AND active = 1 $condition_session";
194
        $rs = Database::query($sql);
195
        $row = Database::fetch_array($rs);
196
197
        return $row[0];
198
    }
199
200
    /**
201
     * Move a thematic.
202
     *
203
     * @param string $direction   (up, down)
204
     * @param int    $thematic_id
205
     */
206
    public function move_thematic($direction, $thematic_id)
207
    {
208
        // Database table definition
209
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
210
211
        // sort direction
212
        if ($direction == 'up') {
213
            $sortorder = 'DESC';
214
        } else {
215
            $sortorder = 'ASC';
216
        }
217
        $course_id = api_get_course_int_id();
218
        $session_id = api_get_session_id();
219
        $condition_session = api_get_session_condition($session_id);
220
221
        $sql = "SELECT id, display_order
222
                FROM $tbl_thematic
223
                WHERE c_id = $course_id AND active = 1 $condition_session
224
                ORDER BY display_order $sortorder";
225
        $res = Database::query($sql);
226
        $found = false;
227
228
        // Variable definition
229
        $current_id = 0;
230
        $next_id = 0;
231
        while ($row = Database::fetch_array($res)) {
232
            if ($found && empty($next_id)) {
233
                $next_id = intval($row['id']);
234
                $next_display_order = intval($row['display_order']);
235
            }
236
237
            if ($row['id'] == $thematic_id) {
238
                $current_id = intval($thematic_id);
239
                $current_display_order = intval($row['display_order']);
240
                $found = true;
241
            }
242
        }
243
244
        // get last done thematic advance before move thematic list
245
        $last_done_thematic_advance = $this->get_last_done_thematic_advance();
246
247
        if (!empty($next_display_order) && !empty($current_id)) {
248
            $sql = "UPDATE $tbl_thematic SET display_order = $next_display_order
249
                    WHERE c_id = $course_id AND id = $current_id ";
250
            Database::query($sql);
251
        }
252
        if (!empty($current_display_order) && !empty($next_id)) {
253
            $sql = "UPDATE $tbl_thematic SET
254
                    display_order = $current_display_order
255
                    WHERE c_id = $course_id AND id = $next_id ";
256
            Database::query($sql);
257
        }
258
259
        // update done advances with de current thematic list
260
        $this->update_done_thematic_advances($last_done_thematic_advance);
261
    }
262
263
    /**
264
     * Get thematic list.
265
     *
266
     * @param int    $thematic_id Thematic id (optional), get list by id
267
     * @param string $course_code
268
     * @param int    $session_id
269
     *
270
     * @return array Thematic data
271
     */
272
    public static function get_thematic_list(
273
        $thematic_id = null,
274
        $course_code = null,
275
        $session_id = null
276
    ) {
277
        // set current course and session
278
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
279
        $course_info = api_get_course_info($course_code);
280
        $course_id = $course_info['real_id'];
281
282
        if (isset($session_id)) {
283
            $session_id = intval($session_id);
284
        } else {
285
            $session_id = api_get_session_id();
286
        }
287
288
        $data = [];
289
        if (isset($thematic_id)) {
290
            $thematic_id = intval($thematic_id);
291
            $condition = " WHERE id = $thematic_id AND active = 1 ";
292
        } else {
293
            if (empty($session_id)) {
294
                $condition_session = api_get_session_condition(0);
295
            } else {
296
                $condition_session = api_get_session_condition($session_id, true, true);
297
            }
298
            $condition = " WHERE active = 1 $condition_session ";
299
        }
300
        $sql = "SELECT *
301
                FROM $tbl_thematic $condition AND c_id = $course_id
302
                ORDER BY display_order ";
303
304
        $res = Database::query($sql);
305
        if (Database::num_rows($res) > 0) {
306
            if (!empty($thematic_id)) {
307
                $data = Database::fetch_array($res, 'ASSOC');
308
            } else {
309
                while ($row = Database::fetch_array($res, 'ASSOC')) {
310
                    $data[$row['id']] = $row;
311
                }
312
            }
313
        }
314
315
        return $data;
316
    }
317
318
    /**
319
     * Insert or update a thematic.
320
     *
321
     * @return int last thematic id
322
     */
323
    public function thematic_save()
324
    {
325
        $_course = api_get_course_info();
326
        // definition database table
327
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
328
329
        // protect data
330
        $id = intval($this->thematic_id);
331
        $title = $this->thematic_title;
332
        $content = $this->thematic_content;
333
        $session_id = intval($this->session_id);
334
        $user_id = api_get_user_id();
335
336
        // get the maximum display order of all the glossary items
337
        $max_thematic_item = $this->get_max_thematic_item(false);
338
339
        if (empty($id)) {
340
            // insert
341
            $params = [
342
                'c_id' => $this->course_int_id,
343
                'title' => $title,
344
                'content' => $content,
345
                'active' => 1,
346
                'display_order' => intval($max_thematic_item) + 1,
347
                'session_id' => $session_id,
348
            ];
349
            $last_id = Database::insert($tbl_thematic, $params);
350
            if ($last_id) {
351
                $sql = "UPDATE $tbl_thematic SET id = iid WHERE iid = $last_id";
352
                Database::query($sql);
353
                api_item_property_update(
354
                    $_course,
355
                    'thematic',
356
                    $last_id,
357
                    "ThematicAdded",
358
                    $user_id
359
                );
360
            }
361
        } else {
362
            // Update
363
            $params = [
364
                'title' => $title,
365
                'content' => $content,
366
                'session_id' => $session_id,
367
            ];
368
369
            Database::update(
370
                $tbl_thematic,
371
                $params,
372
                ['id  = ? AND c_id = ?' => [$id, $this->course_int_id]]
373
            );
374
375
            $last_id = $id;
376
377
            // save inside item property table
378
            api_item_property_update(
379
                $_course,
380
                'thematic',
381
                $last_id,
382
                "ThematicUpdated",
383
                $user_id
384
            );
385
        }
386
387
        return $last_id;
388
    }
389
390
    /**
391
     * Delete logically (set active field to 0) a thematic.
392
     *
393
     * @param int|array One or many thematic ids
394
     *
395
     * @return int Affected rows
396
     */
397
    public function delete($thematic_id)
398
    {
399
        $_course = api_get_course_info();
400
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
401
        $affected_rows = 0;
402
        $user_id = api_get_user_id();
403
        $course_id = api_get_course_int_id();
404
405
        if (is_array($thematic_id)) {
406
            foreach ($thematic_id as $id) {
407
                $id = intval($id);
408
                $sql = "UPDATE $tbl_thematic SET active = 0
409
                        WHERE c_id = $course_id AND id = $id";
410
                $result = Database::query($sql);
411
                $affected_rows += Database::affected_rows($result);
412
                if (!empty($affected_rows)) {
413
                    // update row item property table
414
                    api_item_property_update(
415
                        $_course,
416
                        'thematic',
417
                        $id,
418
                        "ThematicDeleted",
419
                        $user_id
420
                    );
421
                }
422
            }
423
        } else {
424
            $thematic_id = intval($thematic_id);
425
            $sql = "UPDATE $tbl_thematic SET active = 0
426
                    WHERE c_id = $course_id AND id = $thematic_id";
427
            $result = Database::query($sql);
428
            $affected_rows = Database::affected_rows($result);
429
            if (!empty($affected_rows)) {
430
                // update row item property table
431
                api_item_property_update(
432
                    $_course,
433
                    'thematic',
434
                    $thematic_id,
435
                    "ThematicDeleted",
436
                    $user_id
437
                );
438
            }
439
        }
440
441
        return $affected_rows;
442
    }
443
444
    /**
445
     * @param int $thematic_id
446
     */
447
    public function copy($thematic_id)
448
    {
449
        $thematic = self::get_thematic_list($thematic_id, api_get_course_id(), 0);
450
        $thematic_copy = new Thematic();
451
        $thematic_copy->set_thematic_attributes(
452
            '',
453
            $thematic['title'].' - '.get_lang('Copy'),
454
            $thematic['content'],
455
            api_get_session_id()
456
        );
457
458
        $new_thematic_id = $thematic_copy->thematic_save();
459
        if (!empty($new_thematic_id)) {
460
            $thematic_advanced = self::get_thematic_advance_by_thematic_id($thematic_id);
461
            if (!empty($thematic_advanced)) {
462
                foreach ($thematic_advanced as $item) {
463
                    $thematic = new Thematic();
464
                    $thematic->set_thematic_advance_attributes(
465
                        0,
466
                        $new_thematic_id,
467
                        0,
468
                        $item['content'],
469
                        $item['start_date'],
470
                        $item['duration']
471
                    );
472
                    $thematic->thematic_advance_save();
473
                }
474
            }
475
            $thematic_plan = self::get_thematic_plan_data($thematic_id);
476
            if (!empty($thematic_plan)) {
477
                foreach ($thematic_plan as $item) {
478
                    $thematic = new Thematic();
479
                    $thematic->set_thematic_plan_attributes(
480
                        $new_thematic_id,
481
                        $item['title'],
482
                        $item['description'],
483
                        $item['description_type']
484
                    );
485
                    $thematic->thematic_plan_save();
486
                }
487
            }
488
        }
489
    }
490
491
    /**
492
     * Get the total number of thematic advance inside current course.
493
     *
494
     * @see SortableTable#get_total_number_of_items()
495
     */
496
    public static function get_number_of_thematic_advances()
497
    {
498
        global $thematic_id;
499
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
500
        $course_id = api_get_course_int_id();
501
        $thematic_id = (int) $thematic_id;
502
503
        $sql = "SELECT COUNT(id) AS total_number_of_items
504
                FROM $table
505
                WHERE c_id = $course_id AND thematic_id = $thematic_id ";
506
        $res = Database::query($sql);
507
        $obj = Database::fetch_object($res);
508
509
        return $obj->total_number_of_items;
510
    }
511
512
    /**
513
     * Get the thematic advances to display on the current page (fill the sortable-table).
514
     *
515
     * @param   int     offset of first user to recover
516
     * @param   int     Number of users to get
517
     * @param   int     Column to sort on
518
     * @param   string  Order (ASC,DESC)
519
     *
520
     * @return array
521
     *
522
     * @see SortableTable#get_table_data($from)
523
     */
524
    public static function get_thematic_advance_data($from, $number_of_items, $column, $direction)
525
    {
526
        global $thematic_id;
527
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
528
        $column = intval($column);
529
        $from = intval($from);
530
        $number_of_items = intval($number_of_items);
531
        if (!in_array($direction, ['ASC', 'DESC'])) {
532
            $direction = 'ASC';
533
        }
534
        $data = [];
535
        $course_id = api_get_course_int_id();
536
        $thematic_id = (int) $thematic_id;
537
        if (api_is_allowed_to_edit(null, true)) {
538
            $sql = "SELECT id AS col0, start_date AS col1, duration AS col2, content AS col3
539
                    FROM $table
540
                    WHERE c_id = $course_id AND thematic_id = $thematic_id
541
                    ORDER BY col$column $direction
542
                    LIMIT $from,$number_of_items ";
543
544
            $list = api_get_item_property_by_tool(
545
                'thematic_advance',
546
                api_get_course_id(),
547
                api_get_session_id()
548
            );
549
550
            $elements = [];
551
            foreach ($list as $value) {
552
                $elements[] = $value['ref'];
553
            }
554
555
            $res = Database::query($sql);
556
            $i = 1;
557
            while ($thematic_advance = Database::fetch_row($res)) {
558
                if (in_array($thematic_advance[0], $elements)) {
559
                    $thematic_advance[1] = api_get_local_time($thematic_advance[1]);
560
                    $thematic_advance[1] = api_format_date($thematic_advance[1], DATE_TIME_FORMAT_LONG);
561
                    $actions = '';
562
                    $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_edit&thematic_id='.$thematic_id.'&thematic_advance_id='.$thematic_advance[0].'">'.
563
                        Display::return_icon('edit.png', get_lang('Edit'), '', 22).'</a>';
564
                    $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToDelete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=thematic_advance_delete&thematic_id='.$thematic_id.'&thematic_advance_id='.$thematic_advance[0].'">'.
565
                        Display::return_icon('delete.png', get_lang('Delete'), '', 22).'</a></center>';
566
                    $data[] = [$i, $thematic_advance[1], $thematic_advance[2], $thematic_advance[3], $actions];
567
                    $i++;
568
                }
569
            }
570
        }
571
572
        return $data;
573
    }
574
575
    /**
576
     * get thematic advance data by thematic id.
577
     *
578
     * @param int    $thematic_id
579
     * @param string $course_code Course code (optional)
580
     *
581
     * @return array data
582
     */
583
    public function get_thematic_advance_by_thematic_id($thematic_id, $course_code = null)
584
    {
585
        $course_info = api_get_course_info($course_code);
586
        $course_id = $course_info['real_id'];
587
588
        // set current course
589
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
590
        $thematic_id = (int) $thematic_id;
591
        $data = [];
592
        $sql = "SELECT * FROM $table
593
                WHERE c_id = $course_id AND thematic_id = $thematic_id ";
594
595
        $elements = [];
596
        $list = api_get_item_property_by_tool(
597
            'thematic_advance',
598
            $course_info['code'],
599
            api_get_session_id()
600
        );
601
        foreach ($list as $value) {
602
            $elements[] = $value['ref'];
603
        }
604
605
        $res = Database::query($sql);
606
        if (Database::num_rows($res) > 0) {
607
            while ($row = Database::fetch_array($res, 'ASSOC')) {
608
                if (in_array($row['id'], $elements)) {
609
                    $data[] = $row;
610
                }
611
            }
612
        }
613
614
        return $data;
615
    }
616
617
    /**
618
     * @param array $data
619
     *
620
     * @return array
621
     */
622
    public function get_thematic_advance_div($data)
623
    {
624
        $return_array = [];
625
        $uinfo = api_get_user_info();
626
627
        foreach ($data as $thematic_id => $thematic_advance_data) {
628
            foreach ($thematic_advance_data as $key => $thematic_advance) {
629
                $session_star = '';
630
                if (api_is_allowed_to_edit(null, true)) {
631
                    if ($thematic_advance['session_id'] != 0) {
632
                        $session_star = api_get_session_image(api_get_session_id(), $uinfo['status']);
633
                    }
634
                }
635
                // DATE_TIME_FORMAT_LONG
636
                $thematic_advance_item = '<div><strong>'.
637
                    api_convert_and_format_date($thematic_advance['start_date'], DATE_TIME_FORMAT_LONG).
638
                    $session_star.'</strong></div>';
639
                $thematic_advance_item .= '<div>'.$thematic_advance['duration'].' '.get_lang('HourShort').'</div>';
640
                $thematic_advance_item .= '<div>'.Security::remove_XSS($thematic_advance['content'], STUDENT).'</div>';
641
                $return_array[$thematic_id][$thematic_advance['id']] = $thematic_advance_item;
642
            }
643
        }
644
645
        return $return_array;
646
    }
647
648
    /**
649
     * @param array $data
650
     *
651
     * @return array
652
     */
653
    public function get_thematic_plan_array($data)
654
    {
655
        $final_return = [];
656
        $uinfo = api_get_user_info();
657
658
        foreach ($data as $thematic_id => $thematic_plan_data) {
659
            $new_thematic_plan_data = [];
660
            foreach ($thematic_plan_data as $thematic_item) {
661
                $thematic_simple_list[] = $thematic_item['description_type'];
662
                $new_thematic_plan_data[$thematic_item['description_type']] = $thematic_item;
663
            }
664
665
            if (!empty($thematic_simple_list)) {
666
                foreach ($thematic_simple_list as $item) {
667
                    $default_thematic_plan_title[$item] = $new_thematic_plan_data[$item]['title'];
668
                }
669
            }
670
671
            $session_star = '';
672
            $return = [];
673
            if (!empty($default_thematic_plan_title)) {
674
                foreach ($default_thematic_plan_title as $id => $title) {
675
                    //avoid others
676
                    if ($title == 'Others' && empty($data[$thematic_id][$id]['description'])) {
677
                        continue;
678
                    }
679
                    if (!empty($data[$thematic_id][$id]['title']) &&
680
                        !empty($data[$thematic_id][$id]['description'])
681
                    ) {
682
                        if (api_is_allowed_to_edit(null, true)) {
683
                            if ($data[$thematic_id][$id]['session_id'] != 0) {
684
                                $session_star = api_get_session_image(api_get_session_id(), $uinfo['status']);
685
                            }
686
                        }
687
688
                        $return[$id]['title'] = Security::remove_XSS($data[$thematic_id][$id]['title'], STUDENT).$session_star;
689
                        $return[$id]['description'] = Security::remove_XSS($data[$thematic_id][$id]['description'], STUDENT);
690
                    }
691
                }
692
            }
693
            $final_return[$thematic_id] = $return;
694
        }
695
696
        return $final_return;
697
    }
698
699
    /**
700
     * Get thematic advance list.
701
     *
702
     * @param int    $thematic_advance_id Thematic advance id (optional), get data by thematic advance list
703
     * @param string $course_code         Course code (optional)
704
     * @param bool   $force_session_id    Force to have a session id
705
     * @param bool   $withLocalTime       Force start_date to local time
706
     *
707
     * @return array $data
708
     */
709
    public function get_thematic_advance_list(
710
        $thematic_advance_id = null,
711
        $course_code = null,
712
        $force_session_id = false,
713
        $withLocalTime = false
714
    ) {
715
        $course_info = api_get_course_info($course_code);
716
        $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
717
        $data = [];
718
        $condition = '';
719
        $thematic_advance_id = (int) $thematic_advance_id;
720
721
        if (!empty($thematic_advance_id)) {
722
            $condition = " AND a.id = $thematic_advance_id ";
723
        }
724
725
        $course_id = $course_info['real_id'];
726
727
        $sql = "SELECT * FROM $tbl_thematic_advance a
728
                WHERE c_id = $course_id $condition
729
                ORDER BY start_date ";
730
731
        $elements = [];
732
        if ($force_session_id) {
733
            $list = api_get_item_property_by_tool(
734
                'thematic_advance',
735
                $course_info['code'],
736
                api_get_session_id()
737
            );
738
            foreach ($list as $value) {
739
                $elements[$value['ref']] = $value;
740
            }
741
        }
742
743
        $res = Database::query($sql);
744
        if (Database::num_rows($res) > 0) {
745
            if (!empty($thematic_advance_id)) {
746
                $data = Database::fetch_array($res);
747
            } else {
748
                // group all data group by thematic id
749
                $tmp = [];
750
                while ($row = Database::fetch_array($res, 'ASSOC')) {
751
                    if ($withLocalTime == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
752
                        $row['start_date'] = api_get_local_time($row['start_date']);
753
                    }
754
                    $tmp[] = $row['thematic_id'];
755
                    if (in_array($row['thematic_id'], $tmp)) {
756
                        if ($force_session_id) {
757
                            if (in_array($row['id'], array_keys($elements))) {
758
                                $row['session_id'] = $elements[$row['id']]['session_id'];
759
                                $data[$row['thematic_id']][$row['id']] = $row;
760
                            }
761
                        } else {
762
                            $data[$row['thematic_id']][$row['id']] = $row;
763
                        }
764
                    }
765
                }
766
            }
767
        }
768
769
        return $data;
770
    }
771
772
    /**
773
     * insert or update a thematic advance.
774
     *
775
     * @todo problem
776
     *
777
     * @return int last thematic advance id
778
     */
779
    public function thematic_advance_save()
780
    {
781
        $_course = api_get_course_info();
782
        // definition database table
783
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
784
785
        // protect data
786
        $id = intval($this->thematic_advance_id);
787
        $thematic_id = intval($this->thematic_id);
788
        $attendance_id = intval($this->attendance_id);
789
        $content = $this->thematic_advance_content;
790
        $start_date = $this->start_date;
791
        $duration = intval($this->duration);
792
        $user_id = api_get_user_id();
793
794
        $last_id = null;
795
        if (empty($id)) {
796
            // Insert
797
            $params = [
798
                'c_id' => $this->course_int_id,
799
                'thematic_id' => $thematic_id,
800
                'attendance_id' => $attendance_id,
801
                'content' => $content,
802
                'start_date' => api_get_utc_datetime($start_date),
803
                'duration' => $duration,
804
                'done_advance' => 0,
805
            ];
806
            $last_id = Database::insert($table, $params);
807
808
            if ($last_id) {
809
                $sql = "UPDATE $table SET id = iid WHERE iid = $last_id";
810
                Database::query($sql);
811
812
                api_item_property_update(
813
                    $_course,
814
                    'thematic_advance',
815
                    $last_id,
816
                    'ThematicAdvanceAdded',
817
                    $user_id
818
                );
819
            }
820
        } else {
821
            $params = [
822
                'thematic_id' => $thematic_id,
823
                'attendance_id' => $attendance_id,
824
                'content' => $content,
825
                'start_date' => api_get_utc_datetime($start_date),
826
                'duration' => $duration,
827
            ];
828
829
            Database::update(
830
                $table,
831
                $params,
832
                ['id = ? AND c_id = ?' => [$id, $this->course_int_id]]
833
            );
834
835
            api_item_property_update(
836
                $_course,
837
                'thematic_advance',
838
                $id,
839
                'ThematicAdvanceUpdated',
840
                $user_id
841
            );
842
        }
843
844
        return $last_id;
845
    }
846
847
    /**
848
     * delete  thematic advance.
849
     *
850
     * @param int $id Thematic advance id
851
     *
852
     * @return int Affected rows
853
     */
854
    public function thematic_advance_destroy($id)
855
    {
856
        $_course = api_get_course_info();
857
        $course_id = api_get_course_int_id();
858
859
        // definition database table
860
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
861
862
        // protect data
863
        $id = intval($id);
864
        $user_id = api_get_user_id();
865
866
        $sql = "DELETE FROM $table
867
                WHERE c_id = $course_id AND id = $id ";
868
        $result = Database::query($sql);
869
        $affected_rows = Database::affected_rows($result);
870
        if ($affected_rows) {
871
            api_item_property_update(
872
                $_course,
873
                'thematic_advance',
874
                $id,
875
                'ThematicAdvanceDeleted',
876
                $user_id
877
            );
878
        }
879
880
        return $affected_rows;
881
    }
882
883
    /**
884
     * get thematic plan data.
885
     *
886
     * @param int Thematic id (optional), get data by thematic id
887
     * @param int Thematic plan description type (optional), get data by description type
888
     *
889
     * @return array Thematic plan data
890
     */
891
    public function get_thematic_plan_data($thematic_id = null, $description_type = null)
892
    {
893
        // definition database table
894
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
895
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
896
        $course_id = api_get_course_int_id();
897
898
        $data = [];
899
        $condition = '';
900
        if (isset($thematic_id)) {
901
            $thematic_id = intval($thematic_id);
902
            $condition .= " AND thematic_id = $thematic_id ";
903
        }
904
        if (isset($description_type)) {
905
            $description_type = intval($description_type);
906
            $condition .= " AND description_type = $description_type ";
907
        }
908
909
        $items_from_course = api_get_item_property_by_tool(
910
            'thematic_plan',
911
            api_get_course_id(),
912
            0
913
        );
914
        $items_from_session = api_get_item_property_by_tool(
915
            'thematic_plan',
916
            api_get_course_id(),
917
            api_get_session_id()
918
        );
919
920
        $thematic_plan_complete_list = [];
921
        $thematic_plan_id_list = [];
922
923
        if (!empty($items_from_course)) {
924
            foreach ($items_from_course as $item) {
925
                $thematic_plan_id_list[] = $item['ref'];
926
                $thematic_plan_complete_list[$item['ref']] = $item;
927
            }
928
        }
929
930
        if (!empty($items_from_session)) {
931
            foreach ($items_from_session as $item) {
932
                $thematic_plan_id_list[] = $item['ref'];
933
                $thematic_plan_complete_list[$item['ref']] = $item;
934
            }
935
        }
936
        if (!empty($thematic_plan_id_list)) {
937
            $sql = "SELECT
938
                        tp.id, thematic_id, tp.title, description, description_type, t.session_id
939
                    FROM $tbl_thematic_plan tp
940
                    INNER JOIN $tbl_thematic t
941
                    ON (t.id = tp.thematic_id AND t.c_id = tp.c_id)
942
                    WHERE
943
                        t.c_id = $course_id AND
944
                        tp.c_id = $course_id
945
                        $condition AND
946
                        tp.id IN (".implode(', ', $thematic_plan_id_list).") ";
947
948
            $rs = Database::query($sql);
949
950
            if (Database::num_rows($rs)) {
951
                if (!isset($thematic_id) && !isset($description_type)) {
952
                    // group all data group by thematic id
953
                    $tmp = [];
954
                    while ($row = Database::fetch_array($rs, 'ASSOC')) {
955
                        $tmp[] = $row['thematic_id'];
956
                        if (in_array($row['thematic_id'], $tmp)) {
957
                            $row['session_id'] = $thematic_plan_complete_list[$row['id']];
958
                            $data[$row['thematic_id']][$row['description_type']] = $row;
959
                        }
960
                    }
961
                } else {
962
                    while ($row = Database::fetch_array($rs, 'ASSOC')) {
963
                        $row['session_id'] = $thematic_plan_complete_list[$row['id']];
964
                        $data[] = $row;
965
                    }
966
                }
967
            }
968
        }
969
970
        return $data;
971
    }
972
973
    /**
974
     * insert or update a thematic plan.
975
     *
976
     * @return int affected rows
977
     */
978
    public function thematic_plan_save()
979
    {
980
        $_course = api_get_course_info();
981
        // definition database table
982
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
983
984
        // protect data
985
        $thematic_id = intval($this->thematic_id);
986
        $title = $this->thematic_plan_title;
987
        $description = $this->thematic_plan_description;
988
        $description_type = intval($this->thematic_plan_description_type);
989
        $user_id = api_get_user_id();
990
        $course_id = api_get_course_int_id();
991
        $list = api_get_item_property_by_tool(
992
            'thematic_plan',
993
            api_get_course_id(),
994
            api_get_session_id()
995
        );
996
997
        $elements_to_show = [];
998
        foreach ($list as $value) {
999
            $elements_to_show[] = $value['ref'];
1000
        }
1001
        $condition = '';
1002
        if (!empty($elements_to_show)) {
1003
            $condition = "AND id IN (".implode(',', $elements_to_show).") ";
1004
        }
1005
        // check thematic plan type already exists
1006
        $sql = "SELECT id FROM $tbl_thematic_plan
1007
                WHERE
1008
                    c_id = $course_id AND
1009
                    thematic_id = $thematic_id AND
1010
                    description_type = '$description_type'";
1011
        $rs = Database::query($sql);
1012
1013
        $affected_rows = 0;
1014
        if (Database::num_rows($rs) > 0) {
1015
            $row_thematic_plan = Database::fetch_array($rs);
1016
            $thematic_plan_id = $row_thematic_plan['id'];
1017
            $update = false;
1018
            if (in_array($thematic_plan_id, $elements_to_show)) {
1019
                $update = true;
1020
            }
1021
1022
            if ($update) {
1023
                // update
1024
                $params = [
1025
                    'title' => $title,
1026
                    'description' => $description,
1027
                ];
1028
                Database::update(
1029
                    $tbl_thematic_plan,
1030
                    $params,
1031
                    ['c_id = ? AND id = ?' => [$course_id, $thematic_plan_id]]
1032
                );
1033
1034
                api_item_property_update(
1035
                    $_course,
1036
                    'thematic_plan',
1037
                    $thematic_plan_id,
1038
                    'ThematicPlanUpdated',
1039
                    $user_id
1040
                );
1041
            } else {
1042
                // insert
1043
                $params = [
1044
                    'c_id' => $this->course_int_id,
1045
                    'thematic_id' => $thematic_id,
1046
                    'title' => $title,
1047
                    'description' => $description,
1048
                    'description_type' => $description_type,
1049
                ];
1050
                $last_id = Database::insert($tbl_thematic_plan, $params);
1051
                if ($last_id) {
1052
                    $sql = "UPDATE $tbl_thematic_plan SET id = iid WHERE iid = $last_id";
1053
                    Database::query($sql);
1054
                    api_item_property_update(
1055
                        $_course,
1056
                        'thematic_plan',
1057
                        $last_id,
1058
                        'ThematicPlanAdded',
1059
                        $user_id
1060
                    );
1061
                }
1062
            }
1063
        } else {
1064
            // insert
1065
            $params = [
1066
                'c_id' => $this->course_int_id,
1067
                'thematic_id' => $thematic_id,
1068
                'title' => $title,
1069
                'description' => $description,
1070
                'description_type' => $description_type,
1071
            ];
1072
            $last_id = Database::insert($tbl_thematic_plan, $params);
1073
1074
            if ($last_id) {
1075
                $sql = "UPDATE $tbl_thematic_plan SET id = iid WHERE iid = $last_id";
1076
                Database::query($sql);
1077
                api_item_property_update(
1078
                    $_course,
1079
                    'thematic_plan',
1080
                    $last_id,
1081
                    'ThematicPlanAdded',
1082
                    $user_id
1083
                );
1084
            }
1085
        }
1086
1087
        return $affected_rows;
1088
    }
1089
1090
    /**
1091
     * Delete a thematic plan description.
1092
     *
1093
     * @param int $thematic_id      Thematic id
1094
     * @param int $description_type Description type
1095
     *
1096
     * @return int Affected rows
1097
     */
1098
    public function thematic_plan_destroy($thematic_id, $description_type)
1099
    {
1100
        $_course = api_get_course_info();
1101
        // definition database table
1102
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
1103
1104
        // protect data
1105
        $thematic_id = intval($thematic_id);
1106
        $description_type = intval($description_type);
1107
        $user_id = api_get_user_id();
1108
        $course_info = api_get_course_info();
1109
        $course_id = $course_info['real_id'];
1110
1111
        // get thematic plan id
1112
        $thematic_plan_data = $this->get_thematic_plan_data($thematic_id, $description_type);
1113
        $thematic_plan_id = $thematic_plan_data[0]['id'];
1114
1115
        // delete
1116
        $sql = "DELETE FROM $tbl_thematic_plan
1117
                WHERE
1118
                    c_id = $course_id AND
1119
                    thematic_id = $thematic_id AND
1120
                    description_type = $description_type ";
1121
        $result = Database::query($sql);
1122
        $affected_rows = Database::affected_rows($result);
1123
        if ($affected_rows) {
1124
            api_item_property_update(
1125
                $_course,
1126
                'thematic_plan',
1127
                $thematic_plan_id,
1128
                'ThematicPlanDeleted',
1129
                $user_id
1130
            );
1131
        }
1132
1133
        return $affected_rows;
1134
    }
1135
1136
    /**
1137
     * Get next description type for a new thematic plan description (option 'others').
1138
     *
1139
     * @param int $thematic_id Thematic id
1140
     *
1141
     * @return int New Description type
1142
     */
1143
    public function get_next_description_type($thematic_id)
1144
    {
1145
        // definition database table
1146
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
1147
1148
        // protect data
1149
        $thematic_id = intval($thematic_id);
1150
        $course_id = api_get_course_int_id();
1151
1152
        $sql = "SELECT MAX(description_type) as max
1153
                FROM $tbl_thematic_plan
1154
                WHERE
1155
                    c_id = $course_id AND
1156
                    thematic_id = $thematic_id AND
1157
                    description_type >= ".ADD_THEMATIC_PLAN;
1158
        $rs = Database::query($sql);
1159
        $row = Database::fetch_array($rs);
1160
        $last_description_type = $row['max'];
1161
1162
        if (isset($last_description_type)) {
1163
            $next_description_type = $last_description_type + 1;
1164
        } else {
1165
            $next_description_type = ADD_THEMATIC_PLAN;
1166
        }
1167
1168
        return $next_description_type;
1169
    }
1170
1171
    /**
1172
     * update done thematic advances from thematic details interface.
1173
     *
1174
     * @param int $thematic_advance_id
1175
     *
1176
     * @return int Affected rows
1177
     */
1178
    public function update_done_thematic_advances($thematic_advance_id)
1179
    {
1180
        $_course = api_get_course_info();
1181
        $thematic_data = self::get_thematic_list(null, api_get_course_id());
1182
        $thematic_advance_data = $this->get_thematic_advance_list(
1183
            null,
1184
            api_get_course_id(),
1185
            true
1186
        );
1187
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
1188
1189
        $affected_rows = 0;
1190
        $user_id = api_get_user_id();
1191
1192
        $all = [];
1193
        if (!empty($thematic_data)) {
1194
            foreach ($thematic_data as $thematic) {
1195
                if (!empty($thematic_advance_data[$thematic['id']])) {
1196
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1197
                        $all[] = $thematic_advance['id'];
1198
                    }
1199
                }
1200
            }
1201
        }
1202
        $error = null;
1203
        $a_thematic_advance_ids = [];
1204
        $course_id = api_get_course_int_id();
1205
        $sessionId = api_get_session_id();
1206
1207
        if (!empty($thematic_data)) {
1208
            foreach ($thematic_data as $thematic) {
1209
                $my_affected_rows = 0;
1210
                $thematic_id = $thematic['id'];
1211
                if (!empty($thematic_advance_data[$thematic['id']])) {
1212
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1213
                        $item_info = api_get_item_property_info(
1214
                            api_get_course_int_id(),
1215
                            'thematic_advance',
1216
                            $thematic_advance['id'],
1217
                            $sessionId
1218
                        );
1219
1220
                        if ($item_info['session_id'] == $sessionId) {
1221
                            $a_thematic_advance_ids[] = $thematic_advance['id'];
1222
                            // update done thematic for previous advances ((done_advance = 1))
1223
                            $upd = "UPDATE $table SET
1224
                                    done_advance = 1
1225
                                    WHERE c_id = $course_id AND id = ".$thematic_advance['id']." ";
1226
                            $result = Database::query($upd);
1227
                            $my_affected_rows = Database::affected_rows($result);
1228
                            $affected_rows += $my_affected_rows;
1229
                            //if ($my_affected_rows) {
1230
                            api_item_property_update(
1231
                                $_course,
1232
                                'thematic_advance',
1233
                                $thematic_advance['id'],
1234
                                "ThematicAdvanceDone",
1235
                                $user_id
1236
                            );
1237
                            //}
1238
                            if ($thematic_advance['id'] == $thematic_advance_id) {
1239
                                break 2;
1240
                            }
1241
                        }
1242
                    }
1243
                }
1244
            }
1245
        }
1246
1247
        // Update done thematic for others advances (done_advance = 0)
1248
        if (!empty($a_thematic_advance_ids) && count($a_thematic_advance_ids) > 0) {
1249
            $diff = array_diff($all, $a_thematic_advance_ids);
1250
            if (!empty($diff)) {
1251
                $upd = "UPDATE $table SET done_advance = 0
1252
                        WHERE c_id = $course_id AND id IN(".implode(',', $diff).") ";
1253
                Database::query($upd);
1254
            }
1255
1256
            // update item_property
1257
            $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
1258
            $sql = "SELECT ref FROM $tbl_item_property
1259
                    WHERE
1260
                        c_id = $course_id AND
1261
                        tool='thematic_advance' AND
1262
                        lastedit_type='ThematicAdvanceDone' AND
1263
                        session_id = $sessionId ";
1264
            // get all thematic advance done
1265
            $rs_thematic_done = Database::query($sql);
1266
            if (Database::num_rows($rs_thematic_done) > 0) {
1267
                while ($row_thematic_done = Database::fetch_array($rs_thematic_done)) {
1268
                    $ref = $row_thematic_done['ref'];
1269
                    if (in_array($ref, $a_thematic_advance_ids)) {
1270
                        continue;
1271
                    }
1272
                    // update items
1273
                    $sql = "UPDATE $tbl_item_property SET
1274
                                lastedit_date='".api_get_utc_datetime()."',
1275
                                lastedit_type='ThematicAdvanceUpdated',
1276
                                lastedit_user_id = $user_id
1277
                            WHERE
1278
                                c_id = $course_id AND
1279
                                tool='thematic_advance' AND
1280
                                ref=$ref AND
1281
                                session_id = $sessionId  ";
1282
                    Database::query($sql);
1283
                }
1284
            }
1285
        }
1286
1287
        return $affected_rows;
1288
    }
1289
1290
    /**
1291
     * Get last done thematic advance from thematic details interface.
1292
     *
1293
     * @return int Last done thematic advance id
1294
     */
1295
    public function get_last_done_thematic_advance()
1296
    {
1297
        $thematic_data = self::get_thematic_list();
1298
        $thematic_advance_data = $this->get_thematic_advance_list(
1299
            null,
1300
            api_get_course_id(),
1301
            true
1302
        );
1303
1304
        $a_thematic_advance_ids = [];
1305
        $last_done_advance_id = 0;
1306
        if (!empty($thematic_data)) {
1307
            foreach ($thematic_data as $thematic) {
1308
                if (!empty($thematic_advance_data[$thematic['id']])) {
1309
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1310
                        if ($thematic_advance['done_advance'] == 1) {
1311
                            $a_thematic_advance_ids[] = $thematic_advance['id'];
1312
                        }
1313
                    }
1314
                }
1315
            }
1316
        }
1317
        if (!empty($a_thematic_advance_ids)) {
1318
            $last_done_advance_id = array_pop($a_thematic_advance_ids);
1319
            $last_done_advance_id = intval($last_done_advance_id);
1320
        }
1321
1322
        return $last_done_advance_id;
1323
    }
1324
1325
    /**
1326
     * Get next thematic advance not done from thematic details interface.
1327
     *
1328
     * @param   int Offset (if you want to get an item that is not directly the next)
1329
     *
1330
     * @return int next thematic advance not done
1331
     */
1332
    public function get_next_thematic_advance_not_done($offset = 1)
1333
    {
1334
        $thematic_data = self::get_thematic_list();
1335
        $thematic_advance_data = $this->get_thematic_advance_list();
1336
        $a_thematic_advance_ids = [];
1337
        $next_advance_not_done = 0;
1338
        if (!empty($thematic_data)) {
1339
            foreach ($thematic_data as $thematic) {
1340
                if (!empty($thematic_advance_data[$thematic['id']])) {
1341
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1342
                        if ($thematic_advance['done_advance'] == 0) {
1343
                            $a_thematic_advance_ids[] = $thematic_advance['id'];
1344
                        }
1345
                    }
1346
                }
1347
            }
1348
        }
1349
1350
        if (!empty($a_thematic_advance_ids)) {
1351
            for ($i = 0; $i < $offset; $i++) {
1352
                $next_advance_not_done = array_shift($a_thematic_advance_ids);
1353
            }
1354
            $next_advance_not_done = intval($next_advance_not_done);
1355
        }
1356
1357
        return $next_advance_not_done;
1358
    }
1359
1360
    /**
1361
     * Get total average of thematic advances.
1362
     *
1363
     * @param string $course_code (optional)
1364
     * @param int    $session_id  (optional)
1365
     *
1366
     * @return float Average of thematic advances
1367
     */
1368
    public function get_total_average_of_thematic_advances($course_code = null, $session_id = null)
1369
    {
1370
        if (empty($course_code)) {
1371
            $course_code = api_get_course_id();
1372
        }
1373
        if (api_get_session_id()) {
1374
            $thematic_data = self::get_thematic_list(null, $course_code);
1375
        } else {
1376
            $thematic_data = self::get_thematic_list(null, $course_code, 0);
1377
        }
1378
        $new_thematic_data = [];
1379
        if (!empty($thematic_data)) {
1380
            foreach ($thematic_data as $item) {
1381
                $new_thematic_data[] = $item;
1382
            }
1383
            $thematic_data = $new_thematic_data;
1384
        }
1385
1386
        $a_average_of_advances_by_thematic = [];
1387
        $total_average = 0;
1388
        if (!empty($thematic_data)) {
1389
            foreach ($thematic_data as $thematic) {
1390
                $thematic_id = $thematic['id'];
1391
                $a_average_of_advances_by_thematic[$thematic_id] = $this->get_average_of_advances_by_thematic(
1392
                    $thematic_id,
1393
                    $course_code
1394
                );
1395
            }
1396
        }
1397
1398
        // calculate total average
1399
        if (!empty($a_average_of_advances_by_thematic)) {
1400
            $count_tematics = count($thematic_data);
1401
            $score = array_sum($a_average_of_advances_by_thematic);
1402
            $total_average = round(($score * 100) / ($count_tematics * 100));
1403
        }
1404
1405
        return $total_average;
1406
    }
1407
1408
    /**
1409
     * Get average of advances by thematic.
1410
     *
1411
     * @param int Thematic id
1412
     * @param string $course_code
1413
     *
1414
     * @return float Average of thematic advances
1415
     */
1416
    public function get_average_of_advances_by_thematic($thematic_id, $course_code = null)
1417
    {
1418
        $thematic_advance_data = $this->get_thematic_advance_by_thematic_id($thematic_id, $course_code);
1419
        $average = 0;
1420
        if (!empty($thematic_advance_data)) {
1421
            // get all done advances by thematic
1422
            $advances = [];
1423
            $count_done_advances = 0;
1424
            foreach ($thematic_advance_data as $thematic_advance) {
1425
                if ($thematic_advance['done_advance'] == 1) {
1426
                    $count_done_advances++;
1427
                }
1428
                $advances[] = $thematic_advance['done_advance'];
1429
            }
1430
            // calculate average by thematic
1431
            $count_total_advances = count($advances);
1432
            $average = round(($count_done_advances * 100) / $count_total_advances);
1433
        }
1434
1435
        return $average;
1436
    }
1437
1438
    /**
1439
     * set attributes for fields of thematic table.
1440
     *
1441
     * @param    int        Thematic id
1442
     * @param    string    Thematic title
1443
     * @param    string    Thematic content
1444
     * @param    int        Session id
1445
     */
1446
    public function set_thematic_attributes($id = null, $title = '', $content = '', $session_id = 0)
1447
    {
1448
        $this->thematic_id = $id;
1449
        $this->thematic_title = $title;
1450
        $this->thematic_content = $content;
1451
        $this->session_id = $session_id;
1452
    }
1453
1454
    /**
1455
     * set attributes for fields of thematic_plan table.
1456
     *
1457
     * @param    int        Thematic id
1458
     * @param    string    Thematic plan title
1459
     * @param    string    Thematic plan description
1460
     * @param    int        Thematic plan description type
1461
     */
1462
    public function set_thematic_plan_attributes(
1463
        $thematic_id = 0,
1464
        $title = '',
1465
        $description = '',
1466
        $description_type = 0
1467
    ) {
1468
        $this->thematic_id = $thematic_id;
1469
        $this->thematic_plan_title = $title;
1470
        $this->thematic_plan_description = $description;
1471
        $this->thematic_plan_description_type = $description_type;
1472
    }
1473
1474
    /**
1475
     * set attributes for fields of thematic_advance table.
1476
     *
1477
     * @param int $id Thematic advance id
1478
     * @param    int        Thematic id
1479
     * @param    int        Attendance id
1480
     * @param    string    Content
1481
     * @param    string    Date and time
1482
     * @param    int        Duration in hours
1483
     */
1484
    public function set_thematic_advance_attributes(
1485
        $id = null,
1486
        $thematic_id = 0,
1487
        $attendance_id = 0,
1488
        $content = '',
1489
        $start_date = null,
1490
        $duration = 0
1491
    ) {
1492
        $this->thematic_advance_id = $id;
1493
        $this->thematic_id = $thematic_id;
1494
        $this->attendance_id = $attendance_id;
1495
        $this->thematic_advance_content = $content;
1496
        $this->start_date = $start_date;
1497
        $this->duration = $duration;
1498
    }
1499
1500
    /**
1501
     * set thematic id.
1502
     *
1503
     * @param    int     Thematic id
1504
     */
1505
    public function set_thematic_id($thematic_id)
1506
    {
1507
        $this->thematic_id = $thematic_id;
1508
    }
1509
1510
    /**
1511
     * get thematic id.
1512
     *
1513
     * @return int
1514
     */
1515
    public function get_thematic_id()
1516
    {
1517
        return $this->thematic_id;
1518
    }
1519
1520
    /**
1521
     * Get thematic plan titles by default.
1522
     *
1523
     * @return array
1524
     */
1525
    public function get_default_thematic_plan_title()
1526
    {
1527
        $default_thematic_plan_titles = [];
1528
        $default_thematic_plan_titles[1] = get_lang('Objectives');
1529
        $default_thematic_plan_titles[2] = get_lang('SkillToAcquire');
1530
        $default_thematic_plan_titles[3] = get_lang('Methodology');
1531
        $default_thematic_plan_titles[4] = get_lang('Infrastructure');
1532
        $default_thematic_plan_titles[5] = get_lang('Assessment');
1533
        $default_thematic_plan_titles[6] = get_lang('Others');
1534
1535
        return $default_thematic_plan_titles;
1536
    }
1537
1538
    /**
1539
     * Get thematic plan icons by default.
1540
     *
1541
     * @return array
1542
     */
1543
    public function get_default_thematic_plan_icon()
1544
    {
1545
        $default_thematic_plan_icon = [];
1546
        $default_thematic_plan_icon[1] = 'icons/32/objective.png';
1547
        $default_thematic_plan_icon[2] = 'icons/32/skills.png';
1548
        $default_thematic_plan_icon[3] = 'icons/32/strategy.png';
1549
        $default_thematic_plan_icon[4] = 'icons/32/laptop.png';
1550
        $default_thematic_plan_icon[5] = 'icons/32/assessment.png';
1551
        $default_thematic_plan_icon[6] = 'icons/32/wizard.png';
1552
1553
        return $default_thematic_plan_icon;
1554
    }
1555
1556
    /**
1557
     * Get questions by default for help.
1558
     *
1559
     * @return array
1560
     */
1561
    public function get_default_question()
1562
    {
1563
        $question = [];
1564
        $question[1] = get_lang('ObjectivesQuestions');
1565
        $question[2] = get_lang('SkillToAcquireQuestions');
1566
        $question[3] = get_lang('MethodologyQuestions');
1567
        $question[4] = get_lang('InfrastructureQuestions');
1568
        $question[5] = get_lang('AssessmentQuestions');
1569
1570
        return $question;
1571
    }
1572
}
1573