/** * Adds an input element to the complete form * * @param stdClass $item * @param mod_feedback_complete_form $form */ public function complete_form_element($item, $form) { $name = $this->get_display_name($item); $inputname = $item->typ . '_' . $item->id; if ($form->get_mode() != mod_feedback_complete_form::MODE_COMPLETE) { $form->add_form_element($item, ['static', $inputname, $name], false, false); } else { $form->add_form_element($item, ['recaptcha', $inputname, $name], false, false); } // Add recaptcha validation to the form. $form->add_validation_rule(function ($values, $files) use($item, $form) { $elementname = $item->typ . '_' . $item->id; $recaptchaelement = $form->get_form_element($elementname); if (empty($values['recaptcha_response_field'])) { return array($elementname => get_string('required')); } else { if (!empty($values['recaptcha_challenge_field'])) { $challengefield = $values['recaptcha_challenge_field']; $responsefield = $values['recaptcha_response_field']; if (true !== ($result = $recaptchaelement->verify($challengefield, $responsefield))) { return array($elementname => $result); } } else { return array($elementname => get_string('missingrecaptchachallengefield')); } } return true; }); }
/** * Adds an input element to the complete form * * @param stdClass $item * @param mod_feedback_complete_form $form */ public function complete_form_element($item, $form) { $name = $this->get_display_name($item); $inputname = $item->typ . '_' . $item->id; list($cols, $rows) = explode("|", $item->presentation); $form->add_form_element($item, ['textarea', $inputname, $name, array('rows' => $rows, 'cols' => $cols)]); $form->set_element_type($inputname, PARAM_NOTAGS); }
/** * Adds an input element to the complete form * * @param stdClass $item * @param mod_feedback_complete_form $form */ public function complete_form_element($item, $form) { $form->add_form_element($item, ['static', $item->typ . '_' . $item->id, '', '<hr class="feedback_pagebreak">']); }
/** * Adds an input element to the complete form * * @param stdClass $item * @param mod_feedback_complete_form $form */ public function complete_form_element($item, $form) { $name = $this->get_display_name($item); $inputname = $item->typ . '_' . $item->id; $form->add_form_element($item, ['text', $inputname, $name], true, false); $form->set_element_type($inputname, PARAM_NOTAGS); $tmpvalue = $this->format_float($form->get_item_value($item)); $form->set_element_default($inputname, $tmpvalue); // Add form validation rule to check for boundaries. $form->add_validation_rule(function ($values, $files) use($item) { $inputname = $item->typ . '_' . $item->id; list($rangefrom, $rangeto) = explode('|', $item->presentation); if (!isset($values[$inputname]) || trim($values[$inputname]) === '') { return $item->required ? array($inputname => get_string('required')) : true; } $value = unformat_float($values[$inputname], true); if ($value === false) { return array($inputname => get_string('invalidnum', 'error')); } if (is_numeric($rangefrom) && $value < floatval($rangefrom) || is_numeric($rangeto) && $value > floatval($rangeto)) { return array($inputname => get_string('numberoutofrange', 'feedback')); } return true; }); }
/** * 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 * * @param stdClass $item * @param mod_feedback_complete_form $form */ public function complete_form_element($item, $form) { global $DB; if (!$item->feedback and $item->template) { // This is a template. $template = $DB->get_record('feedback_template', array('id' => $item->template)); if ($template->ispublic) { $context = context_system::instance(); } else { $context = context_course::instance($template->course); } $filearea = 'template'; } else { // This is a question in the current feedback. $context = $form->get_cm()->context; $filearea = 'item'; } $output = file_rewrite_pluginfile_urls($item->presentation, 'pluginfile.php', $context->id, 'mod_feedback', $filearea, $item->id); $formatoptions = array('overflowdiv' => true, 'noclean' => true); $output = format_text($output, FORMAT_HTML, $formatoptions); $inputname = $item->typ . '_' . $item->id; $name = $this->get_display_name($item); $form->add_form_element($item, ['static', $inputname, $name, $output], false, false); }
/** * Adds an input element to the complete form * * @param stdClass $item * @param mod_feedback_complete_form $form */ public function complete_form_element($item, $form) { $name = $this->get_display_name($item); $inputname = $item->typ . '_' . $item->id; list($size, $maxlength) = explode("|", $item->presentation); $form->add_form_element($item, ['text', $inputname, $name, ['maxlength' => $maxlength, 'size' => $size]]); $form->set_element_type($inputname, PARAM_NOTAGS); $form->add_element_rule($inputname, get_string('maximumchars', '', $maxlength), 'maxlength', $maxlength, 'client'); }
/** * Adds an input element to the complete form * * @param stdClass $item * @param mod_feedback_complete_form $form */ public function complete_form_element($item, $form) { if ($form->get_mode() == mod_feedback_complete_form::MODE_VIEW_RESPONSE) { $value = strval($form->get_item_value($item)); } else { $value = $this->get_current_value($item, $form->get_feedback(), $form->get_current_course_id()); } $printval = $this->get_printval($item, (object) ['value' => $value]); $class = ''; switch ($item->presentation) { case self::MODE_RESPONSETIME: $class = 'info-responsetime'; $value = $value ? self::CURRENTTIMESTAMP : ''; break; case self::MODE_COURSE: $class = 'info-course'; break; case self::MODE_CATEGORY: $class = 'info-category'; break; } $name = $this->get_display_name($item); $inputname = $item->typ . '_' . $item->id; $element = $form->add_form_element($item, ['select', $inputname, $name, array($value => $printval), array('class' => $class)], false, false); $form->set_element_default($inputname, $value); $element->freeze(); if ($form->get_mode() == mod_feedback_complete_form::MODE_COMPLETE) { $element->setPersistantFreeze(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; }); } }