Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace mod_chat\external;
18
 
19
use core_external\external_api;
20
use core_external\external_function_parameters;
21
use core_external\external_single_structure;
22
use core_external\external_value;
23
use core_external\external_warnings;
24
 
25
/**
26
 * External service to log viewed previous chat sessions.
27
 *
28
 * @package   mod_chat
29
 * @category  external
30
 * @copyright 2023 Rodrigo Mady <rodrigo.mady@moodle.com>
31
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 * @since     Moodle 4.3
33
 */
34
class view_sessions extends external_api {
35
    /**
36
     * Returns description of method parameters
37
     *
38
     * @return external_function_parameters
39
     */
40
    public static function execute_parameters(): external_function_parameters {
41
        return new external_function_parameters([
42
            'cmid'   => new external_value(PARAM_INT, 'Course module id', VALUE_REQUIRED),
43
            'start'  => new external_value(PARAM_INT, 'Session start time', VALUE_DEFAULT, 0),
44
            'end'    => new external_value(PARAM_INT, 'Session end time', VALUE_DEFAULT, 0),
45
        ]);
46
    }
47
 
48
    /**
49
     * Execute the chat view sessions event.
50
     *
51
     * @param int $cmid the chat course module id
52
     * @param null|int $start
53
     * @param null|int $end
54
     * @return array
55
     * @throws \restricted_context_exception
56
     */
57
    public static function execute(int $cmid, ?int $start = 0, ?int $end = 0): array {
58
        global $DB;
59
        $warnings = [];
60
        $status   = false;
61
        // Validate the cmid ID.
62
        [
63
            'cmid'  => $cmid,
64
            'start' => $start,
65
            'end'   => $end,
66
        ] = self::validate_parameters(self::execute_parameters(), [
67
            'cmid'  => $cmid,
68
            'start' => $start,
69
            'end'   => $end,
70
        ]);
71
        if (!$cm = get_coursemodule_from_id('chat', $cmid)) {
72
            throw new \moodle_exception('invalidcoursemodule', 'error');
73
        }
74
        if (!$chat = $DB->get_record('chat', ['id' => $cm->instance])) {
75
            throw new \moodle_exception('invalidcoursemodule', 'error');
76
        }
77
 
78
        $context = \context_module::instance($cm->id);
79
        self::validate_context($context);
80
 
81
        // Check capability.
82
        if (has_capability('mod/chat:readlog', $context)) {
83
            $params = [
84
                'context'  => $context,
85
                'objectid' => $chat->id,
86
                'other'    => [
87
                    'start' => $start,
88
                    'end'   => $end
89
                ]
90
            ];
91
            $event  = \mod_chat\event\sessions_viewed::create($params);
92
            $status = true;
93
            $event->add_record_snapshot('chat', $chat);
94
            $event->trigger();
95
        } else {
96
            $warnings[] = [
97
                'item'        => $cm->id,
98
                'warningcode' => 'nopermissiontoseethechatlog',
99
                'message'     => get_string('nopermissiontoseethechatlog', 'chat')
100
            ];
101
        }
102
 
103
        $result = [
104
            'status'   => $status,
105
            'warnings' => $warnings
106
        ];
107
        return $result;
108
    }
109
 
110
    /**
111
     * Describe the return structure of the external service.
112
     *
113
     * @return external_single_structure
114
     */
115
    public static function execute_returns(): external_single_structure {
116
        return new external_single_structure([
117
            'status'   => new external_value(PARAM_BOOL, 'status: true if success'),
118
            'warnings' => new external_warnings()
119
        ]);
120
    }
121
}