2 // Depending on the current enrolment method, this page
3 // presents the user with whatever they need to know when
4 // they try to enrol in a course.
6 require_once("../config.php");
7 require_once("lib.php");
8 require_once("$CFG->dirroot/enrol/enrol.class.php");
10 $id = required_param('id', PARAM_INT
);
11 $loginasguest = optional_param('loginasguest', 0, PARAM_BOOL
); // hmm, is this still needed?
14 $wwwroot = $CFG->wwwroot
;
15 if (!empty($CFG->loginhttps
)) {
16 $wwwroot = str_replace('http:','https:', $wwwroot);
18 // do not use require_login here because we are usually comming from it
19 redirect($wwwroot.'/login/index.php');
22 if (! $course = get_record('course', 'id', $id) ) {
23 error("That's an invalid course id");
26 if (! $context = get_context_instance(CONTEXT_COURSE
, $course->id
) ) {
27 error("That's an invalid course id");
30 /// do not use when in course login as
31 if (!empty($USER->realuser
) and $USER->loginascontext
->contextlevel
== CONTEXT_COURSE
) {
32 print_error('loginasnoenrol', '', $CFG->wwwroot
.'/course/view.php?id='.$USER->loginascontext
->instanceid
);
35 $enrol = enrolment_factory
::factory($course->enrol
); // do not use if (!$enrol... here, it can not work in PHP4 - see MDL-7529
37 /// Refreshing all current role assignments for the current user
39 load_all_capabilities();
41 /// Double check just in case they are actually enrolled already and
42 /// thus got to this script by mistake. This might occur if enrolments
43 /// changed during this session or something
45 if (has_capability('moodle/course:view', $context) and !has_capability('moodle/legacy:guest', $context, NULL, false)) {
46 if (!empty($SESSION->wantsurl
)) {
47 $destination = $SESSION->wantsurl
;
48 unset($SESSION->wantsurl
);
50 $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
52 redirect($destination); // Bye!
55 /// Check if the course is a meta course (bug 5734)
56 if ($course->metacourse
) {
57 print_header_simple();
58 notice(get_string('coursenotaccessible'), "$CFG->wwwroot/index.php");
61 /// Users can't enroll to site course
62 if ($course->id
== SITEID
) {
63 print_header_simple();
64 notice(get_string('enrollfirst'), "$CFG->wwwroot/index.php");
67 /// Double check just in case they are enrolled to start in the future
69 if ($course->enrolperiod
) { // Only active if the course has an enrolment period in effect
70 if ($roles = get_user_roles($context, $USER->id
)) {
71 foreach ($roles as $role) {
72 if ($role->timestart
and ($role->timestart
>= time())) {
73 $message = get_string('enrolmentnotyet', '', userdate($student->timestart
));
75 notice($message, "$CFG->wwwroot/index.php");
81 /// Check if the course is enrollable
82 if (!method_exists($enrol, 'print_entry')) {
83 print_header_simple();
84 notice(get_string('enrolmentnointernal'), "$CFG->wwwroot/index.php");
87 if (!$course->enrollable ||
88 ($course->enrollable
== 2 && $course->enrolstartdate
> 0 && $course->enrolstartdate
> time()) ||
89 ($course->enrollable
== 2 && $course->enrolenddate
> 0 && $course->enrolenddate
<= time())
91 print_header($course->shortname
, $course->fullname
, build_navigation(array(array('name'=>$course->shortname
,'link'=>'','type'=>'misc'))) );
92 notice(get_string('notenrollable'), "$CFG->wwwroot/index.php");
95 /// Check the submitted enrolment information if there is any (eg could be enrolment key)
97 if ($form = data_submitted()) {
98 $enrol->check_entry($form, $course); // Should terminate/redirect in here if it's all OK
101 /// Otherwise, we print the entry form.
103 $enrol->print_entry($course);