Browse Source

Adding LOG_USER_UPDATED, LOG_USER_ACTIVATED and LOG_USER_DEACTIVATED see BT#4871

Julio Montoya 12 years ago
parent
commit
a6f879d57a
3 changed files with 115 additions and 93 deletions
  1. 5 2
      main/inc/lib/events.lib.inc.php
  2. 61 54
      main/inc/lib/main_api.lib.php
  3. 49 37
      main/inc/lib/usermanager.lib.php

+ 5 - 2
main/inc/lib/events.lib.inc.php

@@ -566,16 +566,19 @@ function event_system($event_type, $event_value_type, $event_value, $datetime =
     if ($event_value_type == LOG_USER_OBJECT) {
         if (is_array($event_value)) {
             unset($event_value['complete_name']);
+            unset($event_value['complete_name_with_username']);
             unset($event_value['firstName']);
             unset($event_value['lastName']);
             unset($event_value['avatar_small']);
+            unset($event_value['avatar_sys_path']);
             unset($event_value['avatar']);
+            unset($event_value['mail']);
             unset($event_value['password']);
-            unset($event_value['lastLogin']);
+            unset($event_value['lastLogin']);            
             unset($event_value['picture_uri']);
             $event_value = serialize($event_value);
         }
-    }
+    } 
 
     $event_value        = Database::escape_string($event_value);
     $course_info        = api_get_course_info($course_code);

+ 61 - 54
main/inc/lib/main_api.lib.php

@@ -137,8 +137,13 @@ define('DIR_HOTPOTATOES','/HotPotatoes_files');
 // event logs types
 define('LOG_COURSE_DELETE',                     'course_deleted');
 define('LOG_COURSE_CREATE',                     'course_created');
-define('LOG_USER_DELETE',                       'user_deleted');
 define('LOG_USER_CREATE',                       'user_created');
+define('LOG_USER_UPDATED',                      'user_updated');
+define('LOG_USER_DELETE',                       'user_deleted');
+define('LOG_USER_ACTIVATED',                    'user_activated');
+define('LOG_USER_DEACTIVATED',                  'user_deactivated');
+
+
 define('LOG_SESSION_CREATE',                    'session_created');
 define('LOG_SESSION_DELETE',                    'session_deleted');
 define('LOG_SESSION_CATEGORY_CREATE',           'session_category_created');
@@ -978,6 +983,61 @@ function api_get_user_courses($userid, $fetch_session = true) {
     return $courses;
 }
 
