Search moodle.org's
Developer Documentation


/enrol/ -> index.php (source)
   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  
  30  if (!isloggedin()) {
  31      $referer = clean_param(get_referer(), PARAM_LOCALURL);
  32      if (empty($referer)) {
  33          // A user that is not logged in has arrived directly on this page,
  34          // they should be redirected to the course page they are trying to enrol on after logging in.
  35          $SESSION->wantsurl = "$CFG->wwwroot/course/view.php?id=$id";
  36      }
  37      // do not use require_login here because we are usually coming from it,
  38      // it would also mess up the SESSION->wantsurl
  39      redirect(get_login_url());
  40  }
  41  
  42  $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
  43  $context = context_course::instance($course->id, MUST_EXIST);
  44  
  45  // Everybody is enrolled on the frontpage
  46  if ($course->id == SITEID) {
  47      redirect("$CFG->wwwroot/");
  48  }
  49  
  50  if (!$course->visible && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
  51      print_error('coursehidden');
  52  }
  53  
  54  $PAGE->set_course($course);
  55  $PAGE->set_pagelayout('course');
  56  $PAGE->set_url('/enrol/index.php', array('id'=>$course->id));
  57  
  58  // do not allow enrols when in login-as session
  59  if (\core\session\manager::is_loggedinas() and $USER->loginascontext->contextlevel == CONTEXT_COURSE) {
  60      print_error('loginasnoenrol', '', $CFG->wwwroot.'/course/view.php?id='.$USER->loginascontext->instanceid);
  61  }
  62  
  63  // get all enrol forms available in this course
  64  $enrols = enrol_get_plugins(true);
  65  $enrolinstances = enrol_get_instances($course->id, true);
  66  $forms = array();
  67  foreach($enrolinstances as $instance) {
  68      if (!isset($enrols[$instance->enrol])) {
  69          continue;
  70      }
  71      $form = $enrols[$instance->enrol]->enrol_page_hook($instance);
  72      if ($form) {
  73          $forms[$instance->id] = $form;
  74      }
  75  }
  76  
  77  // Check if user already enrolled
  78  if (is_enrolled($context, $USER, '', true)) {
  79      if (!empty($SESSION->wantsurl)) {
  80          $destination = $SESSION->wantsurl;
  81          unset($SESSION->wantsurl);
  82      } else {
  83          $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
  84      }
  85      redirect($destination);   // Bye!
  86  }
  87  
  88  $PAGE->set_title($course->shortname);
  89  $PAGE->set_heading($course->fullname);
  90  $PAGE->navbar->add(get_string('enrolmentoptions','enrol'));
  91  
  92  echo $OUTPUT->header();
  93  echo $OUTPUT->heading(get_string('enrolmentoptions','enrol'));
  94  
  95  $courserenderer = $PAGE->get_renderer('core', 'course');
  96  echo $courserenderer->course_info_box($course);
  97  
  98  //TODO: find if future enrolments present and display some info
  99  
 100  foreach ($forms as $form) {
 101      echo $form;
 102  }
 103  
 104  if (!$forms) {
 105      if (isguestuser()) {
 106          notice(get_string('noguestaccess', 'enrol'), get_login_url());
 107      } else {
 108          notice(get_string('notenrollable', 'enrol'), "$CFG->wwwroot/index.php");
 109      }
 110  }
 111  
 112  echo $OUTPUT->footer();

Search This Site: