Automatic installer.php lang files by installer_builder (20080623)
[moodle.git] / course / enrol.php
blob61419cccf24dc2cc7cbcfd2f7546a66e446d50f6
1 <?php // $Id$
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?
13 if (!isloggedin()) {
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 $enrol = enrolment_factory::factory($course->enrol); // do not use if (!$enrol... here, it can not work in PHP4 - see MDL-7529
32 /// Refreshing all current role assignments for the current user
34 load_all_capabilities();
36 /// Double check just in case they are actually enrolled already and
37 /// thus got to this script by mistake. This might occur if enrolments
38 /// changed during this session or something
40 if (has_capability('moodle/course:view', $context) and !has_capability('moodle/legacy:guest', $context, NULL, false)) {
41 if ($SESSION->wantsurl) {
42 $destination = $SESSION->wantsurl;
43 unset($SESSION->wantsurl);
44 } else {
45 $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
47 redirect($destination); // Bye!
50 /// Check if the course is a meta course (bug 5734)
51 if ($course->metacourse) {
52 print_header_simple();
53 notice(get_string('coursenotaccessible'), "$CFG->wwwroot/index.php");
56 /// Users can't enroll to site course
57 if ($course->id == SITEID) {
58 print_header_simple();
59 notice(get_string('enrollfirst'), "$CFG->wwwroot/index.php");
62 /// Double check just in case they are enrolled to start in the future
64 if ($course->enrolperiod) { // Only active if the course has an enrolment period in effect
65 if ($roles = get_user_roles($context, $USER->id)) {
66 foreach ($roles as $role) {
67 if ($role->timestart and ($role->timestart >= time())) {
68 $message = get_string('enrolmentnotyet', '', userdate($student->timestart));
69 print_header();
70 notice($message, "$CFG->wwwroot/index.php");
76 /// Check if the course is enrollable
77 if (!method_exists($enrol, 'print_entry')) {
78 print_header_simple();
79 notice(get_string('enrolmentnointernal'), "$CFG->wwwroot/index.php");
82 if (!$course->enrollable ||
83 ($course->enrollable == 2 && $course->enrolstartdate > 0 && $course->enrolstartdate > time()) ||
84 ($course->enrollable == 2 && $course->enrolenddate > 0 && $course->enrolenddate <= time())
85 ) {
86 print_header($course->shortname, $course->fullname, $course->shortname );
87 notice(get_string('notenrollable'), "$CFG->wwwroot/index.php");
90 /// Check the submitted enrolment information if there is any (eg could be enrolment key)
92 if ($form = data_submitted()) {
93 $enrol->check_entry($form, $course); // Should terminate/redirect in here if it's all OK
96 /// Otherwise, we print the entry form.
98 $enrol->print_entry($course);
100 /// Easy!