+
+/**
+ * Finds all the information about a user. If no paramater is passed you find all the information about the current user.
+ * @param $user_id (integer): the id of the user
+ * @return $user_info (array): user_id, lastname, firstname, username, email, ...
+ * @author Patrick Cool <patrick.cool@UGent.be>
+ * @version 21 September 2004
+ */
+function api_get_user_info($user_id = '', $check_if_user_is_online = false, $show_password = false) {
+    if ($user_id == '') {
+        return _api_format_user($GLOBALS['_user']);
+    }
+    $sql = "SELECT * FROM ".Database :: get_main_table(TABLE_MAIN_USER)." WHERE user_id='".Database::escape_string($user_id)."'";
+    $result = Database::query($sql);
+    if (Database::num_rows($result) > 0) {
+        $result_array = Database::fetch_array($result);
+		if ($check_if_user_is_online) {
+            $use_status_in_platform = user_is_online($user_id);
+
+			$result_array['user_is_online'] = $use_status_in_platform;
+            $user_online_in_chat = 0;
+
+            if ($use_status_in_platform) {
+                $user_status = UserManager::get_extra_user_data_by_field($user_id, 'user_chat_status', false, true);
+                if (intval($user_status['user_chat_status']) == 1) {
+                    $user_online_in_chat = 1;
+                }
+            }
+            $result_array['user_is_online_in_chat'] = $user_online_in_chat;
+		}
+        $user =  _api_format_user($result_array, $show_password);
+        return $user;
+    }
+    return false;
+}
+
+/**
+ * Finds all the information about a user from username instead of user id
+ * @param $username (string): the username
+ * @return $user_info (array): user_id, lastname, firstname, username, email, ...
+ * @author Yannick Warnier <yannick.warnier@beeznest.com>
+ */
+function api_get_user_info_from_username($username = '') {
+    if (empty($username)) { return false; }
+    $sql = "SELECT * FROM ".Database :: get_main_table(TABLE_MAIN_USER)." WHERE username='".Database::escape_string($username)."'";
+    $result = Database::query($sql);
+    if (Database::num_rows($result) > 0) {
+        $result_array = Database::fetch_array($result);
+        return _api_format_user($result_array);
+    }
+    return false;
+}
+
+
+
 /**
  * Formats user information into a standard array
  *
@@ -1079,62 +1139,9 @@ function _api_format_user($user, $add_password = false) {
     if ($add_password) {
         $result['password'] = $user['password'];
     }
-
     return $result;
 }
 
-/**
- * Finds all the information about a user. If no paramater is passed you find all the information about the current user.
- * @param $user_id (integer): the id of the user
- * @return $user_info (array): user_id, lastname, firstname, username, email, ...
- * @author Patrick Cool <patrick.cool@UGent.be>
- * @version 21 September 2004
- */
-function api_get_user_info($user_id = '', $check_if_user_is_online = false, $show_password = false) {
-    if ($user_id == '') {
-        return _api_format_user($GLOBALS['_user']);
-    }
-    $sql = "SELECT * FROM ".Database :: get_main_table(TABLE_MAIN_USER)." WHERE user_id='".Database::escape_string($user_id)."'";
-    $result = Database::query($sql);
-    if (Database::num_rows($result) > 0) {
-        $result_array = Database::fetch_array($result);
-		if ($check_if_user_is_online) {
-            $use_status_in_platform = user_is_online($user_id);
-
-			$result_array['user_is_online'] = $use_status_in_platform;
-            $user_online_in_chat = 0;
-
-            if ($use_status_in_platform) {
-                $user_status = UserManager::get_extra_user_data_by_field($user_id, 'user_chat_status', false, true);
-                if (intval($user_status['user_chat_status']) == 1) {
-                    $user_online_in_chat = 1;
-                }
-            }
-            $result_array['user_is_online_in_chat'] = $user_online_in_chat;
-		}
-        $user =  _api_format_user($result_array, $show_password);
-        return $user;
-    }
-    return false;
-}
-
-/**
- * Finds all the information about a user from username instead of user id
- * @param $username (string): the username
- * @return $user_info (array): user_id, lastname, firstname, username, email, ...
- * @author Yannick Warnier <yannick.warnier@beeznest.com>
- */
-function api_get_user_info_from_username($username = '') {
-    if (empty($username)) { return false; }
-    $sql = "SELECT * FROM ".Database :: get_main_table(TABLE_MAIN_USER)." WHERE username='".Database::escape_string($username)."'";
-    $result = Database::query($sql);
-    if (Database::num_rows($result) > 0) {
-        $result_array = Database::fetch_array($result);
-        return _api_format_user($result_array);
-    }
-    return false;
-}
-
 /**
  * @TODO This function should be the real id (integer)
  * Returns the current course code (string)

+ 49 - 37
main/inc/lib/usermanager.lib.php

@@ -185,13 +185,12 @@ class UserManager {
                     @api_mail_html($recipient_name, $email, $emailsubject, $emailbody, $sender_name, $email_admin);  
                 }
                 /* ENDS MANAGE EVENT WITH MAIL */              
-			}
-          
-
+			}            
 			// Add event to system log			
 			$user_id_manager = api_get_user_id();
 			$user_info = api_get_user_info($return);
