2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Contains class \core\output\inplace_editable
22 * @copyright 2016 Marina Glancy
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 namespace core\output;
33 * Class allowing to quick edit a title inline
35 * This class is used for displaying an element that can be in-place edited by the user. To display call:
36 * echo $OUTPUT->render($element);
38 * echo $OUTPUT->render_from_template('core/inplace_editable', $element->export_for_template($OUTPUT));
40 * Template core/inplace_editable will automatically load javascript module with the same name
41 * core/inplace_editable. Javascript module registers a click-listener on edit link and
42 * then replaces the displayed value with an input field. On "Enter" it sends a request
43 * to web service core_update_inplace_editable, which invokes the callback from the component.
44 * Any exception thrown by the web service (or callback) is displayed as an error popup.
46 * Callback {$component}_inplace_editable($itemtype, $itemid, $newvalue) must be present in the lib.php file of
47 * the component or plugin. It must return instance of this class.
51 * @copyright 2016 Marina Glancy
52 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
54 class inplace_editable implements templatable, renderable {
57 * @var string component responsible for diplsying/updating
59 protected $component = null;
62 * @var string itemtype inside the component
64 protected $itemtype = null;
67 * @var int identifier of the editable element (usually database id)
69 protected $itemid = null;
72 * @var string value of the editable element as it is present in the database
74 protected $value = null;
77 * @var string value of the editable element as it should be displayed,
78 * must be formatted and may contain links or other html tags
80 protected $displayvalue = null;
83 * @var string label for the input element (for screenreaders)
85 protected $editlabel = null;
88 * @var string hint for the input element (for screenreaders)
90 protected $edithint = null;
93 * @var bool indicates if the current user is allowed to edit this element - set in constructor after permissions are checked
95 protected $editable = false;
100 * @param string $component name of the component or plugin responsible for the updating of the value (must declare callback)
101 * @param string $itemtype type of the item inside the component - each component/plugin may implement multiple inplace-editable elements
102 * @param int $itemid identifier of the item that can be edited in-place
103 * @param bool $editable whether this value is editable (check capabilities and editing mode), if false, only "displayvalue"
104 * will be displayed without anything else
105 * @param string $displayvalue what needs to be displayed to the user, it must be cleaned, with applied filters (call
106 * {@link format_string()}). It may be wrapped in an html link, contain icons or other decorations
107 * @param string $value what needs to be edited - usually raw value from the database, it may contain multilang tags
108 * @param lang_string|string $edithint hint (title) that will be displayed under the edit link
109 * @param lang_string|string $editlabel label for the input element in the editing mode (for screenreaders)
111 public function __construct($component, $itemtype, $itemid, $editable,
112 $displayvalue, $value = null, $edithint = null, $editlabel = null) {
113 $this->component = $component;
114 $this->itemtype = $itemtype;
115 $this->itemid = $itemid;
116 $this->editable = $editable;
117 $this->displayvalue = $displayvalue;
118 $this->value = $value;
119 $this->edithint = $edithint;
120 $this->editlabel = $editlabel;
124 * Export this data so it can be used as the context for a mustache template (core/inplace_editable).
126 * @param renderer_base $output typically, the renderer that's calling this function
127 * @return array data context for a mustache template
129 public function export_for_template(\renderer_base $output) {
130 if (!$this->editable) {
132 'displayvalue' => (string)$this->displayvalue
137 'component' => $this->component,
138 'itemtype' => $this->itemtype,
139 'itemid' => $this->itemid,
140 'displayvalue' => (string)$this->displayvalue,
141 'value' => (string)$this->value,
142 'edithint' => (string)$this->edithint,
143 'editlabel' => (string)$this->editlabel,