Passed
Push — 1.11.x ( 7bb1e6...a270bb )
by Julito
09:44
created

SystemAnnouncementManager::update_announcement()   C

Complexity

Conditions 16
Paths 67

Size

Total Lines 124
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 71
nc 67
nop 11
dl 0
loc 124
rs 5.5666
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
/**
6
 * Class SystemAnnouncementManager.
7
 */
8
class SystemAnnouncementManager
9
{
10
    const VISIBLE_GUEST = 'visible_guest';
11
    const VISIBLE_STUDENT = 'visible_student';
12
    const VISIBLE_TEACHER = 'visible_teacher';
13
    // Requires DB change
14
    const VISIBLE_DRH = 'visible_drh';
15
    const VISIBLE_SESSION_ADMIN = 'visible_session_admin';
16
    const VISIBLE_STUDENT_BOSS = 'visible_boss';
17
18
    /**
19
     * @return array
20
     */
21
    public static function getVisibilityList()
22
    {
23
        $extraRoles = self::newRolesActivated();
24
25
        $visibleToUsers = [
26
            self::VISIBLE_TEACHER => get_lang('Teacher'),
27
            self::VISIBLE_STUDENT => get_lang('Student'),
28
            self::VISIBLE_GUEST => get_lang('Guest'),
29
        ];
30
31
        if ($extraRoles) {
32
            $visibleToUsers[self::VISIBLE_DRH] = get_lang('DRH');
33
            $visibleToUsers[self::VISIBLE_SESSION_ADMIN] = get_lang('SessionAdministrator');
34
            $visibleToUsers[self::VISIBLE_STUDENT_BOSS] = get_lang('StudentBoss');
35
        }
36
37
        return $visibleToUsers;
38
    }
39
40
    /**
41
     * @param string $visibility
42
     *
43
     * @return string
44
     */
45
    public static function getVisibilityCondition($visibility)
46
    {
47
        $list = self::getVisibilityList();
48
        $visibilityCondition = " AND ".self::VISIBLE_GUEST." = 1 ";
49
        if (in_array($visibility, array_keys($list))) {
50
            $visibilityCondition = " AND $visibility = 1 ";
51
        }
52
53
        return $visibilityCondition;
54
    }
55
56
    /**
57
     * @param string $visibility
58
     * @param int    $id
59
     * @param int    $start
60
     * @param string $user_id
61
     *
62
     * @return string
63
     */
64
    public static function displayAllAnnouncements(
65
        $visibility,
66
        $id = -1,
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

66
        /** @scrutinizer ignore-unused */ $id = -1,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
        $start = 0,
68
        $user_id = ''
69
    ) {
70
        $user_selected_language = api_get_interface_language();
71
        $start = (int) $start;
72
        $userGroup = new UserGroup();
73
        $tbl_announcement_group = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS);
74
        $temp_user_groups = $userGroup->get_groups_by_user(api_get_user_id(), 0);
75
        $groups = [];
76
        foreach ($temp_user_groups as $user_group) {
77
            $groups = array_merge($groups, [$user_group['id']]);
78
            $groups = array_merge($groups, $userGroup->get_parent_groups($user_group['id']));
79
        }
80
81
        // Checks if tables exists to not break platform not updated
82
        $groups_string = '('.implode($groups, ',').')';
83
84
        $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
85
        $now = api_get_utc_datetime();
86
87
        $sql = "SELECT * FROM $table
88
                WHERE
89
                    (lang = '$user_selected_language' OR lang IS NULL) AND
90
                    ( '$now' >= date_start AND '$now' <= date_end) ";
91
92
        $sql .= self::getVisibilityCondition($visibility);
93
94
        if (count($groups) > 0) {
95
            $sql .= " OR id IN (
96
                    SELECT announcement_id FROM $tbl_announcement_group
97
                    WHERE group_id in $groups_string
98
                    ) ";
99
        }
100
101
        if (api_is_multiple_url_enabled()) {
102
            $current_access_url_id = api_get_current_access_url_id();
103
            $sql .= " AND access_url_id IN ('1', '$current_access_url_id')";
104
        }
105
106
        if (!isset($_GET['start']) || $_GET['start'] == 0) {
107
            $sql .= " ORDER BY date_start DESC LIMIT ".$start.",20";
108
        } else {
109
            $sql .= " ORDER BY date_start DESC LIMIT ".($start + 1).",20";
110
        }
