Skip to content

Commit

Permalink
MDL-49203 webservices: New WS core_comment_get_comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Costantino Cito authored and jleyva committed Mar 26, 2015
1 parent 20d3883 commit be98541
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 1 deletion.
151 changes: 151 additions & 0 deletions comment/classes/external.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?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/>.

/**
* External comment API
*
* @package core_comment
* @category external
* @copyright Costantino Cito <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.9
*/

require_once("$CFG->libdir/externallib.php");
require_once("$CFG->dirroot/comment/lib.php");

/**
* External comment API functions
*
* @package core_comment
* @category external
* @copyright Costantino Cito <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.9
*/
class core_comment_external extends external_api {
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 2.9
*/
public static function get_comments_parameters() {

return new external_function_parameters(
array(
'contextlevel' => new external_value(PARAM_ALPHA, 'contextlevel'),
'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_TEXT, 'string comment area', VALUE_DEFAULT, ''),
'page' => new external_value(PARAM_INT, 'page number', VALUE_DEFAULT, 0),
)
);
}

/**
* Get comments
*
* @param string $contextlevel ('context_course', etc..)
* @param int $instanceid (eg. the 'id' in the 'book' table)
* @param string $component the name of the component
* @param int $itemid the item id
* @param string|null $area
* @param int $page page number
* @return array of comments and warnings
* @since Moodle 2.9
*/
public static function get_comments($contextlevel, $instanceid, $component, $itemid, $area='', $page=0) {

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

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

require_capability('moodle/comment:view', $context);

$args = new stdClass;
$args->context = $context;
$args->area = $params['area'];
$args->itemid = $params['itemid'];
$args->component = $params['component'];

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

if ($comments === false) {
throw new moodle_exception('nopermissions', 'error', '', 'view comments');
}

foreach ($comments as &$comment) {

list($comment->content, $comment->format) = external_format_text($comment->content,
$comment->format,
$context->id,
$params['component'],
'',
0);
$comment = (array)$comment;
}

$results = array(
'comments' => $comments,
'warnings' => $warnings
);
return $results;
}

/**
* Returns description of method result value
*
* @return external_description
* @since Moodle 2.9
*/
public static function get_comments_returns() {
return new external_single_structure(
array(
'comments' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'Comment ID'),
'content' => new external_value(PARAM_RAW, 'The content text formated'),
'format' => new external_format_value('content'),
'timecreated' => new external_value(PARAM_INT, 'Time created (timestamp)'),
'strftimeformat' => new external_value(PARAM_RAW, 'Time format'),
'profileurl' => new external_value(PARAM_URL, 'URL profile'),
'fullname' => new external_value(PARAM_TEXT, 'fullname'),
'time' => new external_value(PARAM_RAW, 'Time in human format'),
'avatar' => new external_value(PARAM_RAW, 'HTML user picture'),
'userid' => new external_value(PARAM_INT, 'User ID'),
'delete' => new external_value(PARAM_BOOL, 'Permission to delete=true/false', VALUE_OPTIONAL)
), 'comment'
), 'List of comments'
),
'warnings' => new external_warnings()
)
);
}
}
112 changes: 112 additions & 0 deletions comment/tests/externallib_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?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/>.

/**
* Events tests.
*
* @package block_comments
* @category test
* @copyright 2013 Rajesh Taneja <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

/**
* Events tests class.
*
* @package block_comments
* @category test
* @copyright 2013 Rajesh Taneja <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
global $CFG;
class core_comment_commentlib_testcase extends advanced_testcase {

public $course;
public $wiki;

public function test_comments_get_comments() {

$commenttext = 'New comment';
//$this->resetAfterTest();
$this->setAdminUser();

$this->course = $this->getDataGenerator()->create_course();
$this->wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $this->course->id));
require_once($CFG->dirroot . '/comment/lib.php');

// Comment on course page.
$context = context_course::instance($this->course->id);
$args = new stdClass;
$args->context = $context;
$args->course = $this->course;
$args->area = 'page_comments';
$args->itemid = 0;
$args->component = 'block_comments';
$args->linktext = get_string('showcomments');
$args->notoggle = true;
$args->autostart = true;
$args->displaycancel = false;
$comment = new comment($args);

// Triggering and capturing the event.
$sink = $this->redirectEvents();
$comment->add($commenttext);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);

// Checking that the event contains the expected values.
$this->assertInstanceOf('\block_comments\event\comment_created', $event);
$this->assertEquals($context, $event->get_context());
$url = new moodle_url('/course/view.php', array('id' => $this->course->id));
$this->assertEquals($url, $event->get_url());
$a = $comment->get_comments(0);
print_object($a);
exit();

/*
// Comments when block is on module (wiki) page.
$context = context_module::instance($this->wiki->cmid);
$args = new stdClass;
$args->context = $context;
$args->course = $this->course;
$args->area = 'page_comments';
$args->itemid = 0;
$args->component = 'block_comments';
$args->linktext = get_string('showcomments');
$args->notoggle = true;
$args->autostart = true;
$args->displaycancel = false;
$comment = new comment($args);
// Triggering and capturing the event.
$sink = $this->redirectEvents();
$comment->add('New comment 1');
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\block_comments\event\comment_created', $event);
$this->assertEquals($context, $event->get_context());
$url = new moodle_url('/mod/wiki/view.php', array('id' => $this->wiki->cmid));
$this->assertEquals($url, $event->get_url());
$this->assertEventContextNotUsed($event);
*/
}
}
1 change: 1 addition & 0 deletions lib/classes/component.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ protected static function fetch_subsystems() {
'cache' => $CFG->dirroot.'/cache',
'calendar' => $CFG->dirroot.'/calendar',
'cohort' => $CFG->dirroot.'/cohort',
'comment' => $CFG->dirroot.'/comment',
'completion' => $CFG->dirroot.'/completion',
'countries' => null,
'course' => $CFG->dirroot.'/course',
Expand Down
10 changes: 10 additions & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@
'type' => 'read',
'capabilities'=> 'moodle/cohort:view',
),
// Comments related functions.

'core_comment_get_comments' => array(
'classname' => 'core_comment_external',
'methodname' => 'get_comments',
'description' => 'Returns comments.',
'type' => 'read',
'capabilities'=> 'moodle/comment:view',
),

// Grade related functions.

'core_grades_get_grades' => array(
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 = 2015032600.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2015032600.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.

Expand Down

0 comments on commit be98541

Please sign in to comment.