diff --git a/main/inc/lib/usermanager.lib.php b/main/inc/lib/usermanager.lib.php index 7b0bf2aa49..3672c5e4f9 100644 --- a/main/inc/lib/usermanager.lib.php +++ b/main/inc/lib/usermanager.lib.php @@ -28,9 +28,11 @@ define('USER_FIELD_TYPE_DOUBLE_SELECT', 8); define('USER_FIELD_TYPE_DIVIDER', 9); class UserManager { + private function __construct () { } + /** * Creates a new user for the platform * @author Hugues Peeters , @@ -54,68 +56,66 @@ class UserManager { * @return mixed new user id - if the new user creation succeeds, false otherwise * * @desc The function tries to retrieve $_user['user_id'] from the global space. - * if it exists, $_user['user_id'] is the creator id If a problem arises, + * if it exists, $_user['user_id'] is the creator id. If a problem arises, * it stores the error message in global $api_failureList */ - public static function create_user($firstName, $lastName, $status, $email, $loginName, $password, $official_code = '', $language='', $phone = '', $picture_uri = '', $auth_source = PLATFORM_AUTH_SOURCE, $expiration_date = '0000-00-00 00:00:00', $active = 1, $hr_dept_id=0, $extra=null) { + public static function create_user($firstName, $lastName, $status, $email, $loginName, $password, $official_code = '', $language='', $phone = '', $picture_uri = '', $auth_source = PLATFORM_AUTH_SOURCE, $expiration_date = '0000-00-00 00:00:00', $active = 1, $hr_dept_id = 0, $extra = null) { global $_user, $userPasswordCrypted; - $firstName=Security::remove_XSS($firstName); - $lastName=Security::remove_XSS($lastName); - $loginName=Security::remove_XSS($loginName); - $phone=Security::remove_XSS($phone); + $firstName = Security::remove_XSS($firstName); + $lastName = Security::remove_XSS($lastName); + $loginName = Security::remove_XSS($loginName); + $phone = Security::remove_XSS($phone); // database table definition $table_user = Database::get_main_table(TABLE_MAIN_USER); // default langauge - if ($language=='') - { + if ($language == '') { $language = api_get_setting('platformLanguage'); } - if ($_user['user_id']) - { + if ($_user['user_id']) { $creator_id = $_user['user_id']; - } - else - { + } else { $creator_id = ''; } // First check wether the login already exists - if (! self::is_username_available($loginName)) + if (!self::is_username_available($loginName)) { return api_set_failure('login-pass already taken'); + } //$password = "PLACEHOLDER"; $password = api_get_encrypted_password($password); //$password = ($userPasswordCrypted ? md5($password) : $password); - $current_date=date('Y-m-d H:i:s',time()); + $current_date = date('Y-m-d H:i:s', time()); $sql = "INSERT INTO $table_user - SET lastname = '".Database::escape_string(trim($lastName))."', - firstname = '".Database::escape_string(trim($firstName))."', - username = '".Database::escape_string(trim($loginName))."', - status = '".Database::escape_string($status)."', - password = '".Database::escape_string($password)."', - email = '".Database::escape_string($email)."', - official_code = '".Database::escape_string($official_code)."', - picture_uri = '".Database::escape_string($picture_uri)."', - creator_id = '".Database::escape_string($creator_id)."', - auth_source = '".Database::escape_string($auth_source)."', - phone = '".Database::escape_string($phone)."', - language = '".Database::escape_string($language)."', - registration_date = '".$current_date."', - expiration_date = '".Database::escape_string($expiration_date)."', - hr_dept_id = '".Database::escape_string($hr_dept_id)."', - active = '".Database::escape_string($active)."'"; - $result = api_sql_query($sql, __FILE__, __LINE__); + SET lastname = '".Database::escape_string(trim($lastName))."', + firstname = '".Database::escape_string(trim($firstName))."', + username = '".Database::escape_string(trim($loginName))."', + status = '".Database::escape_string($status)."', + password = '".Database::escape_string($password)."', + email = '".Database::escape_string($email)."', + official_code = '".Database::escape_string($official_code)."', + picture_uri = '".Database::escape_string($picture_uri)."', + creator_id = '".Database::escape_string($creator_id)."', + auth_source = '".Database::escape_string($auth_source)."', + phone = '".Database::escape_string($phone)."', + language = '".Database::escape_string($language)."', + registration_date = '".$current_date."', + expiration_date = '".Database::escape_string($expiration_date)."', + hr_dept_id = '".Database::escape_string($hr_dept_id)."', + active = '".Database::escape_string($active)."'"; + $result = Database::query($sql, __FILE__, __LINE__); if ($result) { //echo "id returned"; - $return=Database::get_last_insert_id(); + $return = Database::get_last_insert_id(); global $_configuration; - require_once (api_get_path(LIBRARY_PATH).'urlmanager.lib.php'); - if ($_configuration['multiple_access_urls']==true) { - if (api_get_current_access_url_id()!=-1) + require_once api_get_path(LIBRARY_PATH).'urlmanager.lib.php'; + if ($_configuration['multiple_access_urls'] == true) { + if (api_get_current_access_url_id() != -1) { UrlManager::add_user_to_url($return, api_get_current_access_url_id()); - else + } else { UrlManager::add_user_to_url($return, 1); + } } else { //we are adding by default the access_url_user table with access_url_id = 1 UrlManager::add_user_to_url($return, 1); @@ -131,10 +131,10 @@ class UserManager { $return=false; } - if(is_array($extra) AND count($extra)>0) { + if (is_array($extra) && count($extra) > 0) { $res = true; foreach($extra as $fname => $fvalue) { - $res = $res && self::update_extra_field($return,$fname,$fvalue); + $res = $res && self::update_extra_field($return, $fname, $fvalue); } } return $return; @@ -148,16 +148,14 @@ class UserManager { * @param int $user_id The user id * @return boolean true if user can be deleted */ - public static function can_delete_user ($user_id) { + public static function can_delete_user($user_id) { $table_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER); $sql = "SELECT * FROM $table_course_user WHERE status = '1' AND user_id = '".$user_id."'"; - $res = api_sql_query($sql,__FILE__,__LINE__); - while ($course = Database::fetch_object($res)) - { + $res = Database::query($sql, __FILE__, __LINE__); + while ($course = Database::fetch_object($res)) { $sql = "SELECT user_id FROM $table_course_user WHERE status='1' AND course_code ='".$course->course_code."'"; - $res2 = api_sql_query($sql,__FILE__,__LINE__); - if (Database::num_rows($res2) == 1) - { + $res2 = Database::query($sql, __FILE__, __LINE__); + if (Database::num_rows($res2) == 1) { return false; } } @@ -169,10 +167,9 @@ class UserManager { * @param int $user_id The user id * @return boolean true if user is succesfully deleted, false otherwise */ - public static function delete_user ($user_id) { + public static function delete_user($user_id) { global $_configuration; - if (!self :: can_delete_user($user_id)) - { + if (!self::can_delete_user($user_id)) { return false; } $table_user = Database :: get_main_table(TABLE_MAIN_USER); @@ -185,34 +182,32 @@ class UserManager { // Unsubscribe the user from all groups in all his courses $sql = "SELECT * FROM $table_course c, $table_course_user cu WHERE cu.user_id = '".$user_id."' AND c.code = cu.course_code"; - $res = api_sql_query($sql,__FILE__,__LINE__); - while ($course = Database::fetch_object($res)) - { + $res = Database::query($sql, __FILE__, __LINE__); + while ($course = Database::fetch_object($res)) { $table_group = Database :: get_course_table(TABLE_GROUP_USER, $course->db_name); $sql = "DELETE FROM $table_group WHERE user_id = '".$user_id."'"; - api_sql_query($sql,__FILE__,__LINE__); + Database::query($sql, __FILE__, __LINE__); } // Unsubscribe user from all classes $sql = "DELETE FROM $table_class_user WHERE user_id = '".$user_id."'"; - api_sql_query($sql,__FILE__,__LINE__); + Database::query($sql, __FILE__, __LINE__); // Unsubscribe user from all courses $sql = "DELETE FROM $table_course_user WHERE user_id = '".$user_id."'"; - api_sql_query($sql,__FILE__,__LINE__); + Database::query($sql, __FILE__, __LINE__); // Unsubscribe user from all courses in sessions $sql = "DELETE FROM $table_session_course_user WHERE id_user = '".$user_id."'"; - api_sql_query($sql,__FILE__,__LINE__); + Database::query($sql, __FILE__, __LINE__); // Unsubscribe user from all sessions $sql = "DELETE FROM $table_session_user WHERE id_user = '".$user_id."'"; - api_sql_query($sql,__FILE__,__LINE__); + Database::query($sql, __FILE__, __LINE__); // Delete user picture $user_info = api_get_user_info($user_id); - if(strlen($user_info['picture_uri']) > 0) - { + if (strlen($user_info['picture_uri']) > 0) { $img_path = api_get_path(SYS_CODE_PATH).'upload/users/'.$user_id.'/'.$user_info['picture_uri']; unlink($img_path); } @@ -220,33 +215,33 @@ class UserManager { // Delete the personal course categories $course_cat_table = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY); $sql = "DELETE FROM $course_cat_table WHERE user_id = '".$user_id."'"; - api_sql_query($sql,__FILE__,__LINE__); + Database::query($sql, __FILE__, __LINE__); // Delete user from database $sql = "DELETE FROM $table_user WHERE user_id = '".$user_id."'"; - api_sql_query($sql,__FILE__,__LINE__); + Database::query($sql, __FILE__, __LINE__); // Delete user from the admin table $sql = "DELETE FROM $table_admin WHERE user_id = '".$user_id."'"; - api_sql_query($sql,__FILE__,__LINE__); + Database::query($sql, __FILE__, __LINE__); // Delete the personal agenda-items from this user $agenda_table = Database :: get_user_personal_table(TABLE_PERSONAL_AGENDA); $sql = "DELETE FROM $agenda_table WHERE user = '".$user_id."'"; - api_sql_query($sql,__FILE__,__LINE__); + Database::query($sql, __FILE__, __LINE__); $gradebook_results_table = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT); $sql = 'DELETE FROM '.$gradebook_results_table.' WHERE user_id = '.$user_id; - api_sql_query($sql, __FILE__, __LINE__); + Database::query($sql, __FILE__, __LINE__); $user = Database::fetch_array($res); $t_ufv = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES); $sqlv = "DELETE FROM $t_ufv WHERE user_id = $user_id"; - $resv = api_sql_query($sqlv,__FILE__,__LINE__); + $resv = Database::query($sqlv, __FILE__, __LINE__); if ($_configuration['multiple_access_urls']) { - require_once (api_get_path(LIBRARY_PATH).'urlmanager.lib.php'); + require_once api_get_path(LIBRARY_PATH).'urlmanager.lib.php'; $url_id=1; if (api_get_current_access_url_id()!=-1) $url_id=api_get_current_access_url_id(); @@ -267,13 +262,14 @@ class UserManager { * @param string $openid * @return boolean true if the user information was updated */ - public static function update_openid ($user_id, $openid) { + public static function update_openid($user_id, $openid) { $table_user = Database :: get_main_table(TABLE_MAIN_USER); $sql = "UPDATE $table_user SET openid='".Database::escape_string($openid)."'"; $sql .= " WHERE user_id='$user_id'"; - return api_sql_query($sql,__FILE__,__LINE__); + return Database::query($sql, __FILE__, __LINE__); } + /** * Update user information * @param int $user_id @@ -292,7 +288,7 @@ class UserManager { * @param array A series of additional fields to add to this user as extra fields (optional, defaults to null) * @return boolean true if the user information was updated */ - public static function update_user ($user_id, $firstname, $lastname, $username, $password = null, $auth_source = null, $email, $status, $official_code, $phone, $picture_uri, $expiration_date, $active, $creator_id= null, $hr_dept_id=0, $extra=null,$language='english') { + public static function update_user ($user_id, $firstname, $lastname, $username, $password = null, $auth_source = null, $email, $status, $official_code, $phone, $picture_uri, $expiration_date, $active, $creator_id = null, $hr_dept_id = 0, $extra = null, $language = 'english') { global $userPasswordCrypted; $table_user = Database :: get_main_table(TABLE_MAIN_USER); $sql = "UPDATE $table_user SET @@ -300,14 +296,12 @@ class UserManager { firstname='".Database::escape_string($firstname)."', username='".Database::escape_string($username)."', language='".Database::escape_string($language)."',"; - if(!is_null($password)) - { + if (!is_null($password)) { //$password = $userPasswordCrypted ? md5($password) : $password; $password = api_get_encrypted_password($password); $sql .= " password='".Database::escape_string($password)."',"; } - if(!is_null($auth_source)) - { + if (!is_null($auth_source)) { $sql .= " auth_source='".Database::escape_string($auth_source)."',"; } $sql .= " @@ -319,21 +313,17 @@ class UserManager { expiration_date='".Database::escape_string($expiration_date)."', active='".Database::escape_string($active)."', hr_dept_id=".intval($hr_dept_id); - if(!is_null($creator_id)) - { + if (!is_null($creator_id)) { $sql .= ", creator_id='".Database::escape_string($creator_id)."'"; } $sql .= " WHERE user_id='$user_id'"; - $return = api_sql_query($sql,__FILE__,__LINE__); - if(is_array($extra) and count($extra)>0) - { + $return = Database::query($sql, __FILE__, __LINE__); + if (is_array($extra) && count($extra) > 0) { $res = true; - foreach($extra as $fname => $fvalue) - { + foreach($extra as $fname => $fvalue) { $res = $res && self::update_extra_field($user_id,$fname,$fvalue); } } - return $return; } @@ -342,10 +332,10 @@ class UserManager { * @param string the wanted username * @return boolean true if the wanted username is available */ - public static function is_username_available ($username) { + public static function is_username_available($username) { $table_user = Database :: get_main_table(TABLE_MAIN_USER); $sql = "SELECT username FROM $table_user WHERE username = '".addslashes($username)."'"; - $res = api_sql_query($sql,__FILE__,__LINE__); + $res = Database::query($sql, __FILE__, __LINE__); return Database::num_rows($res) == 0; } @@ -478,9 +468,9 @@ class UserManager { } } if (count($order_by)>0) { - $sql_query .= ' ORDER BY '.Database::escape_string(implode(',',$order_by)); + $sql_query .= ' ORDER BY '.Database::escape_string(implode(',', $order_by)); } - $sql_result = api_sql_query($sql_query,__FILE__,__LINE__); + $sql_result = Database::query($sql_query, __FILE__, __LINE__); while ($result = Database::fetch_array($sql_result)) { $return_array[] = $result; } @@ -506,9 +496,9 @@ class UserManager { } } if (count($order_by)>0) { - $sql_query .= ' ORDER BY '.Database::escape_string(implode(',',$order_by)); + $sql_query .= ' ORDER BY '.Database::escape_string(implode(',', $order_by)); } - $sql_result = api_sql_query($sql_query,__FILE__,__LINE__); + $sql_result = Database::query($sql_query, __FILE__, __LINE__); while ($result = Database::fetch_array($sql_result)) { $return_array[] = $result; } @@ -521,16 +511,13 @@ class UserManager { * @param string The username * @return array All user information as an associative array */ - public static function get_user_info ($username) { + public static function get_user_info($username) { $user_table = Database :: get_main_table(TABLE_MAIN_USER); $sql = "SELECT * FROM $user_table WHERE username='".$username."'"; - $res = api_sql_query($sql,__FILE__,__LINE__); - if(Database::num_rows($res)>0) - { + $res = Database::query($sql, __FILE__, __LINE__); + if (Database::num_rows($res) > 0) { $user = Database::fetch_array($res); - } - else - { + } else { $user = false; } return $user; @@ -542,58 +529,49 @@ class UserManager { * @param boolean Whether to return the user's extra fields (defaults to false) * @return array All user information as an associative array */ - public static function get_user_info_by_id ($user_id,$user_fields=false) { + public static function get_user_info_by_id($user_id, $user_fields = false) { $user_id = intval($user_id); $user_table = Database :: get_main_table(TABLE_MAIN_USER); $sql = "SELECT * FROM $user_table WHERE user_id=".$user_id; - $res = api_sql_query($sql,__FILE__,__LINE__); - if(Database::num_rows($res)>0) - { + $res = Database::query($sql, __FILE__, __LINE__); + if (Database::num_rows($res) > 0) { $user = Database::fetch_array($res); $t_uf = Database::get_main_table(TABLE_MAIN_USER_FIELD); $t_ufv = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES); $sqlf = "SELECT * FROM $t_uf ORDER BY field_order"; - $resf = api_sql_query($sqlf,__FILE__,__LINE__); - if(Database::num_rows($resf)>0) - { - while($rowf = Database::fetch_array($resf)) - { + $resf = Database::query($sqlf, __FILE__, __LINE__); + if (Database::num_rows($resf) > 0) { + while ($rowf = Database::fetch_array($resf)) { $sqlv = "SELECT * FROM $t_ufv WHERE field_id = ".$rowf['id']." AND user_id = ".$user['user_id']." ORDER BY id DESC"; - $resv = api_sql_query($sqlv,__FILE__,__LINE__); - if(Database::num_rows($resv)>0) - { + $resv = Database::query($sqlv, __FILE__, __LINE__); + if (Database::num_rows($resv) > 0) { //There should be only one value for a field and a user $rowv = Database::fetch_array($resv); $user['extra'][$rowf['field_variable']] = $rowv['field_value']; - } - else - { + } else { $user['extra'][$rowf['field_variable']] = ''; } } } - - } - else - { + } else { $user = false; } return $user; } + /** Get the teacher list * @param int the course ID * @param array Content the list ID of user_id selected */ //for survey - public static function get_teacher_list ($course_id, $sel_teacher='') { + public static function get_teacher_list($course_id, $sel_teacher = '') { $user_course_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER); $user_table = Database :: get_main_table(TABLE_MAIN_USER); $sql_query = "SELECT * FROM $user_table a, $user_course_table b where a.user_id=b.user_id AND b.status=1 AND b.course_code='$course_id'"; - $sql_result = api_sql_query($sql_query,__FILE__,__LINE__); + $sql_result = Database::query($sql_query, __FILE__, __LINE__); echo ""; @@ -611,14 +589,11 @@ class UserManager { * @param bool If we want that the function returns the /main/img/unknown.jpg image set it at true * @return array Array of 2 elements: 'dir' and 'file' which contain the dir and file as the name implies if image does not exist it will return the unknow image if anonymous parameter is true if not it returns an empty array */ - public static function get_user_picture_path_by_id ($id,$type='none',$preview=false,$anonymous=false) { - if(empty($id) or empty($type)) - { - if ($anonymous) - { - $dir=''; - switch($type) - { + public static function get_user_picture_path_by_id($id, $type = 'none', $preview = false, $anonymous = false) { + if (empty($id) or empty($type)) { + if ($anonymous) { + $dir = ''; + switch ($type) { case 'system': //return the complete path to the file, from root $dir = api_get_path(SYS_CODE_PATH).'img/'; break; @@ -632,33 +607,26 @@ class UserManager { default: break; } - $file_anonymous='unknown.jpg'; - return array('dir'=>$dir,'file'=>$file_anonymous); - } - else - { - return array('dir'=>'','file'=>''); + $file_anonymous = 'unknown.jpg'; + return array('dir' => $dir, 'file' => $file_anonymous); + } else { + return array('dir' => '', 'file' => ''); } } $user_id = intval($id); $user_table = Database :: get_main_table(TABLE_MAIN_USER); $sql = "SELECT picture_uri FROM $user_table WHERE user_id=".$user_id; - $res = api_sql_query($sql,__FILE__,__LINE__); + $res = Database::query($sql, __FILE__, __LINE__); - $user=array(); + $user = array(); - if(Database::num_rows($res)>0) - { + if (Database::num_rows($res) > 0) { $user = Database::fetch_array($res); - } - else - { - if ($anonymous) - { - $dir=''; - switch($type) - { + } else { + if ($anonymous) { + $dir = ''; + switch ($type) { case 'system': //return the complete path to the file, from root $dir = api_get_path(SYS_CODE_PATH).'img/'; break; @@ -672,23 +640,18 @@ class UserManager { default: break; } - $file_anonymous='unknown.jpg'; - return array('dir'=>$dir,'file'=>$file_anonymous); - } - else - { + $file_anonymous = 'unknown.jpg'; + return array('dir' => $dir, 'file' => $file_anonymous); + } else { return array('dir'=>'','file'=>''); } } $path = trim($user['picture_uri']); - if (empty($path)) - { - if ($anonymous) - { - switch($type) - { + if (empty($path)) { + if ($anonymous) { + switch ($type) { case 'system': //return the complete path to the file, from root $dir = api_get_path(SYS_CODE_PATH).'img/'; break; @@ -702,29 +665,24 @@ class UserManager { default: break; } - $file_anonymous='unknown.jpg'; - return array('dir'=>$dir,'file'=>$file_anonymous); + $file_anonymous = 'unknown.jpg'; + return array('dir' => $dir, 'file' => $file_anonymous); } } $dir = ''; $first = ''; - if(api_get_setting('split_users_upload_directory') === 'true') - { - if(!empty($path)) - { - $first = substr($path,0,1).'/'; - } - elseif($preview==true) - { - $first = substr(''.$user_id,0,1).'/'; + if (api_get_setting('split_users_upload_directory') === 'true') { + if (!empty($path)) { + $first = substr($path, 0, 1).'/'; + } elseif($preview == true) { + $first = substr(''.$user_id, 0, 1).'/'; } } $first .= $user_id.'/'; - switch($type) - { + switch ($type) { case 'system': //return the complete path to the file, from root $dir = api_get_path(SYS_CODE_PATH).'upload/users/'.$first; break; @@ -738,7 +696,7 @@ class UserManager { default: break; } - return array('dir'=>$dir,'file'=>$path); + return array('dir' => $dir, 'file' => $path); } /* @@ -761,21 +719,23 @@ class UserManager { * @param $force Optional parameter to force building after a removal request * @return A string containing the XHTML code to dipslay the production list, or FALSE */ - public static function build_production_list ($user_id, $force = false, $showdelete=false) { - if (!$force && !empty($_POST['remove_production'])) + public static function build_production_list($user_id, $force = false, $showdelete = false) { + if (!$force && !empty($_POST['remove_production'])) { return true; // postpone reading from the filesystem + } $productions = UserManager::get_user_productions($user_id); - if (empty($productions)) + if (empty($productions)) { return false; + } - $production_path = UserManager::get_user_picture_path_by_id($user_id,'web',true); + $production_path = UserManager::get_user_picture_path_by_id($user_id, 'web', true); $production_dir = $production_path['dir'].$user_id.'/'; $del_image = api_get_path(WEB_CODE_PATH).'img/delete.gif'; $del_text = get_lang('Delete'); $production_list = ''; - if (count($productions)>0) { + if (count($productions) > 0) { $production_list = '