Skip to content

Commit

Permalink
MDL-64656 core_tag: Return tags in modules and blog
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Apr 11, 2019
1 parent 52fcc0b commit 6c344ff
Show file tree
Hide file tree
Showing 22 changed files with 322 additions and 15 deletions.
13 changes: 13 additions & 0 deletions blog/classes/external/post_exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use external_files;
use renderer_base;
use context_system;
use core_tag\external\tag_item_exporter;

/**
* Class for exporting a blog post (entry).
Expand Down Expand Up @@ -171,6 +172,12 @@ protected static function define_other_properties() {
'multiple' => true,
'optional' => true
),
'tags' => array(
'type' => tag_item_exporter::read_properties_definition(),
'description' => 'Tags.',
'multiple' => true,
'optional' => true,
),
);
}

Expand All @@ -179,6 +186,12 @@ protected function get_other_values(renderer_base $output) {

$values['summaryfiles'] = external_util::get_area_files($context->id, 'blog', 'post', $this->data->id);
$values['attachmentfiles'] = external_util::get_area_files($context->id, 'blog', 'attachment', $this->data->id);
if ($this->data->module == 'blog_external') {
// For external blogs, the content field has the external blog id.
$values['tags'] = \core_tag\external\util::get_item_tags('core', 'blog_external', $this->data->content);
} else {
$values['tags'] = \core_tag\external\util::get_item_tags('core', 'post', $this->data->id);
}

return $values;
}
Expand Down
11 changes: 11 additions & 0 deletions blog/tests/external_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ public function test_get_public_entries_global_level_by_non_logged_users() {
$result = core_blog\external::get_entries();
$result = external_api::clean_returnvalue(core_blog\external::get_entries_returns(), $result);
$this->assertCount(1, $result['entries']);
$this->assertCount(1, $result['entries'][0]['tags']);
$this->assertEquals('tag1', $result['entries'][0]['tags'][0]['rawname']);

$this->assertEquals($this->postid, $result['entries'][0]['id']);
}

Expand Down Expand Up @@ -141,6 +144,9 @@ public function test_get_public_entries_global_level_by_guest_users() {
$result = core_blog\external::get_entries();
$result = external_api::clean_returnvalue(core_blog\external::get_entries_returns(), $result);
$this->assertCount(1, $result['entries']);
$this->assertCount(1, $result['entries'][0]['tags']);
$this->assertEquals('tag1', $result['entries'][0]['tags'][0]['rawname']);

$this->assertEquals($this->postid, $result['entries'][0]['id']);
}

Expand Down Expand Up @@ -331,6 +337,9 @@ public function test_get_all_entries_including_pagination() {
$result = external_api::clean_returnvalue(core_blog\external::get_entries_returns(), $result);
$this->assertCount(2, $result['entries']);
$this->assertEquals(2, $result['totalentries']);
$this->assertCount(0, $result['entries'][0]['tags']);
$this->assertCount(1, $result['entries'][1]['tags']);
$this->assertEquals('tag1', $result['entries'][1]['tags'][0]['rawname']);

$result = core_blog\external::get_entries(array(), 0, 1);
$result = external_api::clean_returnvalue(core_blog\external::get_entries_returns(), $result);
Expand Down Expand Up @@ -365,6 +374,8 @@ public function test_get_entries_filtering_by_course() {
$result = core_blog\external::get_entries(array(array('name' => 'courseid', 'value' => $this->courseid)));
$result = external_api::clean_returnvalue(core_blog\external::get_entries_returns(), $result);
$this->assertCount(1, $result['entries']);
$this->assertCount(1, $result['entries'][0]['tags']);
$this->assertEquals('tag1', $result['entries'][0]['tags'][0]['rawname']);

// There is no entry associated with a wrong course.
$result = core_blog\external::get_entries(array(array('name' => 'courseid', 'value' => $anothercourse->id)));
Expand Down
6 changes: 6 additions & 0 deletions blog/upgrade.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
This files describes API changes in /blog/* ,
information provided here is intended especially for developers.

=== 3.7 ===
* External function get_entries now returns an additional field "tags" returning the post tags.

=== 2.7 ===

* blog_entry->add_associations() does not accept any params.
Expand Down
4 changes: 4 additions & 0 deletions course/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@ public static function get_course_contents_returns() {
'userid' => new external_value(PARAM_INT, 'User who added this content to moodle'),
'author' => new external_value(PARAM_TEXT, 'Content owner'),
'license' => new external_value(PARAM_TEXT, 'Content license'),
'tags' => new external_multiple_structure(
\core_tag\external\tag_item_exporter::get_read_structure(), 'Tags',
VALUE_OPTIONAL
),
)
), VALUE_DEFAULT, array()
),
Expand Down
2 changes: 2 additions & 0 deletions course/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ information provided here is intended especially for developers.
* External function core_course_external::get_course_contents new returns the following additional completiondata field:
- valueused (indicates whether the completion state affects the availability of other content)
* External function core_course_external::get_course_contents now returns a new contentsinfo field with summary files information.
* External function core_course_external::get_course_contents now returns an additional field "tags" returning the content tags.


=== 3.6 ===

Expand Down
1 change: 1 addition & 0 deletions mod/book/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ function book_export_contents($cm, $baseurl) {
$chapterindexfile['userid'] = null;
$chapterindexfile['author'] = null;
$chapterindexfile['license'] = null;
$chapterindexfile['tags'] = \core_tag\external\util::get_item_tags('mod_book', 'book_chapters', $chapter->id);
$contents[] = $chapterindexfile;

// Chapter files (images usually).
Expand Down
21 changes: 19 additions & 2 deletions mod/book/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public function setUp() {
}

public function test_export_contents() {
global $DB;
global $DB, $CFG;
require_once($CFG->dirroot . '/course/externallib.php');

$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course(array('enablecomment' => 1));
Expand All @@ -57,7 +58,10 @@ public function test_export_contents() {
$cm = get_coursemodule_from_id('book', $book->cmid);

$bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
$chapter1 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 1));
$chapter1 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 1,
'tags' => array('Cats', 'Dogs')));
$tag = core_tag_tag::get_by_name(0, 'Cats');

$chapter2 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 2));
$subchapter = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 3, "subchapter" => 1));
$chapter3 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 4, "hidden" => 1));
Expand All @@ -71,11 +75,24 @@ public function test_export_contents() {
$this->assertEquals('structure', $contents[0]['filename']);
$this->assertEquals('index.html', $contents[1]['filename']);
$this->assertEquals('Chapter 1', $contents[1]['content']);
$this->assertCount(2, $contents[1]['tags']);
$this->assertEquals('Cats', $contents[1]['tags'][0]['rawname']);
$this->assertEquals($tag->id, $contents[1]['tags'][0]['id']);
$this->assertEquals('Dogs', $contents[1]['tags'][1]['rawname']);
$this->assertEquals('index.html', $contents[2]['filename']);
$this->assertEquals('Chapter 2', $contents[2]['content']);
$this->assertEquals('index.html', $contents[3]['filename']);
$this->assertEquals('Chapter 3', $contents[3]['content']);

// Now, test the function via the external API.
$contents = core_course_external::get_course_contents($course->id, array());
$contents = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $contents);
$this->assertEquals('book', $contents[0]['modules'][0]['modname']);
$this->assertEquals($cm->id, $contents[0]['modules'][0]['id']);
$this->assertCount(2, $contents[0]['modules'][0]['contents'][1]['tags']);
$this->assertEquals('Cats', $contents[0]['modules'][0]['contents'][1]['tags'][0]['rawname']);
$this->assertEquals('Dogs', $contents[0]['modules'][0]['contents'][1]['tags'][1]['rawname']);

// Test empty book.
$emptybook = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
$cm = get_coursemodule_from_id('book', $emptybook->cmid);
Expand Down
4 changes: 4 additions & 0 deletions mod/book/upgrade.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
This files describes API changes in the book code.

=== 3.7 ===

* book_export_contents() callback now returns tags information for every chapter.

=== 3.1 ===

* The following functions, previously used (exclusively) by upgrade steps are not available
Expand Down
10 changes: 10 additions & 0 deletions mod/data/classes/external/record_exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use core\external\exporter;
use renderer_base;
use core_user;
use core_tag\external\tag_item_exporter;

/**
* Class for exporting record data.
Expand Down Expand Up @@ -102,6 +103,12 @@ protected static function define_other_properties() {
'multiple' => true,
'optional' => true,
),
'tags' => array(
'type' => tag_item_exporter::read_properties_definition(),
'description' => 'Tags.',
'multiple' => true,
'optional' => true,
),
);
}

Expand All @@ -128,6 +135,9 @@ protected function get_other_values(renderer_base $output) {
}
$values['contents'] = $contents;
}

$values['tags'] = \core_tag\external\util::get_item_tags('mod_data', 'data_records', $this->data->id);

return $values;
}
}
7 changes: 5 additions & 2 deletions mod/data/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,9 @@ public function populate_database_with_entries() {
}

$this->setUser($this->student1);
$entry11 = $generator->create_entry($this->database, $fieldcontents, $this->group1->id);
$entry11 = $generator->create_entry($this->database, $fieldcontents, $this->group1->id, ['Cats', 'Dogs']);
$this->setUser($this->student2);
$entry12 = $generator->create_entry($this->database, $fieldcontents, $this->group1->id);
$entry12 = $generator->create_entry($this->database, $fieldcontents, $this->group1->id, ['Cats']);
$entry13 = $generator->create_entry($this->database, $fieldcontents, $this->group1->id);
// Entry not in group.
$entry14 = $generator->create_entry($this->database, $fieldcontents, 0);
Expand Down Expand Up @@ -447,10 +447,13 @@ public function test_get_entries() {
$this->assertCount(3, $result['entries']);
$this->assertEquals(3, $result['totalcount']);
$this->assertEquals($entry11, $result['entries'][0]['id']);
$this->assertCount(2, $result['entries'][0]['tags']);
$this->assertEquals($this->student1->id, $result['entries'][0]['userid']);
$this->assertEquals($this->group1->id, $result['entries'][0]['groupid']);
$this->assertEquals($this->database->id, $result['entries'][0]['dataid']);
$this->assertEquals($entry12, $result['entries'][1]['id']);
$this->assertCount(1, $result['entries'][1]['tags']);
$this->assertEquals('Cats', $result['entries'][1]['tags'][0]['rawname']);
$this->assertEquals($this->student2->id, $result['entries'][1]['userid']);
$this->assertEquals($this->group1->id, $result['entries'][1]['groupid']);
$this->assertEquals($this->database->id, $result['entries'][1]['dataid']);
Expand Down
3 changes: 3 additions & 0 deletions mod/data/upgrade.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
This files describes API changes in /mod/data - plugins,
information provided here is intended especially for developers.

=== 3.7 ===
* External functions get_entries, get_entry and search_entries now return an additional field "tags" containing the entry tags.

=== 3.4 ===
* External function mod_data_external::search_entries() now returns the maxcount field: Total count of records that the user could
see in the database (if all the search criterias were removed).
Expand Down
5 changes: 5 additions & 0 deletions mod/forum/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ public static function get_forum_discussion_posts($discussionid, $sortby = "crea
if (!empty($messageinlinefiles)) {
$post->messageinlinefiles = $messageinlinefiles;
}
// Post tags.
$post->tags = \core_tag\external\util::get_item_tags('mod_forum', 'forum_posts', $post->id);

$posts[] = $post;
}
Expand Down Expand Up @@ -465,6 +467,9 @@ public static function get_forum_discussion_posts_returns() {
'userpictureurl' => new external_value(PARAM_URL, 'Post author picture.', VALUE_OPTIONAL),
'deleted' => new external_value(PARAM_BOOL, 'This post has been removed.'),
'isprivatereply' => new external_value(PARAM_BOOL, 'The post is a private reply'),
'tags' => new external_multiple_structure(
\core_tag\external\tag_item_exporter::get_read_structure(), 'Tags', VALUE_OPTIONAL
),
), 'post'
)
),
Expand Down
7 changes: 7 additions & 0 deletions mod/forum/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ public function test_mod_forum_get_forum_discussion_posts() {

$record->parent = $discussion1reply1->id;
$record->userid = $user3->id;
$record->tags = array('Cats', 'Dogs');
$discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);

// Enrol the user in the course.
Expand Down Expand Up @@ -311,7 +312,12 @@ public function test_mod_forum_get_forum_discussion_posts() {
'userpictureurl' => '',
'deleted' => false,
'isprivatereply' => false,
'tags' => \core_tag\external\util::get_item_tags('mod_forum', 'forum_posts', $discussion1reply2->id),
);
// Cast to expected.
$this->assertCount(2, $expectedposts['posts'][0]['tags']);
$expectedposts['posts'][0]['tags'][0]['isstandard'] = (bool) $expectedposts['posts'][0]['tags'][0]['isstandard'];
$expectedposts['posts'][0]['tags'][1]['isstandard'] = (bool) $expectedposts['posts'][0]['tags'][1]['isstandard'];

$expectedposts['posts'][] = array(
'id' => $discussion1reply1->id,
Expand Down Expand Up @@ -348,6 +354,7 @@ public function test_mod_forum_get_forum_discussion_posts() {
'userpictureurl' => '',
'deleted' => false,
'isprivatereply' => false,
'tags' => array(),
);

// Test a discussion with two additional posts (total 3 posts).
Expand Down
1 change: 1 addition & 0 deletions mod/forum/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ information provided here is intended especially for developers.
* The get_forum_discussion_posts web service has been deprecated in favour of get_discussion_posts.
* The forum_count_replies function has been deprecated in favour of get_reply_count_for_post_id_in_discussion_id in
the Post vault.
* External function get_forum_discussion_posts now returns an additional field "tags" returning the post tags.

=== 3.6 ===

Expand Down
5 changes: 5 additions & 0 deletions mod/glossary/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ protected static function get_entry_return_structure($includecat = false) {
'casesensitive' => new external_value(PARAM_BOOL, 'When true, the matching is case sensitive'),
'fullmatch' => new external_value(PARAM_BOOL, 'When true, the matching is done on full words only'),
'approved' => new external_value(PARAM_BOOL, 'Whether the entry was approved'),
'tags' => new external_multiple_structure(
\core_tag\external\tag_item_exporter::get_read_structure(), 'Tags', VALUE_OPTIONAL
),
);

if ($includecat) {
Expand Down Expand Up @@ -149,6 +152,8 @@ protected static function fill_entry_details($entry, $context) {
if (!empty($definitioninlinefiles)) {
$entry->definitioninlinefiles = $definitioninlinefiles;
}

$entry->tags = \core_tag\external\util::get_item_tags('mod_glossary', 'glossary_entries', $entry->id);
}

/**
Expand Down
Loading

0 comments on commit 6c344ff

Please sign in to comment.