Browse Source

Minor - format code, add cidReq, fix URL

jmontoyaa 7 years ago
parent
commit
885ba7b65d
3 changed files with 107 additions and 53 deletions
  1. 25 7
      main/group/import.php
  2. 68 32
      main/inc/lib/AnnouncementManager.php
  3. 14 14
      main/inc/lib/groupmanager.lib.php

+ 25 - 7
main/group/import.php

@@ -2,6 +2,7 @@
 /* For licensing terms, see /license.txt */
 
 require_once __DIR__.'/../inc/global.inc.php';
+
 $this_section = SECTION_COURSES;
 $current_course_tool = TOOL_GROUP;
 
@@ -13,14 +14,33 @@ if (!api_is_allowed_to_edit(false, true)) {
 }
 
 $nameTools = get_lang('Import');
-$interbreadcrumb[] = array('url' => 'group.php', 'name' => get_lang('Groups'));
-
-$form = new FormValidator('import', 'post', api_get_self().'?'.api_get_cidreq());
+$interbreadcrumb[] = array(
+    'url' => 'group.php?'.api_get_cidreq(),
+    'name' => get_lang('Groups')
+);
+
+$form = new FormValidator(
+    'import',
+    'post',
+    api_get_self().'?'.api_get_cidreq()
+);
 $form->addElement('header', get_lang('ImportGroups'));
 $form->addElement('file', 'file', get_lang('ImportCSVFileLocation'));
 $form->addRule('file', get_lang('ThisFieldIsRequired'), 'required');
