/** * Renders an <input type="button"> element. * * The element must have the 'value' field set. * * @param Pieform $form The form to render the element for * @param array $element The element to render * @return string The HTML for the element * @todo rename to inputbutton */ function pieform_element_button(Pieform $form, $element) { /*{{{*/ if (!isset($element['value'])) { throw new PieformException('Button elements must have a value'); } if (isset($element['confirm'])) { $element['data-confirm'] = Pieform::hsc($element['confirm']); } $element['class'] .= ' btn'; if (isset($element['usebuttontag']) && $element['usebuttontag'] === true) { $value = ''; $action = ''; $type = 'type="submit" '; if (isset($element['content'])) { $content = $element['content']; $value = 'value="' . Pieform::hsc($element['value']) . '" '; } else { $content = $element['value']; } if (isset($element['action'])) { $action = 'formaction="' . Pieform::hsc($element['action']) . '" '; } $button = '<button ' . $value . $action . $type . $form->element_attributes($element) . '>' . $content . '</button>'; } else { $button = '<input type="button"' . $form->element_attributes($element) . ' value="' . Pieform::hsc($element['value']) . '">'; } return $button; }
/** * Provides a javascript calendar for inputting a date. * * General documentation about the calendar is available at * http://www.dynarch.com/static/jscalendar-1.0/doc/html/reference.html * * @param Pieform $form The form to render the element for * @param array $element The element to render * @return string The HTML for the element */ function pieform_element_calendar(Pieform $form, $element) { /*{{{*/ $id = $form->get_name() . '_' . $element['name']; $value = $form->get_value($element); if ($value) { $value = Pieform::hsc(strftime($element['caloptions']['ifFormat'], $value)); } // Build the HTML $result = '<input type="text"' . $form->element_attributes($element) . ' value="' . $value . '">'; if (isset($element['imagefile'])) { $result .= '<a href="" id="' . $id . '_btn" onclick="return false;" class="pieform-calendar-toggle"' . ' tabindex="' . $element['tabindex'] . '">' . '<img src="' . $element['imagefile'] . '" alt=""></a>'; } else { $result .= '<input type="button" id="' . $id . '_btn" onclick="return false;" class="pieform-calendar-toggle"' . ' value="..." tabindex="' . $element['tabindex'] . '">'; } // Build the configuring javascript $options = array_merge($element['caloptions'], array('inputField' => $id, 'button' => $id . '_btn')); $encodedoptions = json_encode($options); // Some options are callbacks and need their quoting removed foreach (array('dateStatusFunc', 'flatCallback', 'onSelect', 'onClose', 'onUpdate') as $function) { $encodedoptions = preg_replace('/("' . $function . '"):"([a-zA-Z0-9$]+)"/', '\\1:\\2', $encodedoptions); } $result .= '<script type="text/javascript">Calendar.setup(' . $encodedoptions . ');</script>'; return $result; }
/** * Renders a basic HTML <textarea> element. * * @param array $element The element to render * @param Pieform $form The form to render the element for * @return string The HTML for the element */ function pieform_element_textarea(Pieform $form, $element) { /*{{{*/ global $_PIEFORM_TEXTAREAS; $rows = $cols = $style = ''; if (isset($element['height'])) { $style .= 'height:' . $element['height'] . ';'; $rows = intval($element['height'] > 0) ? ceil(intval($element['height']) / 10) : 1; } elseif (isset($element['rows'])) { $rows = $element['rows']; } else { Pieform::info('No value for rows or height specified for textarea "' . $element['name'] . '"'); } if (isset($element['width'])) { $style .= 'width:' . $element['width'] . ';'; $cols = intval($element['width'] > 0) ? ceil(intval($element['width']) / 10) : 1; } elseif (isset($element['cols'])) { $cols = $element['cols']; } else { Pieform::info('No value for cols or width specified for textarea "' . $element['name'] . '"'); } $element['style'] = isset($element['style']) ? $style . $element['style'] : $style; $fullwidth = !empty($element['fullwidth']) ? 'true' : 'false'; if (!empty($element['resizable'])) { $element['class'] = isset($element['class']) && $element['class'] ? $element['class'] . ' resizable' : 'resizable'; $_PIEFORM_TEXTAREAS[] = array('formname' => $form->get_name(), 'elementname' => $form->get_name() . '_' . $element['id'], 'fullwidth' => $fullwidth); } return '<textarea' . ($rows ? ' rows="' . $rows . '"' : '') . ($cols ? ' cols="' . $cols . '"' : '') . $form->element_attributes($element, array('maxlength', 'size')) . '>' . Pieform::hsc($form->get_value($element)) . '</textarea>'; }
/** * Renders a set of radio buttons for a form * * @param array $element The element to render * @param Pieform $form The form to render the element for * @return string The HTML for the element */ function pieform_element_radio(Pieform $form, $element) { /*{{{*/ if (!isset($element['options']) || !is_array($element['options']) || count($element['options']) < 1) { throw new PieformException('Radio elements should have at least one option'); } $result = ''; $form_value = $form->get_value($element); $id = $element['id']; $separator = "\n"; if (isset($element['separator'])) { $separator = $element['separator'] . $separator; } foreach ($element['options'] as $value => $data) { $uid = $id . substr(md5(microtime()), 0, 4); $element['id'] = $uid; if (is_array($data)) { $text = $data['text']; $description = isset($data['description']) ? $data['description'] : ''; } else { $text = $data; $description = ''; } $result .= '<input type="radio"' . $form->element_attributes($element) . ' value="' . Pieform::hsc($value) . '"' . ($form_value == $value ? ' checked="checked"' : '') . '> <label for="' . $form->get_name() . '_' . $uid . '">' . Pieform::hsc($text) . "</label>" . ($description != '' ? '<div class="radio-description">' . $description . '</div>' : '') . $separator; } $result = substr($result, 0, -strlen($separator)); return $result; }
/** * Renders a hidden element. * * @param Pieform $form The form to render the element for * @param array $element The element to render * @return string The HTML for the element */ function pieform_element_hidden(Pieform $form, $element) { /*{{{*/ if (!array_key_exists('value', $element)) { throw new PieformException('The hidden element "' . $element['name'] . '" must have a value set'); } if (!empty($element['sesskey']) && $form->get_property('method') != 'post') { throw new PieformException('Sesskey values should be POSTed'); } $value = $form->get_value($element); if (is_array($value)) { $result = ''; foreach ($value as $k => $v) { if (is_array($v)) { foreach ($v as $subk => $subv) { $result .= '<input type="hidden" name="' . Pieform::hsc($element['name']) . '[' . Pieform::hsc($k) . '][' . Pieform::hsc($subk) . ']" value="' . Pieform::hsc($subv) . "\">\n"; } } else { $result .= '<input type="hidden" name="' . Pieform::hsc($element['name']) . '[' . Pieform::hsc($k) . ']" value="' . Pieform::hsc($v) . "\">\n"; } } return $result; } return '<input type="hidden"' . $form->element_attributes($element, array('accesskey', 'onclick', 'size', 'style', 'tabindex')) . ' value="' . Pieform::hsc($form->get_value($element)) . "\">\n"; }
/** * Provides a password field that doesn't send the password text back to the user's browser. * If the value of the field is empty, then you get a standard input type=password. * If the field is non-empty, you get a link that says "Change password?" and if you click * on that, you get an input type=password. * The "Change password?" text can be customized by providing an "expandtext" value. * * @param Pieform $form The form to render the element for * @param array $element The element to render * @return string The HTML for the element */ function pieform_element_passwordnoread(Pieform $form, $element) { if (isset($element['defaultvalue']) && $element['defaultvalue'] == '') { return '<input type="password"' . $form->element_attributes($element) . ' value="">'; } else { $inputid = hsc($form->get_name() . '_' . $element['name']); $linktext = isset($element['expandtext']) ? hsc($element['expandtext']) : get_string('changepassword'); $html = '<a href="" ' . "onclick=\"" . "addElementClass('{$inputid}_expand', 'hidden'); " . "jQuery('#{$inputid}').attr('name', '{$element['name']}');" . "removeElementClass('{$inputid}', 'hidden'); " . "return false;" . "\" id=\"{$inputid}_expand\">" . $linktext . '</a>'; $element['class'] .= ' hidden'; // This password input starts out invisible, and with a placeholder name (so that // it won't be processed by the form). When you click the link, it becomes visible // and gains its real name. // TODO: Non-JS version. return $html . '<input type="password" name="' . hsc($element['name'] . '_placeholder') . '" ' . $form->element_attributes($element, array('name')) . ' value="">'; } }
/** * Provides a basic checkbox input. * * @param Pieform $form The form to render the element for * @param array $element The element to render * @return string The HTML for the element */ function pieform_element_checkbox(Pieform $form, $element) { /*{{{*/ $checked = false; if (isset($element['rules']['required'])) { throw new PieformException("For pieform_element_checkbox, 'required' is not allowed as a rule. Radio should be used instead."); } if (!empty($element['value'])) { $checked = true; } $global = $form->get_property('method') == 'get' ? $_GET : $_POST; if ($form->is_submitted() && isset($global[$element['name']])) { $checked = true; } else { if (!$form->is_submitted() && !empty($element['defaultvalue'])) { $checked = true; } else { if (!empty($element['readonly']) && !empty($element['defaultvalue'])) { $checked = true; } } } $arialabel = ''; if (!empty($element['arialabel'])) { $arialabel = ' aria-label="' . Pieform::hsc($element['title']) . '"'; } return '<input type="checkbox"' . $form->element_attributes($element) . $arialabel . ($checked ? ' checked="checked"' : '') . '>'; }
/** * Renders a set (red, yellow, green) of radio buttons with corresponding background colors as in traffic lights * * @param array $element The element to render * @param Pieform $form The form to render the element for * @return string The HTML for the element */ function pieform_element_trafficlights(Pieform $form, $element) { /*{{{*/ $global = $form->get_property('method') == 'get' ? $_GET : $_POST; $submitted = $form->is_submitted(); if ($submitted && isset($global[$element['name']])) { $value = $global[$element['name']]; } $result = ''; if (!isset($element['options'])) { $element['options'] = array(array('value' => '0', 'title' => 'r'), array('value' => '1', 'title' => 'y'), array('value' => '2', 'title' => 'g')); } $result .= "<span style='display:block;border:0px;padding-bottom:4px;width:70px !important;'>"; $colors = array('r' => '#FF8080', 'y' => '#FFEB80', 'g' => '#80B280'); $borders = array('r' => 'border-top:1px solid #000000;border-left:1px solid #000000;border-bottom:1px solid #000000;', 'y' => 'border-top:1px solid #000000;border-bottom:1px solid #000000', 'g' => 'border-top:1px solid #000000;border-right:1px solid #000000;border-bottom:1px solid #000000;'); foreach ($element['options'] as $e) { $checked = $form->get_value($element) === $e['value'] && !is_null($form->get_value($element)); if ($e['value'] == null) { $checked = false; } $result .= '<span style="' . $borders[$e['title']] . ';padding-bottom:4px;background-color:' . $colors[$e['title']] . '"><input type="radio" value="' . $e['value'] . '" ' . $form->element_attributes($element) . ($checked ? ' checked="checked"' : '') . ' style="vertical-align:top"></span>'; } $result .= "</span>"; $result .= '<div class="cl"></div>'; return $result; }
/** * Renders a submit button * * @param Pieform $form The form to render the element for * @param array $element The element to render * @return string The HTML for the element */ function pieform_element_submit(Pieform $form, $element) { /*{{{*/ if (isset($element['confirm'])) { $element['onclick'] = 'return confirm(' . json_encode($element['confirm']) . ');'; } return '<input type="submit"' . $form->element_attributes($element) . ' value="' . Pieform::hsc($form->get_value($element)) . '">'; }
/** * Renders an <input type="button"> element. * * The element must have the 'value' field set. * * @param Pieform $form The form to render the element for * @param array $element The element to render * @return string The HTML for the element * @todo rename to inputbutton */ function pieform_element_button(Pieform $form, $element) { /*{{{*/ if (!isset($element['value'])) { throw new PieformException('Button elements must have a value'); } return '<input type="button"' . $form->element_attributes($element) . ' value="' . Pieform::hsc($element['value']) . '">'; }
/** * Renders a basic HTML <input type="file"> element. * * @param Pieform $form The form to render the element for * @param array $element The element to render * @return string The HTML for the element */ function pieform_element_file(Pieform $form, $element) { /*{{{*/ $result = ''; if (isset($element['maxfilesize']) && is_int($element['maxfilesize'])) { $result = '<input type="hidden" name="MAX_FILE_SIZE" value="' . $element['maxfilesize'] . '"/>'; } $result .= '<input type="file"' . $form->element_attributes($element) . '>'; return $result; }
/** * Renders an <input type="image"> button * * @param Pieform $form The form to render the element for * @param array $element The element to render * @return string The HTML for the element */ function pieform_element_image(Pieform $form, $element) { /*{{{*/ if (!isset($element['src'])) { throw new PieformException('"image" elements must have a "src" for the image'); } if (!isset($element['value'])) { $element['value'] = true; } return '<input type="image" src="' . Pieform::hsc($element['src']) . '"' . $form->element_attributes($element) . ' value="' . Pieform::hsc($form->get_value($element)) . '">'; }
/** * Renders a set of radio buttons for a form * * @param Pieform $form The form to render the element for * @param array $element The element to render. In addition to the standard Pieform * element attributes, it can also take the following optional * attributes: * - separator: The HTML string that should separate the radio * buttons (defaults to \n, always has \n appended to it) * - rowsize: How many radio buttons to print per row (defaults to 1) * - nolabels: Don't print the labels next to the individual radio buttons. * @return string The HTML for the element */ function pieform_element_radio(Pieform $form, $element) { if (!isset($element['options']) || !is_array($element['options']) || count($element['options']) < 1) { throw new PieformException('Radio elements should have at least one option'); } $result = ''; $form_value = $form->get_value($element); $id = $element['id']; $separator = "\n"; if (isset($element['separator'])) { $separator = $element['separator'] . $separator; } $rowsize = isset($element['rowsize']) ? (int) $element['rowsize'] : 1; $nolabels = isset($element['nolabels']) ? $element['nolabels'] : false; $classname = ''; if (!empty($element['hiddenlabels'])) { $classname = ' class="accessible-hidden"'; } $titletext = ''; if (!empty($element['title'])) { $titletext = '<span class="accessible-hidden">' . Pieform::hsc($element['title']) . ': </span>'; } $i = 0; foreach ($element['options'] as $value => $data) { $idsuffix = substr(md5(microtime()), 0, 4); $baseid = $element['id']; $element['id'] = $uid = $id . $idsuffix; if (is_array($data)) { $text = $data['text']; $description = isset($data['description']) ? $data['description'] : ''; } else { $text = $data; $description = ''; } $attributes = $form->element_attributes($element); $attributes = preg_replace("/aria-describedby=\"[^\"]*{$baseid}{$idsuffix}_description\\s*[^\"]*\"/", 'aria-describedby="$1_description"', $attributes); $result .= '<div class="radio">'; $result .= '<input type="radio"' . $attributes . ' value="' . Pieform::hsc($value) . '"' . ($form_value == $value ? ' checked="checked"' : '') . '>'; if (!$nolabels) { $result .= ' <label for="' . $form->get_name() . '_' . $uid . '"' . $classname . '>' . $titletext . Pieform::hsc($text) . "</label>" . ($description != '' ? '<div class="description">' . $description . '</div>' : ''); } $result .= '</div>'; $i++; if ($rowsize <= 1 || $i % $rowsize == 0) { $result .= $separator; } } // If there was a separator printed on the end, then remove it if ($rowsize <= 1 || $i % $rowsize == 0) { $result = substr($result, 0, -strlen($separator)); } return $result; }
/** * Provides a control that allows the user to input where in a list they want * to put something. Handy for indicating the order of some objects. * * @param Pieform $form The form to render the element for * @param array $element The element to render * @return string The HTML for the element */ function pieform_element_weight(Pieform $form, $element) { pieform_element_weight_validate_element($element); $default = isset($element['defaultvalue']) ? intval($element['defaultvalue']) : 0; $result = '<input type="radio"' . $form->element_attributes($element) . ' value="0"'; if ($default == 0) { $result .= ' checked="checked"'; } $result .= '>'; $i = 0; foreach ($element['existing'] as $existing) { $i++; $result .= "<br>" . Pieform::hsc($existing['title']); $result .= "<br><input type=\"radio\"" . $form->element_attributes($element) . " value=\"{$i}\""; if ($i == $default) { $result .= ' checked="checked"'; } $result .= '>'; } return $result; return '<input type="text"' . $form->element_attributes($element) . ' value="' . Pieform::hsc($form->get_value($element)) . '">'; }
/** * Renders a "cancel" button. Custom buttons are rendered nearly the same as * normal submit buttons, only their name is changed (for use by the Pieform * class internally). * * @param Pieform $form The form to render the element for * @param array $element The element to render * @return string The HTML for the element */ function pieform_element_cancel(Pieform $form, $element) { /*{{{*/ if (!isset($element['value'])) { throw new PieformException('Cancel elements must have a value'); } if (isset($element['confirm'])) { $element['onclick'] = 'return confirm(' . json_encode($element['confirm']) . ');'; } $attributes = $form->element_attributes($element); $attributes = preg_replace('/name="(.*)"/', 'name="cancel_$1"', $attributes); $attributes = preg_replace('/id="(.*)"/', 'id="cancel_$1"', $attributes); return '<input type="submit"' . $attributes . ' value="' . Pieform::hsc($element['value']) . '">'; }
/** * Provides a basic text field input. * * @param Pieform $form The form to render the element for * @param array $element The element to render * @return string The HTML for the element */ function pieform_element_text(Pieform $form, $element) { /*{{{*/ $value = Pieform::hsc($form->get_value($element)); $html = ''; // If hidewhenempty is set, the text box is hidden by a link which expands it. if (!empty($element['hidewhenempty']) && $value == '') { $inputid = hsc($form->get_name() . '_' . $element['name']); $linktext = $element['expandtext'] ? hsc($element['expandtext']) : get_string('edit'); $html .= '<a class="btn btn-default" href="" ' . "onclick=\"addElementClass('{$inputid}_expand', 'hidden'); removeElementClass('{$inputid}', 'hidden'); return false;\"" . "id=\"{$inputid}_expand\">" . $linktext . '</a>'; $element['class'] .= ' hidden'; } return $html . '<input type="text"' . $form->element_attributes($element) . ' value="' . $value . '">'; }
/** * Provides a basic checkbox input. * * @param Pieform $form The form to render the element for * @param array $element The element to render * @return string The HTML for the element */ function pieform_element_checkbox(Pieform $form, $element) { /*{{{*/ $checked = false; if (!empty($element['value'])) { $checked = true; } $global = $form->get_property('method') == 'get' ? $_GET : $_POST; if (isset($global[$element['name']])) { $checked = true; } else { if (!$form->is_submitted() && !empty($element['defaultvalue'])) { $checked = true; } } return '<input type="checkbox"' . $form->element_attributes($element) . ($checked ? ' checked="checked"' : '') . '>'; }
/** * Renders a drag & drop sortable list using Javascript and CSS. * Useful for ranking answers to a question. * * Based on ToolMan DHTML Library. See: http://tool-man.org/examples/sorting.html * * @param Pieform $form The form to render the element for * @param array $element The element to render * @return string The HTML for the element */ function pieform_element_rank(Pieform $form, $element) { /*{{{*/ $optionsavailable = true; if (!isset($element['options']) || !is_array($element['options']) || count($element['options']) < 1) { $optionsavailable = false; Pieform::info('Rank elements should have at least one option'); } if (!empty($element['collapseifoneoption']) && isset($element['options']) && is_array($element['options']) && count($element['options']) == 1) { foreach ($element['options'] as $key => $value) { if (is_array($value)) { $value = $value['value']; } $result = Pieform::hsc($value) . '<input type="hidden" name="' . Pieform::hsc($element['name']) . '" value="' . Pieform::hsc($key) . '">'; } return $result; } $result = '<ul' . $form->element_attributes($element) . ">\n"; if (!$optionsavailable) { $result .= "\t<li> </li>\n</ul>"; return $result; } // Values are serialized like: a|b|c|d // so we need to unserialze them... $values = explode('|', $form->get_value($element)); $optionselected = false; foreach ($element['options'] as $key => $value) { // Get the value to display/put in the value attribute if (is_array($value)) { if (!isset($value['value'])) { Pieform::info('No value set for option "' . $key . '" of rank element "' . $element['name'] . '"'); $value = ''; } else { $value = $value['value']; } } $result .= "\t<li itemID=\"" . Pieform::hsc($key) . "\">" . Pieform::hsc($value) . "</li>\n"; } if (!$optionselected && !is_array($values) && $values !== null) { Pieform::info('Invalid value for rank "' . $element['name'] . '"'); } $result .= '</ul>'; return $result; }
/** * Renders an <input type="image"> button * * @param Pieform $form The form to render the element for * @param array $element The element to render * @return string The HTML for the element */ function pieform_element_image(Pieform $form, $element) { /*{{{*/ if (!isset($element['src'])) { throw new PieformException('"image" elements must have a "src" for the image'); } if (!isset($element['value'])) { $element['value'] = true; } if (isset($element['confirm'])) { $element['onclick'] = 'return confirm(' . json_encode($element['confirm']) . ');'; } if (!isset($element['alt'])) { if (isset($element['elementtitle'])) { $element['alt'] = $element['elementtitle']; } else { $element['alt'] = ''; } } return '<input type="image" src="' . Pieform::hsc($element['src']) . '"' . ' alt="' . Pieform::hsc($element['alt']) . '"' . $form->element_attributes($element) . ' value="' . Pieform::hsc($form->get_value($element)) . '">'; }
/** * Renders a textarea, but with extra javascript to turn it into a wysiwyg * textarea. * * @todo support resizable. * * @param array $element The element to render * @param Pieform $form The form to render the element for * @return string The HTML for the element */ function pieform_element_wysiwyg(Pieform $form, $element) { global $_PIEFORM_WYSIWYGS; $_PIEFORM_WYSIWYGS[] = $form->get_name() . '_' . $element['name']; if (is_html_editor_enabled()) { if (!$form->get_property('elementclasses')) { $element['class'] = isset($element['class']) && $element['class'] !== '' ? $element['class'] . ' wysiwyg' : 'wysiwyg'; } } $rows = $cols = $style = ''; if (isset($element['height'])) { $style .= 'height:' . $element['height'] . ';'; $rows = intval($element['height'] > 0) ? ceil(intval($element['height']) / 10) : 1; } elseif (isset($element['rows'])) { $rows = $element['rows']; } else { log_warn('No value for rows or height specified for textarea ' . $element['name']); } if (isset($element['width'])) { $style .= 'width:' . $element['width'] . ';'; $cols = intval($element['width'] > 0) ? ceil(intval($element['width']) / 10) : 1; } elseif (isset($element['cols'])) { $cols = $element['cols']; } else { log_warn('No value for cols or width specified for textarea ' . $element['name']); } $element['style'] = isset($element['style']) ? $style . $element['style'] : $style; if (is_html_editor_enabled()) { $value = Pieform::hsc($form->get_value($element)); } else { // Replace <br>s as added by wysiwyg editor or nl2br with a newline $value = preg_replace("#<br />\\s#", "\n", $form->get_value($element)); // As placed in the value by the wysiwyg editor $value = str_replace('</p><p>', "\n\n", $value); // Find the last </p> and replace with newlines $value = preg_replace('#</p>\\s#', "\n", $value); $value = strip_tags($value); } return '<textarea' . ($rows ? ' rows="' . $rows . '"' : '') . ($cols ? ' cols="' . $cols . '"' : '') . $form->element_attributes($element, array('maxlength', 'size')) . '>' . $value . '</textarea>'; }
/** * * @package mahara * @subpackage form-element * @author Catalyst IT Ltd * @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later * @copyright For copyright information on Mahara, please see the README file distributed with this software. * */ function pieform_element_checkboxes(Pieform $form, $element) { /*{{{*/ $global = $form->get_property('method') == 'get' ? $_GET : $_POST; $submitted = $form->is_submitted(); if ($submitted) { $value = isset($global[$element['name']]) ? $global[$element['name']] : array(); } $result = ''; if (count($element['elements']) > 1) { $id = hsc($form->get_name() . '_' . $element['name']) . '_container'; $result .= '<a href="" onclick="pieform_element_checkboxes_update(\'' . $id . '\', true); return false;">' . get_string('selectall') . '</a>' . ' ' . ' <a href="" onclick="pieform_element_checkboxes_update(\'' . $id . '\', false); return false;">' . get_string('selectnone') . '</a>'; } $result .= '<div class="cl"></div>'; $element['name'] .= '[]'; // Number of characters in checkbox labels (use 0 or false for no limit). $labelwidth = isset($element['labelwidth']) ? (int) $element['labelwidth'] : 17; $elementtitle = ''; if (isset($element['title'])) { $elementtitle = '<span class="accessible-hidden">' . $element['title'] . ': </span>'; } foreach ($element['elements'] as $e) { $id = $form->get_name() . '_' . $element['id']; $idsuffix = substr(md5(microtime()), 0, 4); if (!$submitted || !empty($e['disabled'])) { $checked = $e['defaultvalue']; } else { $checked = !empty($value[$e['value']]) || in_array($e['value'], $value); } $attributes = $form->element_attributes($element); $attributes = preg_replace("/\\bid=\"{$id}\"/", "id=\"{$id}{$idsuffix}\"", $attributes); $title = $labelwidth ? str_shorten_text($e['title'], $labelwidth, true) : $e['title']; $result .= '<div class="checkboxes-option"><input type="checkbox" value="' . $e['value'] . '" ' . $attributes . ($checked ? ' checked="checked"' : '') . (!empty($e['disabled']) ? ' disabled' : '') . '>' . ' <label class="checkbox" for="' . $id . $idsuffix . '">' . $elementtitle . Pieform::hsc($title) . '</label></div>'; } $result .= '<div class="cl"></div>'; return $result; }
/** * Renders a password field * * @param array $element The element to render * @param Pieform $form The form to render the element for * @return string The HTML for the element */ function pieform_element_password(Pieform $form, $element) { /*{{{*/ return '<input type="password"' . $form->element_attributes($element) . ' value="' . Pieform::hsc($form->get_value($element)) . '">'; }
/** * Renders a set of radio buttons for scale type questions in surveys. * * @param array $element The element to render * @param Pieform $form The form to render the element for * @return string The HTML for the element */ function pieform_element_scale(Pieform $form, $element) { /*{{{*/ $global = $form->get_property('method') == 'get' ? $_GET : $_POST; $submitted = $form->is_submitted(); if ($submitted && isset($global[$element['name']])) { $value = $global[$element['name']]; } $result = ''; $separator = null; //$separator = ''; if (isset($element['separator'])) { $separator = $element['separator']; } $length = 17; if (isset($element['length'])) { $length = $element['length']; } if (!isset($element['options'])) { $steps = 5; if (isset($element['steps'])) { if ($element['steps'] < 2) { $steps = 2; } elseif ($element['steps'] > 10) { $steps = 10; } else { $steps = $element['steps']; } } $element['options'] = array(); for ($i = 0; $i < $steps; $i++) { $element['options'] = array_merge($element['options'], array(array('value' => strval($i), 'title' => ''))); } } // If needed, set answers in reverse order if (isset($element['reverse']) && $element['reverse'] == 'true') { $element['options'] = array_reverse($element['options']); } //$result .= "<span style='display:block-inline'>"; $result .= "<table width='100%' border='0' cellpadding='0' cellspacing='0'><tr>"; $result .= "<td width='45%' valign='top' align='right'>"; if (isset($element['labelleft'])) { $result .= "<strong>" . $element['labelleft'] . "</strong>"; } if (isset($element['titleleft'])) { $result .= $element['titleleft']; } $result .= "</td>"; $result .= "<td width='10%' valign='top' align='center'>"; $result .= "<div style='white-space:nowrap;'>"; foreach ($element['options'] as $e) { $checked = $form->get_value($element) === $e['value'] && !is_null($form->get_value($element)); if ($e['value'] == null) { $checked = false; } $result .= '<input type="radio" value="' . $e['value'] . '" ' . $form->element_attributes($element) . ($checked ? ' checked="checked"' : '') . ' style="vertical-align:top">' . Pieform::hsc(str_shorten_text($e['title'], $length, true)); $result .= $separator; } // Strip the separator after the last option... //$result = substr($result, 0, -strlen($separator)); $result .= "</div>"; $result .= "</td>"; $result .= "<td width='45%' valign='top' align='left'>"; if (isset($element['titleright'])) { $result .= $element['titleright']; } if (isset($element['labelright'])) { $result .= "<strong>" . $element['labelright'] . "</strong>"; } $result .= "</td>"; $result .= "</tr></table>"; //$result .= "</span>"; $result .= '<div class="cl"></div>'; return $result; }
/** * Provides a javascript calendar for inputting a date/time. * * General documentation about the calendar is available at * http://api.jqueryui.com/datepicker/ * General documentation about the timepicker addon is available at * http://trentrichardson.com/examples/timepicker/ * * @param Pieform $form The form to render the element for * @param array $element The element to render * @return string The HTML for the element */ function pieform_element_calendar(Pieform $form, $element) { /*{{{*/ global $LANGDIRECTION; $id = $form->get_name() . '_' . $element['name']; $value = $form->get_value($element); if ($value) { $value = Pieform::hsc(strftime($element['caloptions']['ifFormat'], $value)); } // Build the configuring javascript $options = array_merge($element['caloptions'], array('inputField' => $id)); if (empty($options['dateFormat'])) { $options['dateFormat'] = get_string('calendar_dateFormat', 'langconfig'); } // Set up default timeFormat if needed if (!empty($options['showsTime']) && empty($options['timeFormat'])) { $options['timeFormat'] = get_string('calendar_timeFormat', 'langconfig'); } $options = pieform_element_calendar_get_lang_strings($options, $LANGDIRECTION); // Build the HTML $result = '<input type="text"' . $form->element_attributes($element) . ' value="' . $value . '">'; $result .= '<script type="text/javascript"> var input = jQuery("input#' . $id . '");'; if (!empty($options['showsTime'])) { $result .= 'input.datetimepicker({'; } else { $result .= 'input.datepicker({'; } $result .= ' onSelect: function(date) { if (typeof formchangemanager !== \'undefined\') { var form = input.closest(\'form\')[0]; formchangemanager.setFormState(form, FORM_CHANGED); } },'; foreach ($options as $key => $option) { if (is_numeric($option)) { $result .= $key . ': ' . $option . ','; } else { if (is_array($option)) { foreach ($option as $k => $v) { if (!is_numeric($v)) { if (preg_match('/^\'(.*)\'$/', $v, $match)) { $v = $match[1]; } $option[$k] = json_encode($v); } } $option = '[' . implode(',', $option) . ']'; $result .= $key . ': ' . $option . ','; } else { $result .= $key . ': ' . json_encode($option) . ','; } } } // Adding prev / next year buttons $result .= ' beforeShow: function(input, inst) { setTimeout(function() { add_prev_next_year(inst); }, 1); }, onChangeMonthYear: function(y, m, inst) { setTimeout(function() { add_prev_next_year(inst); }, 1); }, '; if (isset($element['imagefile'])) { $result .= 'showOn: "button", buttonImage: "' . $element['imagefile'] . '", buttonText: "' . get_string('element.calendar.opendatepicker', 'pieforms') . '",'; } $result .= ' }); </script>'; return $result; }
/** * Renders a dropdown list, including support for multiple choices. * * Configuration options: * * In addition to the standard pieform element configuration options, the select element * can take these. Each one must include an "options", "optgroups", or "optionurl" * parameter, because otherwise it won't have any options. * * - "options" (array): The list of items in the menu. A little confusingly, the array key * for each array entry is used as the "value" attribute for the <option> tag. Each array * value should usually be a string, in which case it will be displayed as the text between * the <option> tags. Or for more advanced functionality each array value can be an array * with these keys: * --- "value": The string to display inside the <option> tags * --- "disabled" (boolean default false): Set to true to disable this option * --- "style": CSS styling to set on this option * --- "label": sets a "label" attribute on the <option> tag * * - "optgroups" (array of arrays): Provide this INSTEAD of "options" if you want to use * optgroups. This should be an array of arrays. Each entry in the array represents an * optgroup (the key is ignored). Each optgroup array should contain an item with the * key "label", which will be the user-displayed label, and an item with the key "options", * which should be a list of options to go in that outgroup, with the same format as a * standard "options" array. * * - "multiple" (boolean, default false): Indicates whether to allow multiple items to be selected * * - "collapseifoneoption" (boolean, default true): If there's only one option, print it as a string * instead of as a menu. * * - "allowother" (boolean, default false): Says to add an "other" free text field to the menu. If there * is a menu option with value "other", then selecting this will display the free text * field. If there is no menu option with value "other", one will be added to the option * list. * * - "width" (deprecated): How wide to make this select element in pixels. Better to do this with CSS. * * - "height" (deprecated): How tall to make this select element in pixels. Better to do this with CSS. * * @todo Currently, putting a junk defaultvalue/value for a multiple select * does not trigger any kind of error, it should perhaps trigger a * Pieform::info * * @param Pieform $form The form to render the element for * @param array $element The element to render * @return string The HTML for the element */ function pieform_element_select(Pieform $form, $element) { if (!empty($element['multiple'])) { $element['name'] .= '[]'; } if (!empty($element['allowother']) and !isset($element['options']['other'])) { $element['options']['other'] = get_string('element.select.other', 'pieforms'); } $optionsavailable = true; $options = pieform_element_select_get_options($element); if (empty($options)) { $optionsavailable = false; Pieform::info('Select elements should have at least one option'); } if (!empty($element['collapseifoneoption']) && is_array($options) && count($options) == 1) { foreach ($options as $key => $value) { if (is_array($value)) { $value = $value['value']; } $result = Pieform::hsc($value) . '<input type="hidden" id="' . $form->make_id($element, true) . '" name="' . Pieform::hsc($element['name']) . '" value="' . Pieform::hsc($key) . '">'; } return $result; } $result = '<select' . $form->element_attributes($element) . (!empty($element['multiple']) ? ' multiple="multiple"' : '') . (!empty($element['allowother']) ? ' onChange="pieform_select_other(this);"' : '') . (!empty($element['width']) ? ' style="width: ' . $element['width'] . 'px;' : ' style="') . (!empty($element['height']) ? ' height: ' . $element['height'] . 'px;"' : '"') . ">\n"; if (!$optionsavailable) { $result .= "\t<option></option>\n</select>"; return $result; } $values = $form->get_value($element); $optionselected = false; if (!empty($element['allowother'])) { $use_other = $values; foreach ($element['options'] as $key => $value) { if (!is_array($values) && $key == $values || is_array($values) && in_array($key, $values)) { unset($use_other); break; } } if (isset($use_other)) { $values = 'other'; } } if (empty($element['optgroups'])) { $result .= pieform_element_select_render_options($element['options'], $values, $optionselected, $element); } else { foreach ($element['optgroups'] as $optgroup) { $result .= "\t<optgroup label=\"" . hsc($optgroup['label']) . '">' . "\n" . pieform_element_select_render_options($optgroup['options'], $values, $optionselected, $element) . "\t</optgroup>\n"; } } if (!$optionselected && !is_array($values) && $values !== null) { Pieform::info('Invalid value for select "' . $element['name'] . '"'); } $result .= '</select>'; if (!empty($element['allowother'])) { $other_attrib = array('name' => $element['name'] . '_other', 'id' => $element['id'] . '_other'); if (isset($use_other)) { $other_value = ' value="' . hsc($use_other) . '"'; } else { $other_attrib['class'] = 'hidden'; $other_value = ''; } $result .= '<label for="' . $element['id'] . '_other" class="accessible-hidden">' . get_string('licenseotherurl') . '</label>' . '<input type="text"' . $form->element_attributes($other_attrib) . $other_value . ">\n"; } return $result; }
/** * Renders a dropdown list, including support for multiple choices. * * @todo Currently, putting a junk defaultvalue/value for a multiple select * does not trigger any kind of error, it should perhaps trigger a * Pieform::info * * @param Pieform $form The form to render the element for * @param array $element The element to render * @return string The HTML for the element */ function pieform_element_select(Pieform $form, $element) { /*{{{*/ if (!empty($element['multiple'])) { $element['name'] .= '[]'; } $optionsavailable = true; if (!isset($element['options']) || !is_array($element['options']) || count($element['options']) < 1) { $optionsavailable = false; Pieform::info('Select elements should have at least one option'); } if (!empty($element['collapseifoneoption']) && isset($element['options']) && is_array($element['options']) && count($element['options']) == 1) { foreach ($element['options'] as $key => $value) { if (is_array($value)) { $value = $value['value']; } $result = $value . '<input type="hidden" name="' . $element['name'] . '" value="' . $key . '">'; } return $result; } $result = '<select' . $form->element_attributes($element) . (!empty($element['multiple']) ? ' multiple="multiple"' : '') . ">\n"; if (!$optionsavailable) { $result .= "\t<option></option>\n</select>"; return $result; } $values = $form->get_value($element); $optionselected = false; foreach ($element['options'] as $key => $value) { // Select the element if it's in the values or if there are no values // and this is the first option if (!is_array($values) && $key == $values || is_array($values) && (in_array($key, $values) || isset($values[0]) && $values[0] === null && !$optionselected)) { $selected = ' selected="selected"'; $optionselected = true; } else { $selected = ''; } // Disable the option if necessary if (is_array($value) && !empty($value['disabled'])) { $disabled = ' disabled="disabled"'; } else { $disabled = ''; } // Add a label if necessary. None of the common browsers actually render // this properly at the moment, but that may change in future. if (is_array($value) && isset($value['label'])) { $label = ' label="' . Pieform::hsc($value['label']) . '"'; } else { $label = ''; } // Get the value to display/put in the value attribute if (is_array($value)) { if (!isset($value['value'])) { Pieform::info('No value set for option "' . $key . '" of select element "' . $element['name'] . '"'); $value = ''; } else { $value = $value['value']; } } $result .= "\t<option value=\"" . Pieform::hsc($key) . "\"{$selected}{$label}{$disabled}>" . Pieform::hsc($value) . "</option>\n"; } if (!$optionselected && !is_array($values) && $values !== null) { Pieform::info('Invalid value for select "' . $element['name'] . '"'); } $result .= '</select>'; return $result; }
/** * Renders a basic HTML <input type="file"> element. * * @param Pieform $form The form to render the element for * @param array $element The element to render * @return string The HTML for the element */ function pieform_element_file(Pieform $form, $element) { /*{{{*/ return '<input type="file"' . $form->element_attributes($element) . '>'; }