Browse Source

Using formvalidator helper in order to add users/groups see #6854

Julio Montoya 11 years ago
parent
commit
c1b1816936

+ 4 - 3
main/announcements/announcements.php

@@ -550,7 +550,8 @@ if ($display_form) {
                 $content_to_modify = get_lang('YourAccountIsActiveYouCanLoginAndCheckYourCourses');
             }
         }
-        AnnouncementManager::show_to_form($form, array());
+
+        CourseManager::addUserGroupMultiSelect($form, array());
 
         if (!isset($announcement_to_modify)) {
             $announcement_to_modify = '';
@@ -561,10 +562,10 @@ if ($display_form) {
         if (!isset($announcement_to_modify)) {
             $announcement_to_modify = "";
         }
-        AnnouncementManager::show_to_form_group($form, $group_id, array());
+        CourseManager::addGroupMultiSelect($form, $group_id, array());
         $form->addElement('checkbox', 'email_ann', null, get_lang('EmailOption'));
-
     }
+
     if (isset($announcementInfo) && !empty($announcementInfo)) {
         $defaults = array(
             'emailTitle' => $title_to_modify,

+ 0 - 234
main/inc/lib/announcements.inc.php

@@ -674,7 +674,6 @@ class AnnouncementManager
      */
     public static function get_all_annoucement_by_user_course($course_code, $user_id)
     {
-
         $course_info = api_get_course_info($course_code);
         $course_id = $course_info['real_id'];
 
@@ -719,195 +718,6 @@ class AnnouncementManager
         return false;
     }
 
-    /**
-     * this function shows the form for sending a message to a specific group or user.
-     */
-    /**
-     * @param FormValidator $form
-     * @param $to_already_selected
-     */
-    public static function show_to_form($form, $to_already_selected)
-    {
-        $user_list = self::get_course_users();
-        $group_list = self::get_course_groups();
-
-        $array = self::construct_not_selected_select_form($group_list, $user_list, $to_already_selected);
-
-        $result = array();
-        foreach ($array as $content) {
-            $result[$content['value']] = $content['content'];
-        }
-
-        $group = $form->addElement('advmultiselect', 'users', get_lang('Users'), $result);
-        $group->setButtonAttributes('add');
-        $group->setButtonAttributes('remove');
-    }
-
-    /**
-     * this function shows the form for sending a message to a specific group or user.
-     */
-    /**
-     * @param FormValidator $form
-     * @param int $group_id
-     * @param array $to
-     */
-    public static function show_to_form_group($form, $group_id, $to = array())
-    {
-        $group_users = GroupManager::get_subscribed_users($group_id);
-
-        $array = self::construct_not_selected_select_form(null, $group_users, $to);
-
-        $result = array();
-        foreach ($array as $content) {
-            $result[$content['value']] = $content['content'];
-        }
-
-        $group = $form->addElement('advmultiselect', 'users', get_lang('Users'), $result);
-        $group->setButtonAttributes('add');
-        $group->setButtonAttributes('remove');
-    }
-
-    /*
-      CONSTRUCT_NOT_SELECT_SELECT_FORM
-     */
-
-    /**
-     * this function shows the form for sending a message to a specific group or user.
-     */
-    public static function construct_not_selected_select_form(
-        $group_list = null,
-        $user_list = null,
-        $to_already_selected = array()
-    ) {
-        if (empty($to_already_selected)) {
-            $to_already_selected = array();
-        }
-
-        $result = array();
-        // adding the groups to the select form
-        if ($group_list) {
-            foreach ($group_list as $this_group) {
-                if (is_array($to_already_selected)) {
-                    if (!in_array(
-                        "GROUP:".$this_group['id'],
-                        $to_already_selected
-                    )
-                    ) { // $to_already_selected is the array containing the groups (and users) that are already selected
-                        $user_label = ($this_group['userNb'] > 0) ? get_lang('Users') : get_lang('LowerCaseUser');
-                        $user_disabled = ($this_group['userNb'] > 0) ? "" : "disabled=disabled";
-                        $result []= array(
-                            'disabled' => $user_disabled,
-                            'value' => "GROUP:".$this_group['id'],
-                            'content' => "G: ".$this_group['name']." - ".$this_group['userNb']." ".$user_label
-                        );
-                    }
-                }
-            }
-        }
-
-        // adding the individual users to the select form
-        if ($user_list) {
-            foreach ($user_list as $user) {
-                if (is_array($to_already_selected)) {
-                    if (!in_array(
-                        "USER:".$user['user_id'],
-                        $to_already_selected
-                    )
-                    ) { // $to_already_selected is the array containing the users (and groups) that are already selected
-
-                        $result[]= array(
-                            'value' => "USER:".$user['user_id'],
-                            'content' => api_get_person_name($user['firstname'], $user['lastname'])
-                        );
-                    }
-                }
-            }
-        }
-
-        return $result;
-    }
-
-    /*
-      CONSTRUCT_SELECTED_SELECT_FORM
-     */
-
-    /**
-     * this function shows the form for sending a message to a specific group or user.
-     */
-    public static function construct_selected_select_form($group_list = null, $user_list = null, $to_already_selected)
-    {
-        // we separate the $to_already_selected array (containing groups AND users into
-        // two separate arrays
-        $groupuser = array();
-        if (is_array($to_already_selected)) {
-            $groupuser = self::separate_users_groups($to_already_selected);
-        }
-        $groups_to_already_selected = $groupuser['groups'];
-        $users_to_already_selected = $groupuser['users'];
-
-        // we load all the groups and all the users into a reference array that we use to search the name of the group / user
-        $ref_array_groups = self::get_course_groups();
-        $ref_array_users = self::get_course_users();
-
-        // we construct the form of the already selected groups / users
-        echo '<select id="selectedform" name="selectedform[]" size="7" multiple class="span4">';
-        if (is_array($to_already_selected)) {
-            foreach ($to_already_selected as $groupuser) {
-                list($type, $id) = explode(":", $groupuser);
-                if ($type == "GROUP") {
-                    echo "<option value=\"".$groupuser."\">G: ".$ref_array_groups[$id]['name']."</option>";
-                } else {
-                    foreach ($ref_array_users as $key => $value) {
-                        if ($value['user_id'] == $id) {
-                            echo "<option value=\"".$groupuser."\" title='".sprintf(
-                                get_lang('LoginX'),
-                                $value['username']
-                            )."'>".api_get_person_name($value['firstname'], $value['lastname'])."</option>";
-                            break;
-                        }
-                    }
-                }
-            }
-        } else {
-            if ($to_already_selected == 'everyone') {
-                // adding the groups to the select form
-                if (is_array($ref_array_groups)) {
-                    foreach ($ref_array_groups as $this_group) {
-                        //api_display_normal_message("group " . $thisGroup[id] . $thisGroup[name]);
-                        if (!is_array($to_already_selected) || !in_array(
-                            "GROUP:".$this_group['id'],
-                            $to_already_selected
-                        )
-                        ) { // $to_already_selected is the array containing the groups (and users) that are already selected
-                            echo "<option value=\"GROUP:".$this_group['id']."\">",
-                            "G: ", $this_group['name'], " &ndash; ".$this_group['userNb']." ".get_lang('Users').
-                                "</option>";
-                        }
-                    }
-                }
-                // adding the individual users to the select form
-                foreach ($ref_array_users as $this_user) {
-                    if (!is_array($to_already_selected) || !in_array(
-                        "USER:".$this_user['user_id'],
-                        $to_already_selected
-                    )
-                    ) { // $to_already_selected is the array containing the users (and groups) that are already selected
-                        echo "<option value=\"USER:", $this_user['user_id'], "\"  title='".sprintf(
-                            get_lang('LoginX'),
-                            $user['username']
-                        )."'>",
-                        "", api_get_person_name($this_user['firstname'], $this_user['lastname']),
-                        "</option>";
-                    }
-                }
-            }
-        }
-        echo "</select>";
-    }
-
-    /*
-      DATA FUNCTIONS
-     */
 
     /**
      * Returns announcement info from its id
@@ -939,45 +749,6 @@ class AnnouncementManager
         return array();
     }
 
-    /**
-     * this function gets all the users of the course,
-     * including users from linked courses
-     * @todo deprecate this function, use CourseManager class
-     */
-    public static function get_course_users()
-    {
-        //this would return only the users from real courses:
-        $session_id = api_get_session_id();
-        if ($session_id != 0) {
-            $user_list = CourseManager::get_real_and_linked_user_list(api_get_course_id(), true, $session_id);
-        } else {
-            $user_list = CourseManager::get_real_and_linked_user_list(api_get_course_id(), false, 0);
-        }
-
-        return $user_list;
-    }
-
-    /**
-     * this function gets all the groups of the course,
-     * not including linked courses
-     */
-    public static function get_course_groups()
-    {
-        $session_id = api_get_session_id();
-        if ($session_id != 0) {
-            $new_group_list = CourseManager::get_group_list_of_course(api_get_course_id(), $session_id, 1);
-        } else {
-            $new_group_list = CourseManager::get_group_list_of_course(api_get_course_id(), 0, 1);
-        }
-
-        return $new_group_list;
-    }
-
-    /*
-     *
-      LOAD_EDIT_USERS
-     */
-
     /**
      * This tools loads all the users and all the groups who have received
      * a specific item (in this case an announcement item)
@@ -1011,14 +782,9 @@ class AnnouncementManager
                 $to[] = "USER:".$to_user;
             }
         }
-
         return $to;
     }
 
-    /*
-      USER_GROUP_FILTER_JAVASCRIPT
-     */
-
     /**
      * returns the javascript for setting a filter
      * this goes into the $htmlHeadXtra[] array

+ 148 - 6
main/inc/lib/course.lib.php

@@ -1498,10 +1498,11 @@ class CourseManager
      * Get the list of groups from the course
      * @param   string  Course code
      * @param   int     Session ID (optional)
-     * @param   boolean get empty groups (optional)
+     * @param   int     get empty groups (optional)
      * @return  array   List of groups info
      */
-    public static function get_group_list_of_course($course_code, $session_id = 0, $in_get_empty_group = 0) {
+    public static function get_group_list_of_course($course_code, $session_id = 0, $in_get_empty_group = 0)
+    {
         $course_info = api_get_course_info($course_code);
         $course_id = $course_info['real_id'];
         $group_list = array();
@@ -1509,15 +1510,15 @@ class CourseManager
 
         if ($in_get_empty_group == 0) {
             // get only groups that are not empty
-            $sql = "SELECT DISTINCT g.id, g.name
+            $sql = "SELECT DISTINCT g.iid as id, g.name
                 FROM ".Database::get_course_table(TABLE_GROUP)." AS g
                 INNER JOIN ".Database::get_course_table(TABLE_GROUP_USER)." gu
-                ON (g.id = gu.group_id AND g.c_id = $course_id AND gu.c_id = $course_id)
+                ON (g.iid = gu.group_id AND g.c_id = $course_id AND gu.c_id = $course_id)
                 $session_condition
                 ORDER BY g.name";
         } else {
             // get all groups even if they are empty
-            $sql = "SELECT g.id, g.name
+            $sql = "SELECT g.iid as id, g.name
                     FROM ".Database::get_course_table(TABLE_GROUP)." AS g
                     $session_condition
                     AND c_id = $course_id";
@@ -1528,6 +1529,7 @@ class CourseManager
             $group_data['userNb'] = GroupManager::number_of_students($group_data['id'], $course_id);
             $group_list[$group_data['id']] = $group_data;
         }
+
         return $group_list;
     }
 
@@ -4979,4 +4981,144 @@ class CourseManager
         }
     }
 
-} //end class CourseManager
+    /**
+     * this function gets all the users of the course,
+     * including users from linked courses
+     */
+    public static function getCourseUsers()
+    {
+        //this would return only the users from real courses:
+        $session_id = api_get_session_id();
+        if ($session_id != 0) {
+            $user_list = self::get_real_and_linked_user_list(api_get_course_id(), true, $session_id);
+        } else {
+            $user_list = self::get_real_and_linked_user_list(api_get_course_id(), false, 0);
+        }
+
+        return $user_list;
+    }
+
+    /**
+     * this function gets all the groups of the course,
+     * not including linked courses
+     */
+    public static function getCourseGroups()
+    {
+        $session_id = api_get_session_id();
+        if ($session_id != 0) {
+            $new_group_list = self::get_group_list_of_course(api_get_course_id(), $session_id, 1);
+        } else {
+            $new_group_list = self::get_group_list_of_course(api_get_course_id(), 0, 1);
+        }
+
+        return $new_group_list;
+    }
+
+    /**
+     * this function shows the form for sending a message to a specific group or user.
+     */
+    /**
+     * @param FormValidator $form
+     * @param $to_already_selected
+     */
+    public static function addUserGroupMultiSelect(&$form, $to_already_selected)
+    {
+        $user_list = self::getCourseUsers();
+        $group_list = self::getCourseGroups();
+
+        $array = self::buildSelectOptions($group_list, $user_list, $to_already_selected);
+
+        $result = array();
+        foreach ($array as $content) {
+            $result[$content['value']] = $content['content'];
+        }
+
+        $group = $form->addElement('advmultiselect', 'users', get_lang('Users'), $result);
+        $group->setButtonAttributes('add');
+        $group->setButtonAttributes('remove');
+    }
+
+    /**
+     * this function shows the form for sending a message to a specific group or user.
+     */
+    /**
+     * @param FormValidator $form
+     * @param int $group_id
+     * @param array $to
+     */
+    public static function addGroupMultiSelect($form, $group_id, $to = array())
+    {
+        $group_users = GroupManager::get_subscribed_users($group_id);
+
+        $array = self::buildSelectOptions(null, $group_users, $to);
+
+        $result = array();
+        foreach ($array as $content) {
+            $result[$content['value']] = $content['content'];
+        }
+
+        $group = $form->addElement('advmultiselect', 'users', get_lang('Users'), $result);
+        $group->setButtonAttributes('add');
+        $group->setButtonAttributes('remove');
+    }
+
+
+    /**
+     * this function shows the form for sending a message to a specific group or user.
+     */
+    public static function buildSelectOptions(
+        $group_list = null,
+        $user_list = null,
+        $to_already_selected = array()
+    ) {
+        if (empty($to_already_selected)) {
+            $to_already_selected = array();
+        }
+
+        $result = array();
+        // adding the groups to the select form
+        if ($group_list) {
+            foreach ($group_list as $this_group) {
+                if (is_array($to_already_selected)) {
+                    if (!in_array(
+                        "GROUP:".$this_group['id'],
+                        $to_already_selected
+                    )
+                    ) { // $to_already_selected is the array containing the groups (and users) that are already selected
+                        $user_label = ($this_group['userNb'] > 0) ? get_lang('Users') : get_lang('LowerCaseUser');
+                        $user_disabled = ($this_group['userNb'] > 0) ? "" : "disabled=disabled";
+                        $result []= array(
+                            'disabled' => $user_disabled,
+                            'value' => "GROUP:".$this_group['id'],
+                            'content' => "G: ".$this_group['name']." - ".$this_group['userNb']." ".$user_label
+                        );
+                    }
+                }
+            }
+        }
+
+        // adding the individual users to the select form
+        if ($user_list) {
+            foreach ($user_list as $user) {
+                if (is_array($to_already_selected)) {
+                    if (!in_array(
+                        "USER:".$user['user_id'],
+                        $to_already_selected
+                    )
+                    ) { // $to_already_selected is the array containing the users (and groups) that are already selected
+
+                        $result[]= array(
+                            'value' => "USER:".$user['user_id'],
+                            'content' => api_get_person_name($user['firstname'], $user['lastname'])
+                        );
+                    }
+                }
+            }
+        }
+
+        return $result;
+    }
+
+
+
+}

