Merge branch 'MDL-80544' of https://github.com/paulholden/moodle
[moodle.git] / course / request.php
blobd27e860a5f8fd525322d5c83fe22db8e3baba8ed
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Allows a user to request a course be created for them.
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package course
26 require_once(__DIR__ . '/../config.php');
27 require_once($CFG->dirroot . '/course/lib.php');
28 require_once($CFG->dirroot . '/course/request_form.php');
30 // Where we came from. Used in a number of redirects.
31 $url = new moodle_url('/course/request.php');
32 $return = optional_param('return', null, PARAM_ALPHANUMEXT);
33 $categoryid = optional_param('category', null, PARAM_INT);
34 if ($return === 'management') {
35 $url->param('return', $return);
36 $returnurl = new moodle_url('/course/management.php', array('categoryid' => $CFG->defaultrequestcategory));
37 } else {
38 $returnurl = new moodle_url('/course/index.php');
41 $PAGE->set_url($url);
43 // Check permissions.
44 require_login(null, false);
45 if (isguestuser()) {
46 throw new \moodle_exception('guestsarenotallowed', '', $returnurl);
48 if (empty($CFG->enablecourserequests)) {
49 throw new \moodle_exception('courserequestdisabled', '', $returnurl);
52 if ($CFG->lockrequestcategory) {
53 // Course request category is locked, user will always request in the default request category.
54 $categoryid = null;
55 } else if (!$categoryid) {
56 // Category selection is enabled but category is not specified.
57 // Find a category where user has capability to request courses (preferably the default category).
58 $list = core_course_category::make_categories_list('moodle/course:request');
59 $categoryid = array_key_exists($CFG->defaultrequestcategory, $list) ? $CFG->defaultrequestcategory : key($list);
62 $context = context_coursecat::instance($categoryid ?: $CFG->defaultrequestcategory);
63 $PAGE->set_context($context);
64 require_capability('moodle/course:request', $context);
66 // Set up the form.
67 $data = $categoryid ? (object)['category' => $categoryid] : null;
68 $data = course_request::prepare($data);
69 $requestform = new course_request_form($url);
70 $requestform->set_data($data);
72 $strtitle = get_string('courserequest');
73 $PAGE->set_title($strtitle);
74 $coursecategory = core_course_category::get($categoryid, MUST_EXIST, true);
75 $PAGE->set_heading($coursecategory->get_formatted_name());
76 $PAGE->set_primary_active_tab('home');
78 // Standard form processing if statement.
79 if ($requestform->is_cancelled()){
80 redirect($returnurl);
82 } else if ($data = $requestform->get_data()) {
83 $request = course_request::create($data);
85 // And redirect back to the course listing.
86 notice(get_string('courserequestsuccess'), $returnurl);
89 $categoryurl = new moodle_url('/course/index.php');
90 if ($categoryid) {
91 $categoryurl->param('categoryid', $categoryid);
93 navigation_node::override_active_url($categoryurl);
95 $PAGE->navbar->add($strtitle);
96 echo $OUTPUT->header();
97 echo $OUTPUT->heading($strtitle);
98 // Show the request form.
99 $requestform->display();
100 echo $OUTPUT->footer();