Passed
Push — master ( 7dbaa5...624452 )
by Julito
09:27
created

Thematic::thematicAdvanceSave()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 46
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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

479
            /** @scrutinizer ignore-call */ 
480
            $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...
480
            if (!empty($thematic_plan)) {
481
                foreach ($thematic_plan as $item) {
482
                    $thematic->thematicPlanSave(
0 ignored issues
show
Bug introduced by
The method thematicPlanSave() does not exist on Chamilo\CourseBundle\Entity\CThematic. ( Ignorable by Annotation )

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

482
                    $thematic->/** @scrutinizer ignore-call */ 
483
                               thematicPlanSave(

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...
483
                        $newThematic,
484
                        $item->getTitle(),
485
                        $item->getDescription(),
486
                        $item->getDescriptionType()
487
                    );
488
                }
489
            }
490
        }
491
    }
492
493
    /**
494
     * Get the total number of thematic advance inside current course.
495
     *
496
     * @see SortableTable#get_total_number_of_items()
497
     */
498
    public static function get_number_of_thematic_advances(array $params)
499
    {
500
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
501
        $thematic_id = (int) $params['thematic_id'];
502
503
        $sql = "SELECT COUNT(iid) AS total_number_of_items
504
                FROM $table
505
                WHERE thematic_id = $thematic_id ";
506
        $res = Database::query($sql);
507
        $obj = Database::fetch_object($res);
508
509
        return $obj->total_number_of_items;
510
    }
511
512
    /**
513
     * Get the thematic advances to display on the current page (fill the sortable-table).
514
     *
515
     * @param   int     offset of first user to recover
516
     * @param   int     Number of users to get
517
     * @param   int     Column to sort on
518
     * @param   string  Order (ASC,DESC)
519
     *
520
     * @return array
521
     *
522
     * @see SortableTable#get_table_data($from)
523
     */
524
    public static function get_thematic_advance_data($from, $number_of_items, $column, $direction, $params = [])
525
    {
526
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
527
        $column = (int) $column;
528
        $from = (int) $from;
529
        $number_of_items = (int) $number_of_items;
530
        if (!in_array($direction, ['ASC', 'DESC'])) {
531
            $direction = 'ASC';
532
        }
533
        $data = [];
534
        $thematic_id = (int) $params['thematic_id'];
535
        if (api_is_allowed_to_edit(null, true)) {
536
            $sql = "SELECT iid AS col0, start_date AS col1, duration AS col2, content AS col3
537
                    FROM $table
538
                    WHERE thematic_id = $thematic_id
539
                    ORDER BY col$column $direction
540
                    LIMIT $from,$number_of_items ";
541
542
            /*$list = api_get_item_property_by_tool(
543
                'thematic_advance',
544
                api_get_course_id(),
545
                api_get_session_id()
546
            );*/
547
548
            /*$elements = [];
549
            foreach ($list as $value) {
550
                $elements[] = $value['ref'];
551
            }*/
552
553
            $res = Database::query($sql);
554
            $i = 1;
555
            while ($thematic_advance = Database::fetch_row($res)) {
556
                //if (in_array($thematic_advance[0], $elements)) {
557
                $thematic_advance[1] = api_get_local_time($thematic_advance[1]);
558
                $thematic_advance[1] = api_format_date($thematic_advance[1], DATE_TIME_FORMAT_LONG);
559
                $actions = '';
560
                $actions .= '<a
561
                        href="index.php?'.api_get_cidreq().'&action=thematic_advance_edit&thematic_id='.$thematic_id.'&thematic_advance_id='.$thematic_advance[0].'">'.
562
                        Display::return_icon('edit.png', get_lang('Edit'), '', 22).'</a>';
563
                $actions .= '<a
564
                    onclick="javascript:if(!confirm(\''.get_lang('Are you sure you want to delete').'\')) return false;"
565
                    href="index.php?'.api_get_cidreq().'&action=thematic_advance_delete&thematic_id='.$thematic_id.'&thematic_advance_id='.$thematic_advance[0].'">'.
566
                        Display::return_icon('delete.png', get_lang('Delete'), '', 22).'</a></center>';
567
                $data[] = [$i, $thematic_advance[1], $thematic_advance[2], $thematic_advance[3], $actions];
568
                $i++;
569
                // }
570
            }
571
        }
572
573
        return $data;
574
    }