111
        $announcements = Database::query($sql);
112
        $content = '';
113
        if (Database::num_rows($announcements) > 0) {
114
            $content .= '<div class="system_announcements">';
115
            $content .= '<h3>'.get_lang('SystemAnnouncements').'</h3>';
116
            $content .= '<table align="center">';
117
            $content .= '<tr>';
118
            $content .= '<td>';
119
            $content .= self::display_arrow($user_id);
120
            $content .= '</td>';
121
            $content .= '</tr>';
122
            $content .= '</table>';
123
            $content .= '<table align="center" border="0" width="900px">';
124
            while ($announcement = Database::fetch_object($announcements)) {
125
                $display_date = api_convert_and_format_date($announcement->display_date, DATE_FORMAT_LONG);
126
                $content .= '<tr><td>';
127
                $content .= '<a name="'.$announcement->id.'"></a>
128
                        <div class="system_announcement">
129
                        <h2>'.$announcement->title.'</h2>
130
                        <div class="system_announcement_date">'.$display_date.'</div>
131
                        <br />
132
                        <div class="system_announcement_content">'
133
                            .$announcement->content.'
134
                        </div>
135
                      </div><br />';
136
                $content .= '</tr></td>';
137
            }
138
            $content .= '</table>';
139
140
            $content .= '<table align="center">';
141
            $content .= '<tr>';
142
            $content .= '<td>';
143
            $content .= self::display_arrow($user_id);
144
            $content .= '</td>';
145
            $content .= '</tr>';
146
            $content .= '</table>';
147
            $content .= '</div>';
148
        }
149
150
        return $content;
151
    }
152
153
    /**
154
     * @param int $user_id
155
     *
156
     * @return string
157
     */
158
    public static function display_arrow($user_id)
159
    {
160
        $start = (int) $_GET['start'];
161
        $nb_announcement = self::count_nb_announcement($start, $user_id);
162
        $next = ((int) $_GET['start'] + 19);
163
        $prev = ((int) $_GET['start'] - 19);
164
        $content = '';
165
        if (!isset($_GET['start']) || $_GET['start'] == 0) {
166
            if ($nb_announcement > 20) {
167
                $content .= '<a href="news_list.php?start='.$next.'">'.get_lang('NextBis').' >> </a>';
168
            }
169
        } else {
170
            echo '<a href="news_list.php?start='.$prev.'"> << '.get_lang('Prev').'</a>';
171
            if ($nb_announcement > 20) {
172
                $content .= '<a href="news_list.php?start='.$next.'">'.get_lang('NextBis').' >> </a>';
173
            }
174
        }
175
176
        return $content;
177
    }
178
179
    /**
180
     * @param int    $start
181
     * @param string $user_id
182
     *
183
     * @return int
184
     */
185
    public static function count_nb_announcement($start = 0, $user_id = '')
0 ignored issues
show
Unused Code introduced by
The parameter $user_id is not used and could be removed. ( Ignorable by Annotation )

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

