Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup: lib.php and locallib.php #570

Open
4 tasks
jrchamp opened this issue Jan 29, 2024 · 1 comment
Open
4 tasks

Cleanup: lib.php and locallib.php #570

jrchamp opened this issue Jan 29, 2024 · 1 comment
Labels
bug Fixes problems or reduces technical debt help wanted We need your help to make this possible

Comments

@jrchamp
Copy link
Collaborator

jrchamp commented Jan 29, 2024

In 812974f, I removed some long-forgotten todo tags, so it might be good to review the code in light of the updated developer guide.

  • Check Moodle's list of functions that are expected to be in a plugin's lib.php. Are we missing any new ones? Bonus points for deciding if/why we want to implement them.
  • Determine what the unimplemented functions in mod/zoom/lib.php are supposed to do. Bonus points for deciding if/why we want to implement them.
  • Check the functions that are currently in mod/zoom/lib.php, but are internal/"local" to the Zoom plugin.
  • Move all functions from locallib.php and the misplaceed functions in lib.php to appropriately namespaced classes.

Unimplemented functions

moodle-mod_zoom/lib.php

Lines 439 to 493 in 812974f

/**
* Given a course and a time, this module should find recent activity that has occurred in zoom activities and print it out.
*
* @param stdClass $course The course record
* @param bool $viewfullnames Should we display full names
* @param int $timestart Print activity since this timestamp
* @return boolean True if anything was printed, otherwise false
*/
function zoom_print_recent_activity($course, $viewfullnames, $timestart) {
return false;
}
/**
* Prepares the recent activity data
*
* This callback function is supposed to populate the passed array with
* custom activity records. These records are then rendered into HTML
* zoom_print_recent_mod_activity().
*
* Returns void, it adds items into $activities and increases $index.
*
* @param array $activities sequentially indexed array of objects with added 'cmid' property
* @param int $index the index in the $activities to use for the next record
* @param int $timestart append activity since this time
* @param int $courseid the id of the course we produce the report for
* @param int $cmid course module id
* @param int $userid check for a particular user's activity only, defaults to 0 (all users)
* @param int $groupid check for a particular group's activity only, defaults to 0 (all groups)
*/
function zoom_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $cmid, $userid = 0, $groupid = 0) {
}
/**
* Prints single activity item prepared by zoom_get_recent_mod_activity()
*
* @param stdClass $activity activity record with added 'cmid' property
* @param int $courseid the id of the course we produce the report for
* @param bool $detail print detailed report
* @param array $modnames as returned by get_module_types_names()
* @param bool $viewfullnames display users' full names
*/
function zoom_print_recent_mod_activity($activity, $courseid, $detail, $modnames, $viewfullnames) {
}
/**
* Returns all other caps used in the module
*
* For example, this could be array('moodle/site:accessallgroups') if the
* module uses that capability.
*
* @return array
*/
function zoom_get_extra_capabilities() {
return [];
}

moodle-mod_zoom/lib.php

Lines 892 to 979 in 812974f

/* File API */
/**
* Returns the lists of all browsable file areas within the given module context
*
* The file area 'intro' for the activity introduction field is added automatically
* by file_browser::get_file_info_context_module()
*
* @param stdClass $course
* @param stdClass $cm
* @param stdClass $context
* @return array of [(string)filearea] => (string)description
*/
function zoom_get_file_areas($course, $cm, $context) {
return [];
}
/**
* File browsing support for zoom file areas
*
* @package mod_zoom
* @category files
*
* @param file_browser $browser
* @param array $areas
* @param stdClass $course
* @param stdClass $cm
* @param stdClass $context
* @param string $filearea
* @param int $itemid
* @param string $filepath
* @param string $filename
* @return file_info instance or null if not found
*/
function zoom_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
return null;
}
/**
* Serves the files from the zoom file areas
*
* @package mod_zoom
* @category files
*
* @param stdClass $course the course object
* @param stdClass $cm the course module object
* @param stdClass $context the zoom's context
* @param string $filearea the name of the file area
* @param array $args extra arguments (itemid, path)
* @param bool $forcedownload whether or not force download
* @param array $options additional options affecting the file serving
*/
function zoom_pluginfile($course, $cm, $context, $filearea, array $args, $forcedownload, array $options = []) {
if ($context->contextlevel != CONTEXT_MODULE) {
send_file_not_found();
}
require_login($course, true, $cm);
send_file_not_found();
}
/* Navigation API */
/**
* Extends the global navigation tree by adding zoom nodes if there is a relevant content
*
* This can be called by an AJAX request so do not rely on $PAGE as it might not be set up properly.
*
* @param navigation_node $navref An object representing the navigation tree node of the zoom module instance
* @param stdClass $course current course record
* @param stdClass $module current zoom instance record
* @param cm_info $cm course module information
*/
function zoom_extend_navigation(navigation_node $navref, stdClass $course, stdClass $module, cm_info $cm) {
}
/**
* Extends the settings navigation with the zoom settings
*
* This function is called when the context for the page is a zoom module. This is not called by AJAX
* so it is safe to rely on the $PAGE.
*
* @param settings_navigation $settingsnav complete settings navigation tree
* @param navigation_node $zoomnode zoom administration node
*/
function zoom_extend_settings_navigation(settings_navigation $settingsnav, navigation_node $zoomnode = null) {
}

