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 the Zoom plugin for 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
/**
18
 * The task for getting recordings from Zoom to Moodle.
19
 *
20
 * @package    mod_zoom
21
 * @author     Jwalit Shah <jwalitshah@catalyst-au.net>
22
 * @copyright  2021 Jwalit Shah <jwalitshah@catalyst-au.net>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace mod_zoom\task;
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
require_once($CFG->dirroot . '/mod/zoom/locallib.php');
31
 
32
use core\task\scheduled_task;
33
use moodle_exception;
34
use stdClass;
35
 
36
/**
37
 * Scheduled task to get the meeting recordings.
38
 */
39
class get_meeting_recordings extends scheduled_task {
40
    /**
41
     * Returns name of task.
42
     *
43
     * @return string
44
     */
45
    public function get_name() {
46
        return get_string('getmeetingrecordings', 'mod_zoom');
47
    }
48
 
49
    /**
50
     * Get any new recordings that have been added on zoom.
51
     *
52
     * @return void
53
     */
54
    public function execute() {
55
        global $DB;
56
 
57
        try {
58
            $service = zoom_webservice();
59
        } catch (moodle_exception $exception) {
60
            mtrace('Skipping task - ', $exception->getMessage());
61
            return;
62
        }
63
 
64
        $config = get_config('zoom');
65
        if (empty($config->viewrecordings)) {
66
            mtrace('Skipping task - ', get_string('zoomerr_viewrecordings_off', 'zoom'));
67
            return;
68
        }
69
 
70
        // See if we cannot make anymore API calls.
71
        $retryafter = get_config('zoom', 'retry-after');
72
        if (!empty($retryafter) && time() < $retryafter) {
73
            mtrace('Out of API calls, retry after ' . userdate($retryafter, get_string('strftimedaydatetime', 'core_langconfig')));
74
            return;
75
        }
76
 
77
        mtrace('Finding meeting recordings for this account...');
78
 
79
        $localmeetings = zoom_get_all_meeting_records();
80
 
81
        $now = time();
82
        $from = gmdate('Y-m-d', strtotime('-1 day', $now));
83
        $to = gmdate('Y-m-d', strtotime('+1 day', $now));
84
 
85
        $hostmeetings = [];
86
 
87
        foreach ($localmeetings as $zoom) {
88
            // Only get recordings for this meeting if its recurring or already finished.
89
            if ($zoom->recurring || $now > (intval($zoom->start_time) + intval($zoom->duration))) {
90
                $hostmeetings[$zoom->host_id][$zoom->meeting_id] = $zoom;
91
            }
92
        }
93
 
94
        if (empty($hostmeetings)) {
95
            mtrace('No meetings need to be processed.');
96
            return;
97
        }
98
 
99
        $meetingpasscodes = [];
100
        $localrecordings = zoom_get_meeting_recordings_grouped();
101
 
102
        foreach ($hostmeetings as $hostid => $meetings) {
103
            // Fetch all recordings for this user.
104
            $zoomrecordings = $service->get_user_recordings($hostid, $from, $to);
105
 
106
            foreach ($zoomrecordings as $recordingid => $recording) {
107
                if (isset($localrecordings[$recording->meetinguuid][$recordingid])) {
108
                    mtrace('Recording id: ' . $recordingid . ' exists...skipping');
109
                    $localrecording = $localrecordings[$recording->meetinguuid][$recordingid];
110
 
111
                    if ($localrecording->recordingtype !== $recording->recordingtype) {
112
                        $updatemeeting = (object) [
113
                            'id' => $localrecording->id,
114
                            'recordingtype' => $recording->recordingtype,
115
                        ];
116
                        $DB->update_record('zoom_meeting_recordings', $updatemeeting);
117
                    }
118
                    continue;
119
                }
120
 
121
                if (empty($meetings[$recording->meetingid])) {
122
                    // Skip meetings that are not in Moodle.
123
                    mtrace('Meeting id: ' . $recording->meetingid . ' does not exist...skipping');
124
                    continue;
125
                }
126
 
127
                // As of 2023-09-24, 'password' is not present in the user recordings API response.
128
                if (empty($meetingpasscodes[$recording->meetinguuid])) {
129
                    try {
130
                        $settings = $service->get_recording_settings($recording->meetinguuid);
131
                        $meetingpasscodes[$recording->meetinguuid] = $settings->password;
132
                    } catch (moodle_exception $error) {
133
                        continue;
134
                    }
135
                }
136
 
137
                $zoom = $meetings[$recording->meetingid];
138
                $recordingtype = $recording->recordingtype;
139
 
140
                $record = new stdClass();
141
                $record->zoomid = $zoom->id;
142
                $record->meetinguuid = $recording->meetinguuid;
143
                $record->zoomrecordingid = $recordingid;
144
                $record->name = $zoom->name;
145
                $record->externalurl = $recording->url;
146
                $record->passcode = $meetingpasscodes[$recording->meetinguuid];
147
                $record->recordingtype = $recordingtype;
148
                $record->recordingstart = $recording->recordingstart;
149
                $record->showrecording = $zoom->recordings_visible_default;
150
                $record->timecreated = $now;
151
                $record->timemodified = $now;
152
 
153
                $record->id = $DB->insert_record('zoom_meeting_recordings', $record);
154
                mtrace('Recording id: ' . $recordingid . ' (' . $recordingtype . ') added to the database');
155
            }
156
        }
157
    }
158
}