More robust teacher adding/removing

This commit is contained in:
moodler 2003-08-17 08:51:55 +00:00
parent 6b42ea66a7
commit 3041b0f88a
2 changed files with 79 additions and 69 deletions

View File

@ -92,68 +92,25 @@
redirect("teacher.php?id=$course->id", get_string("changessaved"));
}
/// Get all existing teachers for this course.
$teachers = get_course_teachers($course->id);
/// Add a teacher if one is specified
if (!empty($_GET['add'])) {
if (!isteacher($course->id)){
error("You must be an administrator or teacher to modify this course.");
}
if (! $user = get_record("user", "id", $_GET['add'])) {
error("That teacher (id = $add) doesn't exist", "teacher.php?id=$course->id");
}
if (!empty($teachers)) {
foreach ($teachers as $tt) {
if ($tt->id == $user->id) {
error("That user is already a teacher for this course.", "teacher.php?id=$course->id");
}
}
}
$teacher->userid = $user->id;
$teacher->course = $course->id;
$teacher->editall = 1;
if (!empty($teachers)) {
$teacher->authority = 2;
} else {
$teacher->authority = 1; // First teacher is the main teacher
}
$teacher->id = insert_record("user_teachers", $teacher);
if (empty($teacher->id)) {
if (! add_teacher($add, $course->id)) {
error("Could not add that teacher to this course!");
}
$user->authority = $teacher->authority;
$user->editall = $teacher->editall;
$teachers[] = $user;
}
/// Remove a teacher if one is specified.
if (!empty($_GET['remove'])) {
if (!isteacher($course->id)){
error("You must be an administrator or teacher to modify this course.");
}
if (! $user = get_record("user", "id", $_GET['remove'])) {
error("That teacher (id = $remove) doesn't exist", "teacher.php?id=$course->id");
}
if (!empty($teachers)) {
foreach ($teachers as $key => $tt) {
if ($tt->id == $user->id) {
remove_teacher($user->id, $course->id);
unset($teachers[$key]);
}
}
if (! remove_teacher($remove, $course->id)) {
error("Could not add that teacher to this course!");
}
}
/// Display all current teachers for this course.
$teachers = get_course_teachers($course->id);
print_heading_with_help("$strexistingteachers $parateachers", "teachers");
if (empty($teachers)) {

View File

@ -613,25 +613,25 @@ function authenticate_user_login($username, $password) {
}
}
function enrol_student($userid, $courseid) {
/// Enrols a student in a given course
global $db;
if (!record_exists("user_students", "userid", $userid, "course", $courseid)) {
$student->userid = $userid;
$student->course = $courseid;
$student->start = 0;
$student->end = 0;
$student->time = time();
return insert_record("user_students", $student);
if (record_exists("user", "id", $userid)) {
$student->userid = $userid;
$student->course = $courseid;
$student->start = 0;
$student->end = 0;
$student->time = time();
return insert_record("user_students", $student);
}
return false;
}
return true;
}
function unenrol_student($user, $course=0) {
/// Unenrols a student from a given course
global $db;
if ($course) {
/// First delete any crucial stuff that might still send mail
@ -648,30 +648,83 @@ function unenrol_student($user, $course=0) {
}
}
function remove_teacher($user, $course=0) {
function add_teacher($userid, $courseid) {
/// Add a teacher to a given course
if (!record_exists("user_teachers", "userid", $userid, "course", $courseid)) {
if (record_exists("user", "id", $userid)) {
$teacher->userid = $userid;
$teacher->course = $courseid;
$teacher->editall = 1;
$teacher->role = "";
if (record_exists("user_teachers", "course", $courseid)) {
$teacher->authority = 2;
} else {
$teacher->authority = 1;
}
return insert_record("user_teachers", $teacher);
}
return false;
}
return true;
}
function remove_teacher($userid, $courseid=0) {
/// Removes a teacher from a given course (or ALL courses)
/// Does not delete the user account
global $db;
if ($course) {
if ($courseid) {
/// First delete any crucial stuff that might still send mail
if ($forums = get_records("forum", "course", $course)) {
if ($forums = get_records("forum", "course", $courseid)) {
foreach ($forums as $forum) {
delete_records("forum_subscriptions", "forum", $forum->id, "userid", $user);
delete_records("forum_subscriptions", "forum", $forum->id, "userid", $userid);
}
}
return delete_records("user_teachers", "userid", $user, "course", $course);
return delete_records("user_teachers", "userid", $userid, "course", $courseid);
} else {
delete_records("forum_subscriptions", "userid", $user);
return delete_records("user_teachers", "userid", $user);
delete_records("forum_subscriptions", "userid", $userid);
return delete_records("user_teachers", "userid", $userid);
}
}
function remove_admin($user) {
function add_creator($userid) {
/// Add a creator to the site
if (!record_exists("user_admins", "userid", $userid)) {
if (record_exists("user", "id", $userid)) {
$creator->userid = $userid;
return insert_record("user_coursecreators", $creator);
}
return false;
}
return true;
}
function remove_creator($userid) {
/// Removes a creator from a site
global $db;
return delete_records("user_coursecreators", "userid", $userid);
}
function add_admin($userid) {
/// Add an admin to the site
if (!record_exists("user_admins", "userid", $userid)) {
if (record_exists("user", "id", $userid)) {
$admin->userid = $userid;
return insert_record("user_admins", $admin);
}
return false;
}
return true;
}
function remove_admin($userid) {
/// Removes an admin from a site
global $db;
return delete_records("user_admins", "userid", $user);
return delete_records("user_admins", "userid", $userid);
}