-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.php
More file actions
167 lines (144 loc) · 6.2 KB
/
lib.php
File metadata and controls
167 lines (144 loc) · 6.2 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
<?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/>.
/**
* This file contains functions used by the local contactlist plugin.
*
* @package local_contactlist
* @copyright 2020 University of Vienna
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Adds the contactlist to the course navigation
*
* @param navigation_node $navigation
* @param stdClass $course
* @param context_course $context
* @return void
* @throws coding_exception
* @throws moodle_exception
*/
function local_contactlist_extend_navigation_course(navigation_node $navigation, stdClass $course, context_course $context) {
global $DB, $CFG;
if (!has_capability('local/contactlist:view', $context) || $CFG->version < 2022041900) {
return;
}
$params = [
'name' => 'Privacy Settings',
'shortname' => 'conlistcoursevis',
'instanceid' => $context->instanceid,
];
$sql = "SELECT cfd.intvalue FROM {customfield_data} cfd
JOIN {customfield_field} cff ON cfd.fieldid = cff.id
JOIN {customfield_category} cfc ON cff.categoryid = cfc.id
WHERE cfc.name = :name
AND cff.shortname = :shortname
AND cfd.instanceid = :instanceid";
$customfielddatavalue = $DB->get_field_sql($sql, $params);
if ($customfielddatavalue == 2) {
return;
}
$url = new moodle_url('/local/contactlist/studentview.php', ['id' => $course->id]);
$title = get_string('nodename', 'local_contactlist');
$pix = new pix_icon('t/addcontact', $title);
$newnode = navigation_node::create($title, $url, navigation_node::TYPE_SETTING, 'contactlist',
'contactlist', $pix);
$navigation->add_node($newnode);
}
/**
* This function extends the navigation with the contactlist item
*
* @param navigation_node $navigation
*/
function local_contactlist_extend_navigation($navigation) {
global $USER, $PAGE, $DB, $CFG;
if (empty($USER->id) || $CFG->version >= 2022041900) {
return;
}
if ('admin-index' === $PAGE->pagetype) {
if (!($DB->record_exists('capabilities', ['name' => 'local/contactlist:view']))) {
return;
}
}
$context = context::instance_by_id($PAGE->context->id);
if (!($context instanceof context_course || $context instanceof context_module)) {
return;
}
$coursecontext = $context instanceof context_module ? $context->get_course_context() : $context;
if (!has_capability('local/contactlist:view', $coursecontext, $USER)) {
return;
}
$params = [
'name' => 'Privacy Settings',
'shortname' => 'conlistcoursevis',
'instanceid' => $coursecontext->instanceid,
];
$sql = "SELECT cfd.intvalue FROM {customfield_data} cfd
JOIN {customfield_field} cff ON cfd.fieldid = cff.id
JOIN {customfield_category} cfc ON cff.categoryid = cfc.id
WHERE cfc.name = :name
AND cff.shortname = :shortname
AND cfd.instanceid = :instanceid";
$customfielddatavalue = $DB->get_field_sql($sql, $params);
if (!$customfielddatavalue || $customfielddatavalue == 1) {
$rootnodes = [
$navigation->find('mycourses', navigation_node::TYPE_ROOTNODE),
$navigation->find('courses', navigation_node::TYPE_ROOTNODE),
];
foreach ($rootnodes as $mycoursesnode) {
if (empty($mycoursesnode)) {
continue;
}
$beforekey = null;
$participantsnode = $mycoursesnode->find('participants', navigation_node::TYPE_CONTAINER);
if ($participantsnode) { // Add the navnode after participants.
$keys = $participantsnode->parent->get_children_key_list();
$igrades = array_search('participants', $keys);
if ($igrades !== false) {
if (isset($keys[$igrades + 1])) {
$beforekey = $keys[$igrades + 1];
}
}
}
if ($beforekey == null) { // No participants node found, fall back to other variants!
$activitiesnode = $mycoursesnode->find('activitiescategory', navigation_node::TYPE_CATEGORY);
if ($activitiesnode == false) {
$custom = $mycoursesnode->find_all_of_type(navigation_node::TYPE_CUSTOM);
$sections = $mycoursesnode->find_all_of_type(navigation_node::TYPE_SECTION);
if (!empty($custom)) {
$first = reset($custom);
$beforekey = $first->key;
} else if (!empty($sections)) {
$first = reset($sections);
$beforekey = $first->key;
}
} else {
$beforekey = 'activitiescategory';
}
}
$url = new moodle_url('/local/contactlist/studentview.php', ['id' => $coursecontext->instanceid]);
$title = get_string('nodename', 'local_contactlist');
$pix = new pix_icon('t/addcontact', $title);
$childnode = navigation_node::create($title, $url, navigation_node::TYPE_CUSTOM, 'contactlist', 'contactlist', $pix);
if (($mycoursesnode !== false && $mycoursesnode->has_children())) {
$currentcoursenode = $mycoursesnode->find($coursecontext->instanceid, navigation_node::TYPE_COURSE);
if ($currentcoursenode) {
$currentcoursenode->add_node($childnode, $beforekey);
break;
}
}
}
}
}