185
    public static function count_nb_announcement($start = 0, /** @scrutinizer ignore-unused */ $user_id = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
186
    {
187
        $start = (int) $start;
188
        $user_selected_language = api_get_interface_language();
189
        $db_table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
190
        $sql = 'SELECT id FROM '.$db_table.'
191
                WHERE (lang="'.$user_selected_language.'" OR lang IS NULL) ';
192
193
        $visibility = self::getCurrentUserVisibility();
194
        $sql .= self::getVisibilityCondition($visibility);
195
196
        $current_access_url_id = 1;
197
        if (api_is_multiple_url_enabled()) {
198
            $current_access_url_id = api_get_current_access_url_id();
199
        }
200
        $sql .= " AND access_url_id = '$current_access_url_id' ";
201
        $sql .= 'LIMIT '.$start.', 21';
202
        $announcements = Database::query($sql);
203
        $i = 0;
204
        while ($rows = Database::fetch_array($announcements)) {
205
            $i++;
206
        }
207
208
        return $i;
209
    }
210
211
    /**
212
     * Get all announcements.
213
     *
214
     * @return array An array with all available system announcements (as php
215
     *               objects)
216
     */
217
    public static function get_all_announcements()
218
    {
219
        $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
220
        $now = api_get_utc_datetime();
221
        $sql = "SELECT *, IF ( '$now'  >= date_start AND '$now' <= date_end, '1', '0') AS visible
222
                FROM $table";
223
224
        $current_access_url_id = 1;
225
        if (api_is_multiple_url_enabled()) {
226
            $current_access_url_id = api_get_current_access_url_id();
227
        }
228
        $sql .= " WHERE access_url_id = '$current_access_url_id' ";
229
        $sql .= " ORDER BY date_start ASC";
230
231
        $result = Database::query($sql);
232
        $announcements = [];
233
        while ($announcement = Database::fetch_object($result)) {
234
            $announcements[] = $announcement;
235
        }
236
237
        return $announcements;
238
    }
239
240
    /**
241
     * Adds an announcement to the database.
242
     *
243
     * @param string $title           Title of the announcement
244
     * @param string $content         Content of the announcement
245
     * @param string $date_start      Start date (YYYY-MM-DD HH:II: SS)
246
     * @param string $date_end        End date (YYYY-MM-DD HH:II: SS)
247
     * @param array  $visibility
248
     * @param string $lang            The language for which the announvement should be shown. Leave null for all langages
249
     * @param int    $send_mail       Whether to send an e-mail to all users (1) or not (0)
250
     * @param bool   $add_to_calendar
251
     * @param bool   $sendEmailTest
252
     * @param int    $careerId
253
     * @param int    $promotionId
254
     *
255
     * @return mixed insert_id on success, false on failure
256
     */
257
    public static function add_announcement(
258
        $title,
259
        $content,
260
        $date_start,
261
        $date_end,
262
        $visibility,
263
        $lang = '',
264
        $send_mail = 0,
265
        $add_to_calendar = false,
266
        $sendEmailTest = false,
267
        $careerId = 0,
268
        $promotionId = 0
269
    ) {
270
        $original_content = $content;
271
        $a_dateS = explode(' ', $date_start);
272
        $a_arraySD = explode('-', $a_dateS[0]);
273
        $a_arraySH = explode(':', $a_dateS[1]);
274
        $date_start_to_compare = array_merge($a_arraySD, $a_arraySH);
275
276
        $a_dateE = explode(' ', $date_end);
277
        $a_arrayED = explode('-', $a_dateE[0]);
278
        $a_arrayEH = explode(':', $a_dateE[1]);
279
        $date_end_to_compare = array_merge($a_arrayED, $a_arrayEH);
280
281
        $db_table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
282
283
        if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) {
284
            Display::addFlash(
285
                Display::return_message(get_lang('InvalidStartDate'), 'warning')
286
            );
287
288
            return false;
289
        }
290
291
        if (($date_end_to_compare[1] ||
292
            $date_end_to_compare[2] ||
293
            $date_end_to_compare[0]) &&
294
            !checkdate($date_end_to_compare[1], $date_end_to_compare[2], $date_end_to_compare[0])
295
        ) {
296
            Display::addFlash(
297
                Display::return_message(get_lang('InvalidEndDate'), 'warning')
298
            );
299
300
            return false;
301
        }
302
303
        if (strlen(trim($title)) == 0) {
304
            Display::addFlash(
305
                Display::return_message(get_lang('InvalidTitle'), 'warning')
306
            );
307
308
            return false;
309
        }
310
311
        $start = api_get_utc_datetime($date_start);
312
        $end = api_get_utc_datetime($date_end);
313
314
        //Fixing urls that are sent by email
315
        //$content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content);
316
        //$content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content);
317
        $content = str_replace(
318
            'src=\"'.api_get_path(REL_HOME_PATH),
319
            'src=\"'.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH),
320
            $content
321
        );
322
        $content = str_replace(
323
            'file='.api_get_path(REL_HOME_PATH),
324
            'file='.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH),
325
            $content
326
        );
327
        $lang = is_null($lang) ? '' : $lang;
328
329
        $current_access_url_id = 1;
330
        if (api_is_multiple_url_enabled()) {
331
            $current_access_url_id = api_get_current_access_url_id();
332
        }
333
334
        $params = [
335
            'title' => $title,
336
            'content' => $content,
337
            'date_start' => $start,
338
            'date_end' => $end,
339
            'lang' => $lang,
340
            'access_url_id' => $current_access_url_id,
341
        ];
