Completed
Push — master ( cc3037...815120 )
by Julito
29:55
created

Thematic::get_default_question()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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