Note: You are currently viewing documentation for Moodle 1.9. Up-to-date documentation for the latest stable version is available here: Deprecated functions in 2.0.

Development:Deprecated functions in 2.0: Difference between revisions

From MoodleDocs
Line 1: Line 1:
{{Work in progress}}
{{Moodle 2.0}}
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 (0) ===
Old code:
<code php>
button_to_popup_window($url, $name, $linkname, $height, $width, $title, $options, $return, $id, $class);
</code>
New code:
<code php>
$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);
</code>
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 (192) ===
Old code:
<code php>
choose_from_menu($options, $name, $selected, $nothing, $script, $nothingvalue, $return, $disabled, $tabindex, $id, $listbox, $multiple, $class);
</code>
New code:
<code php>
$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);
</code>
=== choose_from_menu_nested (0) ===
Old code:
<code php>
choose_from_menu_nested($options, $name, $selected, $nothing, $script, $nothingvalue, $return, $disabled, $tabindex);
</code>
New code:
<code php>
$select = moodle_select::make($options, $name, $selected);
$select->tabindex = $tabindex;
$select->disabled = $disabled;
$select->nothingvalue = $nothingvalue;
$select->nested = true;
echo $OUTPUT->select($select);
</code>
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.
=== choose_from_menu_yesno (0) ===
Old code:
<code php>
choose_from_menu_yesno($name, $selected, $script, $return, $disabled, $tabindex);
</code>
New code:
<code php>
$select = moodle_select::make_yes_no($name, $selected);
$select->disabled = $disabled;
$select->tabindex = $tabindex;
echo $OUTPUT->select($select);
</code>
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:
<code php>
close_window_button($name, $return, $reloadopener);
</code>
New code:
<code php>
echo $OUTPUT->close_window_button(get_string($name));
</code>
Note: You must pass a language string already processed by get_string().
=== print_continue (91) ===
Old code:
<code php>
print_continue($link, $return);
</code>
New code:
<code php>
echo $OUTPUT->continue_button($link);
</code>
=== doc_link (0) ===
Old code:
<code php>
doc_link($path, $text, $iconpath);
</code>
New code:
<code php>
echo $OUTPUT->doc_link($path, $text, $iconpath);
</code>
=== helpbutton (391) ===
Old code:
<code php>
helpbutton($page, $title, $module, $image, $linktext, $text, $return, $imagetext);
</code>
New code:
<code php>
$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);
</code>
=== link_to_popup_window (54) ===
Old code:
<code php>
link_to_popup_window($url, $name, $linkname, $height, $width, $title, $options, $return);
</code>
New code:
<code php>
$link = html_link::make($url, $linkname);
$link->add_action(new popup_action('click', $url, $name, $options));
echo $OUTPUT->link_to_popup($link);
</code>
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:
<code php>
notice_yesno($message, $linkyes, $linkno, $optionsyes, $optionsno, $methodyes, $methodno);
</code>
New code (simplest form):
<code php>
echo $OUTPUT->confirm($message, $linkyes, $linkno);
</code>
New code (complex form):
<code php>
$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);
</code>
=== notify (548) ===
Old code:
<code php>
notify($message, $classes, $align, $return);
</code>
New code:
<code php>
echo $OUTPUT->notification($message, $classes='');
</code>
Note: Use a CSS rule for alignment.
=== popup_form (51) ===
=== popup_form (51) ===
Old code:
Old code:
Line 185: Line 6:
New code:
New code:
<code php>
<code php>
// After converting $options' keys to full URLs:
// if $baseurl == 'http://domain.com/index.php?var1=1&amp;var2='
$select = moodle_select::make_popup_form($options, $formid, $selected, $submitvalue);
$select = moodle_select::make_popup_form('http://domain.com/index.php?var1=1', 'var2', $options, $formid, $selected);
$select->disabled = $disabled; // optional
$select->disabled = $disabled; // optional
$select->set_label($selectlabel, $select->id); // 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->set_help_icon($help, $helptext); // optional
$select->form->button->text = $submitvalue; // optional


