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
 * Task: update_meetings
19
 *
20
 * @package    mod_zoom
21
 * @copyright  2018 UC Regents
22
 * @author     Rohan Khajuria
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->libdir . '/modinfolib.php');
31
require_once($CFG->dirroot . '/mod/zoom/lib.php');
32
require_once($CFG->dirroot . '/mod/zoom/locallib.php');
33
 
34
use core\task\scheduled_task;
35
use mod_zoom\not_found_exception;
36
use moodle_exception;
37
use moodle_url;
38
 
39
/**
40
 * Scheduled task to sychronize meeting data.
41
 */
42
class update_meetings extends scheduled_task {
43
    /**
44
     * Returns name of task.
45
     *
46
     * @return string
47
     */
48
    public function get_name() {
49
        return get_string('updatemeetings', 'mod_zoom');
50
    }
51
 
52
    /**
53
     * Updates meetings that are not expired.
54
     *
55
     * @return boolean
56
     */
57
    public function execute() {
58
        global $DB;
59
 
60
        try {
61
            $service = zoom_webservice();
62
        } catch (moodle_exception $exception) {
63
            mtrace('Skipping task - ', $exception->getMessage());
64
            return;
65
        }
66
 
67
        // Show trace message.
68
        mtrace('Starting to process existing Zoom meeting activities ...');
69
 
70
        // Check all meetings, in case they were deleted/changed on Zoom.
71
        $zoomstoupdate = $DB->get_records('zoom', ['exists_on_zoom' => ZOOM_MEETING_EXISTS]);
72
        $courseidstoupdate = [];
73
        $calendarfields = ['intro', 'introformat', 'start_time', 'duration', 'recurring'];
74
 
75
        foreach ($zoomstoupdate as $zoom) {
76
            // Show trace message.
77
            mtrace('Processing next Zoom meeting activity ...');
78
            mtrace('  Zoom meeting ID: ' . $zoom->meeting_id);
79
            mtrace('  Zoom meeting title: ' . $zoom->name);
80
            $zoomactivityurl = new moodle_url('/mod/zoom/view.php', ['n' => $zoom->id]);
81
            mtrace('  Zoom meeting activity URL: ' . $zoomactivityurl->out());
82
            mtrace('  Moodle course ID: ' . $zoom->course);
83
 
84
            $gotinfo = false;
85
            try {
86
                $response = $service->get_meeting_webinar_info($zoom->meeting_id, $zoom->webinar);
87
                $gotinfo = true;
88
            } catch (not_found_exception $error) {
89
                $zoom->exists_on_zoom = ZOOM_MEETING_EXPIRED;
90
                $DB->update_record('zoom', $zoom);
91
 
92
                // Show trace message.
93
                mtrace('  => Marked Zoom meeting activity for Zoom meeting ID ' . $zoom->meeting_id .
94
                        ' as not existing anymore on Zoom');
95
            } catch (moodle_exception $error) {
96
                // Show trace message.
97
                mtrace('  !! Error updating Zoom meeting activity for Zoom meeting ID ' . $zoom->meeting_id . ': ' . $error);
98
            }
99
 
100
            if ($gotinfo) {
101
                $changed = false;
102
                $newzoom = populate_zoom_from_response($zoom, $response);
103
 
104
                // Iterate over all Zoom meeting fields.
105
                foreach ((array) $zoom as $field => $value) {
106
                    // The start_url has a parameter that always changes, so it doesn't really count as a change.
107
                    // Similarly, the timemodified parameter does not count as change if nothing else has changed.
108
                    if ($field === 'start_url' || $field === 'timemodified') {
109
                        continue;
110
                    }
111
 
112
                    // For doing a better comparison and for easing mtrace() output, convert booleans from the Zoom response
113
                    // to strings like they are stored in the Moodle database for the existing activity.
114
                    $newfieldvalue = $newzoom->$field;
115
                    if (is_bool($newfieldvalue)) {
116
                        $newfieldvalue = $newfieldvalue ? '1' : '0';
117
                    }
118
 
119
                    // If the field value has changed.
120
                    if ($newfieldvalue != $value) {
121
                        // Show trace message.
122
                        mtrace('  => Field "' . $field . '" has changed from "' . $value . '" to "' . $newfieldvalue . '"');
123
 
124
                        // Remember this meeting as changed.
125
                        $changed = true;
126
                    }
127
                }
128
 
129
                if ($changed) {
130
                    $newzoom->timemodified = time();
131
                    $DB->update_record('zoom', $newzoom);
132
 
133
                    // Show trace message.
134
                    mtrace('  => Updated Zoom meeting activity for Zoom meeting ID ' . $zoom->meeting_id);
135
 
136
                    // If the topic/title was changed, mark this course for cache clearing.
137
                    if ($zoom->name != $newzoom->name) {
138
                        $courseidstoupdate[] = $newzoom->course;
139
                    }
140
                } else {
141
                    // Show trace message.
142
                    mtrace('  => Skipped Zoom meeting activity for Zoom meeting ID ' . $zoom->meeting_id . ' as unchanged');
143
                }
144
 
145
                // Update the calendar events.
146
                if (!$zoom->recurring && $changed) {
147
                    // Check if calendar needs updating.
148
                    foreach ($calendarfields as $field) {
149
                        if ($zoom->$field != $newzoom->$field) {
150
                            zoom_calendar_item_update($newzoom);
151
 
152
                            // Show trace message.
153
                            mtrace('  => Updated calendar item for Zoom meeting ID ' . $zoom->meeting_id);
154
 
155
                            break;
156
                        }
157
                    }
158
                } else if ($zoom->recurring) {
159
                    // Show trace message.
160
                    mtrace('  => Updated calendar items for recurring Zoom meeting ID ' . $zoom->meeting_id);
161
                    zoom_calendar_item_update($newzoom);
162
                }
163
 
164
                // Update tracking fields for meeting.
165
                mtrace('  => Updated tracking fields for Zoom meeting ID ' . $zoom->meeting_id);
166
                zoom_sync_meeting_tracking_fields($zoom->id, $response->tracking_fields ?? []);
167
            }
168
        }
169
 
170
        // Show trace message.
171
        mtrace('Finished to process existing Zoom meetings');
172
 
173
        // Show trace message.
174
        mtrace('Starting to rebuild course caches ...');
175
 
176
        // Clear caches for meetings whose topic/title changed (and rebuild as needed).
177
        foreach ($courseidstoupdate as $courseid) {
178
            rebuild_course_cache($courseid, true);
179
        }
180
 
181
        // Show trace message.
182
        mtrace('Finished to rebuild course caches');
183
 
184
        return true;
185
    }
186
}