Skip to content

Commit

Permalink
MDL-61245 core_blog: New Web Service core_blog_view_entries
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Oct 10, 2018
1 parent bb96ff7 commit ca48cc9
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 38 deletions.
185 changes: 148 additions & 37 deletions blog/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,72 @@
*/
class external extends external_api {

/**
* Validate access to the blog and the filters to apply when listing entries.
*
* @param array $rawwsfilters array containing the filters in WS format
* @return array context, filters to apply and the calculated courseid and user
* @since Moodle 3.6
*/
protected static function validate_access_and_filters($rawwsfilters) {
global $CFG;

if (empty($CFG->enableblogs)) {
throw new moodle_exception('blogdisable', 'blog');
}

// Init filters.
$filterstype = array(
'courseid' => PARAM_INT,
'groupid' => PARAM_INT,
'userid' => PARAM_INT,
'tagid' => PARAM_INT,
'tag' => PARAM_NOTAGS,
'cmid' => PARAM_INT,
'entryid' => PARAM_INT,
'search' => PARAM_RAW
);
$filters = array(
'courseid' => null,
'groupid' => null,
'userid' => null,
'tagid' => null,
'tag' => null,
'cmid' => null,
'entryid' => null,
'search' => null
);

foreach ($rawwsfilters as $filter) {
$name = trim($filter['name']);
if (!isset($filterstype[$name])) {
throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
}
$filters[$name] = clean_param($filter['value'], $filterstype[$name]);
}

// Do not overwrite here the filters, blog_get_headers and blog_listing will take care of that.
list($courseid, $userid) = blog_validate_access($filters['courseid'], $filters['cmid'], $filters['groupid'],
$filters['entryid'], $filters['userid']);

if ($courseid && $courseid != SITEID) {
$context = context_course::instance($courseid);
self::validate_context($context);
} else {
$context = context_system::instance();
if ($CFG->bloglevel == BLOG_GLOBAL_LEVEL) {
// Everybody can see anything - no login required unless site is locked down using forcelogin.
if ($CFG->forcelogin) {
self::validate_context($context);
}
} else {
self::validate_context($context);
}
}
// Courseid and userid may not be the same that the ones in $filters.
return array($context, $filters, $courseid, $userid);
}

/**
* Returns description of get_entries() parameters.
*
Expand Down Expand Up @@ -92,49 +158,16 @@ public static function get_entries_parameters() {
* @since Moodle 3.6
*/
public static function get_entries($filters = array(), $page = 0, $perpage = 10) {
global $CFG, $DB, $PAGE;
global $PAGE;

$warnings = array();
$params = self::validate_parameters(self::get_entries_parameters(),
array('filters' => $filters, 'page' => $page, 'perpage' => $perpage));

if (empty($CFG->enableblogs)) {
throw new moodle_exception('blogdisable', 'blog');
}

// Init filters.
$filterstype = array('courseid' => PARAM_INT, 'groupid' => PARAM_INT, 'userid' => PARAM_INT, 'tagid' => PARAM_INT,
'tag' => PARAM_NOTAGS, 'cmid' => PARAM_INT, 'entryid' => PARAM_INT, 'search' => PARAM_RAW);
$filters = array('courseid' => null, 'groupid' => null, 'userid' => null, 'tagid' => null,
'tag' => null, 'cmid' => null, 'entryid' => null, 'search' => null);

foreach ($params['filters'] as $filter) {
$name = trim($filter['name']);
if (!isset($filterstype[$name])) {
throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
}
$filters[$name] = clean_param($filter['value'], $filterstype[$name]);
}

// Do not overwrite here the filters, blog_get_headers and blog_listing will take care of that.
list($courseid, $userid) = blog_validate_access($filters['courseid'], $filters['cmid'], $filters['groupid'],
$filters['entryid'], $filters['userid']);
list($context, $filters, $courseid, $userid) = self::validate_access_and_filters($params['filters']);

if ($courseid && $courseid != SITEID) {
$context = context_course::instance($courseid);
self::validate_context($context);
} else {
$context = context_system::instance();
if ($CFG->bloglevel == BLOG_GLOBAL_LEVEL) {
// Everybody can see anything - no login required unless site is locked down using forcelogin.
if ($CFG->forcelogin) {
self::validate_context($context);
}
} else {
self::validate_context($context);
}
}
$PAGE->set_context($context); // Needed by internal APIs.
$output = $PAGE->get_renderer('core');

// Get filters.
$blogheaders = blog_get_headers($filters['courseid'], $filters['groupid'], $filters['userid'], $filters['tagid'],
Expand All @@ -148,7 +181,6 @@ public static function get_entries($filters = array(), $page = 0, $perpage = 10)
$totalentries = $bloglisting->count_entries();

$exportedentries = array();
$output = $PAGE->get_renderer('core');
foreach ($entries as $entry) {
$exporter = new post_exporter($entry, array('context' => $context));
$exportedentries[] = $exporter->export($output);
Expand Down Expand Up @@ -177,4 +209,83 @@ public static function get_entries_returns() {
)
);
}

/**
* Returns description of view_entries() parameters.
*
* @return external_function_parameters
* @since Moodle 3.6
*/
public static function view_entries_parameters() {
return new external_function_parameters(
array(
'filters' => new external_multiple_structure (
new external_single_structure(
array(
'name' => new external_value(PARAM_ALPHA,
'The expected keys (value format) are:
tag PARAM_NOTAGS blog tag
tagid PARAM_INT blog tag id
userid PARAM_INT blog author (userid)
cmid PARAM_INT course module id
entryid PARAM_INT entry id
groupid PARAM_INT group id
courseid PARAM_INT course id
search PARAM_RAW search term
'
),
'value' => new external_value(PARAM_RAW, 'The value of the filter.')
)
), 'Parameters used in the filter of view_entries.', VALUE_DEFAULT, array()
),
)
);
}

/**
* Trigger the blog_entries_viewed event.
*
* @param array $filters the parameters used in the filter of get_entries
* @return array with status result and warnings
* @since Moodle 3.6
*/
public static function view_entries($filters = array()) {

$warnings = array();
$params = self::validate_parameters(self::view_entries_parameters(), array('filters' => $filters));

list($context, $filters, $courseid, $userid) = self::validate_access_and_filters($params['filters']);

$eventparams = array(
'other' => array('entryid' => $filters['entryid'], 'tagid' => $filters['tagid'], 'userid' => $userid,
'modid' => $filters['cmid'], 'groupid' => $filters['groupid'], 'search' => $filters['search']
)
);
if (!empty($userid)) {
$eventparams['relateduserid'] = $userid;
}
$eventparams['other']['courseid'] = ($courseid === SITEID) ? 0 : $courseid;
$event = \core\event\blog_entries_viewed::create($eventparams);
$event->trigger();

return array(
'warnings' => $warnings,
'status' => true,
);
}

/**
* Returns description of view_entries() result value.
*
* @return external_description
* @since Moodle 3.6
*/
public static function view_entries_returns() {
return new external_single_structure(
array(
'status' => new external_value(PARAM_BOOL, 'status: true if success'),
'warnings' => new external_warnings(),
)
);
}
}
59 changes: 59 additions & 0 deletions blog/tests/external_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,5 +561,64 @@ public function test_get_entries_blog_disabled() {
$this->expectException('moodle_exception');
$result = core_blog\external::get_entries(array(array('name' => 'zzZZzz', 'value' => 'wwWWww')));
}

/**
* Test view_blog_entries without filter.
*/
public function test_view_blog_entries_without_filtering() {
// Test user with full capabilities.
$this->setUser($this->userid);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$result = core_blog\external::view_entries();
$result = external_api::clean_returnvalue(core_blog\external::view_entries_returns(), $result);

$events = $sink->get_events();
$this->assertCount(1, $events);
$event = array_shift($events);
// Checking that the event contains the expected values (empty, no filtering done).
$this->assertInstanceOf('\core\event\blog_entries_viewed', $event);
$this->assertEmpty($event->get_data()['relateduserid']);
$this->assertEmpty($event->get_data()['other']['entryid']);
$this->assertEmpty($event->get_data()['other']['tagid']);
$this->assertEmpty($event->get_data()['other']['userid']);
$this->assertEmpty($event->get_data()['other']['modid']);
$this->assertEmpty($event->get_data()['other']['groupid']);
$this->assertEmpty($event->get_data()['other']['search']);
$this->assertEmpty($event->get_data()['other']['courseid']);
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
}

/**
* Test view_blog_entries doing filtering.
*/
public function test_view_blog_entries_with_filtering() {
// Test user with full capabilities.
$this->setUser($this->userid);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$result = core_blog\external::view_entries(array(
array('name' => 'tagid', 'value' => $this->tagid),
array('name' => 'userid', 'value' => $this->userid),
));
$result = external_api::clean_returnvalue(core_blog\external::view_entries_returns(), $result);

$events = $sink->get_events();
$this->assertCount(1, $events);
$event = array_shift($events);
// Checking that the event contains the expected values (filter by user and tag).
$this->assertInstanceOf('\core\event\blog_entries_viewed', $event);
$this->assertEquals($this->userid, $event->get_data()['relateduserid']);
$this->assertEmpty($event->get_data()['other']['entryid']);
$this->assertEquals($this->tagid, $event->get_data()['other']['tagid']);
$this->assertEquals($this->userid, $event->get_data()['other']['userid']);
$this->assertEmpty($event->get_data()['other']['modid']);
$this->assertEmpty($event->get_data()['other']['groupid']);
$this->assertEmpty($event->get_data()['other']['search']);
$this->assertEmpty($event->get_data()['other']['courseid']);
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
}
}

9 changes: 9 additions & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@
'ajax' => true,
'loginrequired' => false,
),
'core_blog_view_entries' => array(
'classname' => 'core_blog\external',
'methodname' => 'view_entries',
'description' => 'Trigger the blog_entries_viewed event.',
'type' => 'read',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
'ajax' => true,
'loginrequired' => false,
),
'core_calendar_get_calendar_monthly_view' => array(
'classname' => 'core_calendar_external',
'methodname' => 'get_calendar_monthly_view',
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

defined('MOODLE_INTERNAL') || die();

$version = 2018092700.02; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2018092700.03; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.

Expand Down

0 comments on commit ca48cc9

Please sign in to comment.