forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenrol.php
102 lines (80 loc) · 3.65 KB
/
enrol.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php // $Id$
// Depending on the current enrolment method, this page
// presents the user with whatever they need to know when
// they try to enrol in a course.
require_once("../config.php");
require_once("lib.php");
require_once("$CFG->dirroot/enrol/enrol.class.php");
$id = required_param('id', PARAM_INT);
$loginasguest = optional_param('loginasguest', 0, PARAM_BOOL); // hmm, is this still needed?
if (!isloggedin()) {
$wwwroot = $CFG->wwwroot;
if (!empty($CFG->loginhttps)) {
$wwwroot = str_replace('http:','https:', $wwwroot);
}
// do not use require_login here because we are usually comming from it
redirect($wwwroot.'/login/index.php');
}
if (! $course = get_record('course', 'id', $id) ) {
error("That's an invalid course id");
}
if (! $context = get_context_instance(CONTEXT_COURSE, $course->id) ) {
error("That's an invalid course id");
}
$enrol = enrolment_factory::factory($course->enrol); // do not use if (!$enrol... here, it can not work in PHP4 - see MDL-7529
/// Refreshing all current role assignments for the current user
load_all_capabilities();
/// Double check just in case they are actually enrolled already and
/// 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) and !has_capability('moodle/legacy:guest', $context, NULL, false)) {
if ($SESSION->wantsurl) {
$destination = $SESSION->wantsurl;
unset($SESSION->wantsurl);
} else {
$destination = "$CFG->wwwroot/course/view.php?id=$course->id";
}
redirect($destination); // Bye!
}
/// Check if the course is a meta course (bug 5734)
if ($course->metacourse) {
print_header_simple();
notice(get_string('coursenotaccessible'), "$CFG->wwwroot/index.php");
}
/// Users can't enroll to site course
if ($course->id == SITEID) {
print_header_simple();
notice(get_string('enrollfirst'), "$CFG->wwwroot/index.php");
}
/// Double check just in case they are enrolled to start in the future
if ($course->enrolperiod) { // Only active if the course has an enrolment period in effect
if ($roles = get_user_roles($context, $USER->id)) {
foreach ($roles as $role) {
if ($role->timestart and ($role->timestart >= time())) {
$message = get_string('enrolmentnotyet', '', userdate($student->timestart));
print_header();
notice($message, "$CFG->wwwroot/index.php");
}
}
}
}
/// Check if the course is enrollable
if (!method_exists($enrol, 'print_entry')) {
print_header_simple();
notice(get_string('enrolmentnointernal'), "$CFG->wwwroot/index.php");
}
if (!$course->enrollable ||
($course->enrollable == 2 && $course->enrolstartdate > 0 && $course->enrolstartdate > time()) ||
($course->enrollable == 2 && $course->enrolenddate > 0 && $course->enrolenddate <= time())
) {
print_header($course->shortname, $course->fullname, $course->shortname );
notice(get_string('notenrollable'), "$CFG->wwwroot/index.php");
}
/// Check the submitted enrolment information if there is any (eg could be enrolment key)
if ($form = data_submitted()) {
$enrol->check_entry($form, $course); // Should terminate/redirect in here if it's all OK
}
/// Otherwise, we print the entry form.
$enrol->print_entry($course);
/// Easy!
?>