Skip to content

Commit

Permalink
MDL-66061 comments: Support pagination and sorting in get_comments WS
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Aug 19, 2019
1 parent 7e16c70 commit 48f5ca7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 17 deletions.
44 changes: 30 additions & 14 deletions comment/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ public static function get_comments_parameters() {

return new external_function_parameters(
array(
'contextlevel' => new external_value(PARAM_ALPHA, 'contextlevel system, course, user...'),
'instanceid' => new external_value(PARAM_INT, 'the Instance id of item associated with the context level'),
'component' => new external_value(PARAM_COMPONENT, 'component'),
'itemid' => new external_value(PARAM_INT, 'associated id'),
'area' => new external_value(PARAM_AREA, 'string comment area', VALUE_DEFAULT, ''),
'page' => new external_value(PARAM_INT, 'page number (0 based)', VALUE_DEFAULT, 0),
'contextlevel' => new external_value(PARAM_ALPHA, 'contextlevel system, course, user...'),
'instanceid' => new external_value(PARAM_INT, 'the Instance id of item associated with the context level'),
'component' => new external_value(PARAM_COMPONENT, 'component'),
'itemid' => new external_value(PARAM_INT, 'associated id'),
'area' => new external_value(PARAM_AREA, 'string comment area', VALUE_DEFAULT, ''),
'page' => new external_value(PARAM_INT, 'page number (0 based)', VALUE_DEFAULT, 0),
'sortdirection' => new external_value(PARAM_ALPHA, 'Sort direction: ASC or DESC', VALUE_DEFAULT, 'DESC'),
)
);
}
Expand All @@ -68,22 +69,33 @@ public static function get_comments_parameters() {
* @param int $itemid the item id
* @param string $area comment area
* @param int $page page number
* @param string $sortdirection sort direction
* @return array of comments and warnings
* @since Moodle 2.9
*/
public static function get_comments($contextlevel, $instanceid, $component, $itemid, $area = '', $page = 0) {
public static function get_comments($contextlevel, $instanceid, $component, $itemid, $area = '', $page = 0,
$sortdirection = 'DESC') {
global $CFG;

$warnings = array();
$arrayparams = array(
'contextlevel' => $contextlevel,
'instanceid' => $instanceid,
'component' => $component,
'itemid' => $itemid,
'area' => $area,
'page' => $page
'contextlevel' => $contextlevel,
'instanceid' => $instanceid,
'component' => $component,
'itemid' => $itemid,
'area' => $area,
'page' => $page,
'sortdirection' => $sortdirection,
);
$params = self::validate_parameters(self::get_comments_parameters(), $arrayparams);

$sortdirection = strtoupper($params['sortdirection']);
$directionallowedvalues = array('ASC', 'DESC');
if (!in_array($sortdirection, $directionallowedvalues)) {
throw new invalid_parameter_exception('Invalid value for sortdirection parameter (value: ' . $sortdirection . '),' .
'allowed values are: ' . implode(',', $directionallowedvalues));
}

$context = self::get_context_from_params($params);
self::validate_context($context);

Expand All @@ -96,7 +108,7 @@ public static function get_comments($contextlevel, $instanceid, $component, $ite
$args->component = $params['component'];

$commentobject = new comment($args);
$comments = $commentobject->get_comments($params['page']);
$comments = $commentobject->get_comments($params['page'], $sortdirection);

// False means no permissions to see comments.
if ($comments === false) {
Expand All @@ -117,6 +129,8 @@ public static function get_comments($contextlevel, $instanceid, $component, $ite

$results = array(
'comments' => $comments,
'count' => $commentobject->count(),
'perpage' => (!empty($CFG->commentsperpage)) ? $CFG->commentsperpage : 15,
'warnings' => $warnings
);
return $results;
Expand Down Expand Up @@ -148,6 +162,8 @@ public static function get_comments_returns() {
), 'comment'
), 'List of comments'
),
'count' => new external_value(PARAM_INT, 'Total number of comments.', VALUE_OPTIONAL),
'perpage' => new external_value(PARAM_INT, 'Number of comments per page.', VALUE_OPTIONAL),
'warnings' => new external_warnings()
)
);
Expand Down
6 changes: 4 additions & 2 deletions comment/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,10 @@ public function output($return = true) {
* Return matched comments
*
* @param int $page
* @param str $sortdirection sort direction, ASC or DESC
* @return array
*/
public function get_comments($page = '') {
public function get_comments($page = '', $sortdirection = 'DESC') {
global $DB, $CFG, $USER, $OUTPUT;
if (!$this->can_view()) {
return false;
Expand All @@ -557,14 +558,15 @@ public function get_comments($page = '') {
$params['component'] = $component;
}

$sortdirection = ($sortdirection === 'ASC') ? 'ASC' : 'DESC';
$sql = "SELECT $ufields, c.id AS cid, c.content AS ccontent, c.format AS cformat, c.timecreated AS ctimecreated
FROM {comments} c
JOIN {user} u ON u.id = c.userid
WHERE c.contextid = :contextid AND
c.commentarea = :commentarea AND
c.itemid = :itemid AND
$componentwhere
ORDER BY c.timecreated DESC";
ORDER BY c.timecreated $sortdirection";
$params['contextid'] = $this->contextid;
$params['commentarea'] = $this->commentarea;
$params['itemid'] = $this->itemid;
Expand Down
25 changes: 24 additions & 1 deletion comment/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,34 @@ public function test_get_comments() {

$this->assertCount(0, $result['warnings']);
$this->assertCount(2, $result['comments']);
$this->assertEquals(2, $result['count']);
$this->assertEquals(15, $result['perpage']);

$this->assertEquals($user->id, $result['comments'][0]['userid']);
$this->assertEquals($user->id, $result['comments'][1]['userid']);

$this->assertEquals($cmtid2, $result['comments'][0]['id']);
$this->assertEquals($cmtid2, $result['comments'][0]['id']); // Default ordering newer first.
$this->assertEquals($cmtid1, $result['comments'][1]['id']);

// Test sort direction and pagination.
$CFG->commentsperpage = 1;
$result = core_comment_external::get_comments($contextlevel, $instanceid, $component, $itemid, $area, $page, 'ASC');
$result = external_api::clean_returnvalue(core_comment_external::get_comments_returns(), $result);

$this->assertCount(0, $result['warnings']);
$this->assertCount(1, $result['comments']); // Only one per page.
$this->assertEquals(2, $result['count']);
$this->assertEquals($CFG->commentsperpage, $result['perpage']);
$this->assertEquals($cmtid1, $result['comments'][0]['id']); // Comments order older first.

// Next page.
$result = core_comment_external::get_comments($contextlevel, $instanceid, $component, $itemid, $area, $page + 1, 'ASC');
$result = external_api::clean_returnvalue(core_comment_external::get_comments_returns(), $result);

$this->assertCount(0, $result['warnings']);
$this->assertCount(1, $result['comments']);
$this->assertEquals(2, $result['count']);
$this->assertEquals($CFG->commentsperpage, $result['perpage']);
$this->assertEquals($cmtid2, $result['comments'][0]['id']);
}
}
7 changes: 7 additions & 0 deletions comment/upgrade.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This files describes API changes in /comment/* ,
information provided here is intended especially for developers.

=== 3.8 ===
* External function get_comments now returns the total count of comments and the number of comments per page.
It also has a new parameter to indicate the sorting direction (defaulted to DESC).

0 comments on commit 48f5ca7

Please sign in to comment.