/** * Adds an input element to the complete form * * @param stdClass $item * @param mod_feedback_complete_form $form */ public function complete_form_element($item, $form) { $info = $this->get_info($item); $name = $this->get_display_name($item); $class = 'multichoicerated-' . $info->subtype; $inputname = $item->typ . '_' . $item->id; $options = $this->get_options($item); if ($info->subtype === 'd' || $form->is_frozen()) { $el = $form->add_form_element($item, ['select', $inputname, $name, array('' => '') + $options, array('class' => $class)]); } else { $objs = array(); foreach ($options as $idx => $label) { $objs[] = ['radio', $inputname, '', $label, $idx]; } $separator = $info->horizontal ? ' ' : '<br>'; $class .= ' multichoicerated-' . ($info->horizontal ? 'horizontal' : 'vertical'); $el = $form->add_form_group_element($item, 'group_' . $inputname, $name, $objs, $separator, $class); // Set previously input values. $form->set_element_default($inputname, $form->get_item_value($item)); // Process "required" rule. if ($item->required) { $form->add_validation_rule(function ($values, $files) use($item) { $inputname = $item->typ . '_' . $item->id; return empty($values[$inputname]) ? array('group_' . $inputname => get_string('required')) : true; }); } } }
/** * Adds an input element to the complete form * * This element has many options - it can be displayed as group or radio elements, * group of checkboxes or a dropdown list. * * @param stdClass $item * @param mod_feedback_complete_form $form */ public function complete_form_element($item, $form) { $info = $this->get_info($item); $name = $this->get_display_name($item); $class = 'multichoice-' . $info->subtype; $inputname = $item->typ . '_' . $item->id; $options = $this->get_options($item); $separator = !empty($info->horizontal) ? ' ' : '<br>'; $tmpvalue = $form->get_item_value($item); if ($info->subtype === 'd' || $info->subtype === 'r' && $form->is_frozen()) { // Display as a dropdown in the complete form or a single value in the response view. $element = $form->add_form_element($item, ['select', $inputname . '[0]', $name, array(0 => '') + $options, array('class' => $class)], false, false); $form->set_element_default($inputname . '[0]', $tmpvalue); } else { if ($info->subtype === 'c' && $form->is_frozen()) { // Display list of checkbox values in the response view. $objs = []; foreach (explode(FEEDBACK_MULTICHOICE_LINE_SEP, $form->get_item_value($item)) as $v) { $objs[] = ['static', $inputname . "[{$v}]", '', isset($options[$v]) ? $options[$v] : '']; } $element = $form->add_form_group_element($item, 'group_' . $inputname, $name, $objs, $separator, $class); } else { // Display group or radio or checkbox elements. $class .= ' multichoice-' . ($info->horizontal ? 'horizontal' : 'vertical'); $objs = []; if ($info->subtype === 'c') { // Checkboxes. $objs[] = ['hidden', $inputname . '[0]', 0]; $form->set_element_type($inputname . '[0]', PARAM_INT); foreach ($options as $idx => $label) { $objs[] = ['advcheckbox', $inputname . '[' . $idx . ']', '', $label, null, array(0, $idx)]; $form->set_element_type($inputname . '[' . $idx . ']', PARAM_INT); } $element = $form->add_form_group_element($item, 'group_' . $inputname, $name, $objs, $separator, $class); if ($tmpvalue) { foreach (explode(FEEDBACK_MULTICHOICE_LINE_SEP, $tmpvalue) as $v) { $form->set_element_default($inputname . '[' . $v . ']', $v); } } } else { // Radio. foreach ($options as $idx => $label) { $objs[] = ['radio', $inputname . '[0]', '', $label, $idx]; } $element = $form->add_form_group_element($item, 'group_' . $inputname, $name, $objs, $separator, $class); $form->set_element_default($inputname . '[0]', $tmpvalue); } } } // Process 'required' rule. if ($item->required) { $elementname = $element->getName(); $form->add_validation_rule(function ($values, $files) use($elementname, $item) { $inputname = $item->typ . '_' . $item->id; return empty($values[$inputname]) || !array_filter($values[$inputname]) ? array($elementname => get_string('required')) : true; }); } }