MDL-53791 mod_wiki: New Web Service get_page_for_editing

This commit is contained in:
Pau Ferrer Ocaña 2016-04-14 10:12:20 +02:00
parent 6c1342d7e5
commit 20dca621e0
3 changed files with 156 additions and 0 deletions

View File

@ -812,4 +812,111 @@ class mod_wiki_external extends external_api {
return array($groupid, $userid); return array($groupid, $userid);
} }
/**
* Describes the parameters for get_page_for_editing.
*
* @return external_function_parameters
* @since Moodle 3.1
*/
public static function get_page_for_editing_parameters() {
return new external_function_parameters (
array(
'pageid' => new external_value(PARAM_INT, 'Page ID to edit.'),
'section' => new external_value(PARAM_TEXT, 'Section page title.', VALUE_DEFAULT, null)
)
);
}
/**
* Locks and retrieves info of page-section to be edited.
*
* @param int $pageid The page ID.
* @param string $section Section page title.
* @return array of warnings and page data.
* @since Moodle 3.1
*/
public static function get_page_for_editing($pageid, $section = null) {
global $USER;
$params = self::validate_parameters(self::get_page_for_editing_parameters(),
array(
'pageid' => $pageid,
'section' => $section
)
);
$warnings = array();
// Get wiki page.
if (!$page = wiki_get_page($params['pageid'])) {
throw new moodle_exception('incorrectpageid', 'wiki');
}
// Get wiki instance.
if (!$wiki = wiki_get_wiki_from_pageid($params['pageid'])) {
throw new moodle_exception('incorrectwikiid', 'wiki');
}
// Get subwiki instance.
if (!$subwiki = wiki_get_subwiki($page->subwikiid)) {
throw new moodle_exception('incorrectsubwikiid', 'wiki');
}
// Permission validation.
$cm = get_coursemodule_from_instance('wiki', $wiki->id, $wiki->course);
$context = context_module::instance($cm->id);
self::validate_context($context);
if (!wiki_user_can_edit($subwiki)) {
throw new moodle_exception('cannoteditpage', 'wiki');
}
if (!wiki_set_lock($params['pageid'], $USER->id, $params['section'], true)) {
throw new moodle_exception('pageislocked', 'wiki');
}
$version = wiki_get_current_version($page->id);
if (empty($version)) {
throw new moodle_exception('versionerror', 'wiki');
}
if (!is_null($params['section'])) {
$content = wiki_parser_proxy::get_section($version->content, $version->contentformat, $params['section']);
} else {
$content = $version->content;
}
$pagesection = array();
$pagesection['content'] = $content;
$pagesection['contentformat'] = $version->contentformat;
$pagesection['version'] = $version->version;
$result = array();
$result['pagesection'] = $pagesection;
$result['warnings'] = $warnings;
return $result;
}
/**
* Describes the get_page_for_editing return value.
*
* @return external_single_structure
* @since Moodle 3.1
*/
public static function get_page_for_editing_returns() {
return new external_single_structure(
array(
'pagesection' => new external_single_structure(
array(
'content' => new external_value(PARAM_RAW, 'The contents of the page-section to be edited.'),
'contentformat' => new external_value(PARAM_TEXT, 'Format of the original content of the page.'),
'version' => new external_value(PARAM_INT, 'Latest version of the page.'),
'warnings' => new external_warnings()
)
)
)
);
}
} }

View File

@ -87,5 +87,14 @@ $functions = array(
'type' => 'read', 'type' => 'read',
'capabilities' => 'mod/wiki:viewpage', 'capabilities' => 'mod/wiki:viewpage',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE) 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
'mod_wiki_get_page_for_editing' => array(
'classname' => 'mod_wiki_external',
'methodname' => 'get_page_for_editing',
'description' => 'Locks and retrieves info of page-section to be edited.',
'type' => 'write',
'capabilities' => 'mod/wiki:editpage',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
) )
); );

View File

@ -1127,4 +1127,44 @@ class mod_wiki_external_testcase extends externallib_advanced_testcase {
$this->assertEquals($expectedfile, $result['files'][0]); $this->assertEquals($expectedfile, $result['files'][0]);
} }
/**
* Test get_page_for_editing. We won't test all the possible cases because that's already
* done in the tests for wiki_parser_proxy::get_section.
*/
public function test_get_page_for_editing() {
$this->create_individual_wikis_with_groups();
$sectioncontent = '<h1>Title1</h1>Text inside section';
$pagecontent = $sectioncontent.'<h1>Title2</h1>Text inside section';
$newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page(
$this->wiki, array('content' => $pagecontent));
// Test user with full capabilities.
$this->setUser($this->student);
// Set expected result: Full Page content.
$expected = array(
'content' => $pagecontent,
'contentformat' => 'html',
'version' => '1'
);
$result = mod_wiki_external::get_page_for_editing($newpage->id);
$result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result);
$this->assertEquals($expected, $result['pagesection']);
// Set expected result: Section Page content.
$expected = array(
'content' => $sectioncontent,
'contentformat' => 'html',
'version' => '1'
);
$result = mod_wiki_external::get_page_for_editing($newpage->id, 'Title1');
$result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result);
$this->assertEquals($expected, $result['pagesection']);
}
} }