/mod/choice/mod_form.php

https://github.com/jarednipper/HSU-common-code · PHP · 148 lines · 103 code · 39 blank · 6 comment · 13 complexity · 8d134b53b48d146f9dcd098d89bfb5a2 MD5 · raw file

  1. <?php
  2. require_once ($CFG->dirroot.'/course/moodleform_mod.php');
  3. class mod_choice_mod_form extends moodleform_mod {
  4. function definition() {
  5. global $CFG, $CHOICE_SHOWRESULTS, $CHOICE_PUBLISH, $CHOICE_DISPLAY;
  6. $mform =& $this->_form;
  7. //-------------------------------------------------------------------------------
  8. $mform->addElement('header', 'general', get_string('general', 'form'));
  9. $mform->addElement('text', 'name', get_string('choicename', 'choice'), array('size'=>'64'));
  10. if (!empty($CFG->formatstringstriptags)) {
  11. $mform->setType('name', PARAM_TEXT);
  12. } else {
  13. $mform->setType('name', PARAM_CLEAN);
  14. }
  15. $mform->addRule('name', null, 'required', null, 'client');
  16. $mform->addElement('htmleditor', 'text', get_string('choicetext', 'choice'));
  17. $mform->setType('text', PARAM_RAW);
  18. $mform->addRule('text', null, 'required', null, 'client');
  19. $mform->setHelpButton('text', array('writing', 'questions', 'richtext'), false, 'editorhelpbutton');
  20. $mform->addElement('format', 'format', get_string('format'));
  21. //-------------------------------------------------------------------------------
  22. $repeatarray=array();
  23. $repeatarray[] = &MoodleQuickForm::createElement('header', '', get_string('choice','choice').' {no}');
  24. $repeatarray[] = &MoodleQuickForm::createElement('text', 'option', get_string('choice','choice'));
  25. $repeatarray[] = &MoodleQuickForm::createElement('text', 'limit', get_string('limit','choice'));
  26. $repeatarray[] = &MoodleQuickForm::createElement('hidden', 'optionid', 0);
  27. $menuoptions=array();
  28. $menuoptions[0] = get_string('disable');
  29. $menuoptions[1] = get_string('enable');
  30. $mform->addElement('header', 'timerestricthdr', get_string('limit', 'choice'));
  31. $mform->addElement('select', 'limitanswers', get_string('limitanswers', 'choice'), $menuoptions);
  32. $mform->setHelpButton('limitanswers', array('limit', get_string('limit', 'choice'), 'choice'));
  33. if ($this->_instance){
  34. $repeatno=count_records('choice_options', 'choiceid', $this->_instance);
  35. $repeatno += 2;
  36. } else {
  37. $repeatno = 5;
  38. }
  39. $repeateloptions = array();
  40. $repeateloptions['limit']['default'] = 0;
  41. $repeateloptions['limit']['disabledif'] = array('limitanswers', 'eq', 0);
  42. $mform->setType('limit', PARAM_INT);
  43. $repeateloptions['option']['helpbutton'] = array('options', get_string('modulenameplural', 'choice'), 'choice');
  44. $mform->setType('option', PARAM_CLEAN);
  45. $mform->setType('optionid', PARAM_INT);
  46. $this->repeat_elements($repeatarray, $repeatno,
  47. $repeateloptions, 'option_repeats', 'option_add_fields', 3);
  48. //-------------------------------------------------------------------------------
  49. $mform->addElement('header', 'timerestricthdr', get_string('timerestrict', 'choice'));
  50. $mform->addElement('checkbox', 'timerestrict', get_string('timerestrict', 'choice'));
  51. $mform->setHelpButton('timerestrict', array("timerestrict", get_string("timerestrict","choice"), "choice"));
  52. $mform->addElement('date_time_selector', 'timeopen', get_string("choiceopen", "choice"));
  53. $mform->disabledIf('timeopen', 'timerestrict');
  54. $mform->addElement('date_time_selector', 'timeclose', get_string("choiceclose", "choice"));
  55. $mform->disabledIf('timeclose', 'timerestrict');
  56. //-------------------------------------------------------------------------------
  57. $mform->addElement('header', 'miscellaneoussettingshdr', get_string('miscellaneoussettings', 'form'));
  58. $mform->addElement('select', 'display', get_string("displaymode","choice"), $CHOICE_DISPLAY);
  59. $mform->addElement('select', 'showresults', get_string("publish", "choice"), $CHOICE_SHOWRESULTS);
  60. $mform->addElement('select', 'publish', get_string("privacy", "choice"), $CHOICE_PUBLISH);
  61. $mform->disabledIf('publish', 'showresults', 'eq', 0);
  62. $mform->addElement('selectyesno', 'allowupdate', get_string("allowupdate", "choice"));
  63. $mform->addElement('selectyesno', 'showunanswered', get_string("showunanswered", "choice"));
  64. //-------------------------------------------------------------------------------
  65. $features = new stdClass;
  66. $features->groups = true;
  67. $features->groupings = true;
  68. $features->groupmembersonly = true;
  69. $features->gradecat = false;
  70. $this->standard_coursemodule_elements($features);
  71. //-------------------------------------------------------------------------------
  72. $this->add_action_buttons();
  73. }
  74. function data_preprocessing(&$default_values){
  75. if (!empty($this->_instance) && ($options = get_records_menu('choice_options','choiceid', $this->_instance, 'id', 'id,text'))
  76. && ($options2 = get_records_menu('choice_options','choiceid', $this->_instance, 'id', 'id,maxanswers')) ) {
  77. $choiceids=array_keys($options);
  78. $options=array_values($options);
  79. $options2=array_values($options2);
  80. foreach (array_keys($options) as $key){
  81. $default_values['option['.$key.']'] = $options[$key];
  82. $default_values['limit['.$key.']'] = $options2[$key];
  83. $default_values['optionid['.$key.']'] = $choiceids[$key];
  84. }
  85. }
  86. if (empty($default_values['timeopen'])) {
  87. $default_values['timerestrict'] = 0;
  88. } else {
  89. $default_values['timerestrict'] = 1;
  90. }
  91. }
  92. function validation($data, $files) {
  93. $errors = parent::validation($data, $files);
  94. $choices = 0;
  95. foreach ($data['option'] as $option){
  96. if (trim($option) != ''){
  97. $choices++;
  98. }
  99. }
  100. if ($choices < 1) {
  101. $errors['option[0]'] = get_string('fillinatleastoneoption', 'choice');
  102. }
  103. if ($choices < 2) {
  104. $errors['option[1]'] = get_string('fillinatleastoneoption', 'choice');
  105. }
  106. return $errors;
  107. }
  108. }
  109. ?>