role_unassign($roleid, $user->id, null, $context->id);
}
- /*
- switch ($fields[1]) {
- case "student":
- if ($fields[0] == "add") {
- if (! enrol_student($user->id, $course->id, $fields[4], $fields[5], 'flatfile')) {
- $elog = "Error enrolling in course\n";
- }
- } else {
- if (! unenrol_student($user->id, $course->id)) {
- $elog = "Error unenrolling from course\n";
- }
- }
- break;
-
- case "teacher":
- if ($fields[0] == "add") {
- if (! add_teacher($user->id, $course->id, 0, '', $fields[4], $fields[5], 'flatfile')) {
- $elog = "Error adding teacher to course\n";
- }
- } else {
- if (! remove_teacher($user->id, $course->id)) {
- $elog = "Error removing teacher from course\n";
- }
- }
- break;
-
- case "teacheredit":
- if ($fields[0] == "add") {
- if (! add_teacher($user->id, $course->id, 1, '', $fields[4], $fields[5], 'flatfile')) {
- $elog = "Error adding teacher to course\n";
- }
- } else {
- if (! remove_teacher($user->id, $course->id)) {
- $elog = "Error removing teacher from course\n";
- }
- }
- break;
-
- default: // should never get here as checks made above for correct values of $fields[1]
-
- } // end of switch*/
-
-
if ( empty($elog) and ($fields[0] == "add") ) {
return has_capability('moodle/legacy:guest', $context, $userid, false);
}
-/**
- * Enrols (or re-enrols) a student in a given course
- *
- * NOTE: Defaults to 'manual' enrolment - enrolment plugins
- * must set it explicitly.
- *
- * @uses $CFG
- * @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
- * @param int $courseid The id of the course that is being viewed
- * @param int $timestart ?
- * @param int $timeend ?
- * @param string $enrol ?
- * @return bool
- */
-function enrol_student($userid, $courseid, $timestart=0, $timeend=0, $enrol='manual') {
-
- global $CFG;
-
- if (!$user = get_record('user', 'id', $userid)) { // Check user
- return false;
- }
-
- if (!$roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) {
- return false;
- }
-
- $role = array_shift($roles); // We can only use one, let's use the first one
-
- if (!$context = get_context_instance(CONTEXT_COURSE, $courseid)) {
- return false;
- }
-
- $res = role_assign($role->id, $user->id, 0, $context->id, $timestart, $timeend, 0, $enrol);
-
- return $res;
-}
-
-/**
- * Unenrols a student from a given course
- *
- * @param int $courseid The id of the course that is being viewed, if any
- * @param int $userid The id of the user that is being tested against.
- * @return bool
- */
-function unenrol_student($userid, $courseid=0) {
- global $CFG;
-
- $status = true;
-
- if ($courseid) {
- /// First delete any crucial stuff that might still send mail
- if ($forums = get_records('forum', 'course', $courseid)) {
- foreach ($forums as $forum) {
- delete_records('forum_subscriptions', 'forum', $forum->id, 'userid', $userid);
- }
- }
- /// remove from all legacy student roles
- if ($courseid == SITEID) {
- $context = get_context_instance(CONTEXT_SYSTEM);
- } else if (!$context = get_context_instance(CONTEXT_COURSE, $courseid)) {
- return false;
- }
- if (!$roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) {
- return false;
- }
- foreach($roles as $role) {
- $status = role_unassign($role->id, $userid, 0, $context->id) and $status;
- }
- } else {
- // recursivelly unenroll student from all courses
- if ($courses = get_records('course')) {
- foreach($courses as $course) {
- $status = unenrol_student($userid, $course->id) and $status;
- }
- }
- }
-
- return $status;
-}
-
/**
* Add a teacher to a given course
*