342
343
        if (api_get_configuration_value('allow_careers_in_global_announcements') && !empty($careerId)) {
344
            $params['career_id'] = (int) $careerId;
345
            $params['promotion_id'] = (int) $promotionId;
346
        }
347
348
        foreach ($visibility as $key => $value) {
349
            $params[$key] = $value;
350
        }
351
352
        $resultId = Database::insert($db_table, $params);
353
354
        if ($resultId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $resultId of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

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

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

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

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
355
            if ($sendEmailTest) {
356
                self::send_system_announcement_by_email(
357
                    $resultId,
358
                    $visibility,
359
                    true
360
                );
361
            } else {
362
                if ($send_mail == 1) {
363
                    self::send_system_announcement_by_email(
364
                        $resultId,
365
                        $visibility
366
                    );
367
                }
368
            }
369
370
            if ($add_to_calendar) {
371
                $agenda = new Agenda('admin');
372
                $agenda->addEvent(
373
                    $date_start,
374
                    $date_end,
375
                    false,
376
                    $title,
377
                    $original_content
378
                );
379
            }
380
381
            return $resultId;
382
        }
383
384
        return false;
385
    }
386
387
    /**
388
     * Makes the announcement id visible only for groups in groups_array.
389
     *
390
     * @param int   $announcement_id
391
     * @param array $group_array     array of group id
392
     *
393
     * @return bool
394
     */
395
    public static function announcement_for_groups($announcement_id, $group_array)
396
    {
397
        $tbl_announcement_group = Database::get_main_table(
398
            TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS
399
        );
400
        //first delete all group associations for this announcement
401
        $res = Database::query(
402
            "DELETE FROM $tbl_announcement_group
403
             WHERE announcement_id=".intval($announcement_id)
404
        );
405
406
        if ($res === false) {
407
            return false;
408
        }
409
410
        foreach ($group_array as $group_id) {
411
            if (intval($group_id) != 0) {
412
                $sql = "INSERT INTO $tbl_announcement_group SET
413
                        announcement_id=".intval($announcement_id).",
414
                        group_id=".intval($group_id);
415
                $res = Database::query($sql);
416
                if ($res === false) {
417
                    return false;
418
                }
419
            }
420
        }
421
422
        return true;
423
    }
424
425
    /**
426
     * Gets the groups of this announce.
427
     *
428
     * @param int announcement id
429
     *
430
     * @return array array of group id
431
     */
432
    public static function get_announcement_groups($announcement_id)
433
    {
434
        $tbl_announcement_group = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS);
435
        $tbl_group = Database::get_main_table(TABLE_USERGROUP);
436
        //first delete all group associations for this announcement
437
        $sql = "SELECT
438
                    g.id as group_id,
439
                    g.name as group_name
440
                FROM $tbl_group g , $tbl_announcement_group ag
441
                WHERE
442
                    announcement_id =".intval($announcement_id)." AND
443
                    ag.group_id = g.id";
444
        $res = Database::query($sql);
445
        $groups = Database::fetch_array($res);
446
447
        return $groups;
448
    }
449
450
    /**
451
     * Updates an announcement to the database.
452
     *
453
     * @param int    $id            of the announcement
454
     * @param string $title         title of the announcement
455
     * @param string $content       content of the announcement
456
     * @param array  $date_start    start date (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute)
457
     * @param array  $date_end      end date of (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute)
458
     * @param array  $visibility
459
     * @param array  $lang
460
     * @param int    $send_mail
461
     * @param bool   $sendEmailTest
462
     * @param int    $careerId
463
     * @param int    $promotionId
464
     *
465
     * @return bool True on success, false on failure
466
     */
467
    public static function update_announcement(
468
        $id,
469
        $title,
470
        $content,
471
        $date_start,
472
        $date_end,
473
        $visibility,
474
        $lang = null,
475
        $send_mail = 0,
476
        $sendEmailTest = false,
477
        $careerId = 0,
478
        $promotionId = 0
479
    ) {
480
        $em = Database::getManager();
481
        $announcement = $em->find('ChamiloCoreBundle:SysAnnouncement', $id);
482
        if (!$announcement) {
483
            return false;
484
        }
485
486
        $a_dateS = explode(' ', $date_start);
487
        $a_arraySD = explode('-', $a_dateS[0]);
488
        $a_arraySH = explode(':', $a_dateS[1]);
489
        $date_start_to_compare = array_merge($a_arraySD, $a_arraySH);
490
491
        $a_dateE = explode(' ', $date_end);
492
        $a_arrayED = explode('-', $a_dateE[0]);
493
        $a_arrayEH = explode(':', $a_dateE[1]);
494
        $date_end_to_compare = array_merge($a_arrayED, $a_arrayEH);
495
496
        $lang = is_null($lang) ? '' : $lang;
497
498
        if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) {
499
            echo Display::return_message(get_lang('InvalidStartDate'));
500
501
            return false;
502
        }
