Browse Source

Add student boss in user_edit.php and user_information see BT#11195

Julio 8 years ago
parent
commit
c312f5a1ad

+ 1 - 1
main/admin/dashboard_add_users_to_user.php

@@ -294,7 +294,7 @@ if (isset($_POST['formSent']) && intval($_POST['formSent']) == 1) {
             $affected_rows = UserManager::suscribe_users_to_hr_manager($user_id, $user_list);
             break;
         case STUDENT_BOSS:
-            $affected_rows = UserManager::subscribeUsersToBoss($user_id, $user_list);
+            $affected_rows = UserManager::subscribeBossToUsers($user_id, $user_list);
             break;
         default:
             $affected_rows = 0;

+ 23 - 0
main/admin/user_edit.php

@@ -345,6 +345,25 @@ if (!$user_data['platform_admin']) {
 	$form->addElement('radio', 'active', get_lang('ActiveAccount'), get_lang('Active'), 1);
 	$form->addElement('radio', 'active', '', get_lang('Inactive'), 0);
 }
+$studentBossList = UserManager::getStudentBossList($user_data['user_id']);
+
+$conditions = ['status' => STUDENT_BOSS];
+$studentBoss = UserManager::get_user_list($conditions);
+$studentBossToSelect = [];
+
+if ($studentBoss) {
+    foreach ($studentBoss as $bossId => $userData) {
+        $bossInfo = api_get_user_info($userData['user_id']);
+        $studentBossToSelect[$bossInfo['user_id']] =  $bossInfo['complete_name_with_username'];
+    }
+}
+
+if ($studentBossList) {
+    $studentBossList = array_column($studentBossList, 'boss_id');
+}
+
+$user_data['student_boss'] = array_values($studentBossList);
+$form->addElement('advmultiselect', 'student_boss', get_lang('StudentBoss'), $studentBossToSelect);
 
 // EXTRA FIELDS
 $extraField = new ExtraField('user');
@@ -461,6 +480,10 @@ if ($form->validate()) {
             $reset_password
         );
 
+        if (isset($user['student_boss'])) {
+            UserManager::subscribeUserToBossList($user_id, $user['student_boss']);
+        }
+
 		if (api_get_setting('openid_authentication') == 'true' && !empty($user['openid'])) {
 			$up = UserManager::update_openid($user_id, $user['openid']);
 		}

+ 51 - 27
main/admin/user_information.php

@@ -21,6 +21,7 @@ if (!isset($_GET['user_id'])) {
     api_not_allowed();
 }
 $user = api_get_user_info($_GET['user_id'], true);
+$userId = $user['user_id'];
 $tool_name = $user['complete_name'].(empty($user['official_code'])?'':' ('.$user['official_code'].')');
 $table_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
 $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
@@ -31,7 +32,7 @@ $editUser = null;
 if (api_is_platform_admin()) {
     $login_as_icon =
         '<a href="'.api_get_path(WEB_CODE_PATH).'admin/user_list.php'
-        .'?action=login_as&user_id='.$user['user_id'].'&'
+        .'?action=login_as&user_id='.$userId.'&'
         .'sec_token='.$_SESSION['sec_token'].'">'
         .Display::return_icon('login_as.png', get_lang('LoginAs'),
             array(), ICON_SIZE_MEDIUM).'</a>';
@@ -42,20 +43,20 @@ if (api_is_platform_admin()) {
             array(),
             ICON_SIZE_MEDIUM
         ),
-        api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$user['user_id']
+        api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$userId
     );
 
     $exportLink = Display::url(
         Display::return_icon(
             'export_csv.png', get_lang('ExportAsCSV'),'', ICON_SIZE_MEDIUM
         ),
-        api_get_self().'?user_id='.$user['user_id'].'&action=export'
+        api_get_self().'?user_id='.$userId.'&action=export'
     );
     $vCardExportLink = Display::url(
         Display::return_icon(
             'vcard.png', get_lang('UserInfo'),'', ICON_SIZE_MEDIUM
         ),
-        api_get_path(WEB_PATH).'main/social/vcard_export.php?userId='.$user['user_id']
+        api_get_path(WEB_PATH).'main/social/vcard_export.php?userId='.$userId
     );
     
 }
