public function get_file_reference()

in lib.php [999:1078]


    public function get_file_reference($source) {
        global $USER;

        $caller = '\repository_office365::get_file_reference';
        $sourceunpacked = $this->unpack_reference($source);
        if (isset($sourceunpacked['source']) && isset($sourceunpacked['id'])) {
            $fileid = $sourceunpacked['id'];
            $filesource = $sourceunpacked['source'];

            $reference = [
                'source' => $filesource,
                'id' => $fileid,
                'url' => '',
            ];

            if (isset($sourceunpacked['url'])) {
                $reference['url'] = $sourceunpacked['url'];
            }
            if (isset($sourceunpacked['downloadurl'])) {
                $reference['downloadurl'] = $sourceunpacked['downloadurl'];
            }

            try {
                if ($filesource === 'onedrive') {
                    if ($this->unifiedconfigured === true) {
                        $sourceclient = $this->get_unified_apiclient();
                        $o365userid = utils::get_o365_userid($USER->id);
                        $reference['url'] = $sourceclient->get_sharing_link($fileid, $o365userid);
                    } else {
                        $sourceclient = $this->get_onedrive_apiclient();
                        $filemetadata = $sourceclient->get_file_metadata($fileid);
                        if (isset($filemetadata['webUrl'])) {
                            $reference['url'] = $filemetadata['webUrl'].'?web=1';
                        }
                    }
                } else if ($filesource === 'onedrivegroup') {
                    if ($this->unifiedconfigured !== true) {
                        utils::debug('Tried to access a onedrive group file while the graph api is disabled.', $caller);
                        throw new \moodle_exception('errorwhiledownload', 'repository_office365');
                    }
                    $sourceclient = $this->get_unified_apiclient();
                    $reference['groupid'] = $sourceunpacked['groupid'];
                    $reference['url'] = $sourceclient->get_group_file_sharing_link($sourceunpacked['groupid'], $fileid);
                } else if ($filesource === 'trendingaround') {
                    if ($this->unifiedconfigured !== true) {
                        utils::debug('Tried to access a trending around me file while the graph api is disabled.', $caller);
                        throw new \moodle_exception('errorwhiledownload', 'repository_office365');
                    }
                    $sourceclient = $this->get_unified_apiclient();
                    $filedata = $sourceclient->get_file_data($fileid);
                    if (isset($filedata['@microsoft.graph.downloadUrl'])) {
                        $reference['url'] = $filedata['@microsoft.graph.downloadUrl'];
                    }
                }

            } catch (\Exception $e) {
                $errmsg = 'There was a problem making the API call.';
                $debugdata = [
                    'source' => $filesource,
                    'id' => $fileid,
                    'message' => $e->getMessage(),
                    'e' => $e,
                ];
                utils::debug($errmsg, $caller, $debugdata);
            }

            return $this->pack_reference($reference);
        } else {
            $errmsg = '';
            if (!isset($sourceunpacked['source'])) {
                $errmsg = 'Source is not set.';
            }
            if (isset($sourceunpacked['id'])) {
                $errmsg .= ' id is not set.';
            }
            $debugdata = ['sourceunpacked' => $sourceunpacked];
            utils::debug($errmsg, $caller, $debugdata);
        }
        return $source;
    }