/**
  * 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 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}\n                WHERE id_coach = '" . $user_id . "'";
     Database::query($sql);
     $sql = "UPDATE {$table_session} SET id_coach = {$currentUserId}\n                WHERE session_admin_id = '" . $user_id . "'";
     Database::query($sql);
     // Unsubscribe user from all sessions
     $sql = "DELETE FROM {$table_session_user}\n                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 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 = '******'";
     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);
     UrlManager::deleteUserFromAllUrls($user_id);
     if (api_get_setting('social.allow_social_tool') == 'true') {
         $userGroup = new UserGroup();
         //Delete user from portal groups
         $group_list = $userGroup->get_groups_by_user($user_id);
         if (!empty($group_list)) {
             foreach ($group_list as $group_id => $data) {
                 $userGroup->delete_user_rel_group($user_id, $group_id);
             }
         }
         // Delete user from friend lists
         SocialManager::remove_user_rel_user($user_id, true);
     }
     // Removing survey invitation
     SurveyManager::delete_all_survey_invitations_by_user($user_id);
     // Delete students works
     $sql = "DELETE FROM {$table_work} WHERE user_id = {$user_id} AND c_id <> 0";
     Database::query($sql);
     $sql = "UPDATE c_item_property SET to_user_id = NULL\n                WHERE to_user_id = '" . $user_id . "'";
     Database::query($sql);
     $sql = "UPDATE c_item_property SET insert_user_id = NULL\n                WHERE insert_user_id = '" . $user_id . "'";
     Database::query($sql);
     $sql = "UPDATE c_item_property SET lastedit_user_id = NULL\n                WHERE lastedit_user_id = '" . $user_id . "'";
     Database::query($sql);
     // Delete user from database
     $sql = "DELETE FROM {$table_user} WHERE id = '" . $user_id . "'";
     Database::query($sql);
     // Add event to system log
     $user_id_manager = api_get_user_id();
     Event::addEvent(LOG_USER_DELETE, LOG_USER_ID, $user_id, api_get_utc_datetime(), $user_id_manager);
     Event::addEvent(LOG_USER_DELETE, LOG_USER_OBJECT, $user_info, api_get_utc_datetime(), $user_id_manager);
     return true;
 }