503
504
        if (($date_end_to_compare[1] ||
505
            $date_end_to_compare[2] ||
506
            $date_end_to_compare[0]) &&
507
            !checkdate($date_end_to_compare[1], $date_end_to_compare[2], $date_end_to_compare[0])
508
        ) {
509
            echo Display::return_message(get_lang('InvalidEndDate'));
510
511
            return false;
512
        }
513
514
        if (strlen(trim($title)) == 0) {
515
            echo Display::return_message(get_lang('InvalidTitle'));
516
517
            return false;
518
        }
519
520
        $start = api_get_utc_datetime($date_start);
521
        $end = api_get_utc_datetime($date_end);
522
523
        //Fixing urls that are sent by email
524
        //$content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content);
525
        //$content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content);
526
        $content = str_replace(
527
            'src=\"'.api_get_path(REL_HOME_PATH),
528
            'src=\"'.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH),
529
            $content
530
        );
531
        $content = str_replace(
532
            'file='.api_get_path(REL_HOME_PATH),
533
            'file='.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH),
534
            $content
535
        );
536
537
        $dateStart = new DateTime($start, new DateTimeZone('UTC'));
538
        $dateEnd = new DateTime($end, new DateTimeZone('UTC'));
539
540
        $announcement
541
            ->setLang($lang)
542
            ->setTitle($title)
543
            ->setContent($content)
544
            ->setDateStart($dateStart)
545
            ->setDateEnd($dateEnd)
546
            //->setVisibleTeacher($visible_teacher)
547
            //->setVisibleStudent($visible_student)
548
            //->setVisibleGuest($visible_guest)
549
            ->setAccessUrlId(api_get_current_access_url_id());
550
551
        $em->merge($announcement);
552
        $em->flush();
553
554
        // Update visibility
555
        $list = self::getVisibilityList();
556
        $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
557
558
        if (api_get_configuration_value('allow_careers_in_global_announcements') && !empty($careerId)) {
559
            $params = [];
560
            $params['career_id'] = (int) $careerId;
561
            $params['promotion_id'] = (int) $promotionId;
562
            Database::update(
563
                $table,
564
                $params,
565
                ['id = ? ' => $id]
566
            );
567
        }
568
569
        foreach ($list as $key => $title) {
0 ignored issues
show
introduced by
$title is overwriting one of the parameters of this function.
Loading history...
570
            $value = isset($visibility[$key]) && $visibility[$key] ? 1 : 0;
571
            $sql = "UPDATE $table SET $key = '$value' WHERE id = $id";
572
            Database::query($sql);
573
        }
574
575
        if ($sendEmailTest) {
576
            self::send_system_announcement_by_email(
577
                $id,
578
                $visibility,
579
                true
580
            );
581
        } else {
582
            if ($send_mail == 1) {
583
                self::send_system_announcement_by_email(
584
                    $id,
585
                    $visibility
586
                );
587
            }
588
        }
589
590
        return true;
591
    }
592
593
    /**
594
     * Deletes an announcement.
595
     *
596
     * @param int $id The identifier of the announcement that should be
597
     *
598
     * @return bool True on success, false on failure
599
     */
600
    public static function delete_announcement($id)
601
    {
602
        $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
603
        $id = (int) $id;
604
        $sql = "DELETE FROM $table WHERE id =".$id;
605
        $res = Database::query($sql);
606
        if ($res === false) {
607
            return false;
608
        }
609
610
        return true;
611
    }
612
613
    /**
614
     * Gets an announcement.
615
     *
616
     * @param int $id The identifier of the announcement that should be
617
     *
618
     * @return object Object of class StdClass or the required class, containing the query result row
619
     */
620
    public static function get_announcement($id)
621
    {
622
        $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
623
        $id = (int) $id;
624
        $sql = "SELECT * FROM ".$table." WHERE id = ".$id;
625
        $announcement = Database::fetch_object(Database::query($sql));
626
627
        return $announcement;
628
    }
