Skip to content

Commit

Permalink
Merge pull request #615 from ncstate-delta/enhancement/moodle-filters…
Browse files Browse the repository at this point in the history
…-adaption

Add moodle filter adaption to the meeting topic and description
  • Loading branch information
jrchamp authored Sep 27, 2024
2 parents fab6dd0 + 0ca435c commit 8136026
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
4 changes: 3 additions & 1 deletion backup/moodle2/restore_zoom_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 22 additions & 7 deletions classes/webservice.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/mod/zoom/lib.php');
require_once($CFG->dirroot . '/mod/zoom/locallib.php');
require_once($CFG->libdir . '/filelib.php');

Expand Down Expand Up @@ -585,20 +586,32 @@ 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;

$options = [];
if (!empty($cmid)) {
$options['context'] = \context_module::instance($cmid);
}

$data = [
'topic' => $zoom->name,
// Process the meeting topic with proper filter.
'topic' => zoom_apply_filter_on_meeting_name($zoom->name, $options),
'settings' => [
'host_video' => (bool) ($zoom->option_host_video),
'audio' => $zoom->option_audio,
],
];
if (isset($zoom->intro)) {
$data['agenda'] = content_to_text($zoom->intro, FORMAT_MOODLE);
// Process the description text with proper filter and then convert to plain text.
$data['agenda'] = substr(content_to_text(format_text(
$zoom->intro,
FORMAT_MOODLE,
$options
), false), 0, 2000);
}

if (isset($CFG->timezone) && !empty($CFG->timezone)) {
Expand Down Expand Up @@ -765,9 +778,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);

Expand All @@ -776,22 +790,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');
}

/**
Expand Down
26 changes: 20 additions & 6 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, $zoom->coursemodule);
$zoom = populate_zoom_from_response($zoom, $response);
$zoom->timemodified = time();
if (!empty($zoom->schedule_for)) {
Expand Down Expand Up @@ -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, $zoom->coursemodule);
if (!empty($zoom->schedule_for)) {
// Only update this if we actually get a valid user.
if ($correcthostzoomuser = zoom_get_user($zoom->schedule_for)) {
Expand Down Expand Up @@ -290,7 +290,8 @@ function populate_zoom_from_response(stdClass $zoom, stdClass $response) {
}

$newzoom->meeting_id = $response->id;
$newzoom->name = $response->topic;
// Need to get the meeting name from API call for comparison in the refresh_events function.
$newzoom->apiresponsename = $response->topic;
if (isset($response->start_time)) {
$newzoom->start_time = strtotime($response->start_time);
}
Expand Down Expand Up @@ -423,9 +424,11 @@ function zoom_refresh_events($courseid, $zoom, $cm) {
$fullzoom = populate_zoom_from_response($zoom, $response);

// Only if the name has changed, update meeting on Zoom.
if ($zoom->name !== $fullzoom->name) {
$fullzoom->name = $zoom->name;
zoom_webservice()->update_meeting($zoom);
// Before comparing, need to apply filter on the name if applicable.
$options = [];
$options['context'] = \context_module::instance($cm->id);
if (zoom_apply_filter_on_meeting_name($zoom->name, $options) !== $fullzoom->apiresponsename) {
zoom_webservice()->update_meeting($zoom, $cm->id);
}

zoom_calendar_item_update($fullzoom);
Expand Down Expand Up @@ -1338,3 +1341,14 @@ function zoom_cm_info_dynamic(cm_info $cm) {
}
}
}

/**
* Apply filter(s) on Zoom activity name if applicable.
*
* @param string $name Original name to be processed
* @param array $options Format_string options
* @return string Filtered name
*/
function zoom_apply_filter_on_meeting_name($name, $options) {
return substr(format_string($name, true, $options + ['escape' => false]), 0, 200);
}
2 changes: 1 addition & 1 deletion recreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 8136026

Please sign in to comment.