From bdcfec027b3de2e682b8371d87a75cbc2dab677f Mon Sep 17 00:00:00 2001 From: Jonathan Champ Date: Thu, 5 Sep 2024 12:17:40 -0400 Subject: [PATCH] filter: webservice needs context --- backup/moodle2/restore_zoom_stepslib.php | 4 +++- classes/webservice.php | 27 ++++++++++++------------ lib.php | 6 +++--- recreate.php | 2 +- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/backup/moodle2/restore_zoom_stepslib.php b/backup/moodle2/restore_zoom_stepslib.php index 3c5b3d0a..0bca6e3e 100755 --- a/backup/moodle2/restore_zoom_stepslib.php +++ b/backup/moodle2/restore_zoom_stepslib.php @@ -65,7 +65,9 @@ protected function process_zoom($data) { // Either create a new meeting or set meeting as expired. try { - $updateddata = zoom_webservice()->create_meeting($data); + // FIXME: Do we provide course context? That won't have the right activity names etc. + $cmid = null; + $updateddata = zoom_webservice()->create_meeting($data, $cmid); $data = populate_zoom_from_response($data, $updateddata); $data->exists_on_zoom = ZOOM_MEETING_EXISTS; } catch (moodle_exception $e) { diff --git a/classes/webservice.php b/classes/webservice.php index fa9aff70..3c0a8593 100644 --- a/classes/webservice.php +++ b/classes/webservice.php @@ -585,23 +585,20 @@ public function get_schedule_for_users($identifier) { * database fields to the appropriate API request fields. * * @param stdClass $zoom The zoom meeting to format. + * @param ?int $cmid The cmid if available. * @return array The formatted meetings for the meeting. */ - private function database_to_api($zoom) { + private function database_to_api($zoom, $cmid) { global $CFG; - if (isset($zoom->coursemodule)) { - $cmid = $zoom->coursemodule; - } else { - [$course, $cm] = get_course_and_cm_from_instance($zoom, 'zoom'); - $cmid = $cm->id; + $options = []; + if (!empty($cmid)) { + $options['context'] = \context_module::instance($cmid); } - $context = \context_module::instance($cmid); - $data = [ // Process the meeting topic with proper filter. - 'topic' => substr(format_string($zoom->name, true, ['context' => $context, 'escape' => false]), 0, 200), + 'topic' => substr(format_string($zoom->name, true, $options + ['escape' => false]), 0, 200), 'settings' => [ 'host_video' => (bool) ($zoom->option_host_video), 'audio' => $zoom->option_audio, @@ -612,7 +609,7 @@ private function database_to_api($zoom) { $data['agenda'] = substr(content_to_text(format_text( $zoom->intro, FORMAT_MOODLE, - ['context' => $context] + $options ), false), 0, 2000); } @@ -780,9 +777,10 @@ public function provide_license($zoomuserid) { * Take a $zoom object as returned from the Moodle form and respond with an object that can be saved to the database. * * @param stdClass $zoom The meeting to create. + * @param ?int $cmid The cmid if available. * @return stdClass The call response. */ - public function create_meeting($zoom) { + public function create_meeting($zoom, $cmid) { // Provide license if needed. $this->provide_license($zoom->host_id); @@ -791,22 +789,23 @@ public function create_meeting($zoom) { // Classic: webinar:write:admin. // Granular: webinar:write:webinar:admin. $url = "users/$zoom->host_id/" . (!empty($zoom->webinar) ? 'webinars' : 'meetings'); - return $this->make_call($url, $this->database_to_api($zoom), 'post'); + return $this->make_call($url, $this->database_to_api($zoom, $cmid), 'post'); } /** * Update a meeting/webinar on Zoom. * * @param stdClass $zoom The meeting to update. + * @param ?int $cmid The cmid if available. * @return void */ - public function update_meeting($zoom) { + public function update_meeting($zoom, $cmid) { // Classic: meeting:write:admin. // Granular: meeting:update:meeting:admin. // Classic: webinar:write:admin. // Granular: webinar:update:webinar:admin. $url = ($zoom->webinar ? 'webinars/' : 'meetings/') . $zoom->meeting_id; - $this->make_call($url, $this->database_to_api($zoom), 'patch'); + $this->make_call($url, $this->database_to_api($zoom, $cmid), 'patch'); } /** diff --git a/lib.php b/lib.php index e69d61d5..1559aa1f 100755 --- a/lib.php +++ b/lib.php @@ -99,7 +99,7 @@ function zoom_add_instance(stdClass $zoom, ?mod_zoom_mod_form $mform = null) { $zoom->breakoutrooms = $breakoutrooms['zoom']; } - $response = zoom_webservice()->create_meeting($zoom); + $response = zoom_webservice()->create_meeting($zoom, $mform->_cm->id); $zoom = populate_zoom_from_response($zoom, $response); $zoom->timemodified = time(); if (!empty($zoom->schedule_for)) { @@ -187,7 +187,7 @@ function zoom_update_instance(stdClass $zoom, ?mod_zoom_mod_form $mform = null) // Update meeting on Zoom. try { - zoom_webservice()->update_meeting($zoom); + zoom_webservice()->update_meeting($zoom, $mform->_cm->id); if (!empty($zoom->schedule_for)) { // Only update this if we actually get a valid user. if ($correcthostzoomuser = zoom_get_user($zoom->schedule_for)) { @@ -424,7 +424,7 @@ function zoom_refresh_events($courseid, $zoom, $cm) { // Only if the name has changed, update meeting on Zoom. if ($zoom->name !== $fullzoom->name) { $fullzoom->name = $zoom->name; - zoom_webservice()->update_meeting($zoom); + zoom_webservice()->update_meeting($zoom, $cm->id); } zoom_calendar_item_update($fullzoom); diff --git a/recreate.php b/recreate.php index 9b4e3e0d..0ea7dc17 100644 --- a/recreate.php +++ b/recreate.php @@ -49,7 +49,7 @@ } // Set the current zoom table entry to use the new meeting (meeting_id/etc). -$response = zoom_webservice()->create_meeting($zoom); +$response = zoom_webservice()->create_meeting($zoom, $cm->id); $zoom = populate_zoom_from_response($zoom, $response); $zoom->exists_on_zoom = ZOOM_MEETING_EXISTS; $zoom->timemodified = time();