Skip to content

Commit

Permalink
Cleanup of the admin interface - especially the user management
Browse files Browse the repository at this point in the history
Bugs fixed, duplicate code removed, plus you can now add new users.

Aaah much better.  :-)
  • Loading branch information
martin committed Aug 8, 2002
1 parent e5a57e8 commit a3447e1
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 200 deletions.
40 changes: 40 additions & 0 deletions CHANGES
@@ -1,3 +1,43 @@
2002-08-08 Thursday 13:06 martin

* doc/future.html: Updated this page (because now bugs.moodle.org
exists)

2002-08-08 Thursday 10:11 martin

* config-dist.php: Slightly clearer explanations ...

2002-08-07 Wednesday 16:46 martin

* mod/assignment/: version.php, db/mysql.sql: Missing
'auto_increment' on assignment_submissions table, for 'id'

2002-08-07 Wednesday 16:26 martin

* lib/moodlelib.php: get_real_size() for translating text to byte
sizes (eg 2M)

2002-08-07 Wednesday 16:24 martin

* files/index.php: Now properly sets and checks limits on file
sizes

2002-08-07 Wednesday 11:45 martin

* files/index.php: Upload works again ...

2002-08-07 Wednesday 01:41 martin

* index.php: Whoops ... typo in save_session

2002-08-07 Wednesday 01:29 martin

* doc/install.html: Updated requirements.

2002-08-07 Wednesday 01:24 martin

* CHANGES: Changes

2002-08-07 Wednesday 01:23 martin 2002-08-07 Wednesday 01:23 martin


* index.php, admin/user.php, course/enrol.php, course/loginas.php, * index.php, admin/user.php, course/enrol.php, course/loginas.php,
Expand Down
5 changes: 3 additions & 2 deletions admin/index.php
Expand Up @@ -165,14 +165,15 @@
$stradministration = get_string("administration"); $stradministration = get_string("administration");
print_header("$site->fullname: $stradministration","$site->fullname: $stradministration", "$stradministration"); print_header("$site->fullname: $stradministration","$site->fullname: $stradministration", "$stradministration");


$table->head = array ("Site Management", "Course Setup", "Other"); $table->head = array (get_string("site"), get_string("courses"), get_string("users"));
$table->align = array ("CENTER", "CENTER", "CENTER"); $table->align = array ("CENTER", "CENTER", "CENTER");
$table->data[0][0] = "<P><A HREF=\"site.php\">".get_string("sitesettings")."</A></P>". $table->data[0][0] = "<P><A HREF=\"site.php\">".get_string("sitesettings")."</A></P>".
"<P><A HREF=\"../course/log.php?id=$site->id\">".get_string("sitelogs")."</A></P>"; "<P><A HREF=\"../course/log.php?id=$site->id\">".get_string("sitelogs")."</A></P>";
$table->data[0][1] = "<P><A HREF=\"../course/edit.php\">".get_string("addnewcourse")."</A></P>". $table->data[0][1] = "<P><A HREF=\"../course/edit.php\">".get_string("addnewcourse")."</A></P>".
"<P><A HREF=\"../course/teacher.php\">".get_string("assignteachers")."</A></P>". "<P><A HREF=\"../course/teacher.php\">".get_string("assignteachers")."</A></P>".
"<P><A HREF=\"../course/delete.php\">".get_string("deletecourse")."</A></P>"; "<P><A HREF=\"../course/delete.php\">".get_string("deletecourse")."</A></P>";
$table->data[0][2] = "<P><A HREF=\"user.php\">".get_string("edituser")."</A></P>"; $table->data[0][2] = "<P><A HREF=\"user.php?newuser=true\">".get_string("addnewuser")."</A></P>".
"<P><A HREF=\"user.php\">".get_string("edituser")."</A></P>";


print_table($table); print_table($table);


Expand Down
11 changes: 6 additions & 5 deletions admin/site.php
Expand Up @@ -2,12 +2,12 @@


require("../config.php"); require("../config.php");


if ($course = get_site()) { if ($site = get_site()) {
if (!isadmin()) { if (!isadmin()) {
error("You need to be admin to edit this page"); error("You need to be admin to edit this page");
} }
} }
$course->format = "social"; // override $site->format = "social"; // override


/// If data submitted, then process and store. /// If data submitted, then process and store.