+ 46 - 4
main/inc/lib/text.lib.php

@@ -9,7 +9,8 @@
  * @package chamilo.library
  */
 
-class Text {
+class Text
+{
 
     /**
      * This function strips all html-tags found in the input string and outputs a pure text.
@@ -17,7 +18,8 @@ class Text {
      * @param  string $string    The input string with html-tags to be converted to plain text.
      * @return string            The returned plain text as a result.
      */
-    static function api_html_to_text($string) {
+    static function api_html_to_text($string)
+    {
         // These purifications have been found experimentally, for nice looking output.
         $string = preg_replace('/<br[^>]*>/i', "\n", $string);
         $string = preg_replace('/<\/?(div|p|h[1-6]|table|ol|ul|blockquote)[^>]*>/i', "\n", $string);
@@ -39,7 +41,8 @@ class Text {
      * @param  string $string                The input html-formatted text.
      * @return string                        Returns the detected encoding.
      */
-    static function api_detect_encoding_html($string) {
+    static function api_detect_encoding_html($string)
+    {
         if (@preg_match('/<head.*(<meta[^>]*content=[^>]*>).*<\/head>/si', $string, $matches)) {
             if (@preg_match('/<meta[^>]*charset=(.*)["\';][^>]*>/si', $matches[1], $matches)) {
                 return api_refine_encoding_id(trim($matches[1]));
@@ -568,7 +571,12 @@ class Text {
         return $file_size;
     }
 
-    static function return_datetime_from_array($array) {
+    /**
+     * @param $array
+     * @return string
+     */
+    static function return_datetime_from_array($array)
+    {
         $year	 = '0000';
         $month = $day = $hours = $minutes = $seconds = '00';
         if (isset($array['Y']) && (isset($array['F']) || isset($array['M']))  && isset($array['d']) && isset($array['H']) && isset($array['i'])) {
@@ -587,4 +595,38 @@ class Text {
         }
         return $datetime;
     }
+
+
+    /**
+     * Converts 2008-10-06 12:45:00 to -> array('prefix' => array(year'=>2008, 'month'=>10, etc...)
+     * @param string
+     * @param string
+     * @param array
+     */
+    static function convert_date_to_array($date, $group)
+    {
+        $parts = explode(' ', $date);
+        $date_parts = explode('-', $parts[0]);
+        $date_parts_tmp = array();
+        foreach ($date_parts as $item) {
+            $date_parts_tmp[] = intval($item);
+        }
+        $time_parts = explode(':', $parts[1]);
+        $time_parts_tmp = array();
+        foreach ($time_parts as $item) {
+            $time_parts_tmp[] = intval($item);
+        }
+        list($data[$group]['year'], $data[$group]['month'], $data[$group]['day']) = $date_parts_tmp;
+        list($data[$group]['hour'], $data[$group]['minute']) = $time_parts_tmp;
+        return $data;
+    }
+
+    /**
+     * converts 1-9 to 01-09
+     */
+    static function two_digits($number)
+    {
+        $number = (int)$number;
+        return ($number < 10) ? '0'.$number : $number;
+    }
 }