Passed
Push — master ( cb50ee...221dc3 )
by Julito
07:56
created

Thematic::get_default_question()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Entity\Course;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Course. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Chamilo\CoreBundle\Entity\Session;
7
use Chamilo\CoreBundle\Framework\Container;
8
use Chamilo\CourseBundle\Entity\CThematic;
9
use Chamilo\CourseBundle\Entity\CThematicAdvance;
10
use Chamilo\CourseBundle\Entity\CThematicPlan;
11
12
/**
13
 * Provides functions for thematic option inside attendance tool.
14
 * It's also used like model to thematic_controller (MVC pattern)
15
 * Thematic class can be used to instanciate objects or as a library for thematic control.
16
 *
17
 * @author Christian Fasanando <[email protected]>
18
 * @author Julio Montoya <[email protected]> SQL fixes
19
 */
20
class Thematic
21
{
22
    private $session_id;
23
    private $thematic_id;
24
    private $thematic_title;
25
    private $thematic_content;
26
    private $thematic_plan_id;
0 ignored issues
show
introduced by
The private property $thematic_plan_id is not used, and could be removed.
Loading history...
27
    private $thematic_plan_title;
28
    private $thematic_plan_description;
29
    private $thematic_plan_description_type;
30
    private $thematic_advance_id;
31
    private $attendance_id;
32
    private $thematic_advance_content;
33
    private $start_date;
34
    private $duration;
35
    private $course_int_id;
36
37
    public function __construct()
38
    {
39
        $this->course_int_id = api_get_course_int_id();
40
    }
41
42
    /**
43
     * Get the total number of thematic inside current course and current session.
44
     *
45
     * @see SortableTable#get_total_number_of_items()
46
     */
47
    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
     *
67
     * @param   int     offset of first user to recover
68
     * @param   int     Number of users to get
69
     * @param   int     Column to sort on
70
     * @param   string  Order (ASC,DESC)
71
     *
72
     * @return array
73
     *
74
     * @see SortableTable#get_table_data($from)
75
     */
76
    public function get_thematic_data($from, $number_of_items, $column, $direction)
77
    {
78
        $column = (int) $column;
79
        $from = (int) $from;
80
        $number_of_items = (int) $number_of_items;
81
82
        if (!in_array($direction, ['ASC', 'DESC'])) {
83
            $direction = 'ASC';
84
        }
85
        /*
86
        $course = api_get_course_entity();
87
        $session = api_get_session_entity();
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 = [];
97
        $user_info = api_get_user_info(api_get_user_id());
98
        while ($thematic = Database::fetch_row($res)) {
99
            $session_star = '';
100
            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('Thematic plan'), '', 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('Thematic advance'), '', 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('Are you sure you want to delete').'\')) 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(
121
                            'lesson_plan_na.png',
122
                            get_lang('Thematic plan'),
123
                            '',
124
                            ICON_SIZE_SMALL
125
                        ).'&nbsp;';
126
                        $actions .= Display::return_icon(
127
                            'lesson_plan_calendar_na.png',
128
                            get_lang('Thematic advance'),
129
                            '',
130
                            ICON_SIZE_SMALL
131
                        ).'&nbsp;';
132
                        $actions .= Display::return_icon('edit_na.png', get_lang('Edit'), '', ICON_SIZE_SMALL);
133
                        $actions .= Display::return_icon(
134
                            'delete_na.png',
135
                            get_lang('Delete'),
136
                            '',
137
                            ICON_SIZE_SMALL
138
                        ).'&nbsp;';
139
                        $actions .= Display::url(
140
                            Display::return_icon('cd.gif', get_lang('Copy')),
141
                            'index.php?'.api_get_cidreq().'&action=thematic_copy&thematic_id='.$thematic[0]
142
                        );
143
                    }
144
                } else {
145
                    $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_plan_list&thematic_id='.$thematic[0].'">'.
146
                        Display::return_icon('lesson_plan.png', get_lang('Thematic plan'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
147
                    $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_list&thematic_id='.$thematic[0].'">'.
148
                        Display::return_icon('lesson_plan_calendar.png', get_lang('Thematic advance'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
149
150
                    if ($thematic[2] > 1) {
151
                        $actions .= '<a href="'.api_get_self().'?action=moveup&'.api_get_cidreq().'&thematic_id='.$thematic[0].'">'.
152
                            Display::return_icon('up.png', get_lang('Up'), '', ICON_SIZE_SMALL).'</a>';
153
                    } else {
154
                        $actions .= Display::return_icon('up_na.png', '&nbsp;', '', ICON_SIZE_SMALL);
155
                    }
156
                    /*if ($thematic[2] < self::get_max_thematic_item()) {
157
                        $actions .= '<a href="'.api_get_self().'?action=movedown&a'.api_get_cidreq().'&thematic_id='.$thematic[0].'">'.
158
                            Display::return_icon('down.png', get_lang('down'), '', ICON_SIZE_SMALL).'</a>';
159
                    } else {
160
                        $actions .= Display::return_icon('down_na.png', '&nbsp;', '', ICON_SIZE_SMALL);
161
                    }
162
                    $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_edit&thematic_id='.$thematic[0].'">'.
163
                        Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).'</a>';
164
                    $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('Are you sure you want to delete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=thematic_delete&thematic_id='.$thematic[0].'">'.
165
                        Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
166
                }
167
                $thematics[] = [$thematic[0], $thematic[1], $actions];
168
            }
169
        }*/
170
171
        //return $thematics;
172
    }
173
174
    /**
175
     * Get the maximum display order of the thematic item.
176
     *
177
     * @return int Maximum display order
178
     */
179
    public function get_max_thematic_item(Course $course, Session $session = null)
180
    {
181
        // Database table definition
182
        /*$tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
183
        $session_id = api_get_session_id();
184
        if ($use_session) {
185
            $condition_session = api_get_session_condition($session_id);
186
        } else {
187
            $condition_session = '';
188
        }
189
        $course_id = api_get_course_int_id();
190
        $sql = "SELECT MAX(display_order)
191
                FROM $tbl_thematic
192
                WHERE c_id = $course_id AND active = 1 $condition_session";
193
        $rs = Database::query($sql);
194
        $row = Database::fetch_array($rs);
195
196
        return $row[0];*/
197
    }
198
199
    /**
200
     * Move a thematic.
201
     *
202
     * @param string $direction   (up, down)
203
     * @param int    $thematic_id
204
     */
205
    public function moveThematic($direction, $thematic_id, $course, $session = null)
206
    {
207
        // Database table definition
208
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
209
210
        // sort direction
211
        if ('up' === $direction) {
212
            $sortorder = 'DESC';
213
        } else {
214
            $sortorder = 'ASC';
215
        }
216
        $course_id = api_get_course_int_id();
217
        $session_id = api_get_session_id();
218
        $condition_session = api_get_session_condition($session_id);
219
220
        $sql = "SELECT id, display_order
221
                FROM $tbl_thematic
222
                WHERE c_id = $course_id AND active = 1 $condition_session
223
                ORDER BY display_order $sortorder";
224
        $res = Database::query($sql);
225
        $found = false;
226
227
        // Variable definition
228
        $current_id = 0;
229
        $next_id = 0;
230
        while ($row = Database::fetch_array($res)) {
231
            if ($found && empty($next_id)) {
232
                $next_id = intval($row['id']);
233
                $next_display_order = intval($row['display_order']);
234
            }
235
236
            if ($row['id'] == $thematic_id) {
237
                $current_id = intval($thematic_id);
238
                $current_display_order = intval($row['display_order']);
239
                $found = true;
240
            }
241
        }
242
243
        // get last done thematic advance before move thematic list
244
        $last_done_thematic_advance = $this->get_last_done_thematic_advance($course, $session);
245
246
        if (!empty($next_display_order) && !empty($current_id)) {
247
            $sql = "UPDATE $tbl_thematic SET display_order = $next_display_order
248
                    WHERE c_id = $course_id AND id = $current_id ";
249
            Database::query($sql);
250
        }
251
        if (!empty($current_display_order) && !empty($next_id)) {
252
            $sql = "UPDATE $tbl_thematic SET
253
                    display_order = $current_display_order
254
                    WHERE c_id = $course_id AND id = $next_id ";
255
            Database::query($sql);
256
        }
257
258
        // update done advances with de current thematic list
259
        $this->updateDoneThematicAdvance($last_done_thematic_advance, $course, $session);
260
    }
261
262
    /**
263
     * Get thematic list.
264
     *
265
     * @return array Thematic data
266
     */
267
    public static function getThematicList(Course $course, Session $session = null)
268
    {
269
        // set current course and session
270
        /*$tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
271
        $course_info = api_get_course_info($course_code);
272
        $course_id = $course_info['real_id'];
273
274
        if (!empty($session_id)) {
275
            $session_id = (int) $session_id;
276
        } else {
277
            $session_id = api_get_session_id();
278
        }
279
280
        $data = [];
281
        if (empty($session_id)) {
282
            $condition_session = api_get_session_condition(0);
283
        } else {
284
            $condition_session = api_get_session_condition($session_id, true, true);
285
        }
286
        $condition = " WHERE active = 1 $condition_session ";*/
287
288
        $repo = Container::getThematicRepository();
289
        $qb = $repo->getResourcesByCourse($course, $session);
290
291
        return $qb->getQuery()->getResult();
292
293
        /*$sql = "SELECT *
294
                FROM $tbl_thematic $condition AND c_id = $course_id
295
                ORDER BY display_order ";
296
297
        $res = Database::query($sql);
298
        if (Database::num_rows($res) > 0) {
299
            while ($row = Database::fetch_array($res, 'ASSOC')) {
300
                $entity = $repo->find($row['iid']);
301
                $data[$row['iid']] = $entity;
302
            }
303
        }
304
305
        return $data;*/
306
    }
307
308
    /**
309
     * Insert or update a thematic.
310
     *
311
     * @return CThematic
312
     */
313
    public function thematic_save()
314
    {
315
        // protect data
316
        $id = intval($this->thematic_id);
317
        $title = $this->thematic_title;
318
        $content = $this->thematic_content;
319
        $session_id = intval($this->session_id);
320
321
        // get the maximum display order of all the glossary items
322
        //$max_thematic_item = $this->get_max_thematic_item(false);
323
324
        $repo = Container::getThematicRepository();
325
326
        if (empty($id)) {
327
            $thematic = new CThematic();
328
            $courseEntity = api_get_course_entity();
329
            $thematic
330
                ->setTitle($title)
331
                ->setContent($content)
332
                ->setActive(1)
333
                //->setDisplayOrder($max_thematic_item + 1)
334
                ->setDisplayOrder(0)
335
                ->setParent($courseEntity)
336
                ->addCourseLink($courseEntity, api_get_session_entity())
337
            ;
338
339
            $repo->create($thematic);
340
341
            // insert
342
            /*$params = [
343
                'c_id' => $this->course_int_id,
344
                'active' => 1,
345
                'display_order' => intval($max_thematic_item) + 1,
346
                'session_id' => $session_id,
347
            ];*/
348
            $last_id = $thematic->getIid();
349
            if ($last_id) {
350
                /*api_item_property_update(
351
                    $_course,
352
                    'thematic',
353
                    $last_id,
354
                    'ThematicAdded',
355
                    $user_id
356
                );*/
357
            }
358
        } else {
359
            $thematic = $repo->find($id);
360
            if ($thematic) {
361
                $thematic
362
                    ->setTitle($title)
363
                    ->setContent($content)
364
                ;
365
                $repo->update($thematic);
366
            }
367
368
            // Update
369
            /*$params = [
370
                'title' => $title,
371
                'content' => $content,
372
                'session_id' => $session_id,
373
            ];
374
375
            Database::update(
376
                $tbl_thematic,
377
                $params,
378
                ['id  = ? AND c_id = ?' => [$id, $this->course_int_id]]
379
            );
380
381
            $last_id = $id;
382
383
            // save inside item property table
384
            api_item_property_update(
385
                $_course,
386
                'thematic',
387
                $last_id,
388
                'ThematicUpdated',
389
                $user_id
390
            );*/
391
        }
392
393
        return $thematic;
394
    }
395
396
    /**
397
     * Delete logically (set active field to 0) a thematic.
398
     *
399
     * @param int|array One or many thematic ids
400
     *
401
     * @return int Affected rows
402
     */
403
    public function delete($thematic_id)
404
    {
405
        $_course = api_get_course_info();
406
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
407
        $affected_rows = 0;
408
        $user_id = api_get_user_id();
409
        $course_id = api_get_course_int_id();
410
411
        if (is_array($thematic_id)) {
412
            foreach ($thematic_id as $id) {
413
                $id = intval($id);
414
                $sql = "UPDATE $tbl_thematic SET active = 0
415
                        WHERE c_id = $course_id AND id = $id";
416
                $result = Database::query($sql);
417
                $affected_rows += Database::affected_rows($result);
418
                if (!empty($affected_rows)) {
419
                    // update row item property table
420
                    /*api_item_property_update(
421
                        $_course,
422
                        'thematic',
423
                        $id,
424
                        'ThematicDeleted',
425
                        $user_id
426
                    );*/
427
                }
428
            }
429
        } else {
430
            $thematic_id = intval($thematic_id);
431
            $sql = "UPDATE $tbl_thematic SET active = 0
432
                    WHERE c_id = $course_id AND id = $thematic_id";
433
            $result = Database::query($sql);
434
            $affected_rows = Database::affected_rows($result);
435
            if (!empty($affected_rows)) {
436
                // update row item property table
437
                /*api_item_property_update(
438
                    $_course,
439
                    'thematic',
440
                    $thematic_id,
441
                    'ThematicDeleted',
442
                    $user_id
443
                );*/
444
            }
445
        }
446
447
        return $affected_rows;
448
    }
449
450
    /**
451
     * @param int $thematicId
452
     */
453
    public function copy($thematicId)
454
    {
455
        $repo = Container::getThematicRepository();
456
        /** @var CThematic $thematic */
457
        $thematic = $repo->find($thematicId);
458
        if (null === $thematic) {
459
            return false;
460
        }
461
462
        $thematicManager = new Thematic();
463
        $thematicManager->set_thematic_attributes(
464
            '',
465
            $thematic->getTitle().' - '.get_lang('Copy'),
466
            $thematic->getContent(),
467
            api_get_session_id()
468
        );
469
        $new_thematic_id = $thematicManager->thematic_save();
470
471
        if (!empty($new_thematic_id)) {
472
            $thematic_advanced = $thematic->getAdvances();
473
            if (!empty($thematic_advanced)) {
474
                foreach ($thematic_advanced as $item) {
475
                    $thematic = new Thematic();
476
                    $thematic->set_thematic_advance_attributes(
477
                        0,
478
                        $new_thematic_id,
479
                        0,
480
                        $item->getContent(),
481
                        $item->getStartDate()->format('Y-m-d H:i:s'),
482
                        $item->getDuration()
483
                    );
484
                    $thematic->thematic_advance_save();
485
                }
486
            }
487
            $thematic_plan = $thematic->getPlans();
0 ignored issues
show
Bug introduced by
The method getPlans() does not exist on Thematic. ( Ignorable by Annotation )

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

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

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

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

Loading history...
488
            if (!empty($thematic_plan)) {
489
                foreach ($thematic_plan as $item) {
490
                    $thematic = new Thematic();
491
                    $thematic->set_thematic_plan_attributes(
492
                        $new_thematic_id,
493
                        $item->getTitle(),
494
                        $item->getDescription(),
495
                        $item->getDescriptionType()
496
                    );
497
                    $thematic->thematic_plan_save();
498
                }
499
            }
500
        }
501
    }
502
503
    /**
504
     * Get the total number of thematic advance inside current course.
505
     *
506
     * @see SortableTable#get_total_number_of_items()
507
     */
508
    public static function get_number_of_thematic_advances(array $params)
509
    {
510
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
511
        $course_id = api_get_course_int_id();
512
        $thematic_id = (int) $params['thematic_id'];
513
514
        $sql = "SELECT COUNT(iid) AS total_number_of_items
515
                FROM $table
516
                WHERE thematic_id = $thematic_id ";
517
        $res = Database::query($sql);
518
        $obj = Database::fetch_object($res);
519
520
        return $obj->total_number_of_items;
521
    }
522
523
    /**
524
     * Get the thematic advances to display on the current page (fill the sortable-table).
525
     *
526
     * @param   int     offset of first user to recover
527
     * @param   int     Number of users to get
528
     * @param   int     Column to sort on
529
     * @param   string  Order (ASC,DESC)
530
     *
531
     * @return array
532
     *
533
     * @see SortableTable#get_table_data($from)
534
     */
535
    public static function get_thematic_advance_data($from, $number_of_items, $column, $direction, $params = [])
536
    {
537
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
538
        $column = (int) $column;
539
        $from = (int) $from;
540
        $number_of_items = (int) $number_of_items;
541
        if (!in_array($direction, ['ASC', 'DESC'])) {
542
            $direction = 'ASC';
543
        }
544
        $data = [];
545
        $course_id = api_get_course_int_id();
546
        $thematic_id = (int) $params['thematic_id'];
547
        if (api_is_allowed_to_edit(null, true)) {
548
            $sql = "SELECT iid AS col0, start_date AS col1, duration AS col2, content AS col3
549
                    FROM $table
550
                    WHERE thematic_id = $thematic_id
551
                    ORDER BY col$column $direction
552
                    LIMIT $from,$number_of_items ";
553
554
            /*$list = api_get_item_property_by_tool(
555
                'thematic_advance',
556
                api_get_course_id(),
557
                api_get_session_id()
558
            );*/
559
560
            /*$elements = [];
561
            foreach ($list as $value) {
562
                $elements[] = $value['ref'];
563
            }*/
564
565
            $res = Database::query($sql);
566
            $i = 1;
567
            while ($thematic_advance = Database::fetch_row($res)) {
568
                //if (in_array($thematic_advance[0], $elements)) {
569
                $thematic_advance[1] = api_get_local_time($thematic_advance[1]);
570
                $thematic_advance[1] = api_format_date($thematic_advance[1], DATE_TIME_FORMAT_LONG);
571
                $actions = '';
572
                $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_edit&thematic_id='.$thematic_id.'&thematic_advance_id='.$thematic_advance[0].'">'.
573
                        Display::return_icon('edit.png', get_lang('Edit'), '', 22).'</a>';
574
                $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('Are you sure you want to delete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=thematic_advance_delete&thematic_id='.$thematic_id.'&thematic_advance_id='.$thematic_advance[0].'">'.
575
                        Display::return_icon('delete.png', get_lang('Delete'), '', 22).'</a></center>';
576
                $data[] = [$i, $thematic_advance[1], $thematic_advance[2], $thematic_advance[3], $actions];
577
                $i++;
578
                // }
579
            }
580
        }
581
582
        return $data;
583
    }
584
585
    /**
586
     * get thematic advance data by thematic id.
587
     *
588
     * @param int    $thematic_id
589
     * @param string $course_code Course code (optional)
590
     *
591
     * @return array data
592
     */
593
    public function get_thematic_advance_by_thematic_id($thematic_id, $course_code = null)
594
    {
595
        $course_info = api_get_course_info($course_code);
596
        $course_id = $course_info['real_id'];
597
598
        $repo = Container::getThematicAdvanceRepository();
599
600
        $courseEntity = api_get_course_entity($course_id);
601
        $sessionEntity = api_get_session_entity(api_get_session_id());
602
603
        $qb = $repo->getResourcesByCourse($courseEntity, $sessionEntity);
604
605
        $qb->andWhere($qb->expr()->eq('resource.thematic', $thematic_id));
606
607
        return $qb->getQuery()->getResult();
608
    }
609
610
    public function getThematicAdvance($id): ?CThematicAdvance
611
    {
612
        $repo = Container::getThematicAdvanceRepository();
613
614
        return $repo->find($id);
615
    }
616
617
    /**
618
     * Get thematic advance list.
619
     *
620
     * @param string $course_code      Course code (optional)
621
     * @param bool   $force_session_id Force to have a session id
622
     * @param bool   $withLocalTime    Force start_date to local time
623
     *
624
     * @return CThematicAdvance[]
625
     */
626
    public function get_thematic_advance_list($course_code = null, $force_session_id = false, $withLocalTime = false)
627
    {
628
        $course_info = api_get_course_info($course_code);
629
        $course_id = $course_info['real_id'];
630
        $repo = Container::getThematicAdvanceRepository();
631
632
        $courseEntity = api_get_course_entity($course_id);
633
        $sessionEntity = null;
634
        if ($force_session_id) {
635
            $sessionEntity = api_get_session_entity(api_get_session_id());
636
            /*$list = api_get_item_property_by_tool(
637
                'thematic_advance',
638
                $course_info['code'],
639
                api_get_session_id()
640
            );
641
            foreach ($list as $value) {
642
                $elements[$value['ref']] = $value;
643
            }*/
644
        }
645
646
        $qb = $repo->getResourcesByCourse($courseEntity, $sessionEntity);
647
        $qb->orderBy('resource.startDate', 'DESC');
648
649
        return $qb->getQuery()->getResult();
650
    }
651
652
    /**
653
     * insert or update a thematic advance.
654
     *
655
     * @todo problem
656
     *
657
     * @return int last thematic advance id
658
     */
659
    public function thematic_advance_save()
660
    {
661
        $id = (int) $this->thematic_advance_id;
662
        $thematic_id = intval($this->thematic_id);
663
        $attendance_id = intval($this->attendance_id);
664
        $content = $this->thematic_advance_content;
665
        $start_date = $this->start_date;
666
        $duration = intval($this->duration);
667
        $repo = Container::getThematicAdvanceRepository();
668
        $em = Database::getManager();
669
670
        /** @var CThematicAdvance $advance */
671
        $advance = $repo->find($id);
672
673
        $repoThematic = Container::getThematicRepository();
674
        $thematic = $repoThematic->find($thematic_id);
675
        $attendanceRepo = Container::getAttendanceRepository();
676
        $attendance = $attendanceRepo->find($attendance_id);
677
678
        $last_id = null;
679
        if (null === $advance) {
680
            $advance = new CThematicAdvance();
681
            $advance
682
                ->setContent($content)
683
                ->setThematic($thematic)
684
                ->setAttendance($attendance)
685
                ->setStartDate(api_get_utc_datetime($start_date, true, true))
686
                ->setDuration($duration)
687
            ;
688
689
            if ($thematic) {
690
                $advance->setThematic($thematic);
691
            }
692
693
            if ($attendance) {
694
                $advance->setAttendance($attendance);
695
            }
696
697
            $courseEntity = api_get_course_entity();
698
            /*$advance
699
                ->setParent($courseEntity)
700
                ->addCourseLink($courseEntity, api_get_session_entity())
701
            ;*/
702
            $em->persist($advance);
703
            $em->flush();
704
705
            $last_id = $advance->getIid();
706
        } else {
707
            $advance
708
                ->setContent($content)
709
                ->setStartDate(api_get_utc_datetime($start_date, true, true))
710
                ->setDuration($duration)
711
            ;
712
713
            if ($thematic) {
714
                $advance->setThematic($thematic);
715
            }
716
717
            if ($attendance) {
718
                $advance->setAttendance($attendance);
719
            }
720
            //$repo->update($advance);
721
            $em->persist($advance);
722
            $em->flush();
723
        }
724
725
        return $last_id;
726
    }
727
728
    /**
729
     * delete  thematic advance.
730
     *
731
     * @param int $id Thematic advance id
732
     *
733
     * @return int Affected rows
734
     */
735
    public function thematic_advance_destroy($id)
736
    {
737
        $repo = Container::getThematicAdvanceRepository();
738
        $advance = $repo->find($id);
739
740
        if ($advance) {
741
            $repo->delete($advance);
742
        }
743
744
        return true;
745
746
        /*$_course = api_get_course_info();
747
        $course_id = api_get_course_int_id();
748
749
        // definition database table
750
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
751
752
        // protect data
753
        $id = intval($id);
754
        $user_id = api_get_user_id();
755
756
        $sql = "DELETE FROM $table
757
                WHERE c_id = $course_id AND id = $id ";
758
        $result = Database::query($sql);
759
        $affected_rows = Database::affected_rows($result);
760
761
        if ($affected_rows) {
762
            api_item_property_update(
763
                $_course,
764
                'thematic_advance',
765
                $id,
766
                'ThematicAdvanceDeleted',
767
                $user_id
768
            );
769
        }
770
771
        return $affected_rows;*/
772
    }
773
774
    /**
775
     * get thematic plan data.
776
     *
777
     * @param int Thematic id (optional), get data by thematic id
778
     * @param int Thematic plan description type (optional), get data by description type
779
     *
780
     * @return array Thematic plan data
781
     */
782
    public function get_thematic_plan_data($thematic_id = null, $description_type = null)
783
    {
784
        // definition database table
785
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
786
        $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
787
        $course_id = api_get_course_int_id();
788
789
        $repo = Container::getThematicPlanRepository();
790
791
        $courseEntity = api_get_course_entity();
792
        $sessionEntity = api_get_session_entity(api_get_session_id());
793
794
        $qb = $repo->getResourcesByCourse($courseEntity, $sessionEntity);
795
        $result = $qb->getQuery()->getResult();
796
797
        //var_dump(count($result));
798
799
        $data = [];
800
        $condition = '';
801
        //var_dump($thematic_id, $description_type);
802
        if (!empty($thematic_id)) {
803
            $qb->andWhere($qb->expr()->eq('resource.thematic', $thematic_id));
804
            //$thematic_id = intval($thematic_id);
805
            //$condition .= " AND thematic_id = $thematic_id ";
806
        }
807
808
        if (!empty($description_type)) {
809
            $qb->andWhere($qb->expr()->eq('resource.descriptionType', $description_type));
810
            //$condition .= " AND description_type = $description_type ";
811
        }
812
813
        return $qb->getQuery()->getResult();
814
815
        /*$items_from_course = api_get_item_property_by_tool(
816
            'thematic_plan',
817
            api_get_course_id(),
818
            0
819
        );
820
        $items_from_session = api_get_item_property_by_tool(
821
            'thematic_plan',
822
            api_get_course_id(),
823
            api_get_session_id()
824
        );*/
825
826
        $thematic_plan_complete_list = [];
0 ignored issues
show
Unused Code introduced by
$thematic_plan_complete_list = array() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
827
        $thematic_plan_id_list = [];
828
829
        /*if (!empty($items_from_course)) {
830
            foreach ($items_from_course as $item) {
831
                $thematic_plan_id_list[] = $item['ref'];
832
                $thematic_plan_complete_list[$item['ref']] = $item;
833
            }
834
        }
835
836
        if (!empty($items_from_session)) {
837
            foreach ($items_from_session as $item) {
838
                $thematic_plan_id_list[] = $item['ref'];
839
                $thematic_plan_complete_list[$item['ref']] = $item;
840
            }
841
        }*/
842
843
        if (!empty($thematic_plan_id_list)) {
844
            $sql = "SELECT
845
                        tp.id, thematic_id, tp.title, description, description_type, t.session_id
846
                    FROM $tbl_thematic_plan tp
847
                    INNER JOIN $tbl_thematic t
848
                    ON (t.id = tp.thematic_id AND t.c_id = tp.c_id)
849
                    WHERE
850
                        t.c_id = $course_id AND
851
                        tp.c_id = $course_id
852
                        $condition AND
853
                        tp.id IN (".implode(', ', $thematic_plan_id_list).') ';
854
855
            $rs = Database::query($sql);
856
857
            if (Database::num_rows($rs)) {
858
                if (!isset($thematic_id) && !isset($description_type)) {
859
                    // group all data group by thematic id
860
                    $tmp = [];
861
                    while ($row = Database::fetch_array($rs, 'ASSOC')) {
862
                        $tmp[] = $row['thematic_id'];
863
                        if (in_array($row['thematic_id'], $tmp)) {
864
                            $row['session_id'] = $thematic_plan_complete_list[$row['id']];
865
                            $data[$row['thematic_id']][$row['description_type']] = $row;
866
                        }
867
                    }
868
                } else {
869
                    while ($row = Database::fetch_array($rs, 'ASSOC')) {
870
                        $row['session_id'] = $thematic_plan_complete_list[$row['id']];
871
                        $data[] = $row;
872
                    }
873
                }
874
            }
875
        }
876
877
        return $data;
878
    }
879
880
    /**
881
     * insert or update a thematic plan.
882
     *
883
     * @return int affected rows
884
     */
885
    public function thematic_plan_save()
886
    {
887
        $_course = api_get_course_info();
888
        // definition database table
889
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
890
891
        // protect data
892
        $thematic_id = intval($this->thematic_id);
893
        $title = $this->thematic_plan_title;
894
        $description = $this->thematic_plan_description;
895
        $description_type = intval($this->thematic_plan_description_type);
896
        $user_id = api_get_user_id();
897
        $course_id = api_get_course_int_id();
898
899
        /*$list = api_get_item_property_by_tool(
900
            'thematic_plan',
901
            api_get_course_id(),
902
            api_get_session_id()
903
        );
904
905
        $elements_to_show = [];
906
        foreach ($list as $value) {
907
            $elements_to_show[] = $value['ref'];
908
        }
909
        $condition = '';
910
        if (!empty($elements_to_show)) {
911
            $condition = 'AND id IN ('.implode(',', $elements_to_show).') ';
912
        }*/
913
914
        $repo = Container::getThematicPlanRepository();
915
        $criteria = [
916
            'thematic' => $thematic_id,
917
            'descriptionType' => $description_type,
918
        ];
919
        /** @var CThematicPlan $plan */
920
        $plan = $repo->findOneBy($criteria);
921
        $em = Database::getManager();
922
        // check thematic plan type already exists
923
        /*$sql = "SELECT id FROM $tbl_thematic_plan
924
                WHERE
925
                    c_id = $course_id AND
926
                    thematic_id = $thematic_id AND
927
                    description_type = '$description_type'";
928
        $rs = Database::query($sql);*/
929
        if ($plan) {
0 ignored issues
show
introduced by
$plan is of type Chamilo\CourseBundle\Entity\CThematicPlan, thus it always evaluated to true.
Loading history...
930
            $plan
931
                ->setTitle($title)
932
                ->setDescription($description)
933
            ;
934
            $em->persist($plan);
935
            $em->flush();
936
        //$repo->update($plan);
937
938
        // update
939
            /*$params = [
940
                'title' => $title,
941
                'description' => $description,
942
            ];
943
            Database::update(
944
                $tbl_thematic_plan,
945
                $params,
946
                ['c_id = ? AND id = ?' => [$course_id, $thematic_plan_id]]
947
            );
948
949
            api_item_property_update(
950
                $_course,
951
                'thematic_plan',
952
                $thematic_plan_id,
953
                'ThematicPlanUpdated',
954
                $user_id
955
            );*/
956
        } else {
957
            $thematic = Container::getThematicRepository()->find($thematic_id);
958
            $plan = new CThematicPlan();
959
            $plan
960
                ->setTitle($title)
961
                ->setDescription($description)
962
                ->setThematic($thematic)
963
                ->setDescriptionType($description_type)
964
                //->setParent($course)
965
                //->addCourseLink($course, api_get_session_entity())
966
            ;
967
968
            //$repo->create($plan);
969
            $em->persist($plan);
970
            $em->flush();
971
            if ($plan && $plan->getIid()) {
972
                /*
973
                api_item_property_update(
974
                    $_course,
975
                    'thematic_plan',
976
                    $last_id,
977
                    'ThematicPlanAdded',
978
                    $user_id
979
                );*/
980
            }
981
        }
982
983
        return true;
984
    }
985
986
    /**
987
     * Delete a thematic plan description.
988
     *
989
     * @param int $thematic_id Thematic id
990
     *
991
     * @return int Affected rows
992
     */
993
    public function thematic_plan_destroy($thematic_id, $descriptionType)
994
    {
995
        $repo = Container::getThematicRepository();
996
997
        /** @var CThematic $thematic */
998
        $thematic = $repo->find($thematic_id);
999
1000
        foreach ($thematic->getPlans() as $plan) {
1001
            if ($descriptionType == $plan->getDescriptionType()) {
1002
                $thematic->getPlans()->removeElement($plan);
1003
            }
1004
        }
1005
1006
        $repo->update($thematic);
1007
1008
        return false;
1009
1010
        $_course = api_get_course_info();
0 ignored issues
show
Unused Code introduced by
$_course = api_get_course_info() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
1011
        // definition database table
1012
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
1013
1014
        // protect data
1015
        $thematic_id = intval($thematic_id);
1016
        $description_type = intval($description_type);
1017
        $user_id = api_get_user_id();
1018
        $course_info = api_get_course_info();
1019
        $course_id = $course_info['real_id'];
1020
1021
        // get thematic plan id
1022
        $thematic_plan_data = $this->get_thematic_plan_data($thematic_id, $description_type);
1023
        $thematic_plan_id = $thematic_plan_data[0]['id'];
1024
1025
        // delete
1026
        $sql = "DELETE FROM $tbl_thematic_plan
1027
                WHERE
1028
                    c_id = $course_id AND
1029
                    thematic_id = $thematic_id AND
1030
                    description_type = $description_type ";
1031
        $result = Database::query($sql);
1032
        $affected_rows = Database::affected_rows($result);
1033
        if ($affected_rows) {
1034
            /*api_item_property_update(
1035
                $_course,
1036
                'thematic_plan',
1037
                $thematic_plan_id,
1038
                'ThematicPlanDeleted',
1039
                $user_id
1040
            );*/
1041
        }
1042
1043
        return $affected_rows;
1044
    }
1045
1046
    /**
1047
     * Get next description type for a new thematic plan description (option 'others').
1048
     *
1049
     * @param int $thematic_id Thematic id
1050
     *
1051
     * @return int New Description type
1052
     */
1053
    public function get_next_description_type($thematic_id)
1054
    {
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
        $course_id = api_get_course_int_id();
1061
1062
        $sql = "SELECT MAX(description_type) as max
1063
                FROM $tbl_thematic_plan
1064
                WHERE
1065
                    c_id = $course_id AND
1066
                    thematic_id = $thematic_id AND
1067
                    description_type >= ".ADD_THEMATIC_PLAN;
1068
        $rs = Database::query($sql);
1069
        $row = Database::fetch_array($rs);
1070
        $last_description_type = $row['max'];
1071
1072
        if (isset($last_description_type)) {
1073
            $next_description_type = $last_description_type + 1;
1074
        } else {
1075
            $next_description_type = ADD_THEMATIC_PLAN;
1076
        }
1077
1078
        return $next_description_type;
1079
    }
1080
1081
    /**
1082
     * update done thematic advances from thematic details interface.
1083
     *
1084
     * @return int Affected rows
1085
     */
1086
    public function updateDoneThematicAdvance($advanceId, $course, $session = null)
1087
    {
1088
        $repo = Container::getThematicRepository();
1089
        $em = Database::getManager();
1090
        $list = self::getThematicList($course, $session);
1091
        $ordered = [];
1092
1093
        foreach ($list as $thematic) {
1094
            $done = true;
1095
            foreach ($thematic->getAdvances() as $advance) {
1096
                $ordered[] = $advance;
1097
                /*if ($advanceId === $advance->getIid()) {
1098
                    $done = false;
1099
                }*/
1100
                $advance->setDoneAdvance($done);
1101
            }
1102
        }
1103
1104
        $done = true;
1105
        foreach ($ordered as $advance) {
1106
            if ($advanceId === $advance->getIid()) {
1107
                $done = false;
1108
                $advance->setDoneAdvance(true);
1109
                $repo->update($advance, false);
1110
                continue;
1111
            }
1112
1113
            $advance->setDoneAdvance($done);
1114
            $repo->update($advance, false);
1115
        }
1116
1117
        $em->flush();
1118
1119
        return true;
1120
1121
        /*$_course = api_get_course_info();
1122
        $thematic_data = self::getThematicList(api_get_course_id());
1123
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
1124
1125
        $affected_rows = 0;
1126
        $user_id = api_get_user_id();*/
1127
1128
        /*$all = [];
1129
        if (!empty($thematic_data)) {
1130
            foreach ($thematic_data as $thematic) {
1131
                $thematic_id = $thematic['id'];
1132
                if (!empty($thematic_advance_data[$thematic['id']])) {
1133
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1134
                        $all[] = $thematic_advance['id'];
1135
                    }
1136
                }
1137
            }
1138
        }*/
1139
        $error = null;
0 ignored issues
show
Unused Code introduced by
$error = null is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
1140
        $a_thematic_advance_ids = [];
1141
        $course_id = api_get_course_int_id();
1142
        $sessionId = api_get_session_id();
1143
1144
        /*if (!empty($thematic_data)) {
1145
            foreach ($thematic_data as $thematic) {
1146
                $my_affected_rows = 0;
1147
                $thematic_id = $thematic['id'];
1148
                if (!empty($thematic_advance_data[$thematic['id']])) {
1149
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1150
                        $item_info = api_get_item_property_info(
1151
                            api_get_course_int_id(),
1152
                            'thematic_advance',
1153
                            $thematic_advance['id'],
1154
                            $sessionId
1155
                        );
1156
1157
                        if ($item_info['session_id'] == $sessionId) {
1158
                            $a_thematic_advance_ids[] = $thematic_advance['id'];
1159
                            // update done thematic for previous advances ((done_advance = 1))
1160
                            $upd = "UPDATE $table SET
1161
                                    done_advance = 1
1162
                                    WHERE c_id = $course_id AND id = ".$thematic_advance['id'].' ';
1163
                            $result = Database::query($upd);
1164
                            $my_affected_rows = Database::affected_rows($result);
1165
                            $affected_rows += $my_affected_rows;
1166
                            //if ($my_affected_rows) {
1167
                            api_item_property_update(
1168
                                $_course,
1169
                                'thematic_advance',
1170
                                $thematic_advance['id'],
1171
                                'ThematicAdvanceDone',
1172
                                $user_id
1173
                            );
1174
                            //}
1175
                            if ($thematic_advance['id'] == $thematic_advance_id) {
1176
                                break 2;
1177
                            }
1178
                        }
1179
                    }
1180
                }
1181
            }
1182
        }*/
1183
1184
        // Update done thematic for others advances (done_advance = 0)
1185
        if (!empty($a_thematic_advance_ids) && count($a_thematic_advance_ids) > 0) {
1186
            $diff = array_diff($all, $a_thematic_advance_ids);
1187
            if (!empty($diff)) {
1188
                $upd = "UPDATE $table SET done_advance = 0
1189
                        WHERE c_id = $course_id AND id IN(".implode(',', $diff).') ';
1190
                Database::query($upd);
1191
            }
1192
1193
            // update item_property
1194
            $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
1195
            $sql = "SELECT ref FROM $tbl_item_property
1196
                    WHERE
1197
                        c_id = $course_id AND
1198
                        tool='thematic_advance' AND
1199
                        lastedit_type='ThematicAdvanceDone' AND
1200
                        session_id = $sessionId ";
1201
            // get all thematic advance done
1202
            $rs_thematic_done = Database::query($sql);
1203
            if (Database::num_rows($rs_thematic_done) > 0) {
1204
                while ($row_thematic_done = Database::fetch_array($rs_thematic_done)) {
1205
                    $ref = $row_thematic_done['ref'];
1206
                    if (in_array($ref, $a_thematic_advance_ids)) {
1207
                        continue;
1208
                    }
1209
                    // update items
1210
                    $sql = "UPDATE $tbl_item_property SET
1211
                                lastedit_date='".api_get_utc_datetime()."',
1212
                                lastedit_type='ThematicAdvanceUpdated',
1213
                                lastedit_user_id = $user_id
1214
                            WHERE
1215
                                c_id = $course_id AND
1216
                                tool='thematic_advance' AND
1217
                                ref=$ref AND
1218
                                session_id = $sessionId  ";
1219
                    Database::query($sql);
1220
                }
1221
            }
1222
        }
1223
1224
        return $affected_rows;
1225
    }
1226
1227
    /**
1228
     * Get last done thematic advance from thematic details interface.
1229
     *
1230
     * @return int Last done thematic advance id
1231
     */
1232
    public function get_last_done_thematic_advance($course, $session = null)
1233
    {
1234
        $thematic_data = self::getThematicList($course, $session);
1235
1236
        $a_thematic_advance_ids = [];
1237
        $last_done_advance_id = 0;
1238
        if (!empty($thematic_data)) {
1239
            /** @var CThematic $thematic */
1240
            foreach ($thematic_data as $thematic) {
1241
                $id = $thematic->getIid();
1242
                if ($thematic->getAdvances()->count()) {
1243
                    foreach ($thematic->getAdvances() as $thematic_advance) {
1244
                        if (1 == $thematic_advance->getDoneAdvance()) {
1245
                            $a_thematic_advance_ids[] = $thematic_advance->getIid();
1246
                        }
1247
                    }
1248
                }
1249
            }
1250
        }
1251
        if (!empty($a_thematic_advance_ids)) {
1252
            $last_done_advance_id = array_pop($a_thematic_advance_ids);
1253
            $last_done_advance_id = intval($last_done_advance_id);
1254
        }
1255
1256
        return $last_done_advance_id;
1257
    }
1258
1259
    /**
1260
     * Get next thematic advance not done from thematic details interface.
1261
     *
1262
     * @param   int Offset (if you want to get an item that is not directly the next)
1263
     *
1264
     * @return int next thematic advance not done
1265
     */
1266
    public function get_next_thematic_advance_not_done($offset = 1, $course, $session = null)
1267
    {
1268
        $thematic_data = self::getThematicList($course, $session);
1269
        $thematic_advance_data = $this->get_thematic_advance_list();
1270
        $a_thematic_advance_ids = [];
1271
        $next_advance_not_done = 0;
1272
        if (!empty($thematic_data)) {
1273
            foreach ($thematic_data as $thematic) {
1274
                if (!empty($thematic_advance_data[$thematic['id']])) {
1275
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
1276
                        if (0 == $thematic_advance['done_advance']) {
1277
                            $a_thematic_advance_ids[] = $thematic_advance['id'];
1278
                        }
1279
                    }
1280
                }
1281
            }
1282
        }
1283
1284
        if (!empty($a_thematic_advance_ids)) {
1285
            for ($i = 0; $i < $offset; $i++) {
1286
                $next_advance_not_done = array_shift($a_thematic_advance_ids);
1287
            }
1288
            $next_advance_not_done = intval($next_advance_not_done);
1289
        }
1290
1291
        return $next_advance_not_done;
1292
    }
1293
1294
    /**
1295
     * Get total average of thematic advances.
1296
     *
1297
     * @return float Average of thematic advances
1298
     */
1299
    public function get_total_average_of_thematic_advances(Course $course, Session $session = null)
1300
    {
1301
        $thematic_data = self::getThematicList($course, $session);
1302
1303
        $list = [];
1304
        $total_average = 0;
1305
        if (!empty($thematic_data)) {
1306
            /** @var CThematic $thematic */
1307
            foreach ($thematic_data as $thematic) {
1308
                $thematic_id = $thematic->getIid();
1309
                $list[$thematic_id] = $this->get_average_of_advances_by_thematic(
1310
                    $thematic
1311
                );
1312
            }
1313
        }
1314
1315
        // calculate total average
1316
        if (!empty($list)) {
1317
            $count = count($thematic_data);
1318
            $score = array_sum($list);
1319
            $total_average = round(($score * 100) / ($count * 100));
1320
        }
1321
1322
        return $total_average;
1323
    }
1324
1325
    /**
1326
     * Get average of advances by thematic.
1327
     *
1328
     * @param CThematic $thematic
1329
     *
1330
     * @return float Average of thematic advances
1331
     */
1332
    public function get_average_of_advances_by_thematic($thematic)
1333
    {
1334
        $advances = $thematic->getAdvances();
1335
        $average = 0;
1336
        if ($advances->count()) {
1337
            // get all done advances by thematic
1338
            $count = 0;
1339
            /** @var CThematicAdvance $thematic_advance */
1340
            foreach ($advances as $thematic_advance) {
1341
                if ($thematic_advance->getDoneAdvance()) {
1342
                    $count++;
1343
                }
1344
            }
1345
1346
            // calculate average by thematic
1347
            $average = round(($count * 100) / count($advances));
1348
        }
1349
1350
        return $average;
1351
    }
1352
1353
    /**
1354
     * set attributes for fields of thematic table.
1355
     *
1356
     * @param    int        Thematic id
1357
     * @param    string    Thematic title
1358
     * @param    string    Thematic content
1359
     * @param    int        Session id
1360
     */
1361
    public function set_thematic_attributes($id = null, $title = '', $content = '', $session_id = 0)
1362
    {
1363
        $this->thematic_id = $id;
1364
        $this->thematic_title = $title;
1365
        $this->thematic_content = $content;
1366
        $this->session_id = $session_id;
1367
    }
1368
1369
    /**
1370
     * set attributes for fields of thematic_plan table.
1371
     *
1372
     * @param    int        Thematic id
1373
     * @param    string    Thematic plan title
1374
     * @param    string    Thematic plan description
1375
     * @param    int        Thematic plan description type
1376
     */
1377
    public function set_thematic_plan_attributes(
1378
        $thematic_id = 0,
1379
        $title = '',
1380
        $description = '',
1381
        $description_type = 0
1382
    ) {
1383
        $this->thematic_id = $thematic_id;
1384
        $this->thematic_plan_title = $title;
1385
        $this->thematic_plan_description = $description;
1386
        $this->thematic_plan_description_type = $description_type;
1387
    }
1388
1389
    /**
1390
     * set attributes for fields of thematic_advance table.
1391
     *
1392
     * @param int $id Thematic advance id
1393
     * @param    int        Thematic id
1394
     * @param    int        Attendance id
1395
     * @param    string    Content
1396
     * @param    string    Date and time
1397
     * @param    int        Duration in hours
1398
     */
1399
    public function set_thematic_advance_attributes(
1400
        $id = null,
1401
        $thematic_id = 0,
1402
        $attendance_id = 0,
1403
        $content = '',
1404
        $start_date = null,
1405
        $duration = 0
1406
    ) {
1407
        $this->thematic_advance_id = $id;
1408
        $this->thematic_id = $thematic_id;
1409
        $this->attendance_id = $attendance_id;
1410
        $this->thematic_advance_content = $content;
1411
        $this->start_date = $start_date;
1412
        $this->duration = $duration;
1413
    }
1414
1415
    /**
1416
     * set thematic id.
1417
     *
1418
     * @param    int     Thematic id
1419
     */
1420
    public function set_thematic_id($thematic_id)
1421
    {
1422
        $this->thematic_id = $thematic_id;
1423
    }
1424
1425
    /**
1426
     * get thematic id.
1427
     *
1428
     * @return int
1429
     */
1430
    public function get_thematic_id()
1431
    {
1432
        return $this->thematic_id;
1433
    }
1434
1435
    /**
1436
     * Get thematic plan titles by default.
1437
     *
1438
     * @return array
1439
     */
1440
    public function get_default_thematic_plan_title()
1441
    {
1442
        $default_thematic_plan_titles = [];
1443
        $default_thematic_plan_titles[1] = get_lang('Objectives');
1444
        $default_thematic_plan_titles[2] = get_lang('Skills to acquire');
1445
        $default_thematic_plan_titles[3] = get_lang('Methodology');
1446
        $default_thematic_plan_titles[4] = get_lang('Infrastructure');
1447
        $default_thematic_plan_titles[5] = get_lang('Assessment');
1448
        $default_thematic_plan_titles[6] = get_lang('Others');
1449
1450
        return $default_thematic_plan_titles;
1451
    }
1452
1453
    /**
1454
     * Get thematic plan icons by default.
1455
     *
1456
     * @return array
1457
     */
1458
    public function get_default_thematic_plan_icon()
1459
    {
1460
        $default_thematic_plan_icon = [];
1461
        $default_thematic_plan_icon[1] = 'icons/32/objective.png';
1462
        $default_thematic_plan_icon[2] = 'icons/32/skills.png';
1463
        $default_thematic_plan_icon[3] = 'icons/32/strategy.png';
1464
        $default_thematic_plan_icon[4] = 'icons/32/laptop.png';
1465
        $default_thematic_plan_icon[5] = 'icons/32/assessment.png';
1466
        $default_thematic_plan_icon[6] = 'icons/32/wizard.png';
1467
1468
        return $default_thematic_plan_icon;
1469
    }
1470
1471
    /**
1472
     * Get questions by default for help.
1473
     *
1474
     * @return array
1475
     */
1476
    public function get_default_question()
1477
    {
1478
        $question = [];
1479
        $question[1] = get_lang('What should the end results be when the learner has completed the course? What are the activities performed during the course?');
1480
        $question[2] = get_lang('Skills to acquireQuestions');
1481
        $question[3] = get_lang('What methods and activities help achieve the objectives of the course?  What would the schedule be?');
1482
        $question[4] = get_lang('What infrastructure is necessary to achieve the goals of this topic normally?');
1483
        $question[5] = get_lang('How will learners be assessed? Are there strategies to develop in order to master the topic?');
1484
1485
        return $question;
1486
    }
1487
}
1488