629
630
    /**
631
     * Change the visibility of an announcement.
632
     *
633
     * @param int  $id
634
     * @param int  $user    For who should the visibility be changed
635
     * @param bool $visible
636
     *
637
     * @return bool True on success, false on failure
638
     */
639
    public static function set_visibility($id, $user, $visible)
640
    {
641
        $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
642
        $id = (int) $id;
643
        $list = array_keys(self::getVisibilityList());
644
        $user = trim($user);
645
        $visible = (int) $visible;
646
        if (!in_array($user, $list)) {
647
            return false;
648
        }
649
650
        $field = $user;
651
        $sql = "UPDATE $table SET ".$field." = '".$visible."'
652
                WHERE id='".$id."'";
653
        $res = Database::query($sql);
654
655
        if ($res === false) {
656
            return false;
657
        }
658
659
        return true;
660
    }
661
662
    /**
663
     * Send a system announcement by e-mail to all teachers/students depending on parameters.
664
     *
665
     * @param int   $id
666
     * @param array $visibility
667
     * @param bool  $sendEmailTest
668
     *
669
     * @return bool True if the message was sent or there was no destination matching.
670
     *              False on database or e-mail sending error.
671
     */
672
    public static function send_system_announcement_by_email(
673
        $id,
674
        $visibility,
675
        $sendEmailTest = false
676
    ) {
677
        $announcement = self::get_announcement($id);
678
679
        if (empty($announcement)) {
680
            return false;
681
        }
682
683
        $title = $announcement->title;
684
        $content = $announcement->content;
685
        $language = $announcement->lang;
686
687
        $content = str_replace(['\r\n', '\n', '\r'], '', $content);
688
        $now = api_get_utc_datetime();
689
        $teacher = $visibility['visible_teacher'];
690
        $student = $visibility['visible_student'];
691
        if ($sendEmailTest) {
692
            MessageManager::send_message_simple(api_get_user_id(), $title, $content);
693
694
            return true;
695
        }
696
697
        $urlJoin = '';
698
        $urlCondition = '';
699
        $user_table = Database::get_main_table(TABLE_MAIN_USER);
700
        if (api_is_multiple_url_enabled()) {
701
            $current_access_url_id = api_get_current_access_url_id();
702
            $url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
703
            $urlJoin = " INNER JOIN $url_rel_user uu ON uu.user_id = u.user_id ";
704
            $urlCondition = " AND access_url_id = '".$current_access_url_id."' ";
705
        }
706
707
        if ($teacher != 0 && $student == 0) {
708
            $sql = "SELECT DISTINCT u.user_id FROM $user_table u $urlJoin
709
                    WHERE status = '1' $urlCondition";
710
        }
711
712
        if ($teacher == 0 && $student != 0) {
713
            $sql = "SELECT DISTINCT u.user_id FROM $user_table u $urlJoin
714
                    WHERE status = '5' $urlCondition";
715
        }
716
717
        if ($teacher != 0 && $student != 0) {
718
            $sql = "SELECT DISTINCT u.user_id FROM $user_table u $urlJoin
719
                    WHERE 1 = 1 $urlCondition";
720
        }
721
722
        if (!isset($sql)) {
723
            return false;
724
        }
725
726
        if (!empty($language)) {
727
            //special condition because language was already treated for SQL insert before
728
            $sql .= " AND language = '".Database::escape_string($language)."' ";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sql does not seem to be defined for all execution paths leading up to this point.
Loading history...
729
        }
730
731
        // Sent to active users.
732
        $sql .= " AND email <>'' AND active = 1 ";
733
734
        // Expiration date
735
        $sql .= " AND (expiration_date = '' OR expiration_date IS NULL OR expiration_date > '$now') ";
736
737
        if ((empty($teacher) || $teacher == '0') && (empty($student) || $student == '0')) {
738
            return true;
739
        }
740
741
        $userListToFilter = [];
742
        // @todo check if other filters will apply for the career/promotion option.
743
        if (isset($announcement->career_id) && !empty($announcement->career_id)) {
744
            $promotion = new Promotion();
745
            $promotionList = $promotion->get_all_promotions_by_career_id($announcement->career_id);
746
            if (isset($announcement->promotion_id) && !empty($announcement->promotion_id)) {
747
                $promotionList = [];
748
                $promotionList[] = $promotion->get($announcement->promotion_id);
749
            }
750
751
            if (!empty($promotionList)) {
752
                foreach ($promotionList as $promotion) {
753
                    $sessionList = SessionManager::get_all_sessions_by_promotion($promotion['id']);
754
                    foreach ($sessionList as $session) {
755
                        if ($teacher) {
756
                            $users = SessionManager::get_users_by_session($session['id'], 2);
757
                            if (!empty($users)) {
758
                                $userListToFilter = array_merge($users, $userListToFilter);
759
                            }
760
                        }
761
762
                        if ($student) {
763
                            $users = SessionManager::get_users_by_session($session['id'], 0);
764
                            if (!empty($users)) {
765
                                $userListToFilter = array_merge($users, $userListToFilter);
766
                            }
767
                        }
768
                    }
769
                }
770
            }
771
        }
772
773
        if (!empty($userListToFilter)) {
774
            $userListToFilter = array_column($userListToFilter, 'user_id');
775
            $userListToFilterToString = implode("', '", $userListToFilter);
776
            $sql .= " AND (u.user_id IN ('$userListToFilterToString') ) ";
777
        }
778
779
        $result = Database::query($sql);
780
        if ($result === false) {
781
            return false;
782
        }
783
784
        $message_sent = false;
785
        while ($row = Database::fetch_array($result, 'ASSOC')) {
786
            MessageManager::send_message_simple($row['user_id'], $title, $content);
787
            $message_sent = true;
788
        }
789
790
        // Minor validation to clean up the attachment files in the announcement
791
        if (!empty($_FILES)) {
792
            $attachments = $_FILES;
793
            foreach ($attachments as $attachment) {
794
                unlink($attachment['tmp_name']);
795
            }
796
        }
797
798
        return $message_sent; //true if at least one e-mail was sent
799
    }
