Merge branch 'MDL-68853-master' of git://github.com/ilyatregubov/moodle

This commit is contained in:
Andrew Nicols 2021-02-17 11:47:27 +08:00
commit 57e233e742
4 changed files with 270 additions and 1 deletions

View File

@ -0,0 +1,133 @@
<?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/>.
/**
* This is the external method for logging that the h5pactivity was viewed.
*
* @package mod_h5pactivity
* @since Moodle 3.11
* @copyright 2021 Ilya Tregubov <ilya@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_h5pactivity\external;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/externallib.php');
use mod_h5pactivity\local\manager;
use mod_h5pactivity\event\report_viewed;
use external_api;
use external_function_parameters;
use external_value;
use external_single_structure;
use external_warnings;
use moodle_exception;
use context_module;
use stdClass;
/**
* This is the external method for logging that the h5pactivity was viewed.
*
* @copyright 2021 Ilya Tregubov <ilya@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class log_report_viewed extends external_api {
/**
* Webservice parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters(
[
'h5pactivityid' => new external_value(PARAM_INT, 'h5p activity instance id'),
'userid' => new external_value(PARAM_INT,
'The user id to log attempt (null means only current user)', VALUE_DEFAULT),
'attemptid' => new external_value(PARAM_INT, 'The attempt id', VALUE_DEFAULT),
]
);
}
/**
* Logs that the h5pactivity was viewed.
*
* @throws moodle_exception if the user cannot see the report
* @param int $h5pactivityid The h5p activity id
* @param int|null $userid The user id
* @param int|null $attemptid The attempt id
* @return array of warnings and status result
*/
public static function execute(int $h5pactivityid, int $userid = null, int $attemptid = null): stdClass {
$params = external_api::validate_parameters(self::execute_parameters(), [
'h5pactivityid' => $h5pactivityid,
'userid' => $userid,
'attemptid' => $attemptid,
]);
$h5pactivityid = $params['h5pactivityid'];
$userid = $params['userid'];
$attemptid = $params['attemptid'];
// Request and permission validation.
list ($course, $cm) = get_course_and_cm_from_instance($h5pactivityid, 'h5pactivity');
$context = context_module::instance($cm->id);
self::validate_context($context);
$manager = manager::create_from_coursemodule($cm);
$instance = $manager->get_instance();
// Trigger event.
$other = [
'instanceid' => $instance->id,
'userid' => $userid,
'attemptid' => $attemptid,
];
$event = report_viewed::create([
'objectid' => $instance->id,
'context' => $context,
'other' => $other,
]);
$event->trigger();
$result = (object)[
'status' => true,
'warnings' => [],
];
return $result;
}
/**
* Describes the report_viewed return value.
*
* @return external_single_structure
* @since Moodle 3.11
*/
public static function execute_returns() {
return new external_single_structure(
[
'status' => new external_value(PARAM_BOOL, 'status: true if success'),
'warnings' => new external_warnings()
]
);
}
}

View File

@ -73,4 +73,12 @@ $functions = [
'capabilities' => 'mod/h5pactivity:view',
'services' => [MOODLE_OFFICIAL_MOBILE_SERVICE],
],
'mod_h5pactivity_log_report_viewed' => [
'classname' => 'mod_h5pactivity\external\log_report_viewed',
'methodname' => 'execute',
'classpath' => '',
'description' => 'Log that the h5pactivity was viewed.',
'type' => 'write',
'services' => [MOODLE_OFFICIAL_MOBILE_SERVICE],
],
];

View File

@ -0,0 +1,128 @@
<?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 function test for log_report_viewed.
*
* @package mod_h5pactivity
* @category external
* @since Moodle 3.11
* @copyright 2021 Ilya Tregubov <ilya@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_h5pactivity\external;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
use mod_h5pactivity\local\manager;
use external_api;
use externallib_advanced_testcase;
/**
* External function test for log_report_viewed.
*
* @package mod_h5pactivity
* @copyright 2021 Ilya Tregubov <ilya@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class log_report_viewed_testcase extends externallib_advanced_testcase {
/**
* Test the behaviour of log_report_viewed.
*
* @dataProvider execute_data
* @param int $enabletracking the activity tracking enable
* @param int $reviewmode the activity review mode
* @param string $loginuser the user which calls the webservice
* @param string|null $participant the user to log the data
*/
public function test_execute(int $enabletracking, int $reviewmode, string $loginuser,
?string $participant): void {
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Enrol users: 1 teacher, 1 student.
$users = [
'editingteacher' => $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'),
'student' => $this->getDataGenerator()->create_and_enrol($course, 'student'),
];
// Add h5p activity.
$activity = $this->getDataGenerator()->create_module('h5pactivity',
['course' => $course, 'enabletracking' => $enabletracking, 'reviewmode' => $reviewmode]);
// Create attempt for h5p activity.
$attempts = [];
$generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity');
$user = $users['student'];
$manager = manager::create_from_instance($activity);
$cm = $manager->get_coursemodule();
$params = ['cmid' => $cm->id, 'userid' => $user->id];
$attempts['student'] = $generator->create_content($activity, $params);
// Redirect events to the sink, so we can recover them later.
$sink = $this->redirectEvents();
// Execute external method.
$this->setUser($users[$loginuser]);
$attemptid = $attempts[$participant]->id ?? 0;
$result = log_report_viewed::execute($activity->id, $user->id, $attemptid);
$result = external_api::clean_returnvalue(
log_report_viewed::execute_returns(),
$result
);
// Validate general structure.
$this->assertArrayHasKey('status', $result);
$events = $sink->get_events();
$event = end($events);
// Check the event details are correct.
$this->assertInstanceOf('mod_h5pactivity\event\report_viewed', $event);
$this->assertEquals(\context_module::instance($cm->id), $event->get_context());
$this->assertEquals($cm->instance, $event->other['instanceid']);
$this->assertEquals($user->id, $event->other['userid']);
$this->assertEquals($attemptid, $event->other['attemptid']);
}
/**
* Data provider for the test_execute tests.
*
* @return array
*/
public function execute_data(): array {
return [
'Student reviewing own attempt' => [
1, manager::REVIEWCOMPLETION, 'student', 'student'
],
'Teacher reviewing student attempts' => [
1, manager::REVIEWCOMPLETION, 'editingteacher', 'student'
],
];
}
}

View File

@ -25,5 +25,5 @@
defined('MOODLE_INTERNAL') || die();
$plugin->component = 'mod_h5pactivity';
$plugin->version = 2021052500;
$plugin->version = 2021052501;
$plugin->requires = 2021052500;