-$form->addElement('checkbox', 'delete_not_in_file', null, get_lang('DeleteItemsNotInFile'));
-$form->addElement('label', null, Display::url(get_lang('ExampleCSVFile'), api_get_path(WEB_CODE_PATH).'group/example.csv'));
+$form->addElement(
+    'checkbox',
+    'delete_not_in_file',
+    null,
+    get_lang('DeleteItemsNotInFile')
+);
+$form->addElement(
+    'label',
+    null,
+    Display::url(
+        get_lang('ExampleCSVFile'),
+        api_get_path(WEB_CODE_PATH).'group/example.csv'
+    )
+);
 $form->addButtonImport(get_lang('Import'));
 
 if ($form->validate()) {
@@ -83,7 +103,5 @@ if ($form->validate()) {
 }
 
 Display::display_header($nameTools, 'Group');
-
 $form->display();
-
 Display::display_footer();

+ 68 - 32
main/inc/lib/AnnouncementManager.php

@@ -208,7 +208,10 @@ class AnnouncementManager
      */
     public static function delete_all_announcements($_course)
     {
-        $announcements = self::get_all_annoucement_by_course($_course, api_get_session_id());
+        $announcements = self::get_all_annoucement_by_course(
+            $_course,
+            api_get_session_id()
+        );
         if (!empty($announcements)) {
             foreach ($announcements as $annon) {
                 api_item_property_update(
@@ -548,7 +551,9 @@ class AnnouncementManager
             }
 
             // store in item_property (first the groups, then the users
-            if (empty($sentTo) || (!empty($sentTo) && isset($sentTo[0]) && $sentTo[0] == 'everyone')) {
+            if (empty($sentTo) ||
+                (!empty($sentTo) && isset($sentTo[0]) && $sentTo[0] == 'everyone')
+            ) {
                 // The message is sent to EVERYONE, so we set the group to 0
                 api_item_property_update(
                     $courseInfo,
@@ -567,7 +572,9 @@ class AnnouncementManager
                 $batchSize = 20;
                 $em = Database::getManager();
                 // Storing the selected groups
-                if (is_array($send_to['groups']) && !empty($send_to['groups'])) {
+                if (is_array($send_to['groups']) &&
+                    !empty($send_to['groups'])
+                ) {
                     $counter = 1;
                     foreach ($send_to['groups'] as $group) {
                         $groupInfo = GroupManager::get_group_properties($group);
@@ -662,7 +669,8 @@ class AnnouncementManager
 
         // Store the attach file
         if ($last_id) {
-            $sql = "UPDATE $tbl_announcement SET id = iid WHERE iid = $last_id";
+            $sql = "UPDATE $tbl_announcement SET id = iid 
+                    WHERE iid = $last_id";
             Database::query($sql);
 
             if (!empty($file)) {
@@ -773,9 +781,17 @@ class AnnouncementManager
 
         if (!empty($file)) {
             if (empty($id_attach)) {
-                self::add_announcement_attachment_file($id, $file_comment, $file);
+                self::add_announcement_attachment_file(
+                    $id,
+                    $file_comment,
+                    $file
+                );
             } else {
-                self::edit_announcement_attachment_file($id_attach, $file, $file_comment);
+                self::edit_announcement_attachment_file(
+                    $id_attach,
+                    $file,
+                    $file_comment
+                );
             }
         }
 
@@ -965,12 +981,12 @@ class AnnouncementManager
      * Returns announcement info from its id
      *
      * @param int $course_id
-     * @param int $annoucement_id
+     * @param int $id
      * @return array
      */
-    public static function get_by_id($course_id, $annoucement_id)
+    public static function get_by_id($course_id, $id)
     {
-        $annoucement_id = intval($annoucement_id);
+        $id = intval($id);
         $course_id = $course_id ? intval($course_id) : api_get_course_int_id();
 
         $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
@@ -988,7 +1004,7 @@ class AnnouncementManager
                WHERE
                     announcement.c_id = $course_id AND
                     ip.tool='announcement' AND
-                    announcement.id = $annoucement_id
+                    announcement.id = $id
                 ";
         $result = Database::query($sql);
         if (Database::num_rows($result)) {
@@ -1029,12 +1045,12 @@ class AnnouncementManager
      */
     public static function load_edit_users($tool, $id)
     {
-        $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
+        $table = Database::get_course_table(TABLE_ITEM_PROPERTY);
         $tool = Database::escape_string($tool);
         $id = intval($id);
         $course_id = api_get_course_int_id();
 
-        $sql = "SELECT * FROM $tbl_item_property
+        $sql = "SELECT * FROM $table
                 WHERE c_id = $course_id AND tool='$tool' AND ref = $id";
         $result = Database::query($sql);
         $to = array();
@@ -1059,11 +1075,14 @@ class AnnouncementManager
 
     /**
      * constructs the form to display all the groups and users the message has been sent to
-     * input:    $sent_to_array is a 2 dimensional array containing the groups and the users
-     *            the first level is a distinction between groups and users:
-     *            $sent_to_array['groups'] * and $sent_to_array['users']
-     *            $sent_to_array['groups'] (resp. $sent_to_array['users']) is also an array
-     *            containing all the id's of the groups (resp. users) who have received this message.
+     * @param array $sent_to_array
+     * input:
+     * $sent_to_array is a 2 dimensional array containing the groups and the users
+     * the first level is a distinction between groups and users:
+     * $sent_to_array['groups'] * and $sent_to_array['users']
+     * $sent_to_array['groups'] (resp. $sent_to_array['users']) is also an array
+     * containing all the id's of the groups (resp. users) who have received this message.
+     * @return string
      * @author Patrick Cool <patrick.cool@>
      */
     public static function sent_to_form($sent_to_array)
@@ -1139,8 +1158,7 @@ class AnnouncementManager
      */
     public static function sent_to($tool, $id)
     {
-        $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
-
+        $table = Database::get_course_table(TABLE_ITEM_PROPERTY);
         $tool = Database::escape_string($tool);
         $id = (int) $id;
 
@@ -1149,7 +1167,7 @@ class AnnouncementManager
         $course_id = api_get_course_int_id();
 
         $sql = "SELECT to_group_id, to_user_id
-                FROM $tbl_item_property
+                FROM $table
                 WHERE c_id = $course_id AND tool = '$tool' AND ref=".$id;
         $result = Database::query($sql);
 
@@ -1214,7 +1232,7 @@ class AnnouncementManager
         $file
     ) {
         $_course = api_get_course_info();
-        $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
+        $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
         $return = 0;
         $announcement_id = intval($announcement_id);
         $course_id = api_get_course_int_id();
@@ -1249,9 +1267,9 @@ class AnnouncementManager
                     'size' => intval($file['size']),
                 ];
 
-                $insertId = Database::insert($tbl_announcement_attachment, $params);
+                $insertId = Database::insert($table, $params);
                 if ($insertId) {
-                    $sql = "UPDATE $tbl_announcement_attachment SET id = iid 
+                    $sql = "UPDATE $table SET id = iid 
                             WHERE iid = $insertId";
                     Database::query($sql);
                 }
@@ -1276,7 +1294,7 @@ class AnnouncementManager
         $file_comment
     ) {
         $_course = api_get_course_info();
-        $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
+        $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
         $return = 0;
         $course_id = api_get_course_int_id();
 
@@ -1287,13 +1305,19 @@ class AnnouncementManager
             $updir = $sys_course_path.$courseDir;
 
             // Try to add an extension to the file if it hasn't one
-            $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']);
+            $new_file_name = add_ext_on_mime(
+                stripslashes($file['name']),
+                $file['type']
+            );
             // user's file name
             $file_name = $file ['name'];
 
             if (!filter_extension($new_file_name)) {
                 $return = -1;
-                echo Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error');
+                echo Display::return_message(
+                    get_lang('UplUnableToSaveFileFilteredExtension'),
+                    'error'
+                );
             } else {
                 $new_file_name = uniqid('');
                 $new_path = $updir.'/'.$new_file_name;
@@ -1302,7 +1326,7 @@ class AnnouncementManager
                 $safe_file_name = Database::escape_string($file_name);
                 $safe_new_file_name = Database::escape_string($new_file_name);
                 $id_attach = intval($id_attach);
-                $sql = "UPDATE $tbl_announcement_attachment SET 
+                $sql = "UPDATE $table SET 
                             filename = '$safe_file_name', 
                             comment = '$safe_file_comment', 
                             path = '$safe_new_file_name', 
@@ -1311,7 +1335,10 @@ class AnnouncementManager
                 $result = Database::query($sql);
                 if ($result === false) {
                     $return = -1;
-                    echo Display::return_message(get_lang('UplUnableToSaveFile'), 'error');
+                    echo Display::return_message(
+                        get_lang('UplUnableToSaveFile'),
+                        'error'
+                    );
                 } else {
                     $return = 1;
                 }
@@ -1444,7 +1471,9 @@ class AnnouncementManager
 
             //if (!empty($user_id)) {
             if (0) {
-                if (is_array($group_memberships) && count($group_memberships) > 0) {
+                if (is_array($group_memberships) &&
+                    count($group_memberships) > 0
+                ) {
                     $sql = "SELECT $select
                             FROM $tbl_announcement announcement 
                             INNER JOIN $tbl_item_property ip
@@ -1825,9 +1854,14 @@ class AnnouncementManager
         } else {
             // students only get to see the visible announcements
             if (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath') {
-                $group_memberships = GroupManager::get_group_ids($_course['real_id'], $userId);
+                $group_memberships = GroupManager::get_group_ids(
+                    $_course['real_id'],
+                    $userId
+                );
 
-                if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
+                if ((api_get_course_setting('allow_user_edit_announcement') &&
+                    !api_is_anonymous())
+                ) {
                     if (api_get_group_id() == 0) {
                         $cond_user_id = " AND (
                         ip.lastedit_user_id = '".$userId."' OR (
@@ -1881,7 +1915,9 @@ class AnnouncementManager
                     // the user is not member of any group
                     // this is an identified user => show the general announcements AND his personal announcements
                     if ($userId) {
-                        if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
+                        if ((api_get_course_setting('allow_user_edit_announcement') &&
+                            !api_is_anonymous())
+                        ) {
                             $cond_user_id = " AND (
                                 ip.lastedit_user_id = '".$userId."' OR
                                 ( ip.to_user_id='".$userId."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL)

+ 14 - 14
main/inc/lib/groupmanager.lib.php

@@ -390,6 +390,9 @@ class GroupManager
      */
     public static function delete_groups($groupInfo, $course_code = null)
     {
+        if (empty($groupInfo['iid'])) {
+            return false;
+        }
         $course_info = api_get_course_info($course_code);
         $course_id = $course_info['real_id'];
 
@@ -400,16 +403,16 @@ class GroupManager
         if ($groupInfo) {
             $groupIid = $groupInfo['iid'];
             $groupId = $groupInfo['id'];
-            $directory = $groupInfo['secret_directory'];
             // Unsubscribe all users
             self::unsubscribe_all_users($groupInfo);
             self::unsubscribe_all_tutors($groupInfo);
-            // move group-documents to garbage
-            $source_directory = api_get_path(SYS_COURSE_PATH).$course_info['path']."/document".$directory;
-            // File to renamed
-            $destination_dir = api_get_path(SYS_COURSE_PATH).$course_info['path']."/document".$directory.'_DELETED_'.$groupInfo['id'];
 
-            if (!empty($directory)) {
+            if (!empty($groupInfo['secret_directory'])) {
+                $directory = $groupInfo['secret_directory'];
+                // move group-documents to garbage
+                $source_directory = api_get_path(SYS_COURSE_PATH).$course_info['path']."/document".$directory;
+                // File to renamed
+                $destination_dir = api_get_path(SYS_COURSE_PATH).$course_info['path']."/document".$directory.'_DELETED_'.$groupInfo['id'];
                 //Deleting from document tool
                 DocumentManager::delete_document(
                     $course_info,
@@ -2431,7 +2434,6 @@ class GroupManager
                     $data['category_id'] = $categoryId;
                     $result['updated']['category'][] = $data;
                 } else {
-
                     // Add
                     $categoryId = self::create_category(
                         $data['category'],
@@ -2469,7 +2471,6 @@ class GroupManager
                 }
 
                 if (empty($groupInfo)) {
-
                     // Add
                     $groupId = self::create_group(
                         $data['group'],
@@ -2524,8 +2525,6 @@ class GroupManager
                     $groupInfo = self::get_group_properties($groupId);
                 }
 
-
-
                 $students = isset($data['students']) ? explode(',', $data['students']) : [];
                 if (!empty($students)) {
                     $studentUserIdList = array();
@@ -2536,8 +2535,7 @@ class GroupManager
                             continue;
                         }
 
-                        if (
-                            !CourseManager::is_user_subscribed_in_course(
+                        if (!CourseManager::is_user_subscribed_in_course(
                                 $userInfo['user_id'],
                                 $courseCode,
                                 !empty($sessionId),
@@ -2546,11 +2544,13 @@ class GroupManager
                         ) {
                             Display::addFlash(
                                 Display::return_message(
-                                    sprintf(get_lang('StudentXIsNotSubscribedToCourse'), $userInfo['complete_name']),
+                                    sprintf(
+                                        get_lang('StudentXIsNotSubscribedToCourse'),
+                                        $userInfo['complete_name']
+                                    ),
                                     'warning'
                                 )
                             );
-
                             continue;
                         }