@@ -65,11 +66,10 @@ $creatorId = $user['creator_id'];
 $creatorInfo = api_get_user_info($creatorId);
 $registrationDate = $user['registration_date'];
 
-$csvContent = array();
-
 $table = new HTML_Table(array('class' => 'data_table'));
 $table->setHeaderContents(0, 0, get_lang('Information'));
-$csvContent[] = get_lang('Information');
+
+$csvContent[] = [get_lang('Information')];
 $data = array(
     get_lang('Name') => $user['complete_name'],
     get_lang('Email') => $user['email'],
@@ -104,10 +104,10 @@ $userInformation = $table->toHtml();
 
 $table = new HTML_Table(array('class' => 'data_table'));
 $table->setHeaderContents(0, 0, get_lang('Tracking'));
-$csvContent[] = get_lang('Tracking');
+$csvContent[] = [get_lang('Tracking')];
 $data = array(
-    get_lang('FirstLogin') => Tracking :: get_first_connection_date($user['user_id']),
-    get_lang('LatestLogin') => Tracking :: get_last_connection_date($user['user_id'], true)
+    get_lang('FirstLogin') => Tracking :: get_first_connection_date($userId),
+    get_lang('LatestLogin') => Tracking :: get_last_connection_date($userId, true)
 );
 $row = 1;
 foreach ($data as $label => $item) {
@@ -126,8 +126,7 @@ $tbl_session = Database:: get_main_table(TABLE_MAIN_SESSION);
 $tbl_course = Database:: get_main_table(TABLE_MAIN_COURSE);
 $tbl_user = Database:: get_main_table(TABLE_MAIN_USER);
 
-$user_id = $user['user_id'];
-$sessions = SessionManager::get_sessions_by_user($user_id, true);
+$sessions = SessionManager::get_sessions_by_user($userId, true);
 $personal_course_list = array();
 $courseToolInformationTotal = null;
 if (count($sessions) > 0) {
@@ -159,7 +158,7 @@ if (count($sessions) > 0) {
         foreach ($session_item['courses'] as $my_course) {
             $courseInfo = api_get_course_info_by_id($my_course['real_id']);
             $sessionStatus = SessionManager::get_user_status_in_session(
-                $user['user_id'],
+                $userId,
                 $courseInfo['real_id'],
                 $id_session
             );
@@ -179,20 +178,20 @@ if (count($sessions) > 0) {
                 Display::return_icon('course_home.gif', get_lang('CourseHomepage')).'</a>';
 
             if ($my_course['status'] == STUDENT) {
-                $tools .= '<a href="user_information.php?action=unsubscribeSessionCourse&course_code='.$courseInfo['code'].'&user_id='.$user['user_id'].'&id_session='.$id_session.'">'.
+                $tools .= '<a href="user_information.php?action=unsubscribeSessionCourse&course_code='.$courseInfo['code'].'&user_id='.$userId.'&id_session='.$id_session.'">'.
                     Display::return_icon('delete.png', get_lang('Delete')).'</a>';
             }
 
             $timeSpent = api_time_to_hms(
                 Tracking :: get_time_spent_on_the_course(
-                    $user['user_id'],
+                    $userId,
                     $courseInfo['real_id'],
                     $id_session
                 )
             );
 
             $totalForumMessages = CourseManager::getCountPostInForumPerUser(
-                $user['user_id'],
+                $userId,
                 $courseInfo['real_id'],
                 $id_session
             );
@@ -213,7 +212,7 @@ if (count($sessions) > 0) {
             $data[] = $row;
 
             $result = TrackingUserLogCSV::getToolInformation(
-                $user['user_id'],
+                $userId,
                 $courseInfo,
                 $id_session
             );
@@ -261,7 +260,7 @@ $courseToolInformationTotal = null;
  */
 $sql = 'SELECT * FROM '.$table_course_user.' cu, '.$table_course.' c
         WHERE
-            cu.user_id = '.$user['user_id'].' AND
+            cu.user_id = '.$userId.' AND
             cu.c_id = c.id AND
             cu.relation_type <> '.COURSE_RELATION_TYPE_RRHH.' ';
 $res = Database::query($sql);
@@ -297,20 +296,20 @@ if (Database::num_rows($res) > 0) {
             '<a href="course_edit.php?id='.$course->c_id.'">'.
             Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
         if ($course->status == STUDENT) {
-            $tools .= '<a href="user_information.php?action=unsubscribe&course_code='.$courseCode.'&user_id='.$user['user_id'].'">'.
+            $tools .= '<a href="user_information.php?action=unsubscribe&course_code='.$courseCode.'&user_id='.$userId.'">'.
                 Display::return_icon('delete.png', get_lang('Delete')).'</a>';
         }
 
         $timeSpent = api_time_to_hms(
             Tracking :: get_time_spent_on_the_course(
-                $user['user_id'],
+                $userId,
                 $courseInfo['real_id'],
                 0
             )
         );
 
         $totalForumMessages = CourseManager::getCountPostInForumPerUser(
-            $user['user_id'],
+            $userId,
             $course->id,
             0
         );
@@ -328,7 +327,7 @@ if (Database::num_rows($res) > 0) {
         $data[] = $row;
 
         $result = TrackingUserLogCSV::getToolInformation(
-            $user['user_id'],
+            $userId,
             $courseInfo,
             0
         );
@@ -352,9 +351,9 @@ if (Database::num_rows($res) > 0) {
 /**
  * Show the URL in which this user is subscribed
  */
-$urlInformation = null;
+$urlInformation = '';
 if (api_is_multiple_url_enabled()) {
-    $urlList = UrlManager::get_access_url_from_user($user['user_id']);
+    $urlList = UrlManager::get_access_url_from_user($userId);
     if (count($urlList) > 0) {
         $header = array();
         $header[] = array('URL', true);
@@ -381,6 +380,25 @@ if (api_is_multiple_url_enabled()) {
         $urlInformation = '<p>'.get_lang('NoUrlForThisUser').'</p>';
     }
 }
+
+
+$studentBossList = UserManager::getStudentBossList($userId);
+$studentBossListToString = '';
+if ($studentBossList) {
+    $table = new HTML_Table(array('class' => 'data_table'));
+    $table->setHeaderContents(0, 0, get_lang('User'));
+    $csvContent[] = [get_lang('StudentBoss')];
+
+    $row = 1;
+    foreach ($studentBossList as $studentBossId) {
+        $studentBoss = api_get_user_info($studentBossId);
+        $table->setCellContents($row, 0, $studentBoss['complete_name_with_username']);
+        $csvContent[] = array($studentBoss['complete_name_with_username']);
+        $row++;
+    }
+    $studentBossListToString = $table->toHtml();
+}
+
 $message = null;
 
 if (isset($_GET['action'])) {
@@ -425,14 +443,13 @@ echo '<div class="actions">
 
 echo Display::page_header($tool_name);
 
-
 $fullUrlBig = UserManager::getUserPicture(
-    $user['user_id'],
+    $userId,
     USER_IMAGE_SIZE_BIG
 );
 
 $fullUrl = UserManager::getUserPicture(
-    $user['user_id'],
+    $userId,
     USER_IMAGE_SIZE_ORIGINAL
 );
 
@@ -453,8 +470,15 @@ echo $trackingInformation;
 echo '</div>';
 echo '</div>';
 
+if ($studentBossList) {
+    echo Display::page_subheader(get_lang('StudentBossList'));
+    echo $studentBossListToString;
+}
+
 echo Display::page_subheader(get_lang('SessionList'));
 echo $sessionInformation;
+
+echo Display::page_subheader(get_lang('CourseList'));
 echo $courseInformation;
 echo $urlInformation;
 

+ 1 - 0
main/inc/lib/export.lib.inc.php

@@ -41,6 +41,7 @@ class Export
     public static function arrayToCsv($data, $filename = 'export')
     {
         if (empty($data)) {
+            
             return false;
         }
 

+ 99 - 23
main/inc/lib/usermanager.lib.php

@@ -4128,7 +4128,7 @@ class UserManager
      * @param array $subscribedUsersId The id of suscribed users
      * @param action $relationType The relation type
      */
-    public static function subscribeUsersToUser($userId, $subscribedUsersId, $relationType)
+    public static function subscribeUsersToUser($userId, $subscribedUsersId, $relationType, $deleteUsersBeforeInsert = false)
     {
         $userRelUserTable = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
         $userRelAccessUrlTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
@@ -4138,40 +4138,48 @@ class UserManager
         $affectedRows = 0;
 
         if (api_get_multiple_access_url()) {
-            //Deleting assigned users to hrm_id
-            $sql = "SELECT s.user_id FROM $userRelUserTable s "
-                . "INNER JOIN $userRelAccessUrlTable a ON (a.user_id = s.user_id) "
-                . "WHERE friend_user_id = $userId "
-                . "AND relation_type = $relationType "
-                . "AND access_url_id = " . api_get_current_access_url_id() . "";
+            // Deleting assigned users to hrm_id
+            $sql = "SELECT s.user_id FROM $userRelUserTable s 
+                    INNER JOIN $userRelAccessUrlTable a ON (a.user_id = s.user_id) 
+                    WHERE 
+                        friend_user_id = $userId AND 
+                        relation_type = $relationType AND 
+                        access_url_id = " . api_get_current_access_url_id();
         } else {
-            $sql = "SELECT user_id FROM $userRelUserTable "
-                . "WHERE friend_user_id = $userId "
-                . "AND relation_type = $relationType";
+            $sql = "SELECT user_id FROM $userRelUserTable 
+                    WHERE friend_user_id = $userId 
+                    AND relation_type = $relationType";
         }
         $result = Database::query($sql);
 
         if (Database::num_rows($result) > 0) {
             while ($row = Database::fetch_array($result)) {
-                $sql = "DELETE FROM $userRelUserTable "
-                    . "WHERE user_id = {$row['user_id']} "
-                    . "AND friend_user_id = $userId "
-                    . "AND relation_type = $relationType";
-
+                $sql = "DELETE FROM $userRelUserTable 
+                        WHERE 
+                          user_id = {$row['user_id']} AND 
+                          friend_user_id = $userId AND 
+                          relation_type = $relationType";
                 Database::query($sql);
             }
         }
 
+        if ($deleteUsersBeforeInsert) {
+            $sql = "DELETE FROM $userRelUserTable 
+                    WHERE 
+                        user_id = $userId AND
+                        relation_type = $relationType";
+            Database::query($sql);
+        }
+
         // Inserting new user list
         if (is_array($subscribedUsersId)) {
             foreach ($subscribedUsersId as $subscribedUserId) {
                 $subscribedUserId = intval($subscribedUserId);
 
-                $sql = "INSERT IGNORE INTO $userRelUserTable(user_id, friend_user_id, relation_type) "
-                    . "VALUES ($subscribedUserId, $userId, $relationType)";
+                $sql = "INSERT IGNORE INTO $userRelUserTable (user_id, friend_user_id, relation_type)
+                        VALUES ($subscribedUserId, $userId, $relationType)";
 
                 $result = Database::query($sql);
-
                 $affectedRows = Database::affected_rows($result);
             }
         }
@@ -4808,16 +4816,44 @@ EOF;
     }
 
     /**
-     * Subscribe users to student boss
+     * Subscribe boss to students
+     * 
      * @param int $bossId The boss id
      * @param array $usersId The users array
      * @return int Affected rows
      */
-    public static function subscribeUsersToBoss($bossId, $usersId)
+    public static function subscribeBossToUsers($bossId, $usersId)
     {
         return self::subscribeUsersToUser($bossId, $usersId, USER_RELATION_TYPE_BOSS);
     }
 
+    /**
+     * Subscribe boss to students
+     *
+     * @param int $studentId
+     * @param array $bossList
+     * @return int Affected rows
+     */
+    public static function subscribeUserToBossList($studentId, $bossList)
+    {
+        $count = 1;
+        if ($bossList) {
+            $studentId = (int) $studentId;
+            $userRelUserTable = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
+            $userRelAccessUrlTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
+            $sql = "DELETE FROM $userRelUserTable 
+                    WHERE user_id = $studentId AND relation_type = ".USER_RELATION_TYPE_BOSS;
+            Database::query($sql);
+
+            foreach ($bossList as $bossId) {
+                $sql = "INSERT IGNORE INTO $userRelUserTable (user_id, friend_user_id, relation_type)
+                        VALUES ($studentId, $bossId, ".USER_RELATION_TYPE_BOSS.")";
+
+                Database::query($sql);
+            }
+        }
+    }
+
     /**
      * Get users followed by student boss
      * @param int $userId
@@ -4847,8 +4883,18 @@ EOF;
         $lastConnectionDate = null
     ){
         return self::getUsersFollowedByUser(
-                $userId, $userStatus, $getOnlyUserId, $getSql, $getCount, $from, $numberItems, $column, $direction,
-                $active, $lastConnectionDate, STUDENT_BOSS
+            $userId,
+            $userStatus,
+            $getOnlyUserId,
+            $getSql,
+            $getCount,
+            $from,
+            $numberItems,
+            $column,
+            $direction,
+            $active,
+            $lastConnectionDate,
+            STUDENT_BOSS
         );
     }
 
@@ -4962,7 +5008,7 @@ EOF;
      * @param $userId
      * @return bool
      */
-    public static function getStudentBoss($userId)
+    public static function getFirstStudentBoss($userId)
     {
         $userId = intval($userId);
         if ($userId > 0) {
@@ -4988,6 +5034,36 @@ EOF;
         return false;
     }
 
+    /**
+     * Get the boss user ID from a followed user id
+     * @param $userId
+     * @return bool
+     */
+    public static function getStudentBossList($userId)
+    {
+        $userId = intval($userId);
+        if ($userId > 0) {
+            $userRelTable = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
+            $result = Database::select(
+                'DISTINCT friend_user_id AS boss_id',
+                $userRelTable,
+                array(
+                    'where' => array(
+                        'user_id = ? AND relation_type = ? ' => array(
+                            $userId,
+                            USER_RELATION_TYPE_BOSS,
+                        )
+                    )
+                ),
+                'all'
+            );
+
+            return $result;
+        }
+
+        return false;
+    }
+
     /**
      * Get either a Gravatar URL or complete image tag for a specified email address.
      *

+ 2 - 2
plugin/advanced_subscription/ajax/advanced_subscription.ajax.php

@@ -109,7 +109,7 @@ if ($verified) {
                 $studentArray['picture'] = $studentArray['avatar'];
 
                 // Get superior data if exist
-                $superiorId = UserManager::getStudentBoss($data['studentUserId']);
+                $superiorId = UserManager::getFirstStudentBoss($data['studentUserId']);
                 if (!empty($superiorId)) {
                     $superiorArray = api_get_user_info($superiorId);
                 } else {
@@ -265,7 +265,7 @@ if ($verified) {
                     $studentArray = api_get_user_info($data['studentUserId']);
                     $studentArray['picture'] = $studentArray['avatar'];
                     // Prepare superior data
-                    $superiorId = UserManager::getStudentBoss($data['studentUserId']);
+                    $superiorId = UserManager::getFirstStudentBoss($data['studentUserId']);
                     if (!empty($superiorId)) {
                         $superiorArray = api_get_user_info($superiorId);
                     } else {

+ 1 - 1
plugin/advanced_subscription/test/mails.php

@@ -67,7 +67,7 @@ $studentArray = api_get_user_info($data['studentUserId']);
 $studentArray['picture'] = $studentArray['avatar'];
 
 // Get superior data if exist
-$superiorId = UserManager::getStudentBoss($data['studentUserId']);
+$superiorId = UserManager::getFirstStudentBoss($data['studentUserId']);
 if (!empty($superiorId)) {
     $superiorArray = api_get_user_info($superiorId);
 } else {