MDL-51802 core: new template for quick editing a title

convert editing tag name to use new template
This commit is contained in:
Marina Glancy 2016-01-18 15:18:14 +08:00
parent 2f45a11ac4
commit cdc5f9785b
23 changed files with 636 additions and 171 deletions

View file

@ -346,4 +346,65 @@ class core_external extends external_api {
)
);
}
/**
* Parameters for function update_inplace_editable()
*
* @since Moodle 3.1
* @return external_function_parameters
*/
public static function update_inplace_editable_parameters() {
return new external_function_parameters(
array(
'component' => new external_value(PARAM_COMPONENT, 'component responsible for the update', VALUE_REQUIRED),
'itemtype' => new external_value(PARAM_NOTAGS, 'type of the updated item inside the component', VALUE_REQUIRED),
'itemid' => new external_value(PARAM_INT, 'identifier of the updated item', VALUE_REQUIRED),
'value' => new external_value(PARAM_RAW, 'new value', VALUE_REQUIRED),
));
}
/**
* Update any component's editable value assuming that component implements necessary callback
*
* @since Moodle 3.1
* @param string $component
* @param string $itemtype
* @param string $itemid
* @param string $value
*/
public static function update_inplace_editable($component, $itemtype, $itemid, $value) {
global $PAGE;
// Validate and normalize parameters.
$params = self::validate_parameters(self::update_inplace_editable_parameters(),
array('component' => $component, 'itemtype' => $itemtype, 'itemid' => $itemid, 'value' => $value));
if (!$functionname = component_callback_exists($component, 'inplace_editable')) {
throw new \moodle_exception('inplaceeditableerror');
}
$tmpl = component_callback($params['component'], 'inplace_editable',
array($params['itemtype'], $params['itemid'], $params['value']));
if (!$tmpl || !($tmpl instanceof \core\output\inplace_editable)) {
throw new \moodle_exception('inplaceeditableerror');
}
return $tmpl->export_for_template($PAGE->get_renderer('core'));
}
/**
* Return structure for update_inplace_editable()
*
* @since Moodle 3.1
* @return external_description
*/
public static function update_inplace_editable_returns() {
return new external_single_structure(
array(
'displayvalue' => new external_value(PARAM_RAW, 'display value (may contain link or other html tags)'),
'component' => new external_value(PARAM_NOTAGS, 'component responsible for the update', VALUE_OPTIONAL),
'itemtype' => new external_value(PARAM_NOTAGS, 'itemtype', VALUE_OPTIONAL),
'value' => new external_value(PARAM_RAW, 'value of the item as it is stored', VALUE_OPTIONAL),
'itemid' => new external_value(PARAM_RAW, 'identifier of the updated item', VALUE_OPTIONAL),
'edithint' => new external_value(PARAM_NOTAGS, 'hint for editing element', VALUE_OPTIONAL),
'editlabel' => new external_value(PARAM_NOTAGS, 'label for editing element', VALUE_OPTIONAL),
)
);
}
}

View file

@ -135,4 +135,27 @@ class core_external_testcase extends externallib_advanced_testcase {
$this->assertSame($string['string'], $wsstrings[$string['stringid']]);
}
}
/**
* Test update_inplace_editable()
*/
public function test_update_inplace_editable() {
$this->resetAfterTest(true);
// Call service for component that does not have inplace_editable callback.
try {
core_external::update_inplace_editable('tool_log', 'itemtype', 1, 'newvalue');
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertEquals('Error calling update processor', $e->getMessage());
}
// This is a very basic test for the return value of the external function.
// More detailed test for tag updating can be found in core_tag component.
$this->setAdminUser();
$tag = $this->getDataGenerator()->create_tag();
$res = core_external::update_inplace_editable('core_tag', 'tagname', $tag->id, 'new tag name');
$res = external_api::clean_returnvalue(core_external::update_inplace_editable_returns(), $res);
$this->assertEquals('new tag name', $res['value']);
}
}