for ($i = 1; $i <= $this->maxlickert; $i++) { if ($i == $this->maxlickert) { $defaults[$i] = get_string(self::STRATEGYID . '_rating_biggestwish', ratingallocate_MOD_NAME, "{$i}"); } else { $defaults[$i] = $i; } } return $defaults; } protected function getValidationInfo() { return array(self::MAXNO => array(true, 0), self::COUNTLICKERT => array(true, 2)); } } // register with the strategymanager \strategymanager::add_strategy(strategy::STRATEGYID); class mod_ratingallocate_view_form extends \ratingallocate_options_strategyform { //Already specified by parent class protected function construct_strategy($strategyoptions) { return new strategy($strategyoptions); } public function get_choiceoptions() { $params = $this->get_strategysetting(strategy::COUNTLICKERT); return $this->get_strategy()->get_choiceoptions($params); } protected function get_max_amount_of_nos() { return $this->get_strategysetting(strategy::MAXNO);
public function definition_after_data() { parent::definition_after_data(); $mform =& $this->_form; $data = $this->current; $choices = array(); if (!empty($data->id)) { global $DB; /* The method $this->get_submitted_data() does only return values for fields that are already defined->load ids from db -> create fields(values are overwritten by submitted data). */ $choices = $DB->get_records('ratingallocate_choices', array('ratingallocateid' => $data->id), 'title ASC'); } else { $this->newchoicecounter = 2; } // If there is already submitted data? if ($this->is_submitted()) { // Load new_choice_counter. if (property_exists($this->get_submitted_data(), self::NEW_CHOICE_COUNTER)) { $this->newchoicecounter = $this->get_submitted_data()->{self::NEW_CHOICE_COUNTER}; } // Increment new choice counter if add_new_choice button was pressed. if (property_exists($this->get_submitted_data(), self::ADD_CHOICE_ACTION)) { $this->newchoicecounter++; } } for ($i = 0; $i < $this->newchoicecounter; $i++) { $choices[] = $this->create_empty_choice($i + 1); } // Generate array with ids of all choices. $choiceids = array_map(function ($elem) { return (int) $elem->id; }, $choices); // Initialize variable. $deletechoicearray = array(); // If delete choice button was pressed. if ($this->is_submitted()) { // Retrieve ids of choices to be deleted from the static field. if (property_exists($this->get_submitted_data(), self::DELETED_CHOICE_IDS)) { $deletedchoiceids = $this->get_submitted_data()->{self::DELETED_CHOICE_IDS}; } // If the string is not empty the array of choice ids is exploded from it. if (!empty($deletedchoiceids)) { $deletechoicearray = explode(',', $deletedchoiceids); } // Parses all delete choice ids to integers. $integercheck = function ($elem) { return (int) $elem; }; array_map($integercheck, $deletechoicearray); // Retrieve id of choice to be deleted if delete button was pressed. $matches = preg_grep('/' . self::DELETE_CHOICE_ACTION . '([-]?[0-9]+)/', array_keys($mform->getSubmitValues())); // Only proceed if exaclty one delete button was found in the submitted data. if (count($matches) == 1) { // Retrieve the id as an Integer from the button name. $elem = array_pop($matches); $parts = explode('_', $elem); $deletechoiceid = (int) array_pop($parts); // If the id matches one of the choices add it to the choices to be deleted. if (in_array($deletechoiceid, $choiceids)) { $deletechoicearray[] = $deletechoiceid; } } } // Clean array to only contain feasible ids. $deletechoicearray = array_intersect($deletechoicearray, $choiceids); // Create fields for all choices. foreach ($choices as $id => $choice) { if (!in_array($choice->id, $deletechoicearray)) { $this->add_choice_group($mform, $choice); } else { // The nosubmit button has to be added since the form uses it for no_submit_button_pressed(). $mform->registerNoSubmitButton(self::DELETE_CHOICE_ACTION . $choice->id); } } if ($this->is_submitted()) { $subdata = $this->get_submitted_data(); $allstrategyoptions = $subdata->{self::STRATEGY_OPTIONS}; } else { if (isset($data->setting)) { $allstrategyoptions = json_decode($data->setting, true); } } // Add dynamic settings fields. foreach (\strategymanager::get_strategies() as $strategy) { // Load strategy class. $strategyclassp = 'ratingallocate\\' . $strategy . '\\strategy'; /* @var $strategyclass \strategytemplate */ if (isset($allstrategyoptions) && array_key_exists($strategy, $allstrategyoptions)) { $strategyclass = new $strategyclassp($allstrategyoptions[$strategy]); } else { $strategyclass = new $strategyclassp(); } $strategyplaceholder = self::STRATEGY_OPTIONS_PLACEHOLDER . '[' . $strategy . ']'; // Add options fields. $dynamicsettingsfields = $strategyclass->get_dynamic_settingfields(); foreach ($dynamicsettingsfields as $key => $value) { $fieldid = $this->get_settingsfield_identifier($strategy, $key); $this->add_settings_field($fieldid, $value, $strategy, $mform); $mform->insertElementBefore($mform->removeElement($fieldid, false), $strategyplaceholder); } // If any dynamic field is present, add a no submit button to refresh the page. if (count($dynamicsettingsfields) > 0) { $buttonname = self::STRATEGY_OPTIONS . $strategy . 'refresh'; $mform->registerNoSubmitButton($buttonname); $mform->addElement('submit', $buttonname, get_string('refresh')); $mform->insertElementBefore($mform->removeElement($buttonname, false), $strategyplaceholder); } $mform->removeElement($strategyplaceholder); } // UPDATE OF FORM VALUES NEEDS TO BE EXECUTED IN THE END!!! // Update new_choice_counter. $mform->getElement(self::NEW_CHOICE_COUNTER)->setValue($this->newchoicecounter); // Update delete_choice_string. if (!empty($deletechoicearray)) { $deletedchoiceids = implode(',', $deletechoicearray); $mform->getElement(self::DELETED_CHOICE_IDS)->setValue($deletedchoiceids); } }