575
576
    public function getThematicAdvance($id): ?CThematicAdvance
577
    {
578
        $repo = Container::getThematicAdvanceRepository();
579
580
        return $repo->find($id);
581
    }
582
583
    /**
584
     * insert or update a thematic advance.
585
     *
586
     * @return CThematicAdvance
587
     */
588
    public function thematicAdvanceSave(
589
        CThematic $thematic,
590
        CAttendance $attendance,
591
        CThematicAdvance $advance = null,
592
        $content,
593
        $start_date,
594
        $duration
595
    ) {
596
        $em = Database::getManager();
597
598
        if (null === $advance) {
599
            $advance = new CThematicAdvance();
600
            $advance
601
                ->setContent($content)
602
                ->setThematic($thematic)
603
                ->setAttendance($attendance)
604
                ->setStartDate(api_get_utc_datetime($start_date, true, true))
605
                ->setDuration($duration)
606
            ;
607
608
            //$courseEntity = api_get_course_entity();
609
            /*$advance
610
                ->setParent($courseEntity)
611
                ->addCourseLink($courseEntity, api_get_session_entity())
612
            ;*/
613
            $em->persist($advance);
614
            $em->flush();
615
        } else {
616
            $advance
617
                ->setContent($content)
618
                ->setStartDate(api_get_utc_datetime($start_date, true, true))
619
                ->setDuration($duration)
620
            ;
621
622
            if ($thematic) {
0 ignored issues
show
introduced by
$thematic is of type Chamilo\CourseBundle\Entity\CThematic, thus it always evaluated to true.
Loading history...
623
                $advance->setThematic($thematic);
624
            }
625
626
            if ($attendance) {
0 ignored issues
show
introduced by
$attendance is of type Chamilo\CourseBundle\Entity\CAttendance, thus it always evaluated to true.
Loading history...
627
                $advance->setAttendance($attendance);
628
            }
629
            $em->persist($advance);
630
            $em->flush();
631
        }
632
633
        return $advance;
634
    }
635
636
    /**
637
     * insert or update a thematic plan.
638
     *
639
     * @return int affected rows
640
     */
641
    public function thematicPlanSave(CThematic $thematic, $title, $description, $description_type, $course = null, $session = null)
642
    {
643
        // protect data
644
        $thematic_id = $thematic->getIid();
645
        $description_type = (int) $description_type;
646
647
        /*$list = api_get_item_property_by_tool(
648
            'thematic_plan',
649
            api_get_course_id(),
650
            api_get_session_id()
651
        );
652
653
        $elements_to_show = [];
654
        foreach ($list as $value) {
655
            $elements_to_show[] = $value['ref'];
656
        }
657
        $condition = '';
658
        if (!empty($elements_to_show)) {
659
            $condition = 'AND id IN ('.implode(',', $elements_to_show).') ';
660
        }*/
661
662
        $repo = Container::getThematicPlanRepository();
663
        $criteria = [
664
            'thematic' => $thematic_id,
665
            'descriptionType' => $description_type,
666
        ];
667
        /** @var CThematicPlan $plan */
668
        $plan = $repo->findOneBy($criteria);
669
        $em = Database::getManager();
670
        // check thematic plan type already exists
671
        /*$sql = "SELECT id FROM $tbl_thematic_plan
672
                WHERE
673
                    c_id = $course_id AND
674
                    thematic_id = $thematic_id AND
675
                    description_type = '$description_type'";
676
        $rs = Database::query($sql);*/
677
        if ($plan) {
0 ignored issues
show
introduced by
$plan is of type Chamilo\CourseBundle\Entity\CThematicPlan, thus it always evaluated to true.
Loading history...
678
            $plan
679
                ->setTitle($title)
680
                ->setDescription($description)
681
            ;
682
            $em->persist($plan);
683
            $em->flush();
684
        //$repo->update($plan);
685
686
        // update
687
            /*$params = [
688
                'title' => $title,
689
                'description' => $description,
690
            ];
691
            Database::update(
692
                $tbl_thematic_plan,
693
                $params,
694
                ['c_id = ? AND id = ?' => [$course_id, $thematic_plan_id]]
695
            );
696
697
            api_item_property_update(
698
                $_course,
699
                'thematic_plan',
700
                $thematic_plan_id,
701
                'ThematicPlanUpdated',
702
                $user_id
703
            );*/
704
        } else {
705
            $thematic = Container::getThematicRepository()->find($thematic_id);
706
            $plan = new CThematicPlan();
707
            $plan
708
                ->setTitle($title)
709
                ->setDescription($description)
710
                ->setThematic($thematic)
711
                ->setDescriptionType($description_type)
712
                //->setParent($course)
713
                //->addCourseLink($course, api_get_session_entity())
714
            ;
715
716
            //$repo->create($plan);
717
            $em->persist($plan);
718
            $em->flush();
719
            if ($plan && $plan->getIid()) {
720
                /*
721
                api_item_property_update(
722
                    $_course,
723
                    'thematic_plan',
724
                    $last_id,
725
                    'ThematicPlanAdded',
726
                    $user_id
727
                );*/
728
            }
729
        }
730
731
        return true;
732
    }
