You are here

function smarty_function_html_options in Quiz 6.5

Same name and namespace in other branches
  1. 6.6 includes/moodle/lib/smarty/plugins/function.html_options.php \smarty_function_html_options()

Smarty {html_options} function plugin

Type: function<br> Name: html_options<br> Input:<br>

  • name (optional) - string default "select"
  • values (required if no options supplied) - array
  • options (required if no values supplied) - associative array
  • selected (optional) - string default not set
  • output (required if not options supplied) - array

Purpose: Prints the list of <option> tags generated from the passed parameters @link http://smarty.php.net/manual/en/language.function.html.options.php {html_image} (Smarty online manual)

@uses smarty_function_escape_special_chars()

Parameters

array:

Smarty:

Return value

string

2 calls to smarty_function_html_options()
smarty_function_html_select_date in includes/moodle/lib/smarty/plugins/function.html_select_date.php
Smarty {html_select_date} plugin
smarty_function_html_select_time in includes/moodle/lib/smarty/plugins/function.html_select_time.php
Smarty {html_select_time} function plugin

File

includes/moodle/lib/smarty/plugins/function.html_options.php, line 29

Code

function smarty_function_html_options($params, &$smarty) {
  require_once $smarty
    ->_get_plugin_filepath('shared', 'escape_special_chars');
  $name = null;
  $values = null;
  $options = null;
  $selected = array();
  $output = null;
  $extra = '';
  foreach ($params as $_key => $_val) {
    switch ($_key) {
      case 'name':
        ${$_key} = (string) $_val;
        break;
      case 'options':
        ${$_key} = (array) $_val;
        break;
      case 'values':
      case 'output':
        ${$_key} = array_values((array) $_val);
        break;
      case 'selected':
        ${$_key} = array_map('strval', array_values((array) $_val));
        break;
      default:
        if (!is_array($_val)) {
          $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
        }
        else {
          $smarty
            ->trigger_error("html_options: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
        }
        break;
    }
  }
  if (!isset($options) && !isset($values)) {
    return '';
  }

  /* raise error here? */
  $_html_result = '';
  if (isset($options)) {
    foreach ($options as $_key => $_val) {
      $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
    }
  }
  else {
    foreach ($values as $_i => $_key) {
      $_val = isset($output[$_i]) ? $output[$_i] : '';
      $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
    }
  }
  if (!empty($name)) {
    $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
  }
  return $_html_result;
}