800
801
    /**
802
     * Displays announcements as an slideshow.
803
     *
804
     * @param string $visible see self::VISIBLE_* constants
805
     * @param int    $id      The identifier of the announcement to display
806
     *
807
     * @return string
808
     */
809
    public static function displayAnnouncementsSlider($visible, $id = null)
810
    {
811
        $user_selected_language = Database::escape_string(api_get_interface_language());
812
        $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
813
814
        $cut_size = 500;
815
        $now = api_get_utc_datetime();
816
        $sql = "SELECT * FROM $table
817
                WHERE
818
                    (lang = '$user_selected_language' OR lang = '') AND
819
                    ('$now' >= date_start AND '$now' <= date_end) ";
820
821
        $sql .= self::getVisibilityCondition($visible);
822
823
        if (isset($id) && !empty($id)) {
824
            $id = (int) $id;
825
            $sql .= " AND id = $id ";
826
        }
827
828
        if (api_is_multiple_url_enabled()) {
829
            $current_url_id = api_get_current_access_url_id();
830
            $sql .= " AND access_url_id IN ('1', '$current_url_id') ";
831
        }
832
833
        $checkCareers = api_get_configuration_value('allow_careers_in_global_announcements') === true;
834
835
        $userId = api_get_user_id();
836
837
        $promotion = new Promotion();
838
        $sql .= ' ORDER BY date_start DESC';
839
        $result = Database::query($sql);
840
        $announcements = [];
841
        if (Database::num_rows($result) > 0) {
842
            while ($announcement = Database::fetch_object($result)) {
843
                if ($checkCareers && !empty($announcement->career_id)) {
844
                    if (!empty($announcement->promotion_id)) {
845
                        $promotionList[] = $announcement->promotion_id;
846
                    } else {
847
                        $promotionList = $promotion->get_all_promotions_by_career_id($announcement->career_id);
848
                        if (!empty($promotionList)) {
849
                            $promotionList = array_column($promotionList, 'id');
850
                        }
851
                    }
852
853
                    $show = false;
854
                    foreach ($promotionList as $promotionId) {
855
                        $sessionList = SessionManager::get_all_sessions_by_promotion($promotionId);
856
                        foreach ($sessionList as $session) {
857
                            $status = (int) SessionManager::getUserStatusInSession($userId, $session['id']);
858
                            if ($visible === self::VISIBLE_TEACHER && $status === 2) {
859
                                $show = true;
860
                                break 2;
861
                            }
862
863
                            if ($visible === self::VISIBLE_STUDENT && $status === 1) {
864
                                $show = true;
865
                                break 2;
866
                            }
867
                        }
868
                    }
869
870
                    if (false === $show) {
871
                        continue;
872
                    }
873
                }
874
                $announcementData = [
875
                    'id' => $announcement->id,
876
                    'title' => $announcement->title,
877
                    'content' => $announcement->content,
878
                    'readMore' => null,
879
                ];
880
881
                if (empty($id)) {
882
                    if (api_strlen(strip_tags($announcement->content)) > $cut_size) {
883
                        $announcementData['content'] = cut($announcement->content, $cut_size);
884
                        $announcementData['readMore'] = true;
885
                    }
886
                }
887
888
                $announcements[] = $announcementData;
889
            }
890
        }
891
892
        if (count($announcements) === 0) {
893
            return null;
894
        }
895
896
        $template = new Template(null, false, false);
897
        $template->assign('announcements', $announcements);
898
        $layout = $template->get_template('announcement/slider.tpl');
899
900
        return $template->fetch($layout);
901
    }
