-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.php
261 lines (235 loc) · 8.78 KB
/
lib.php
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
<?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/>.
/**
* Convenient wrappers and helper for using the X-Ray web service API.
*
* @package local_xray
* @author Pablo Pagnone
* @author German Vitale
* @author Darko Miletic
* @author David Castro <[email protected]>
* @copyright Copyright (c) 2017 Open LMS (https://www.openlms.net)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Generate list of report links according to the current page
* Result is returned as associative array ($reportname => $reporturl)
*
* @param moodle_page $page
* @param context $context
* @return array
* @throws coding_exception
*/
function local_xray_navigationlinks(moodle_page $page, context $context) {
global $CFG;
static $reports = null;
// Course selection check.
if (!\local_xray\local\api\course_manager::is_xray_course($page->course->id)) {
return $reports;
}
// Small caching to prevent double calculation call since we need the same information in both calls.
// The forums in home page should not display the X-ray link.
if (!is_null($reports) || $page->course->format == "singleactivity" || ($context->contextlevel < CONTEXT_COURSE) ||
($page->course->id == SITEID) || !has_capability('local/xray:view', $context)) {
return $reports;
}
$reports = [];
$extraparams = [];
require_once($CFG->dirroot.'/local/xray/locallib.php');
if (!local_xray_risk_disabled()) {
$reportlist['courseadmin']['risk'] = 'local/xray:risk_view';
}
$reportlist['courseadmin']['activityreport'] = 'local/xray:activityreport_view';
$reportlist['courseadmin']['gradebookreport'] = 'local/xray:gradebookreport_view';
$reportlist['courseadmin']['discussionreport'] = 'local/xray:discussionreport_view';
if (in_array($page->pagetype, ['mod-forum-view', 'mod-hsuforum-view',
'mod-forum-discuss', 'mod-hsuforum-discuss'])) {
if (local_xray_reports()) {
$extraparams['forumid'] = $page->cm->instance;
} else {
$extraparams['forum'] = $page->cm->instance;
}
$extraparams['cmid' ] = $context->instanceid;
// Support for discussion of forum/hsforum.
$d = $page->url->get_param('d');
if (!empty($d)) {
$extraparams['d'] = $d;
}
$reportlist = [
'modulesettings' => [
'discussionreportindividualforum' => 'local/xray:discussionreportindividualforum_view',
]
];
}
$baseurl = new moodle_url('/local/xray/view.php');
foreach ($reportlist as $nodename => $reportsublist) {
foreach ($reportsublist as $report => $capability) {
if (has_capability($capability, $context)) {
require_once($CFG->dirroot.'/local/xray/locallib.php');
if (local_xray_reports()) {
$reports[$nodename][$report] = $baseurl->out(false, ['controller' => 'xrayreports',
'name' => local_xray_name_conversion($report),
'courseid' => $page->course->id,
'action' => 'view'] + $extraparams);
} else {
$reports[$nodename][$report] = $baseurl->out(false, ['controller' => $report,
'courseid' => $page->course->id,
'action' => 'view'] + $extraparams);
}
}
}
}
return $reports;
}
/**
* Extend navigations block.
*
* @param settings_navigation $settings
* @param context $context
* @return void
* @throws coding_exception
*/
function local_xray_extends_settings_navigation(settings_navigation $settings, context $context) {
global $PAGE;
$reports = local_xray_navigationlinks($PAGE, $context);
if (empty($reports)) {
return;
}
foreach ($reports as $nodename => $reportsublist) {
$coursenode = $settings->get($nodename);
if ($coursenode === false) {
continue;
}
$extranavigation = $coursenode->add(get_string('navigation_xray', 'local_xray'));
foreach ($reportsublist as $reportstring => $url) {
$extranavigation->add(get_string($reportstring, 'local_xray'), $url);
}
$extranavigation = null;
$coursenode = null;
}
}
/**
* This is the version of the JS that should be used up to Moodle 2.8
* New one will be required for Moodle 2.9+
*
* @param navigation_node $nav
* @return void
*/
function local_xray_extends_navigation($nav) {
global $PAGE;
($nav); // Just to remove unused param warning.
static $search = [
'topics' => '#region-main',
'weeks' => '#region-main',
'onetopic' => '#region-main',
'singleactivity' => '.notexist', // Not sure what to do here?
'social' => '#region-main',
'tabbedweek' => '#region-main',
'topcoll' => '#region-main',
];
$courseformat = $PAGE->course->format;
if (!isset($search[$courseformat])) {
$search[$courseformat] = '#region-main';
}
$reportview = ($PAGE->pagetype == 'local-xray-view');
$courseview = false;
if ($PAGE->has_set_url()) {
$courseview = $PAGE->url->compare(new moodle_url('/course/view.php', ['id' => $PAGE->course->id]), URL_MATCH_BASE);
if ($courseview) {
$buieditid = (int)optional_param('bui_editid' , 0, PARAM_INT);
$buihideid = (int)optional_param('bui_hideid' , 0, PARAM_INT);
$buidelid = (int)optional_param('bui_deleteid', 0, PARAM_INT);
if (!empty($buidelid) || !empty($buihideid) || !empty($buieditid)) {
$courseview = false;
}
}
}
if (!$reportview && !$courseview) {
return;
}
$menu = '';
if (!$reportview) {
$reportcontroller = optional_param('controller', '', PARAM_ALPHA);
$reports = local_xray_navigationlinks($PAGE, $PAGE->context);
/** @var local_xray_renderer $renderer */
$renderer = $PAGE->get_renderer('local_xray');
$menu = $renderer->print_course_menu($reportcontroller, $reports);
}
if (!empty($menu)) {
$menuappend = 0;
// Easy way to force include on every page (provided that navigation block is present).
$PAGE->requires->yui_module(['moodle-local_xray-custmenu'],
'M.local_xray.custmenu.init',
[[
'menusearch' => $search[$courseformat],
'menuappend' => $menuappend,
'items' => $menu,
]],
null,
false
);
}
}
/**
* Moodle 2.9+ compliant method.
*
* @link https://tracker.moodle.org/browse/MDL-49643 MDL-49643
* @param settings_navigation $settings
* @param context $context
*/
function local_xray_extend_settings_navigation(settings_navigation $settings, context $context) {
local_xray_extends_settings_navigation($settings, $context);
}
/**
* Moodle 2.9+ compliant method.
*
* @link https://tracker.moodle.org/browse/MDL-49643 MDL-49643
* @param navigation_node $nav
*/
function local_xray_extend_navigation($nav) {
local_xray_extends_navigation($nav);
}
/**
* Add the link in the profile user for subscriptions.
* @param \core_user\output\myprofile\tree $tree
* @param stdClass $user
* @param bool $iscurrentuser
* @param stdClass $course
* @return bool
*/
function local_xray_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
global $CFG;
($iscurrentuser);
($course);
require_once($CFG->dirroot.'/local/xray/locallib.php');
// Validate user.
if ((!has_capability("local/xray:globalsub_view", context_system::instance()) &&
!local_xray_is_teacher($user->id)) || !local_xray_email_enable()) {
return false;
}
// Url for subscription.
$subscriptionurl = new moodle_url("/local/xray/view.php", ["controller" => "globalsub"]);
$node = new core_user\output\myprofile\node(
'miscellaneous',
'local_xray',
get_string('profilelink', 'local_xray'),
null,
$subscriptionurl
);
$tree->add_node($node);
return true;
}