733
734
    /**
735
     * Delete a thematic plan description.
736
     *
737
     * @param int $thematic_id Thematic id
738
     *
739
     * @return int Affected rows
740
     */
741
    public function thematic_plan_destroy($thematic_id, $descriptionType)
742
    {
743
        $repo = Container::getThematicRepository();
744
745
        /** @var CThematic $thematic */
746
        $thematic = $repo->find($thematic_id);
747
748
        foreach ($thematic->getPlans() as $plan) {
749
            if ($descriptionType == $plan->getDescriptionType()) {
750
                $thematic->getPlans()->removeElement($plan);
751
            }
752
        }
753
754
        $repo->update($thematic);
755
756
        return false;
757
758
        /*$_course = api_get_course_info();
759
        // definition database table
760
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
761
762
        // protect data
763
        $thematic_id = intval($thematic_id);
764
        $description_type = intval($description_type);
765
        $user_id = api_get_user_id();
766
        $course_info = api_get_course_info();
767
        $course_id = $course_info['real_id'];
768
769
        // get thematic plan id
770
        $thematic_plan_data = $this->get_thematic_plan_data($thematic_id, $description_type);
771
        $thematic_plan_id = $thematic_plan_data[0]['id'];
772
773
        // delete
774
        $sql = "DELETE FROM $tbl_thematic_plan
775
                WHERE
776
                    c_id = $course_id AND
777
                    thematic_id = $thematic_id AND
778
                    description_type = $description_type ";
779
        $result = Database::query($sql);
780
        $affected_rows = Database::affected_rows($result);*/
781
        /*
782
        if ($affected_rows) {
783
            /*api_item_property_update(
784
                $_course,
785
                'thematic_plan',
786
                $thematic_plan_id,
787
                'ThematicPlanDeleted',
788
                $user_id
789
            );
790
        }*/
791
792
        return $affected_rows;
0 ignored issues
show
Unused Code introduced by
return $affected_rows 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...
793
    }
794
795
    /**
796
     * Get next description type for a new thematic plan description (option 'others').
797
     *
798
     * @param int $thematic_id Thematic id
799
     *
800
     * @return int New Description type
801
     */
802
    public function get_next_description_type($thematic_id)
803
    {
804
        // definition database table
805
        $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
806
807
        // protect data
808
        $thematic_id = intval($thematic_id);
809
        $course_id = api_get_course_int_id();
810
811
        $sql = "SELECT MAX(description_type) as max
812
                FROM $tbl_thematic_plan
813
                WHERE
814
                    c_id = $course_id AND
815
                    thematic_id = $thematic_id AND
816
                    description_type >= ".ADD_THEMATIC_PLAN;
817
        $rs = Database::query($sql);
818
        $row = Database::fetch_array($rs);
819
        $last_description_type = $row['max'];
820
821
        if (isset($last_description_type)) {
822
            $next_description_type = $last_description_type + 1;
823
        } else {
824
            $next_description_type = ADD_THEMATIC_PLAN;
825
        }
826
827
        return $next_description_type;
828
    }
829
830
    /**
831
     * update done thematic advances from thematic details interface.
832
     *
833
     * @return int Affected rows
834
     */
835
    public function updateDoneThematicAdvance($advanceId, $course, $session = null)
