Completed
Push — master ( 27e209...a08afa )
by Julito
186:04 queued 150:53
created

Thematic::set_thematic_plan_attributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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
 * @author Christian Fasanando <[email protected]>
9
 * @author Julio Montoya <[email protected]> SQL fixes
10
 * @package chamilo.course_progress
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;
0 ignored issues
show
introduced by
The private property $thematic_plan_id is not used, and could be removed.
Loading history...
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
     * @see SortableTable#get_total_number_of_items()
40
     */
41
    public function get_number_of_thematics()
42
    {
43
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
44
        $condition_session = '';
45
        if (!api_get_session_id()) {
46
            $condition_session = api_get_session_condition(0);
47
        }
48
        $course_id = api_get_course_int_id();
49
        $sql = "SELECT COUNT(id) AS total_number_of_items
50
                FROM $tbl_thematic
51
                WHERE c_id = $course_id AND active = 1 $condition_session ";
52
        $res = Database::query($sql);
53
        $obj = Database::fetch_object($res);
54
55
        return $obj->total_number_of_items;
56
    }
57
58
    /**
59
     * Get the thematics to display on the current page (fill the sortable-table)
60
     * @param   int     offset of first user to recover
61
     * @param   int     Number of users to get
62
     * @param   int     Column to sort on
63
     * @param   string  Order (ASC,DESC)
64
     * @return array
65
     * @see SortableTable#get_table_data($from)
66
     */
67
    public function get_thematic_data($from, $number_of_items, $column, $direction)
