From dbb24f689d6f6bca35bfda6c7156b13d3690df78 Mon Sep 17 00:00:00 2001 From: martin Date: Thu, 29 Nov 2001 05:46:15 +0000 Subject: [PATCH] Basic script to add/delete teachers of courses --- admin/teacher.php | 140 ++++++++++++++++++++++++++++++---------------- 1 file changed, 91 insertions(+), 49 deletions(-) diff --git a/admin/teacher.php b/admin/teacher.php index 4aec93f917..f274272cb7 100644 --- a/admin/teacher.php +++ b/admin/teacher.php @@ -12,7 +12,7 @@ require_login(); if (!isadmin()) { - error("You must be an administrator to edit users this way."); + error("You must be an administrator to use this page."); } if (!$id) { @@ -34,75 +34,117 @@ } -/// If data submitted, then process and store. + print_header("Add teachers to $course->shortname", "Add teachers to a course", "wwwroot/admin\">Admin -> Add teachers to $course->shortname", ""); - if (match_referer() && isset($HTTP_POST_VARS)) { - $usernew = (object)$HTTP_POST_VARS; +/// 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"); - if (find_form_errors($user, $usernew, $err) ) { - $user = $usernew; +/// Add a teacher if one is specified - } else { - - $usernew->timemodified = time(); + if ($add) { + if (! $user = get_record("user", "id", $add)) { + error("That teacher (id = $add) doesn't exist", "teacher.php?id=$course->id"); + } - if (update_record("user", $usernew)) { - redirect("index.php", "Changes saved"); - } else { - error("Could not update the user record ($user->id)"); + foreach ($teachers as $tt) { + if ($tt->id == $user->id) { + error("That user is already a teacher for this course.", "teacher.php?id=$course->id"); } - } - } - -/// Otherwise fill and print the form. - - XXXXXXX - - print_header("Edit user profile", "Edit user profile", "wwwroot/admin\">Admin -> Edit user", ""); - - print_simple_box_start("center", "", "$THEME->cellheading"); - echo "

User profile for $usernew->firstname $usernew->lastname

"; - include("user.html"); - print_simple_box_end(); + } - print_footer(); + $teacher->user = $user->id; + $teacher->course = $course->id; + if ($teachers) { + $teacher->authority = 2; + } else { + $teacher->authority = 1; // First teacher is the main teacher + } + $teacher->id = insert_record("user_teachers", $teacher); + if (! $teacher->id) { + error("Could not add that teacher to this course!"); + } + $user->authority = $teacher->authority; + $teachers[] = $user; + } +/// Remove a teacher if one is specified. + if ($remove) { + if (! $user = get_record("user", "id", $remove)) { + error("That teacher (id = $remove) doesn't exist", "teacher.php?id=$course->id"); + } + foreach ($teachers as $tt) { + if ($tt->id == $user->id) { + delete_records("user_teachers", "id", "$tt->teachid"); + } + } + $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"); + } -/// FUNCTIONS //////////////////// +/// Show existing teachers for this course -function find_form_errors(&$user, &$usernew, &$err) { + if ($teachers) { + print_simple_box_start("center", "", "$THEME->cellheading"); + print_heading("Existing teachers"); + foreach ($teachers as $teacher) { + echo "
  • $teacher->firstname $teacher->lastname, $teacher->email    id&remove=$teacher->id\">remove"; + } + print_simple_box_end(); + } - if (empty($usernew->email)) - $err["email"] = "Missing email address"; +/// Print list of potential teachers - else if (! validate_email($usernew->email)) - $err["email"] = "Invalid email address, check carefully"; + echo "
    "; + print_simple_box_start("center", "", "$THEME->cellcontent"); + print_heading("Potential teachers"); - else if ($otheruser = get_record("user", "email", $usernew->email)) { - if ($otheruser->id <> $user->id) { - $err["email"] = "Email address already in use by someone else."; - } + if ($search) { + $users = get_records_sql("SELECT * from user WHERE confirmed = 1 + AND (firstname LIKE '%$search%' OR + lastname LIKE '%$search%' OR + email LIKE '%$search%')"); + } else { + $users = get_records("user", "confirmed", "1"); } - $user->email = $usernew->email; - if (empty($user->password) && empty($usernew->password)) { - $err["password"] = "Must have a password"; + + foreach ($users as $user) { // Remove users who are already teachers + foreach ($teachers as $teacher) { + if ($teacher->id == $user->id) { + continue 2; + } + } + $potential[] = $user; } - if (empty($usernew->username)) - $err["username"] = "Must have a username"; - - if (empty($usernew->firstname)) - $err["firstname"] = "Must enter your first name"; + if (! $potential) { + echo "No potential teachers"; - if (empty($usernew->lastname)) - $err["lastname"] = "Must enter your last name"; + } else { + if (count($potential) <= 20) { + foreach ($potential as $user) { + echo "
  • $user->firstname $user->lastname, $user->email    id&add=$user->id\">add"; + } + } else { + echo "There are too many users to show.
    Enter a search word here."; + echo "
    "; + echo "id\">"; + echo ""; + echo ""; + echo "
    "; + } + } - return count($err); -} + print_simple_box_end(); + print_footer(); ?> -- 2.39.5