836
    {
837
        $em = Database::getManager();
838
        $list = self::getThematicList($course, $session);
839
        $ordered = [];
840
841
        foreach ($list as $thematic) {
842
            $done = true;
843
            foreach ($thematic->getAdvances() as $advance) {
844
                $ordered[] = $advance;
845
                /*if ($advanceId === $advance->getIid()) {
846
                    $done = false;
847
                }*/
848
                $advance->setDoneAdvance($done);
849
            }
850
        }
851
852
        $done = true;
853
        foreach ($ordered as $advance) {
854
            if ($advanceId === $advance->getIid()) {
855
                $done = false;
856
                $advance->setDoneAdvance(true);
857
                $em->persist($advance);
858
                continue;
859
            }
860
861
            $advance->setDoneAdvance($done);
862
            $em->persist($advance);
863
        }
864
865
        $em->flush();
866
867
        return true;
868
869
        /*$_course = api_get_course_info();
870
        $thematic_data = self::getThematicList(api_get_course_id());
871
        $table = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
872
873
        $affected_rows = 0;
874
        $user_id = api_get_user_id();*/
875
876
        /*$all = [];
877
        if (!empty($thematic_data)) {
878
            foreach ($thematic_data as $thematic) {
879
                $thematic_id = $thematic['id'];
880
                if (!empty($thematic_advance_data[$thematic['id']])) {
881
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
882
                        $all[] = $thematic_advance['id'];
883
                    }
884
                }
885
            }
886
        }*/
887
        $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...
888
        $a_thematic_advance_ids = [];
889
        $course_id = api_get_course_int_id();
890
        $sessionId = api_get_session_id();
891
892
        /*if (!empty($thematic_data)) {
893
            foreach ($thematic_data as $thematic) {
894
                $my_affected_rows = 0;
895
                $thematic_id = $thematic['id'];
896
                if (!empty($thematic_advance_data[$thematic['id']])) {
897
                    foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
898
                        $item_info = api_get_item_property_info(
899
                            api_get_course_int_id(),
900
                            'thematic_advance',
901
                            $thematic_advance['id'],
902
                            $sessionId
903
                        );
904
905
                        if ($item_info['session_id'] == $sessionId) {
906
                            $a_thematic_advance_ids[] = $thematic_advance['id'];
907
                            // update done thematic for previous advances ((done_advance = 1))
908
                            $upd = "UPDATE $table SET
909
                                    done_advance = 1
910
                                    WHERE c_id = $course_id AND id = ".$thematic_advance['id'].' ';
911
                            $result = Database::query($upd);
912
                            $my_affected_rows = Database::affected_rows($result);
913
                            $affected_rows += $my_affected_rows;
914
                            //if ($my_affected_rows) {
915
                            api_item_property_update(
916
                                $_course,
917
                                'thematic_advance',
918
                                $thematic_advance['id'],
919
                                'ThematicAdvanceDone',
920
                                $user_id
921
                            );
922
                            //}
923
                            if ($thematic_advance['id'] == $thematic_advance_id) {
924
                                break 2;
925
                            }
926
                        }
927
                    }
928
                }
929
            }
930
        }*/
931
932
        // Update done thematic for others advances (done_advance = 0)
933
        if (!empty($a_thematic_advance_ids) && count($a_thematic_advance_ids) > 0) {
934
            $diff = array_diff($all, $a_thematic_advance_ids);
935
            if (!empty($diff)) {
936
                $upd = "UPDATE $table SET done_advance = 0
937
                        WHERE iid IN(".implode(',', $diff).') ';
938
                Database::query($upd);
939
            }
940
941
            // update item_property
942
            /*$tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
943
            $sql = "SELECT ref FROM $tbl_item_property
944
                    WHERE
945
                        c_id = $course_id AND
946
                        tool='thematic_advance' AND
947
                        lastedit_type='ThematicAdvanceDone' AND
948
                        session_id = $sessionId ";
949
            // get all thematic advance done
950
            $rs_thematic_done = Database::query($sql);
951
            if (Database::num_rows($rs_thematic_done) > 0) {
952
                while ($row_thematic_done = Database::fetch_array($rs_thematic_done)) {
953
                    $ref = $row_thematic_done['ref'];
954
                    if (in_array($ref, $a_thematic_advance_ids)) {
955
                        continue;
956
                    }
957
                    // update items
958
                    $sql = "UPDATE $tbl_item_property SET
959
                                lastedit_date='".api_get_utc_datetime()."',
960
                                lastedit_type='ThematicAdvanceUpdated',
961
                                lastedit_user_id = $user_id
962
                            WHERE
963
                                c_id = $course_id AND
964
                                tool='thematic_advance' AND
965
                                ref=$ref AND
966
                                session_id = $sessionId  ";
967
                    Database::query($sql);
968
                }
969
            }*/
970
        }
971
972
        return $affected_rows;
973
    }
974
975
    /**
976
     * Get last done thematic advance from thematic details interface.
977
     *
978
     * @return int Last done thematic advance id
979
     */
980
    public function get_last_done_thematic_advance($course, $session = null)
981
    {
982
        $thematic_data = self::getThematicList($course, $session);
983
        $a_thematic_advance_ids = [];
984
        $last_done_advance_id = 0;
985
        if (!empty($thematic_data)) {
986
            /** @var CThematic $thematic */
987
            foreach ($thematic_data as $thematic) {
988
                $id = $thematic->getIid();
989
                if ($thematic->getAdvances()->count()) {
990
                    foreach ($thematic->getAdvances() as $thematic_advance) {
991
                        if (1 == $thematic_advance->getDoneAdvance()) {
992
                            $a_thematic_advance_ids[] = $thematic_advance->getIid();
993
                        }
994
                    }
995
                }
996
            }
997
        }
998
        if (!empty($a_thematic_advance_ids)) {
999
            $last_done_advance_id = array_pop($a_thematic_advance_ids);
1000
            $last_done_advance_id = intval($last_done_advance_id);
1001
        }
1002
1003
        return $last_done_advance_id;
1004
    }
1005
1006
    /**
1007
     * Get next thematic advance not done from thematic details interface.
1008
     *
1009
     * @param   int Offset (if you want to get an item that is not directly the next)
1010
     *
1011
     * @return int next thematic advance not done
1012
     */
1013
    public function get_next_thematic_advance_not_done($offset = 1, $course, $session = null)
1014
    {
1015
        $thematic_data = self::getThematicList($course, $session);
1016
        $a_thematic_advance_ids = [];
1017
        if (!empty($thematic_data)) {
1018
            foreach ($thematic_data as $thematic) {
1019
                $advanceList = $thematic->getAdvances();
1020
                foreach ($advanceList as $advance) {
1021
                    if (0 == $advance->getDoneAdvance()) {
1022
                        $a_thematic_advance_ids[] = $advance->getIid();
1023
                    }
1024
                }
1025
            }
1026
        }
1027
1028
        $next_advance_not_done = 0;
1029
        if (!empty($a_thematic_advance_ids)) {
1030
            for ($i = 0; $i < $offset; $i++) {
1031
                $next_advance_not_done = array_shift($a_thematic_advance_ids);
1032
            }
1033
            $next_advance_not_done = intval($next_advance_not_done);
1034
        }
1035
1036
        return $next_advance_not_done;
1037
    }
1038
1039
    /**
1040
     * Get total average of thematic advances.
1041
     *
1042
     * @return float Average of thematic advances
1043
     */
1044
    public function get_total_average_of_thematic_advances(Course $course, Session $session = null)
1045
    {
1046
        $thematic_data = self::getThematicList($course, $session);
1047
1048
        $list = [];
1049
        $total_average = 0;
1050
        if (!empty($thematic_data)) {
1051
            /** @var CThematic $thematic */
1052
            foreach ($thematic_data as $thematic) {
1053
                $thematic_id = $thematic->getIid();
1054
                $list[$thematic_id] = $this->get_average_of_advances_by_thematic(
1055
                    $thematic
1056
                );
1057
            }
1058
        }
1059
1060
        // calculate total average
1061
        if (!empty($list)) {
1062
            $count = count($thematic_data);
1063
            $score = array_sum($list);
1064
            $total_average = round(($score * 100) / ($count * 100));
1065
        }
1066
1067
        return $total_average;
1068
    }
1069
1070
    /**
1071
     * Get average of advances by thematic.
1072
     *
1073
     * @param CThematic $thematic
1074
     *
1075
     * @return float Average of thematic advances
1076
     */
1077
    public function get_average_of_advances_by_thematic($thematic)
1078
    {
1079
        $advances = $thematic->getAdvances();
1080
        $average = 0;
1081
        if ($advances->count()) {
1082
            // get all done advances by thematic
1083
            $count = 0;
1084
            /** @var CThematicAdvance $thematic_advance */
1085
            foreach ($advances as $thematic_advance) {
1086
                if ($thematic_advance->getDoneAdvance()) {
1087
                    $count++;
1088
                }
1089
            }
1090
1091
            // calculate average by thematic
1092
            $average = round(($count * 100) / count($advances));
1093
        }
1094
1095
        return $average;
1096
    }
1097
1098
    /**
1099
     * set attributes for fields of thematic_advance table.
1100
     *
1101
     * @param int $id Thematic advance id
1102
     * @param    int        Thematic id
1103
     * @param    int        Attendance id
1104
     * @param    string    Content
1105
     * @param    string    Date and time
1106
     * @param    int        Duration in hours
1107
     */
1108
    public function set_thematic_advance_attributes(
1109
        $id = null,
1110
        $thematic_id = 0,
1111
        $attendance_id = 0,
1112
        $content = '',
1113
        $start_date = null,
1114
        $duration = 0
1115
    ) {
1116
        $this->thematic_advance_id = $id;
1117
        $this->thematic_id = $thematic_id;
1118
        $this->attendance_id = $attendance_id;
1119
        $this->thematic_advance_content = $content;
1120
        $this->start_date = $start_date;
1121
        $this->duration = $duration;
1122
    }
1123
1124
    /**
1125
     * set thematic id.
1126
     *
1127
     * @param    int     Thematic id
1128
     */
1129
    public function set_thematic_id($thematic_id)
1130
    {
1131
        $this->thematic_id = $thematic_id;
1132
    }
1133
1134
    /**
1135
     * get thematic id.
1136
     *
1137
     * @return int
1138
     */
1139
    public function get_thematic_id()
1140
    {
1141
        return $this->thematic_id;
1142
    }
1143
1144
    /**
1145
     * Get thematic plan titles by default.
1146
     *
1147
     * @return array
1148
     */
1149
    public function get_default_thematic_plan_title()
1150
    {
1151
        $default_thematic_plan_titles = [];
1152
        $default_thematic_plan_titles[1] = get_lang('Objectives');
1153
        $default_thematic_plan_titles[2] = get_lang('Skills to acquire');
1154
        $default_thematic_plan_titles[3] = get_lang('Methodology');
1155
        $default_thematic_plan_titles[4] = get_lang('Infrastructure');
1156
        $default_thematic_plan_titles[5] = get_lang('Assessment');
1157
        $default_thematic_plan_titles[6] = get_lang('Others');
1158
1159
        return $default_thematic_plan_titles;
1160
    }
1161
1162
    /**
1163
     * Get thematic plan icons by default.
1164
     *
1165
     * @return array
1166
     */
1167
    public function get_default_thematic_plan_icon()
1168
    {
1169
        $default_thematic_plan_icon = [];
1170
        $default_thematic_plan_icon[1] = 'icons/32/objective.png';
1171
        $default_thematic_plan_icon[2] = 'icons/32/skills.png';
1172
        $default_thematic_plan_icon[3] = 'icons/32/strategy.png';
1173
        $default_thematic_plan_icon[4] = 'icons/32/laptop.png';
1174
        $default_thematic_plan_icon[5] = 'icons/32/assessment.png';
1175
        $default_thematic_plan_icon[6] = 'icons/32/wizard.png';
1176
1177
        return $default_thematic_plan_icon;
1178
    }
1179
1180
    /**
1181
     * Get questions by default for help.
1182
     *
1183
     * @return array
1184
     */
1185
    public function get_default_question()
1186
    {
1187
        $question = [];
1188
        $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?');
1189
        $question[2] = get_lang('Skills to acquireQuestions');
1190
        $question[3] = get_lang('What methods and activities help achieve the objectives of the course?  What would the schedule be?');
1191
        $question[4] = get_lang('What infrastructure is necessary to achieve the goals of this topic normally?');
1192
        $question[5] = get_lang('How will learners be assessed? Are there strategies to develop in order to master the topic?');
1193
1194
        return $question;
1195
    }
1196
}
1197