Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
/enrol/ -> index.php (source)

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This page shows all course enrolment options for current user.
  19   *
  20   * @package    core_enrol
  21   * @copyright  2010 Petr Skoda {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require('../config.php');
  26  require_once("$CFG->libdir/formslib.php");
  27  
  28  $id = required_param('id', PARAM_INT);
  29  $returnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
  30  
  31  if (!isloggedin()) {
  32      $referer = get_local_referer();
  33      if (empty($referer)) {
  34          // A user that is not logged in has arrived directly on this page,
  35          // they should be redirected to the course page they are trying to enrol on after logging in.
  36          $SESSION->wantsurl = "$CFG->wwwroot/course/view.php?id=$id";
  37      }
  38      // do not use require_login here because we are usually coming from it,
  39      // it would also mess up the SESSION->wantsurl
  40      redirect(get_login_url());
  41  }
  42  
  43  $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
  44  $context = context_course::instance($course->id, MUST_EXIST);
  45  
  46  // Everybody is enrolled on the frontpage
  47  if ($course->id == SITEID) {
  48      redirect("$CFG->wwwroot/");
  49  }
  50  
  51  if (!$course->visible && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
  52      print_error('coursehidden');
  53  }
  54  
  55  $PAGE->set_course($course);
  56  $PAGE->set_pagelayout('incourse');
  57  $PAGE->set_url('/enrol/index.php', array('id'=>$course->id));
  58  
  59  // do not allow enrols when in login-as session
  60  if (\core\session\manager::is_loggedinas() and $USER->loginascontext->contextlevel == CONTEXT_COURSE) {
  61      print_error('loginasnoenrol', '', $CFG->wwwroot.'/course/view.php?id='.$USER->loginascontext->instanceid);
  62  }
  63  
  64  // Check if user has access to the category where the course is located.
  65  if (!core_course_category::can_view_course_info($course) && !is_enrolled($context, $USER, '', true)) {
  66      print_error('coursehidden', '', $CFG->wwwroot . '/');
  67  }
  68  
  69  // get all enrol forms available in this course
  70  $enrols = enrol_get_plugins(true);
  71  $enrolinstances = enrol_get_instances($course->id, true);
  72  $forms = array();
  73  foreach($enrolinstances as $instance) {
  74      if (!isset($enrols[$instance->enrol])) {
  75          continue;
  76      }
  77      $form = $enrols[$instance->enrol]->enrol_page_hook($instance);
  78      if ($form) {
  79          $forms[$instance->id] = $form;
  80      }
  81  }
  82  
  83  // Check if user already enrolled
  84  if (is_enrolled($context, $USER, '', true)) {
  85      if (!empty($SESSION->wantsurl)) {
  86          $destination = $SESSION->wantsurl;
  87          unset($SESSION->wantsurl);
  88      } else {
  89          $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
  90      }
  91      redirect($destination);   // Bye!
  92  }
  93  
  94  $PAGE->set_title($course->shortname);
  95  $PAGE->set_heading($course->fullname);
  96  $PAGE->navbar->add(get_string('enrolmentoptions','enrol'));
  97  
  98  echo $OUTPUT->header();
  99  echo $OUTPUT->heading(get_string('enrolmentoptions','enrol'));
 100  
 101  $courserenderer = $PAGE->get_renderer('core', 'course');
 102  echo $courserenderer->course_info_box($course);
 103  
 104  //TODO: find if future enrolments present and display some info
 105  
 106  foreach ($forms as $form) {
 107      echo $form;
 108  }
 109  
 110  if (!$forms) {
 111      if (isguestuser()) {
 112          notice(get_string('noguestaccess', 'enrol'), get_login_url());
 113      } else if ($returnurl) {
 114          notice(get_string('notenrollable', 'enrol'), $returnurl);
 115      } else {
 116          $url = get_local_referer(false);
 117          if (empty($url)) {
 118              $url = new moodle_url('/index.php');
 119          }
 120          notice(get_string('notenrollable', 'enrol'), $url);
 121      }
 122  }
 123  
 124  echo $OUTPUT->footer();