echo $OUTPUT->select($select);
echo $OUTPUT->select($select);
</code>
</code>
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.
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* (380) ===
Old code:
<code php>
print_box($message, $classes, $ids, $return);
print_box_start($classes, $ids, $return);
print_box_end();
</code>
New code:
<code php>
echo $OUTPUT->box($message, $classes, $ids);
echo $OUTPUT->box_start($classes, $ids);
echo $OUTPUT->box_end();
</code>
 
=== print_checkbox (10) ===
Old code:
<code php>
print_checkbox($name, $value, $checked, $label, $alt, $script, $return);
</code>
New code:
<code php>
$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);
</code>
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 (153) ===
Old code:
<code php>
print_container($message, $clearfix, $classes, $idbase, $return);
</code>
New code:
<code php>
if ($clearfix) {
    $classes .= ' clearfix';
}
echo $OUTPUT->container($message, $classes, $idbase);
</code>
 
=== print_date_selector (0) ===
 
=== print_footer (0) ===
Old code:
<code php>
print_footer($course, $usercourse, $return);
</code>
New code:
<code php>
echo $OUTPUT->footer();
</code>
Notes: No parameters are required.
 
=== print_header (501) ===
Old code:
<code php>
print_header($title, $heading, $navigation, $focus, $meta, $cache, $button, $menu, $usexml, $bodytags, $return);
</code>
New code:
<code php>
$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);
</code>
Note: Navigation code is being rewritten, this doc will then be updated with the new usage.
 
=== print_header_with_help (0) ===
=== print_heading (0) ===
Old code:
<code php>
print_heading($text, $align, $size, $class, $return, $id);
</code>
New code:
<code php>
echo $OUTPUT->heading($text, $size, $class, $id);
</code>
Note: Use a CSS class rule to control alignment.
 
=== print_heading_block (0) ===
 
=== print_headline (0) ===
Old code:
<code php>
print_headline($text, $size);
</code>
New code:
<code php>
echo $OUTPUT->heading($text, $size);
</code>
 
=== print_paging_bar (0) ===
 
Old code:
<code php>
print_paging_bar($totalcount, $page, $perpage, $baseurl, $pagevar, $nocurr, $return);
</code>
New code:
<code php>
$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);
</code>
 
=== print_scale_menu_helpbutton (0) ===
Old code:
<code php>
print_scale_menu_helpbutton($courseid, $scale);
</code>
New code:
<code php>
echo $OUTPUT->help_button(help_button::make_scale_menu($courseid, $scale));
</code>
 
=== print_side_block (4) ===
=== print_single_button (108) ===
Old code:
<code php>
print_single_button($link, $options, $label, $method, $notusedanymore, $return, $tooltip, $disabled, $jsconfirmmessage, $formid)
</code>
New code:
<code php>
$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);
</code>
 
=== print_spacer (0) ===
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) ===
Old code:
<code php>
$table = new stdClass();
$table->class = 'mytable';
$table->head  = array('Firstname', 'Lastname');
$table->rowclass = array();
// (other table properties here)
$table->data = array(array(...),array(...),array(...));
print_table($table, $return);
</code>
New code:
<code php>
$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();      // note the change of rowclass[] to rowclasses[]
// (other table properties here)
$table->data = array(array(...),array(...),array(...));
echo $OUTPUT->table($newtable);
</code>
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 (77) ===
 
Old code:
<code php>
print_user_picture($user, $courseid, $picture, $size, $return, $link, $target, $alttext);
</code>
New code (simple):
<code php>
$userpic = new user_picture();
$userpic->user = $user;
$userpic->courseid = $courseid;
echo $OUTPUT->user_picture($userpic);
</code>
New code (complex: alternate text, size, different image and popup action):
<code php>
$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);
</code>
 
=== update_course_button (0) ===
 
=== update_module_button (94) ===
Old code:
<code php>
$button = update_module_button($cm->id, $course->id, get_string('modulename', 'workshop')); // passed to print_header_simple()
</code>
New code:
<code php>
$PAGE->set_button($OUTPUT->update_module_button($cm->id, 'workshop'));
</code>
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.
=== blocks_print_group ===
=== print_file_picture ===
=== print_png ===
=== print_scale_menu ===
=== print_side_block_end ===
=== print_side_block_start ===
=== print_timer_selector ===
=== print_user ===
=== update_categories_search_button ===
=== update_mymoodle_icon ===

Revision as of 01:33, 10 August 2009

popup_form (51)

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 = moodle_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.