MDL-67264 core_course: Activity chooser new feature
[moodle.git] / course / request.php
CommitLineData
d9cb06dc 1<?php
2
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.
14//
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/>.
77eddcd5 17
18/**
19 * Allows a user to request a course be created for them.
20 *
d9cb06dc 21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
77eddcd5 23 * @package course
d9cb06dc 24 */
25
1fcf0ca8 26require_once(__DIR__ . '/../config.php');
8bdc9cac 27require_once($CFG->dirroot . '/course/lib.php');
d9cb06dc 28require_once($CFG->dirroot . '/course/request_form.php');
77eddcd5 29
484c4c6c
SH
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);
3e15abe5 33$categoryid = optional_param('category', null, PARAM_INT);
484c4c6c
SH
34if ($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');
39}
77eddcd5 40
484c4c6c 41$PAGE->set_url($url);
f8f384b5 42
484c4c6c 43// Check permissions.
c6fb5a7e 44require_login(null, false);
d9cb06dc 45if (isguestuser()) {
46 print_error('guestsarenotallowed', '', $returnurl);
47}
48if (empty($CFG->enablecourserequests)) {
49 print_error('courserequestdisabled', '', $returnurl);
50}
3e15abe5 51
959e4f0e
MG
52if ($CFG->lockrequestcategory) {
53 // Course request category is locked, user will always request in the default request category.
3e15abe5
MG
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);
60}
61
62$context = context_coursecat::instance($categoryid ?: $CFG->defaultrequestcategory);
f8f384b5
DP
63$PAGE->set_context($context);
64require_capability('moodle/course:request', $context);
b6241ce2 65
484c4c6c 66// Set up the form.
3e15abe5
MG
67$data = $categoryid ? (object)['category' => $categoryid] : null;
68$data = course_request::prepare($data);
6f6ea8c3 69$requestform = new course_request_form($url);
8bdc9cac 70$requestform->set_data($data);
b6241ce2 71
d9cb06dc 72$strtitle = get_string('courserequest');
8bdc9cac
SH
73$PAGE->set_title($strtitle);
74$PAGE->set_heading($strtitle);
e452210a 75
484c4c6c 76// Standard form processing if statement.
d9cb06dc 77if ($requestform->is_cancelled()){
78 redirect($returnurl);
0a122046 79
aa6c1ced 80} else if ($data = $requestform->get_data()) {
8bdc9cac 81 $request = course_request::create($data);
d9cb06dc 82
484c4c6c 83 // And redirect back to the course listing.
d9cb06dc 84 notice(get_string('courserequestsuccess'), $returnurl);
85}
86
d9cb06dc 87$PAGE->navbar->add($strtitle);
d9cb06dc 88echo $OUTPUT->header();
89echo $OUTPUT->heading($strtitle);
8bdc9cac 90// Show the request form.
d9cb06dc 91$requestform->display();
f8f384b5 92echo $OUTPUT->footer();