Misplaced functions

These functions should probably have been in locallib.php to begin with, but Moodle's current recommendation is to move supporting functions that do not need to be in lib.php to an autoloaded class in the plugin's classes directory.

moodle-mod_zoom/lib.php

Lines 215 to 351 in 812974f

/**
* Function to handle selected weekdays, for recurring weekly meeting.
*
* @param stdClass $zoom The zoom instance
* @return string The comma separated string for selected weekdays
*/
function zoom_handle_weekly_days($zoom) {
$weekdaynumbers = [];
for ($i = 1; $i <= 7; $i++) {
$key = 'weekly_days_' . $i;
if (!empty($zoom->$key)) {
$weekdaynumbers[] = $i;
}
}
return implode(',', $weekdaynumbers);
}
/**
* Function to unset the weekly options in postprocessing.
*
* @param stdClass $data The form data object
* @return stdClass $data The form data object minus weekly options.
*/
function zoom_remove_weekly_options($data) {
// Unset the weekly_days options.
for ($i = 1; $i <= 7; $i++) {
$key = 'weekly_days_' . $i;
unset($data->$key);
}
return $data;
}
/**
* Function to unset the monthly options in postprocessing.
*
* @param stdClass $data The form data object
* @return stdClass $data The form data object minus monthly options.
*/
function zoom_remove_monthly_options($data) {
// Unset the monthly options.
unset($data->monthly_repeat_option);
unset($data->monthly_day);
unset($data->monthly_week);
unset($data->monthly_week_day);
return $data;
}
/**
* Populates a zoom meeting or webinar from a response object.
*
* Given a zoom meeting object from mod_form.php, this function uses the response to repopulate some of the object properties.
*
* @param stdClass $zoom An object from the form in mod_form.php
* @param stdClass $response A response from an API call like 'create meeting' or 'update meeting'
* @return stdClass A $zoom object ready to be added to the database.
*/
function populate_zoom_from_response(stdClass $zoom, stdClass $response) {
global $CFG;
// Inlcuded for constants.
require_once($CFG->dirroot . '/mod/zoom/locallib.php');
$newzoom = clone $zoom;
$samefields = ['join_url', 'created_at', 'timezone'];
foreach ($samefields as $field) {
if (isset($response->$field)) {
$newzoom->$field = $response->$field;
}
}
if (isset($response->duration)) {
$newzoom->duration = $response->duration * 60;
}
$newzoom->meeting_id = $response->id;
$newzoom->name = $response->topic;
if (isset($response->start_time)) {
$newzoom->start_time = strtotime($response->start_time);
}
$recurringtypes = [
ZOOM_RECURRING_MEETING,
ZOOM_RECURRING_FIXED_MEETING,
ZOOM_RECURRING_WEBINAR,
ZOOM_RECURRING_FIXED_WEBINAR,
];
$newzoom->recurring = in_array($response->type, $recurringtypes);
if (!empty($response->occurrences)) {
$newzoom->occurrences = [];
// Normalise the occurrence times.
foreach ($response->occurrences as $occurrence) {
$occurrence->start_time = strtotime($occurrence->start_time);
$occurrence->duration = $occurrence->duration * 60;
$newzoom->occurrences[] = $occurrence;
}
}
if (isset($response->password)) {
$newzoom->password = $response->password;
}
if (isset($response->settings->encryption_type)) {
$newzoom->option_encryption_type = $response->settings->encryption_type;
}
if (isset($response->settings->join_before_host)) {
$newzoom->option_jbh = $response->settings->join_before_host;
}
if (isset($response->settings->participant_video)) {
$newzoom->option_participants_video = $response->settings->participant_video;
}
if (isset($response->settings->alternative_hosts)) {
$newzoom->alternative_hosts = $response->settings->alternative_hosts;
}
if (isset($response->settings->mute_upon_entry)) {
$newzoom->option_mute_upon_entry = $response->settings->mute_upon_entry;
}
if (isset($response->settings->meeting_authentication)) {
$newzoom->option_authenticated_users = $response->settings->meeting_authentication;
}
if (isset($response->settings->waiting_room)) {
$newzoom->option_waiting_room = $response->settings->waiting_room;
}
if (isset($response->settings->auto_recording)) {
$newzoom->option_auto_recording = $response->settings->auto_recording;
}
return $newzoom;
}

moodle-mod_zoom/lib.php

Lines 553 to 624 in 812974f

/**
* Return an array with the days of the week.
*
* @return array
*/
function zoom_get_weekday_options() {
return [
1 => get_string('sunday', 'calendar'),
2 => get_string('monday', 'calendar'),
3 => get_string('tuesday', 'calendar'),
4 => get_string('wednesday', 'calendar'),
5 => get_string('thursday', 'calendar'),
6 => get_string('friday', 'calendar'),
7 => get_string('saturday', 'calendar'),
];
}
/**
* Return an array with the weeks of the month.
*
* @return array
*/
function zoom_get_monthweek_options() {
return [
1 => get_string('weekoption_first', 'zoom'),
2 => get_string('weekoption_second', 'zoom'),
3 => get_string('weekoption_third', 'zoom'),
4 => get_string('weekoption_fourth', 'zoom'),
-1 => get_string('weekoption_last', 'zoom'),
];
}
/**
* Populate the calendar event object, based on the zoom instance
*
* @param stdClass $zoom The zoom instance.
* @param stdClass $occurrence The occurrence object passed from the zoom api.
* @return stdClass The calendar event object.
*/
function zoom_populate_calender_item(stdClass $zoom, stdClass $occurrence = null) {
$event = new stdClass();
$event->type = CALENDAR_EVENT_TYPE_ACTION;
$event->modulename = 'zoom';
$event->eventtype = 'zoom';
$event->courseid = $zoom->course;
$event->instance = $zoom->id;
$event->visible = true;
$event->name = $zoom->name;
if ($zoom->intro) {
$event->description = $zoom->intro;
$event->format = $zoom->introformat;
}
if (!$occurrence) {
$event->timesort = $zoom->start_time;
$event->timestart = $zoom->start_time;
$event->timeduration = $zoom->duration;
} else {
$event->timesort = $occurrence->start_time;
$event->timestart = $occurrence->start_time;
$event->timeduration = $occurrence->duration;
$event->uuid = $occurrence->occurrence_id;
}
// Recurring meetings/webinars with no fixed time are created as invisible events.
// For recurring meetings/webinars with a fixed time, we want to see the events on the calendar.
if ($zoom->recurring && $zoom->recurrence_type == ZOOM_RECURRINGTYPE_NOTIME) {
$event->visible = false;
}
return $event;
}

moodle-mod_zoom/lib.php

Lines 992 to 1272 in 812974f

