Test Setup Failed
Push — master ( ec638a...cb9435 )
by Julito
51:10
created

Thematic   D

Complexity

Total Complexity 170

Size/Duplication

Total Lines 1495
Duplicated Lines 12.24 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 183
loc 1495
rs 4.4102
c 0
b 0
f 0
wmc 170
lcom 1
cbo 3

35 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C get_thematic_data() 15 86 10
A get_max_thematic_item() 19 19 2
C move_thematic() 0 58 10
A setCourseIntId() 0 4 1
A get_number_of_thematics() 16 16 2
B thematic_advance_destroy() 28 28 2
C get_thematic_list() 0 44 7
A thematic_save() 0 66 3
B thematic_destroy() 0 46 5
B copy() 0 43 6
A get_number_of_thematic_advances() 0 14 1
B get_thematic_advance_data() 0 46 6
B get_thematic_advance_by_thematic_id() 7 34 5
B get_thematic_advance_div() 5 23 5
C get_thematic_plan_array() 9 44 13
C get_thematic_advance_list() 0 57 10
A thematic_advance_save() 0 67 3
C get_thematic_plan_data() 7 81 14
C thematic_plan_save() 45 112 8
B thematic_plan_destroy() 0 36 2
B get_next_description_type() 0 27 2
D update_done_thematic_advances() 10 111 17
C get_last_done_thematic_advance() 11 29 7
C get_next_thematic_advance_not_done() 11 27 8
C get_total_average_of_thematic_advances() 0 39 8
A get_average_of_advances_by_thematic() 0 21 4
A set_thematic_attributes() 0 7 1
A set_thematic_plan_attributes() 0 7 1
A set_thematic_advance_attributes() 0 15 1
A set_thematic_id() 0 4 1
A get_thematic_id() 0 4 1
A get_default_thematic_plan_title() 0 12 1
A get_default_thematic_plan_icon() 0 12 1
A get_default_question() 0 11 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Thematic often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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

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

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
Unused Code introduced by
The property $thematic_plan_id is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can 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
    public function setCourseIntId($course_id)
38
    {
39
        $this->course_int_id = intval($course_id);
40
    }
41
42
43
    /**
44
     * Get the total number of thematic inside current course and current session
45
     * @see SortableTable#get_total_number_of_items()
46
     */
47 View Code Duplication
    public function get_number_of_thematics()
48
    {
49
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
50
        $condition_session = '';
51
        if (!api_get_session_id()) {
52
            $condition_session = api_get_session_condition(0);
53
        }
54
        $course_id = api_get_course_int_id();
55
        $sql = "SELECT COUNT(id) AS total_number_of_items
56
		        FROM $tbl_thematic
57
                WHERE c_id = $course_id AND active = 1 $condition_session ";
58
        $res = Database::query($sql);
59
        $obj = Database::fetch_object($res);
60
61
        return $obj->total_number_of_items;
62
    }
63
64
    /**
65
     * Get the thematics to display on the current page (fill the sortable-table)
66
     * @param   int     offset of first user to recover
67
     * @param   int     Number of users to get
68
     * @param   int     Column to sort on
69
     * @param   string  Order (ASC,DESC)
70
     * @return array
71
     * @see SortableTable#get_table_data($from)
72
     */
73
    public function get_thematic_data($from, $number_of_items, $column, $direction)
