PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/feedback/edit_form.php

https://bitbucket.org/moodle/moodle
PHP | 288 lines | 136 code | 36 blank | 116 comment | 8 complexity | d389a1c4776a3bd7a2b2e75be976d29a MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * prints the forms to choose an item-typ to create items and to choose a template to use
  18. *
  19. * @author Andreas Grabs
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  21. * @package mod_feedback
  22. */
  23. //It must be included from a Moodle page
  24. if (!defined('MOODLE_INTERNAL')) {
  25. die('Direct access to this script is forbidden.');
  26. }
  27. require_once($CFG->libdir.'/formslib.php');
  28. /**
  29. * The feedback_edit_use_template_form
  30. *
  31. * @deprecated since 4.0 new dynamic forms created
  32. */
  33. class feedback_edit_use_template_form extends moodleform {
  34. public function __construct($action = null, $customdata = null, $method = 'post', $target = '',
  35. $attributes = null, $editable = true, $ajaxformdata = null) {
  36. debugging('Class feedback_edit_use_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
  37. parent::__construct($action, $customdata, $method, $target, $attributes, $editable, $ajaxformdata);
  38. }
  39. /**
  40. * Overrides parent static method for deprecation purposes.
  41. *
  42. * @deprecated since 4.0
  43. * @return array
  44. */
  45. public static function get_js_module() {
  46. debugging('Class feedback_edit_use_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
  47. return parent::get_js_module();
  48. }
  49. /**
  50. * Overrides parent static method for deprecation purposes.
  51. *
  52. * @deprecated since 4.0
  53. * @param array $simulatedsubmitteddata
  54. * @param array $simulatedsubmittedfiles
  55. * @param string $method
  56. * @param null $formidentifier
  57. * @return array
  58. */
  59. public static function mock_ajax_submit($simulatedsubmitteddata, $simulatedsubmittedfiles = array(),
  60. $method = 'post', $formidentifier = null) {
  61. debugging('Class feedback_edit_use_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
  62. return parent::mock_ajax_submit($simulatedsubmitteddata, $simulatedsubmittedfiles, $method, $formidentifier);
  63. }
  64. /**
  65. * Overrides parent static method for deprecation purposes.
  66. *
  67. * @deprecated since 4.0
  68. * @param array $data
  69. * @return array
  70. */
  71. public static function mock_generate_submit_keys($data = []) {
  72. debugging('Class feedback_edit_use_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
  73. return parent::mock_generate_submit_keys($data);
  74. }
  75. /**
  76. * Overrides parent static method for deprecation purposes.
  77. *
  78. * @deprecated since 4.0
  79. * @param array $simulatedsubmitteddata
  80. * @param array $simulatedsubmittedfiles
  81. * @param string $method
  82. * @param null $formidentifier
  83. */
  84. public static function mock_submit($simulatedsubmitteddata, $simulatedsubmittedfiles = array(),
  85. $method = 'post', $formidentifier = null) {
  86. debugging('Class feedback_edit_use_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
  87. parent::mock_submit($simulatedsubmitteddata, $simulatedsubmittedfiles, $method, $formidentifier);
  88. }
  89. /**
  90. * Form definition
  91. */
  92. public function definition() {
  93. $mform =& $this->_form;
  94. $course = $this->_customdata['course'];
  95. $elementgroup = array();
  96. //headline
  97. $mform->addElement('header', 'using_templates', get_string('using_templates', 'feedback'));
  98. // hidden elements
  99. $mform->addElement('hidden', 'id');
  100. $mform->setType('id', PARAM_INT);
  101. // visible elements
  102. $templates_options = array();
  103. $owntemplates = feedback_get_template_list($course, 'own');
  104. $publictemplates = feedback_get_template_list($course, 'public');
  105. $options = array();
  106. if ($owntemplates or $publictemplates) {
  107. $options[''] = array('' => get_string('choosedots'));
  108. if ($owntemplates) {
  109. $courseoptions = array();
  110. foreach ($owntemplates as $template) {
  111. $courseoptions[$template->id] = format_string($template->name);
  112. }
  113. $options[get_string('course')] = $courseoptions;
  114. }
  115. if ($publictemplates) {
  116. $publicoptions = array();
  117. foreach ($publictemplates as $template) {
  118. $publicoptions[$template->id] = format_string($template->name);
  119. }
  120. $options[get_string('public', 'feedback')] = $publicoptions;
  121. }
  122. $attributes = [
  123. 'onChange="this.form.submit()"',
  124. 'data-form-change-checker-override="1"',
  125. ];
  126. $elementgroup[] = $mform->createElement(
  127. 'selectgroups',
  128. 'templateid',
  129. get_string('using_templates', 'feedback'),
  130. $options,
  131. implode(' ', $attributes)
  132. );
  133. $elementgroup[] = $mform->createElement('submit',
  134. 'use_template',
  135. get_string('use_this_template', 'feedback'),
  136. array('class' => 'hiddenifjs'));
  137. $mform->addGroup($elementgroup, 'elementgroup', '', array(' '), false);
  138. } else {
  139. $mform->addElement('static', 'info', get_string('no_templates_available_yet', 'feedback'));
  140. }
  141. $this->set_data(array('id' => $this->_customdata['id']));
  142. }
  143. }
  144. /**
  145. * The feedback_edit_create_template_form
  146. *
  147. * @deprecated since 4.0, new dynamic forms have been created instead.
  148. */
  149. class feedback_edit_create_template_form extends moodleform {
  150. public function __construct($action = null, $customdata = null, $method = 'post',
  151. $target = '', $attributes = null, $editable = true, $ajaxformdata = null) {
  152. debugging('Class feedback_edit_create_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
  153. parent::__construct($action, $customdata, $method, $target, $attributes, $editable, $ajaxformdata);
  154. }
  155. /**
  156. * Overrides parent static method for deprecation purposes.
  157. *
  158. * @deprecated since 4.0
  159. * @return array
  160. */
  161. public static function get_js_module() {
  162. debugging('Class feedback_edit_create_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
  163. return parent::get_js_module();
  164. }
  165. /**
  166. * Overrides parent static method for deprecation purposes.
  167. *
  168. * @deprecated since 4.0
  169. * @param array $simulatedsubmitteddata
  170. * @param array $simulatedsubmittedfiles
  171. * @param string $method
  172. * @param null $formidentifier
  173. * @return array
  174. */
  175. public static function mock_ajax_submit($simulatedsubmitteddata, $simulatedsubmittedfiles = array(),
  176. $method = 'post', $formidentifier = null) {
  177. debugging('Class feedback_edit_create_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
  178. return parent::mock_ajax_submit($simulatedsubmitteddata, $simulatedsubmittedfiles, $method, $formidentifier);
  179. }
  180. /**
  181. * Overrides parent static method for deprecation purposes.
  182. *
  183. * @deprecated since 4.0
  184. * @param array $data
  185. * @return array
  186. */
  187. public static function mock_generate_submit_keys($data = []) {
  188. debugging('Class feedback_edit_create_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
  189. return parent::mock_generate_submit_keys($data);
  190. }
  191. /**
  192. * Overrides parent static method for deprecation purposes.
  193. *
  194. * @deprecated since 4.0
  195. * @param array $simulatedsubmitteddata
  196. * @param array $simulatedsubmittedfiles
  197. * @param string $method
  198. * @param null $formidentifier
  199. */
  200. public static function mock_submit($simulatedsubmitteddata, $simulatedsubmittedfiles = array(),
  201. $method = 'post', $formidentifier = null) {
  202. debugging('Class feedback_edit_create_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
  203. parent::mock_submit($simulatedsubmitteddata, $simulatedsubmittedfiles, $method, $formidentifier);
  204. }
  205. /**
  206. * Form definition
  207. */
  208. public function definition() {
  209. $mform =& $this->_form;
  210. // hidden elements
  211. $mform->addElement('hidden', 'id');
  212. $mform->setType('id', PARAM_INT);
  213. $mform->addElement('hidden', 'do_show');
  214. $mform->setType('do_show', PARAM_ALPHANUMEXT);
  215. $mform->setConstant('do_show', 'edit');
  216. // visible elements
  217. $elementgroup = array();
  218. $elementgroup[] = $mform->createElement('text',
  219. 'templatename',
  220. get_string('name', 'feedback'),
  221. ['maxlength' => '200']);
  222. if (has_capability('mod/feedback:createpublictemplate', context_system::instance())) {
  223. $elementgroup[] = $mform->createElement('checkbox',
  224. 'ispublic', '',
  225. get_string('public', 'feedback'));
  226. }
  227. $mform->addGroup($elementgroup,
  228. 'elementgroup',
  229. get_string('name', 'feedback'),
  230. array(' '),
  231. false);
  232. // Buttons.
  233. $mform->addElement('submit', 'create_template', get_string('save_as_new_template', 'feedback'));
  234. $mform->setType('templatename', PARAM_TEXT);
  235. $this->set_data(array('id' => $this->_customdata['id']));
  236. }
  237. /**
  238. * Form validation
  239. *
  240. * @param array $data array of ("fieldname"=>value) of submitted data
  241. * @param array $files array of uploaded files "element_name"=>tmp_file_path
  242. * @return array of "element_name"=>"error_description" if there are errors,
  243. * or an empty array if everything is OK (true allowed for backwards compatibility too).
  244. */
  245. public function validation($data, $files) {
  246. $errors = parent::validation($data, $files);
  247. if (!isset($data['templatename']) || trim(strval($data['templatename'])) === '') {
  248. $errors['elementgroup'] = get_string('name_required', 'feedback');
  249. }
  250. return $errors;
  251. }
  252. }