/** * Do the job. */ public function execute() { global $DB; $oidcconfig = get_config('auth_oidc'); if (empty($oidcconfig)) { throw new \moodle_exception('erroracpauthoidcnotconfig', 'local_o365'); } $spresource = \local_o365\rest\sharepoint::get_resource(); if (empty($spresource)) { throw new \moodle_exception('erroracplocalo365notconfig', 'local_o365'); } $httpclient = new \local_o365\httpclient(); $clientdata = new \local_o365\oauth2\clientdata($oidcconfig->clientid, $oidcconfig->clientsecret, $oidcconfig->authendpoint, $oidcconfig->tokenendpoint); $sptoken = \local_o365\oauth2\systemtoken::instance(null, $spresource, $clientdata, $httpclient); if (empty($sptoken)) { throw new \moodle_exception('erroracpnosptoken', 'local_o365'); } $sharepoint = new \local_o365\rest\sharepoint($sptoken, $httpclient); $sharepoint->set_site(''); $moodlesiteuri = $sharepoint->get_moodle_parent_site_uri(); if ($sharepoint->site_exists($moodlesiteuri) === false) { $moodlesitename = get_string('acp_parentsite_name', 'local_o365'); $moodlesitedesc = get_string('acp_parentsite_desc', 'local_o365'); $frontpagerec = $DB->get_record('course', ['id' => SITEID], 'id,shortname'); if (!empty($frontpagerec) && !empty($frontpagerec->shortname)) { $moodlesitename = $frontpagerec->shortname; } $result = $sharepoint->create_site($moodlesitename, $moodlesiteuri, $moodlesitedesc); mtrace('Created parent site'); } $courses = $DB->get_recordset('course'); $successes = []; $failures = []; foreach ($courses as $course) { if ($course->id == SITEID) { continue; } try { $sharepoint->create_course_site($course); $successes[] = $course->id; mtrace('Created course subsite for course ' . $course->id); } catch (\Exception $e) { $failures[$course->id] = $e->getMessage(); } } set_config('sharepoint_initialized', '1', 'local_o365'); }
/** * Construct a sharepoint API client using the system API user. * * @return \local_o365\rest\sharepoint|bool A constructed sharepoint API client, or false if error. */ public static function construct_sharepoint_api_with_system_user() { try { $spresource = \local_o365\rest\sharepoint::get_resource(); if (!empty($spresource)) { $httpclient = new \local_o365\httpclient(); $clientdata = \local_o365\oauth2\clientdata::instance_from_oidc(); $sptoken = \local_o365\oauth2\systemtoken::instance(null, $spresource, $clientdata, $httpclient); if (!empty($sptoken)) { $sharepoint = new \local_o365\rest\sharepoint($sptoken, $httpclient); return $sharepoint; } } } catch (\Exception $e) { \local_o365\utils::debug($e->getMessage(), get_called_class()); } return false; }
/** * Get a SharePoint token. * * @return \local_o365\oauth2\token A SharePoint token object. */ protected function get_sharepoint_token() { global $USER; $resource = \local_o365\rest\sharepoint::get_resource(); return \local_o365\oauth2\token::instance($USER->id, $resource, $this->clientdata, $this->httpclient); }
/** * Looks for links pointing to Office 365 Video content and processes them. * * @param $link HTML tag containing a link * @return string HTML content after processing. */ function filter_oembed_o365videocallback($link) { if (empty($link[3])) { return $link[0]; } $link[3] = preg_replace("/&/", "&", $link[3]); $values = array(); parse_str($link[3], $values); if (empty($values['chid']) || empty($values['vid'])) { return $link[0]; } if (!\local_o365\rest\sharepoint::is_configured()) { \local_o365\utils::debug('filter_oembed share point is not configured', 'filter_oembed_o365videocallback'); return $link[0]; } try { $spresource = \local_o365\rest\sharepoint::get_resource(); if (!empty($spresource)) { $httpclient = new \local_o365\httpclient(); $clientdata = \local_o365\oauth2\clientdata::instance_from_oidc(); $sptoken = \local_o365\oauth2\systemtoken::instance(null, $spresource, $clientdata, $httpclient); if (!empty($sptoken)) { $sharepoint = new \local_o365\rest\sharepoint($sptoken, $httpclient); // Retrieve api url for video service. $url = $sharepoint->videoservice_discover(); if (!empty($url)) { $sharepoint->override_resource($url); $width = 640; if (!empty($values['width'])) { $width = $values['width']; } $height = 360; if (!empty($values['height'])) { $height = $values['height']; } // Retrieve embed code. return $sharepoint->get_video_embed_code($values['chid'], $values['vid'], $width, $height); } } } } catch (\Exception $e) { \local_o365\utils::debug('filter_oembed share point execption: ' . $e->getMessage(), 'filter_oembed_o365videocallback'); } return $link[0]; }
/** * Do the job. */ public function execute() { global $DB; // API Setup. try { $spresource = \local_o365\rest\sharepoint::get_resource(); if (empty($spresource)) { throw new \moodle_exception('erroracplocalo365notconfig', 'local_o365'); } $httpclient = new \local_o365\httpclient(); $clientdata = \local_o365\oauth2\clientdata::instance_from_oidc(); $sptoken = \local_o365\oauth2\systemtoken::instance(null, $spresource, $clientdata, $httpclient); if (empty($sptoken)) { throw new \moodle_exception('erroracpnosptoken', 'local_o365'); } $sharepoint = new \local_o365\rest\sharepoint($sptoken, $httpclient); } catch (\Exception $e) { $errmsg = 'ERROR: Problem initializing SharePoint API. Reason: ' . $e->getMessage(); mtrace($errmsg); \local_o365\utils::debug($errmsg, 'local_o365\\task\\sharepointinit::execute'); set_config('sharepoint_initialized', 'error', 'local_o365'); return false; } // Create parent site(s). try { mtrace('Creating parent site for Moodle...'); $moodlesiteuri = $sharepoint->get_moodle_parent_site_uri(); $sitelevels = explode('/', $moodlesiteuri); $currentparentsite = ''; foreach ($sitelevels as $partialurl) { $sharepoint->set_site($currentparentsite); if ($sharepoint->site_exists($currentparentsite . '/' . $partialurl) === false) { $moodlesitename = get_string('acp_parentsite_name', 'local_o365'); $moodlesitedesc = get_string('acp_parentsite_desc', 'local_o365'); $frontpagerec = $DB->get_record('course', ['id' => SITEID], 'id,shortname'); if (!empty($frontpagerec) && !empty($frontpagerec->shortname)) { $moodlesitename = $frontpagerec->shortname; } mtrace('Setting parent site to "' . $currentparentsite . '", creating subsite "' . $partialurl . '"'); $result = $sharepoint->create_site($moodlesitename, $partialurl, $moodlesitedesc); $currentparentsite .= '/' . $partialurl; mtrace('Created parent site "' . $currentparentsite . '"'); } else { $currentparentsite .= '/' . $partialurl; mtrace('Parent site "' . $currentparentsite . '" already exists.'); } } mtrace('Finished creating Moodle parent site.'); } catch (\Exception $e) { $errmsg = 'ERROR: Problem creating parent site. Reason: ' . $e->getMessage(); mtrace($errmsg); \local_o365\utils::debug($errmsg, 'local_o365\\task\\sharepointinit::execute'); set_config('sharepoint_initialized', 'error', 'local_o365'); return false; } // Create course sites. mtrace('Creating course subsites in "' . $moodlesiteuri . '"'); $sharepoint->set_site($moodlesiteuri); $courses = $DB->get_recordset('course'); $successes = []; $failures = []; foreach ($courses as $course) { if ($course->id == SITEID) { continue; } try { $sharepoint->create_course_site($course); $successes[] = $course->id; mtrace('Created course subsite for course ' . $course->id); } catch (\Exception $e) { mtrace('Encountered error creating course subsite for course ' . $course->id); $failures[$course->id] = $e->getMessage(); } } if (!empty($failures)) { $errmsg = 'ERROR: Encountered problems creating course sites.'; mtrace($errmsg . ' See logs.'); \local_o365\utils::debug($errmsg, 'local_o365\\task\\sharepointinit::execute', $failures); set_config('sharepoint_initialized', 'error', 'local_o365'); } else { set_config('sharepoint_initialized', '1', 'local_o365'); mtrace('SharePoint successfully initialized.'); return true; } }
/** * Get a SharePoint token. * * @param bool $system If true, get a system API ser token instead of the user's token. * @param int|null $userid The userid to get a token for. If null, the current user will be used. * @return \local_o365\oauth2\token A SharePoint token object. */ protected function get_sharepoint_token($system = false, $userid = null) { global $USER; $resource = \local_o365\rest\sharepoint::get_resource(); if ($system === true) { return \local_o365\oauth2\systemtoken::instance(null, $resource, $this->clientdata, $this->httpclient); } else { $userid = !empty($userid) ? $userid : $USER->id; return \local_o365\oauth2\token::instance($userid, $resource, $this->clientdata, $this->httpclient); } }
/** * Do the job. */ public function execute() { $reqcap = \local_o365\rest\sharepoint::get_course_site_required_capability(); $oidcconfig = get_config('auth_oidc'); if (!empty($oidcconfig)) { $spresource = \local_o365\rest\sharepoint::get_resource(); if (!empty($spresource)) { $httpclient = new \local_o365\httpclient(); $clientdata = new \local_o365\oauth2\clientdata($oidcconfig->clientid, $oidcconfig->clientsecret, $oidcconfig->authendpoint, $oidcconfig->tokenendpoint); $sptoken = \local_o365\oauth2\systemtoken::instance(null, $spresource, $clientdata, $httpclient); if (!empty($sptoken)) { $sharepoint = new \local_o365\rest\sharepoint($sptoken, $httpclient); } } } if (empty($sharepoint)) { throw new \moodle_exception('errorcreatingsharepointclient', 'local_o365'); } $opdata = $this->get_custom_data(); if ($opdata->userid !== '*' && $opdata->roleid !== '*' && !empty($opdata->contextid)) { // Single user role assign/unassign. $this->do_role_assignmentchange($opdata->roleid, $opdata->userid, $opdata->contextid, $reqcap, $sharepoint); } else { if ($opdata->userid === '*' && $opdata->roleid !== '*') { // Capability update. $this->do_role_capabilitychange($opdata->roleid, $reqcap, $sharepoint); } else { if ($opdata->roleid === '*' && $opdata->userid === '*') { // Role deleted. $this->do_role_delete($reqcap, $sharepoint); } } } }
/** * Construct a sharepoint API client using the system API user. * * @return \local_o365\rest\sharepoint|bool A constructed sharepoint API client, or false if error. */ public static function construct_sharepoint_api_with_system_user() { $oidcconfig = get_config('auth_oidc'); if (!empty($oidcconfig)) { $spresource = \local_o365\rest\sharepoint::get_resource(); if (!empty($spresource)) { $httpclient = new \local_o365\httpclient(); $clientdata = new \local_o365\oauth2\clientdata($oidcconfig->clientid, $oidcconfig->clientsecret, $oidcconfig->authendpoint, $oidcconfig->tokenendpoint); $sptoken = \local_o365\oauth2\systemtoken::instance(null, $spresource, $clientdata, $httpclient); if (!empty($sptoken)) { $sharepoint = new \local_o365\rest\sharepoint($sptoken, $httpclient); return $sharepoint; } } } return false; }
/** * Get content for a connected user. * * @return string Block content. */ protected function get_content_connected() { global $PAGE, $DB; $o365config = get_config('local_o365'); $html = ''; $langconnected = get_string('o365connected', 'block_microsoft'); $html .= '<h5>' . $langconnected . '</h5>'; $outlookurl = new \moodle_url('/local/o365/ucp.php?action=calendar'); $outlookstr = get_string('linkoutlook', 'block_microsoft'); $sharepointstr = get_string('linksharepoint', 'block_microsoft'); $prefsurl = new \moodle_url('/local/o365/ucp.php'); $prefsstr = get_string('linkprefs', 'block_microsoft'); $connecturl = new \moodle_url('/local/o365/ucp.php', ['action' => 'aadlogin']); $connectstr = get_string('linkconnection', 'block_microsoft'); $items = []; if ($PAGE->context instanceof \context_course && $PAGE->context->instanceid !== SITEID) { if (!empty($o365config->sharepointlink)) { $coursespsite = $DB->get_record('local_o365_coursespsite', ['courseid' => $PAGE->context->instanceid]); if (!empty($coursespsite)) { $spsite = \local_o365\rest\sharepoint::get_resource(); if (!empty($spsite)) { $spurl = $spsite . '/' . $coursespsite->siteurl; $spattrs = ['class' => 'servicelink block_microsoft_sharepoint', 'target' => '_blank']; $items[] = html_writer::link($spurl, $sharepointstr, $spattrs); $items[] = '<hr/>'; } } } } $items[] = $this->render_onenote(); $items[] = \html_writer::link($outlookurl, $outlookstr, ['class' => 'servicelink block_microsoft_outlook']); $items[] = \html_writer::link($prefsurl, $prefsstr, ['class' => 'servicelink block_microsoft_preferences']); $items[] = \html_writer::link($connecturl, $connectstr, ['class' => 'servicelink block_microsoft_connection']); $html .= \html_writer::alist($items); return $html; }
/** * Get content for a connected user. * * @return string Block content. */ protected function get_content_connected() { global $PAGE, $DB, $CFG, $SESSION, $USER, $OUTPUT; $o365config = get_config('local_o365'); $html = ''; $aadsync = get_config('local_o365', 'aadsync'); $aadsync = array_flip(explode(',', $aadsync)); // Only profile sync once for each session. if (empty($SESSION->block_microsoft_profilesync) && isset($aadsync['photosynconlogin'])) { $PAGE->requires->jquery(); $PAGE->requires->js('/blocks/microsoft/js/microsoft.js'); $PAGE->requires->js_init_call('microsoft_update_profile', array($CFG->wwwroot)); } $user = $DB->get_record('user', array('id' => $USER->id)); $langconnected = get_string('o365connected', 'block_microsoft', $user); $html .= '<h5>' . $langconnected . '</h5>'; if (!empty($user->picture)) { $html .= '<div class="profilepicture">'; $html .= $OUTPUT->user_picture($user, array('size' => 100, 'class' => 'block_microsoft_profile')); $html .= '</div>'; } $outlookurl = new \moodle_url('/local/o365/ucp.php?action=calendar'); $outlookstr = get_string('linkoutlook', 'block_microsoft'); $sharepointstr = get_string('linksharepoint', 'block_microsoft'); $prefsurl = new \moodle_url('/local/o365/ucp.php'); $prefsstr = get_string('linkprefs', 'block_microsoft'); $connecturl = new \moodle_url('/local/o365/ucp.php', ['action' => 'aadlogin']); $connectstr = get_string('linkconnection', 'block_microsoft'); $items = []; if ($PAGE->context instanceof \context_course && $PAGE->context->instanceid !== SITEID) { if (!empty($o365config->sharepointlink)) { $coursespsite = $DB->get_record('local_o365_coursespsite', ['courseid' => $PAGE->context->instanceid]); if (!empty($coursespsite)) { $spsite = \local_o365\rest\sharepoint::get_resource(); if (!empty($spsite)) { $spurl = $spsite . '/' . $coursespsite->siteurl; $spattrs = ['class' => 'servicelink block_microsoft_sharepoint', 'target' => '_blank']; $items[] = html_writer::link($spurl, $sharepointstr, $spattrs); $items[] = '<hr/>'; } } } } $items[] = $this->render_onenote(); $items[] = \html_writer::link($outlookurl, $outlookstr, ['class' => 'servicelink block_microsoft_outlook']); $items[] = \html_writer::link($prefsurl, $prefsstr, ['class' => 'servicelink block_microsoft_preferences']); $items[] = \html_writer::link($connecturl, $connectstr, ['class' => 'servicelink block_microsoft_connection']); $downloadlinks = $this->get_content_o365download(); foreach ($downloadlinks as $link) { $items[] = $link; } $html .= \html_writer::alist($items); return $html; }