📄 usermanager.lib.php
字号:
<?php // $Id: usermanager.lib.php 15169 2008-04-29 06:27:22Z yannoo $/*============================================================================== Dokeos - elearning and course management software Copyright (c) 2004-2008 Dokeos SPRL Copyright (c) 2003 Ghent University (UGent) Copyright (c) 2001 Universite catholique de Louvain (UCL) Copyright (c) various contributors Copyright (c) Bart Mollet, Hogeschool Gent For a full list of contributors, see "credits.txt". The full license can be read in "license.txt". This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. See the GNU General Public License for more details. Contact: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium, info@dokeos.com==============================================================================*//**==============================================================================* This library provides functions for user management.* Include/require it in your code to use its functionality.** @package dokeos.library==============================================================================*/// define constants for user extra field typesdefine('USER_FIELD_TYPE_TEXT',1);define('USER_FIELD_TYPE_TEXTAREA',2);define('USER_FIELD_TYPE_RADIO',3);define('USER_FIELD_TYPE_SELECT',4);define('USER_FIELD_TYPE_SELECT_MULTIPLE',5);define('USER_FIELD_TYPE_DATE',6);define('USER_FIELD_TYPE_DATETIME',7);class UserManager{ /** * Creates a new user for the platform * @author Hugues Peeters <peeters@ipm.ucl.ac.be>, * Roan Embrechts <roan_embrechts@yahoo.com> * * @param string Firstname * @param string Lastname * @param int Status (1 for course tutor, 5 for student, 6 for anonymous) * @param string e-mail address * @param string Login * @param string Password * @param string Any official code (optional) * @param int User language (optional) * @param string Phone number (optional) * @param string Picture URI (optional) * @param string Authentication source (optional, defaults to 'platform', dependind on constant) * @param string Account expiration date (optional, defaults to '0000-00-00 00:00:00') * @param int Whether the account is enabled or disabled by default * @param int The user ID of the person who registered this user (optional, defaults to null) * @param int The department of HR in which the user is registered (optional, defaults to 0) * @return int new user id - if the new user creation succeeds * boolean 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, * it stores the error message in global $api_failureList */ 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; // database table definition $table_user = Database::get_main_table(TABLE_MAIN_USER); // default langauge if ($language=='') { $language = api_get_setting('platformLanguage'); } if ($_user['user_id']) { $creator_id = $_user['user_id']; } else { $creator_id = ''; } // First check wether the login already exists if (! UserManager::is_username_available($loginName)) return api_set_failure('login-pass already taken'); //$password = "PLACEHOLDER"; $password = ($userPasswordCrypted ? md5($password) : $password); $sql = "INSERT INTO $table_user SET lastname = '".Database::escape_string($lastName)."', firstname = '".Database::escape_string($firstName)."', username = '".Database::escape_string($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 = now(), 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); if ($result) { //echo "id returned"; $return=Database::get_last_insert_id(); } else { //echo "false - failed" ; $return=false; } if(is_array($extra) AND count($extra)>0) { $res = true; foreach($extra as $fname => $fvalue) { $res = $res && UserManager::update_extra_field($return,$fname,$fvalue); } } return $return; } /** * Can user be deleted? * This functions checks if there's a course in which the given user is the * only course administrator. If that is the case, the user can't be * deleted because the course would remain without a course admin. * @param int $user_id The user id * @return boolean true if user can be deleted */ 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)) { $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) { return false; } } return true; } /** * Delete a user from the platform * @param int $user_id The user id * @return boolean true if user is succesfully deleted, false otherwise */ function delete_user($user_id) { if (!UserManager :: can_delete_user($user_id)) { return false; } $table_user = Database :: get_main_table(TABLE_MAIN_USER); $table_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER); $table_class_user = Database :: get_main_table(TABLE_MAIN_CLASS_USER); $table_course = Database :: get_main_table(TABLE_MAIN_COURSE); $table_admin = Database :: get_main_table(TABLE_MAIN_ADMIN); $table_session_user = Database :: get_main_table(TABLE_MAIN_SESSION_USER); $table_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER); // 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)) { $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__); } // Unsubscribe user from all classes $sql = "DELETE FROM $table_class_user WHERE user_id = '".$user_id."'"; api_sql_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__); // 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__); // Unsubscribe user from all sessions $sql = "DELETE FROM $table_session_user WHERE id_user = '".$user_id."'"; api_sql_query($sql,__FILE__,__LINE__); // Delete user picture $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_info['picture_uri']; unlink($img_path); } // 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__); // Delete user from database $sql = "DELETE FROM $table_user WHERE user_id = '".$user_id."'"; api_sql_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__); // 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__); $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__); $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__); return true; } /** * Update user information with new openid * @param int $user_id * @param string $openid * @return boolean true if the user information was updated */ 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__); } /** * Update user information * @param int $user_id * @param string $firstname * @param string $lastname * @param string $username * @param string $password * @param string $auth_source * @param string $email * @param int $status * @param string $official_code * @param string $phone * @param string $picture_uri * @param int The user ID of the person who registered this user (optional, defaults to null) * @param int The department of HR in which the user is registered (optional, defaults to 0) * @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 */ 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) { global $userPasswordCrypted; $table_user = Database :: get_main_table(TABLE_MAIN_USER); $sql = "UPDATE $table_user SET lastname='".Database::escape_string($lastname)."', firstname='".Database::escape_string($firstname)."', username='".Database::escape_string($username)."',"; if(!is_null($password)) { $password = $userPasswordCrypted ? md5($password) : $password; $sql .= " password='".Database::escape_string($password)."',"; } if(!is_null($auth_source)) { $sql .= " auth_source='".Database::escape_string($auth_source)."',"; } $sql .= " email='".Database::escape_string($email)."', status='".Database::escape_string($status)."', 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)."', 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'"; $return = api_sql_query($sql,__FILE__,__LINE__); if(is_array($extra) and count($extra)>0) { $res = true; foreach($extra as $fname => $fvalue) { $res = $res && UserManager::update_extra_field($user_id,$fname,$fvalue); } } return $return; } /** * Check if a username is available * @param string the wanted username * @return boolean true if the wanted username is available */ 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__); return Database::num_rows($res) == 0; } /** * @param array $conditions a list of condition (exemple : status=>STUDENT) * @param array $order_by a list of fields on which sort * @return array An array with all users of the platform. * @todo optional course code parameter, optional sorting parameters... */ function get_user_list($conditions = array(), $order_by = array()) { $user_table = Database :: get_main_table(TABLE_MAIN_USER); $return_array = array(); $sql_query = "SELECT * FROM $user_table"; if(count($conditions)>0) { $sql_query .= ' WHERE '; foreach($conditions as $field=>$value) { $sql_query .= $field.' = '.$value; } } if(count($order_by)>0) { $sql_query .= ' ORDER BY '.implode(',',$order_by); } $sql_result = api_sql_query($sql_query,__FILE__,__LINE__); while ($result = Database::fetch_array($sql_result)) { $return_array[] = $result; } return $return_array; } /** * Get user information * @param string The username * @return array All user information as an associative array */ 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) { $user = Database::fetch_array($res); } else { $user = false; } return $user; } /** * Get user information * @param string The id * @param boolean Whether to return the user's extra fields (defaults to false) * @return array All user information as an associative array */ 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) { $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";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -