MDL-49203 webservices: New WS core_comment_get_comments

This commit is contained in:
Costantino Cito 2015-02-27 17:06:27 +01:00 committed by Juan Leyva
parent 20d38830ae
commit be9854162f
5 changed files with 275 additions and 1 deletions

View File

@ -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 <ccito@cvaconsulting.com>
* @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 <ccito@cvaconsulting.com>
* @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()
)
);
}
}

View File

@ -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 <rajesh@moodle.com>
* @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 <rajesh@moodle.com>
* @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);
*/
}
}

View File

@ -348,6 +348,7 @@ $cache = '.var_export($cache, true).';
'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',

View File

@ -99,6 +99,16 @@ $functions = array(
'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(

View File

@ -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.