/**
* This function updates the tracking field settings in config_plugins.
*/
function mod_zoom_update_tracking_fields() {
global $DB;
try {
$defaulttrackingfields = zoom_clean_tracking_fields();
$zoomprops = ['id', 'field', 'required', 'visible', 'recommended_values'];
$confignames = [];
if (!empty($defaulttrackingfields)) {
$zoomtrackingfields = zoom_list_tracking_fields();
foreach ($zoomtrackingfields as $field => $zoomtrackingfield) {
if (isset($defaulttrackingfields[$field])) {
foreach ($zoomprops as $zoomprop) {
$configname = 'tf_' . $field . '_' . $zoomprop;
$confignames[] = $configname;
if ($zoomprop === 'recommended_values') {
$configvalue = implode(', ', $zoomtrackingfield[$zoomprop]);
} else {
$configvalue = $zoomtrackingfield[$zoomprop];
}
set_config($configname, $configvalue, 'zoom');
}
}
}
}
$config = get_config('zoom');
$proparray = get_object_vars($config);
$properties = array_keys($proparray);
$oldconfigs = array_diff($properties, $confignames);
$pattern = '/^tf_(?P<oldfield>.*)_(' . implode('|', $zoomprops) . ')$/';
foreach ($oldconfigs as $oldconfig) {
if (preg_match($pattern, $oldconfig, $matches)) {
set_config($oldconfig, null, 'zoom');
$DB->delete_records('zoom_meeting_tracking_fields', ['tracking_field' => $matches['oldfield']]);
}
}
} catch (Exception $e) {
// Fail gracefully because the callback function might be called directly.
return false;
}
return true;
}
/**
* Insert zoom instance breakout rooms
*
* @param int $zoomid
* @param array $breakoutrooms zoom breakout rooms
*/
function zoom_insert_instance_breakout_rooms($zoomid, $breakoutrooms) {
global $DB;
foreach ($breakoutrooms as $breakoutroom) {
$item = new stdClass();
$item->name = $breakoutroom['name'];
$item->zoomid = $zoomid;
$breakoutroomid = $DB->insert_record('zoom_meeting_breakout_rooms', $item);
foreach ($breakoutroom['participants'] as $participant) {
$item = new stdClass();
$item->userid = $participant;
$item->breakoutroomid = $breakoutroomid;
$DB->insert_record('zoom_breakout_participants', $item);
}
foreach ($breakoutroom['groups'] as $group) {
$item = new stdClass();
$item->groupid = $group;
$item->breakoutroomid = $breakoutroomid;
$DB->insert_record('zoom_breakout_groups', $item);
}
}
}
/**
* Update zoom instance breakout rooms
*
* @param int $zoomid
* @param array $breakoutrooms
*/
function zoom_update_instance_breakout_rooms($zoomid, $breakoutrooms) {
global $DB;
zoom_delete_instance_breakout_rooms($zoomid);
zoom_insert_instance_breakout_rooms($zoomid, $breakoutrooms);
}
/**
* Delete zoom instance breakout rooms
*
* @param int $zoomid
*/
function zoom_delete_instance_breakout_rooms($zoomid) {
global $DB;
$zoomcurrentbreakoutroomsids = $DB->get_fieldset_select('zoom_meeting_breakout_rooms', 'id', "zoomid = {$zoomid}");
foreach ($zoomcurrentbreakoutroomsids as $id) {
$DB->delete_records('zoom_breakout_participants', ['breakoutroomid' => $id]);
$DB->delete_records('zoom_breakout_groups', ['breakoutroomid' => $id]);
}
$DB->delete_records('zoom_meeting_breakout_rooms', ['zoomid' => $zoomid]);
}
/**
* Build zoom instance breakout rooms array for api
*
* @param stdClass $zoom Submitted data from the form in mod_form.php.
* @return array The meeting breakout rooms array.
*/
function zoom_build_instance_breakout_rooms_array_for_api($zoom) {
$context = context_course::instance($zoom->course);
$users = get_enrolled_users($context);
$groups = groups_get_all_groups($zoom->course);
// Building meeting breakout rooms array.
$breakoutrooms = [];
if (!empty($zoom->rooms)) {
foreach ($zoom->rooms as $roomid => $roomname) {
// Getting meeting rooms participants.
$roomparticipants = [];
$dbroomparticipants = [];
if (!empty($zoom->roomsparticipants[$roomid])) {
foreach ($zoom->roomsparticipants[$roomid] as $participantid) {
if (isset($users[$participantid])) {
$roomparticipants[] = $users[$participantid]->email;
$dbroomparticipants[] = $participantid;
}
}
}
// Getting meeting rooms groups members.
$roomgroupsmembers = [];
$dbroomgroupsmembers = [];
if (!empty($zoom->roomsgroups[$roomid])) {
foreach ($zoom->roomsgroups[$roomid] as $groupid) {
if (isset($groups[$groupid])) {
$groupmembers = groups_get_members($groupid);
$roomgroupsmembers[] = array_column(array_values($groupmembers), 'email');
$dbroomgroupsmembers[] = $groupid;
}
}
$roomgroupsmembers = array_merge(...$roomgroupsmembers);
}
$zoomdata = [
'name' => $roomname,
'participants' => array_values(array_unique(array_merge($roomparticipants, $roomgroupsmembers))),
];
$dbdata = [
'name' => $roomname,
'participants' => $dbroomparticipants,
'groups' => $dbroomgroupsmembers,
];
$breakoutrooms['zoom'][] = $zoomdata;
$breakoutrooms['db'][] = $dbdata;
}
}
return $breakoutrooms;
}
/**
* Build zoom instance breakout rooms array for view.
*
* @param int $zoomid
* @param array $courseparticipants
* @param array $coursegroups
* @return array The meeting breakout rooms array.
*/
function zoom_build_instance_breakout_rooms_array_for_view($zoomid, $courseparticipants, $coursegroups) {
$breakoutrooms = zoom_get_instance_breakout_rooms($zoomid);
$rooms = [];
if (!empty($breakoutrooms)) {
foreach ($breakoutrooms as $key => $breakoutroom) {
$roomparticipants = $courseparticipants;
if (!empty($breakoutroom['participants'])) {
$participants = $breakoutroom['participants'];
$roomparticipants = array_map(function ($roomparticipant) use ($participants) {
if (isset($participants[$roomparticipant['participantid']])) {
$roomparticipant['selected'] = true;
}
return $roomparticipant;
}, $courseparticipants);
}
$roomgroups = $coursegroups;
if (!empty($breakoutroom['groups'])) {
$groups = $breakoutroom['groups'];
$roomgroups = array_map(function ($roomgroup) use ($groups) {
if (isset($groups[$roomgroup['groupid']])) {
$roomgroup['selected'] = true;
}
return $roomgroup;
}, $coursegroups);
}
$rooms[] = [
'roomid' => $breakoutroom['roomid'],
'roomname' => $breakoutroom['roomname'],
'courseparticipants' => $roomparticipants,
'coursegroups' => $roomgroups,
];
}
$rooms[0]['roomactive'] = true;
}
return $rooms;
}
/**
* Get zoom instance breakout rooms.
*
* @param int $zoomid
* @return array
*/
function zoom_get_instance_breakout_rooms($zoomid) {
global $DB;
$breakoutrooms = [];
$params = [$zoomid];
$sql = "SELECT id, name
FROM {zoom_meeting_breakout_rooms}
WHERE zoomid = ?";
$rooms = $DB->get_records_sql($sql, $params);
foreach ($rooms as $room) {
$breakoutrooms[$room->id] = [
'roomid' => $room->id,
'roomname' => $room->name,
'participants' => [],
'groups' => [],
];
// Get breakout room participants.
$params = [$room->id];
$sql = "SELECT userid
FROM {zoom_breakout_participants}
WHERE breakoutroomid = ?";
$participants = $DB->get_records_sql($sql, $params);
if (!empty($participants)) {
foreach ($participants as $participant) {
$breakoutrooms[$room->id]['participants'][$participant->userid] = $participant->userid;
}
}
// Get breakout room groups.
$sql = "SELECT groupid
FROM {zoom_breakout_groups}
WHERE breakoutroomid = ?";
$groups = $DB->get_records_sql($sql, $params);
if (!empty($groups)) {
foreach ($groups as $group) {
$breakoutrooms[$room->id]['groups'][$group->groupid] = $group->groupid;
}
}
}
return $breakoutrooms;
}

@jrchamp jrchamp added the bug Fixes problems or reduces technical debt label Jan 29, 2024
@jrchamp
Copy link
Collaborator Author

jrchamp commented Feb 1, 2024

Please note that mod_zoom_update_tracking_fields() is set as a callback in the module settings, so this may need to stay in lib.php if set_updatedcallback() does not allow for namespaced class methods.

@jrchamp jrchamp added the help wanted We need your help to make this possible label Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Fixes problems or reduces technical debt help wanted We need your help to make this possible
Projects
Status: To Do
Development

No branches or pull requests

1 participant