68
    {
69
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
70
        $condition_session = '';
71
        if (!api_get_session_id()) {
72
            $condition_session = api_get_session_condition(0);
73
        }
74
        $column = intval($column);
75
        $from = intval($from);
76
        $number_of_items = intval($number_of_items);
77
78
        if (!in_array($direction, ['ASC', 'DESC'])) {
79
            $direction = 'ASC';
80
        }
81
82
        $course_id = api_get_course_int_id();
83
84
        $sql = "SELECT id AS col0, title AS col1, display_order AS col2, session_id
85
                FROM $tbl_thematic
86
                WHERE c_id = $course_id AND active = 1 $condition_session
87
                ORDER BY col2
88
                LIMIT $from,$number_of_items ";
89
        $res = Database::query($sql);
90
91
        $thematics = [];
92
        $user_info = api_get_user_info(api_get_user_id());
93
        while ($thematic = Database::fetch_row($res)) {
94
            $session_star = '';
95
            if (api_get_session_id() == $thematic[3]) {
96
                $session_star = api_get_session_image(api_get_session_id(), $user_info['status']);
97
            }
98
            $thematic[1] = '<a href="index.php?'.api_get_cidreq().'&action=thematic_details&thematic_id='.$thematic[0].'">'.
99
                Security::remove_XSS($thematic[1], STUDENT).$session_star.'</a>';
100
            if (api_is_allowed_to_edit(null, true)) {
101
                $actions = '';
102
103
                if (api_get_session_id()) {
104
                    if (api_get_session_id() == $thematic[3]) {
105
                        $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_plan_list&thematic_id='.$thematic[0].'">'.
106
                            Display::return_icon('lesson_plan.png', get_lang('ThematicPlan'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
107
                        $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_list&thematic_id='.$thematic[0].'">'.
108
                            Display::return_icon('lesson_plan_calendar.png', get_lang('ThematicAdvance'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
109
110
                        $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_edit&thematic_id='.$thematic[0].'">'.
111
                            Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).'</a>';
112
                        $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToDelete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=thematic_delete&thematic_id='.$thematic[0].'">'.
113
                            Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
114
                    } else {
115
                        $actions .= Display::return_icon(
116
                            'lesson_plan_na.png',
117
                            get_lang('ThematicPlan'),
118
                            '',
119
                            ICON_SIZE_SMALL
120
                        ).'&nbsp;';
121
                        $actions .= Display::return_icon(
122
                            'lesson_plan_calendar_na.png',
123
                            get_lang('ThematicAdvance'),
124
                            '',
125
                            ICON_SIZE_SMALL
126
                        ).'&nbsp;';
127
                        $actions .= Display::return_icon('edit_na.png', get_lang('Edit'), '', ICON_SIZE_SMALL);
128
                        $actions .= Display::return_icon(
129
                            'delete_na.png',
130
                            get_lang('Delete'),
131
                            '',
132
                            ICON_SIZE_SMALL
133
                        ).'&nbsp;';
134
                        $actions .= Display::url(
135
                            Display::return_icon('cd.gif', get_lang('Copy')),
136
                            'index.php?'.api_get_cidreq().'&action=thematic_copy&thematic_id='.$thematic[0]
137
                        );
138
                    }
139
                } else {
140
                    $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_plan_list&thematic_id='.$thematic[0].'">'.
141
                        Display::return_icon('lesson_plan.png', get_lang('ThematicPlan'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
142
                    $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_list&thematic_id='.$thematic[0].'">'.
143
                        Display::return_icon('lesson_plan_calendar.png', get_lang('ThematicAdvance'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
144
145
                    if ($thematic[2] > 1) {
146
                        $actions .= '<a href="'.api_get_self().'?action=moveup&'.api_get_cidreq().'&thematic_id='.$thematic[0].'">'.
147
                            Display::return_icon('up.png', get_lang('Up'), '', ICON_SIZE_SMALL).'</a>';
148
                    } else {
149
                        $actions .= Display::return_icon('up_na.png', '&nbsp;', '', ICON_SIZE_SMALL);
150
                    }
151
                    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

151
                    if ($thematic[2] < self::/** @scrutinizer ignore-call */ get_max_thematic_item()) {
Loading history...
152
                        $actions .= '<a href="'.api_get_self().'?action=movedown&a'.api_get_cidreq().'&thematic_id='.$thematic[0].'">'.
153
                            Display::return_icon('down.png', get_lang('Down'), '', ICON_SIZE_SMALL).'</a>';
154
                    } else {
155
                        $actions .= Display::return_icon('down_na.png', '&nbsp;', '', ICON_SIZE_SMALL);
156
                    }
157
                    $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_edit&thematic_id='.$thematic[0].'">'.
158
                        Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).'</a>';
159
                    $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToDelete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=thematic_delete&thematic_id='.$thematic[0].'">'.
160
                        Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
161
                }
162
                $thematics[] = [$thematic[0], $thematic[1], $actions];
163
            }
164
        }
165
166
        return $thematics;
167
    }
168
169
    /**
170
     * Get the maximum display order of the thematic item
171
     * @param bool $use_session
172
     * @return int    Maximum display order
173
     */
174
    public function get_max_thematic_item($use_session = true)
175
    {
176
        // Database table definition
177
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
178
        $session_id   = api_get_session_id();
179
        if ($use_session) {
180
            $condition_session = api_get_session_condition($session_id);
181
        } else {
182
            $condition_session = '';
183
        }
184
        $course_id = api_get_course_int_id();
185
        $sql = "SELECT MAX(display_order)
186
                FROM $tbl_thematic
187
                WHERE c_id = $course_id AND active = 1 $condition_session";
188
        $rs = Database::query($sql);
189
        $row = Database::fetch_array($rs);
190
191
        return $row[0];
192
    }
193
194
    /**
195
     * Move a thematic
196
     *
197
     * @param string $direction (up, down)
198
     * @param int $thematic_id
199
     */
200
    public function move_thematic($direction, $thematic_id)
201
    {
202
        // Database table definition
203
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
204
205
        // sort direction
206
        if ($direction == 'up') {
207
            $sortorder = 'DESC';
208
        } else {
209
            $sortorder = 'ASC';
210
        }
211
        $course_id = api_get_course_int_id();
212
        $session_id = api_get_session_id();
213
        $condition_session = api_get_session_condition($session_id);
214
215
        $sql = "SELECT id, display_order
216
                FROM $tbl_thematic
217
                WHERE c_id = $course_id AND active = 1 $condition_session
218
                ORDER BY display_order $sortorder";
219
        $res = Database::query($sql);
220
        $found = false;
221
222
        // Variable definition
223
        $current_id = 0;
224
        $next_id = 0;
225
        while ($row = Database::fetch_array($res)) {
226
            if ($found && empty($next_id)) {
227
                $next_id = intval($row['id']);
228
                $next_display_order = intval($row['display_order']);
229
            }
230
231
            if ($row['id'] == $thematic_id) {
232
                $current_id = intval($thematic_id);
233
                $current_display_order = intval($row['display_order']);
234
                $found = true;
235
            }
236
        }
237
238
        // get last done thematic advance before move thematic list
239
        $last_done_thematic_advance = $this->get_last_done_thematic_advance();
240
241
        if (!empty($next_display_order) && !empty($current_id)) {
242
            $sql = "UPDATE $tbl_thematic SET display_order = $next_display_order
243
                    WHERE c_id = $course_id AND id = $current_id ";
244
            Database::query($sql);
245
        }
246
        if (!empty($current_display_order) && !empty($next_id)) {
247
            $sql = "UPDATE $tbl_thematic SET
248
                    display_order = $current_display_order
249
                    WHERE c_id = $course_id AND id = $next_id ";
250
            Database::query($sql);
251
        }
252
253
        // update done advances with de current thematic list
254
        $this->update_done_thematic_advances($last_done_thematic_advance);
255
    }
256
257
    /**
258
     * Get thematic list
259
     * @param integer $thematic_id Thematic id (optional), get list by id
260
     * @param string $course_code
261
     * @param integer $session_id
262
     * @return array    Thematic data
263
     */
264
    public static function get_thematic_list(
265
        $thematic_id = null,
266
        $course_code = null,
267
        $session_id = null
268
    ) {
269
        // set current course and session
270
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
271
        $course_info = api_get_course_info($course_code);
272
        $course_id = $course_info['real_id'];
273
274
        if (isset($session_id)) {
275
            $session_id = intval($session_id);
276
        } else {
277
            $session_id = api_get_session_id();
278
        }
279
280
        $data = [];
281
        if (isset($thematic_id)) {
282
            $thematic_id = intval($thematic_id);
283
            $condition = " WHERE id = $thematic_id AND active = 1 ";
284
        } else {
285
            if (empty($session_id)) {
286
                $condition_session = api_get_session_condition(0);
287
            } else {
288
                $condition_session = api_get_session_condition($session_id, true, true);
289
            }
290
            $condition = " WHERE active = 1 $condition_session ";
291
        }
292
        $sql = "SELECT * 
293
                FROM $tbl_thematic $condition AND c_id = $course_id
294
                ORDER BY display_order ";
295
296
        $res = Database::query($sql);
297
        if (Database::num_rows($res) > 0) {
298
            if (!empty($thematic_id)) {
299
                $data = Database::fetch_array($res, 'ASSOC');
300
            } else {
301
                while ($row = Database::fetch_array($res, 'ASSOC')) {
302
                    $data[$row['id']] = $row;
303
                }
304
            }
305
        }
306
307
        return $data;
308
    }
309
310
    /**
311
     * Insert or update a thematic
312
     * @return int last thematic id
313
     */
314
    public function thematic_save()
315
    {
316
        $_course = api_get_course_info();
317
        // definition database table
318
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
319
320
        // protect data
321
        $id = intval($this->thematic_id);
322
        $title = $this->thematic_title;
323
        $content = $this->thematic_content;
324
        $session_id = intval($this->session_id);
325
        $user_id = api_get_user_id();
326
327
        // get the maximum display order of all the glossary items
328
        $max_thematic_item = $this->get_max_thematic_item(false);
329
330
        if (empty($id)) {
331
            // insert
332
            $params = [
333
                'c_id' => $this->course_int_id,
334
                'title' => $title,
335
                'content' => $content,
336
                'active' => 1,
337
                'display_order' => intval($max_thematic_item) + 1,
338
                'session_id' => $session_id
339
            ];
340
            $last_id = Database::insert($tbl_thematic, $params);
341
            if ($last_id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $last_id of type integer|false is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
342
                $sql = "UPDATE $tbl_thematic SET id = iid WHERE iid = $last_id";
343
                Database::query($sql);
344
                api_item_property_update(
345
                    $_course,
346
                    'thematic',
347
                    $last_id,
348
                    "ThematicAdded",
349
                    $user_id
350
                );
351
            }
352
        } else {
353
            // Update
354
            $params = [
355
                'title' => $title,
356
                'content' => $content,
357
                'session_id' => $session_id
358
            ];
359
360
            Database::update(
361
                $tbl_thematic,
362
                $params,
363
                ['id  = ? AND c_id = ?' => [$id, $this->course_int_id]]
364
            );
365
366
            $last_id = $id;
367
368
            // save inside item property table
369
            api_item_property_update(
370
                $_course,
371
                'thematic',
372
                $last_id,
373
                "ThematicUpdated",
374
                $user_id
375
            );
376
        }
377
378
        return $last_id;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $last_id could also return false which is incompatible with the documented return type integer. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
379
    }
380
381
    /**
382
     * Delete logically (set active field to 0) a thematic
383
     * @param int|array One or many thematic ids
384
     * @return int            Affected rows
385
     */
386
    public function delete($thematic_id)
387
    {
388
        $_course = api_get_course_info();
389
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
390
        $affected_rows = 0;
391
        $user_id = api_get_user_id();
392
        $course_id = api_get_course_int_id();
393
394
        if (is_array($thematic_id)) {
395
            foreach ($thematic_id as $id) {
396
                $id = intval($id);
397
                $sql = "UPDATE $tbl_thematic SET active = 0
398
                        WHERE c_id = $course_id AND id = $id";
399
                $result = Database::query($sql);
400
                $affected_rows += Database::affected_rows($result);
401
                if (!empty($affected_rows)) {
402
                    // update row item property table
403
                    api_item_property_update(
404
                        $_course,
405
                        'thematic',
406
                        $id,
407
                        "ThematicDeleted",
408
                        $user_id
409
                    );
410
                }
411
            }
412
        } else {
413
            $thematic_id = intval($thematic_id);
414
            $sql = "UPDATE $tbl_thematic SET active = 0
415
                    WHERE c_id = $course_id AND id = $thematic_id";
416
            $result = Database::query($sql);
417
            $affected_rows = Database::affected_rows($result);
418
            if (!empty($affected_rows)) {
419
                // update row item property table
420
                api_item_property_update(
421
                    $_course,
422
                    'thematic',
423
                    $thematic_id,
424
                    "ThematicDeleted",
425
                    $user_id
426
                );
427
            }
428
        }
429
430
        return $affected_rows;
431
    }
432
433
    /**
434
     * @param int $thematic_id
435
     */
436
    public function copy($thematic_id)
437
    {
438
        $thematic = self::get_thematic_list($thematic_id, api_get_course_id(), 0);
439
        $thematic_copy = new Thematic();
440
        $thematic_copy->set_thematic_attributes(
441
            '',
442
            $thematic['title'].' - '.get_lang('Copy'),
443
            $thematic['content'],
444
            api_get_session_id()
445
        );
446
447
        $new_thematic_id = $thematic_copy->thematic_save();
448
        if (!empty($new_thematic_id)) {
449
            $thematic_advanced = self::get_thematic_advance_by_thematic_id($thematic_id);
0 ignored issues
show
Bug Best Practice introduced by
The method Thematic::get_thematic_advance_by_thematic_id() is not static, but was called statically. ( Ignorable by Annotation )

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

449
            /** @scrutinizer ignore-call */ 
450
            $thematic_advanced = self::get_thematic_advance_by_thematic_id($thematic_id);
Loading history...
450
            if (!empty($thematic_advanced)) {
451
                foreach ($thematic_advanced as $item) {
452
                    $thematic = new Thematic();
453
                    $thematic->set_thematic_advance_attributes(
454
                        0,
455
                        $new_thematic_id,
456
                        0,
457
                        $item['content'],
458
                        $item['start_date'],
459
                        $item['duration']
460
                    );
461
                    $thematic->thematic_advance_save();
462
                }
463
            }
464
            $thematic_plan = self::get_thematic_plan_data($thematic_id);
0 ignored issues
show
Bug Best Practice introduced by
The method Thematic::get_thematic_plan_data() is not static, but was called statically. ( Ignorable by Annotation )

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

464
            /** @scrutinizer ignore-call */ 
465
            $thematic_plan = self::get_thematic_plan_data($thematic_id);
Loading history...
465
            if (!empty($thematic_plan)) {
466
                foreach ($thematic_plan as $item) {
467
                    $thematic = new Thematic();
468
                    $thematic->set_thematic_plan_attributes(
469
                        $new_thematic_id,
470
                        $item['title'],
471
                        $item['description'],
472
                        $item['description_type']
473
                    );
474
                    $thematic->thematic_plan_save();
475
                }
476
            }
477
        }
478
    }
479
480
    /**
481
     * Get the total number of thematic advance inside current course
482
     * @see SortableTable#get_total_number_of_items()
483
     */
484
    public static function get_number_of_thematic_advances()
485
    {
486
        global $thematic_id;
487
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
488
        $course_id = api_get_course_int_id();
489
490
        $sql = "SELECT COUNT(id) AS total_number_of_items 
491
                FROM $table
492
                WHERE c_id = $course_id AND thematic_id = $thematic_id ";
493
        $res = Database::query($sql);
494
        $obj = Database::fetch_object($res);
495
496
        return $obj->total_number_of_items;
497
    }
498
499
    /**
500
     * Get the thematic advances to display on the current page (fill the sortable-table)
501
     * @param   int     offset of first user to recover
502
     * @param   int     Number of users to get
503
     * @param   int     Column to sort on
504
     * @param   string  Order (ASC,DESC)
505
     * @return array
506
     * @see SortableTable#get_table_data($from)
507
     */
508
    public static function get_thematic_advance_data($from, $number_of_items, $column, $direction)
509
    {
510
        global $thematic_id;
511
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
512
        $column = intval($column);
513
        $from = intval($from);
514
        $number_of_items = intval($number_of_items);
515
        if (!in_array($direction, ['ASC', 'DESC'])) {
516
            $direction = 'ASC';
517
        }
518
        $data = [];
519
        $course_id = api_get_course_int_id();
520
        if (api_is_allowed_to_edit(null, true)) {
521
            $sql = "SELECT id AS col0, start_date AS col1, duration AS col2, content AS col3
522
                    FROM $table
523
                    WHERE c_id = $course_id AND thematic_id = $thematic_id
524
                    ORDER BY col$column $direction
525
                    LIMIT $from,$number_of_items ";
526
527
            $list = api_get_item_property_by_tool(
528
                'thematic_advance',
529
                api_get_course_id(),
530
                api_get_session_id()
531
            );
532
533
            $elements = [];
534
            foreach ($list as $value) {
535
                $elements[] = $value['ref'];
536
            }
537
538
            $res = Database::query($sql);
539
            $i = 1;
540
            while ($thematic_advance = Database::fetch_row($res)) {
541
                if (in_array($thematic_advance[0], $elements)) {
542
                    $thematic_advance[1] = api_get_local_time($thematic_advance[1]);
543
                    $thematic_advance[1] = api_format_date($thematic_advance[1], DATE_TIME_FORMAT_LONG);
544
                    $actions  = '';
545
                    $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_edit&thematic_id='.$thematic_id.'&thematic_advance_id='.$thematic_advance[0].'">'.Display::return_icon('edit.png', get_lang('Edit'), '', 22).'</a>';
546
                    $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].'">'.Display::return_icon('delete.png', get_lang('Delete'), '', 22).'</a></center>';
547
                    $data[] = [$i, $thematic_advance[1], $thematic_advance[2], $thematic_advance[3], $actions];
548
                    $i++;
549
                }
550
            }
551
        }
552
        return $data;
553
    }
554
555
    /**
556
     * get thematic advance data by thematic id
557
     * @param    int $thematic_id
558
     * @param    string $course_code    Course code (optional)
559
     * @return    array    data
560
     */
561
    public function get_thematic_advance_by_thematic_id($thematic_id, $course_code = null)
562
    {
563
        $course_info = api_get_course_info($course_code);
564
        $course_id = $course_info['real_id'];
565
566
        // set current course
567
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
568
        $thematic_id = intval($thematic_id);
569
        $data = [];
570
        $sql = "SELECT * FROM $table
571
                WHERE c_id = $course_id AND thematic_id = $thematic_id ";
572
573
        $elements = [];
574
        $list = api_get_item_property_by_tool(
575
            'thematic_advance',
576
            $course_info['code'],
577
            api_get_session_id()
578
        );
579
        foreach ($list as $value) {
580
            $elements[] = $value['ref'];
581
        }
582
583
        $res = Database::query($sql);
584
        if (Database::num_rows($res) > 0) {
585
            while ($row = Database::fetch_array($res, 'ASSOC')) {
586
                if (in_array($row['id'], $elements)) {
587
                    $data[] = $row;
588
                }
589
            }
590
        }
591
592
        return $data;
593
    }
594
595
    /**
596
     * @param array $data
597
     * @return array
598
     */
599
    public function get_thematic_advance_div($data)
600
    {
601
        $return_array = [];
602
        $uinfo = api_get_user_info();
603
604
        foreach ($data as $thematic_id => $thematic_advance_data) {
605
            foreach ($thematic_advance_data as $key => $thematic_advance) {
606
                $session_star = '';
607
                if (api_is_allowed_to_edit(null, true)) {
608
                    if ($thematic_advance['session_id'] != 0) {
609
                        $session_star = api_get_session_image(api_get_session_id(), $uinfo['status']);
610
                    }
611
                }
612
                // DATE_TIME_FORMAT_LONG
613
                $thematic_advance_item = '<div><strong>'.api_convert_and_format_date($thematic_advance['start_date'], DATE_TIME_FORMAT_LONG).$session_star.'</strong></div>';
614
                //				$thematic_advance_item .= '<div>'.get_lang('DurationInHours').' : '.$thematic_advance['duration'].'</div>';
615
                $thematic_advance_item .= '<div>'.$thematic_advance['duration'].' '.get_lang('HourShort').'</div>';
616
                $thematic_advance_item .= '<div>'.Security::remove_XSS($thematic_advance['content'], STUDENT).'</div>';
617
                $return_array[$thematic_id][$thematic_advance['id']] = $thematic_advance_item;
618
            }
619
        }
620
        return $return_array;
621
    }
622
623
    /**
624
     * @param array $data
625
     * @return array
626
     */
627
    public function get_thematic_plan_array($data)
628
    {
629
        $final_return = [];
630
        $uinfo = api_get_user_info();
631
632
        foreach ($data as $thematic_id => $thematic_plan_data) {
633
            $new_thematic_plan_data = [];
634
            foreach ($thematic_plan_data as $thematic_item) {
635
                $thematic_simple_list[] = $thematic_item['description_type'];
636
                $new_thematic_plan_data[$thematic_item['description_type']] = $thematic_item;
637
            }
638
639
            if (!empty($thematic_simple_list)) {
640
                foreach ($thematic_simple_list as $item) {
641
                    $default_thematic_plan_title[$item] = $new_thematic_plan_data[$item]['title'];
642
                }
643
            }
644
645
            $session_star = '';
646
            $return = [];
647
            if (!empty($default_thematic_plan_title)) {
648
                foreach ($default_thematic_plan_title as $id => $title) {
649
                    //avoid others
650
                    if ($title == 'Others' && empty($data[$thematic_id][$id]['description'])) {
651
                        continue;
652
                    }
653
                    if (!empty($data[$thematic_id][$id]['title']) &&
654
                        !empty($data[$thematic_id][$id]['description'])
655
                    ) {
656
                        if (api_is_allowed_to_edit(null, true)) {
657
                            if ($data[$thematic_id][$id]['session_id'] != 0) {
658
                                $session_star = api_get_session_image(api_get_session_id(), $uinfo['status']);
659
                            }
660
                        }
661
662
                        $return[$id]['title'] = Security::remove_XSS($data[$thematic_id][$id]['title'], STUDENT).$session_star;
663
                        $return[$id]['description'] = Security::remove_XSS($data[$thematic_id][$id]['description'], STUDENT);
664
                    }
665
                }
666
            }
667
            $final_return[$thematic_id] = $return;
668
        }
669
670
        return $final_return;
671
    }
672
673
    /**
674
     * Get thematic advance list
675
     * @param int $thematic_advance_id Thematic advance id (optional), get data by thematic advance list
676
     * @param string $course_code Course code (optional)
677
     * @param bool $force_session_id Force to have a session id
678
     * @return array $data
679
     */
680
    public function get_thematic_advance_list(
681
        $thematic_advance_id = null,
682
        $course_code = null,
683
        $force_session_id = false
684
    ) {
685
        $course_info = api_get_course_info($course_code);
686
        $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
687
        $data = [];
688
        $condition = '';
689
        if (isset($thematic_advance_id)) {
690
            $thematic_advance_id = intval($thematic_advance_id);
691
            $condition = " AND a.id = $thematic_advance_id ";
692
        }
693
694
        $course_id = $course_info['real_id'];
695
696
        $sql = "SELECT * FROM $tbl_thematic_advance a
697
                WHERE c_id = $course_id $condition
698
                ORDER BY start_date ";
699
700
        $elements = [];
701
        if ($force_session_id) {
702
            $list = api_get_item_property_by_tool(
703
                'thematic_advance',
704
                $course_info['code'],
705
                api_get_session_id()
706
            );
707
            foreach ($list as $value) {
708
                $elements[$value['ref']] = $value;
709
            }
710
        }
711
712
        $res = Database::query($sql);
713
        if (Database::num_rows($res) > 0) {
714
            if (!empty($thematic_advance_id)) {
715
                $data = Database::fetch_array($res);
716
            } else {
717
                // group all data group by thematic id
718
                $tmp = [];
719
                while ($row = Database::fetch_array($res, 'ASSOC')) {
720
                    $tmp[] = $row['thematic_id'];
721
                    if (in_array($row['thematic_id'], $tmp)) {
722
                        if ($force_session_id) {
723
                            if (in_array($row['id'], array_keys($elements))) {
724
                                $row['session_id'] = $elements[$row['id']]['session_id'];
725
                                $data[$row['thematic_id']][$row['id']] = $row;
726
                            }
727
                        } else {
728
                            $data[$row['thematic_id']][$row['id']] = $row;
729
                        }
730
                    }
731
                }
732
            }
733
        }
734
735
        return $data;
736
    }
737
738
    /**
739
     * insert or update a thematic advance
740
     * @todo problem
741
     * @return int last thematic advance id
742
     */
743
    public function thematic_advance_save()
744
    {
745
        $_course = api_get_course_info();
746
        // definition database table
747
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
748
749
        // protect data
750
        $id = intval($this->thematic_advance_id);
751
        $thematic_id = intval($this->thematic_id);
752
        $attendance_id = intval($this->attendance_id);
753
        $content = $this->thematic_advance_content;
754
        $start_date = $this->start_date;
755
        $duration = intval($this->duration);
756
        $user_id = api_get_user_id();
757
758
        $last_id = null;
759
        if (empty($id)) {
760
            // Insert
761
            $params = [
762
                'c_id' => $this->course_int_id,
763
                'thematic_id' => $thematic_id,
764
                'attendance_id' => $attendance_id,
765
                'content' => $content,
766
                'start_date' => api_get_utc_datetime($start_date),
767
                'duration' => $duration,
768
                'done_advance' => 0
769
            ];
770
            $last_id = Database::insert($table, $params);
771
772
            if ($last_id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $last_id of type integer|false is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
773
                $sql = "UPDATE $table SET id = iid WHERE iid = $last_id";
774
                Database::query($sql);
775
776
                api_item_property_update(
777
                    $_course,
778
                    'thematic_advance',
779
                    $last_id,
780
                    'ThematicAdvanceAdded',
781
                    $user_id
782
                );
783
            }
784
        } else {
785
            $params = [
786
                'thematic_id' => $thematic_id,
787
                'attendance_id' => $attendance_id,
788
                'content' => $content,
789
                'start_date' => api_get_utc_datetime($start_date),
790
                'duration' => $duration
791
            ];
792
793
            Database::update(
794
                $table,
795
                $params,
796
                ['id = ? AND c_id = ?' => [$id, $this->course_int_id]]
797
            );
798
799
            api_item_property_update(
800
                $_course,
801
                'thematic_advance',
802
                $id,
803
                'ThematicAdvanceUpdated',
804
                $user_id
805
            );
806
        }
807
808
        return $last_id;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $last_id could also return false which is incompatible with the documented return type integer. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
809
    }
810
811
    /**
812
     * delete  thematic advance
813
     * @param int        Thematic advance id
814
     * @param integer $id
815
     * @return int        Affected rows
816
     */
817
    public function thematic_advance_destroy($id)
818
    {
819
        $_course = api_get_course_info();
820
        $course_id = api_get_course_int_id();
821
822
        // definition database table
823
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
824
825
        // protect data
826
        $id = intval($id);
827
        $user_id = api_get_user_id();
828
829
        $sql = "DELETE FROM $table
830
                WHERE c_id = $course_id AND id = $id ";
831
        $result = Database::query($sql);
832
        $affected_rows = Database::affected_rows($result);
833
        if ($affected_rows) {
834
            api_item_property_update(
835
                $_course,
836
                'thematic_advance',
837
                $id,
838
                'ThematicAdvanceDeleted',
839
                $user_id
840
            );
841
        }
842
843
        return $affected_rows;
844
    }
845
846
    /**
847
     * get thematic plan data
848
     * @param int Thematic id (optional), get data by thematic id
849
     * @param int Thematic plan description type (optional), get data by description type
850
     * @return array Thematic plan data
851
     */
852
    public function get_thematic_plan_data($thematic_id = null, $description_type = null)
853
    {
854
        // definition database table
855
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
856
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
857
        $course_id = api_get_course_int_id();
858
859
        $data = [];
860
        $condition = '';
861
        if (isset($thematic_id)) {
862
            $thematic_id = intval($thematic_id);
863
            $condition .= " AND thematic_id = $thematic_id ";
864
        }
865
        if (isset($description_type)) {
866
            $description_type = intval($description_type);
867
            $condition .= " AND description_type = $description_type ";
868
        }
869
870
        $items_from_course = api_get_item_property_by_tool(
871
            'thematic_plan',
872
            api_get_course_id(),
873
            0
874
        );
875
        $items_from_session = api_get_item_property_by_tool(
876
            'thematic_plan',
877
            api_get_course_id(),
878
            api_get_session_id()
879
        );
880
881
        $thematic_plan_complete_list = [];
882
        $thematic_plan_id_list = [];
883
884
        if (!empty($items_from_course)) {
885
            foreach ($items_from_course as $item) {
886
                $thematic_plan_id_list[] = $item['ref'];
887
                $thematic_plan_complete_list[$item['ref']] = $item;
888
            }
889
        }
890
891
        if (!empty($items_from_session)) {
892
            foreach ($items_from_session as $item) {
893
                $thematic_plan_id_list[] = $item['ref'];
894
                $thematic_plan_complete_list[$item['ref']] = $item;
895
            }
896
        }
897
        if (!empty($thematic_plan_id_list)) {
898
            $sql = "SELECT
899
                        tp.id, thematic_id, tp.title, description, description_type, t.session_id
900
                    FROM $tbl_thematic_plan tp
901
                    INNER JOIN $tbl_thematic t 
902
                    ON (t.id = tp.thematic_id AND t.c_id = tp.c_id)
903
                    WHERE
904
                        t.c_id = $course_id AND
905
                        tp.c_id = $course_id
906
                        $condition AND
907
                        tp.id IN (".implode(', ', $thematic_plan_id_list).") ";
908
909
            $rs = Database::query($sql);
910
911
            if (Database::num_rows($rs)) {
912
                if (!isset($thematic_id) && !isset($description_type)) {
913
                    // group all data group by thematic id
914
                    $tmp = [];
915
                    while ($row = Database::fetch_array($rs, 'ASSOC')) {
916
                        $tmp[] = $row['thematic_id'];
917
                        if (in_array($row['thematic_id'], $tmp)) {
918
                            $row['session_id'] = $thematic_plan_complete_list[$row['id']];
919
                            $data[$row['thematic_id']][$row['description_type']] = $row;
920
                        }
921
                    }
922
                } else {
923
                    while ($row = Database::fetch_array($rs, 'ASSOC')) {
924
                        $row['session_id'] = $thematic_plan_complete_list[$row['id']];
925
                        $data[] = $row;
926
                    }
927
                }
928
            }
929
        }
930
931
        return $data;
932
    }
933
934
    /**
935
     * insert or update a thematic plan
936
     * @return int affected rows
937
     */
938
    public function thematic_plan_save()
939
    {
940
        $_course = api_get_course_info();
941
        // definition database table
942
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
943
944
        // protect data
945
        $thematic_id = intval($this->thematic_id);
946
        $title = $this->thematic_plan_title;
947
        $description = $this->thematic_plan_description;
948
        $description_type = intval($this->thematic_plan_description_type);
949
        $user_id = api_get_user_id();
950
        $course_id = api_get_course_int_id();
951
        $list = api_get_item_property_by_tool(
952
            'thematic_plan',
953
            api_get_course_id(),
954
            api_get_session_id()
955
        );
956
957
        $elements_to_show = [];
958
        foreach ($list as $value) {
959
            $elements_to_show[] = $value['ref'];
960
        }
961
        $condition = '';
962
        if (!empty($elements_to_show)) {
963
            $condition = "AND id IN (".implode(',', $elements_to_show).") ";
964
        }
965
        // check thematic plan type already exists
966
        $sql = "SELECT id FROM $tbl_thematic_plan
967
                WHERE
968
                    c_id = $course_id AND
969
                    thematic_id = $thematic_id AND
970
                    description_type = '$description_type'";
971
        $rs = Database::query($sql);
972
973
        $affected_rows = 0;
974
        if (Database::num_rows($rs) > 0) {
975
            $row_thematic_plan = Database::fetch_array($rs);
976
            $thematic_plan_id = $row_thematic_plan['id'];
977
            $update = false;
978
            if (in_array($thematic_plan_id, $elements_to_show)) {
979
                $update = true;
980
            }
981
982
            if ($update) {
983
                // update
984
                $params = [
985
                    'title' => $title,
986
                    'description' => $description
987
                ];
988
                Database::update(
989
                    $tbl_thematic_plan,
990
                    $params,
991
                    ['c_id = ? AND id = ?' => [$course_id, $thematic_plan_id]]
992
                );
993
994
                api_item_property_update(
995
                    $_course,
996
                    'thematic_plan',
997
                    $thematic_plan_id,
998
                    'ThematicPlanUpdated',
999
                    $user_id
1000
                );
1001
            } else {
1002
                // insert
1003
                $params = [
1004
                    'c_id' => $this->course_int_id,
1005
                    'thematic_id' => $thematic_id,
1006
                    'title' => $title,
1007
                    'description' => $description,
1008
                    'description_type' => $description_type
1009
                ];
1010
                $last_id = Database::insert($tbl_thematic_plan, $params);
1011
                if ($last_id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $last_id of type integer|false is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
1012
                    $sql = "UPDATE $tbl_thematic_plan SET id = iid WHERE iid = $last_id";
1013
                    Database::query($sql);
1014
                    api_item_property_update(
1015
                        $_course,
1016
                        'thematic_plan',
1017
                        $last_id,
1018
                        'ThematicPlanAdded',
1019
                        $user_id
1020
                    );
1021
                }
1022
            }
1023
        } else {
1024
            // insert
1025
            $params = [
1026
                'c_id' => $this->course_int_id,
1027
                'thematic_id' => $thematic_id,
1028
                'title' => $title,
1029
                'description' => $description,
1030
                'description_type' => $description_type
1031
            ];
1032
            $last_id = Database::insert($tbl_thematic_plan, $params);
1033
1034
            if ($last_id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $last_id of type integer|false is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
1035
                $sql = "UPDATE $tbl_thematic_plan SET id = iid WHERE iid = $last_id";
1036
                Database::query($sql);
1037
                api_item_property_update(
1038
                    $_course,
1039
                    'thematic_plan',
1040
                    $last_id,
1041
                    'ThematicPlanAdded',
1042
                    $user_id
1043
                );
1044
            }
1045
        }
1046
1047
        return $affected_rows;
1048
    }
1049
1050
    /**
1051
     * Delete a thematic plan description
1052
     * @param int $thematic_id Thematic id
1053
     * @param int $description_type Description type
1054
     * @return int        Affected rows
1055
     */
1056
    public function thematic_plan_destroy($thematic_id, $description_type)
1057
    {
1058
        $_course = api_get_course_info();
1059
        // definition database table
1060
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
1061
1062
        // protect data
1063
        $thematic_id = intval($thematic_id);
1064
        $description_type = intval($description_type);
1065
        $user_id = api_get_user_id();
1066
        $course_info = api_get_course_info();
1067
        $course_id = $course_info['real_id'];
1068
1069
        // get thematic plan id
1070
        $thematic_plan_data = $this->get_thematic_plan_data($thematic_id, $description_type);
1071
        $thematic_plan_id = $thematic_plan_data[0]['id'];
1072
1073
        // delete
1074
        $sql = "DELETE FROM $tbl_thematic_plan
1075
                WHERE
1076
                    c_id = $course_id AND
1077
                    thematic_id = $thematic_id AND
1078
                    description_type = $description_type ";
1079
        $result = Database::query($sql);
1080
        $affected_rows = Database::affected_rows($result);
1081
        if ($affected_rows) {
1082
            api_item_property_update(
1083
                $_course,
1084
                'thematic_plan',
1085
                $thematic_plan_id,
1086
                'ThematicPlanDeleted',
1087
                $user_id
1088
            );
1089
        }
1090
        return $affected_rows;
1091
    }
1092
1093
    /**
1094
     * Get next description type for a new thematic plan description (option 'others')
1095
     * @param int $thematic_id Thematic id
1096
     * @return int New Description type
1097
     */
1098
    public function get_next_description_type($thematic_id)
1099
    {
1100
        // definition database table
1101
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
1102
1103
        // protect data
1104
        $thematic_id = intval($thematic_id);
1105
        $course_id = api_get_course_int_id();
1106
1107
        $sql = "SELECT MAX(description_type) as max
1108
                FROM $tbl_thematic_plan
1109
                WHERE
1110
                    c_id = $course_id AND
1111
                    thematic_id = $thematic_id AND
1112
                    description_type >= ".ADD_THEMATIC_PLAN;
1113
        $rs = Database::query($sql);
1114
        $row = Database::fetch_array($rs);
1115
        $last_description_type = $row['max'];
1116
1117
        if (isset($last_description_type)) {
1118
            $next_description_type = $last_description_type + 1;
1119
        } else {
1120
            $next_description_type = ADD_THEMATIC_PLAN;
1121
        }
1122
1123
        return $next_description_type;
1124
    }
1125
1126
    /**
1127
     * update done thematic advances from thematic details interface
1128
     * @param int $thematic_advance_id
1129
     * @return int Affected rows
1130
     */
1131
    public function update_done_thematic_advances($thematic_advance_id)
1132
    {
1133
        $_course = api_get_course_info();
1134
        $thematic_data = self::get_thematic_list(null, api_get_course_id());
1135
        $thematic_advance_data = $this->get_thematic_advance_list(
1136
            null,
1137
            api_get_course_id(),
1138
            true
1139
        );
1140
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
1141
1142
        $affected_rows = 0;
1143
        $user_id       = api_get_user_id();
1144
1145
        $all = [];
1146
        if (!empty($thematic_data)) {
1147
            foreach ($thematic_data as $thematic) {
1148
                $thematic_id = $thematic['id'];
1149
                if (!empty($thematic_advance_data[$thematic['id']])) {
1150
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1151
                        $all[] = $thematic_advance['id'];
1152
                    }
1153
                }
1154
            }
1155
        }
1156
        $error = null;
1157
        $a_thematic_advance_ids = [];
1158
        $course_id = api_get_course_int_id();
1159
        $sessionId = api_get_session_id();
1160
1161
        if (!empty($thematic_data)) {
1162
            foreach ($thematic_data as $thematic) {
1163
                $my_affected_rows = 0;
1164
                $thematic_id = $thematic['id'];
1165
                if (!empty($thematic_advance_data[$thematic['id']])) {
1166
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1167
                        $item_info = api_get_item_property_info(
1168
                            api_get_course_int_id(),
1169
                            'thematic_advance',
1170
                            $thematic_advance['id'],
1171
                            $sessionId
1172
                        );
1173
1174
                        if ($item_info['session_id'] == $sessionId) {
1175
                            $a_thematic_advance_ids[] = $thematic_advance['id'];
1176
                            // update done thematic for previous advances ((done_advance = 1))
1177
                            $upd = "UPDATE $table SET
1178
                                    done_advance = 1
1179
                                    WHERE c_id = $course_id AND id = ".$thematic_advance['id']." ";
1180
                            $result = Database::query($upd);
1181
                            $my_affected_rows = Database::affected_rows($result);
1182
                            $affected_rows += $my_affected_rows;
1183
                            //if ($my_affected_rows) {
1184
                            api_item_property_update(
1185
                                $_course,
1186
                                'thematic_advance',
1187
                                $thematic_advance['id'],
1188
                                "ThematicAdvanceDone",
1189
                                $user_id
1190
                            );
1191
                            //}
1192
                            if ($thematic_advance['id'] == $thematic_advance_id) {
1193
                                break 2;
1194
                            }
1195
                        }
1196
                    }
1197
                }
1198
            }
1199
        }
1200
1201
        // Update done thematic for others advances (done_advance = 0)
1202
        if (!empty($a_thematic_advance_ids) && count($a_thematic_advance_ids) > 0) {
1203
            $diff = array_diff($all, $a_thematic_advance_ids);
1204
            if (!empty($diff)) {
1205
                $upd = "UPDATE $table SET done_advance = 0
1206
                        WHERE c_id = $course_id AND id IN(".implode(',', $diff).") ";
1207
                Database::query($upd);
1208
            }
1209
1210
            // update item_property
1211
            $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
1212
            $sql = "SELECT ref FROM $tbl_item_property
1213
                    WHERE
1214
                        c_id = $course_id AND
1215
                        tool='thematic_advance' AND
1216
                        lastedit_type='ThematicAdvanceDone' AND
1217
                        session_id = $sessionId ";
1218
            // get all thematic advance done
1219
            $rs_thematic_done = Database::query($sql);
1220
            if (Database::num_rows($rs_thematic_done) > 0) {
1221
                while ($row_thematic_done = Database::fetch_array($rs_thematic_done)) {
1222
                    $ref = $row_thematic_done['ref'];
1223
                    if (in_array($ref, $a_thematic_advance_ids)) {
1224
                        continue;
1225
                    }
1226
                    // update items
1227
                    $sql = "UPDATE $tbl_item_property SET
1228
                                lastedit_date='".api_get_utc_datetime()."',
1229
                                lastedit_type='ThematicAdvanceUpdated',
1230
                                lastedit_user_id = $user_id
1231
                            WHERE
1232
                                c_id = $course_id AND
1233
                                tool='thematic_advance' AND
1234
                                ref=$ref AND
1235
                                session_id = $sessionId  ";
1236
                    Database::query($sql);
1237
                }
1238
            }
1239
        }
1240
1241
        return $affected_rows;
1242
    }
1243
1244
    /**
1245
     * Get last done thematic advance from thematic details interface
1246
     * @return int Last done thematic advance id
1247
     */
1248
    public function get_last_done_thematic_advance()
1249
    {
1250
        $thematic_data = self::get_thematic_list();
1251
        $thematic_advance_data = $this->get_thematic_advance_list(
1252
            null,
1253
            api_get_course_id(),
1254
            true
1255
        );
1256
1257
        $a_thematic_advance_ids = [];
1258
        $last_done_advance_id = 0;
1259
        if (!empty($thematic_data)) {
1260
            foreach ($thematic_data as $thematic) {
1261
                if (!empty($thematic_advance_data[$thematic['id']])) {
1262
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1263
                        if ($thematic_advance['done_advance'] == 1) {
1264
                            $a_thematic_advance_ids[] = $thematic_advance['id'];
1265
                        }
1266
                    }
1267
                }
1268
            }
1269
        }
1270
        if (!empty($a_thematic_advance_ids)) {
1271
            $last_done_advance_id = array_pop($a_thematic_advance_ids);
1272
            $last_done_advance_id = intval($last_done_advance_id);
1273
        }
1274
1275
        return $last_done_advance_id;
1276
    }
1277
1278
    /**
1279
     * Get next thematic advance not done from thematic details interface
1280
     * @param   int Offset (if you want to get an item that is not directly the next)
1281
     * @return int        next thematic advance not done
1282
     */
1283
    public function get_next_thematic_advance_not_done($offset = 1)
1284
    {
1285
        $thematic_data = self::get_thematic_list();
1286
        $thematic_advance_data = $this->get_thematic_advance_list();
1287
        $a_thematic_advance_ids = [];
1288
        $next_advance_not_done = 0;
1289
        if (!empty($thematic_data)) {
1290
            foreach ($thematic_data as $thematic) {
1291
                if (!empty($thematic_advance_data[$thematic['id']])) {
1292
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1293
                        if ($thematic_advance['done_advance'] == 0) {
1294
                            $a_thematic_advance_ids[] = $thematic_advance['id'];
1295
                        }
1296
                    }
1297
                }
1298
            }
1299
        }
1300
1301
        if (!empty($a_thematic_advance_ids)) {
1302
            for ($i = 0; $i < $offset; $i++) {
1303
                $next_advance_not_done = array_shift($a_thematic_advance_ids);
1304
            }
1305
            $next_advance_not_done = intval($next_advance_not_done);
1306
        }
1307
1308
        return $next_advance_not_done;
1309
    }
1310
1311
    /**
1312
     * Get total average of thematic advances
1313
     * @param string $course_code (optional)
1314
     * @param int $session_id (optional)
1315
     * @return float    Average of thematic advances
1316
     */
1317
    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

1317
    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...
1318
    {
1319
        if (empty($course_code)) {
1320
            $course_code = api_get_course_id();
1321
        }
1322
        if (api_get_session_id()) {
1323
            $thematic_data = self::get_thematic_list(null, $course_code);
1324
        } else {
1325
            $thematic_data = self::get_thematic_list(null, $course_code, 0);
1326
        }
1327
        $new_thematic_data = [];
1328
        if (!empty($thematic_data)) {
1329
            foreach ($thematic_data as $item) {
1330
                $new_thematic_data[] = $item;
1331
            }
1332
            $thematic_data = $new_thematic_data;
1333
        }
1334
1335
        $a_average_of_advances_by_thematic = [];
1336
        $total_average = 0;
1337
        if (!empty($thematic_data)) {
1338
            foreach ($thematic_data as $thematic) {
1339
                $thematic_id = $thematic['id'];
1340
                $a_average_of_advances_by_thematic[$thematic_id] = $this->get_average_of_advances_by_thematic(
1341
                    $thematic_id,
1342
                    $course_code
1343
                );
1344
            }
1345
        }
1346
1347
        // calculate total average
1348
        if (!empty($a_average_of_advances_by_thematic)) {
1349
            $count_tematics = count($thematic_data);
1350
            $score = array_sum($a_average_of_advances_by_thematic);
1351
            $total_average = round(($score * 100) / ($count_tematics * 100));
1352
        }
1353
1354
        return $total_average;
1355
    }
1356
1357
    /**
1358
     * Get average of advances by thematic
1359
     * @param int Thematic id
1360
     * @param string $course_code
1361
     * @return    float    Average of thematic advances
1362
     */
1363
    public function get_average_of_advances_by_thematic($thematic_id, $course_code = null)
1364
    {
1365
        $thematic_advance_data = $this->get_thematic_advance_by_thematic_id($thematic_id, $course_code);
1366
        $average = 0;
1367
        if (!empty($thematic_advance_data)) {
1368
            // get all done advances by thematic
1369
            $advances = [];
1370
            $count_done_advances = 0;
1371
            foreach ($thematic_advance_data as $thematic_advance) {
1372
                if ($thematic_advance['done_advance'] == 1) {
1373
                    $count_done_advances++;
1374
                }
1375
                $advances[] = $thematic_advance['done_advance'];
1376
            }
1377
            // calculate average by thematic
1378
            $count_total_advances = count($advances);
1379
            $average = round(($count_done_advances * 100) / $count_total_advances);
1380
        }
1381
1382
        return $average;
1383
    }
1384
1385
    /**
1386
     * set attributes for fields of thematic table
1387
     * @param    int        Thematic id
1388
     * @param    string    Thematic title
1389
     * @param    string    Thematic content
1390
     * @param    int        Session id
1391
     * @return void
1392
     */
1393
    public function set_thematic_attributes($id = null, $title = '', $content = '', $session_id = 0)
1394
    {
1395
        $this->thematic_id = $id;
1396
        $this->thematic_title = $title;
1397
        $this->thematic_content = $content;
1398
        $this->session_id = $session_id;
1399
    }
1400
1401
    /**
1402
     * set attributes for fields of thematic_plan table
1403
     * @param    int        Thematic id
1404
     * @param    string    Thematic plan title
1405
     * @param    string    Thematic plan description
1406
     * @param    int        Thematic plan description type
1407
     * @return void
1408
     */
1409
    public function set_thematic_plan_attributes(
1410
        $thematic_id = 0,
1411
        $title = '',
1412
        $description = '',
1413
        $description_type = 0
1414
    ) {
1415
        $this->thematic_id = $thematic_id;
1416
        $this->thematic_plan_title = $title;
1417
        $this->thematic_plan_description = $description;
1418
        $this->thematic_plan_description_type = $description_type;
1419
    }
1420
1421
    /**
1422
     * set attributes for fields of thematic_advance table
1423
     * @param    int $id Thematic advance id
1424
     * @param    int        Thematic id
1425
     * @param    int        Attendance id
1426
     * @param    string    Content
1427
     * @param    string    Date and time
1428
     * @param    int        Duration in hours
1429
     * @return void
1430
     */
1431
    public function set_thematic_advance_attributes(
1432
        $id = null,
1433
        $thematic_id = 0,
1434
        $attendance_id = 0,
1435
        $content = '',
1436
        $start_date = null,
1437
        $duration = 0
1438
    ) {
1439
        $this->thematic_advance_id = $id;
1440
        $this->thematic_id = $thematic_id;
1441
        $this->attendance_id = $attendance_id;
1442
        $this->thematic_advance_content = $content;
1443
        $this->start_date = $start_date;
1444
        $this->duration = $duration;
1445
    }
1446
1447
    /**
1448
     * set thematic id
1449
     * @param    int     Thematic id
1450
     * @return void
1451
     */
1452
    public function set_thematic_id($thematic_id)
1453
    {
1454
        $this->thematic_id = $thematic_id;
1455
    }
1456
1457
    /**
1458
     * get thematic id
1459
     * @return integer
1460
     */
1461
    public function get_thematic_id()
1462
    {
1463
        return $this->thematic_id;
1464
    }
1465
1466
    /**
1467
     * Get thematic plan titles by default
1468
     * @return array
1469
     */
1470
    public function get_default_thematic_plan_title()
1471
    {
1472
        $default_thematic_plan_titles = [];
1473
        $default_thematic_plan_titles[1] = get_lang('Objectives');
1474
        $default_thematic_plan_titles[2] = get_lang('SkillToAcquire');
1475
        $default_thematic_plan_titles[3] = get_lang('Methodology');
1476
        $default_thematic_plan_titles[4] = get_lang('Infrastructure');
1477
        $default_thematic_plan_titles[5] = get_lang('Assessment');
1478
        $default_thematic_plan_titles[6] = get_lang('Others');
1479
1480
        return $default_thematic_plan_titles;
1481
    }
1482
1483
    /**
1484
     * Get thematic plan icons by default
1485
     * @return array
1486
     */
1487
    public function get_default_thematic_plan_icon()
1488
    {
1489
        $default_thematic_plan_icon = [];
1490
        $default_thematic_plan_icon[1] = 'icons/32/objective.png';
1491
        $default_thematic_plan_icon[2] = 'icons/32/skills.png';
1492
        $default_thematic_plan_icon[3] = 'icons/32/strategy.png';
1493
        $default_thematic_plan_icon[4] = 'icons/32/laptop.png';
1494
        $default_thematic_plan_icon[5] = 'icons/32/assessment.png';
1495
        $default_thematic_plan_icon[6] = 'icons/32/wizard.png';
1496
1497
        return $default_thematic_plan_icon;
1498
    }
1499
1500
    /**
1501
     * Get questions by default for help
1502
     * @return array
1503
     */
1504
    public function get_default_question()
1505
    {
1506
        $question = [];
1507
        $question[1] = get_lang('ObjectivesQuestions');
1508
        $question[2] = get_lang('SkillToAcquireQuestions');
1509
        $question[3] = get_lang('MethodologyQuestions');
1510
        $question[4] = get_lang('InfrastructureQuestions');
1511
        $question[5] = get_lang('AssessmentQuestions');
1512
1513
        return $question;
1514
    }
1515
}
1516