in classes/page/acp.php [776:988]
public function mode_teamconnections() {
global $DB, $OUTPUT, $PAGE;
$PAGE->navbar->add(get_string('acp_teamconnections', 'local_o365'),
new \moodle_url($this->url, ['mode' => 'teamconnections']));
// Check settings.
$createteams = get_config('local_o365', 'createteams');
if ($createteams === 'off') {
$redirecturl = new \moodle_url('/admin/settings.php', ['section' => 'local_o365', 's_local_o365_tabs' => 1]);
redirect($redirecturl, get_string('acp_teamconnections_sync_disabled', 'local_o365'));
}
$totalcount = 0;
$perpage = 20;
$curpage = optional_param('page', 0, PARAM_INT);
$sort = optional_param('sort', '', PARAM_ALPHA);
$search = optional_param('search', '', PARAM_TEXT);
$sortdir = strtolower(optional_param('sortdir', 'asc', PARAM_ALPHA));
$headers = [
'fullname' => get_string('fullnamecourse'),
'shortname' => get_string('shortnamecourse'),
];
if (empty($sort) || !isset($headers[$sort])) {
$sort = 'fullname';
}
if (!in_array($sortdir, ['asc', 'desc'], true)) {
$sortdir = 'asc';
}
$table = new \html_table();
foreach ($headers as $hkey => $desc) {
$diffsortdir = ($sort === $hkey && $sortdir === 'asc') ? 'desc' : 'asc';
$linkattrs = ['mode' => 'teamconnections', 'sort' => $hkey, 'sortdir' => $diffsortdir];
$link = new \moodle_url('/local/o365/acp.php', $linkattrs);
if ($sort === $hkey) {
$desc .= ' ' . $OUTPUT->pix_icon('t/sort_' . $sortdir, 'sort');
}
$table->head[] = \html_writer::link($link, $desc);
}
$table->head[] = get_string('acp_teamconnections_connected_team', 'local_o365');
$table->head[] = get_string('acp_teamconnections_actions', 'local_o365');
$limitfrom = $curpage * $perpage;
$coursesid = [];
if (empty($search)) {
$sortdir = 1;
if ($sortdir == 'desc') {
$sortdir = -1;
}
$options = [
'recursive' => true,
'sort' => [$sort => $sortdir],
'offset' => $limitfrom,
'limit' => $perpage,
];
$topcat = \core_course_category::get(0);
$courses = $topcat->get_courses($options);
$totalcount = $topcat->get_courses_count($options);
} else {
$searchar = explode(' ', $search);
$courses = get_courses_search($searchar, 'c.' . $sort . ' ' . $sortdir, $curpage, $perpage, $totalcount);
}
foreach ($courses as $course) {
$actions = [];
if ($course->id == SITEID) {
continue;
}
$coursesid[] = $course->id;
if ($grouprecord = $DB->get_record('local_o365_objects',
['moodleid' => $course->id, 'type' => 'group', 'subtype' => 'course'])) {
if ($teamrecord = $DB->get_record('local_o365_objects',
['moodleid' => $course->id, 'type' => 'group', 'subtype' => 'courseteam'])) {
// Synced to both group and team.
if ($teamscache = $DB->get_record('local_o365_teams_cache', ['objectid' => $grouprecord->objectid])) {
// Team record can be found in cache.
$existingconnection = \html_writer::link($teamscache->url, $teamscache->name);
$updateurl = new \moodle_url('/local/o365/acp.php',
['mode' => 'teamconnections_update', 'course' => $course->id, 'sesskey' => sesskey()]);
$updatelabel = get_string('acp_teamconnections_table_update', 'local_o365');
$actions = [\html_writer::link($updateurl, $updatelabel)];
} else {
// A matching record exists in local_o365_objects, but the team cannot be found.
// This may be caused by the connection was soft deleted.
$existingconnection = get_string('acp_teamconnections_not_connected', 'local_o365');
$connecturl = new \moodle_url('/local/o365/acp.php',
['mode' => 'teamconnections_connect', 'course' => $course->id, 'sesskey' => sesskey()]);
$connectlabel = get_string('acp_teamconnections_table_connect', 'local_o365');
$actions = [\html_writer::link($connecturl, $connectlabel)];
}
} else {
// Synced to group only.
$metadata = (!empty($grouprecord->metadata)) ? json_decode($grouprecord->metadata, true) : [];
if (is_array($metadata) && !empty($metadata['softdelete'])) {
// Deleted group connection.
$existingconnection = get_string('acp_teamconnections_not_connected', 'local_o365');
$connecturl = new \moodle_url('/local/o365/acp.php',
['mode' => 'teamconnections_connect', 'course' => $course->id, 'sesskey' => sesskey()]);
$connectlabel = get_string('acp_teamconnections_table_connect', 'local_o365');
$actions = [\html_writer::link($connecturl, $connectlabel)];
} else if ($teamscache = $DB->get_record('local_o365_teams_cache', ['objectid' => $grouprecord->objectid])) {
// A team is found for the synced group.
$existingconnection = \html_writer::link($teamscache->url, $teamscache->name) . '<br/>' .
get_string('acp_teamconnections_team_exists_but_not_connected', 'local_o365');
$completeconnectionurl = new \moodle_url('/local/o365/acp.php',
['mode' => 'teamconnections_complete_connection', 'course' => $course->id, 'sesskey' => sesskey()]);
$completeconnectionlabel = get_string('acp_teamconnections_table_complete_connection', 'local_o365');
$actions = [\html_writer::link($completeconnectionurl, $completeconnectionlabel)];
$connecturl = new \moodle_url('/local/o365/acp.php',
['mode' => 'teamconnections_connect', 'course' => $course->id, 'sesskey' => sesskey()]);
$connectlabel = get_string('acp_teamconnections_table_connect_to_different_team', 'local_o365');
$actions[] = \html_writer::link($connecturl, $connectlabel);
} else {
// A team does not exist for the synced group.
$teamownerids = \local_o365\feature\usergroups\coursegroups::get_team_owner_ids_by_course_id($course->id);
$existingconnection = $grouprecord->o365name . get_string('acp_teamconnections_group_only', 'local_o365');
if (!empty($teamownerids)) {
$createteamurl = new \moodle_url('/local/o365/acp.php',
['mode' => 'teamconnections_create_team', 'course' => $course->id, 'sesskey' => sesskey()]);
$createteamlabel = get_string('acp_teamconnections_table_create_team', 'local_o365');
$actions = [\html_writer::link($createteamurl, $createteamlabel)];
} else {
$actions = [\html_writer::span(get_string('acp_teamconnections_table_cannot_create_team_from_group',
'local_o365'))];
}
$connecturl = new \moodle_url('/local/o365/acp.php',
['mode' => 'teamconnections_connect', 'course' => $course->id, 'sesskey' => sesskey()]);
$connectlabel = get_string('acp_teamconnections_table_connect_to_different_team', 'local_o365');
$actions[] = \html_writer::link($connecturl, $connectlabel);
}
}
} else {
$existingconnection = get_string('acp_teamconnections_not_connected', 'local_o365');
$teamownerids = \local_o365\feature\usergroups\coursegroups::get_team_owner_ids_by_course_id($course->id);
if (!empty($teamownerids)) {
$connecturl = new \moodle_url('/local/o365/acp.php',
['mode' => 'teamconnections_connect', 'course' => $course->id, 'sesskey' => sesskey()]);
$connectlabel = get_string('acp_teamconnections_table_connect', 'local_o365');
$actions = [\html_writer::link($connecturl, $connectlabel)];
}
}
$actionsfield = implode('<br/>', $actions);
$courseurl = new \moodle_url('/course/view.php', ['id' => $course->id]);
$rowdata = [
\html_writer::link($courseurl, $course->fullname),
$course->shortname,
$existingconnection,
$actionsfield,
];
$table->data[] = $rowdata;
}
$PAGE->requires->jquery();
$this->standard_header();
echo \html_writer::tag('h2', get_string('acp_teamconnections_title', 'local_o365'));
// Cache status.
$teamscacheupdated = get_config('local_o365', 'teamscacheupdated');
$updatecacheurl = new \moodle_url('/local/o365/acp.php',
['mode' => 'teamconnections_update_cache', 'sesskey' => sesskey()]);
$linkparams = ['updateurl' => $updatecacheurl->out()];
if ($teamscacheupdated) {
$linkparams['lastupdated'] = userdate($teamscacheupdated);
echo \html_writer::div(get_string('acp_teamconnections_cache_last_updated', 'local_o365', $linkparams));
} else {
echo \html_writer::div(get_string('acp_teamconnections_cache_never_updated', 'local_o365', $linkparams));
}
// Search form.
echo \html_writer::tag('h5', get_string('search'));
echo \html_writer::start_tag('form', ['id' => 'coursesearchform', 'method' => 'get']);
echo \html_writer::start_tag('fieldset', ['class' => 'coursesearchbox invisiblefieldset']);
echo \html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'mode', 'value' => 'teamconnections']);
echo \html_writer::empty_tag('input', ['type' => 'text', 'id' => 'coursesearchbox', 'size' => 30, 'name' => 'search',
'value' => s($search)]);
echo \html_writer::empty_tag('input', ['type' => 'submit', 'value' => get_string('go')]);
echo \html_writer::div(\html_writer::tag('strong', get_string('acp_usergroupcustom_searchwarning', 'local_o365')));
echo \html_writer::end_tag('fieldset');
echo \html_writer::end_tag('form');
echo \html_writer::empty_tag('br');
echo \html_writer::tag('h5', get_string('courses'));
echo \html_writer::table($table);
$searchtext = optional_param('search', '', PARAM_TEXT);
$cururl = new \moodle_url('/local/o365/acp.php', ['mode' => 'teamconnections', 'search' => $searchtext]);
echo $OUTPUT->paging_bar($totalcount, $curpage, $perpage, $cururl);
$this->standard_footer();
}