'; if ($nothing != '') { $output .= '"; } } } $output .= ""; if ($return) return $output; echo $output; } /** * Prints an array of fields in a popup menu of a form based on a SQL query. * The first and second columns of the query will be used. * * Based on choose_from_menu() from Moodle * * $sql SQL sentence, the first field will be the identifier of the option. * The second field will be the shown value in the dropdown. * $name Select form name * $selected Current selected value. * $script Javascript onChange code. * $nothing Label when nothing is selected. * $nothing_value Value when nothing is selected */ function print_select_from_sql ($sql, $name, $selected = '', $script = '', $nothing = 'select', $nothing_value = '0', $return = false, $multiple = false, $sort = true) { $fields = array (); $result = mysql_query ($sql); if (! $result) { echo mysql_error (); return ""; } while ($row = mysql_fetch_array ($result)) { $fields[$row[0]] = $row[1]; } $output = print_select ($fields, $name, $selected, $script, $nothing, $nothing_value, true, $multiple, $sort); if ($return) return $output; echo $output; } /** * Render an input text element. Extended version, use print_input_text() to simplify. * * @param string $name Input name. * @param string $value Input value. * @param string $alt Alternative HTML string. * @param int $size Size of the input. * @param int $maxlength Maximum length allowed. * @param bool $disabled Disable the button (optional, button enabled by default). * @param string $alt Alternative HTML string. * @param bool $return Whether to return an output string or echo now (optional, echo by default). */ function print_input_text_extended ($name, $value, $id, $alt, $size, $maxlength, $disabled, $script, $attributes, $return = false, $password = false) { static $idcounter = 0; ++$idcounter; $type = $password ? 'password' : 'text'; if (empty ($name)) { $name = 'unnamed'; } if (empty ($alt)) { $alt = 'textfield'; } if (! empty ($maxlength)) { $maxlength = ' maxlength="'.$maxlength.'" '; } $output = ''; if ($return) return $output; echo $output; } function print_submit_button ($label = 'OK', $name = '', $disabled = false, $attributes = '', $return = false) { $output = ''; $output .= ''; $output .= $value; $output .= ''; if ($return) return $output; echo $output; } /** * Print a nicely formatted table. Code taken from moodle. * * @param array $table is an object with several properties: * $table->head - An array of heading names. * $table->align - An array of column alignments * $table->valign - An array of column alignments * $table->size - An array of column sizes * $table->wrap - An array of "nowrap"s or nothing * $table->style - An array of personalized style for each column. * $table->rowstyle - An array of personalized style of each row. * $table->rowclass - An array of personalized classes of each row (odd-evens classes will be ignored). * $table->colspan - An array of colspans of each column. * $table->data[] - An array of arrays containing the data. * $table->width - A percentage of the page * $table->border - Border of the table. * $table->tablealign - Align the whole table * $table->cellpadding - Padding on each cell * $table->cellspacing - Spacing between cells * $table->class - CSS table class * @param bool $return whether to return an output string or echo now */ function print_table (&$table, $return = false) { $output = ''; static $table_count = 0; $table_count++; if (isset ($table->align)) { foreach ($table->align as $key => $aa) { if ($aa) { $align[$key] = ' text-align:'. $aa.';'; } else { $align[$key] = ''; } } } if (isset ($table->valign)) { foreach ($table->valign as $key => $aa) { if ($aa) { $valign[$key] = ' vertical-align:'. $aa.';'; } else { $valign[$key] = ''; } } } if (isset ($table->size)) { foreach ($table->size as $key => $ss) { if ($ss) { $size[$key] = ' width:'. $ss .';'; } else { $size[$key] = ''; } } } if (isset ($table->style)) { foreach ($table->style as $key => $st) { if ($st) { $style[$key] = ' '. $st .';'; } else { $style[$key] = ''; } } } if (isset ($table->rowstyle)) { foreach ($table->rowstyle as $key => $st) { $rowstyle[$key] = ' '. $st .';'; } } if (isset ($table->rowclass)) { foreach ($table->rowclass as $key => $class) { $rowclass[$key] = $class; } } if (isset ($table->colspan)) { foreach ($table->colspan as $keyrow => $cspan) { foreach ($cspan as $key => $span) { $colspan[$keyrow][$key] = ' colspan="'.$span.'"'; } } } if (empty ($table->width)) { $table->width = '80%'; } if (empty ($table->border)) { $table->border = '0px'; } if (empty ($table->tablealign)) { $table->tablealign = 'center'; } if (empty ($table->cellpadding)) { $table->cellpadding = '4'; } if (empty ($table->cellspacing)) { $table->cellspacing = '4'; } if (empty ($table->class)) { $table->class = 'databox'; } $tableid = empty ($table->id) ? 'table'.$table_count : $table->id; $output .= 'cellpadding\" cellspacing=\"$table->cellspacing\" "; $output .= " border=\"$table->border\" class=\"$table->class\" id=\"$tableid\" >\n"; $countcols = 0; if (!empty ($table->head)) { $countcols = count ($table->head); $output .= ''; foreach ($table->head as $key => $heading) { if (!isset ($size[$key])) { $size[$key] = ''; } if (!isset ($align[$key])) { $align[$key] = ''; } $output .= ''; } $output .= ''."\n"; } if (!empty ($table->data)) { $oddeven = 1; foreach ($table->data as $keyrow => $row) { if (!isset ($rowstyle[$keyrow])) { $rowstyle[$keyrow] = ''; } $oddeven = $oddeven ? 0 : 1; $class = 'datos'.($oddeven ? "" : "2"); if (isset ($rowclass[$keyrow])) { $class = $rowclass[$keyrow]; } $output .= ''."\n"; /* Special separator rows */ if ($row == 'hr' and $countcols) { $output .= ''; continue; } /* It's a normal row */ foreach ($row as $key => $item) { if (!isset ($size[$key])) { $size[$key] = ''; } if (!isset ($colspan[$keyrow][$key])) { $colspan[$keyrow][$key] = ''; } if (!isset ($align[$key])) { $align[$key] = ''; } if (!isset ($valign[$key])) { $valign[$key] = ''; } if (!isset ($wrap[$key])) { $wrap[$key] = ''; } if (!isset ($style[$key])) { $style[$key] = ''; } $output .= ''."\n"; } $output .= ''."\n"; } } $output .= '
'. $heading .'
'. $item .'
'."\n"; if ($return) return $output; echo $output; } /** * Render a radio button input. Extended version, use print_radio_button() to simplify. * * @param string $name Input name. * @param string $value Input value. * @param string $checked Set the button to be marked (optional, unmarked by default). * @param bool $disabled Disable the button (optional, button enabled by default). * @param string $script Script to execute when onClick event is triggered (optional). * @param string $attributes Optional HTML attributes. It's a free string which will be inserted into the HTML tag, use it carefully (optional). * @param bool $return Whether to return an output string or echo now (optional, echo by default). */ function print_radio_button_extended ($name, $value, $label, $checkedvalue, $disabled, $script, $attributes, $return = false) { static $idcounter = 0; $output = ''; $output = ''. $label .'' . "\n"; } if ($return) return $output; echo $output; } /** * Render a radio button input. * * @param string $name Input name. * @param string $value Input value. * @param string $label Label to add after the radio button (optional). * @param string $checkedvalue Checked and selected value, the button will be selected if it matches $value (optional). * @param bool $return Whether to return an output string or echo now (optional, echo by default). */ function print_radio_button ($name, $value, $label = '', $checkedvalue = '', $return = false) { $output = print_radio_button_extended ($name, $value, $label, $checkedvalue, false, '', '', true); if ($return) return $output; echo $output; } /** * Render a checkbox button input. Extended version, use print_checkbox() to simplify. * * @param string $name Input name. * @param string $value Input value. * @param string $checked Set the button to be marked (optional, unmarked by default). * @param bool $disabled Disable the button (optional, button enabled by default). * @param string $script Script to execute when onClick event is triggered (optional). * @param string $attributes Optional HTML attributes. It's a free string which will be inserted into the HTML tag, use it carefully (optional). * @param bool $return Whether to return an output string or echo now (optional, echo by default). */ function print_checkbox_extended ($name, $value, $checked, $disabled, $script, $attributes, $return = false) { static $idcounter = 0; $htmlid = 'checkbox'.sprintf ('%04d', ++$idcounter); $output = ''; if ($return) return $output; echo $output; } ?>