From ebd3c7ac94c775318740f4ecd09f7d8080812a8b Mon Sep 17 00:00:00 2001 From: jamiesensei Date: Fri, 24 Nov 2006 12:20:26 +0000 Subject: [PATCH] added repeat_elements method to moodleform and used method for choice form --- lang/en_utf8/form.php | 4 +- lib/formslib.php | 79 +++++++++- mod/choice/lib.php | 125 +++++++--------- mod/choice/mod.html | 320 ---------------------------------------- mod/choice/mod_form.php | 116 +++++++++++++++ 5 files changed, 249 insertions(+), 395 deletions(-) delete mode 100644 mod/choice/mod.html create mode 100644 mod/choice/mod_form.php diff --git a/lang/en_utf8/form.php b/lang/en_utf8/form.php index d41afd043a..022d26259b 100644 --- a/lang/en_utf8/form.php +++ b/lang/en_utf8/form.php @@ -18,4 +18,6 @@ $string['nonexistentformelements'] = 'Trying to add help buttons to nonexistent $string['requiredelement'] = 'Required field.'; $string['general'] = 'General Settings'; $string['modstandardels']='Common Module Settings'; -?> +$string['miscellaneoussettings']='Miscellaneous Settings'; +$string['addfields']='Add $a fields to form'; +?> \ No newline at end of file diff --git a/lib/formslib.php b/lib/formslib.php index 1a1a0dbba3..89b59c4ab1 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -63,6 +63,13 @@ class moodleform { */ var $_upload_manager; // + /** + * Array of buttons that if pressed do not result in the processing of the form. + * + * @var array + */ + var $_nosubmitbuttons=array(); + /** * The constructor function calls the abstract function definition() and it will then * process and clean and attempt to validate incoming data. @@ -229,7 +236,7 @@ class moodleform { * Must be used BEFORE creating of file element! * * @param object $course - * @param object $modbytes - max size limit defined in module + * @param object $modbytes - max size limit defined in module */ function set_max_file_size($course=null, $modbytes=0) { global $CFG, $COURSE; @@ -292,6 +299,11 @@ class moodleform { * @return object submitted data; NULL if not valid or not submitted */ function data_submitted($slashed=true) { + foreach ($this->_nosubmitbuttons as $nosubmitbutton){ + if (optional_param($nosubmitbutton, 0, PARAM_TEXT)){ + return NULL; + } + } if ($this->is_submitted() and $this->is_validated()) { $data = $this->_form->exportValues(null, $slashed); unset($data['sesskey']); // we do not need to return sesskey @@ -358,6 +370,71 @@ class moodleform { function validation($data) { return true; } + + function _register_no_submit_button($addfieldsname){ + $this->_nosubmitbuttons[]=$addfieldsname; + } + + function repeat_elements($elementobjs, $repeats, $options, $repeathiddenname, $addfieldsname, $addfieldsno=5, $addstring=array('addfields', 'form')){ + $repeats = optional_param($repeathiddenname, $repeats, PARAM_INT); + $addfields = optional_param($addfieldsname, '', PARAM_TEXT); + if (!empty($addfields)){ + $repeats += $addfieldsno; + } + $this->_register_no_submit_button($addfieldsname); + $mform =& $this->_form; + $mform->addElement('hidden', $repeathiddenname, $repeats); + //value not to be overridden by submitted value + $mform->setConstants(array($repeathiddenname=>$repeats)); + for ($i=0; $i<$repeats; $i++) { + foreach ($elementobjs as $elementobj){ + $elementclone=clone($elementobj); + $name=$elementclone->getName(); + $elementclone->setName($name."[$i]"); + if (is_a($elementclone, 'HTML_QuickForm_header')){ + $value=$elementclone->_text; + $elementclone->setValue($value.' '.($i+1)); + + } + $mform->addElement($elementclone); + } + } + for ($i=0; $i<$repeats; $i++) { + foreach ($options as $elementname => $elementoptions){ + $pos=strpos($elementname, '['); + if ($pos!==FALSE){ + $realelementname = substr($elementname, 0, $pos+1)."[$i]"; + $realelementname .= substr($elementname, $pos+1); + }else { + $realelementname = $elementname."[$i]"; + } + foreach ($elementoptions as $option => $params){ + + switch ($option){ + case 'default' : + $mform->setDefault($realelementname, $params); + break; + case 'type' : + $mform->setType($realelementname, $params); + break; + case 'helpbutton' : + $mform->setHelpButton($realelementname, $params); + break; + case 'disabledif' : + $mform->disabledIf($realelementname, $params[0], $params[1], $params[2]); + break; + + } + } + } + } + $mform->addElement('submit', $addfieldsname, get_string('addfields', 'form', $addfieldsno), + array('onclick'=>'this.form.submit();'));//need this to bypass client validation + + $renderer =& $mform->defaultRenderer(); + $renderer->addStopFieldsetElements($addfieldsname); + + } } /** diff --git a/mod/choice/lib.php b/mod/choice/lib.php index ed4aa6cefe..65164e3c56 100644 --- a/mod/choice/lib.php +++ b/mod/choice/lib.php @@ -48,38 +48,31 @@ function choice_user_complete($course, $user, $mod, $choice) { function choice_add_instance($choice) { -// Given an object containing all the necessary data, -// (defined by the form in mod.html) this function -// will create a new instance and return the id number +// Given an object containing all the necessary data, +// (defined by the form in mod.html) this function +// will create a new instance and return the id number // of the new instance. $choice->timemodified = time(); - if (!empty($choice->timerestrict) and $choice->timerestrict) { - $choice->timeopen = make_timestamp($choice->openyear, $choice->openmonth, $choice->openday, - $choice->openhour, $choice->openminute, 0); - $choice->timeclose = make_timestamp($choice->closeyear, $choice->closemonth, $choice->closeday, - $choice->closehour, $choice->closeminute, 0); - } else { + if (empty($choice->timerestrict)) { $choice->timeopen = 0; $choice->timeclose = 0; } //insert answers if ($choice->id = insert_record("choice", $choice)) { - foreach ($choice as $name => $value) { - if (strstr($name, "newoption")) { /// New option - $value = trim($value); - if (isset($value) && $value <> '') { - $option = NULL; - $option->text = $value; - $option->choiceid = $choice->id; - if (isset($choice->{'newlimit'.substr($name, 9)})) { - $option->maxanswers = $choice->{'newlimit'.substr($name, 9)}; - } - $option->timemodified = time(); - insert_record("choice_options", $option); + foreach ($choice->option as $key => $value) { + $value = trim($value); + if (isset($value) && $value <> '') { + $option = new object(); + $option->text = $value; + $option->choiceid = $choice->id; + if (isset($choice->limit[$key])) { + $option->maxanswers = $choice->limit[$key]; } + $option->timemodified = time(); + insert_record("choice_options", $option); } } } @@ -88,52 +81,38 @@ function choice_add_instance($choice) { function choice_update_instance($choice) { -// Given an object containing all the necessary data, -// (defined by the form in mod.html) this function +// Given an object containing all the necessary data, +// (defined by the form in mod.html) this function // will update an existing instance with new data. $choice->id = $choice->instance; $choice->timemodified = time(); - if (!empty($choice->timerestrict) and $choice->timerestrict) { - $choice->timeopen = make_timestamp($choice->openyear, $choice->openmonth, $choice->openday, - $choice->openhour, $choice->openminute, 0); - $choice->timeclose = make_timestamp($choice->closeyear, $choice->closemonth, $choice->closeday, - $choice->closehour, $choice->closeminute, 0); - } else { + if (empty($choice->timerestrict)) { $choice->timeopen = 0; $choice->timeclose = 0; } - //update answers - - foreach ($choice as $name => $value) { + //update, delete or insert answers + foreach ($choice->option as $key => $value) { $value = trim($value); - - if (strstr($name, "oldoption")) { // Old option + $option = new object(); + $option->text = $value; + $option->choiceid = $choice->id; + if (isset($choice->limit[$key])) { + $option->maxanswers = $choice->limit[$key]; + } + $option->timemodified = time(); + if (isset($choice->optionid[$key])){//existing choice record + $option->id=$choice->optionid[$key]; if (isset($value) && $value <> '') { - $option = NULL; - $option->id = substr($name, 9); // Get the ID of the answer that needs to be updated. - $option->text = $value; - $option->choiceid = $choice->id; - if (isset($choice->{'oldlimit'.substr($name, 9)})) { - $option->maxanswers = $choice->{'oldlimit'.substr($name, 9)}; - } - $option->timemodified = time(); update_record("choice_options", $option); } else { //empty old option - needs to be deleted. - delete_records("choice_options", "id", substr($name, 9)); + delete_records("choice_options", "id", $option->id); } - } else if (strstr($name, "newoption")) { /// New option - if (isset($value)&& $value <> '') { - $option = NULL; - $option->text = $value; - $option->choiceid = $choice->id; - if (isset($choice->{'newlimit'.substr($name, 9)})) { - $option->maxanswers = $choice->{'newlimit'.substr($name, 9)}; - } - $option->timemodified = time(); + } else { + if (isset($value) && $value <> '') { insert_record("choice_options", $option); } } @@ -144,7 +123,7 @@ function choice_update_instance($choice) { } function choice_show_form($choice, $user, $cm) { - + //$cdisplay is an array of the display info for a choice $cdisplay[$optionid]->text - text name of option. // ->maxanswers -maxanswers for this option // ->full - whether this option is full or not. 0=not full, 1=full @@ -154,7 +133,7 @@ function choice_show_form($choice, $user, $cm) { foreach ($choice->option as $optionid => $text) { if (isset($text)) { //make sure there are no dud entries in the db with blank text values. $countanswers = (get_records("choice_answers", "optionid", $optionid)); - $countans = 0; + $countans = 0; $context = get_context_instance(CONTEXT_MODULE, $cm->id); if (!empty($countanswers)) { foreach ($countanswers as $ca) { //only return enrolled users. @@ -183,7 +162,7 @@ function choice_show_form($choice, $user, $cm) { if ($choice->limitanswers && ($countans >= $maxans) && (empty($cdisplay[$aid]->checked)) ) { $cdisplay[$aid]->disabled = ' disabled="disabled" '; } else { - $cdisplay[$aid]->disabled = ''; + $cdisplay[$aid]->disabled = ''; } $aid++; } @@ -192,11 +171,11 @@ function choice_show_form($choice, $user, $cm) { switch ($choice->display) { case CHOICE_DISPLAY_HORIZONTAL: echo ""; - - foreach ($cdisplay as $cd) { + + foreach ($cdisplay as $cd) { echo "
"; echo "optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />"; - if (!empty($cd->disabled)) { + if (!empty($cd->disabled)) { echo format_text($cd->text."
".get_string('full', 'choice').""); } else { echo format_text($cd->text); @@ -272,7 +251,7 @@ function choice_user_submit_response($formanswer, $choice, $userid, $courseid, $ if (!($choice->limitanswers && ($countanswers >= $maxans) )) { if ($current) { - + $newanswer = $current; $newanswer->optionid = $formanswer; $newanswer->timemodified = time(); @@ -293,7 +272,7 @@ function choice_user_submit_response($formanswer, $choice, $userid, $courseid, $ } } else { if (!($current->optionid==$formanswer)) { //check to see if current choice already selected - if not display error - error("this choice is full!"); + error("this choice is full!"); } } } @@ -383,7 +362,7 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') { echo ''; } - echo ""; + echo "
"; echo ""; $count = 0; foreach ($useranswer as $optionid => $userlist) { @@ -442,8 +421,8 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') { } echo ""; $count++; } - + /// Print "Select all" etc. if (has_capability('mod/choice:readresponses', $context)) { echo ''; } - + echo "
"; $countanswers = get_records("choice_answers", "optionid", $optionid); - $countans = 0; - if (!empty($countanswers)) { + $countans = 0; + if (!empty($countanswers)) { foreach ($countanswers as $ca) { //only return enrolled users. if (has_capability('mod/choice:choose', get_context_instance(CONTEXT_MODULE, $cm->id))) { $countans = $countans+1; @@ -461,7 +440,7 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') { echo "

'; @@ -476,7 +455,7 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') { echo ''; echo '

"; if (has_capability('mod/choice:readresponses', $context)) { echo ""; @@ -561,12 +540,12 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') { echo "
"; break; - } + } } function choice_delete_responses($attemptids) { - + if(!is_array($attemptids) || empty($attemptids)) { return false; } @@ -581,15 +560,15 @@ function choice_delete_responses($attemptids) { if ($todelete = get_record('choice_answers', 'id', $attemptid)) { delete_records('choice_answers', 'id', $attemptid); } - } + } return true; } function choice_delete_instance($id) { -// Given an ID of an instance of this module, -// this function will permanently delete the instance -// and any data that depends on it. +// Given an ID of an instance of this module, +// this function will permanently delete the instance +// and any data that depends on it. if (! $choice = get_record("choice", "id", "$id")) { return false; @@ -607,7 +586,7 @@ function choice_delete_instance($id) { if (! delete_records("choice", "id", "$choice->id")) { $result = false; - } + } return $result; } @@ -645,7 +624,7 @@ function choice_get_choice($choiceid) { if ($choice = get_record("choice", "id", $choiceid)) { if ($options = get_records("choice_options", "choiceid", $choiceid, "id")) { foreach ($options as $option) { - $choice->option[$option->id] = $option->text; + $choice->option[$option->id] = $option->text; $choice->maxanswers[$option->id] = $option->maxanswers; } return $choice; diff --git a/mod/choice/mod.html b/mod/choice/mod.html deleted file mode 100644 index 59d8b6ebe5..0000000000 --- a/mod/choice/mod.html +++ /dev/null @@ -1,320 +0,0 @@ -name)) { - $form->name = ""; - } - if (empty($form->text)) { - $form->text = ""; - } - if (empty($form->format)) { - $form->format = 0; - } - if (empty($form->display)) { - $form->display = 0; - } - if (empty($form->timeopen)) { - $form->timeopen = ""; - $form->timerestrict = 0; - } else { - $form->timerestrict = 1; - } - if (empty($form->timeclose)) { - $form->timeclose = ""; - } - if (empty($form->publish)) { - $form->publish = 0; - } - if (empty($form->showresults)) { - $form->showresults = 0; - } - if (empty($form->allowupdate)) { - $form->allowupdate = 0; - } - if (empty($form->showunanswered)) { - $form->showunanswered = 0; - } - if (empty($form->limitanswers)) { - $form->limitanswers = 0; - } - -?> - -
- - - - - - - - - - - - - -instance) && ($options = get_records_menu('choice_options','choiceid', $form->instance, 'id', 'id,text')) && ($options2 = get_records_menu('choice_options','choiceid', $form->instance, 'id', 'id,maxanswers')) ) { - foreach ($options as $id => $text) { - $count++; - ?> - - - - - - - - - - - - - - - - - - - -limitanswers == 0) { //lock the limit fields if limit isn't used. -?> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
: - " value="name) ?>" /> -
:

- "; - helpbutton("questions", get_string("helpquestions"), "moodle", true, true); - echo "
"; - if ($usehtmleditor) { - helpbutton("richtext", get_string("helprichtext"), "moodle", true, true); - } else { - emoticonhelpbutton("form", "text"); - } - echo "
"; - ?> -
- text); - - if ($usehtmleditor) { - echo ''; - } else { - echo "
"; - helpbutton("textformat", get_string("formattexttype")); - print_string("formattexttype"); - echo ": "; - if (!$form->format) { - $form->format = $defaultformat; - } - choose_from_menu(format_text_menu(), "format", $form->format, ""); - echo "
"; - } - ?> -
: - - - : - - -
: - - - : - - -
: - limitanswers", "", "return lockselects('form','limitanswers.options[0]', limititems)"); - ?> -
: - - - " onclick="return lockoptions('form','timerestrict', timeitems)" timerestrict) echo 'checked="checked"'; ?> /> - -
  - - - - - - - - - -
: - - timeopen and $course->format == "weeks") { - $form->timeopen = $course->startdate + (($form->section - 1) * 608400); - } - print_date_selector("openday", "openmonth", "openyear", $form->timeopen); - print_time_selector("openhour", "openminute", $form->timeopen); - ?> - - - - - - -
: - - timeclose and $course->format == "weeks") { - $form->timeclose = $course->startdate + (($form->section) * 608400); - } - print_date_selector("closeday", "closemonth", "closeyear", $form->timeclose); - print_time_selector("closehour", "closeminute", $form->timeclose); - ?> - - - - - - - - timerestrict) { - echo "\n"; - } - ?> - -
- -
: - dirroot/mod/choice/lib.php"); - choose_from_menu($CHOICE_DISPLAY, "display", "$form->display", ""); - ?> -
-
: - dirroot/mod/choice/lib.php"); - choose_from_menu($CHOICE_SHOWRESULTS, "showresults", "$form->showresults", "", "return lockselects('form','showresults.options[0]',['publish'])"); - ?> -
-
: - dirroot/mod/choice/lib.php"); - choose_from_menu($CHOICE_PUBLISH, "publish", "$form->publish", ""); - ?> -
- -
: - allowupdate", ""); - ?> -
-
: - showunanswered", ""); - ?> -
-
- -
- - - - - - - - -" /> -" /> -
-
diff --git a/mod/choice/mod_form.php b/mod/choice/mod_form.php new file mode 100644 index 0000000000..ba5ae70a22 --- /dev/null +++ b/mod/choice/mod_form.php @@ -0,0 +1,116 @@ +_form; + $renderer =& $mform->defaultRenderer(); + +//------------------------------------------------------------------------------- + $mform->addElement('header', 'general', get_string('general', 'form')); + + $mform->addElement('text', 'name', get_string('choicename', 'choice')); + $mform->setType('name', PARAM_TEXT); + $mform->addRule('name', null, 'required', null, 'client'); + + $mform->addElement('htmleditor', 'text', get_string('choicetext', 'choice')); + $mform->setType('text', PARAM_RAW); + $mform->addRule('text', null, 'required', null, 'client'); + + $mform->addElement('format', 'format', get_string('format')); + + $repeatarray=array(); + $repeatarray[] = &MoodleQuickForm::createElement('header', '', get_string('choice','choice')); + $repeatarray[] = &MoodleQuickForm::createElement('text', 'option', get_string('choice','choice')); + $repeatarray[] = &MoodleQuickForm::createElement('text', 'limit', get_string('limit','choice')); + $repeatarray[] = &MoodleQuickForm::createElement('hidden', 'optionid', 0); + + if ($this->_instance){ + $repeatno=count_records('choice_options', 'choiceid', $this->_instance); + $repeatno+=2; + } else { + $repeatno=10; + } + + $this->repeat_elements($repeatarray, $repeatno, + array('limit'=>array('default'=>0, + 'type'=>PARAM_INT, + 'disabledif'=>array('limitanswers', 'eq', 0)), + + 'option' =>array('type'=>PARAM_TEXT, + 'helpbutton'=>array('options', get_string('modulenameplural', 'choice'), 'choice')), + 'optionid' =>array('type'=>PARAM_INT)), + 'option_repeats', 'option_add_fields'); + + + + $menuoptions=array(); + $menuoptions[0] = get_string('disable'); + $menuoptions[1] = get_string('enable'); + $mform->addElement('select', 'limitanswers', get_string('limitanswers', 'choice'), $menuoptions); + $mform->setHelpButton('limitanswers', array('limit', get_string('limit', 'choice'), 'choice')); + +//------------------------------------------------------------------------------- + $mform->addElement('header', 'timerestricthdr', get_string('timerestrict', 'choice')); + $mform->addElement('checkbox', 'timerestrict', get_string('timerestrict', 'choice')); + $mform->setHelpButton('timerestrict', array("timerestrict", get_string("timerestrict","choice"), "choice")); + + + $mform->addElement('date_time_selector', 'timeopen', get_string("choiceopen", "choice")); + $mform->disabledIf('timeopen', 'timerestrict'); + + $mform->addElement('date_time_selector', 'timeclose', get_string("choiceclose", "choice")); + $mform->disabledIf('timeclose', 'timerestrict'); + +//------------------------------------------------------------------------------- + $mform->addElement('header', 'miscellaneoussettingshdr', get_string('miscellaneoussettings', 'form')); + + $mform->addElement('select', 'display', get_string("displaymode","choice"), $CHOICE_DISPLAY); + + $mform->addElement('select', 'showresults', get_string("publish", "choice"), $CHOICE_SHOWRESULTS); + + $mform->addElement('select', 'publish', get_string("privacy", "choice"), $CHOICE_PUBLISH); + $mform->disabledIf('publish', 'showresults', 'eq', 0); + + $mform->addElement('selectyesno', 'allowupdate', get_string("allowupdate", "choice")); + + $mform->addElement('selectyesno', 'showunanswered', get_string("showunanswered", "choice")); + +//------------------------------------------------------------------------------- + $this->standard_coursemodule_elements(); +//------------------------------------------------------------------------------- + + $buttonarray=array(); + $buttonarray[] = &MoodleQuickForm::createElement('submit', 'submit', get_string('savechanges')); + $buttonarray[] = &MoodleQuickForm::createElement('submit', 'cancel', get_string('cancel')); + $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); + $renderer->addStopFieldsetElements('buttonar'); + } + + function defaults_preprocessing(&$default_values){ + if (!empty($this->_instance) && ($options = get_records_menu('choice_options','choiceid', $this->_instance, 'id', 'id,text')) + && ($options2 = get_records_menu('choice_options','choiceid', $this->_instance, 'id', 'id,maxanswers')) ) { + $choiceids=array_keys($options); + $options=array_values($options); + $options2=array_values($options2); + + foreach (array_keys($options) as $key){ + $default_values['option['.$key.']'] = $options[$key]; + $default_values['limit['.$key.']'] = $options2[$key]; + $default_values['optionid['.$key.']'] = $choiceids[$key]; + } + + } + if (empty($default_values['timeopen'])) { + $default_values['timerestrict'] = 0; + } else { + $default_values['timerestrict'] = 1; + } + + } + +} +?> \ No newline at end of file -- 2.39.5