-
Notifications
You must be signed in to change notification settings - Fork 2
/
locallib.php
290 lines (246 loc) · 10.6 KB
/
locallib.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
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
<?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/>.
/**
* Internal functions used to help manage the Relationship CRUD interface
*
* @package local_relationship
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/user/selector/lib.php');
require_once($CFG->dirroot . '/tag/lib.php');
function relationship_set_header($context, $url, $relationship=null, $module=null) {
global $PAGE, $COURSE, $DB;
if ($context->contextlevel != CONTEXT_COURSECAT) {
print_error('invalidcontext');
}
$category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
$navtitle = get_string('relationships', 'local_relationship');
$PAGE->set_pagelayout('standard');
$PAGE->set_context($context);
$PAGE->set_url($url);
$PAGE->set_heading($COURSE->fullname);
$PAGE->set_title($navtitle);
$PAGE->navbar->add($category->name, new moodle_url('/course/index.php', array('categoryid'=>$category->id)));
$PAGE->navbar->add($navtitle, new moodle_url('/local/relationship/index.php', array('contextid'=>$context->id)));
if($module) {
$PAGE->navbar->add(get_string($module, 'local_relationship'),
new moodle_url("/local/relationship/{$module}.php", array('relationshipid'=>$relationship->id)));
}
}
function relationship_set_title($relationship=null, $action=null, $param=null) {
global $OUTPUT;
echo $OUTPUT->header();
if($relationship) {
echo $OUTPUT->heading(get_string('relationship', 'local_relationship') . ': ' . format_string($relationship->name));
echo html_writer::empty_tag('BR');
}
if($action) {
echo $OUTPUT->heading(get_string($action, 'local_relationship', $param), '4');
}
}
function relationship_groups_parse_name($format, $value, $value_is_a_name=false) {
if($value_is_a_name) {
if (strstr($format, '@') !== false) {
$str = str_replace('@', $value, $format);
} else {
$str = str_replace('#', $value, $format);
}
} else {
if (strstr($format, '@') !== false) { // Convert $value to a character series
$letter = 'A';
for($i=0; $i<$value; $i++) {
$letter++;
}
$str = str_replace('@', $letter, $format);
} else { // Convert $value to a number series
$str = str_replace('#', $value+1, $format);
}
}
return($str);
}
function relationship_get_role_options() {
$all_roles = role_get_names();
$ctx_roles = get_roles_for_contextlevels(CONTEXT_COURSE);
$roles = array();
foreach($ctx_roles AS $id=>$roleid) {
if($roleid > 2) {
$roles[$roleid] = $all_roles[$roleid]->localname;
}
}
asort($roles);
return $roles;
}
function relationship_get_cohort_options($relationshipid) {
global $DB;
$relationship = $DB->get_record('relationship', array('id' => $relationshipid), '*', MUST_EXIST);
$context = context::instance_by_id($relationship->contextid);
$contextids = array();
foreach($context->get_parent_context_ids(true) as $ctxid) {
$context = context::instance_by_id($ctxid);
if (has_capability('moodle/cohort:view', $context)) {
$contextids[] = $ctxid;
}
}
list($in_sql, $params) = $DB->get_in_or_equal($contextids, SQL_PARAMS_NAMED);
$sql = "SELECT id, name FROM {cohort} WHERE contextid {$in_sql} ORDER BY name";
return $DB->get_records_sql_menu($sql, $params);
}
function relationship_get_courses($relationshipid) {
global $DB;
$sql = "SELECT DISTINCT c.id, c.shortname, c.fullname
FROM {enrol} e
JOIN {course} c ON (c.id = e.courseid)
WHERE e.enrol = 'relationship'
AND e.customint1 = :relationshipid
ORDER BY c.fullname";
return $DB->get_records_sql($sql, array('relationshipid'=>$relationshipid));
}
// Nomes de outros grupos que não os do próprio relacionamento
function relationship_get_other_courses_group_names($relationshipid) {
global $DB;
$sql = "SELECT DISTINCT g.name, e.courseid, c.fullname
FROM {enrol} e
JOIN {course} c ON (c.id = e.courseid)
JOIN {groups} g ON (g.courseid = e.courseid)
WHERE e.enrol = 'relationship'
AND e.customint1 = :relationshipid
AND g.idnumber NOT LIKE 'relationship_{$relationshipid}_%'
ORDER BY g.name";
$groups = array();
foreach($DB->get_records_sql($sql, array('relationshipid'=>$relationshipid)) as $r) {
$groups[$r->name][] = $r;
}
return $groups;
}
function relationship_is_in_use($relationshipid) {
global $DB;
return $DB->record_exists('enrol', array('enrol'=>'relationship', 'customint1'=>$relationshipid));
}
function relationship_courses_where_is_in_use($relationshipid) {
global $DB;
$sql = "SELECT DISTINCT c.id, c.shortname, c.fullname
FROM {enrol} e
JOIN {course} c ON (c.id = e.courseid)
WHERE e.enrol = 'relationship'
AND e.customint1 = :relationshipid";
return $DB->get_records_sql($sql, array('relationshipid'=>$relationshipid));
}
/**
* Somehow deal with relationships when deleting course category,
* we can not just delete them because they might be used in enrol
* plugins or referenced in external systems.
* @param stdClass|coursecat $category
* @return void
*/
function relationship_delete_category($category) {
global $DB;
// TODO: make sure that relationships are really, really not used anywhere and delete, for now just move to parent or system context
$oldcontext = context_coursecat::instance($category->id);
if ($category->parent and $parent = $DB->get_record('course_categories', array('id'=>$category->parent))) {
$parentcontext = context_coursecat::instance($parent->id);
$sql = "UPDATE {relationship} SET contextid = :newcontext WHERE contextid = :oldcontext";
$params = array('oldcontext'=>$oldcontext->id, 'newcontext'=>$parentcontext->id);
} else {
$syscontext = context_system::instance();
$sql = "UPDATE {relationship} SET contextid = :newcontext WHERE contextid = :oldcontext";
$params = array('oldcontext'=>$oldcontext->id, 'newcontext'=>$syscontext->id);
}
$DB->execute($sql, $params);
}
/**
* Get all the relationships defined in given context.
*
* @param int $contextid
* @param int $page number of the current page
* @param int $perpage items per page
* @param string $search search string
* @return array Array(totalrelationships => int, relationships => array, allrelationships => int)
*/
function relationship_search_relationships($contextid, $page = 0, $perpage = 25, $search = '') {
global $DB;
// Add some additional sensible conditions
$tests = array('contextid = ?');
$params = array($contextid);
if (!empty($search)) {
$conditions = array('name', 'idnumber', 'description');
$searchparam = '%' . $DB->sql_like_escape($search) . '%';
foreach ($conditions as $key=>$condition) {
$conditions[$key] = $DB->sql_like($condition, "?", false);
$params[] = $searchparam;
}
$tests[] = '(' . implode(' OR ', $conditions) . ')';
}
$wherecondition = implode(' AND ', $tests);
$fields = "SELECT *";
$countfields = "SELECT COUNT(1)";
$sql = " FROM {relationship}
WHERE $wherecondition";
$order = " ORDER BY name ASC";
$allrelationships = $DB->count_records('relationship', array('contextid'=>$contextid));
$totalrelationships = $DB->count_records_sql($countfields . $sql, $params);
$relationships = $DB->get_records_sql($fields . $sql . $order, $params, $page*$perpage, $perpage);
foreach($relationships as $rl) {
$rl->tags = core_tag_tag::get_item_tags_array('relationship', 'relationship', $rl->id);
}
return array('totalrelationships' => $totalrelationships, 'relationships' => $relationships, 'allrelationships'=>$allrelationships);
}
/**
* Get a single relationshipcohort by shortname role
*
* @param $relationshipid
* @param $roleshortname
* @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found;
* IGNORE_MULTIPLE means return first, ignore multiple records found(not recommended);
* MUST_EXIST means throw exception if no record or multiple records found
* @return mixed
* @throws coding_exception
*/
function relationship_get_cohort_by_roleshortname($relationshipid, $roleshortname, $strictness=IGNORE_MISSING) {
global $DB;
$sql = "SELECT rc.*, ch.name, ch.component, r.name rolename
FROM {relationship_cohorts} rc
JOIN {cohort} ch ON (ch.id = rc.cohortid)
JOIN {role} r ON (r.id = rc.roleid)
WHERE rc.relationshipid = :relationshipid
AND r.shortname = :roleshortname";
$rc = $DB->get_record_sql($sql, array('relationshipid' => $relationshipid, 'roleshortname' => $roleshortname), $strictness);
return $rc;
}
function relationship_render_buttons($buttons) {
global $OUTPUT;
$text = '';
foreach ($buttons AS $btn) {
$text .= $OUTPUT->render($btn);
}
$text = html_writer::tag('div', $text, array('class' => 'buttons'));
return $OUTPUT->box($text, 'generalbox', 'notice');
}
function relationship_print_tabs($url, $tabs, $action='') {
global $OUTPUT;
if (is_array($tabs) && count($tabs) > 1) {
$tabsobject = array();
foreach ($tabs as $tab) {
$urltab = clone($url);
$urltab->param('action', $tab);
$tabsobject[$tab] = new tabobject($tab, $urltab, get_string($tab, 'local_relationship'));
}
$action = isset($tabsobject[$action]) ? $action : reset($tabs);
if (count($tabsobject) > 1) {
echo $OUTPUT->tabtree($tabsobject, $action);
}
}
}