MDL-74568 mod_chat: Add Webservice mod_chat_view_sessions
This commit is contained in:
committed by
Rodrigo Mady
parent
4ed782dd03
commit
407856338c
+121
@@ -0,0 +1,121 @@
|
||||
<?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/>.
|
||||
|
||||
namespace mod_chat\external;
|
||||
|
||||
use core_external\external_api;
|
||||
use core_external\external_function_parameters;
|
||||
use core_external\external_single_structure;
|
||||
use core_external\external_value;
|
||||
use core_external\external_warnings;
|
||||
|
||||
/**
|
||||
* External service to log viewed previous chat sessions.
|
||||
*
|
||||
* @package mod_chat
|
||||
* @category external
|
||||
* @copyright 2023 Rodrigo Mady <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 4.3
|
||||
*/
|
||||
class view_sessions extends external_api {
|
||||
/**
|
||||
* Returns description of method parameters
|
||||
*
|
||||
* @return external_function_parameters
|
||||
*/
|
||||
public static function execute_parameters(): external_function_parameters {
|
||||
return new external_function_parameters([
|
||||
'cmid' => new external_value(PARAM_INT, 'Course module id', VALUE_REQUIRED),
|
||||
'start' => new external_value(PARAM_INT, 'Session start time', VALUE_DEFAULT, 0),
|
||||
'end' => new external_value(PARAM_INT, 'Session end time', VALUE_DEFAULT, 0),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the chat view sessions event.
|
||||
*
|
||||
* @param int $cmid the chat course module id
|
||||
* @param null|int $start
|
||||
* @param null|int $end
|
||||
* @return array
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public static function execute(int $cmid, ?int $start = 0, ?int $end = 0): array {
|
||||
global $DB;
|
||||
$warnings = [];
|
||||
$status = false;
|
||||
// Validate the cmid ID.
|
||||
[
|
||||
'cmid' => $cmid,
|
||||
'start' => $start,
|
||||
'end' => $end,
|
||||
] = self::validate_parameters(self::execute_parameters(), [
|
||||
'cmid' => $cmid,
|
||||
'start' => $start,
|
||||
'end' => $end,
|
||||
]);
|
||||
if (!$cm = get_coursemodule_from_id('chat', $cmid)) {
|
||||
throw new \moodle_exception('invalidcoursemodule', 'error');
|
||||
}
|
||||
if (!$chat = $DB->get_record('chat', ['id' => $cm->instance])) {
|
||||
throw new \moodle_exception('invalidcoursemodule', 'error');
|
||||
}
|
||||
|
||||
$context = \context_module::instance($cm->id);
|
||||
self::validate_context($context);
|
||||
|
||||
// Check capability.
|
||||
if (has_capability('mod/chat:readlog', $context)) {
|
||||
$params = [
|
||||
'context' => $context,
|
||||
'objectid' => $chat->id,
|
||||
'other' => [
|
||||
'start' => $start,
|
||||
'end' => $end
|
||||
]
|
||||
];
|
||||
$event = \mod_chat\event\sessions_viewed::create($params);
|
||||
$status = true;
|
||||
$event->add_record_snapshot('chat', $chat);
|
||||
$event->trigger();
|
||||
} else {
|
||||
$warnings[] = [
|
||||
'item' => $cm->id,
|
||||
'warningcode' => 'nopermissiontoseethechatlog',
|
||||
'message' => get_string('nopermissiontoseethechatlog', 'chat')
|
||||
];
|
||||
}
|
||||
|
||||
$result = [
|
||||
'status' => $status,
|
||||
'warnings' => $warnings
|
||||
];
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Describe the return structure of the external service.
|
||||
*
|
||||
* @return external_single_structure
|
||||
*/
|
||||
public static function execute_returns(): external_single_structure {
|
||||
return new external_single_structure([
|
||||
'status' => new external_value(PARAM_BOOL, 'status: true if success'),
|
||||
'warnings' => new external_warnings()
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -96,4 +96,13 @@ $functions = array(
|
||||
'type' => 'read',
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
|
||||
),
|
||||
|
||||
'mod_chat_view_sessions' => [
|
||||
'classname' => 'mod_chat\external\view_sessions',
|
||||
'methodname' => 'execute',
|
||||
'description' => 'Trigger the chat session viewed event.',
|
||||
'type' => 'write',
|
||||
'capabilities' => 'mod/chat:readlog',
|
||||
'services' => [MOODLE_OFFICIAL_MOBILE_SERVICE]
|
||||
],
|
||||
);
|
||||
|
||||
+133
@@ -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/>.
|
||||
|
||||
/**
|
||||
* Tests for external function mod_chat_view_sessions.
|
||||
*
|
||||
* @package mod_chat
|
||||
* @category external
|
||||
* @copyright 2022 Rodrigo Mady <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 4.3
|
||||
*/
|
||||
|
||||
namespace mod_chat\external;
|
||||
|
||||
use externallib_advanced_testcase;
|
||||
use moodle_exception;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
||||
|
||||
/**
|
||||
* Test Class for external function mod_chat_view_sessions.
|
||||
*
|
||||
* @package mod_chat
|
||||
* @category external
|
||||
* @copyright 2023 Rodrigo Mady <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 4.3
|
||||
* @coversDefaultClass \mod_chat\external\view_sessions
|
||||
*/
|
||||
class view_sessions_test extends externallib_advanced_testcase {
|
||||
|
||||
/**
|
||||
* Prepare the test.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function prepare_test_data(): array {
|
||||
global $DB;
|
||||
$this->resetAfterTest(true);
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$student1 = $this->getDataGenerator()->create_and_enrol($course);
|
||||
$chat = $this->getDataGenerator()->create_module('chat', ['course' => $course->id]);
|
||||
$context = \context_module::instance($chat->cmid);
|
||||
$studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student']);
|
||||
assign_capability('mod/chat:readlog', CAP_ALLOW, $studentroleid, $context, true);
|
||||
$this->setUser($student1);
|
||||
|
||||
return [
|
||||
'chat' => $chat,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to call view_sessions WS function.
|
||||
*
|
||||
* @param int $cmid
|
||||
* @param int $sessionstart
|
||||
* @param int $sessionend
|
||||
* @return array
|
||||
*/
|
||||
protected function view_sessions(int $cmid, int $sessionstart = 0, int $sessionend = 0): array {
|
||||
$result = view_sessions::execute($cmid, $sessionstart, $sessionend);
|
||||
return \core_external\external_api::clean_returnvalue(view_sessions::execute_returns(), $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for webservice view sessions.
|
||||
* @covers ::execute
|
||||
*/
|
||||
public function test_view_sessions(): void {
|
||||
$data = $this->prepare_test_data();
|
||||
$result = $this->view_sessions($data['chat']->cmid);
|
||||
$this->assertArrayHasKey('status', $result);
|
||||
$this->assertArrayHasKey('warnings', $result);
|
||||
$this->assertTrue($result['status']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for webservice view sessions without capability.
|
||||
* @covers ::execute
|
||||
*/
|
||||
public function test_view_sessions_without_capability(): void {
|
||||
global $DB;
|
||||
$data = $this->prepare_test_data();
|
||||
$context = \context_module::instance($data['chat']->cmid);
|
||||
$studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student']);
|
||||
assign_capability('mod/chat:readlog', CAP_PROHIBIT, $studentroleid, $context, true);
|
||||
$result = $this->view_sessions($data['chat']->cmid);
|
||||
$this->assertArrayHasKey('status', $result);
|
||||
$this->assertArrayHasKey('warnings', $result);
|
||||
$this->assertFalse($result['status']);
|
||||
$this->assertEquals(get_string('nopermissiontoseethechatlog', 'chat'), $result['warnings'][0]['message']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for webservice view sessions with start and end dates.
|
||||
* @covers ::execute
|
||||
*/
|
||||
public function test_view_sessions_with_start_end_dates(): void {
|
||||
$data = $this->prepare_test_data();
|
||||
$result = $this->view_sessions($data['chat']->cmid, strtotime('today'), strtotime('tomorrow'));
|
||||
$this->assertArrayHasKey('status', $result);
|
||||
$this->assertArrayHasKey('warnings', $result);
|
||||
$this->assertTrue($result['status']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test execute with no valid instance of cmid.
|
||||
* @covers ::execute
|
||||
*/
|
||||
public function test_view_sessions_no_instance(): void {
|
||||
$this->expectException(moodle_exception::class);
|
||||
$this->view_sessions(1234);
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,6 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$plugin->version = 2023042400; // The current module version (Date: YYYYMMDDXX).
|
||||
$plugin->version = 2023042401; // The current module version (Date: YYYYMMDDXX).
|
||||
$plugin->requires = 2023041800; // Requires this Moodle version.
|
||||
$plugin->component = 'mod_chat'; // Full name of the plugin (used for diagnostics).
|
||||
|
||||
Reference in New Issue
Block a user