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 01:29, 3 February 2016 by Marina Glancy (talk | contribs)

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, $object->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

  • Template core/inplace_editable
  • Javascript module core/inplace_editable
  • Webservice core_update_inplace_editable available from AJAX
  • Interface core\inplace_editable_saver_base

See also

[Category:AJAX]