PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/course/request_form.php

https://bitbucket.org/moodle/moodle
PHP | 151 lines | 89 code | 27 blank | 35 comment | 11 complexity | 52ab9a554e8f0d4dfeb858e77b2d5d66 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. ///////////////////////////////////////////////////////////////////////////
  3. // //
  4. // NOTICE OF COPYRIGHT //
  5. // //
  6. // Moodle - Modular Object-Oriented Dynamic Learning Environment //
  7. // http://moodle.org //
  8. // //
  9. // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
  10. // //
  11. // This program is free software; you can redistribute it and/or modify //
  12. // it under the terms of the GNU General Public License as published by //
  13. // the Free Software Foundation; either version 2 of the License, or //
  14. // (at your option) any later version. //
  15. // //
  16. // This program is distributed in the hope that it will be useful, //
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  19. // GNU General Public License for more details: //
  20. // //
  21. // http://www.gnu.org/copyleft/gpl.html //
  22. // //
  23. ///////////////////////////////////////////////////////////////////////////
  24. /**
  25. * Forms associated with requesting courses, and having requests approved.
  26. * Note that several related forms are defined in this one file.
  27. *
  28. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  29. * @package course
  30. */
  31. if (!defined('MOODLE_INTERNAL')) {
  32. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  33. }
  34. require_once($CFG->libdir.'/formslib.php');
  35. /**
  36. * A form for a user to request a course.
  37. */
  38. class course_request_form extends moodleform {
  39. function definition() {
  40. global $CFG, $DB, $USER;
  41. $mform =& $this->_form;
  42. if ($pending = $DB->get_records('course_request', array('requester' => $USER->id))) {
  43. $mform->addElement('header', 'pendinglist', get_string('coursespending'));
  44. $list = array();
  45. foreach ($pending as $cp) {
  46. $list[] = format_string($cp->fullname);
  47. }
  48. $list = implode(', ', $list);
  49. $mform->addElement('static', 'pendingcourses', get_string('courses'), $list);
  50. }
  51. $mform->addElement('header','coursedetails', get_string('courserequestdetails'));
  52. $mform->addElement('text', 'fullname', get_string('fullnamecourse'), 'maxlength="254" size="50"');
  53. $mform->addHelpButton('fullname', 'fullnamecourse');
  54. $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
  55. $mform->setType('fullname', PARAM_TEXT);
  56. $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
  57. $mform->addHelpButton('shortname', 'shortnamecourse');
  58. $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
  59. $mform->setType('shortname', PARAM_TEXT);
  60. if (empty($CFG->lockrequestcategory)) {
  61. $displaylist = core_course_category::make_categories_list('moodle/course:request');
  62. $mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
  63. $mform->addRule('category', null, 'required', null, 'client');
  64. $mform->setDefault('category', $CFG->defaultrequestcategory);
  65. $mform->addHelpButton('category', 'coursecategory');
  66. }
  67. $mform->addElement('editor', 'summary_editor', get_string('summary'), null, course_request::summary_editor_options());
  68. $mform->addHelpButton('summary_editor', 'coursesummary');
  69. $mform->setType('summary_editor', PARAM_RAW);
  70. $mform->addElement('header','requestreason', get_string('courserequestreason'));
  71. $mform->addElement('textarea', 'reason', get_string('courserequestsupport'), array('rows'=>'15', 'cols'=>'50'));
  72. $mform->addRule('reason', get_string('missingreqreason'), 'required', null, 'client');
  73. $mform->setType('reason', PARAM_TEXT);
  74. $this->add_action_buttons(true, get_string('requestcourse'));
  75. }
  76. function validation($data, $files) {
  77. global $DB;
  78. $errors = parent::validation($data, $files);
  79. $foundcourses = null;
  80. $foundreqcourses = null;
  81. if (!empty($data['shortname'])) {
  82. $foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']));
  83. $foundreqcourses = $DB->get_records('course_request', array('shortname'=>$data['shortname']));
  84. }
  85. if (!empty($foundreqcourses)) {
  86. if (!empty($foundcourses)) {
  87. $foundcourses = array_merge($foundcourses, $foundreqcourses);
  88. } else {
  89. $foundcourses = $foundreqcourses;
  90. }
  91. }
  92. if (!empty($foundcourses)) {
  93. foreach ($foundcourses as $foundcourse) {
  94. if (!empty($foundcourse->requester)) {
  95. $pending = 1;
  96. $foundcoursenames[] = $foundcourse->fullname.' [*]';
  97. } else {
  98. $foundcoursenames[] = $foundcourse->fullname;
  99. }
  100. }
  101. $foundcoursenamestring = implode(',', $foundcoursenames);
  102. $errors['shortname'] = get_string('shortnametaken', '', $foundcoursenamestring);
  103. if (!empty($pending)) {
  104. $errors['shortname'] .= get_string('starpending');
  105. }
  106. }
  107. return $errors;
  108. }
  109. }
  110. /**
  111. * A form for an administrator to reject a course request.
  112. */
  113. class reject_request_form extends moodleform {
  114. function definition() {
  115. $mform =& $this->_form;
  116. $mform->addElement('hidden', 'reject', 0);
  117. $mform->setType('reject', PARAM_INT);
  118. $mform->addElement('header','coursedetails', get_string('coursereasonforrejecting'));
  119. $mform->addElement('textarea', 'rejectnotice', get_string('coursereasonforrejectingemail'), array('rows'=>'15', 'cols'=>'50'));
  120. $mform->addRule('rejectnotice', get_string('missingreqreason'), 'required', null, 'client');
  121. $mform->setType('rejectnotice', PARAM_TEXT);
  122. $this->add_action_buttons(true, get_string('reject'));
  123. }
  124. }