74
    {
75
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
76
        $condition_session = '';
77
        if (!api_get_session_id()) {
78
            $condition_session = api_get_session_condition(0);
79
        }
80
        $column = intval($column);
81
        $from = intval($from);
82
        $number_of_items = intval($number_of_items);
83
84
        if (!in_array($direction, array('ASC','DESC'))) {
85
            $direction = 'ASC';
86
        }
87
88
        $course_id = api_get_course_int_id();
89
90
        $sql = "SELECT id AS col0, title AS col1, display_order AS col2, session_id
91
                FROM $tbl_thematic
92
				WHERE c_id = $course_id AND active = 1 $condition_session
93
				ORDER BY col2
94
				LIMIT $from,$number_of_items ";
95
        $res = Database::query($sql);
96
97
        $thematics = array();
98
        $user_info = api_get_user_info(api_get_user_id());
99
        while ($thematic = Database::fetch_row($res)) {
100
            $session_star = '';
101 View Code Duplication
            if (api_get_session_id() == $thematic[3]) {
102
                $session_star = api_get_session_image(api_get_session_id(), $user_info['status']);
103
            }
104
            $thematic[1] = '<a href="index.php?'.api_get_cidreq().'&action=thematic_details&thematic_id='.$thematic[0].'">'.
105
                Security::remove_XSS($thematic[1], STUDENT).$session_star.'</a>';
106
            if (api_is_allowed_to_edit(null, true)) {
107
                $actions  = '';
108
109
                if (api_get_session_id()) {
110
                    if (api_get_session_id() == $thematic[3]) {
111
                        $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_plan_list&thematic_id='.$thematic[0].'">'.
112
                            Display::return_icon('lesson_plan.png',get_lang('ThematicPlan'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
113
                        $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_list&thematic_id='.$thematic[0].'">'.
114
                            Display::return_icon('lesson_plan_calendar.png',get_lang('ThematicAdvance'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
115
116
                        $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_edit&thematic_id='.$thematic[0].'">'.
117
                            Display::return_icon('edit.png',get_lang('Edit'),'',ICON_SIZE_SMALL).'</a>';
118
                        $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToDelete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=thematic_delete&thematic_id='.$thematic[0].'">'.
119
                            Display::return_icon('delete.png',get_lang('Delete'),'',ICON_SIZE_SMALL).'</a>';
120
                    } else {
121
                        $actions .= Display::return_icon('lesson_plan_na.png',get_lang('ThematicPlan'),'',ICON_SIZE_SMALL).'&nbsp;';
122
                        $actions .= Display::return_icon('lesson_plan_calendar_na.png',get_lang('ThematicAdvance'),'',ICON_SIZE_SMALL).'&nbsp;';
123
                        $actions .= Display::return_icon('edit_na.png',get_lang('Edit'),'',ICON_SIZE_SMALL);
124
                        $actions .= Display::return_icon('delete_na.png',get_lang('Delete'),'',ICON_SIZE_SMALL).'&nbsp;';
125
                        $actions .= Display::url(
126
                            Display::return_icon('cd.gif', get_lang('Copy')),
127
                            'index.php?'.api_get_cidreq().'&action=thematic_copy&thematic_id='.$thematic[0]
128
                        );
129
                    }
130
                } else {
131
                    $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_plan_list&thematic_id='.$thematic[0].'">'.
132
                        Display::return_icon('lesson_plan.png',get_lang('ThematicPlan'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
133
                    $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_list&thematic_id='.$thematic[0].'">'.
134
                        Display::return_icon('lesson_plan_calendar.png',get_lang('ThematicAdvance'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
135
136 View Code Duplication
                    if ($thematic[2] > 1) {
137
                        $actions .= '<a href="'.api_get_self().'?action=moveup&'.api_get_cidreq().'&thematic_id='.$thematic[0].'">'.
138
                            Display::return_icon('up.png', get_lang('Up'),'',ICON_SIZE_SMALL).'</a>';
139
                    } else {
140
                        $actions .= Display::return_icon('up_na.png','&nbsp;','',ICON_SIZE_SMALL);
141
                    }
142 View Code Duplication
                    if ($thematic[2] < self::get_max_thematic_item()) {
143
                        $actions .= '<a href="'.api_get_self().'?action=movedown&a'.api_get_cidreq().'&thematic_id='.$thematic[0].'">'.
144
                            Display::return_icon('down.png',get_lang('Down'),'',ICON_SIZE_SMALL).'</a>';
145
                    } else {
146
                        $actions .= Display::return_icon('down_na.png','&nbsp;','',ICON_SIZE_SMALL);
147
                    }
148
                    $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_edit&thematic_id='.$thematic[0].'">'.
149
                        Display::return_icon('edit.png',get_lang('Edit'),'',ICON_SIZE_SMALL).'</a>';
150
                    $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToDelete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=thematic_delete&thematic_id='.$thematic[0].'">'.
151
                        Display::return_icon('delete.png',get_lang('Delete'),'',ICON_SIZE_SMALL).'</a>';
152
                }
153
                $thematics[] = array($thematic[0], $thematic[1], $actions);
154
            }
155
        }
156
157
        return $thematics;
158
    }
159
160
    /**
161
     * Get the maximum display order of the thematic item
162
     * @return int	Maximum display order
163
     */
164 View Code Duplication
    public function get_max_thematic_item($use_session = true)
165
    {
166
        // Database table definition
167
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
168
        $session_id   = api_get_session_id();
169
        if ($use_session) {
170
            $condition_session = api_get_session_condition($session_id);
171
        } else {
172
            $condition_session = '';
173
        }
174
        $course_id = api_get_course_int_id();
175
        $sql = "SELECT MAX(display_order)
176
                FROM $tbl_thematic
177
                WHERE c_id = $course_id AND active = 1 $condition_session";
178
        $rs = Database::query($sql);
179
        $row = Database::fetch_array($rs);
180
181
        return $row[0];
182
    }
183
184
    /**
185
     * Move a thematic
186
     *
187
     * @param string $direction (up, down)
188
     * @param int $thematic_id
189
     */
190
    public function move_thematic($direction, $thematic_id)
191
    {
192
        // Database table definition
193
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
194
195
        // sort direction
196
        if ($direction == 'up') {
197
            $sortorder = 'DESC';
198
        } else {
199
            $sortorder = 'ASC';
200
        }
201
        $course_id = api_get_course_int_id();
202
203
        $session_id = api_get_session_id();
204
        $condition_session = api_get_session_condition($session_id);
205
206
        $sql = "SELECT id, display_order
207
                FROM $tbl_thematic
208
		        WHERE c_id = $course_id AND active = 1 $condition_session
209
		        ORDER BY display_order $sortorder";
210
        $res = Database::query($sql);
211
        $found = false;
212
213
        // Variable definition
214
        $current_id = 0;
215
        $next_id = 0;
216
217
        while ($row = Database::fetch_array($res)) {
218
            if ($found && empty($next_id)) {
219
                $next_id = intval($row['id']);
220
                $next_display_order = intval($row['display_order']);
221
            }
222
223
            if ($row['id'] == $thematic_id) {
224
                $current_id = intval($thematic_id);
225
                $current_display_order = intval($row['display_order']);
226
                $found = true;
227
            }
228
        }
229
230
        // get last done thematic advance before move thematic list
231
        $last_done_thematic_advance = $this->get_last_done_thematic_advance();
232
233
        if (!empty($next_display_order) && !empty($current_id)) {
234
            $sql = "UPDATE $tbl_thematic SET display_order = $next_display_order
235
			        WHERE c_id = $course_id AND id = $current_id ";
236
            Database::query($sql);
237
        }
238
        if (!empty($current_display_order) && !empty($next_id)) {
239
            $sql = "UPDATE $tbl_thematic SET
240
                    display_order = $current_display_order
241
			        WHERE c_id = $course_id AND id = $next_id ";
242
            Database::query($sql);
243
        }
244
245
        // update done advances with de current thematic list
246
        $this->update_done_thematic_advances($last_done_thematic_advance);
247
    }
248
249
    /**
250
     * get thematic list
251
     * @param int Thematic id (optional), get list by id
252
     * @param integer $thematic_id
253
     * @param string $course_code
254
     * @param integer $session_id
255
     * @return array    Thematic data
256
     */
257
    public static function get_thematic_list(
258
        $thematic_id = null,
259
        $course_code = null,
260
        $session_id = null
261
    ) {
262
        // set current course and session
263
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
264
        $course_info = api_get_course_info($course_code);
265
        $course_id = $course_info['real_id'];
266
267
        if (isset($session_id)) {
268
            $session_id = intval($session_id);
269
        } else {
270
            $session_id = api_get_session_id();
271
        }
272
273
        $data = array();
274
        if (isset($thematic_id)) {
275
            $thematic_id = intval($thematic_id);
276
            $condition = " WHERE id = $thematic_id AND active = 1 ";
277
        } else {
278
            if (empty($session_id)) {
279
                $condition_session = api_get_session_condition(0);
280
            } else {
281
                $condition_session = api_get_session_condition($session_id, true, true);
282
            }
283
            $condition = " WHERE active = 1 $condition_session ";
284
        }
285
        $sql = "SELECT * FROM $tbl_thematic $condition AND c_id = $course_id
286
                ORDER BY display_order ";
287
288
        $res = Database::query($sql);
289
        if (Database::num_rows($res) > 0) {
290
            if (!empty($thematic_id)) {
291
                $data = Database::fetch_array($res, 'ASSOC');
292
            } else {
293
                while ($row = Database::fetch_array($res, 'ASSOC')) {
294
                    $data[$row['id']] = $row;
295
                }
296
            }
297
        }
298
299
        return $data;
300
    }
301
302
    /**
303
     * insert or update a thematic
304
     * @return int last thematic id
305
     */
306
    public function thematic_save()
307
    {
308
        $_course = api_get_course_info();
309
        // definition database table
310
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
311
312
        // protect data
313
        $id = intval($this->thematic_id);
314
        $title = $this->thematic_title;
315
        $content = $this->thematic_content;
316
        $session_id = intval($this->session_id);
317
        $user_id = api_get_user_id();
318
319
        // get the maximum display order of all the glossary items
320
        $max_thematic_item = $this->get_max_thematic_item(false);
321
322
        if (empty($id)) {
323
            // insert
324
            $params = [
325
                'c_id' => $this->course_int_id,
326
                'title' => $title,
327
                'content' => $content,
328
                'active' => 1,
329
                'display_order' => intval($max_thematic_item) + 1,
330
                'session_id' => $session_id
331
            ];
332
            $last_id = Database::insert($tbl_thematic, $params);
333
            if ($last_id) {
334
                $sql = "UPDATE $tbl_thematic SET id = iid WHERE iid = $last_id";
335
                Database::query($sql);
336
                api_item_property_update(
337
                    $_course,
338
                    'thematic',
339
                    $last_id,
340
                    "ThematicAdded",
341
                    $user_id
342
                );
343
            }
344
        } else {
345
            // Update
346
            $params = [
347
                'title' => $title,
348
                'content' => $content,
349
                'session_id' => $session_id
350
            ];
351
352
            Database::update(
353
                $tbl_thematic,
354
                $params,
355
                ['id  = ? AND c_id = ?' => [$id, $this->course_int_id]]
356
            );
357
358
            $last_id = $id;
359
360
            // save inside item property table
361
            api_item_property_update(
362
                $_course,
363
                'thematic',
364
                $last_id,
365
                "ThematicUpdated",
366
                $user_id
367
            );
368
        }
369
370
        return $last_id;
371
    }
372
373
    /**
374
     * Delete logically (set active field to 0) a thematic
375
     * @param int|array One or many thematic ids
376
     * @return int            Affected rows
377
     */
378
    public function thematic_destroy($thematic_id)
379
    {
380
        $_course = api_get_course_info();
381
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
382
        $affected_rows = 0;
383
        $user_id = api_get_user_id();
384
        $course_id = api_get_course_int_id();
385
386
        if (is_array($thematic_id)) {
387
            foreach ($thematic_id as $id) {
388
                $id	= intval($id);
389
                $sql = "UPDATE $tbl_thematic SET active = 0
390
                        WHERE c_id = $course_id AND id = $id";
391
                $result = Database::query($sql);
392
                $affected_rows += Database::affected_rows($result);
393
                if (!empty($affected_rows)) {
394
                    // update row item property table
395
                    api_item_property_update(
396
                        $_course,
397
                        'thematic',
398
                        $id,
399
                        "ThematicDeleted",
400
                        $user_id
401
                    );
402
                }
403
            }
404
        } else {
405
            $thematic_id = intval($thematic_id);
406
            $sql = "UPDATE $tbl_thematic SET active = 0
407
                    WHERE c_id = $course_id AND id = $thematic_id";
408
            $result = Database::query($sql);
409
            $affected_rows = Database::affected_rows($result);
410
            if (!empty($affected_rows)) {
411
                // update row item property table
412
                api_item_property_update(
413
                    $_course,
414
                    'thematic',
415
                    $thematic_id,
416
                    "ThematicDeleted",
417
                    $user_id
418
                );
419
            }
420
        }
421
422
        return $affected_rows;
423
    }
424
425
    /**
426
     * @param int $thematic_id
427
     */
428
    public function copy($thematic_id)
429
    {
430
        $thematic = self::get_thematic_list($thematic_id, api_get_course_id(), 0);
431
        $thematic_copy = new Thematic();
432
        $thematic_copy->set_thematic_attributes(
433
            '',
434
            $thematic['title'].' - '.get_lang('Copy'),
435
            $thematic['content'],
436
            api_get_session_id()
437
        );
438
439
        $new_thematic_id = $thematic_copy->thematic_save();
440
        if (!empty($new_thematic_id)) {
441
            $thematic_advanced = self::get_thematic_advance_by_thematic_id($thematic_id);
442
            if (!empty($thematic_advanced)) {
443
                foreach ($thematic_advanced as $item) {
444
                    $thematic = new Thematic();
445
                    $thematic->set_thematic_advance_attributes(
446
                        0,
447
                        $new_thematic_id,
448
                        0,
449
                        $item['content'],
450
                        $item['start_date'],
451
                        $item['duration']
452
                    );
453
                    $thematic->thematic_advance_save();
454
                }
455
            }
456
            $thematic_plan = self::get_thematic_plan_data($thematic_id);
457
            if (!empty($thematic_plan)) {
458
                foreach ($thematic_plan as $item) {
459
                    $thematic = new Thematic();
460
                    $thematic->set_thematic_plan_attributes(
461
                        $new_thematic_id,
462
                        $item['title'],
463
                        $item['description'],
464
                        $item['description_type']
465
                    );
466
                    $thematic->thematic_plan_save();
467
                }
468
            }
469
        }
470
    }
471
472
    /**
473
     * Get the total number of thematic advance inside current course
474
     * @see SortableTable#get_total_number_of_items()
475
     */
476
    public static function get_number_of_thematic_advances()
477
    {
478
        global $thematic_id;
479
        $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
480
        $course_id = api_get_course_int_id();
481
482
        $sql = "SELECT COUNT(id) AS total_number_of_items 
483
                FROM $tbl_thematic_advance
484
                WHERE c_id = $course_id AND thematic_id = $thematic_id ";
485
        $res = Database::query($sql);
486
        $obj = Database::fetch_object($res);
487
488
        return $obj->total_number_of_items;
489
    }
490
491
    /**
492
     * Get the thematic advances to display on the current page (fill the sortable-table)
493
     * @param   int     offset of first user to recover
494
     * @param   int     Number of users to get
495
     * @param   int     Column to sort on
496
     * @param   string  Order (ASC,DESC)
497
     * @return array
498
     * @see SortableTable#get_table_data($from)
499
     */
500
    public static function get_thematic_advance_data($from, $number_of_items, $column, $direction)
501
    {
502
        global $thematic_id;
503
        $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
504
        $column = intval($column);
505
        $from   = intval($from);
506
        $number_of_items = intval($number_of_items);
507
        if (!in_array($direction, array('ASC','DESC'))) {
508
            $direction = 'ASC';
509
        }
510
        $data = array();
511
        $course_id = api_get_course_int_id();
512
        if (api_is_allowed_to_edit(null, true)) {
513
            $sql = "SELECT id AS col0, start_date AS col1, duration AS col2, content AS col3
514
                    FROM $tbl_thematic_advance
515
    				WHERE c_id = $course_id AND thematic_id = $thematic_id
516
    				ORDER BY col$column $direction
517
    				LIMIT $from,$number_of_items ";
518
519
            $list = api_get_item_property_by_tool(
520
                'thematic_advance',
521
                api_get_course_id(),
522
                api_get_session_id()
523
            );
524
525
            $elements = array();
526
            foreach ($list as $value) {
527
                $elements[] = $value['ref'];
528
            }
529
530
            $res = Database::query($sql);
531
            $i = 1;
532
            while ($thematic_advance = Database::fetch_row($res)) {
533
                if (in_array($thematic_advance[0], $elements)) {
534
                    $thematic_advance[1] = api_get_local_time($thematic_advance[1]);
535
                    $thematic_advance[1] = api_format_date($thematic_advance[1], DATE_TIME_FORMAT_LONG);
536
                    $actions  = '';
537
                    $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>';
538
                    $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>';
539
                    $data[] = array($i, $thematic_advance[1], $thematic_advance[2], $thematic_advance[3], $actions);
540
                    $i++;
541
                }
542
            }
543
        }
544
        return $data;
545
    }
546
547
    /**
548
     * get thematic advance data by thematic id
549
     * @param	int		$thematic_id
550
     * @param	string	Course code (optional)
551
     * @return	array	data
552
     */
553
    public function get_thematic_advance_by_thematic_id($thematic_id, $course_code = null)
554
    {
555
        $course_info = api_get_course_info($course_code);
556
        $course_id = $course_info['real_id'];
557
558
        // set current course
559
        $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
560
561
        $thematic_id = intval($thematic_id);
562
        $data = array();
563
        $sql = "SELECT * FROM $tbl_thematic_advance
564
                WHERE c_id = $course_id AND thematic_id = $thematic_id ";
565
566
        $elements = array();
567
        $list = api_get_item_property_by_tool(
568
            'thematic_advance',
569
            $course_info['code'],
570
            api_get_session_id()
571
        );
572
        foreach ($list as $value) {
573
            $elements[] = $value['ref'];
574
        }
575
576
        $res = Database::query($sql);
577 View Code Duplication
        if (Database::num_rows($res) > 0) {
578
            while ($row = Database::fetch_array($res, 'ASSOC')) {
579
                if (in_array($row['id'], $elements)) {
580
                    $data[] = $row;
581
                }
582
            }
583
        }
584
585
        return $data;
586
    }
587
588
    /**
589
     * @param array $data
590
     * @return array
591
     */
592
    public function get_thematic_advance_div($data)
593
    {
594
        $return_array = array();
595
        $uinfo = api_get_user_info();
596
597
        foreach ($data as $thematic_id => $thematic_advance_data) {
598
            foreach ($thematic_advance_data as $key => $thematic_advance) {
599
                $session_star = '';
600 View Code Duplication
                if (api_is_allowed_to_edit(null, true)) {
601
                    if ($thematic_advance['session_id'] !=0) {
602
                        $session_star = api_get_session_image(api_get_session_id(), $uinfo['status']);
603
                    }
604
                }
605
                // DATE_TIME_FORMAT_LONG
606
                $thematic_advance_item  = '<div><strong>'.api_convert_and_format_date($thematic_advance['start_date'], DATE_TIME_FORMAT_LONG).$session_star.'</strong></div>';
607
//				$thematic_advance_item .= '<div>'.get_lang('DurationInHours').' : '.$thematic_advance['duration'].'</div>';
608
                $thematic_advance_item .= '<div>'.$thematic_advance['duration'].' '.get_lang('HourShort').'</div>';
609
                $thematic_advance_item .= '<div>'.Security::remove_XSS($thematic_advance['content'], STUDENT).'</div>';
610
                $return_array[$thematic_id][$thematic_advance['id']] = $thematic_advance_item;
611
            }
612
        }
613
        return $return_array;
614
    }
615
616
    /**
617
     * @param array $data
618
     * @return array
619
     */
620
    public function get_thematic_plan_array($data)
621
    {
622
        $final_return = array();
623
        $uinfo = api_get_user_info();
624
625
        foreach ($data as $thematic_id => $thematic_plan_data) {
626
            $new_thematic_plan_data = array();
627 View Code Duplication
            foreach($thematic_plan_data as $thematic_item) {
628
                $thematic_simple_list[] = $thematic_item['description_type'];
629
                $new_thematic_plan_data[$thematic_item['description_type']] = $thematic_item;
630
            }
631
632
            if (!empty($thematic_simple_list)) {
633
                foreach($thematic_simple_list as $item) {
634
                    $default_thematic_plan_title[$item] = $new_thematic_plan_data[$item]['title'];
635
                }
636
            }
637
638
            $session_star = '';
639
            $return = array();
640
            if (!empty($default_thematic_plan_title)) {
641
642
                foreach ($default_thematic_plan_title as $id=>$title) {
643
                    //avoid others
644
                    if ($title == 'Others' && empty($data[$thematic_id][$id]['description'])) {
645
                        continue;
646
                    }
647
                    if (!empty($data[$thematic_id][$id]['title']) && !empty($data[$thematic_id][$id]['description'])) {
648 View Code Duplication
                        if (api_is_allowed_to_edit(null, true)) {
649
                            if ($data[$thematic_id][$id]['session_id'] !=0) {
650
                                $session_star = api_get_session_image(api_get_session_id(), $uinfo['status']);
651
                            }
652
                        }
653
654
                        $return[$id]['title'] = Security::remove_XSS($data[$thematic_id][$id]['title'], STUDENT).$session_star;
655
                        $return[$id]['description'] = Security::remove_XSS($data[$thematic_id][$id]['description'], STUDENT);
656
                    }
657
                }
658
            }
659
            $final_return[$thematic_id] = $return;
660
        }
661
662
        return $final_return;
663
    }
664
665
    /**
666
     * get thematic advance list
667
     * @param int $thematic_advance_id Thematic advance id (optional), get data by thematic advance list
668
     * @param string $course_code Course code (optional)
669
     * @param bool $force_session_id Force to have a session id
670
     * @return array $data
671
     */
672
    public function get_thematic_advance_list(
673
        $thematic_advance_id = null,
674
        $course_code = null,
675
        $force_session_id = false
676
    ) {
677
        $course_info = api_get_course_info($course_code);
678
        $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
679
        $data = array();
680
        $condition = '';
681
        if (isset($thematic_advance_id)) {
682
            $thematic_advance_id = intval($thematic_advance_id);
683
            $condition = " AND a.id = $thematic_advance_id ";
684
        }
685
686
        $course_id = $course_info['real_id'];
687
688
        $sql = "SELECT * FROM $tbl_thematic_advance a
689
                WHERE c_id = $course_id $condition
690
                ORDER BY start_date ";
691
692
        $elements = array();
693
        if ($force_session_id) {
694
            $list = api_get_item_property_by_tool(
695
                'thematic_advance',
696
                $course_info['code'],
697
                api_get_session_id()
698
            );
699
            foreach ($list as $value) {
700
                $elements[$value['ref']] = $value;
701
            }
702
        }
703
704
        $res = Database::query($sql);
705
        if (Database::num_rows($res) > 0) {
706
            if (!empty($thematic_advance_id)) {
707
                $data = Database::fetch_array($res);
708
            } else {
709
                // group all data group by thematic id
710
                $tmp = array();
711
                while ($row = Database::fetch_array($res, 'ASSOC')) {
712
                    $tmp[] = $row['thematic_id'];
713
                    if (in_array($row['thematic_id'], $tmp)) {
714
                        if ($force_session_id) {
715
                            if (in_array($row['id'], array_keys($elements))) {
716
                                $row['session_id'] = $elements[$row['id']]['session_id'];
717
                                $data[$row['thematic_id']][$row['id']] = $row;
718
                            }
719
                        } else {
720
                            $data[$row['thematic_id']][$row['id']] = $row;
721
                        }
722
                    }
723
                }
724
            }
725
        }
726
727
        return $data;
728
    }
729
730
    /**
731
     * insert or update a thematic advance
732
     * @todo problem
733
     * @return int last thematic advance id
734
     */
735
    public function thematic_advance_save()
736
    {
737
        $_course = api_get_course_info();
738
        // definition database table
739
        $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
740
741
        // protect data
742
        $id = intval($this->thematic_advance_id);
743
        $thematic_id = intval($this->thematic_id);
744
        $attendance_id = intval($this->attendance_id);
745
        $content = $this->thematic_advance_content;
746
        $start_date = $this->start_date;
747
        $duration = intval($this->duration);
748
        $user_id = api_get_user_id();
749
750
        $last_id = null;
751
        if (empty($id)) {
752
            // Insert
753
            $params = [
754
                'c_id' => $this->course_int_id,
755
                'thematic_id' => $thematic_id,
756
                'attendance_id' => $attendance_id,
757
                'content' => $content,
758
                'start_date' => api_get_utc_datetime($start_date),
759
                'duration' => $duration,
760
                'done_advance' => 0
761
            ];
762
            $last_id = Database::insert($tbl_thematic_advance, $params);
763
764
            if ($last_id) {
765
                $sql = "UPDATE $tbl_thematic_advance SET id = iid WHERE iid = $last_id";
766
                Database::query($sql);
767
768
                api_item_property_update(
769
                    $_course,
770
                    'thematic_advance',
771
                    $last_id,
772
                    "ThematicAdvanceAdded",
773
                    $user_id
774
                );
775
            }
776
        } else {
777
            $params = [
778
                'thematic_id' => $thematic_id,
779
                'attendance_id' => $attendance_id,
780
                'content' => $content,
781
                'start_date' => api_get_utc_datetime($start_date),
782
                'duration' => $duration
783
            ];
784
785
            Database::update(
786
                $tbl_thematic_advance,
787
                $params,
788
                ['id = ? AND c_id = ?' => [$id, $this->course_int_id]]
789
            );
790
791
            api_item_property_update(
792
                $_course,
793
                'thematic_advance',
794
                $id,
795
                "ThematicAdvanceUpdated",
796
                $user_id
797
            );
798
        }
799
800
        return $last_id;
801
    }
802
803
    /**
804
     * delete  thematic advance
805
     * @param	int		Thematic advance id
806
     * @param integer $thematic_advance_id
807
     * @return	int		Affected rows
808
     */
809 View Code Duplication
    public function thematic_advance_destroy($thematic_advance_id)
810
    {
811
        $_course = api_get_course_info();
812
        $course_id = api_get_course_int_id();
813
814
        // definition database table
815
        $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
816
817
        // protect data
818
        $thematic_advance_id = intval($thematic_advance_id);
819
        $user_id = api_get_user_id();
820
821
        $sql = "DELETE FROM $tbl_thematic_advance
822
                WHERE c_id = $course_id AND id = $thematic_advance_id ";
823
        $result = Database::query($sql);
824
        $affected_rows = Database::affected_rows($result);
825
        if ($affected_rows) {
826
            api_item_property_update(
827
                $_course,
828
                'thematic_advance',
829
                $thematic_advance_id,
830
                'ThematicAdvanceDeleted',
831
                $user_id
832
            );
833
        }
834
835
        return $affected_rows;
836
    }
837
838
    /**
839
     * get thematic plan data
840
     * @param	int		Thematic id (optional), get data by thematic id
841
     * @param	int		Thematic plan description type (optional), get data by description type
842
     * @return 	array	Thematic plan data
843
     */
844
    public function get_thematic_plan_data($thematic_id = null, $description_type = null)
845
    {
846
        // definition database table
847
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
848
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
849
        $course_id = api_get_course_int_id();
850
851
        $data = array();
852
        $condition = '';
853
        if (isset($thematic_id)) {
854
            $thematic_id = intval($thematic_id);
855
            $condition .= " AND thematic_id = $thematic_id ";
856
        }
857
        if (isset($description_type)) {
858
            $description_type = intval($description_type);
859
            $condition .= " AND description_type = $description_type ";
860
        }
861
862
        $items_from_course = api_get_item_property_by_tool(
863
            'thematic_plan',
864
            api_get_course_id(),
865
            0
866
        );
867
        $items_from_session = api_get_item_property_by_tool(
868
            'thematic_plan',
869
            api_get_course_id(),
870
            api_get_session_id()
871
        );
872
873
        $thematic_plan_complete_list  = array();
874
        $thematic_plan_id_list = array();
875
876
        if (!empty($items_from_course)) {
877
            foreach($items_from_course as $item) {
878
                $thematic_plan_id_list[] = $item['ref'];
879
                $thematic_plan_complete_list[$item['ref']] = $item;
880
            }
881
        }
882
883
        if (!empty($items_from_session)) {
884
            foreach($items_from_session as $item) {
885
                $thematic_plan_id_list[] = $item['ref'];
886
                $thematic_plan_complete_list[$item['ref']] = $item;
887
            }
888
        }
889
        if (!empty($thematic_plan_id_list)) {
890
            $sql = "SELECT
891
                        tp.id, thematic_id, tp.title, description, description_type, t.session_id
892
			        FROM $tbl_thematic_plan tp
893
			        INNER JOIN $tbl_thematic t 
894
			        ON (t.id=tp.thematic_id)
895
                    WHERE
896
                        t.c_id = $course_id AND
897
                        tp.c_id = $course_id
898
                        $condition  AND
899
                        tp.id IN (".implode(', ', $thematic_plan_id_list).") ";
900
901
            $rs = Database::query($sql);
902
903
            if (Database::num_rows($rs)) {
904
                if (!isset($thematic_id) && !isset($description_type)) {
905
                    // group all data group by thematic id
906
                    $tmp = array();
907 View Code Duplication
                    while ($row = Database::fetch_array($rs,'ASSOC')) {
908
                        $tmp[] = $row['thematic_id'];
909
                        if (in_array($row['thematic_id'], $tmp)) {
910
                            $row['session_id'] = $thematic_plan_complete_list[$row['id']];
911
                            $data[$row['thematic_id']][$row['description_type']] = $row;
912
                        }
913
                    }
914
                } else {
915
                    while ($row = Database::fetch_array($rs,'ASSOC')) {
916
                        $row['session_id'] = $thematic_plan_complete_list[$row['id']];
917
                        $data[] = $row;
918
                    }
919
                }
920
            }
921
        }
922
923
        return $data;
924
    }
925
926
    /**
927
     * insert or update a thematic plan
928
     * @return int affected rows
929
     */
930
    public function thematic_plan_save()
931
    {
932
        $_course = api_get_course_info();
933
        // definition database table
934
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
935
936
        // protect data
937
        $thematic_id = intval($this->thematic_id);
938
        $title = $this->thematic_plan_title;
939
        $description = $this->thematic_plan_description;
940
        $description_type = intval($this->thematic_plan_description_type);
941
        $user_id = api_get_user_id();
942
        $course_id = api_get_course_int_id();
943
        $list = api_get_item_property_by_tool(
944
            'thematic_plan',
945
            api_get_course_id(),
946
            api_get_session_id()
947
        );
948
949
        $elements_to_show = array();
950
        foreach($list as $value) {
951
            $elements_to_show[]= $value['ref'];
952
        }
953
        $condition = '';
954
        if (!empty($elements_to_show)) {
955
            $condition = "AND id IN (".implode(',', $elements_to_show).") ";
956
        }
957
        // check thematic plan type already exists
958
        $sql = "SELECT id FROM $tbl_thematic_plan
959
                WHERE
960
                    c_id = $course_id AND
961
                    thematic_id = $thematic_id AND
962
                    description_type = '$description_type'";
963
        $rs	 = Database::query($sql);
964
965
        $affected_rows = 0;
966
        if (Database::num_rows($rs) > 0) {
967
            $row_thematic_plan = Database::fetch_array($rs);
968
            $thematic_plan_id = $row_thematic_plan['id'];
969
            $update = false;
970
            if (in_array($thematic_plan_id, $elements_to_show)) {
971
                $update = true;
972
            }
973
974
            if ($update) {
975
                // update
976
                $params = [
977
                    'title' => $title,
978
                    'description' => $description
979
                ];
980
                Database::update(
981
                    $tbl_thematic_plan,
982
                    $params,
983
                    ['c_id = ? AND id = ?' => [$course_id, $thematic_plan_id]]
984
                );
985
986
                api_item_property_update(
987
                    $_course,
988
                    'thematic_plan',
989
                    $thematic_plan_id,
990
                    "ThematicPlanUpdated",
991
                    $user_id
992
                );
993
994 View Code Duplication
            } else {
995
                // insert
996
                $params = [
997
                    'c_id' => $this->course_int_id,
998
                    'thematic_id' => $thematic_id,
999
                    'title' => $title,
1000
                    'description' => $description,
1001
                    'description_type' => $description_type
1002
                ];
1003
                $last_id = Database::insert($tbl_thematic_plan, $params);
1004
                if ($last_id) {
1005
                    $sql = "UPDATE $tbl_thematic_plan SET id = iid WHERE iid = $last_id";
1006
                    Database::query($sql);
1007
                    api_item_property_update(
1008
                        $_course,
1009
                        'thematic_plan',
1010
                        $last_id,
1011
                        "ThematicPlanAdded",
1012
                        $user_id
1013
                    );
1014
                }
1015
            }
1016 View Code Duplication
        } else {
1017
            // insert
1018
            $params = [
1019
                'c_id' => $this->course_int_id,
1020
                'thematic_id' => $thematic_id,
1021
                'title' => $title,
1022
                'description' => $description,
1023
                'description_type' => $description_type
1024
            ];
1025
            $last_id = Database::insert($tbl_thematic_plan, $params);
1026
1027
            if ($last_id) {
1028
                $sql = "UPDATE $tbl_thematic_plan SET id = iid WHERE iid = $last_id";
1029
                Database::query($sql);
1030
                api_item_property_update(
1031
                    $_course,
1032
                    'thematic_plan',
1033
                    $last_id,
1034
                    "ThematicPlanAdded",
1035
                    $user_id
1036
                );
1037
            }
1038
        }
1039
1040
        return $affected_rows;
1041
    }
1042
1043
    /**
1044
     * delete a thematic plan description
1045
     * @param	int		$thematic_id Thematic id
1046
     * @param	int		$description_type Description type
1047
     * @return	int		Affected rows
1048
     */
1049
    public function thematic_plan_destroy($thematic_id, $description_type)
1050
    {
1051
        $_course = api_get_course_info();
1052
        // definition database table
1053
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
1054
1055
        // protect data
1056
        $thematic_id = intval($thematic_id);
1057
        $description_type = intval($description_type);
1058
        $user_id = api_get_user_id();
1059
        $course_info = api_get_course_info();
1060
        $course_id = $course_info['real_id'];
1061
1062
        // get thematic plan id
1063
        $thematic_plan_data = $this->get_thematic_plan_data($thematic_id, $description_type);
1064
        $thematic_plan_id = $thematic_plan_data[0]['id'];
1065
1066
        // delete
1067
        $sql = "DELETE FROM $tbl_thematic_plan
1068
                WHERE
1069
                    c_id = $course_id AND
1070
                    thematic_id = $thematic_id AND
1071
                    description_type = $description_type ";
1072
        $result = Database::query($sql);
1073
        $affected_rows = Database::affected_rows($result);
1074
        if ($affected_rows) {
1075
            api_item_property_update(
1076
                $_course,
1077
                'thematic_plan',
1078
                $thematic_plan_id,
1079
                'ThematicPlanDeleted',
1080
                $user_id
1081
            );
1082
        }
1083
        return $affected_rows;
1084
    }
1085
1086
    /**
1087
     * Get next description type for a new thematic plan description (option 'others')
1088
     * @param	int		Thematic id
1089
     * @return 	int		New Description type
1090
     */
1091
    public function get_next_description_type($thematic_id)
1092
    {
1093
        // definition database table
1094
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
1095
1096
        // protect data
1097
        $thematic_id = intval($thematic_id);
1098
        $course_id = api_get_course_int_id();
1099
1100
        $sql = "SELECT MAX(description_type) as max
1101
                FROM $tbl_thematic_plan
1102
		        WHERE
1103
		            c_id = $course_id AND
1104
		            thematic_id = $thematic_id AND
1105
		            description_type >= ".ADD_THEMATIC_PLAN;
1106
        $rs = Database::query($sql);
1107
        $row = Database::fetch_array($rs);
1108
        $last_description_type = $row['max'];
1109
1110
        if (isset($last_description_type)) {
1111
            $next_description_type = $last_description_type + 1;
1112
        } else {
1113
            $next_description_type = ADD_THEMATIC_PLAN;
1114
        }
1115
1116
        return $next_description_type;
1117
    }
1118
1119
    /**
1120
     * update done thematic advances from thematic details interface
1121
     * @param 	int		Thematic id
1122
     * @param integer $thematic_advance_id
1123
     * @return	int		Affected rows
1124
     */
1125
    public function update_done_thematic_advances($thematic_advance_id)
1126
    {
1127
        $_course = api_get_course_info();
1128
        $thematic_data = $this->get_thematic_list(null, api_get_course_id());
1129
        $thematic_advance_data = $this->get_thematic_advance_list(
1130
            null,
1131
            api_get_course_id(),
1132
            true
1133
        );
1134
        $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
1135
1136
        $affected_rows = 0;
1137
        $user_id       = api_get_user_id();
1138
1139
        $all = array();
1140 View Code Duplication
        if (!empty($thematic_data)) {
1141
            foreach ($thematic_data as $thematic) {
1142
                $thematic_id = $thematic['id'];
1143
                if (!empty($thematic_advance_data[$thematic['id']])) {
1144
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1145
                        $all[] = $thematic_advance['id'];
1146
                    }
1147
                }
1148
            }
1149
        }
1150
        $error = null;
1151
        $a_thematic_advance_ids = array();
1152
        $course_id = api_get_course_int_id();
1153
        $sessionId = api_get_session_id();
1154
1155
        if (!empty($thematic_data)) {
1156
            foreach ($thematic_data as $thematic) {
1157
                $my_affected_rows = 0;
1158
                $thematic_id = $thematic['id'];
1159
                if (!empty($thematic_advance_data[$thematic['id']])) {
1160
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1161
1162
                        $item_info = api_get_item_property_info(
1163
                            api_get_course_int_id(),
1164
                            'thematic_advance',
1165
                            $thematic_advance['id'],
1166
                            $sessionId
1167
                        );
1168
1169
                        if ($item_info['session_id'] == $sessionId) {
1170
                            $a_thematic_advance_ids[] = $thematic_advance['id'];
1171
                            // update done thematic for previous advances ((done_advance = 1))
1172
                            $upd = "UPDATE $tbl_thematic_advance SET
1173
                                    done_advance = 1
1174
                                    WHERE c_id = $course_id AND id = ".$thematic_advance['id']." ";
1175
                            $result = Database::query($upd);
1176
                            $my_affected_rows = Database::affected_rows($result);
1177
                            $affected_rows += $my_affected_rows;
1178
                            //if ($my_affected_rows) {
1179
                            api_item_property_update(
1180
                                $_course,
1181
                                'thematic_advance',
1182
                                $thematic_advance['id'],
1183
                                "ThematicAdvanceDone",
1184
                                $user_id
1185
                            );
1186
                            //}
1187
                            if ($thematic_advance['id'] == $thematic_advance_id) {
1188
                                break 2;
1189
                            }
1190
                        }
1191
                    }
1192
                }
1193
            }
1194
        }
1195
1196
        // Update done thematic for others advances (done_advance = 0)
1197
        if (!empty($a_thematic_advance_ids) && count($a_thematic_advance_ids) > 0) {
1198
            $diff = array_diff($all, $a_thematic_advance_ids);
1199
            if (!empty($diff)) {
1200
                $upd = "UPDATE $tbl_thematic_advance SET done_advance = 0
1201
    			        WHERE c_id = $course_id AND id IN(".implode(',',$diff).") ";
1202
                Database::query($upd);
1203
            }
1204
1205
            // update item_property
1206
            $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
1207
            $sql = "SELECT ref FROM $tbl_item_property
1208
                    WHERE
1209
                        c_id = $course_id AND
1210
                        tool='thematic_advance' AND
1211
                        lastedit_type='ThematicAdvanceDone' AND
1212
                        session_id = $sessionId ";
1213
            // get all thematic advance done
1214
            $rs_thematic_done = Database::query($sql);
1215
            if (Database::num_rows($rs_thematic_done) > 0) {
1216
                while ($row_thematic_done = Database::fetch_array($rs_thematic_done)) {
1217
                    $ref = $row_thematic_done['ref'];
1218
                    if (in_array($ref, $a_thematic_advance_ids)) { continue; }
1219
                    // update items
1220
                    $sql = "UPDATE $tbl_item_property SET
1221
                                lastedit_date='".api_get_utc_datetime()."',
1222
                                lastedit_type='ThematicAdvanceUpdated',
1223
                                lastedit_user_id = $user_id
1224
                            WHERE
1225
                                c_id = $course_id AND
1226
                                tool='thematic_advance' AND
1227
                                ref=$ref AND
1228
                                session_id = $sessionId  ";
1229
                    Database::query($sql);
1230
                }
1231
            }
1232
        }
1233
1234
        return $affected_rows;
1235
    }
1236
1237
    /**
1238
     * Get last done thematic advance from thematic details interface
1239
     * @return	int		Last done thematic advance id
1240
     */
1241
    public function get_last_done_thematic_advance()
1242
    {
1243
        $thematic_data = $this->get_thematic_list();
1244
        $thematic_advance_data = $this->get_thematic_advance_list(
1245
            null,
1246
            api_get_course_id(),
1247
            true
1248
        );
1249
1250
        $a_thematic_advance_ids = array();
1251
        $last_done_advance_id = 0;
1252 View Code Duplication
        if (!empty($thematic_data)) {
1253
            foreach ($thematic_data as $thematic) {
1254
                if (!empty($thematic_advance_data[$thematic['id']])) {
1255
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1256
                        if ($thematic_advance['done_advance'] == 1) {
1257
                            $a_thematic_advance_ids[] = $thematic_advance['id'];
1258
                        }
1259
                    }
1260
                }
1261
            }
1262
        }
1263
        if (!empty($a_thematic_advance_ids)) {
1264
            $last_done_advance_id = array_pop($a_thematic_advance_ids);
1265
            $last_done_advance_id = intval($last_done_advance_id);
1266
        }
1267
1268
        return $last_done_advance_id;
1269
    }
1270
1271
    /**
1272
     * Get next thematic advance not done from thematic details interface
1273
     * @param   int Offset (if you want to get an item that is not directly the next)
1274
     * @return	int		next thematic advance not done
1275
     */
1276
    public function get_next_thematic_advance_not_done($offset = 1)
1277
    {
1278
        $thematic_data = $this->get_thematic_list();
1279
        $thematic_advance_data = $this->get_thematic_advance_list();
1280
        $a_thematic_advance_ids = array();
1281
        $next_advance_not_done = 0;
1282 View Code Duplication
        if (!empty($thematic_data)) {
1283
            foreach ($thematic_data as $thematic) {
1284
                if (!empty($thematic_advance_data[$thematic['id']])) {
1285
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1286
                        if ($thematic_advance['done_advance'] == 0) {
1287
                            $a_thematic_advance_ids[] = $thematic_advance['id'];
1288
                        }
1289
                    }
1290
                }
1291
            }
1292
        }
1293
1294
        if (!empty($a_thematic_advance_ids)) {
1295
            for ($i = 0; $i < $offset; $i++) {
1296
                $next_advance_not_done = array_shift($a_thematic_advance_ids);
1297
            }
1298
            $next_advance_not_done = intval($next_advance_not_done);
1299
        }
1300
1301
        return $next_advance_not_done;
1302
    }
1303
1304
    /**
1305
     * Get total average of thematic advances
1306
     * @param	string	$course_code (optional)
1307
     * @param	int		$session_id	(optional)
1308
     * @return 	float	Average of thematic advances
1309
     */
1310
    public function get_total_average_of_thematic_advances($course_code = null, $session_id = null)
1311
    {
1312
        if (empty($course_code)) {
1313
            $course_code = api_get_course_id();
1314
        }
1315
        if (api_get_session_id()) {
1316
            $thematic_data = $this->get_thematic_list(null, $course_code );
1317
        } else {
1318
            $thematic_data = $this->get_thematic_list(null, $course_code, 0);
1319
        }
1320
        $new_thematic_data = array();
1321
        if (!empty($thematic_data)) {
1322
            foreach ($thematic_data as $item) {
1323
                $new_thematic_data[] = $item;
1324
            }
1325
            $thematic_data = $new_thematic_data;
1326
        }
1327
1328
        $a_average_of_advances_by_thematic = array();
1329
        $total_average = 0;
1330
        if (!empty($thematic_data)) {
1331
            foreach ($thematic_data as $thematic) {
1332
                $thematic_id = $thematic['id'];
1333
                $a_average_of_advances_by_thematic[$thematic_id] = $this->get_average_of_advances_by_thematic(
1334
                    $thematic_id,
1335
                    $course_code
1336
                );
1337
            }
1338
        }
1339
1340
        // calculate total average
1341
        if (!empty($a_average_of_advances_by_thematic)) {
1342
            $count_tematics = count($thematic_data);
1343
            $score = array_sum($a_average_of_advances_by_thematic);
1344
            $total_average = round(($score * 100) / ($count_tematics * 100));
1345
        }
1346
1347
        return $total_average;
1348
    }
1349
1350
    /**
1351
     * Get average of advances by thematic
1352
     * @param	int		Thematic id
1353
     * @param	string	Course code (optional)
1354
     * @param string $course_code
1355
     * @return 	float	Average of thematic advances
1356
     */
1357
    public function get_average_of_advances_by_thematic($thematic_id, $course_code = null)
1358
    {
1359
        $thematic_advance_data = $this->get_thematic_advance_by_thematic_id($thematic_id, $course_code);
1360
        $average = 0;
1361
        if (!empty($thematic_advance_data)) {
1362
            // get all done advances by thematic
1363
            $advances = array();
1364
            $count_done_advances = 0;
1365
            foreach ($thematic_advance_data as $thematic_advance) {
1366
                if ($thematic_advance['done_advance'] == 1) {
1367
                    $count_done_advances++;
1368
                }
1369
                $advances[] = $thematic_advance['done_advance'];
1370
            }
1371
            // calculate average by thematic
1372
            $count_total_advances = count($advances);
1373
            $average = round(($count_done_advances*100)/$count_total_advances);
1374
        }
1375
1376
        return $average;
1377
    }
1378
1379
    /**
1380
     * set attributes for fields of thematic table
1381
     * @param	int		Thematic id
1382
     * @param	string	Thematic title
1383
     * @param	string	Thematic content
1384
     * @param	int		Session id
1385
     * @return void
1386
     */
1387
    public function set_thematic_attributes($id = null, $title = '', $content = '', $session_id = 0)
1388
    {
1389
        $this->thematic_id = $id;
1390
        $this->thematic_title = $title;
1391
        $this->thematic_content = $content;
1392
        $this->session_id = $session_id;
1393
    }
1394
1395
    /**
1396
     * set attributes for fields of thematic_plan table
1397
     * @param	int		Thematic id
1398
     * @param	string	Thematic plan title
1399
     * @param	string	Thematic plan description
1400
     * @param	int		Thematic plan description type
1401
     * @return void
1402
     */
1403
    public function set_thematic_plan_attributes($thematic_id = 0, $title = '', $description = '', $description_type = 0)
1404
    {
1405
        $this->thematic_id = $thematic_id;
1406
        $this->thematic_plan_title = $title;
1407
        $this->thematic_plan_description = $description;
1408
        $this->thematic_plan_description_type = $description_type;
1409
    }
1410
1411
    /**
1412
     * set attributes for fields of thematic_advance table
1413
     * @param	int		Thematic advance id
1414
     * @param	int		Thematic id
1415
     * @param	int		Attendance id
1416
     * @param	string	Content
1417
     * @param	string	Date and time
1418
     * @param	int		Duration in hours
1419
     * @param integer $id
1420
     * @return void
1421
     */
1422
    public function set_thematic_advance_attributes(
1423
        $id = null,
1424
        $thematic_id = 0,
1425
        $attendance_id = 0,
1426
        $content = '',
1427
        $start_date = null,
1428
        $duration = 0
1429
    ) {
1430
        $this->thematic_advance_id = $id;
1431
        $this->thematic_id = $thematic_id;
1432
        $this->attendance_id = $attendance_id;
1433
        $this->thematic_advance_content = $content;
1434
        $this->start_date = $start_date;
1435
        $this->duration = $duration;
1436
    }
1437
1438
    /**
1439
     * set thematic id
1440
     * @param	int	 Thematic id
1441
     * @return void
1442
     */
1443
    public function set_thematic_id($thematic_id)
1444
    {
1445
        $this->thematic_id = $thematic_id;
1446
    }
1447
1448
    /**
1449
     * get thematic id
1450
     * @return integer
1451
     */
1452
    public function get_thematic_id()
1453
    {
1454
        return $this->thematic_id;
1455
    }
1456
1457
    /**
1458
     * Get thematic plan titles by default
1459
     * @return array
1460
     */
1461
    public function get_default_thematic_plan_title()
1462
    {
1463
        $default_thematic_plan_titles = array();
1464
        $default_thematic_plan_titles[1]= get_lang('Objectives');
1465
        $default_thematic_plan_titles[2]= get_lang('SkillToAcquire');
1466
        $default_thematic_plan_titles[3]= get_lang('Methodology');
1467
        $default_thematic_plan_titles[4]= get_lang('Infrastructure');
1468
        $default_thematic_plan_titles[5]= get_lang('Assessment');
1469
        $default_thematic_plan_titles[6]= get_lang('Others');
1470
1471
        return $default_thematic_plan_titles;
1472
    }
1473
1474
    /**
1475
     * Get thematic plan icons by default
1476
     * @return array
1477
     */
1478
    public function get_default_thematic_plan_icon()
1479
    {
1480
        $default_thematic_plan_icon = array();
1481
        $default_thematic_plan_icon[1]= 'icons/32/objective.png';
1482
        $default_thematic_plan_icon[2]= 'icons/32/skills.png';
1483
        $default_thematic_plan_icon[3]= 'icons/32/strategy.png';
1484
        $default_thematic_plan_icon[4]= 'icons/32/laptop.png';
1485
        $default_thematic_plan_icon[5]= 'icons/32/assessment.png';
1486
        $default_thematic_plan_icon[6]= 'icons/32/wizard.png';
1487
1488
        return $default_thematic_plan_icon;
1489
    }
1490
1491
    /**
1492
     * Get questions by default for help
1493
     * @return array
1494
     */
1495
    public function get_default_question()
1496
    {
1497
        $question = array();
1498
        $question[1]= get_lang('ObjectivesQuestions');
1499
        $question[2]= get_lang('SkillToAcquireQuestions');
1500
        $question[3]= get_lang('MethodologyQuestions');
1501
        $question[4]= get_lang('InfrastructureQuestions');
1502
        $question[5]= get_lang('AssessmentQuestions');
1503
1504
        return $question;
1505
    }
1506
}
1507