PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/feedback/mod_form.php

https://bitbucket.org/moodle/moodle
PHP | 221 lines | 129 code | 41 blank | 51 comment | 14 complexity | 8bbe881b1cd83f34b36a76ddb140a722 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. * print the form to add or edit a feedback-instance
  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->dirroot.'/course/moodleform_mod.php');
  28. class mod_feedback_mod_form extends moodleform_mod {
  29. public function definition() {
  30. global $CFG, $DB;
  31. $editoroptions = feedback_get_editor_options();
  32. $mform =& $this->_form;
  33. //-------------------------------------------------------------------------------
  34. $mform->addElement('header', 'general', get_string('general', 'form'));
  35. $mform->addElement('text', 'name', get_string('name', 'feedback'), array('size'=>'64'));
  36. $mform->setType('name', PARAM_TEXT);
  37. $mform->addRule('name', null, 'required', null, 'client');
  38. $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  39. $this->standard_intro_elements(get_string('description', 'feedback'));
  40. //-------------------------------------------------------------------------------
  41. $mform->addElement('header', 'timinghdr', get_string('availability'));
  42. $mform->addElement('date_time_selector', 'timeopen', get_string('feedbackopen', 'feedback'),
  43. array('optional' => true));
  44. $mform->addElement('date_time_selector', 'timeclose', get_string('feedbackclose', 'feedback'),
  45. array('optional' => true));
  46. //-------------------------------------------------------------------------------
  47. $mform->addElement('header', 'feedbackhdr', get_string('questionandsubmission', 'feedback'));
  48. $options=array();
  49. $options[1] = get_string('anonymous', 'feedback');
  50. $options[2] = get_string('non_anonymous', 'feedback');
  51. $mform->addElement('select',
  52. 'anonymous',
  53. get_string('anonymous_edit', 'feedback'),
  54. $options);
  55. // check if there is existing responses to this feedback
  56. if (is_numeric($this->_instance) AND
  57. $this->_instance AND
  58. $feedback = $DB->get_record("feedback", array("id"=>$this->_instance))) {
  59. $completed_feedback_count = feedback_get_completeds_group_count($feedback);
  60. } else {
  61. $completed_feedback_count = false;
  62. }
  63. if ($completed_feedback_count) {
  64. $multiple_submit_value = $feedback->multiple_submit ? get_string('yes') : get_string('no');
  65. $mform->addElement('text',
  66. 'multiple_submit_static',
  67. get_string('multiplesubmit', 'feedback'),
  68. array('size'=>'4',
  69. 'disabled'=>'disabled',
  70. 'value'=>$multiple_submit_value));
  71. $mform->setType('multiple_submit_static', PARAM_RAW);
  72. $mform->addElement('hidden', 'multiple_submit', '');
  73. $mform->setType('multiple_submit', PARAM_INT);
  74. $mform->addHelpButton('multiple_submit_static', 'multiplesubmit', 'feedback');
  75. } else {
  76. $mform->addElement('selectyesno',
  77. 'multiple_submit',
  78. get_string('multiplesubmit', 'feedback'));
  79. $mform->addHelpButton('multiple_submit', 'multiplesubmit', 'feedback');
  80. }
  81. $mform->addElement('selectyesno', 'email_notification', get_string('email_notification', 'feedback'));
  82. $mform->addHelpButton('email_notification', 'email_notification', 'feedback');
  83. $mform->addElement('selectyesno', 'autonumbering', get_string('autonumbering', 'feedback'));
  84. $mform->addHelpButton('autonumbering', 'autonumbering', 'feedback');
  85. //-------------------------------------------------------------------------------
  86. $mform->addElement('header', 'aftersubmithdr', get_string('after_submit', 'feedback'));
  87. $mform->addElement('selectyesno', 'publish_stats', get_string('show_analysepage_after_submit', 'feedback'));
  88. $mform->addElement('editor',
  89. 'page_after_submit_editor',
  90. get_string("page_after_submit", "feedback"),
  91. null,
  92. $editoroptions);
  93. $mform->setType('page_after_submit_editor', PARAM_RAW);
  94. $mform->addElement('text',
  95. 'site_after_submit',
  96. get_string('url_for_continue', 'feedback'),
  97. array('size'=>'64', 'maxlength'=>'255'));
  98. $mform->setType('site_after_submit', PARAM_TEXT);
  99. $mform->addHelpButton('site_after_submit', 'url_for_continue', 'feedback');
  100. //-------------------------------------------------------------------------------
  101. $this->standard_coursemodule_elements();
  102. //-------------------------------------------------------------------------------
  103. // buttons
  104. $this->add_action_buttons();
  105. }
  106. public function data_preprocessing(&$default_values) {
  107. $editoroptions = feedback_get_editor_options();
  108. if ($this->current->instance) {
  109. // editing an existing feedback - let us prepare the added editor elements (intro done automatically)
  110. $draftitemid = file_get_submitted_draft_itemid('page_after_submit');
  111. $default_values['page_after_submit_editor']['text'] =
  112. file_prepare_draft_area($draftitemid, $this->context->id,
  113. 'mod_feedback', 'page_after_submit', false,
  114. $editoroptions,
  115. $default_values['page_after_submit']);
  116. $default_values['page_after_submit_editor']['format'] = $default_values['page_after_submitformat'];
  117. $default_values['page_after_submit_editor']['itemid'] = $draftitemid;
  118. } else {
  119. // adding a new feedback instance
  120. $draftitemid = file_get_submitted_draft_itemid('page_after_submit_editor');
  121. // no context yet, itemid not used
  122. file_prepare_draft_area($draftitemid, null, 'mod_feedback', 'page_after_submit', false);
  123. $default_values['page_after_submit_editor']['text'] = '';
  124. $default_values['page_after_submit_editor']['format'] = editors_get_preferred_format();
  125. $default_values['page_after_submit_editor']['itemid'] = $draftitemid;
  126. }
  127. }
  128. /**
  129. * Allows module to modify the data returned by form get_data().
  130. * This method is also called in the bulk activity completion form.
  131. *
  132. * Only available on moodleform_mod.
  133. *
  134. * @param stdClass $data the form data to be modified.
  135. */
  136. public function data_postprocessing($data) {
  137. parent::data_postprocessing($data);
  138. if (isset($data->page_after_submit_editor)) {
  139. $data->page_after_submitformat = $data->page_after_submit_editor['format'];
  140. $data->page_after_submit = $data->page_after_submit_editor['text'];
  141. if (!empty($data->completionunlocked)) {
  142. // Turn off completion settings if the checkboxes aren't ticked
  143. $autocompletion = !empty($data->completion) &&
  144. $data->completion == COMPLETION_TRACKING_AUTOMATIC;
  145. if (!$autocompletion || empty($data->completionsubmit)) {
  146. $data->completionsubmit=0;
  147. }
  148. }
  149. }
  150. }
  151. /**
  152. * Enforce validation rules here
  153. *
  154. * @param array $data array of ("fieldname"=>value) of submitted data
  155. * @param array $files array of uploaded files "element_name"=>tmp_file_path
  156. * @return array
  157. **/
  158. public function validation($data, $files) {
  159. $errors = parent::validation($data, $files);
  160. // Check open and close times are consistent.
  161. if ($data['timeopen'] && $data['timeclose'] &&
  162. $data['timeclose'] < $data['timeopen']) {
  163. $errors['timeclose'] = get_string('closebeforeopen', 'feedback');
  164. }
  165. return $errors;
  166. }
  167. public function add_completion_rules() {
  168. $mform =& $this->_form;
  169. $mform->addElement('checkbox',
  170. 'completionsubmit',
  171. '',
  172. get_string('completionsubmit', 'feedback'));
  173. // Enable this completion rule by default.
  174. $mform->setDefault('completionsubmit', 1);
  175. return array('completionsubmit');
  176. }
  177. public function completion_rule_enabled($data) {
  178. return !empty($data['completionsubmit']);
  179. }
  180. }