/mod/choice/mod_form.php

https://bitbucket.org/synergylearning/campusconnect · PHP · 145 lines · 101 code · 36 blank · 8 comment · 14 complexity · a53065570a5f6b3dbd4e8dcf37de1676 MD5 · raw file

  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->add_intro_editor(true, get_string('chatintro', 'chat'));
  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', 'limitanswers', get_string('limitanswers', 'choice'));
  26. $mform->addHelpButton('limitanswers', 'limitanswers', 'choice');
  27. $repeatarray = array();
  28. $repeatarray[] = $mform->createElement('text', 'option', get_string('optionno', 'choice'));
  29. $repeatarray[] = $mform->createElement('text', 'limit', get_string('limitno', 'choice'));
  30. $repeatarray[] = $mform->createElement('hidden', 'optionid', 0);
  31. if ($this->_instance){
  32. $repeatno = $DB->count_records('choice_options', array('choiceid'=>$this->_instance));
  33. $repeatno += 2;
  34. } else {
  35. $repeatno = 5;
  36. }
  37. $repeateloptions = array();
  38. $repeateloptions['limit']['default'] = 0;
  39. $repeateloptions['limit']['disabledif'] = array('limitanswers', 'eq', 0);
  40. $repeateloptions['limit']['rule'] = 'numeric';
  41. $repeateloptions['limit']['type'] = PARAM_INT;
  42. $repeateloptions['option']['helpbutton'] = array('choiceoptions', 'choice');
  43. $mform->setType('option', PARAM_CLEANHTML);
  44. $mform->setType('optionid', PARAM_INT);
  45. $this->repeat_elements($repeatarray, $repeatno,
  46. $repeateloptions, 'option_repeats', 'option_add_fields', 3, null, true);
  47. // Make the first option required
  48. if ($mform->elementExists('option[0]')) {
  49. $mform->addRule('option[0]', get_string('atleastoneoption', 'choice'), 'required', null, 'client');
  50. }
  51. //-------------------------------------------------------------------------------
  52. $mform->addElement('header', 'timerestricthdr', get_string('availability'));
  53. $mform->addElement('checkbox', 'timerestrict', get_string('timerestrict', 'choice'));
  54. $mform->addElement('date_time_selector', 'timeopen', get_string("choiceopen", "choice"));
  55. $mform->disabledIf('timeopen', 'timerestrict');
  56. $mform->addElement('date_time_selector', 'timeclose', get_string("choiceclose", "choice"));
  57. $mform->disabledIf('timeclose', 'timerestrict');
  58. //-------------------------------------------------------------------------------
  59. $mform->addElement('header', 'resultshdr', get_string('results', 'choice'));
  60. $mform->addElement('select', 'showresults', get_string("publish", "choice"), $CHOICE_SHOWRESULTS);
  61. $mform->addElement('select', 'publish', get_string("privacy", "choice"), $CHOICE_PUBLISH);
  62. $mform->disabledIf('publish', 'showresults', 'eq', 0);
  63. $mform->addElement('selectyesno', 'showunanswered', get_string("showunanswered", "choice"));
  64. //-------------------------------------------------------------------------------
  65. $this->standard_coursemodule_elements();
  66. //-------------------------------------------------------------------------------
  67. $this->add_action_buttons();
  68. }
  69. function data_preprocessing(&$default_values){
  70. global $DB;
  71. if (!empty($this->_instance) && ($options = $DB->get_records_menu('choice_options',array('choiceid'=>$this->_instance), 'id', 'id,text'))
  72. && ($options2 = $DB->get_records_menu('choice_options', array('choiceid'=>$this->_instance), 'id', 'id,maxanswers')) ) {
  73. $choiceids=array_keys($options);
  74. $options=array_values($options);
  75. $options2=array_values($options2);
  76. foreach (array_keys($options) as $key){
  77. $default_values['option['.$key.']'] = $options[$key];
  78. $default_values['limit['.$key.']'] = $options2[$key];
  79. $default_values['optionid['.$key.']'] = $choiceids[$key];
  80. }
  81. }
  82. if (empty($default_values['timeopen'])) {
  83. $default_values['timerestrict'] = 0;
  84. } else {
  85. $default_values['timerestrict'] = 1;
  86. }
  87. }
  88. function get_data() {
  89. $data = parent::get_data();
  90. if (!$data) {
  91. return false;
  92. }
  93. // Set up completion section even if checkbox is not ticked
  94. if (!empty($data->completionunlocked)) {
  95. if (empty($data->completionsubmit)) {
  96. $data->completionsubmit = 0;
  97. }
  98. }
  99. return $data;
  100. }
  101. function add_completion_rules() {
  102. $mform =& $this->_form;
  103. $mform->addElement('checkbox', 'completionsubmit', '', get_string('completionsubmit', 'choice'));
  104. return array('completionsubmit');
  105. }
  106. function completion_rule_enabled($data) {
  107. return !empty($data['completionsubmit']);
  108. }
  109. }