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
);
15 if (! $course = get_record("course", "id", $id) ) {
16 error("That's an invalid course id");
19 if (! $site = get_site()) {
20 error("Could not find a site!");
23 check_for_restricted_user($USER->username
);
25 /// Refreshing enrolment data in the USER session
26 if (!($plugins = explode(',', $CFG->enrol_plugins_enabled
))) {
27 $plugins = array($CFG->enrol
);
29 require_once($CFG->dirroot
.'/enrol/enrol.class.php');
30 foreach ($plugins as $p) {
31 $enrol = enrolment_factory
::factory($p);
32 if (method_exists($enrol, 'get_student_courses')) {
33 $enrol->get_student_courses($USER);
35 if (method_exists($enrol, 'get_teacher_courses')) {
36 $enrol->get_teacher_courses($USER);
41 $enrol = enrolment_factory
::factory($course->enrol
);
43 /// Double check just in case they are actually enrolled already
44 /// This might occur if they were enrolled during this session
45 /// also happens when course is unhidden after student logs in
47 if ( !empty($USER->student
[$course->id
]) or !empty($USER->teacher
[$course->id
]) ) {
49 if ($SESSION->wantsurl
) {
50 $destination = $SESSION->wantsurl
;
51 unset($SESSION->wantsurl
);
53 $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
56 redirect($destination);
59 /// Check if the course is a meta course
60 /// moved here to fix bug 5734
61 if ($course->metacourse
) {
62 print_header_simple();
63 notice(get_string('coursenotaccessible'), "$CFG->wwwroot/index.php");
66 /// Users can't enroll to site course
67 if (!$course->category
) {
68 print_header_simple();
69 notice(get_string('enrollfirst'), "$CFG->wwwroot/index.php");
72 /// Double check just in case they are enrolled to start in the future
74 if ($student = get_record('user_students', 'userid', $USER->id
, 'course', $course->id
)) {
75 if ($course->enrolperiod
and $student->timestart
and ($student->timestart
>= time())) {
76 $message = get_string('enrolmentnotyet', '', userdate($student->timestart
));
78 notice($message, "$CFG->wwwroot/index.php");
82 /// Check if the course is enrollable
83 if (!method_exists($enrol, 'print_entry')) {
84 print_header_simple();
85 notice(get_string('enrolmentnointernal'), "$CFG->wwwroot/index.php");
88 if (!$course->enrollable ||
89 ($course->enrollable
== 2 && $course->enrolstartdate
> 0 && $course->enrolstartdate
> time()) ||
90 ($course->enrollable
== 2 && $course->enrolenddate
> 0 && $course->enrolenddate
<= time())
92 print_header($course->shortname
, $course->fullname
, $course->shortname
);
93 notice(get_string('notenrollable'), "$CFG->wwwroot/index.php");
96 /// Check the submitted enrollment key if there is one
98 if ($form = data_submitted()) {
99 //User is not enrolled in the course, wants to access course content
100 //as a guest, and course setting allow unlimited guest access
102 //the original idea was to use "loginas" feature, but require_login() would have to be changed
103 //and we would have to explain it to all users - it is now plain login action
104 if ($loginasguest and !empty($CFG->guestloginbutton
) and ($course->guest
==1 or $course->guest
==2)) {
105 if (isset($SESSION->currentgroup
)) {
106 unset($SESSION->currentgroup
);
108 $USER = get_complete_user_data('username', 'guest'); // get full guest user data
109 add_to_log(SITEID
, 'user', 'login', "view.php?id=$USER->id&course=".SITEID
, $USER->id
, 0, $USER->id
);
110 if ($SESSION->wantsurl
) {
111 $destination = $SESSION->wantsurl
;
112 unset($SESSION->wantsurl
);
114 $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
116 redirect($destination);
118 $enrol->check_entry($form, $course);
121 $enrol->print_entry($course);