Development:Deprecated functions in 2.0: Difference between revisions
Line 254: | Line 254: | ||
=== print_spacer (20) === | === print_spacer (20) === | ||
Old code: | |||
<code php> | |||
print_spacer($height, $width, $br, $return); | |||
</code> | |||
New code: | |||
<code php> | |||
$spacer = new html_image(); | |||
$spacer->height = $height; | |||
$spacer->width = $width; | |||
echo $OUTPUT->spacer($spacer); | |||
</code> | |||
Note: The $br attribute has been dropped. You can simply add a line break manually if you need one. | |||
=== print_table (140) === | === print_table (140) === | ||
Old code: | Old code: |
Revision as of 06:53, 5 August 2009
Note: This article is a work in progress. Please use the page comments or an appropriate moodle.org forum for any recommendations/suggestions for improvement.
This page lists the old HTML-outputting functions of pre-2.0 lib/weblib.php and their 2.0 equivalents using $OUTPUT functions.
General notes
The principle of the new rendering API is to let the developer build metadata-rich components that represent HTML elements, but may be rendered in many different ways. A menu component, for example, could be rendered as a <select> element, or as a list of radio buttons or check boxes.
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 (4)
Old code:
choose_from_menu($options, $name, $selected, $nothing, $script, $nothingvalue, $return, $disabled, $tabindex, $id, $listbox, $multiple, $class);
New code:
$select = moodle_select::make($options, $name, $selected); // Required
$select->nothinglabel = $nothing;
$select->nothingvalue = $nothingvalue;
$select->disabled = $disabled;
$select->tabindex = $tabindex;
$select->id = $id;
$select->listbox = $listbox;
$select->multiple = $multiple;
$select->add_classes($class);
echo $OUTPUT->select($select);
choose_from_radio (6)
close_window_button (34)
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 (91)
Old code:
print_continue($link, $return);
New code:
echo $OUTPUT->continue_button($link);
doc_link (30)
helpbutton (391)
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 (54)
Old code:
link_to_popup_window($url, $name, $linkname, $height, $width, $title, $options, $return);
New code:
$link = html_link::make($url, $linkname);
$link->add_action(new popup_action('click', $url, $name, $options));
echo $OUTPUT->link_to_popup($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 (79)
Old code:
notice_yesno($message, $linkyes, $linkno, $optionsyes, $optionsno, $methodyes, $methodno);
New code (simplest form):
echo $OUTPUT->confirm($message, $linkyes, $linkno);
New code (complex form):
$formcontinue = new html_form();
$formcontinue->url = new moodle_url($linkyes, $optionsyes);
$formcontinue->button->text = get_string('yes');
$formcontinue->method = $methodyes;
$formcancel = new html_form();
$formcancel->url = new moodle_url($linkno, $optionsno);
$formcancel->button->text = get_string('no');
$formcancel->method = $methodno;
echo $OUTPUT->confirm($message, $formcontinue, $formcancel);
notify (548)
Old code:
notify($message, $classes, $align, $return);
New code:
echo $OUTPUT->notification($message, $classes=);
Note: Use a CSS rule for alignment.
page_doc_link (20)
popup_form (51)
Old code:
popup_form($baseurl, $options, $formid, $selected, $nothing, $help, $helptext, $return, $targetwindow, $selectlabel, $optionsextra, $submitvalue, $disabled, $showbutton);
New code:
// After converting $options' keys to full URLs:
$select = moodle_select::make_popup_form($options, $formid, $submitvalue, $selected);
$select->disabled = $disabled; // optional
$select->set_label($selectlabel, $select->id); // optional
$select->set_help_icon($help, $helptext); // optional
echo $OUTPUT->select($select);
Notes: You must convert the $options array so that the index keys represent a full URL: $baseurl . $key . $optionsextra[$key]. See lib/deprecatedlib.php for an example of conversion.
print_arrow (15)
print_box* (380)
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 (33)
print_container (153)
Old code:
print_container($message, $clearfix, $classes, $idbase, $return);
New code:
if ($clearfix) {
$classes .= ' clearfix';
}
echo $OUTPUT->container($message, $classes, $idbase);
print_date_selector (11)
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);
echo $OUTPUT->header($navigation, $menu);
Note: Navigation code is being rewritten, this doc will then be updated with the new usage.
print_header_with_help (0)
print_heading (436)
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 (9)
print_headline (11)
print_paging_bar (41)
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;
$pagingbar->nocurr = $nocurr;
echo $OUTPUT->paging_bar($pagingbar);
print_side_block (4)
print_single_button (108)
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 (20)
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 (140)
Old code:
print_table($table, $return);
New code:
$newtable = new html_table();
foreach ($table as $property => $value) {
if (property_exists($newtable, $property)) {
$newtable->{$property} = $value;
}
}
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 (2)
print_time_selector (9)
print_user_picture (77)
Old code:
print_user_picture($user, $courseid, $picture, $size, $return, $link, $target, $alttext);
New code (simple):
$userpic = new 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->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 (2)
update_module_button (94)
Old code:
update_module_button($cmid, $courseid, $string);
New code:
echo $OUTPUT->update_module_button($cmid, $string);
Note: $courseid param has been dropped.
update_tag_button (4)
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.