in repository/office365/lib.php [422:569]
protected function get_listing_groups($path = '') {
global $OUTPUT, $USER, $DB;
$caller = '\repository_office365::get_listing_groups';
$list = [];
$breadcrumb = [
['name' => $this->name, 'path' => '/'],
['name' => get_string('groups', 'repository_office365'), 'path' => '/groups/'],
];
$coursesbyid = enrol_get_users_courses($USER->id, true);
if ($path === '/') {
// Show available courses.
$enabledcourses = \local_o365\feature\usergroups\utils::get_enabled_courses();
foreach ($coursesbyid as $course) {
if ($enabledcourses === true || in_array($course->id, $enabledcourses)) {
$list[] = [
'title' => $course->shortname,
'path' => '/groups/'.$course->id,
'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false),
'children' => [],
];
}
}
} else {
$pathtrimmed = trim($path, '/');
$pathparts = explode('/', $pathtrimmed);
if (!is_numeric($pathparts[0]) || !isset($coursesbyid[$pathparts[0]])
|| \local_o365\feature\usergroups\utils::course_is_group_enabled($pathparts[0]) !== true) {
utils::debug(get_string('errorbadpath', 'repository_office365'), $caller, ['path' => $path]);
throw new \moodle_exception('errorbadpath', 'repository_office365');
}
$courseid = (int)$pathparts[0];
$curpath = '/groups/'.$courseid;
$breadcrumb[] = ['name' => $coursesbyid[$courseid]->shortname, 'path' => $curpath];
$sql = 'SELECT g.*
FROM {groups} g
JOIN {groups_members} m ON m.groupid = g.id
WHERE m.userid = ? AND g.courseid = ?';
$coursegroups = $DB->get_records_sql($sql, [$USER->id, $courseid]);
if (count($pathparts) === 1) {
$list[] = [
'title' => get_string('defaultgroupsfolder', 'repository_office365'),
'path' => $curpath.'/coursegroup/',
'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false),
'children' => [],
];
foreach ($coursegroups as $group) {
$list[] = [
'title' => $group->name,
'path' => $curpath.'/'.$group->id.'/',
'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false),
'children' => [],
];
}
} else {
// Validate the received group identifier.
if (!is_numeric($pathparts[1]) && $pathparts[1] !== 'coursegroup') {
utils::debug(get_string('errorbadpath', 'repository_office365'), $caller, ['path' => $path]);
throw new \moodle_exception('errorbadpath', 'repository_office365');
}
$curpath .= '/'.$pathparts[1].'/';
if ($pathparts[1] === 'coursegroup') {
$breadcrumb[] = ['name' => get_string('defaultgroupsfolder', 'repository_office365'), 'path' => $curpath];
$filters = ['type' => 'group', 'subtype' => 'course', 'moodleid' => $courseid];
$group = $DB->get_record('local_o365_objects', $filters);
} else {
// Validate the user is a member of the group.
if (!isset($coursegroups[$pathparts[1]])) {
utils::debug(get_string('errorbadpath', 'repository_office365'), $caller, ['path' => $path]);
throw new \moodle_exception('errorbadpath', 'repository_office365');
}
$groupid = (int)$pathparts[1];
$group = $DB->get_record('groups', ['id' => $groupid]);
$breadcrumb[] = ['name' => $group->name, 'path' => $curpath];
$filters = ['type' => 'group', 'subtype' => 'usergroup', 'moodleid' => $groupid];
$group = $DB->get_record('local_o365_objects', $filters);
}
$intragrouppath = $pathparts;
unset($intragrouppath[0], $intragrouppath[1]);
$curparent = trim(end($intragrouppath));
if (!empty($group)) {
if ($curparent === 'upload') {
$breadcrumb[] = ['name' => get_string('upload', 'repository_office365'), 'path' => $curpath.'upload/'];
} else {
$unified = $this->get_unified_apiclient();
if (!empty($curparent)) {
$metadata = $unified->get_group_file_metadata($group->objectid, $curparent);
if (!empty($metadata['parentReference']) && !empty($metadata['parentReference']['path'])) {
$parentrefpath = substr($metadata['parentReference']['path']
, (strpos($metadata['parentReference']['path'], ':') + 1));
$cache = \cache::make('repository_office365', 'unifiedgroupfolderids');
$result = $cache->set($parentrefpath.'/'.$metadata['name'], $metadata['id']);
if (!empty($parentrefpath)) {
$parentrefpath = explode('/', trim($parentrefpath, '/'));
$currentfullpath = '';
foreach ($parentrefpath as $folder) {
$currentfullpath .= '/'.$folder;
$folderid = $cache->get($currentfullpath);
$breadcrumb[] = ['name' => $folder, 'path' => $curpath.$folderid];
}
}
}
$breadcrumb[] = ['name' => $metadata['name'], 'path' => $curpath.$metadata['id']];
}
try {
$filesresults = $unified->get_group_files($group->objectid, $curparent);
$contents = $filesresults['value'];
while (!empty($filesresults['@odata.nextLink'])) {
$nextlink = parse_url($filesresults['@odata.nextLink']);
if (isset($nextlink['query'])) {
$query = [];
parse_str($nextlink['query'], $query);
if (isset($query['$skiptoken'])) {
$filesresults = $unified->get_group_files($group->objectid, $curparent,
$query['$skiptoken']);
$contents = array_merge($contents, $filesresults['value']);
}
}
}
$list = $this->contents_api_response_to_list($contents, $path, 'unifiedgroup', $group->objectid, true);
} catch (\Exception $e) {
$errmsg = 'Exception when retrieving share point files for group';
$debugdata = [
'fullpath' => $path,
'message' => $e->getMessage(),
'groupid' => $group->objectid,
];
utils::debug($errmsg, $caller, $debugdata);
$list = [];
}
}
} else {
utils::debug('Could not file group object record', $caller, ['path' => $path]);
$list = [];
}
}
}
return [$list, $breadcrumb];
}