Deprecated functions in 2.0: Difference between revisions
(Note about plan not to migrate this page to the new developer resources. See template for more info.) |
|||
(27 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
{{Template:WillNotMigrate}} | |||
{{Work in progress}} | {{Work in progress}} | ||
{{Moodle 2.0}} | {{Moodle 2.0}} | ||
This page lists | |||
THIS PAGE IS OUTDATED, DO NOT USE! | |||
This page lists all functions that have been deprecated between 1.9 and 2.0, with instructions on how to upgrade your code. A count of remaining function calls in core is kept next to each function name. | |||
== General notes == | == General notes == | ||
The following functions have been arranged in alphabetical order, although some of them are very closely related to each other. The number of occurrences of these function calls at the time of writing are indicated in parentheses. | The following functions have been arranged in alphabetical order, although some of them are very closely related to each other. The number of occurrences of these function calls at the time of writing are indicated in parentheses. | ||
Line 12: | Line 16: | ||
=== button_to_popup_window (0) === | === button_to_popup_window (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
button_to_popup_window($url, $name, $linkname, $height, $width, $title, $options, $return, $id, $class); | button_to_popup_window($url, $name, $linkname, $height, $width, $title, $options, $return, $id, $class); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
$form = new html_form(); | $form = new html_form(); | ||
$form->button->text = $linkname; | $form->button->text = $linkname; | ||
Line 25: | Line 29: | ||
$form->button->add_action(new popup_action('click', $url, $name, $options)); | $form->button->add_action(new popup_action('click', $url, $name, $options)); | ||
echo $OUTPUT->button($form); | echo $OUTPUT->button($form); | ||
</ | </syntaxhighlight> | ||
Note: popup parameters ($options) '''must''' be prepared as an associative array, not a string as previously. Simply reusing the $options param previously passed to button_to_popup_window() will not work. The $height and $width params are also part of the popup params. See lib/deprecatedlib.php for an example of how the legacy function call is handled. | Note: popup parameters ($options) '''must''' be prepared as an associative array, not a string as previously. Simply reusing the $options param previously passed to button_to_popup_window() will not work. The $height and $width params are also part of the popup params. See lib/deprecatedlib.php for an example of how the legacy function call is handled. | ||
=== choose_from_menu (1) === | === choose_from_menu (1) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
choose_from_menu($options, $name, $selected, $nothing, $script, $nothingvalue, $return, $disabled, $tabindex, $id, $listbox, $multiple, $class); | choose_from_menu($options, $name, $selected, $nothing, $script, $nothingvalue, $return, $disabled, $tabindex, $id, $listbox, $multiple, $class); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
$ | $attributes['disabled'] = $disabled; | ||
$attributes['tabindex'] = $tabindex; | |||
$attributes['multiple'] = $multiple; | |||
$attributes['class'] = $class; | |||
$ | $attributes['id'] = $id; | ||
$ | |||
$ | |||
$ | |||
echo $ | echo html_writer::select($options, $name, $selected, array($nothingvalue=>$nothing), $attributes); | ||
</ | </syntaxhighlight> | ||
=== choose_from_menu_nested (0) === | === choose_from_menu_nested (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
choose_from_menu_nested($options, $name, $selected, $nothing, $script, $nothingvalue, $return, $disabled, $tabindex); | choose_from_menu_nested($options, $name, $selected, $nothing, $script, $nothingvalue, $return, $disabled, $tabindex); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
$select = moodle_select::make($options, $name, $selected); | $select = moodle_select::make($options, $name, $selected); | ||
$select->tabindex = $tabindex; | $select->tabindex = $tabindex; | ||
Line 62: | Line 62: | ||
echo $OUTPUT->select($select); | echo $OUTPUT->select($select); | ||
</ | </syntaxhighlight> | ||
Notes: | Notes: | ||
#The lang string "choose" is no longer used, in favour of "choosedots". | #The lang string "choose" is no longer used, in favour of "choosedots". | ||
Line 69: | Line 69: | ||
=== choose_from_menu_yesno (0) === | === choose_from_menu_yesno (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
choose_from_menu_yesno($name, $selected, $script, $return, $disabled, $tabindex); | choose_from_menu_yesno($name, $selected, $script, $return, $disabled, $tabindex); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
$select = moodle_select::make_yes_no($name, $selected); | $select = moodle_select::make_yes_no($name, $selected); | ||
$select->disabled = $disabled; | $select->disabled = $disabled; | ||
$select->tabindex = $tabindex; | $select->tabindex = $tabindex; | ||
echo $OUTPUT->select($select); | echo $OUTPUT->select($select); | ||
</ | </syntaxhighlight> | ||
Note: The $script param has been dropped, you need to use component::add_action() instead. | Note: The $script param has been dropped, you need to use component::add_action() instead. | ||
Line 85: | Line 85: | ||
=== close_window_button (0) === | === close_window_button (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
close_window_button($name, $return, $reloadopener); | close_window_button($name, $return, $reloadopener); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
echo $OUTPUT->close_window_button(get_string($name)); | echo $OUTPUT->close_window_button(get_string($name)); | ||
</ | </syntaxhighlight> | ||
Note: You must pass a language string already processed by get_string(). | Note: You must pass a language string already processed by get_string(). | ||
=== print_continue (0) === | === print_continue (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
print_continue($link, $return); | print_continue($link, $return); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
echo $OUTPUT->continue_button($link); | echo $OUTPUT->continue_button($link); | ||
</ | </syntaxhighlight> | ||
=== doc_link (0) === | === doc_link (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
doc_link($path, $text, $iconpath); | doc_link($path, $text, $iconpath); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
echo $OUTPUT->doc_link($path, $text, $iconpath); | echo $OUTPUT->doc_link($path, $text, $iconpath); | ||
</ | </syntaxhighlight> | ||
Note: This is not for general use, do not confuse with help_icon() | Note: This is not for general use, do not confuse with help_icon() | ||
=== formerr ( | === formerr (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
formerr($string); | formerr($string); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
echo $OUTPUT->error_text($error); | echo $OUTPUT->error_text($error); | ||
</ | </syntaxhighlight> | ||
=== helpbutton (0) === | === helpbutton (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
helpbutton($page, $title, $module, $image, $linktext, $text, $return, $imagetext); | helpbutton($page, $title, $module, $image, $linktext, $text, $return, $imagetext); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
$helpicon = new help_icon(); | $helpicon = new help_icon(); | ||
$helpicon->page = $page; // required | $helpicon->page = $page; // required | ||
Line 139: | Line 139: | ||
echo $OUTPUT->help_icon($helpicon); | echo $OUTPUT->help_icon($helpicon); | ||
</ | </syntaxhighlight> | ||
=== link_to_popup_window ( | === link_to_popup_window (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
link_to_popup_window($url, $name, $linkname, $height, $width, $title, $options, $return); | link_to_popup_window($url, $name, $linkname, $height, $width, $title, $options, $return); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
$link = html_link::make($url, $linkname); | $link = html_link::make($url, $linkname); | ||
$link->title = $title; // optional | |||
$options['height'] = $height; // optional | |||
$options['width'] = $width; // optional | |||
$link->add_action(new popup_action('click', $url, $name, $options)); | $link->add_action(new popup_action('click', $url, $name, $options)); | ||
echo $OUTPUT-> | echo $OUTPUT->link($link); | ||
</ | </syntaxhighlight> | ||
Note: popup parameters ($options) '''must''' be prepared as an associative array, not a string as previously. Simply reusing the $options param previously passed to link_to_popup_window() will not work. See lib/deprecatedlib.php for an example of how the legacy function call is handled. | Note: popup parameters ($options) '''must''' be prepared as an associative array, not a string as previously. Simply reusing the $options param previously passed to link_to_popup_window() will not work. See lib/deprecatedlib.php for an example of how the legacy function call is handled. | ||
=== notice_yesno ( | === notice_yesno (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
notice_yesno($message, $linkyes, $linkno, $optionsyes, $optionsno, $methodyes, $methodno); | notice_yesno($message, $linkyes, $linkno, $optionsyes, $optionsno, $methodyes, $methodno); | ||
</ | </syntaxhighlight> | ||
New code (simplest form): | New code (simplest form): | ||
< | <syntaxhighlight lang="php"> | ||
echo $OUTPUT->confirm($message, $linkyes, $linkno); | echo $OUTPUT->confirm($message, $linkyes, $linkno); | ||
</ | </syntaxhighlight> | ||
New code (with options in arrays): | |||
$ | <syntaxhighlight lang="php"> | ||
echo $OUTPUT->confirm($message, new moodle_url($linkyes, $optionsyes), new moodle_url($linkno, $optionsno)); | |||
</syntaxhighlight> | |||
New code (if the "get" method is required): | |||
<syntaxhighlight lang="php"> | |||
$formcontinue = new single_button($linkyes, $optionsyes, get_string('yes'), $methodyes); | |||
$formcancel = new single_button($linkno, $optionsno, get_string('no'), $methodno); | |||
echo $OUTPUT->confirm($message, $formcontinue, $formcancel); | echo $OUTPUT->confirm($message, $formcontinue, $formcancel); | ||
</ | </syntaxhighlight> | ||
=== notify (0) === | === notify (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
notify($message, $classes, $align, $return); | notify($message, $classes, $align, $return); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
echo $OUTPUT->notification($message, $classes=''); | echo $OUTPUT->notification($message, $classes=''); | ||
</ | </syntaxhighlight> | ||
Note: Use a CSS rule for alignment. | Note: Use a CSS rule for alignment. | ||
=== popup_form (0) === | === popup_form (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
popup_form($baseurl, $options, $formid, $selected, $nothing, $help, $helptext, $return, $targetwindow, $selectlabel, $optionsextra, $submitvalue, $disabled, $showbutton); | popup_form($baseurl, $options, $formid, $selected, $nothing, $help, $helptext, $return, $targetwindow, $selectlabel, $optionsextra, $submitvalue, $disabled, $showbutton); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
// if $baseurl == 'http://domain.com/index.php?var1=1&var2=' | // if $baseurl == 'http://domain.com/index.php?var1=1&var2=' | ||
$select = html_select::make_popup_form('http://domain.com/index.php?var1=1', 'var2', $options, $formid, $selected); | $select = html_select::make_popup_form('http://domain.com/index.php?var1=1', 'var2', $options, $formid, $selected); | ||
Line 204: | Line 205: | ||
echo $OUTPUT->select($select); | echo $OUTPUT->select($select); | ||
</ | </syntaxhighlight> | ||
Notes: | Notes: | ||
*The $optionsextra param is not supported. If your code uses it, you should find another way to add extra params to your <option> tags without using this horrible hack which usually includes inline JS or CSS. | *The $optionsextra param is not supported. If your code uses it, you should find another way to add extra params to your <option> tags without using this horrible hack which usually includes inline JS or CSS. | ||
Line 211: | Line 212: | ||
=== print_box* (0) === | === print_box* (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
print_box($message, $classes, $ids, $return); | print_box($message, $classes, $ids, $return); | ||
print_box_start($classes, $ids, $return); | print_box_start($classes, $ids, $return); | ||
print_box_end(); | print_box_end(); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
echo $OUTPUT->box($message, $classes, $ids); | echo $OUTPUT->box($message, $classes, $ids); | ||
echo $OUTPUT->box_start($classes, $ids); | echo $OUTPUT->box_start($classes, $ids); | ||
echo $OUTPUT->box_end(); | echo $OUTPUT->box_end(); | ||
</ | </syntaxhighlight> | ||
=== print_checkbox (10) === | === print_checkbox (10) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
print_checkbox($name, $value, $checked, $label, $alt, $script, $return); | print_checkbox($name, $value, $checked, $label, $alt, $script, $return); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
$checkbox = new html_select_option(); | $checkbox = new html_select_option(); | ||
$checkbox->value = $value; // Required | $checkbox->value = $value; // Required | ||
Line 238: | Line 239: | ||
echo $OUTPUT->checkbox($checkbox, $name); | echo $OUTPUT->checkbox($checkbox, $name); | ||
</ | </syntaxhighlight> | ||
Note: html_select_option is a component that can be rendered as a select <option>, a radio button or a checkbox. It holds sufficient information to render all these elements, except the $name variable which is attached to a moodle_select component. This is why we pass the $name string to $OUTPUT->checkbox(). | Note: html_select_option is a component that can be rendered as a select <option>, a radio button or a checkbox. It holds sufficient information to render all these elements, except the $name variable which is attached to a moodle_select component. This is why we pass the $name string to $OUTPUT->checkbox(). | ||
=== print_container ( | === print_container (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
print_container($message, $clearfix, $classes, $idbase, $return); | print_container($message, $clearfix, $classes, $idbase, $return); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
if ($clearfix) { | if ($clearfix) { | ||
$classes .= ' clearfix'; | $classes .= ' clearfix'; | ||
} | } | ||
echo $OUTPUT->container($message, $classes, $idbase); | echo $OUTPUT->container($message, $classes, $idbase); | ||
</ | </syntaxhighlight> | ||
=== print_date_selector (0) === | === print_date_selector (0) === | ||
Line 258: | Line 259: | ||
=== print_footer (0) === | === print_footer (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
print_footer($course, $usercourse, $return); | print_footer($course, $usercourse, $return); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
echo $OUTPUT->footer(); | echo $OUTPUT->footer(); | ||
</ | </syntaxhighlight> | ||
Notes: No parameters are required. | Notes: No parameters are required. | ||
=== print_header (501) === | === print_header (501) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
print_header($title, $heading, $navigation, $focus, $meta, $cache, $button, $menu, $usexml, $bodytags, $return); | print_header($title, $heading, $navigation, $focus, $meta, $cache, $button, $menu, $usexml, $bodytags, $return); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
$PAGE->set_heading($heading); // Required | $PAGE->set_heading($heading); // Required | ||
$PAGE->set_title($title); | $PAGE->set_title($title); | ||
Line 279: | Line 280: | ||
$PAGE->set_focuscontrol($focus); | $PAGE->set_focuscontrol($focus); | ||
$PAGE->set_button($button); | $PAGE->set_button($button); | ||
echo $OUTPUT->header( | $PAGE->navbar->add($nav_entry[0]); | ||
</ | $PAGE->navbar->add($nav_entry[1]); | ||
echo $OUTPUT->header(); | |||
</syntaxhighlight> | |||
Note: Navigation code is being rewritten, this doc will then be updated with the new usage. | Note: Navigation code is being rewritten, this doc will then be updated with the new usage. | ||
Line 287: | Line 291: | ||
=== print_heading (0) === | === print_heading (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
print_heading($text, $align, $size, $class, $return, $id); | print_heading($text, $align, $size, $class, $return, $id); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
echo $OUTPUT->heading($text, $size, $class, $id); | echo $OUTPUT->heading($text, $size, $class, $id); | ||
</ | </syntaxhighlight> | ||
Note: Use a CSS class rule to control alignment. | Note: Use a CSS class rule to control alignment. | ||
Line 300: | Line 304: | ||
=== print_headline (0) === | === print_headline (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
print_headline($text, $size); | print_headline($text, $size); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
echo $OUTPUT->heading($text, $size); | echo $OUTPUT->heading($text, $size); | ||
</ | </syntaxhighlight> | ||
=== print_paging_bar ( | === print_paging_bar (2) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
print_paging_bar($totalcount, $page, $perpage, $baseurl, $pagevar, $nocurr, $return); | print_paging_bar($totalcount, $page, $perpage, $baseurl, $pagevar, $nocurr, $return); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
$pagingbar = new moodle_paging_bar(); | $pagingbar = new moodle_paging_bar(); | ||
$pagingbar->totalcount = $totalcount; // Required | $pagingbar->totalcount = $totalcount; // Required | ||
Line 322: | Line 326: | ||
$pagingbar->baseurl = $baseurl; // Required | $pagingbar->baseurl = $baseurl; // Required | ||
$pagingbar->pagevar = $pagevar; | $pagingbar->pagevar = $pagevar; | ||
echo $OUTPUT->paging_bar($pagingbar); | echo $OUTPUT->paging_bar($pagingbar); | ||
</ | </syntaxhighlight> | ||
Note: The last two instances of print_paging_bar are found in the old tablelib, which will soon be deprecated. | |||
Note: $nocurr has been dropped. Current page number is never displayed as a link | |||
=== print_scale_menu_helpbutton (0) === | === print_scale_menu_helpbutton (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
print_scale_menu_helpbutton($courseid, $scale); | print_scale_menu_helpbutton($courseid, $scale); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
echo $OUTPUT->help_button(help_button::make_scale_menu($courseid, $scale)); | echo $OUTPUT->help_button(help_button::make_scale_menu($courseid, $scale)); | ||
</ | </syntaxhighlight> | ||
=== print_side_block (4) === | === print_side_block (4) === | ||
=== print_single_button ( | === print_single_button (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
print_single_button($link, $options, $label, $method, $notusedanymore, $return, $tooltip, $disabled, $jsconfirmmessage, $formid) | print_single_button($link, $options, $label, $method, $notusedanymore, $return, $tooltip, $disabled, $jsconfirmmessage, $formid) | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
$form = new html_form(); | $form = new html_form(); | ||
$form->url = new moodle_url($link, $options); // Required | $form->url = new moodle_url($link, $options); // Required | ||
Line 359: | Line 365: | ||
$output = $OUTPUT->button($form); | $output = $OUTPUT->button($form); | ||
</ | </syntaxhighlight> | ||
=== print_spacer (0) === | === print_spacer (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
print_spacer($height, $width, $br, $return); | print_spacer($height, $width, $br, $return); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
$spacer = new html_image(); | $spacer = new html_image(); | ||
$spacer->height = $height; | $spacer->height = $height; | ||
$spacer->width = $width; | $spacer->width = $width; | ||
echo $OUTPUT->spacer($spacer); | echo $OUTPUT->spacer($spacer); | ||
</ | </syntaxhighlight> | ||
Note: The $br attribute has been dropped. You can simply add a line break manually if you need one. | Note: The $br attribute has been dropped. You can simply add a line break manually if you need one. | ||
=== print_table ( | === print_table (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
$table = new stdClass(); | $table = new stdClass(); | ||
$table->class = 'mytable'; | $table->class = 'mytable'; | ||
$table->head = array('Firstname', 'Lastname'); | $table->head = array('Firstname', 'Lastname'); | ||
$table-> | $table->rowclasses = array(); | ||
// (other table properties here) | // (other table properties here) | ||
$table->data = array(array(...),array(...),array(...)); | $table->data = array(array(...),array(...),array(...)); | ||
print_table($table, $return); | print_table($table, $return); | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
$table = new html_table(); | $table = new html_table(); | ||
$table->set_classes('mytable'); // note the new style of setting CSS class | $table->set_classes('mytable'); // note the new style of setting CSS class | ||
$table->head = array('First name', 'Last name'); | $table->head = array('First name', 'Last name'); | ||
$table->colclasses = array('name fname','name lname'); | $table->colclasses = array('name fname','name lname'); | ||
$table->rowclasses = array(); | $table->rowclasses = array(); | ||
// (other table properties here) | // (other table properties here) | ||
$table->data = array(array(...),array(...),array(...)); | $table->data = array(array(...),array(...),array(...)); | ||
echo $OUTPUT->table($newtable); | echo $OUTPUT->table($newtable); | ||
</ | </syntaxhighlight> | ||
Notes: The new html_table object has the same member variables as the one originally used by print_table(), but has additional, required methods. You must map the old $table object to the new html_table object, as demonstrated above and in lib/deprecatedlib.php (see old print_table function). | Notes: The new html_table object has the same member variables as the one originally used by print_table(), but has additional, required methods. You must map the old $table object to the new html_table object, as demonstrated above and in lib/deprecatedlib.php (see old print_table function). | ||
Line 404: | Line 410: | ||
=== print_time_selector (0) === | === print_time_selector (0) === | ||
=== print_user_picture ( | === print_user_picture (0) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
print_user_picture($user, $courseid, $picture, $size, $return, $link, $target, $alttext); | print_user_picture($user, $courseid, $picture, $size, $return, $link, $target, $alttext); | ||
</ | </syntaxhighlight> | ||
New code (simple): | New code (simple): | ||
< | <syntaxhighlight lang="php"> | ||
$userpic = new moodle_user_picture(); | $userpic = new moodle_user_picture(); | ||
$userpic->user = $user; | $userpic->user = $user; | ||
$userpic->courseid = $courseid; | $userpic->courseid = $courseid; | ||
echo $OUTPUT->user_picture($userpic); | echo $OUTPUT->user_picture($userpic); | ||
</ | </syntaxhighlight> | ||
New code (complex: alternate text, size, different image and popup action): | New code (complex: alternate text, size, different image and popup action): | ||
< | <syntaxhighlight lang="php"> | ||
$userpic = new user_picture(); | $userpic = new user_picture(); | ||
$userpic->user = $user; | $userpic->user = $user; | ||
Line 424: | Line 430: | ||
$userpic->size = $size; | $userpic->size = $size; | ||
$userpic->link = $link; | $userpic->link = $link; | ||
$userpic->visibletoscreenreaders = false; | |||
$userpic->alttext = $alttext; | $userpic->alttext = $alttext; | ||
$userpic->image->src = $picture; | $userpic->image->src = $picture; | ||
Line 429: | Line 436: | ||
echo $OUTPUT->user_picture($userpic); | echo $OUTPUT->user_picture($userpic); | ||
</ | </syntaxhighlight> | ||
=== update_course_button (0) === | === update_course_button (0) === | ||
Line 435: | Line 442: | ||
=== update_module_button (94) === | === update_module_button (94) === | ||
Old code: | Old code: | ||
< | <syntaxhighlight lang="php"> | ||
$button = update_module_button($cm->id, $course->id, get_string('modulename', 'workshop')); // passed to print_header_simple() | $button = update_module_button($cm->id, $course->id, get_string('modulename', 'workshop')); // passed to print_header_simple() | ||
</ | </syntaxhighlight> | ||
New code: | New code: | ||
< | <syntaxhighlight lang="php"> | ||
$PAGE->set_button($OUTPUT->update_module_button($cm->id, 'workshop')); | $PAGE->set_button($OUTPUT->update_module_button($cm->id, 'workshop')); | ||
</ | </syntaxhighlight> | ||
Note: $courseid param has been dropped. | Note: $courseid param has been dropped. | ||
Line 461: | Line 468: | ||
==See also== | ==See also== | ||
* [[ | * [[How Moodle outputs HTML]] | ||
* [[Migrating your code to the 2.0 | * [[Migrating your code to the 2.0 rendering API]] | ||
Latest revision as of 13:40, 24 June 2022
Warning: This page is no longer in use. The information contained on the page should NOT be seen as relevant or reliable. |
Note: This page is a work-in-progress. Feedback and suggested improvements are welcome. Please join the discussion on moodle.org or use the page comments.
Moodle 2.0
THIS PAGE IS OUTDATED, DO NOT USE!
This page lists all functions that have been deprecated between 1.9 and 2.0, with instructions on how to upgrade your code. A count of remaining function calls in core is kept next to each function name.
General notes
The following functions have been arranged in alphabetical order, although some of them are very closely related to each other. The number of occurrences of these function calls at the time of writing are indicated in parentheses.
Functions to migrate
button_to_popup_window (0)
Old code:
button_to_popup_window($url, $name, $linkname, $height, $width, $title, $options, $return, $id, $class);
New code:
$form = new html_form();
$form->button->text = $linkname;
$form->button->title = $title;
$form->button->id = $id;
$form->url = $url;
$form->add_class($class);
$form->button->add_action(new popup_action('click', $url, $name, $options));
echo $OUTPUT->button($form);
Note: popup parameters ($options) must be prepared as an associative array, not a string as previously. Simply reusing the $options param previously passed to button_to_popup_window() will not work. The $height and $width params are also part of the popup params. See lib/deprecatedlib.php for an example of how the legacy function call is handled.
Old code:
choose_from_menu($options, $name, $selected, $nothing, $script, $nothingvalue, $return, $disabled, $tabindex, $id, $listbox, $multiple, $class);
New code:
$attributes['disabled'] = $disabled;
$attributes['tabindex'] = $tabindex;
$attributes['multiple'] = $multiple;
$attributes['class'] = $class;
$attributes['id'] = $id;
echo html_writer::select($options, $name, $selected, array($nothingvalue=>$nothing), $attributes);
Old code:
choose_from_menu_nested($options, $name, $selected, $nothing, $script, $nothingvalue, $return, $disabled, $tabindex);
New code:
$select = moodle_select::make($options, $name, $selected);
$select->tabindex = $tabindex;
$select->disabled = $disabled;
$select->nothingvalue = $nothingvalue;
$select->nested = true;
echo $OUTPUT->select($select);
Notes:
- The lang string "choose" is no longer used, in favour of "choosedots".
- The $script param is no longer supported. Please use component::add_action() instead.
Old code:
choose_from_menu_yesno($name, $selected, $script, $return, $disabled, $tabindex);
New code:
$select = moodle_select::make_yes_no($name, $selected);
$select->disabled = $disabled;
$select->tabindex = $tabindex;
echo $OUTPUT->select($select);
Note: The $script param has been dropped, you need to use component::add_action() instead.
choose_from_radio (0)
close_window_button (0)
Old code:
close_window_button($name, $return, $reloadopener);
New code:
echo $OUTPUT->close_window_button(get_string($name));
Note: You must pass a language string already processed by get_string().
print_continue (0)
Old code:
print_continue($link, $return);
New code:
echo $OUTPUT->continue_button($link);
doc_link (0)
Old code:
doc_link($path, $text, $iconpath);
New code:
echo $OUTPUT->doc_link($path, $text, $iconpath);
Note: This is not for general use, do not confuse with help_icon()
formerr (0)
Old code:
formerr($string);
New code:
echo $OUTPUT->error_text($error);
helpbutton (0)
Old code:
helpbutton($page, $title, $module, $image, $linktext, $text, $return, $imagetext);
New code:
$helpicon = new help_icon();
$helpicon->page = $page; // required
$helpicon->text = $title; // required
$helpicon->module = $module; // defaults to 'moodle'
$helpicon->linktext = $linktext;
echo $OUTPUT->help_icon($helpicon);
link_to_popup_window (0)
Old code:
link_to_popup_window($url, $name, $linkname, $height, $width, $title, $options, $return);
New code:
$link = html_link::make($url, $linkname);
$link->title = $title; // optional
$options['height'] = $height; // optional
$options['width'] = $width; // optional
$link->add_action(new popup_action('click', $url, $name, $options));
echo $OUTPUT->link($link);
Note: popup parameters ($options) must be prepared as an associative array, not a string as previously. Simply reusing the $options param previously passed to link_to_popup_window() will not work. See lib/deprecatedlib.php for an example of how the legacy function call is handled.
notice_yesno (0)
Old code:
notice_yesno($message, $linkyes, $linkno, $optionsyes, $optionsno, $methodyes, $methodno);
New code (simplest form):
echo $OUTPUT->confirm($message, $linkyes, $linkno);
New code (with options in arrays):
echo $OUTPUT->confirm($message, new moodle_url($linkyes, $optionsyes), new moodle_url($linkno, $optionsno));
New code (if the "get" method is required):
$formcontinue = new single_button($linkyes, $optionsyes, get_string('yes'), $methodyes);
$formcancel = new single_button($linkno, $optionsno, get_string('no'), $methodno);
echo $OUTPUT->confirm($message, $formcontinue, $formcancel);
notify (0)
Old code:
notify($message, $classes, $align, $return);
New code:
echo $OUTPUT->notification($message, $classes='');
Note: Use a CSS rule for alignment.
popup_form (0)
Old code:
popup_form($baseurl, $options, $formid, $selected, $nothing, $help, $helptext, $return, $targetwindow, $selectlabel, $optionsextra, $submitvalue, $disabled, $showbutton);
New code:
// if $baseurl == 'http://domain.com/index.php?var1=1&var2='
$select = html_select::make_popup_form('http://domain.com/index.php?var1=1', 'var2', $options, $formid, $selected);
$select->disabled = $disabled; // optional
$select->set_label($selectlabel, $select->id); // optional, set to false if no "nothing" option is desired (when $selectlabel == '' in original call)
$select->set_help_icon($help, $helptext); // optional
$select->form->button->text = $submitvalue; // optional
echo $OUTPUT->select($select);
Notes:
- The $optionsextra param is not supported. If your code uses it, you should find another way to add extra params to your <option> tags without using this horrible hack which usually includes inline JS or CSS.
print_arrow (15)
print_box* (0)
Old code:
print_box($message, $classes, $ids, $return);
print_box_start($classes, $ids, $return);
print_box_end();
New code:
echo $OUTPUT->box($message, $classes, $ids);
echo $OUTPUT->box_start($classes, $ids);
echo $OUTPUT->box_end();
print_checkbox (10)
Old code:
print_checkbox($name, $value, $checked, $label, $alt, $script, $return);
New code:
$checkbox = new html_select_option();
$checkbox->value = $value; // Required
$checkbox->selected = $checked;
$checkbox->text = $label;
$checkbox->label->text = $label;
$checkbox->alt = $alt;
echo $OUTPUT->checkbox($checkbox, $name);
Note: html_select_option is a component that can be rendered as a select <option>, a radio button or a checkbox. It holds sufficient information to render all these elements, except the $name variable which is attached to a moodle_select component. This is why we pass the $name string to $OUTPUT->checkbox().
print_container (0)
Old code:
print_container($message, $clearfix, $classes, $idbase, $return);
New code:
if ($clearfix) {
$classes .= ' clearfix';
}
echo $OUTPUT->container($message, $classes, $idbase);
print_date_selector (0)
Old code:
print_footer($course, $usercourse, $return);
New code:
echo $OUTPUT->footer();
Notes: No parameters are required.
print_header (501)
Old code:
print_header($title, $heading, $navigation, $focus, $meta, $cache, $button, $menu, $usexml, $bodytags, $return);
New code:
$PAGE->set_heading($heading); // Required
$PAGE->set_title($title);
$PAGE->set_cacheable($cache);
$PAGE->set_focuscontrol($focus);
$PAGE->set_button($button);
$PAGE->navbar->add($nav_entry[0]);
$PAGE->navbar->add($nav_entry[1]);
echo $OUTPUT->header();
Note: Navigation code is being rewritten, this doc will then be updated with the new usage.
print_heading_with_help (0)
print_heading (0)
Old code:
print_heading($text, $align, $size, $class, $return, $id);
New code:
echo $OUTPUT->heading($text, $size, $class, $id);
Note: Use a CSS class rule to control alignment.
print_heading_block (0)
print_headline (0)
Old code:
print_headline($text, $size);
New code:
echo $OUTPUT->heading($text, $size);
print_paging_bar (2)
Old code:
print_paging_bar($totalcount, $page, $perpage, $baseurl, $pagevar, $nocurr, $return);
New code:
$pagingbar = new moodle_paging_bar();
$pagingbar->totalcount = $totalcount; // Required
$pagingbar->page = $page; // Required
$pagingbar->perpage = $perpage; // Required
$pagingbar->baseurl = $baseurl; // Required
$pagingbar->pagevar = $pagevar;
echo $OUTPUT->paging_bar($pagingbar);
Note: The last two instances of print_paging_bar are found in the old tablelib, which will soon be deprecated.
Note: $nocurr has been dropped. Current page number is never displayed as a link
Old code:
print_scale_menu_helpbutton($courseid, $scale);
New code:
echo $OUTPUT->help_button(help_button::make_scale_menu($courseid, $scale));
print_side_block (4)
print_single_button (0)
Old code:
print_single_button($link, $options, $label, $method, $notusedanymore, $return, $tooltip, $disabled, $jsconfirmmessage, $formid)
New code:
$form = new html_form();
$form->url = new moodle_url($link, $options); // Required
$form->button = new html_button();
$form->button->text = $label; // Required
$form->button->disabled = $disabled;
$form->button->title = $tooltip;
$form->method = $method;
$form->id = $formid;
if ($jsconfirmmessage) {
$confirmaction = new component_action('click', 'confirm_dialog', array('message' => $jsconfirmmessage));
$form->button->add_action($confirmaction);
}
$output = $OUTPUT->button($form);
print_spacer (0)
Old code:
print_spacer($height, $width, $br, $return);
New code:
$spacer = new html_image();
$spacer->height = $height;
$spacer->width = $width;
echo $OUTPUT->spacer($spacer);
Note: The $br attribute has been dropped. You can simply add a line break manually if you need one.
print_table (0)
Old code:
$table = new stdClass();
$table->class = 'mytable';
$table->head = array('Firstname', 'Lastname');
$table->rowclasses = array();
// (other table properties here)
$table->data = array(array(...),array(...),array(...));
print_table($table, $return);
New code:
$table = new html_table();
$table->set_classes('mytable'); // note the new style of setting CSS class
$table->head = array('First name', 'Last name');
$table->colclasses = array('name fname','name lname');
$table->rowclasses = array();
// (other table properties here)
$table->data = array(array(...),array(...),array(...));
echo $OUTPUT->table($newtable);
Notes: The new html_table object has the same member variables as the one originally used by print_table(), but has additional, required methods. You must map the old $table object to the new html_table object, as demonstrated above and in lib/deprecatedlib.php (see old print_table function).
print_textarea (55)
print_textfield (0)
print_time_selector (0)
print_user_picture (0)
Old code:
print_user_picture($user, $courseid, $picture, $size, $return, $link, $target, $alttext);
New code (simple):
$userpic = new moodle_user_picture();
$userpic->user = $user;
$userpic->courseid = $courseid;
echo $OUTPUT->user_picture($userpic);
New code (complex: alternate text, size, different image and popup action):
$userpic = new user_picture();
$userpic->user = $user;
$userpic->courseid = $courseid;
$userpic->size = $size;
$userpic->link = $link;
$userpic->visibletoscreenreaders = false;
$userpic->alttext = $alttext;
$userpic->image->src = $picture;
$userpic->add_action(new popup_action('click', new moodle_url($target)));
echo $OUTPUT->user_picture($userpic);
update_course_button (0)
update_module_button (94)
Old code:
$button = update_module_button($cm->id, $course->id, get_string('modulename', 'workshop')); // passed to print_header_simple()
New code:
$PAGE->set_button($OUTPUT->update_module_button($cm->id, 'workshop'));
Note: $courseid param has been dropped.
update_tag_button (0)
Non-supported functions
These functions have been completely dropped in 2.0, most likely because they were used very little or not at all. All calls to these functions in core should have been replaced.