-			event_system(LOG_USER_CREATE, LOG_USER_ID, $return, api_get_utc_datetime(), $user_id_manager, null, $user_info);
+			event_system(LOG_USER_CREATE, LOG_USER_ID, $return, api_get_utc_datetime(), $user_id_manager);
+            event_system(LOG_USER_CREATE, LOG_USER_OBJECT, $user_info, api_get_utc_datetime(), $user_id_manager);
 
 		} else {
 			//echo "false - failed" ;
@@ -294,6 +293,7 @@ class UserManager {
 		// Delete user picture
 		// TODO: Logic about api_get_setting('split_users_upload_directory') === 'true' , a user has 4 differnt sized photos to be deleted.
 		$user_info = api_get_user_info($user_id);
+        
 		if (strlen($user_info['picture_uri']) > 0) {
 			$img_path = api_get_path(SYS_CODE_PATH).'upload/users/'.$user_id.'/'.$user_info['picture_uri'];
 			if (file_exists($img_path))
@@ -352,8 +352,8 @@ class UserManager {
 		}
 		// Add event to system log		
 		$user_id_manager = api_get_user_id();
-		event_system(LOG_USER_DELETE, LOG_USER_ID, $user_id, api_get_utc_datetime(), $user_id_manager, null, $user_info);
-        	event_system(LOG_USER_DELETE, LOG_USER_OBJECT, implode(';',$user_info), api_get_utc_datetime(), $user_id_manager, null, $user_info);
+		event_system(LOG_USER_DELETE, LOG_USER_ID, $user_id, api_get_utc_datetime(), $user_id_manager);
+        event_system(LOG_USER_DELETE, LOG_USER_OBJECT, $user_info, api_get_utc_datetime(), $user_id_manager);
 		return true;
 	}
     
@@ -367,13 +367,11 @@ class UserManager {
      * @return boolean  True if at least one user was successfuly deleted. False otherwise.
      * @author Laurent Opprecht
      */
-    static function delete_users($ids = array())
-    {
+    static function delete_users($ids = array()) {
         $result = false;
         $ids = is_array($ids) ? $ids : func_get_args();        
         $ids = array_map('intval', $ids);
-        foreach($ids as $id)
-        {
+        foreach ($ids as $id) {
             $deleted = self::delete_user($id);
             $result = $deleted || $result;
         }
@@ -393,14 +391,14 @@ class UserManager {
     static function deactivate_users($ids = array()) {
         if (empty($ids)) {
             return false;
-        }
-        $table_user = Database :: get_main_table(TABLE_MAIN_USER);
+        }        
         $ids = is_array($ids) ? $ids : func_get_args();
-        $ids = array_map('intval', $ids);
-        $ids = implode(',', $ids);
-        
-        $sql = "UPDATE $table_user SET active = 0 WHERE user_id IN ($ids)";
-        return Database::query($sql);
+        if (!empty($ids)) {
+            foreach ($ids as $id) {
+                self::change_active_state($id, 0);
+            }        
+        }
+        return true;
     }
 
     /**
@@ -413,20 +411,18 @@ class UserManager {
      * @return boolean 
      * @author Laurent Opprecht
      */
-    static function activate_users($ids = array())
-    {
+    static function activate_users($ids = array()) {
         if (empty($ids)) {
             return false;
         }
-
         $table_user = Database :: get_main_table(TABLE_MAIN_USER);
 
         $ids = is_array($ids) ? $ids : func_get_args();
-        $ids = array_map('intval', $ids);
-        $ids = implode(',', $ids);
-        
-        $sql = "UPDATE $table_user SET active = 1 WHERE user_id IN ($ids)";
-        return Database::query($sql);
+        if (!empty($ids)) {
+            foreach ($ids as $id) {
+                self::change_active_state($id, 1);
+            }        
+        }
     }
     
 	/**
@@ -439,8 +435,7 @@ class UserManager {
 		$table_user = Database :: get_main_table(TABLE_MAIN_USER);
 		if ($user_id != strval(intval($user_id))) return false;
 		if ($user_id === false) return false;
-		$sql = "UPDATE $table_user SET
-				openid='".Database::escape_string($openid)."'";
+		$sql = "UPDATE $table_user SET openid='".Database::escape_string($openid)."'";
 		$sql .=	" WHERE user_id='$user_id'";
 		return Database::query($sql);
 	}
@@ -525,13 +520,12 @@ class UserManager {
 				official_code='".Database::escape_string($official_code)."',
 				phone='".Database::escape_string($phone)."',
 				picture_uri='".Database::escape_string($picture_uri)."',
-				expiration_date='".Database::escape_string($expiration_date)."',
-				active='".Database::escape_string($active)."',
+				expiration_date='".Database::escape_string($expiration_date)."',				
 				hr_dept_id=".intval($hr_dept_id);
 		if (!is_null($creator_id)) {
 			$sql .= ", creator_id='".Database::escape_string($creator_id)."'";
 		}
-		$sql .=	" WHERE user_id='$user_id'";
+		$sql .=	" WHERE user_id = '$user_id' ";
 		$return = Database::query($sql);
 		if (is_array($extra) && count($extra) > 0) {
 			$res = true;
@@ -540,6 +534,11 @@ class UserManager {
 			}
 		}
         
+        if ($user_info['active'] != $active) {
+            self::change_active_state($user_id, $active);
+        }
+        //active='".Database::escape_string($active)."',
+        
         if (!empty($email) && $send_email) {
 			$recipient_name = api_get_person_name($firstname, $lastname, null, PERSON_NAME_EMAIL_ADDRESS);
 			$emailsubject = '['.api_get_setting('siteName').'] '.get_lang('YourReg').' '.api_get_setting('siteName');
@@ -558,6 +557,9 @@ class UserManager {
 			@api_mail_html($recipient_name, $email, $emailsubject, $emailbody, $sender_name, $email_admin);
         }
         
+        $user_info = api_get_user_info($user_id);
+        event_system(LOG_USER_UPDATED, LOG_USER_ID, $user_id, api_get_utc_datetime(), api_get_user_id());
+        event_system(LOG_USER_UPDATED, LOG_USER_OBJECT, $user_info, api_get_utc_datetime(), api_get_user_id());
 		return $return;
 	}
 
@@ -567,11 +569,21 @@ class UserManager {
 	 * @param int user_id
 	 * @param int Enable or disable
 	 */
-	private static function change_active_state($user_id, $active) {
-		$user_id = (int)$user_id;
+	private static function change_active_state($user_id, $active) {		
+        $user_id = intval($user_id);
+        $active = intval($active);
 		$table_user = Database :: get_main_table(TABLE_MAIN_USER);
+        
 		$sql = "UPDATE $table_user SET active = '$active' WHERE user_id = '$user_id';";
-		Database::query($sql);
+        Database::query($sql);
+        
+        $log_event = LOG_USER_DEACTIVATED;
+        if ($active == 1) {
+            $log_event = LOG_USER_ACTIVATED;    
+        }
+        $user_info = api_get_user_info($user_id);
+        event_system($log_event, LOG_USER_ID, $user_id, api_get_utc_datetime(), api_get_user_id());
+        event_system($log_event, LOG_USER_OBJECT, $user_info, api_get_utc_datetime(), api_get_user_id());		
 	}
 
 	/**
@@ -644,10 +656,10 @@ class UserManager {
 		$firstname = api_substr(preg_replace(USERNAME_PURIFIER, '', api_transliterate($firstname, '', $encoding)), 0, 1); // The first letter only.
 		
 		//Looking for a space in the lastname
-                $pos = api_strpos($lastname, ' ');
-                    if ($pos !== false ) {
-                        $lastname = api_substr($lastname, 0, $pos);
-                } 
+        $pos = api_strpos($lastname, ' ');
+            if ($pos !== false ) {
+                $lastname = api_substr($lastname, 0, $pos);
+        }
 
 		$lastname = preg_replace(USERNAME_PURIFIER, '', api_transliterate($lastname, '', $encoding));
 		//$username = api_is_western_name_order(null, $language) ? $firstname.$lastname : $lastname.$firstname;