/**
* Delete a user from the platform, and all its belongings. This is a
* very dangerous function that should only be accessible by
* super-admins. Other roles should only be able to disable a user,
* which removes access to the platform but doesn't delete anything.
* @param int The ID of th user to be deleted
* @return boolean true if user is successfully deleted, false otherwise
* @assert (null) === false
* @assert ('abc') === false
*/
public static function delete_user($user_id)
{
if ($user_id != strval(intval($user_id))) {
return false;
}
if ($user_id === false) {
return false;
}
if (!self::can_delete_user($user_id)) {
return false;
}
$table_user = Database::get_main_table(TABLE_MAIN_USER);
$usergroup_rel_user = Database::get_main_table(TABLE_USERGROUP_REL_USER);
$table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
$table_course = Database::get_main_table(TABLE_MAIN_COURSE);
$table_session = Database::get_main_table(TABLE_MAIN_SESSION);
$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);
$table_group = Database::get_course_table(TABLE_GROUP_USER);
$table_work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
// Unsubscribe the user from all groups in all his courses
$sql = "SELECT c.id FROM {$table_course} c, {$table_course_user} cu\n WHERE\n cu.user_id = '" . $user_id . "' AND\n relation_type<>" . COURSE_RELATION_TYPE_RRHH . " AND\n c.id = cu.c_id";
$res = Database::query($sql);
while ($course = Database::fetch_object($res)) {
$sql = "DELETE FROM {$table_group}\n WHERE c_id = {$course->id} AND user_id = {$user_id}";
Database::query($sql);
}
// Unsubscribe user from all classes
//Classes are not longer supported
/* $sql = "DELETE FROM $table_class_user WHERE user_id = '".$user_id."'";
Database::query($sql); */
// Unsubscribe user from usergroup_rel_user
$sql = "DELETE FROM {$usergroup_rel_user} WHERE user_id = '" . $user_id . "'";
Database::query($sql);
// Unsubscribe user from all courses
$sql = "DELETE FROM {$table_course_user} WHERE user_id = '" . $user_id . "'";
Database::query($sql);
// Unsubscribe user from all courses in sessions
$sql = "DELETE FROM {$table_session_course_user} WHERE user_id = '" . $user_id . "'";
Database::query($sql);
// If the user was added as a id_coach then set the current admin as coach see BT#
$currentUserId = api_get_user_id();
$sql = "UPDATE {$table_session} SET id_coach = {$currentUserId} WHERE id_coach = '" . $user_id . "'";
Database::query($sql);
$sql = "UPDATE {$table_session} SET id_coach = {$currentUserId} WHERE session_admin_id = '" . $user_id . "'";
Database::query($sql);
// Unsubscribe user from all sessions
$sql = "DELETE FROM {$table_session_user} WHERE user_id = '" . $user_id . "'";
Database::query($sql);
// Delete user picture
/* TODO: Logic about api_get_setting('split_users_upload_directory') == 'true'
a user has 4 different sized photos to be deleted. */
$user_info = api_get_user_info($user_id);
if (strlen($user_info['picture_uri']) > 0) {
$path = self::getUserPathById($user_id, 'system');
$img_path = $path . $user_info['picture_uri'];
if (file_exists($img_path)) {
unlink($img_path);
}
}
// Delete the personal course categories
$course_cat_table = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
$sql = "DELETE FROM {$course_cat_table} WHERE user_id = '" . $user_id . "'";
Database::query($sql);
// Delete user from database
$sql = "DELETE FROM {$table_user} WHERE id = '" . $user_id . "'";
Database::query($sql);
// Delete user from the admin table
$sql = "DELETE FROM {$table_admin} WHERE user_id = '" . $user_id . "'";
Database::query($sql);
// Delete the personal agenda-items from this user
$agenda_table = Database::get_main_table(TABLE_PERSONAL_AGENDA);
$sql = "DELETE FROM {$agenda_table} WHERE user = '" . $user_id . "'";
Database::query($sql);
$gradebook_results_table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
$sql = 'DELETE FROM ' . $gradebook_results_table . ' WHERE user_id = ' . $user_id;
Database::query($sql);
$extraFieldValue = new ExtraFieldValue('user');
$extraFieldValue->deleteValuesByItem($user_id);
if (api_get_multiple_access_url()) {
$url_id = api_get_current_access_url_id();
UrlManager::delete_url_rel_user($user_id, $url_id);
} else {
//we delete the user from the url_id =1
UrlManager::delete_url_rel_user($user_id, 1);
}
if (api_get_setting('allow_social_tool') == 'true') {
$userGroup = new UserGroup();
//Delete user from portal groups
//.........这里部分代码省略.........
/**
* Delete a course
* This function deletes a whole course-area from the platform. When the
* given course is a virtual course, the database and directory will not be
* deleted.
* When the given course is a real course, also all virtual courses refering
* to the given course will be deleted.
* Considering the fact that we remove all traces of the course in the main
* database, it makes sense to remove all tracking as well (if stats databases exist)
* so that a new course created with this code would not use the remains of an older
* course.
*
* @param string The code of the course to delete
* @todo When deleting a virtual course: unsubscribe users from that virtual
* course from the groups in the real course if they are not subscribed in
* that real course.
* @todo Remove globals
*/
public static function delete_course($code)
{
$table_course = Database::get_main_table(TABLE_MAIN_COURSE);
$table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
$table_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
$table_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
$table_course_survey = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY);
$table_course_survey_question = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
$table_course_survey_question_option = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
$table_course_rel_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
$table_stats_hotpots = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
$table_stats_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
$table_stats_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
$table_stats_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
$table_stats_lastaccess = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
$table_stats_course_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
$table_stats_online = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
$table_stats_default = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
$table_stats_downloads = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
$table_stats_links = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LINKS);
$table_stats_uploads = Database::get_main_table(TABLE_STATISTIC_TRACK_E_UPLOADS);
$codeFiltered = Database::escape_string($code);
$sql = "SELECT * FROM {$table_course} WHERE code='" . $codeFiltered . "'";
$res = Database::query($sql);
if (Database::num_rows($res) == 0) {
return;
}
$sql = "SELECT * FROM {$table_course}\n WHERE code = '" . $codeFiltered . "'";
$res = Database::query($sql);
$course = Database::fetch_array($res);
$courseId = $course['id'];
$count = 0;
if (api_is_multiple_url_enabled()) {
$url_id = 1;
if (api_get_current_access_url_id() != -1) {
$url_id = api_get_current_access_url_id();
}
UrlManager::delete_url_rel_course($courseId, $url_id);
$count = UrlManager::getCountUrlRelCourse($courseId);
}
if ($count == 0) {
self::create_database_dump($code);
$course_tables = AddCourse::get_course_tables();
// Cleaning c_x tables
if (!empty($courseId)) {
foreach ($course_tables as $table) {
$table = Database::get_course_table($table);
$sql = "DELETE FROM {$table} WHERE c_id = {$courseId} ";
Database::query($sql);
}
}
$course_dir = api_get_path(SYS_COURSE_PATH) . $course['directory'];
$archive_dir = api_get_path(SYS_ARCHIVE_PATH) . $course['directory'] . '_' . time();
if (is_dir($course_dir)) {
rename($course_dir, $archive_dir);
}
// Unsubscribe all users from the course
$sql = "DELETE FROM {$table_course_user} WHERE c_id='" . $courseId . "'";
Database::query($sql);
// Delete the course from the sessions tables
$sql = "DELETE FROM {$table_session_course} WHERE c_id='" . $courseId . "'";
Database::query($sql);
$sql = "DELETE FROM {$table_session_course_user} WHERE c_id='" . $courseId . "'";
Database::query($sql);
// Delete from Course - URL
$sql = "DELETE FROM {$table_course_rel_url} WHERE c_id = '" . $courseId . "'";
Database::query($sql);
$sql = 'SELECT survey_id FROM ' . $table_course_survey . ' WHERE course_code="' . $codeFiltered . '"';
$result_surveys = Database::query($sql);
while ($surveys = Database::fetch_array($result_surveys)) {
$survey_id = $surveys[0];
$sql = 'DELETE FROM ' . $table_course_survey_question . ' WHERE survey_id="' . $survey_id . '"';
Database::query($sql);
$sql = 'DELETE FROM ' . $table_course_survey_question_option . ' WHERE survey_id="' . $survey_id . '"';
Database::query($sql);
$sql = 'DELETE FROM ' . $table_course_survey . ' WHERE survey_id="' . $survey_id . '"';
Database::query($sql);
}
// Cleaning group categories
$groupCategories = GroupManager::get_categories($course['code']);
if (!empty($groupCategories)) {
foreach ($groupCategories as $category) {
//.........这里部分代码省略.........
/**
* Updates the access_url_rel_session table with a given user list
* @author Julio Montoya
* @param array user list
* @param int access_url_id
* */
public static function update_urls_rel_session($session_list, $access_url_id)
{
$table_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
$sql = "SELECT session_id FROM {$table_url_rel_session} WHERE access_url_id=" . intval($access_url_id);
$result = Database::query($sql);
$existing_sessions = array();
while ($row = Database::fetch_array($result)) {
$existing_sessions[] = $row['session_id'];
}
// Adding users
foreach ($session_list as $session) {
if (!in_array($session, $existing_sessions)) {
if (!empty($session) && !empty($access_url_id)) {
UrlManager::add_session_to_url($session, $access_url_id);
}
}
}
// Deleting old users
foreach ($existing_sessions as $existing_session) {
if (!in_array($existing_session, $session_list)) {
if (!empty($existing_session) && !empty($access_url_id)) {
UrlManager::delete_url_rel_session($existing_session, $access_url_id);
}
}
}
}
请发表评论