PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/mod/choice/mod_form.php

https://bitbucket.org/moodle/moodle
PHP | 181 lines | 114 code | 41 blank | 26 comment | 15 complexity | 47cdd471162f8d5e0d522346ddf1b066 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. if (!defined('MOODLE_INTERNAL')) {
  3. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  4. }
  5. require_once ($CFG->dirroot.'/course/moodleform_mod.php');
  6. class mod_choice_mod_form extends moodleform_mod {
  7. function definition() {
  8. global $CFG, $CHOICE_SHOWRESULTS, $CHOICE_PUBLISH, $CHOICE_DISPLAY, $DB;
  9. $mform =& $this->_form;
  10. //-------------------------------------------------------------------------------
  11. $mform->addElement('header', 'general', get_string('general', 'form'));
  12. $mform->addElement('text', 'name', get_string('choicename', 'choice'), array('size'=>'64'));
  13. if (!empty($CFG->formatstringstriptags)) {
  14. $mform->setType('name', PARAM_TEXT);
  15. } else {
  16. $mform->setType('name', PARAM_CLEANHTML);
  17. }
  18. $mform->addRule('name', null, 'required', null, 'client');
  19. $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  20. $this->standard_intro_elements(get_string('description', 'choice'));
  21. $mform->addElement('select', 'display', get_string("displaymode","choice"), $CHOICE_DISPLAY);
  22. //-------------------------------------------------------------------------------
  23. $mform->addElement('header', 'optionhdr', get_string('options', 'choice'));
  24. $mform->addElement('selectyesno', 'allowupdate', get_string("allowupdate", "choice"));
  25. $mform->addElement('selectyesno', 'allowmultiple', get_string('allowmultiple', 'choice'));
  26. if ($this->_instance) {
  27. if ($DB->count_records('choice_answers', array('choiceid' => $this->_instance)) > 0) {
  28. // Prevent user from toggeling the number of allowed answers once there are submissions.
  29. $mform->freeze('allowmultiple');
  30. }
  31. }
  32. $mform->addElement('selectyesno', 'limitanswers', get_string('limitanswers', 'choice'));
  33. $mform->addHelpButton('limitanswers', 'limitanswers', 'choice');
  34. $mform->addElement('selectyesno', 'showavailable', get_string('showavailable', 'choice'));
  35. $mform->addHelpButton('showavailable', 'showavailable', 'choice');
  36. $mform->hideIf('showavailable', 'limitanswers', 'eq', 0);
  37. $repeatarray = array();
  38. $repeatarray[] = $mform->createElement('text', 'option', get_string('optionno', 'choice'));
  39. $repeatarray[] = $mform->createElement('text', 'limit', get_string('limitno', 'choice'));
  40. $repeatarray[] = $mform->createElement('hidden', 'optionid', 0);
  41. if ($this->_instance){
  42. $repeatno = $DB->count_records('choice_options', array('choiceid'=>$this->_instance));
  43. $repeatno += 2;
  44. } else {
  45. $repeatno = 5;
  46. }
  47. $repeateloptions = array();
  48. $repeateloptions['limit']['default'] = 0;
  49. $repeateloptions['limit']['hideif'] = array('limitanswers', 'eq', 0);
  50. $repeateloptions['limit']['rule'] = 'numeric';
  51. $repeateloptions['limit']['type'] = PARAM_INT;
  52. $repeateloptions['option']['helpbutton'] = array('choiceoptions', 'choice');
  53. $mform->setType('option', PARAM_CLEANHTML);
  54. $mform->setType('optionid', PARAM_INT);
  55. $this->repeat_elements($repeatarray, $repeatno,
  56. $repeateloptions, 'option_repeats', 'option_add_fields', 3, null, true);
  57. // Make the first option required
  58. if ($mform->elementExists('option[0]')) {
  59. $mform->addRule('option[0]', get_string('atleastoneoption', 'choice'), 'required', null, 'client');
  60. }
  61. //-------------------------------------------------------------------------------
  62. $mform->addElement('header', 'availabilityhdr', get_string('availability'));
  63. $mform->addElement('date_time_selector', 'timeopen', get_string("choiceopen", "choice"),
  64. array('optional' => true));
  65. $mform->addElement('date_time_selector', 'timeclose', get_string("choiceclose", "choice"),
  66. array('optional' => true));
  67. $mform->addElement('advcheckbox', 'showpreview', get_string('showpreview', 'choice'));
  68. $mform->addHelpButton('showpreview', 'showpreview', 'choice');
  69. $mform->disabledIf('showpreview', 'timeopen[enabled]');
  70. //-------------------------------------------------------------------------------
  71. $mform->addElement('header', 'resultshdr', get_string('results', 'choice'));
  72. $mform->addElement('select', 'showresults', get_string("publish", "choice"), $CHOICE_SHOWRESULTS);
  73. $mform->addElement('select', 'publish', get_string("privacy", "choice"), $CHOICE_PUBLISH);
  74. $mform->hideIf('publish', 'showresults', 'eq', 0);
  75. $mform->addElement('selectyesno', 'showunanswered', get_string("showunanswered", "choice"));
  76. $mform->addElement('selectyesno', 'includeinactive', get_string('includeinactive', 'choice'));
  77. $mform->setDefault('includeinactive', 0);
  78. //-------------------------------------------------------------------------------
  79. $this->standard_coursemodule_elements();
  80. //-------------------------------------------------------------------------------
  81. $this->add_action_buttons();
  82. }
  83. function data_preprocessing(&$default_values){
  84. global $DB;
  85. if (!empty($this->_instance) && ($options = $DB->get_records_menu('choice_options',array('choiceid'=>$this->_instance), 'id', 'id,text'))
  86. && ($options2 = $DB->get_records_menu('choice_options', array('choiceid'=>$this->_instance), 'id', 'id,maxanswers')) ) {
  87. $choiceids=array_keys($options);
  88. $options=array_values($options);
  89. $options2=array_values($options2);
  90. foreach (array_keys($options) as $key){
  91. $default_values['option['.$key.']'] = $options[$key];
  92. $default_values['limit['.$key.']'] = $options2[$key];
  93. $default_values['optionid['.$key.']'] = $choiceids[$key];
  94. }
  95. }
  96. }
  97. /**
  98. * Allows module to modify the data returned by form get_data().
  99. * This method is also called in the bulk activity completion form.
  100. *
  101. * Only available on moodleform_mod.
  102. *
  103. * @param stdClass $data the form data to be modified.
  104. */
  105. public function data_postprocessing($data) {
  106. parent::data_postprocessing($data);
  107. // Set up completion section even if checkbox is not ticked
  108. if (!empty($data->completionunlocked)) {
  109. if (empty($data->completionsubmit)) {
  110. $data->completionsubmit = 0;
  111. }
  112. }
  113. }
  114. /**
  115. * Enforce validation rules here
  116. *
  117. * @param array $data array of ("fieldname"=>value) of submitted data
  118. * @param array $files array of uploaded files "element_name"=>tmp_file_path
  119. * @return array
  120. **/
  121. public function validation($data, $files) {
  122. $errors = parent::validation($data, $files);
  123. // Check open and close times are consistent.
  124. if ($data['timeopen'] && $data['timeclose'] &&
  125. $data['timeclose'] < $data['timeopen']) {
  126. $errors['timeclose'] = get_string('closebeforeopen', 'choice');
  127. }
  128. return $errors;
  129. }
  130. function add_completion_rules() {
  131. $mform =& $this->_form;
  132. $mform->addElement('checkbox', 'completionsubmit', '', get_string('completionsubmit', 'choice'));
  133. // Enable this completion rule by default.
  134. $mform->setDefault('completionsubmit', 1);
  135. return array('completionsubmit');
  136. }
  137. function completion_rule_enabled($data) {
  138. return !empty($data['completionsubmit']);
  139. }
  140. }