Merge branch 'MDL-49203-master' of git://github.com/jleyva/moodle

This commit is contained in:
Eloy Lafuente (stronk7)
2015-03-30 20:01:10 +02:00
6 changed files with 300 additions and 2 deletions
+151
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 <[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 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),
)
);
}
/**
* Return a list of comments
*
* @param string $contextlevel ('system, course, user', etc..)
* @param int $instanceid
* @param string $component the name of the component
* @param int $itemid the item id
* @param string $area comment 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($params['page']);
// False means no permissions to see comments.
if ($comments === false) {
throw new moodle_exception('nopermissions', 'error', '', 'view comments');
}
foreach ($comments as $key => $comment) {
list($comments[$key]->content, $comments[$key]->format) = external_format_text($comment->content,
$comment->format,
$context->id,
$params['component'],
'',
0);
}
$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_NOTAGS, 'Time format'),
'profileurl' => new external_value(PARAM_URL, 'URL profile'),
'fullname' => new external_value(PARAM_NOTAGS, 'fullname'),
'time' => new external_value(PARAM_NOTAGS, '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()
)
);
}
}
+132
View File
@@ -0,0 +1,132 @@
<?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 functions unit tests
*
* @package core_comment
* @category external
* @copyright 2015 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.9
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* External comment functions unit tests
*
* @package core_comment
* @category external
* @copyright 2015 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.9
*/
class core_comment_externallib_testcase extends externallib_advanced_testcase {
/**
* Tests set up
*/
protected function setUp() {
global $CFG;
require_once($CFG->dirroot . '/comment/lib.php');
}
/**
* Test get_comments
*/
public function test_get_comments() {
global $DB, $CFG;
$this->resetAfterTest(true);
$CFG->usecomments = true;
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course(array('enablecomment' => 1));
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
$record = new stdClass();
$record->course = $course->id;
$record->name = "Mod data test";
$record->intro = "Some intro of some sort";
$record->comments = 1;
$module = $this->getDataGenerator()->create_module('data', $record);
$field = data_get_field_new('text', $module);
$fielddetail = new stdClass();
$fielddetail->name = 'Name';
$fielddetail->description = 'Some name';
$field->define_field($fielddetail);
$field->insert_field();
$recordid = data_add_record($module);
$datacontent = array();
$datacontent['fieldid'] = $field->field->id;
$datacontent['recordid'] = $recordid;
$datacontent['content'] = 'Asterix';
$contentid = $DB->insert_record('data_content', $datacontent);
$cm = get_coursemodule_from_instance('data', $module->id, $course->id);
$context = context_module::instance($module->cmid);
$this->setUser($user);
// We need to add the comments manually, the comment API uses the global OUTPUT and this is going to make the WS to fail.
$newcmt = new stdClass;
$newcmt->contextid = $context->id;
$newcmt->commentarea = 'database_entry';
$newcmt->itemid = $recordid;
$newcmt->content = 'New comment';
$newcmt->format = 0;
$newcmt->userid = $user->id;
$newcmt->timecreated = time();
$cmtid1 = $DB->insert_record('comments', $newcmt);
$newcmt->content = 'New comment 2';
$cmtid2 = $DB->insert_record('comments', $newcmt);
$contextlevel = 'module';
$instanceid = $cm->id;
$component = 'mod_data';
$itemid = $recordid;
$area = 'database_entry';
$page = 0;
$result = core_comment_external::get_comments($contextlevel, $instanceid, $component, $itemid, $area, $page);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(
core_comment_external::get_comments_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertCount(2, $result['comments']);
$this->assertEquals($user->id, $result['comments'][0]['userid']);
$this->assertEquals($user->id, $result['comments'][1]['userid']);
$this->assertEquals($cmtid1, $result['comments'][0]['id']);
$this->assertEquals($cmtid2, $result['comments'][1]['id']);
}
}
+1
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',
+12 -1
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(
@@ -1023,7 +1033,8 @@ $services = array(
'core_user_remove_user_device',
'core_course_get_courses',
'core_completion_update_activity_completion_status_manually',
'mod_data_get_databases_by_courses'
'mod_data_get_databases_by_courses',
'core_comment_get_comments',
),
'enabled' => 0,
'restrictedusers' => 0,
+3
View File
@@ -135,6 +135,9 @@
<testsuite name="core_completion_testsuite">
<directory suffix="_test.php">completion/tests</directory>
</testsuite>
<testsuite name="core_comment_testsuite">
<directory suffix="_test.php">comment/tests</directory>
</testsuite>
<!--Plugin suites: use admin/tool/phpunit/cli/util.php to build phpunit.xml from phpunit.xml.dist with up-to-date list of plugins in current install-->
<!--@plugin_suites_start@-->
+1 -1
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.