Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Inplace editable

From MoodleDocs
Revision as of 02:00, 4 February 2016 by Marina Glancy (talk | contribs)

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.


inplace_editable is a mini-API being introduced under MDL-51802 for Moodle 3.1. It allows developers easily add in-place editing of a value on any page. The interface looks very similar to activity name editing but implemented as AMD module using JQuery and is re-usable.

Implementing inplace_editable in a plugin

The best way is to explain the usage on a simple example. Imagine we have plugin tool_mytest that needs to implement in-place editing of a field 'name' from db table tool_mytest_mytable. We are going to call this itemtype "mytestname". This plugin only uses one in-place element so we will basically ignore $itemtype argument in the saver function.

Create a file /admin/tool/mytest/classes/inplace_editable_saver.php with a class: namespace tool_mytest; class inplace_editable_saver implements core\inplace_editable_saver_base {

   public function render_value($itemtype, $record) {
       $value = $record->name;
       $displayvalue = format_string($value); // You can add html link to the displayvalue.
       $editable = has_capability('tool/mytest:update', context_system::instance());
       $edithint = 'Edit mytest name';
       $editlabel = 'New value for ' . format_string($value); // Obviously, use language strings here!
       return new \core\output\inplace_editable('tool_mytest', $itemtype, $record->id, $editable, $displayvalue,
           $value, $edithint, $editlabel);
   }
   public function update_value($itemtype, $itemid, $newvalue) {
       global $DB;
       // Always clean and validate input!
       $newvalue = clean_param($newvalue, PARAM_NOTAGS);
       // Check permission of the user to update this item. Call require_login($course) if needed.
       require_capability('tool/mytest:update', context_system::instance());
       $record = $DB->get_record('tool_mytest_mytable', array('id' => $itemid), '*', MUST_EXIST);
       // Update the record (the best way is to call an existing function for it).
       $DB->update_record('tool_mytest_mytable', array('id' => $itemid, 'name' => $newvalue));
       $record->name = $newvalue;
       return $this->render_value($itemtype, $record);
   }

}

Please note that function "render_value" is not part of the interface and you can choose to call rendering function differently or not have it at all. Although it is recommended because you need to use the same templateable/renderable element for actually displaying the element on the page. For example:

$tmpl = new \tool_mytest\inplace_editable_saver(); echo $OUTPUT->render($tmpl->render_value('mytestname', $record));

How does it work

inplace_editable consists of

  • Templateable/renderable class core/inplace_editable
  • Javascript module core/inplace_editable
  • Web service core_update_inplace_editable available from AJAX
  • Interface core\inplace_editable_saver_base

All four call each other so it's hard to decide where we start explaining this circle of friends but let's start with web service.

Web service receives arguments ($component, $itemtype, $itemid, $newvalue) - it searches for the inplace_editable_saver class in the component and makes sure it implements the interface core\inplace_editable_saver_base. This is a security check that ensures that only allowed classes can be initiated in this web service. Then web service calls method update_value($itemtype, $itemid, $newvalue), this must return templateable element which is sent back to the web service caller.

Templateable element contains such properties as component, itemtype, itemid, displayvalue, value, editlabel and edithint. It only renders the displayvalue and the edit link (with title=edithint). All other properties are rendered as data-xxx attributes. Template also ensures that javascript module is loaded.

Javascript module registers a listener to when the edit link is clicked and then it replaces the displayvalue with the text input box that allows to edit value. When user presses "Enter" the AJAX request is called to the web service and code from the component is executed. If web service throws an exception it is displayed for user as a popup.

See also

[Category:AJAX]