902
903
    /**
904
     * Get the HTML code for an announcement.
905
     *
906
     * @param int $announcementId The announcement ID
907
     * @param int $visibility     The announcement visibility
908
     *
909
     * @return string The HTML code
910
     */
911
    public static function displayAnnouncement($announcementId, $visibility)
912
    {
913
        $selectedUserLanguage = Database::escape_string(api_get_interface_language());
914
        $announcementTable = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
915
        $now = api_get_utc_datetime();
916
        $announcementId = (int) $announcementId;
917
918
        $whereConditions = [
919
            "(lang = ? OR lang IS NULL OR lang = '') " => $selectedUserLanguage,
920
            "AND (? >= date_start AND ? <= date_end) " => [$now, $now],
921
            "AND id = ? " => $announcementId,
922
        ];
923
924
        $condition = self::getVisibilityCondition($visibility);
925
        $whereConditions[$condition] = 1;
926
927
        if (api_is_multiple_url_enabled()) {
928
            $whereConditions["AND access_url_id IN (1, ?) "] = api_get_current_access_url_id();
929
        }
930
931
        $announcement = Database::select(
932
            '*',
933
            $announcementTable,
934
            [
935
                'where' => $whereConditions,
936
                'order' => 'date_start',
937
            ],
938
            'first'
939
        );
940
941
        $template = new Template(null, false, false);
942
        $template->assign('announcement', $announcement);
943
        $layout = $template->get_template('announcement/view.tpl');
944
945
        return $template->fetch($layout);
946
    }
947
948
    /**
949
     * @return bool
950
     */
951
    public static function newRolesActivated()
952
    {
953
        /* In order to use this option you need to run this SQL changes :
954
         ALTER TABLE sys_announcement ADD COLUMN visible_drh INT DEFAULT 0;
955
         ALTER TABLE sys_announcement ADD COLUMN visible_session_admin INT DEFAULT 0;
956
         ALTER TABLE sys_announcement ADD COLUMN visible_boss INT DEFAULT 0;
957
        */
958
        return api_get_configuration_value('system_announce_extra_roles');
959
    }
960
961
    /**
962
     * @return string
963
     */
964
    public static function getCurrentUserVisibility()
965
    {
966
        if (api_is_anonymous()) {
967
            return SystemAnnouncementManager::VISIBLE_GUEST;
968
        }
969
970
        if (self::newRolesActivated()) {
971
            if (api_is_student_boss()) {
972
                return SystemAnnouncementManager::VISIBLE_STUDENT_BOSS;
973
            }
974
975
            if (api_is_session_admin()) {
976
                return SystemAnnouncementManager::VISIBLE_SESSION_ADMIN;
977
            }
978
979
            if (api_is_drh()) {
980
                return SystemAnnouncementManager::VISIBLE_DRH;
981
            }
982
983
            if (api_is_teacher()) {
984
                return SystemAnnouncementManager::VISIBLE_TEACHER;
985
            } else {
986
                return SystemAnnouncementManager::VISIBLE_STUDENT;
987
            }
988
        } else {
989
            // Default behaviour
990
            return api_is_teacher() ? SystemAnnouncementManager::VISIBLE_TEACHER : SystemAnnouncementManager::VISIBLE_STUDENT;
991
        }
992
    }
993
}
994