/// thus got to this script by mistake. This might occur if enrolments
/// changed during this session or something
- if (has_capability('moodle/course:view', $context)) {
+ if (has_capability('moodle/course:view', $context) and !has_capability('moodle/legacy:guest', $context)) {
if ($SESSION->wantsurl) {
$destination = $SESSION->wantsurl;
unset($SESSION->wantsurl);
$groupid = $this->check_group_entry($course->id, $form->password);
+ $context = get_context_instance(CONTEXT_COURSE, $course->id);
+
if (($form->password == $course->password) or ($groupid !== false) ) {
if (has_capability('moodle/legacy:guest', $context, $USER->id, false)) {
+ $USER->enrolkey[$course->id] = true;
add_to_log($course->id, 'course', 'guest', 'view.php?id='.$course->id, getremoteaddr());
} else { /// Update or add new enrolment
break;
- case 2: /// Guests allowed with key (drop through to logic below)
+ case 2: /// Guests allowed with key
+ if (!empty($USER->enrolkey[$course->id])) { // Set by enrol/manual/enrol.php
+ return true;
+ }
+ // otherwise drop through to logic below (--> enrol.php)
break;
default: /// Guests not allowed