forked from microsoft/moodle-block_microsoft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock_microsoft.php
More file actions
482 lines (420 loc) · 20.1 KB
/
block_microsoft.php
File metadata and controls
482 lines (420 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package block_microsoft
* @author James McQuillan <james.mcquillan@remote-learner.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright (C) 2014 onwards Microsoft, Inc. (http://microsoft.com/)
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/blocks/microsoft/lib.php');
require_once($CFG->dirroot.'/auth/oidc/lib.php');
require_once($CFG->dirroot.'/local/o365/lib.php');
/**
* Microsoft Block.
*/
class block_microsoft extends block_base {
/**
* Initialize plugin.
*/
public function init() {
$this->title = get_string('microsoft', 'block_microsoft');
$this->globalconfig = get_config('block_microsoft');
}
/**
* Whether the block has settings.
*
* @return bool Has settings or not.
*/
public function has_config() {
return true;
}
/**
* Get the content of the block.
*
* @return stdObject
*/
public function get_content() {
global $USER, $DB;
if (!isloggedin()) {
return null;
}
if ($this->content !== null) {
return $this->content;
}
$this->content = new \stdClass;
$this->content->text = '';
$this->content->footer = '';
try {
$o365connected = \local_o365\utils::is_o365_connected($USER->id);
if ($o365connected === true) {
$this->content->text .= $this->get_content_connected();
} else {
$connection = $DB->get_record('local_o365_connections', ['muserid' => $USER->id]);
if (!empty($connection) && (auth_oidc_connectioncapability($USER->id, 'connect') ||
local_o365_connectioncapability($USER->id, 'link'))) {
$uselogin = (!empty($connection->uselogin)) ? true : false;
$this->content->text .= $this->get_content_matched($connection->aadupn, $uselogin);
} else {
$this->content->text .= $this->get_content_notconnected();
}
}
} catch (\Exception $e) {
$this->content->text = $e->getMessage();
}
return $this->content;
}
/**
* Get block content for an unconnected but matched user.
*
* @param string $o365account The o365 account the user was matched to.
* @param bool $uselogin Whether the match includes login change.
* @return string Block content.
*/
protected function get_content_matched($o365account, $uselogin = false) {
$html = '';
$langmatched = get_string('o365matched_title', 'block_microsoft');
$html .= '<h5>'.$langmatched.'</h5>';
$langmatcheddesc = get_string('o365matched_desc', 'block_microsoft', $o365account);
$html .= '<p>'.$langmatcheddesc.'</p>';
$langlogin = get_string('logintoo365', 'block_microsoft');
$html .= '<p>'.get_string('o365matched_complete_authreq', 'block_microsoft').'</p>';
if ($uselogin === true) {
$html .= '<p>'.\html_writer::link(new \moodle_url('/local/o365/ucp.php'), $langlogin).'</p>';
} else {
$html .= '<p>'.\html_writer::link(new \moodle_url('/local/o365/ucp.php?action=connecttoken'), $langlogin).'</p>';
}
return $html;
}
/**
* Get links to study groups.
*
* @return string Returns string containing links to study groups.
*/
protected function get_study_groups() {
global $PAGE, $USER;
$groupsenabled = \local_o365\feature\usergroups\utils::is_enabled();
$iscoursecontext = $PAGE->context instanceof \context_course && $PAGE->context->instanceid !== SITEID;
$courseisgroupenabled = \local_o365\feature\usergroups\utils::course_is_group_enabled($PAGE->context->instanceid);
$config = (array)get_config('block_microsoft');
$items = [];
if ($iscoursecontext && $groupsenabled && $courseisgroupenabled) {
$canmanage = (has_capability('local/o365:managegroups', $PAGE->context) === true) ? true : false;
$canview = (is_enrolled($PAGE->context) && has_capability('local/o365:viewgroups', $PAGE->context)) ? true : false;
if ($canmanage === true || $canview === true) {
if (!empty($config['settings_showcoursegroup'])) {
$coursegroupurl = new \moodle_url('/local/o365/groupcp.php', ['courseid' => $PAGE->context->instanceid]);
$managecoursegroupstr = get_string('linkcoursegroup', 'block_microsoft');
$coursegroupattrs = ['class' => 'servicelink block_microsoft_coursegroup'];
$items[] = \html_writer::link($coursegroupurl, $managecoursegroupstr, $coursegroupattrs);
}
//if (!empty($config['settings_showstudygroups'])) {
// Temporarily removed pending further design work.
//$studygroups = \local_o365\feature\usergroups\utils::study_groups_list($USER->id, ['courseid' => $PAGE->context->instanceid], false, 0, 5);
//$items = array_merge($items, $studygroups);
//}
}
}
return $items;
}
/**
* 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>';
$odburl = get_config('local_o365', 'odburl');
$o365object = $DB->get_record('local_o365_objects', ['type' => 'user', 'moodleid' => $USER->id]);
if (!empty($o365object) && !empty($o365object->metadata)) {
$metadata = json_decode($o365object->metadata, true);
if (!empty($metadata['odburl'])) {
$odburl = $metadata['odburl'];
}
}
if (!empty($odburl) && !empty($this->globalconfig->settings_showmydelve)) {
if (!empty($o365object)) {
$delveurl = 'https://'.$odburl.'/_layouts/15/me.aspx?u='.$o365object->objectid.'&v=work';
}
}
if (!empty($user->picture)) {
$html .= '<div class="profilepicture">';
$picturehtml = $OUTPUT->user_picture($user, array('size' => 100, 'class' => 'block_microsoft_profile'));
$profileurl = new \moodle_url('/user/profile.php', ['id' => $USER->id]);
if (!empty($delveurl)) {
// If "My Delve" is enabled, clicking the user picture should take you to their Delve page.
$picturehtml = str_replace($profileurl->out(), $delveurl, $picturehtml);
}
$html .= $picturehtml;
$html .= '</div>';
}
$items = [];
$items = array_merge($items, $this->get_study_groups());
$userupn = \local_o365\utils::get_o365_upn($USER->id);
if ($PAGE->context instanceof \context_course && $PAGE->context->instanceid !== SITEID) {
// Course SharePoint Site.
if (!empty($this->globalconfig->settings_showcoursespsite) && !empty($o365config->sharepointlink)) {
$sharepointstr = get_string('linksharepoint', 'block_microsoft');
$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/>';
}
}
}
}
// My Delve URL
if (!empty($delveurl)) {
$delveattrs = ['class' => 'servicelink block_microsoft_delve', 'target' => '_blank'];
$delvestr = get_string('linkmydelve', 'block_microsoft');
$items[] = html_writer::link($delveurl, $delvestr, $delveattrs);
}
// My email.
if (!empty($this->globalconfig->settings_showemail)) {
$emailurl = 'https://outlook.office365.com/';
$emailattrs = ['class' => 'servicelink block_microsoft_outlook', 'target' => '_blank'];
$emailstr = get_string('linkemail', 'block_microsoft');
$items[] = \html_writer::link($emailurl, $emailstr, $emailattrs);
}
// My Forms URL.
if (!empty($this->globalconfig->settings_showmyforms)) {
$formsattrs = ['class' => 'servicelink block_microsoft_forms', 'target' => '_blank'];
$formsstr = get_string('linkmyforms', 'block_microsoft');
$formsurl = get_string('settings_showmyforms_default', 'block_microsoft');
if (!empty($odburl)) {
$items[] = \html_writer::link($formsurl, $formsstr, $formsattrs);
}
}
// My OneNote Notebook.
$items[] = $this->render_onenote();
// My OneDrive.
if (!empty($this->globalconfig->settings_showonedrive)) {
$odbattrs = [
'target' => '_blank',
'class' => 'servicelink block_microsoft_onedrive',
];
$stronedrive = get_string('linkonedrive', 'block_microsoft');
if (!empty($odburl)) {
$items[] = \html_writer::link('https://'.$odburl, $stronedrive, $odbattrs);
}
}
// Microsoft Stream.
if (!empty($this->globalconfig->settings_showmsstream)) {
$streamurl = 'https://web.microsoftstream.com/?noSignUpCheck=1';
$streamattrs = ['target' => '_blank', 'class' => 'servicelink block_microsoft_msstream'];
$items[] = \html_writer::link($streamurl, get_string('linkmsstream', 'block_microsoft'), $streamattrs);
}
// Microsoft Teams.
if (!empty($this->globalconfig->settings_showmsteams)) {
$teamsurl = 'https://teams.microsoft.com/_';
$teamsattrs = ['target' => '_blank', 'class' => 'servicelink block_microsoft_msteams'];
$items[] = \html_writer::link($teamsurl, get_string('linkmsteams', 'block_microsoft'), $teamsattrs);
}
// My Sways.
if (!empty($this->globalconfig->settings_showsways) && !empty($userupn)) {
$swayurl = 'https://www.sway.com/my?auth_pvr=OrgId&auth_upn='.$userupn;
$swayattrs = ['target' => '_blank', 'class' => 'servicelink block_microsoft_sway'];
$items[] = \html_writer::link($swayurl, get_string('linksways', 'block_microsoft'), $swayattrs);
}
// Configure Outlook Sync.
if (!empty($this->globalconfig->settings_showoutlooksync)) {
$outlookurl = new \moodle_url('/local/o365/ucp.php?action=calendar');
$outlookstr = get_string('linkoutlook', 'block_microsoft');
$items[] = \html_writer::link($outlookurl, $outlookstr, ['class' => 'servicelink block_microsoft_outlook']);
}
// Preferences.
if (!empty($this->globalconfig->settings_showpreferences)) {
$prefsurl = new \moodle_url('/local/o365/ucp.php');
$prefsstr = get_string('linkprefs', 'block_microsoft');
$items[] = \html_writer::link($prefsurl, $prefsstr, ['class' => 'servicelink block_microsoft_preferences']);
}
if (auth_oidc_connectioncapability($USER->id, 'connect') === true || auth_oidc_connectioncapability($USER->id, 'disconnect') === true ||
local_o365_connectioncapability($USER->id, 'link') || local_o365_connectioncapability($USER->id, 'unlink')) {
if (!empty($this->globalconfig->settings_showmanageo365conection)) {
$connecturl = new \moodle_url('/local/o365/ucp.php', ['action' => 'connection']);
$connectstr = get_string('linkconnection', 'block_microsoft');
$items[] = \html_writer::link($connecturl, $connectstr, ['class' => 'servicelink block_microsoft_connection']);
}
}
// Download Office 365.
$downloadlinks = $this->get_content_o365download();
foreach ($downloadlinks as $link) {
$items[] = $link;
}
$html .= \html_writer::alist($items);
return $html;
}
/**
* Get block content for unconnected users.
*
* @return string Block content.
*/
protected function get_content_notconnected() {
global $DB, $USER, $OUTPUT;
$html = '<h5>'.get_string('notconnected', 'block_microsoft').'</h5>';
$connecturl = new \moodle_url('/local/o365/ucp.php');
$connectstr = get_string('connecttoo365', 'block_microsoft');
$items = [];
$items = array_merge($items, $this->get_study_groups());
if (auth_oidc_connectioncapability($USER->id, 'connect') === true || local_o365_connectioncapability($USER->id, 'link')) {
if (!empty($this->globalconfig->settings_showo365connect)) {
$items[] = \html_writer::link($connecturl, $connectstr, ['class' => 'servicelink block_microsoft_connection']);
}
}
$items[] = $this->render_onenote();
$downloadlinks = $this->get_content_o365download();
foreach ($downloadlinks as $link) {
$items[] = $link;
}
$html .= \html_writer::alist($items);
return $html;
}
/**
* Get Office 365 download links (if enabled).
*
* @return array Array of download link HTML, or empty array if download links disabled.
*/
protected function get_content_o365download() {
if (empty($this->globalconfig->showo365download)) {
return [];
}
$url = get_config('block_microsoft', 'settings_geto365link');
$str = get_string('geto365', 'block_microsoft');
return [
\html_writer::link($url, $str, ['class' => 'servicelink block_microsoft_downloado365', 'target' => '_blank']),
];
}
/**
* Get the user's Moodle OneNote Notebook.
*
* @param \local_onenote\api\base $onenoteapi A constructed OneNote API to use.
* @return array Array of information about the user's OneNote notebook used for Moodle.
*/
protected function get_onenote_notebook(\local_onenote\api\base $onenoteapi) {
$moodlenotebook = null;
for ($i = 0; $i < 2; $i++) {
$notebooks = $onenoteapi->get_items_list('');
if (!empty($notebooks)) {
$notebookname = get_string('notebookname', 'block_microsoft');
foreach ($notebooks as $notebook) {
if ($notebook['title'] == $notebookname) {
$moodlenotebook = $notebook;
break;
}
}
}
if (empty($moodlenotebook)) {
$onenoteapi->sync_notebook_data();
} else {
break;
}
}
return $moodlenotebook;
}
/**
* Render OneNote section of the block.
*
* @return string HTML for the rendered OneNote section of the block.
*/
protected function render_onenote() {
global $USER, $PAGE;
if (empty($this->globalconfig->settings_showonenotenotebook)) {
return '';
}
if (!class_exists('\local_onenote\api\base')) {
$url = new \moodle_url('http://portal.office.com/onenote');
$stropennotebook = get_string('linkonenote', 'block_microsoft');
$linkattrs = [
'onclick' => 'window.open(this.href,\'_blank\'); return false;',
'class' => 'servicelink block_microsoft_onenote',
];
return \html_writer::link($url->out(false), $stropennotebook, $linkattrs);
}
$action = optional_param('action', '', PARAM_TEXT);
try {
$onenoteapi = \local_onenote\api\base::getinstance();
$output = '';
if ($onenoteapi->is_logged_in()) {
// Add the "save to onenote" button if we are on an assignment page.
$onassignpage = ($PAGE->cm && $PAGE->cm->modname == 'assign' && $action == 'editsubmission') ? true : false;
if ($onassignpage === true && $onenoteapi->is_student($PAGE->cm->id, $USER->id)) {
$workstr = get_string('workonthis', 'block_microsoft');
$output .= $onenoteapi->render_action_button($workstr, $PAGE->cm->id).'<br /><br />';
}
// Find moodle notebook, create if not found.
$moodlenotebook = null;
$cache = \cache::make('block_microsoft', 'onenotenotebook');
$moodlenotebook = $cache->get($USER->id);
if (empty($moodlenotebook)) {
$moodlenotebook = $this->get_onenote_notebook($onenoteapi);
$result = $cache->set($USER->id, $moodlenotebook);
}
if (!empty($moodlenotebook)) {
$url = new \moodle_url($moodlenotebook['url']);
$stropennotebook = get_string('linkonenote', 'block_microsoft');
$linkattrs = [
'onclick' => 'window.open(this.href,\'_blank\'); return false;',
'class' => 'servicelink block_microsoft_onenote',
];
$output .= \html_writer::link($url->out(false), $stropennotebook, $linkattrs);
} else {
$output .= get_string('error_nomoodlenotebook', 'block_microsoft');
}
} else {
if (\local_o365\utils::is_configured_msaccount()) {
$output .= $this->render_signin_widget($onenoteapi->get_login_url());
}
}
return $output;
} catch (\Exception $e) {
if (class_exists('\local_o365\utils')) {
\local_o365\utils::debug($e->getMessage(), 'block_microsoft', $e);
}
return '<span class="block_microsoft_onenote servicelink">'.get_string('linkonenote_unavailable', 'block_microsoft')
.'<br /><small>'.get_string('contactadmin', 'block_microsoft').'</small></span>';
}
}
/**
* Get the HTML for the sign in button for an MS account.
*
* @return string HTML containing the sign in widget.
*/
public function render_signin_widget($loginurl) {
$loginstr = get_string('msalogin', 'block_microsoft');
$attrs = [
'onclick' => 'window.open(this.href,\'mywin\',\'left=20,top=20,width=500,height=500,toolbar=1,resizable=0\'); return false;',
'class' => 'servicelink block_microsoft_msasignin'
];
return \html_writer::link($loginurl, $loginstr, $attrs);
}
}