Jelajahi Sumber

System announcements rework to use an array of visibilities. BT#12476

- Requires db change
jmontoyaa 7 tahun lalu
induk
melakukan
d8f78f1660

+ 8 - 14
main/admin/system_announcements.php

@@ -28,7 +28,7 @@ $interbreadcrumb[] = array(
     "name" => get_lang('PlatformAdmin'),
 );
 
-$visibleToUsers = SystemAnnouncementManager::getVisibilityList();
+$visibleList = SystemAnnouncementManager::getVisibilityList();
 
 $tool_name = null;
 if (empty($_GET['lang'])) {
@@ -83,6 +83,7 @@ switch ($action) {
             $_GET['person'],
             $status
         );
+        echo Display::return_message(get_lang('Updated'), 'confirmation');
         break;
     case 'delete':
         // Delete an announcement.
@@ -119,14 +120,11 @@ switch ($action) {
             substr(api_get_local_time($announcement->date_end), 0, 16);
 
         $data = (array) $announcement;
-        foreach ($visibleToUsers as $key => $value) {
+        foreach ($visibleList as $key => $value) {
             if (isset($data[$key])) {
                 $values[$key] = $data[$key];
             }
         }
-        /*$values['visible_teacher'] = $announcement->visible_teacher;
-        $values['visible_student'] = $announcement->visible_student;
-        $values['visible_guest'] = $announcement->visible_guest;*/
 
         $values['lang'] = $announcement->lang;
         $values['action'] = 'edit';
@@ -176,7 +174,7 @@ if ($action_todo) {
     );
 
     $group = [];
-    foreach ($visibleToUsers as $key => $name) {
+    foreach ($visibleList as $key => $name) {
         $group[] = $form->createElement(
             'checkbox',
             $key,
@@ -222,7 +220,7 @@ if ($action_todo) {
     if ($form->validate()) {
         $values = $form->getSubmitValues();
         $visibilityResult = [];
-        foreach ($visibleToUsers as $key => $value) {
+        foreach ($visibleList as $key => $value) {
             if (!isset($values[$key])) {
                 $values[$key] = false;
             }
@@ -316,7 +314,7 @@ if ($show_announcement_list) {
         $row[] = api_convert_and_format_date($announcement->date_end);
 
         $data = (array) $announcement;
-        foreach ($visibleToUsers as $key => $value) {
+        foreach ($visibleList as $key => $value) {
             $value = $data[$key];
             $action = $value ? 'make_invisible' : 'make_visible';
             $row[] = "<a href=\"?id=".$announcement->id."&person=".$key."&action=".$action."\">".
@@ -339,15 +337,11 @@ if ($show_announcement_list) {
     $table->set_header(4, get_lang('EndTimeWindow'));
 
     $count = 5;
-    foreach ($visibleToUsers as $key => $value) {
-        $table->set_header($count, $value);
+    foreach ($visibleList as $key => $title) {
+        $table->set_header($count, $title);
         $count++;
     }
 
-    //$table->set_header(5, get_lang('Teacher'));
-    //$table->set_header(6, get_lang('Student'));
-    //$table->set_header(7, get_lang('Guest'));
-
     $table->set_header($count++, get_lang('Language'));
     $table->set_header($count++, get_lang('Modify'), false, 'width="50px"');
     $form_actions = array();

+ 111 - 97
main/inc/lib/system_announcements.lib.php

@@ -6,16 +6,57 @@
  */
 class SystemAnnouncementManager
 {
-    const VISIBLE_GUEST = 1;
-    const VISIBLE_STUDENT = 2;
-    const VISIBLE_TEACHER = 3;
+    const VISIBLE_GUEST = 'visible_guest';
+    const VISIBLE_STUDENT = 'visible_student';
+    const VISIBLE_TEACHER = 'visible_teacher';
+    // Requires DB change
+    const VISIBLE_DRH = 'visible_drh';
+    const VISIBLE_SESSION_ADMIN = 'visible_session_admin';
+    const VISIBLE_STUDENT_BOSS = 'visible_boss';
+
+    /**
+     * @return array
+     */
+    public static function getVisibilityList()
+    {
+        $extraRoles = self::newRolesActivated();
+
+        $visibleToUsers = [
+            self::VISIBLE_TEACHER => get_lang('Teacher'),
+            self::VISIBLE_STUDENT => get_lang('Student'),
+            self::VISIBLE_GUEST => get_lang('Guest')
+        ];
+
+        if ($extraRoles) {
+            $visibleToUsers[self::VISIBLE_DRH] = get_lang('DRH');
+            $visibleToUsers[self::VISIBLE_SESSION_ADMIN] = get_lang('SessionAdministrator');
+            $visibleToUsers[self::VISIBLE_STUDENT_BOSS] = get_lang('StudentBoss');
+        }
+
+        return $visibleToUsers;
+    }
+
+    /**
+     * @param string $visibility
+     * @return string
+     */
+    public static function getVisibilityCondition($visibility)
+    {
+        $list = self::getVisibilityList();
+        $visibilityCondition = " AND ".self::VISIBLE_GUEST." = 1 ";
+        if (in_array($visibility, array_keys($list))) {
+            $visibilityCondition = " AND $visibility = 1 ";
+        }
+
+        return $visibilityCondition;
+    }
 
     /**
      * Displays all announcements
-     * @param int $visible VISIBLE_GUEST, VISIBLE_STUDENT or VISIBLE_TEACHER
+     * @param string $visibility VISIBLE_GUEST, VISIBLE_STUDENT or VISIBLE_TEACHER
      * @param int $id The identifier of the announcement to display
      */
-    public static function display_announcements($visible, $id = -1)
+    public static function display_announcements($visibility, $id = -1)
     {
         $user_selected_language = api_get_interface_language();
         $db_table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
@@ -37,17 +78,8 @@ class SystemAnnouncementManager
                     (lang='$user_selected_language' OR lang IS NULL) AND
                     (('$now' BETWEEN date_start AND date_end) OR date_end='0000-00-00') ";
 
-        switch ($visible) {
-            case self::VISIBLE_GUEST:
-                $sql .= " AND visible_guest = 1 ";
-                break;
-            case self::VISIBLE_STUDENT:
-                $sql .= " AND visible_student = 1 ";
-                break;
-            case self::VISIBLE_TEACHER:
-                $sql .= " AND visible_teacher = 1 ";
-                break;
-        }
+
+        $sql .= self::getVisibilityCondition($visibility);
 
         if (count($groups) > 0) {
             $sql .= " OR id IN (
@@ -98,13 +130,13 @@ class SystemAnnouncementManager
     }
 
     /**
-     * @param $visible
-     * @param $id
+     * @param string $visibility
+     * @param int $id
      * @param int $start
      * @param string $user_id
      * @return string
      */
-    public static function display_all_announcements($visible, $id = -1, $start = 0, $user_id = '')
+    public static function displayAllAnnouncements($visibility, $id = -1, $start = 0, $user_id = '')
     {
         $user_selected_language = api_get_interface_language();
         $start = intval($start);
@@ -120,25 +152,15 @@ class SystemAnnouncementManager
         // Checks if tables exists to not break platform not updated
         $groups_string = '('.implode($groups, ',').')';
 
-        $db_table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
+        $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
         $now = api_get_utc_datetime();
 
-        $sql = "SELECT * FROM ".$db_table."
+        $sql = "SELECT * FROM $table
                 WHERE
                     (lang = '$user_selected_language' OR lang IS NULL) AND
                     ( '$now' >= date_start AND '$now' <= date_end) ";
 
-        switch ($visible) {
-            case self::VISIBLE_GUEST:
-                $sql .= " AND visible_guest = 1 ";
-                break;
-            case self::VISIBLE_STUDENT:
-                $sql .= " AND visible_student = 1 ";
-                break;
-            case self::VISIBLE_TEACHER:
-                $sql .= " AND visible_teacher = 1 ";
-                break;
-        }
+        $sql .= self::getVisibilityCondition($visibility);
 
         if (count($groups) > 0) {
             $sql .= " OR id IN (
@@ -230,24 +252,13 @@ class SystemAnnouncementManager
     public static function count_nb_announcement($start = 0, $user_id = '')
     {
         $start = intval($start);
-        $visibility = api_is_allowed_to_create_course() ? self::VISIBLE_TEACHER : self::VISIBLE_STUDENT;
         $user_selected_language = api_get_interface_language();
         $db_table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
         $sql = 'SELECT id FROM '.$db_table.'
                 WHERE (lang="'.$user_selected_language.'" OR lang IS NULL) ';
-        if (isset($user_id)) {
-            switch ($visibility) {
-                case self::VISIBLE_GUEST:
-                    $sql .= " AND visible_guest = 1 ";
-                    break;
-                case self::VISIBLE_STUDENT:
-                    $sql .= " AND visible_student = 1 ";
-                    break;
-                case self::VISIBLE_TEACHER:
-                    $sql .= " AND visible_teacher = 1 ";
-                    break;
-            }
-        }
+
+        $visibility = self::getCurrentUserVisibility();
+        $sql .= self::getVisibilityCondition($visibility);
 
         $current_access_url_id = 1;
         if (api_is_multiple_url_enabled()) {
@@ -630,32 +641,6 @@ class SystemAnnouncementManager
         return $announcement;
     }
 
-    /**
-     * @return array
-     */
-    public static function getVisibilityList()
-    {
-        $extraRoles = api_get_configuration_value('system_announce_extra_roles');
-        /* Requires DB change:
-         ALTER TABLE sys_announcement ADD COLUMN visible_drh INT DEFAULT 0;
-         ALTER TABLE sys_announcement ADD COLUMN visible_session_admin INT DEFAULT 0;
-         ALTER TABLE sys_announcement ADD COLUMN visible_boss INT DEFAULT 0;
-        */
-        $visibleToUsers = [
-            'visible_teacher' => get_lang('Teacher'),
-            'visible_student' => get_lang('Student'),
-            'visible_guest' => get_lang('Guest')
-        ];
-
-        if ($extraRoles) {
-            $visibleToUsers['visible_drh'] = get_lang('DRH');
-            $visibleToUsers['visible_session_admin'] = get_lang('SessionAdministrator');
-            $visibleToUsers['visible_boss'] = get_lang('StudentBoss');
-        }
-
-        return $visibleToUsers;
-    }
-
     /**
      * Change the visibility of an announcement
      * @param int $id
@@ -778,10 +763,12 @@ class SystemAnnouncementManager
 
     /**
      * Displays announcements as an slideshow
-     * @param int $visible VISIBLE_GUEST, VISIBLE_STUDENT or VISIBLE_TEACHER
+     * @param string $visible see self::VISIBLE_* constants
      * @param int $id The identifier of the announcement to display
+     *
+     * @return string
      */
-    public static function display_announcements_slider($visible, $id = null)
+    public static function displayAnnouncementsSlider($visible, $id = null)
     {
         $user_selected_language = Database::escape_string(api_get_interface_language());
         $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
@@ -793,17 +780,7 @@ class SystemAnnouncementManager
                     (lang = '$user_selected_language' OR lang = '') AND
                     ('$now' >= date_start AND '$now' <= date_end) ";
 
-        switch ($visible) {
-            case self::VISIBLE_GUEST:
-                $sql .= " AND visible_guest = 1 ";
-                break;
-            case self::VISIBLE_STUDENT:
-                $sql .= " AND visible_student = 1 ";
-                break;
-            case self::VISIBLE_TEACHER:
-                $sql .= " AND visible_teacher = 1 ";
-                break;
-        }
+        $sql .= self::getVisibilityCondition($visible);
 
         if (isset($id) && !empty($id)) {
             $id = intval($id);
@@ -860,7 +837,6 @@ class SystemAnnouncementManager
     {
         $selectedUserLanguage = Database::escape_string(api_get_interface_language());
         $announcementTable = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
-
         $now = api_get_utc_datetime();
 
         $whereConditions = [
@@ -869,17 +845,8 @@ class SystemAnnouncementManager
             "AND id = ? " => intval($announcementId)
         ];
 
-        switch ($visibility) {
-            case self::VISIBLE_GUEST:
-                $whereConditions["AND visible_guest = ? "] = 1;
-                break;
-            case self::VISIBLE_STUDENT:
-                $whereConditions["AND visible_student = ? "] = 1;
-                break;
-            case self::VISIBLE_TEACHER:
-                $whereConditions["AND visible_teacher = ? "] = 1;
-                break;
-        }
+        $condition = self::getVisibilityCondition($visibility);
+        $whereConditions[$condition] = 1;
 
         if (api_is_multiple_url_enabled()) {
             $whereConditions["AND access_url_id IN (1, ?) "] = api_get_current_access_url_id();
@@ -901,4 +868,51 @@ class SystemAnnouncementManager
 
         return $template->fetch($layout);
     }
+
+    /**
+     * @return bool
+     */
+    public static function newRolesActivated()
+    {
+        /* In order to use this option you need to run this SQL changes :
+         ALTER TABLE sys_announcement ADD COLUMN visible_drh INT DEFAULT 0;
+         ALTER TABLE sys_announcement ADD COLUMN visible_session_admin INT DEFAULT 0;
+         ALTER TABLE sys_announcement ADD COLUMN visible_boss INT DEFAULT 0;
+        */
+
+        return api_get_configuration_value('system_announce_extra_roles');
+    }
+
+    /**
+     * @return string
+     */
+    public static function getCurrentUserVisibility()
+    {
+        if (api_is_anonymous()) {
+            return SystemAnnouncementManager::VISIBLE_GUEST;
+        }
+
+        if (self::newRolesActivated()) {
+            if (api_is_student_boss()) {
+                return SystemAnnouncementManager::VISIBLE_STUDENT_BOSS;
+            }
+
+            if (api_is_session_admin()) {
+                return SystemAnnouncementManager::VISIBLE_SESSION_ADMIN;
+            }
+
+            if (api_is_drh()) {
+                return SystemAnnouncementManager::VISIBLE_DRH;
+            }
+
+            if (api_is_allowed_to_create_course()) {
+                return SystemAnnouncementManager::VISIBLE_TEACHER;
+            } else {
+                return SystemAnnouncementManager::VISIBLE_STUDENT;
+            }
+        } else {
+            // Default behaviour
+            return api_is_allowed_to_create_course() ? SystemAnnouncementManager::VISIBLE_TEACHER : SystemAnnouncementManager::VISIBLE_STUDENT;
+        }
+    }
 }

+ 5 - 6
main/inc/lib/userportal.lib.php

@@ -102,7 +102,6 @@ class IndexManager
      */
     public function return_announcements($show_slide = true)
     {
-        // Display System announcements
         $hideAnnouncements = api_get_setting('hide_global_announcements_when_not_connected');
         $currentUserId = api_get_user_id();
         if ($hideAnnouncements == 'true' && empty($currentUserId)) {
@@ -112,26 +111,26 @@ class IndexManager
         $announcement = intval($announcement);
 
         if (!api_is_anonymous() && $this->user_id) {
-            $visibility = api_is_allowed_to_create_course() ? SystemAnnouncementManager::VISIBLE_TEACHER : SystemAnnouncementManager::VISIBLE_STUDENT;
+            $visibility = SystemAnnouncementManager::getCurrentUserVisibility();
             if ($show_slide) {
-                $announcements = SystemAnnouncementManager:: display_announcements_slider(
+                $announcements = SystemAnnouncementManager::displayAnnouncementsSlider(
                     $visibility,
                     $announcement
                 );
             } else {
-                $announcements = SystemAnnouncementManager:: display_all_announcements(
+                $announcements = SystemAnnouncementManager::displayAllAnnouncements(
                     $visibility,
                     $announcement
                 );
             }
         } else {
             if ($show_slide) {
-                $announcements = SystemAnnouncementManager:: display_announcements_slider(
+                $announcements = SystemAnnouncementManager::displayAnnouncementsSlider(
                     SystemAnnouncementManager::VISIBLE_GUEST,
                     $announcement
                 );
             } else {
-                $announcements = SystemAnnouncementManager:: display_all_announcements(
+                $announcements = SystemAnnouncementManager::displayAllAnnouncements(
                     SystemAnnouncementManager::VISIBLE_GUEST,
                     $announcement
                 );

+ 2 - 9
news_list.php

@@ -1,18 +1,11 @@
 <?php
 /* For licensing terms, see /license.txt */
 
-// including necessary files
 require_once 'main/inc/global.inc.php';
 $tool_name = get_lang('SystemAnnouncements');
-
-if (api_is_anonymous()) {
-    $visibility = SystemAnnouncementManager::VISIBLE_GUEST;
-} else {
-    $visibility = api_is_allowed_to_create_course() ? SystemAnnouncementManager::VISIBLE_TEACHER : SystemAnnouncementManager::VISIBLE_STUDENT;
-}
-
+$visibility = SystemAnnouncementManager::getCurrentUserVisibility();
 if (!isset($_GET['id']) || empty($_GET['id'])) {
-    $content = SystemAnnouncementManager::display_announcements_slider($visibility, $_GET['id']);
+    $content = SystemAnnouncementManager::displayAnnouncementsSlider($visibility, $_GET['id']);
 } else {
     $content = SystemAnnouncementManager::displayAnnouncement($_GET['id'], $visibility);
 }