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

This commit is contained in:
David Monllao
2015-03-24 17:43:29 +08:00
4 changed files with 218 additions and 2 deletions
+117
View File
@@ -0,0 +1,117 @@
<?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/>.
/**
* Completion external API
*
* @package core_completion
* @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;
require_once("$CFG->libdir/externallib.php");
require_once("$CFG->libdir/completionlib.php");
/**
* Completion external functions
*
* @package core_completion
* @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_completion_external extends external_api {
/**
* Describes the parameters for update_activity_completion_status_manually.
*
* @return external_external_function_parameters
* @since Moodle 2.9
*/
public static function update_activity_completion_status_manually_parameters() {
return new external_function_parameters (
array(
'cmid' => new external_value(PARAM_INT, 'course module id'),
'completed' => new external_value(PARAM_BOOL, 'activity completed or not'),
)
);
}
/**
* Update completion status for the current user in an activity, only for activities with manual tracking.
* @param int $cmid Course module id
* @param bool $completed Activity completed or not
* @return array Result and possible warnings
* @since Moodle 2.9
* @throws moodle_exception
*/
public static function update_activity_completion_status_manually($cmid, $completed) {
// Validate and normalize parameters.
$params = self::validate_parameters(self::update_activity_completion_status_manually_parameters(),
array('cmid' => $cmid, 'completed' => $completed));
$cmid = $params['cmid'];
$completed = $params['completed'];
$warnings = array();
$context = context_module::instance($cmid);
self::validate_context($context);
list($course, $cm) = get_course_and_cm_from_cmid($cmid);
// Set up completion object and check it is enabled.
$completion = new completion_info($course);
if (!$completion->is_enabled()) {
throw new moodle_exception('completionnotenabled', 'completion');
}
// Check completion state is manual.
if ($cm->completion != COMPLETION_TRACKING_MANUAL) {
throw new moodle_exception('cannotmanualctrack', 'error');
}
$targetstate = ($completed) ? COMPLETION_COMPLETE : COMPLETION_INCOMPLETE;
$completion->update_state($cm, $targetstate);
$result = array();
$result['status'] = true;
$result['warnings'] = $warnings;
return $result;
}
/**
* Describes the update_activity_completion_status_manually return value.
*
* @return external_single_structure
* @since Moodle 2.9
*/
public static function update_activity_completion_status_manually_returns() {
return new external_single_structure(
array(
'status' => new external_value(PARAM_BOOL, 'status, true if success'),
'warnings' => new external_warnings(),
)
);
}
}
+90
View File
@@ -0,0 +1,90 @@
<?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 completion functions unit tests
*
* @package core_completion
* @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 completion functions unit tests
*
* @package core_completion
* @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_completion_externallib_testcase extends externallib_advanced_testcase {
/**
* Test update_activity_completion_status_manually
*/
public function test_update_activity_completion_status_manually() {
global $DB, $CFG;
$this->resetAfterTest(true);
$CFG->enablecompletion = true;
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
$data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
array('completion' => 1));
$cm = get_coursemodule_from_id('data', $data->cmid);
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
$this->setUser($user);
$result = core_completion_external::update_activity_completion_status_manually($data->cmid, true);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(
core_completion_external::update_activity_completion_status_manually_returns(), $result);
// Check in DB.
$this->assertEquals(1, $DB->get_field('course_modules_completion', 'completionstate',
array('coursemoduleid' => $data->cmid)));
// Check using the API.
$completion = new completion_info($course);
$completiondata = $completion->get_data($cm);
$this->assertEquals(1, $completiondata->completionstate);
$this->assertTrue($result['status']);
$result = core_completion_external::update_activity_completion_status_manually($data->cmid, false);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(
core_completion_external::update_activity_completion_status_manually_returns(), $result);
$this->assertEquals(0, $DB->get_field('course_modules_completion', 'completionstate',
array('coursemoduleid' => $data->cmid)));
$completiondata = $completion->get_data($cm);
$this->assertEquals(0, $completiondata->completionstate);
$this->assertTrue($result['status']);
}
}
+10 -1
View File
@@ -957,6 +957,14 @@ $functions = array(
'description' => 'Load a template for a renderable',
'type' => 'read'
),
// Completion related functions.
'core_completion_update_activity_completion_status_manually' => array(
'classname' => 'core_completion_external',
'methodname' => 'update_activity_completion_status_manually',
'description' => 'Update completion status for the current user in an activity, only for activities with manual tracking.',
'type' => 'write',
),
);
$services = array(
@@ -1013,7 +1021,8 @@ $services = array(
'gradereport_user_get_grades_table',
'core_group_get_course_user_groups',
'core_user_remove_user_device',
'core_course_get_courses'
'core_course_get_courses',
'core_completion_update_activity_completion_status_manually',
),
'enabled' => 0,
'restrictedusers' => 0,
+1 -1
View File
@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2015031900.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2015031900.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.