Skip to content

Commit

Permalink
Major cleanup of user administration and display, including sortable …
Browse files Browse the repository at this point in the history
…listings,

confirmation on deletions, removing deleted user from all student lists,
teacher lists and subscription lists and freeing up their username and
email to be used again.
  • Loading branch information
martin committed Sep 22, 2002
1 parent 7684ce4 commit d578afc
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 125 deletions.
78 changes: 67 additions & 11 deletions admin/user.php
Expand Up @@ -4,6 +4,12 @@
require("../user/lib.php");
require("../lib/countries.php");

optional_variable($newuser, "");
optional_variable($delete, "");
optional_variable($confirm, "");
optional_variable($sort, "name");
optional_variable($dir, "ASC");

if (! record_exists_sql("SELECT * FROM user_admins")) { // No admin user yet
$user->firstname = "Admin";
$user->lastname = "User";
Expand Down Expand Up @@ -61,7 +67,6 @@
}

if ($newuser) { // Create a new user

$user->firstname = "";
$user->lastname = "";
$user->username = "changeme";
Expand Down Expand Up @@ -89,28 +94,79 @@
print_header("$site->fullname : $stredituser", $site->fullname,
"<A HREF=\"$CFG->wwwroot/admin\">$stradministration</A> -> $stredituser");

if ($delete) {
if ($deleteuser = get_record("user", "id", "$delete")) {
if (set_field("user", "deleted", "1", "id", "$delete")) {
set_field("user", "timemodified", time(), "id", "$delete");
notify(get_string("deletedactivity", "", "$deleteuser->firstname $deleteuser->lastname"));
if ($delete) { // Delete a selected user, after confirmation
if (!$user = get_record("user", "id", "$delete")) {
error("No such user!");
}
if ($confirm != md5($delete)) {
notice_yesno(get_string("deletecheckfull", "", "'$user->firstname $user->lastname'"),
"user.php?delete=$delete&confirm=".md5($delete), "user.php");

exit;
} else {
$user->deleted = "1";
$user->username = $user->email; // Remember it just in case
$user->email = ""; // Clear this field to free it up
$user->timemodified = time();
if (update_record("user", $user)) {
unenrol_student($user->id); // From all courses
remove_teacher($user->id); // From all courses
remove_admin($user->id);
notify(get_string("deletedactivity", "", "$user->firstname $user->lastname"));
} else {
notify(get_string("deletednot", "", "$user->firstname $user->lastname"));
}
}
}

if ($users = get_records_sql("SELECT * from user WHERE username <> 'guest' AND deleted <> '1' ORDER BY firstname")) {
// Carry on with the user listing

$columns = array("name", "email", "city", "country", "lastaccess");

foreach ($columns as $column) {
$string[$column] = get_string("$column");
$columnsort = "$column";
if ($column == "lastaccess") {
$columndir = "DESC";
} else {
$columndir = "ASC";
}
if ($columnsort == $sort) {
$$column = $string[$column];
} else {
$$column = "<A HREF=\"user.php?sort=$columnsort&dir=$columndir\">".$string[$column]."</A>";
}
}

if ($sort == "name") {
$sort = "firstname";
}

if ($users = get_records_sql("SELECT * from user WHERE username <> 'guest'
AND deleted <> '1' ORDER BY $sort $dir")) {

print_heading(get_string("chooseuser"));

$table->head = array (get_string("fullname"), get_string("email"), get_string("city"),
get_string("country"), " ", " ");
$table->align = array ("LEFT", "LEFT", "CENTER", "CENTER", "CENTER", "CENTER", "CENTER");
$table->head = array ($name, $email, $city, $country, $lastaccess, "", "");
$table->align = array ("LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "CENTER", "CENTER");
foreach ($users as $user) {
if ($user->id == $USER->id or $user->username == "changeme") {
$deletebutton = "";
} else {
$deletebutton = "<A HREF=\"user.php?delete=$user->id\" TARGET=\"$strdeletecheck\">$strdelete</A>";
}
if ($user->lastaccess) {
$strlastaccess = format_time(time() - $user->lastaccess);
} else {
$strlastaccess = get_string("never");
}
$table->data[] = array ("<A HREF=\"../user/view.php?id=$user->id&course=$site->id\">$user->firstname $user->lastname</A>",
"$user->email",
"$user->city",
$COUNTRIES[$user->country],
$strlastaccess,
"<A HREF=\"../user/edit.php?id=$user->id&course=$site->id\">$stredit</A>",
"<A HREF=\"user.php?delete=$user->id\" TARGET=\"$strdeletecheck\">$strdelete</A>");
$deletebutton);
}
print_table($table);

Expand Down
4 changes: 2 additions & 2 deletions course/enrol.php
Expand Up @@ -19,7 +19,7 @@
add_to_log($course->id, "course", "guest", "view.php?id=$course->id", "$REMOTE_ADDR, $REMOTE_HOST");

} else {
if (! enrol_student_in_course($USER->id, $course->id)) {
if (! enrol_student($USER->id, $course->id)) {
error("An error occurred while trying to enrol you.");
}

Expand Down Expand Up @@ -65,7 +65,7 @@
if (isguest()) {
add_to_log($course->id, "course", "guest", "view.php?id=$course->id", "$USER->id");
} else {
if (! enrol_student_in_course($USER->id, $course->id)) {
if (! enrol_student($USER->id, $course->id)) {
error("An error occurred while trying to enrol you.");
}
add_to_log($course->id, "course", "enrol", "view.php?id=$course->id", "$USER->id");
Expand Down
25 changes: 3 additions & 22 deletions course/lib.php
Expand Up @@ -308,7 +308,9 @@ function print_recent_activity($course) {
$content = true;
}
$user = get_record("user", "id", $log->info);
echo "<P><FONT SIZE=1><A HREF=\"../user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A></FONT></P>";
if (isstudent($course->id, $user->id)) {
echo "<P><FONT SIZE=1><A HREF=\"../user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A></FONT></P>";
}
}
}

Expand Down Expand Up @@ -382,27 +384,6 @@ function print_recent_activity($course) {
}


function unenrol_student_in_course($user, $course) {
global $db;

return $db->Execute("DELETE FROM user_students WHERE user = '$user' AND course = '$course'");
}



function enrol_student_in_course($user, $course) {
global $db;

$timenow = time();

$rs = $db->Execute("INSERT INTO user_students (user, course, start, end, time)
VALUES ($user, $course, 0, 0, $timenow)");
if ($rs) {
return true;
} else {
return false;
}
}

function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
// Returns a number of useful structures for course displays
Expand Down
19 changes: 6 additions & 13 deletions course/teacher.php
Expand Up @@ -62,10 +62,7 @@


/// Get all existing teachers for this course.
$teachers = get_records_sql("SELECT u.*,t.authority,t.id as teachid FROM user u, user_teachers t
WHERE t.course = '$course->id'
AND t.user = u.id
ORDER BY t.authority ASC");
$teachers = get_course_teachers($course->id);

/// Add a teacher if one is specified

Expand Down Expand Up @@ -93,7 +90,6 @@
if (! $teacher->id) {
error("Could not add that teacher to this course!");
}
$user->authority = $teacher->authority;
$teachers[] = $user;
}

Expand All @@ -104,16 +100,13 @@
error("That teacher (id = $remove) doesn't exist", "teacher.php?id=$course->id");
}
if ($teachers) {
foreach ($teachers as $tt) {
foreach ($teachers as $key => $tt) {
if ($tt->id == $user->id) {
delete_records("user_teachers", "id", "$tt->teachid");
remove_teacher($user->id, $course->id);
unset($teachers[$key]);
}
}
}
$teachers = get_records_sql("SELECT u.*,t.authority,t.id as teachid FROM user u, user_teachers t
WHERE t.course = '$course->id'
AND t.user = u.id
ORDER BY t.authority ASC");
}


Expand All @@ -139,13 +132,13 @@
/// Print list of potential teachers

if ($search) {
$users = get_records_sql("SELECT * from user WHERE confirmed = 1
$users = get_records_sql("SELECT * from user WHERE confirmed = 1 AND deleted = 0
AND (firstname LIKE '%$search%' OR
lastname LIKE '%$search%' OR
email LIKE '%$search%')
AND username <> 'guest' AND username <> 'changeme'");
} else {
$users = get_records_sql("SELECT * from user WHERE confirmed = 1
$users = get_records_sql("SELECT * from user WHERE confirmed = 1 AND deleted = 0
AND username <> 'guest' AND username <> 'changeme'");
}

Expand Down
2 changes: 1 addition & 1 deletion course/unenrol.php
Expand Up @@ -26,7 +26,7 @@

if (isset($confirm)) {

if (! unenrol_student_in_course($user->id, $course->id)) {
if (! unenrol_student($user->id, $course->id)) {
error("An error occurred while trying to unenrol you.");
}

Expand Down
3 changes: 3 additions & 0 deletions lang/en/moodle.php
Expand Up @@ -93,6 +93,7 @@
$string['deleted'] = "Deleted";
$string['deletedactivity'] = "Deleted \$a";
$string['deletedcourse'] = "\$a has been completely deleted";
$string['deletednot'] = "Could not delete \$a !";
$string['deletingcourse'] = "Deleting \$a";
$string['description'] = "Description";
$string['doyouagree'] = "Have you read these conditions and understood them?";
Expand Down Expand Up @@ -150,6 +151,8 @@
$string['filloutallfields'] = "Please fill out all fields in this form";
$string['firstname'] = "First name";
$string['firsttime'] = "Is this your first time here?";
$string['followingoptional'] = "The following items are optional";
$string['followingrequired'] = "The following items are required";
$string['forgotten'] = "Forgotten your username or password?";
$string['format'] = "Format";
$string['formatsocial'] = "Social format";
Expand Down
41 changes: 41 additions & 0 deletions lib/moodlelib.php
Expand Up @@ -1033,6 +1033,47 @@ function update_login_count() {
}
}

function remove_admin($user) {
global $db;

return $db->Execute("DELETE FROM user_admins WHERE user = '$user'");
}

function remove_teacher($user, $course=0) {
global $db;

if ($course) {
return $db->Execute("DELETE FROM user_teachers WHERE user = '$user' AND course = '$course'");
} else {
return $db->Execute("DELETE FROM user_teachers WHERE user = '$user'");
}
}


function enrol_student($user, $course) {
global $db;

$timenow = time();

$rs = $db->Execute("INSERT INTO user_students (user, course, start, end, time)
VALUES ($user, $course, 0, 0, $timenow)");
if ($rs) {
return true;
} else {
return false;
}
}

function unenrol_student($user, $course=0) {
global $db;

if ($course) {
return $db->Execute("DELETE FROM user_students WHERE user = '$user' AND course = '$course'");
} else {
return $db->Execute("DELETE FROM user_students WHERE user = '$user'");
}
}


function isadmin($userid=0) {
global $USER;
Expand Down

0 comments on commit d578afc

Please sign in to comment.