MDL-64656 core_tag: New WebService core_tag_get_tag_collections

This commit is contained in:
Juan Leyva 2019-02-27 13:33:13 +01:00
parent 07a4883796
commit 87bdad57c4
4 changed files with 171 additions and 0 deletions

View file

@ -520,4 +520,63 @@ class core_tag_external extends external_api {
)
);
}
/**
* Returns description of get_tag_collections() parameters.
*
* @return external_function_parameters
* @since Moodle 3.7
*/
public static function get_tag_collections_parameters() {
return new external_function_parameters(array());
}
/**
* Retrieves existing tag collections.
*
* @return array an array of warnings and tag collections
* @throws moodle_exception
* @since Moodle 3.7
*/
public static function get_tag_collections() {
global $CFG, $PAGE;
if (empty($CFG->usetags)) {
throw new moodle_exception('tagsaredisabled', 'tag');
}
$context = context_system::instance();
self::validate_context($context);
$PAGE->set_context($context); // Needed by internal APIs.
$output = $PAGE->get_renderer('core');
$collections = core_tag_collection::get_collections();
$exportedcollections = array();
foreach ($collections as $collection) {
$exporter = new \core_tag\external\tag_collection_exporter($collection);
$exportedcollections[] = $exporter->export($output);
}
return array(
'collections' => $exportedcollections,
'warnings' => array(),
);
}
/**
* Returns description of get_tag_collections() result value.
*
* @return external_description
* @since Moodle 3.7
*/
public static function get_tag_collections_returns() {
return new external_single_structure(
array(
'collections' => new external_multiple_structure(
\core_tag\external\tag_collection_exporter::get_read_structure()
),
'warnings' => new external_warnings(),
)
);
}
}

View file

@ -0,0 +1,82 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains related class for displaying information of a tag collection.
*
* @package core_tag
* @copyright 2019 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_tag\external;
defined('MOODLE_INTERNAL') || die();
use core\external\exporter;
/**
* Contains related class for displaying information of a tag collection.
*
* @package core_tag
* @copyright 2019 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tag_collection_exporter extends exporter {
/**
* Return the list of properties.
*
* @return array
*/
protected static function define_properties() {
return [
'id' => [
'type' => PARAM_INT,
'description' => 'Collection id.',
],
'name' => [
'type' => PARAM_NOTAGS,
'description' => 'Collection name.',
'null' => NULL_ALLOWED,
],
'isdefault' => [
'type' => PARAM_BOOL,
'description' => 'Whether is the default collection.',
'default' => false,
],
'component' => [
'type' => PARAM_COMPONENT,
'description' => 'Component the collection is related to.',
'null' => NULL_ALLOWED,
],
'sortorder' => [
'type' => PARAM_INT,
'description' => 'Collection ordering in the list.',
],
'searchable' => [
'type' => PARAM_BOOL,
'description' => 'Whether the tag collection is searchable.',
'default' => true,
],
'customurl' => [
'type' => PARAM_NOTAGS,
'description' => 'Custom URL for the tag page instead of /tag/index.php.',
'null' => NULL_ALLOWED,
],
];
}
}

View file

@ -252,4 +252,27 @@ class core_tag_external_testcase extends externallib_advanced_testcase {
$this->assertEquals($areas[$area['id']]->itemtype, $area['itemtype']);
}
}
/**
* Test get_tag_collections.
*/
public function test_get_tag_collections() {
global $DB;
$this->resetAfterTest(true);
// Create new tag collection.
$data = (object) array('name' => 'new tag coll');
core_tag_collection::create($data);
$this->setAdminUser();
$result = core_tag_external::get_tag_collections();
$result = external_api::clean_returnvalue(core_tag_external::get_tag_collections_returns(), $result);
$collections = $DB->get_records('tag_coll');
$this->assertCount(count($collections), $result['collections']);
foreach ($result['collections'] as $collection) {
$this->assertEquals($collections[$collection['id']]->component, $collection['component']);
$this->assertEquals($collections[$collection['id']]->name, $collection['name']);
}
}
}