PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/course/request.php

http://github.com/moodle/moodle
PHP | 92 lines | 48 code | 14 blank | 30 comment | 10 complexity | df44480adefa6e661e4d32e9d8a618e9 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * Allows a user to request a course be created for them.
  18. *
  19. * @copyright 1999 Martin Dougiamas http://dougiamas.com
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package course
  22. */
  23. require_once(__DIR__ . '/../config.php');
  24. require_once($CFG->dirroot . '/course/lib.php');
  25. require_once($CFG->dirroot . '/course/request_form.php');
  26. // Where we came from. Used in a number of redirects.
  27. $url = new moodle_url('/course/request.php');
  28. $return = optional_param('return', null, PARAM_ALPHANUMEXT);
  29. $categoryid = optional_param('category', null, PARAM_INT);
  30. if ($return === 'management') {
  31. $url->param('return', $return);
  32. $returnurl = new moodle_url('/course/management.php', array('categoryid' => $CFG->defaultrequestcategory));
  33. } else {
  34. $returnurl = new moodle_url('/course/index.php');
  35. }
  36. $PAGE->set_url($url);
  37. // Check permissions.
  38. require_login(null, false);
  39. if (isguestuser()) {
  40. print_error('guestsarenotallowed', '', $returnurl);
  41. }
  42. if (empty($CFG->enablecourserequests)) {
  43. print_error('courserequestdisabled', '', $returnurl);
  44. }
  45. if ($CFG->lockrequestcategory) {
  46. // Course request category is locked, user will always request in the default request category.
  47. $categoryid = null;
  48. } else if (!$categoryid) {
  49. // Category selection is enabled but category is not specified.
  50. // Find a category where user has capability to request courses (preferably the default category).
  51. $list = core_course_category::make_categories_list('moodle/course:request');
  52. $categoryid = array_key_exists($CFG->defaultrequestcategory, $list) ? $CFG->defaultrequestcategory : key($list);
  53. }
  54. $context = context_coursecat::instance($categoryid ?: $CFG->defaultrequestcategory);
  55. $PAGE->set_context($context);
  56. require_capability('moodle/course:request', $context);
  57. // Set up the form.
  58. $data = $categoryid ? (object)['category' => $categoryid] : null;
  59. $data = course_request::prepare($data);
  60. $requestform = new course_request_form($url);
  61. $requestform->set_data($data);
  62. $strtitle = get_string('courserequest');
  63. $PAGE->set_title($strtitle);
  64. $PAGE->set_heading($strtitle);
  65. // Standard form processing if statement.
  66. if ($requestform->is_cancelled()){
  67. redirect($returnurl);
  68. } else if ($data = $requestform->get_data()) {
  69. $request = course_request::create($data);
  70. // And redirect back to the course listing.
  71. notice(get_string('courserequestsuccess'), $returnurl);
  72. }
  73. $PAGE->navbar->add($strtitle);
  74. echo $OUTPUT->header();
  75. echo $OUTPUT->heading($strtitle);
  76. // Show the request form.
  77. $requestform->display();
  78. echo $OUTPUT->footer();