Expand All @@ -25,7 +25,7 @@
if (update_record("course", $form)) { if (update_record("course", $form)) {
redirect("$CFG->wwwroot/admin/", "Changes saved"); redirect("$CFG->wwwroot/admin/", "Changes saved");
} else { } else {
error("Serious Error! Could not update the course record! (id = $form->id)"); error("Serious Error! Could not update the site record! (id = $form->id)");
} }
} else { } else {
if ($newid = insert_record("course", $form)) { if ($newid = insert_record("course", $form)) {
Expand All @@ -50,11 +50,12 @@


/// Otherwise fill and print the form. /// Otherwise fill and print the form.


if ($course && !$form) { if ($site && !$form) {
$form = $course; $form = $site;
} else { } else {
$form->category = 0; $form->category = 0;
$form->format = "social"; $form->format = "social";
$form->newsitems = 0;
} }


print_header("Admin: Setting up site", "Administration: Setting up site", print_header("Admin: Setting up site", "Administration: Setting up site",
Expand Down
212 changes: 49 additions & 163 deletions admin/user.php
Expand Up @@ -15,17 +15,17 @@
$user->confirmed = 1; $user->confirmed = 1;
$user->timemodified = time(); $user->timemodified = time();


if (! $id = insert_record("user", $user)) { if (! $user->id = insert_record("user", $user)) {
error("Could not create admin user record !!!"); error("SERIOUS ERROR: Could not create admin user record !!!");
} }


$admin->user = $id; $admin->user = $user->id;


if (! insert_record("user_admins", $admin)) { if (! insert_record("user_admins", $admin)) {
error("Could not make user $id an admin !!!"); error("Could not make user $user->id an admin !!!");
} }


if (! $user = get_record("user", "id", $id)) { if (! $user = get_record("user", "id", $user->id)) { // Double check
error("User ID was incorrect (can't find it)"); error("User ID was incorrect (can't find it)");
} }


Expand All @@ -45,6 +45,13 @@
$USER->admin = true; $USER->admin = true;
$USER->teacher["$course->id"] = true; $USER->teacher["$course->id"] = true;
save_session("USER"); save_session("USER");

$id = $user->id;

} else {
if (! $course = get_site()) {
error("Could not find site-level course");
}
} }


require_login(); require_login();
Expand All @@ -53,172 +60,51 @@
error("You must be an administrator to edit users this way."); error("You must be an administrator to edit users this way.");
} }


if (!$id) { if ($newuser) { // Create a new user
$users = get_records_sql("SELECT * from user ORDER BY firstname");

print_header("Edit users", "Edit users", "<A HREF=\"$CFG->wwwroot/admin\">Admin</A> -> Edit users", "");
print_heading("Choose a user to edit");
$table->head = array ("Name", "Email", "City/Town", "Country");
$table->align = array ("LEFT", "LEFT", "CENTER", "CENTER");
foreach ($users as $user) {
$table->data[] = array ("<A HREF=\"user.php?id=$user->id\">$user->firstname $user->lastname</A>",
"$user->email",
"$user->city",
$COUNTRIES[$user->country]);
}
print_table($table);
print_footer();
exit;
}

if (! $user = get_record("user", "id", $id)) {
error("User ID was incorrect (can't find it)");
}


/// If data submitted, then process and store.

if (match_referer() && isset($HTTP_POST_VARS)) {


$usernew = (object)$HTTP_POST_VARS; $user->firstname = "New";
$user->lastname = "User";
$user->username = "username";
$user->password = "";
$user->email = "";
$user->confirmed = 1;
$user->timemodified = time();


if (find_form_errors($user, $usernew, $err) ) { if (! $user->id = insert_record("user", $user)) {
$user = $usernew; error("Could not create new user record !!!");
}


} else { redirect("$CFG->wwwroot/user/edit.php?id=$user->id&course=$course->id");



$timenow = time(); } else if ($id) { // Edit a particular user


if ($filename = valid_uploaded_file($imagefile)) { if (! $user = get_record("user", "id", $id)) {
$imageinfo = GetImageSize($filename); error("User ID was incorrect (can't find it)");
$image->width = $imageinfo[0]; }
$image->height = $imageinfo[1];
$image->type = $imageinfo[2];

switch ($image->type) {
case 2: $im = ImageCreateFromJPEG($filename); break;
case 3: $im = ImageCreateFromPNG($filename); break;
default: error("Image must be in JPG or PNG format");
}
if (function_exists("ImageCreateTrueColor")) {
$im1 = ImageCreateTrueColor(100,100);
$im2 = ImageCreateTrueColor(35,35);
} else {
$im1 = ImageCreate(100,100);
$im2 = ImageCreate(35,35);
}

$cx = $image->width / 2;
$cy = $image->height / 2;

if ($image->width < $image->height) {
$half = floor($image->width / 2.0);
} else {
$half = floor($image->height / 2.0);
}

if (!file_exists("$CFG->dataroot/users")) {
mkdir("$CFG->dataroot/users", 0777);
}
if (!file_exists("$CFG->dataroot/users/$USER->id")) {
mkdir("$CFG->dataroot/users/$USER->id", 0777);
}

ImageCopyBicubic($im1, $im, 0, 0, $cx-$half, $cy-$half, 100, 100, $half*2, $half*2);
ImageCopyBicubic($im2, $im, 0, 0, $cx-$half, $cy-$half, 35, 35, $half*2, $half*2);

// Draw borders over the top.
$black1 = ImageColorAllocate ($im1, 0, 0, 0);
$black2 = ImageColorAllocate ($im2, 0, 0, 0);
ImageLine ($im1, 0, 0, 0, 99, $black1);
ImageLine ($im1, 0, 99, 99, 99, $black1);
ImageLine ($im1, 99, 99, 99, 0, $black1);
ImageLine ($im1, 99, 0, 0, 0, $black1);
ImageLine ($im2, 0, 0, 0, 34, $black2);
ImageLine ($im2, 0, 34, 34, 34, $black2);
ImageLine ($im2, 34, 34, 34, 0, $black2);
ImageLine ($im2, 34, 0, 0, 0, $black2);

ImageJpeg($im1, "$CFG->dataroot/users/$USER->id/f1.jpg", 90);
ImageJpeg($im2, "$CFG->dataroot/users/$USER->id/f2.jpg", 95);
$usernew->picture = "1";
} else {
$usernew->picture = $user->picture;
}


if ($usernew->password) { redirect("$CFG->wwwroot/user/edit.php?id=$user->id&course=$course->id");
$usernew->password = md5($usernew->password);
} else {
unset($usernew->password); } else { // List all users for editing
}

if ($users = get_records_sql("SELECT * from user WHERE username <> 'guest' ORDER BY firstname")) {
$usernew->timemodified = time(); print_header("Edit users", "Edit users", "<A HREF=\"$CFG->wwwroot/admin\">Admin</A> -> Edit users", "");

print_heading("Choose a user to edit");
if (update_record("user", $usernew)) { $table->head = array ("Name", "Email", "City/Town", "Country");
if ($usernew->id == $USER->id) { // Reload admin $table->align = array ("LEFT", "LEFT", "CENTER", "CENTER");
$USER = get_user_info_from_db("id", $usernew->id); foreach ($users as $user) {
$USER->loggedin = true; $table->data[] = array ("<A HREF=\"user.php?id=$user->id\">$user->firstname $user->lastname</A>",
save_session("USER"); "$user->email",
set_moodle_cookie($USER->username); "$user->city",
} $COUNTRIES[$user->country]);
redirect("index.php", "Changes saved");
} else {
error("Could not update the user record ($user->id)");
} }
} print_table($table);
} } else {

error("No users found!");
/// Otherwise fill and print the form.

if (!$usernew) {
$usernew = $user;
$usernew->password = "";
}

print_header("Edit user profile", "Edit user profile", "<A HREF=\"$CFG->wwwroot/admin\">Admin</A> -> Edit user", "");

print_simple_box_start("center", "", "$THEME->cellheading");
echo "<H2>User profile for $usernew->firstname $usernew->lastname</H2>";
include("user.html");
print_simple_box_end();

print_footer();




/// FUNCTIONS ////////////////////

function find_form_errors(&$user, &$usernew, &$err) {

if (empty($usernew->email))
$err["email"] = "Missing email address";

else if (! validate_email($usernew->email))
$err["email"] = "Invalid email address, check carefully";

else if ($otheruser = get_record("user", "email", $usernew->email)) {
if ($otheruser->id <> $user->id) {
$err["email"] = "Email address already in use by someone else.";
} }
print_footer();
} }
$user->email = $usernew->email;

if (empty($user->password) && empty($usernew->password)) {
$err["password"] = "Must have a password";
}

if (empty($usernew->username))
$err["username"] = "Must have a username";

if (empty($usernew->firstname))
$err["firstname"] = "Must enter your first name";

if (empty($usernew->lastname))
$err["lastname"] = "Must enter your last name";

return count($err);
}



?> ?>
2 changes: 1 addition & 1 deletion course/delete.php
Expand Up @@ -17,7 +17,7 @@
$strdelete = get_string("deletecourse"); $strdelete = get_string("deletecourse");
print_header($strdelete, $strdelete, "<A HREF=\"$CFG->wwwroot/admin\">$stradmin</A> -> $strdelete"); print_header($strdelete, $strdelete, "<A HREF=\"$CFG->wwwroot/admin\">$stradmin</A> -> $strdelete");
if ($courses = get_records_sql("SELECT * from course WHERE category > 0 ORDER BY fullname")) { if ($courses = get_records_sql("SELECT * from course WHERE category > 0 ORDER BY fullname")) {
print_heading("choosecourse"); print_heading(get_string("choosecourse"));
print_simple_box_start("CENTER"); print_simple_box_start("CENTER");
foreach ($courses as $course) { foreach ($courses as $course) {
echo "<A HREF=\"delete.php?id=$course->id\">$course->fullname</A><BR>"; echo "<A HREF=\"delete.php?id=$course->id\">$course->fullname</A><BR>";
Expand Down

0 comments on commit a3447e1

Please sign in to comment.