Note:

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

Inplace editable: Difference between revisions

From MoodleDocs
(Created page with "inplace_editable is a mini-API introduced in Moodle 3.1. It allows developers easily add in-place editing of a value on any page. The interface looks very similar to activity...")
 
No edit summary
Line 1: Line 1:
inplace_editable is a mini-API introduced in 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.  
inplace_editable is a mini-API being introduced under [https://tracker.moodle.org/browse/MDL-51802 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.  


== inplace_editable consists of ==
== Implementing inplace_editable in a plugin ==
 
* Template core/inplace_editable
* Javascript module core/inplace_editable
* Webservice core_update_inplace_editable available from AJAX
* Interface core\inplace_editable_saver_base


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


Define class \yourpluginname\inplace_editable_saver, typically it will look like this:
Create a file */admin/tool/mytest/classes/inplace_editable_saver.php* with a class:
<pre>
<pre>
namespace yourpluginname;
namespace tool_mytest;
class inplace_editable_saver implements core\inplace_editable_saver_base {
class inplace_editable_saver implements core\inplace_editable_saver_base {
     public function render_value($itemtype, $object) {
     public function render_value($itemtype, $record) {
         $value = $object->name;
         $value = $record->name;
         $displayvalue = format_string($value); // You can add html link to the displayvalue.
         $displayvalue = format_string($value); // You can add html link to the displayvalue.
         $editable = has_capability('....', context_system::instance());
         $editable = has_capability('tool/mytest:update', context_system::instance());
         $edithint = 'Edit name';
         $edithint = 'Edit mytest name';
         $editlabel = 'New value for '.format_string($value); // Obviously, use strings here!
         $editlabel = 'New value for ' . format_string($value); // Obviously, use language strings here!
         return new \core\output\inplace_editable('yourpluginname', $itemtype, $object->id, $editable, $displayvalue,
         return new \core\output\inplace_editable('tool_mytest', $itemtype, $object->id, $editable, $displayvalue,
             $value, $edithint, $editlabel);
             $value, $edithint, $editlabel);
     }
     }


     public function update_value($itemtype, $itemid, $newvalue) {
     public function update_value($itemtype, $itemid, $newvalue) {
        global $DB;
         // Always clean and validate input!
         // Always clean and validate input!
         $newvalue = clean_param($newvalue, PARAM_NOTAGS);
         $newvalue = clean_param($newvalue, PARAM_NOTAGS);
         // Check permission of the user to update this item. Call require_login($course) if needed.
         // Check permission of the user to update this item. Call require_login($course) if needed.
         require_capability('....', context_system::instance());
         require_capability('tool/mytest:update', context_system::instance());
         $object = $DB->get_record('tablename', array('id' => $itemid), '*', MUST_EXIST);
         $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).
         // Update the record (the best way is to call an existing function for it).
         $DB->update_record('tablename', array('id' => $itemid, 'name' => $newvalue));
         $DB->update_record('tool_mytest_mytable', array('id' => $itemid, 'name' => $newvalue));
         return $this->render_value($itemtype, $object);
        $record->name = $newvalue;
         return $this->render_value($itemtype, $record);
     }
     }
}
}
</pre>
</pre>


In your renderer where you display the value call:
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:


<pre>
<pre>
        $tmpl = new core_tag\inplace_editable_saver();
$tmpl = new \tool_mytest\inplace_editable_saver();
        echo $OUTPUT->render($tmpl->render_value($itemtype, $object));
echo $OUTPUT->render($tmpl->render_value('mytestname', $record));
</pre>
</pre>


That's it!
== 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 ==
== See also ==


[Category:AJAX]
[Category:AJAX]

Revision as of 01:26, 3 February 2016

inplace_editable is a mini-API being introduced under [https://tracker.moodle.org/browse/MDL-51802 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]