-
Notifications
You must be signed in to change notification settings - Fork 86
/
locallib.php
338 lines (295 loc) · 12 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
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
<?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/>.
/**
* General library for the scheduler module.
*
* @package mod_scheduler
* @copyright 2011 Henning Bostelmann and others (see README.txt)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/filelib.php');
require_once(dirname(__FILE__).'/customlib.php');
/* Events related functions */
/**
* Will delete calendar events for a given scheduler slot, and not complain if the record does not exist.
* The only argument this function requires is the complete database record of a scheduler slot.
* @param object $slot the slot instance
* @uses $DB
* @return bool true if success, false otherwise
*/
function scheduler_delete_calendar_events($slot) {
global $DB;
$teachereventtype = "SSsup:{$slot->id}";
$studenteventtype = "SSstu:{$slot->id}";
$teacherdeletionsuccess = $DB->delete_records('event', array('eventtype' => $teachereventtype));
$studentdeletionsuccess = $DB->delete_records('event', array('eventtype' => $studenteventtype));
return ($teacherdeletionsuccess && $studentdeletionsuccess);
// This return may not be meaningful if the delete records functions do not return anything meaningful.
}
/**
* Prints a summary of a user in a nice little box.
*
* @uses $CFG
* @uses $USER
* @param user $user A {@see $USER} object representing a user
* @param course $course A {@see $COURSE} object representing a course
* @param bool $messageselect whether to include a checkbox to select the user
* @param bool $return whether the HTML fragment is to be returned as a string (otherwise printed)
* @return string HTML fragment, if so selected
*/
function scheduler_print_user($user, $course, $messageselect=false, $return=false) {
global $CFG, $USER, $OUTPUT;
$output = '';
static $string;
static $datestring;
static $countries;
$context = context_course::instance($course->id);
if (isset($user->context->id)) {
$usercontext = $user->context;
} else {
$usercontext = context_user::instance($user->id);
}
if (empty($string)) { // Cache all the strings for the rest of the page.
$string = new stdClass();
$string->email = get_string('email');
$string->lastaccess = get_string('lastaccess');
$string->activity = get_string('activity');
$string->loginas = get_string('loginas');
$string->fullprofile = get_string('fullprofile');
$string->role = get_string('role');
$string->name = get_string('name');
$string->never = get_string('never');
$datestring = new stdClass();
$datestring->day = get_string('day');
$datestring->days = get_string('days');
$datestring->hour = get_string('hour');
$datestring->hours = get_string('hours');
$datestring->min = get_string('min');
$datestring->mins = get_string('mins');
$datestring->sec = get_string('sec');
$datestring->secs = get_string('secs');
$datestring->year = get_string('year');
$datestring->years = get_string('years');
}
// Get the hidden field list.
if (has_capability('moodle/course:viewhiddenuserfields', $context)) {
$hiddenfields = array();
} else {
$hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
}
$output .= '<table class="userinfobox">';
$output .= '<tr>';
$output .= '<td class="left side">';
$output .= $OUTPUT->user_picture($user, array('size' => 100));
$output .= '</td>';
$output .= '<td class="content">';
$output .= '<div class="username">'.fullname($user, has_capability('moodle/site:viewfullnames', $context)).'</div>';
$output .= '<div class="info">';
if (!empty($user->role) && ($user->role <> $course->teacher)) {
$output .= $string->role .': '. $user->role .'<br />';
}
$extrafields = scheduler_get_user_fields($user, $context);
foreach ($extrafields as $field) {
$output .= $field->title . ': ' . $field->value . '<br />';
}
if (!isset($hiddenfields['lastaccess'])) {
if ($user->lastaccess) {
$output .= $string->lastaccess .': '. userdate($user->lastaccess);
$output .= ' ('. format_time(time() - $user->lastaccess, $datestring) .')';
} else {
$output .= $string->lastaccess .': '. $string->never;
}
}
$output .= '</div></td><td class="links">';
// Link to blogs.
if ($CFG->bloglevel > 0) {
$output .= '<a href="'.$CFG->wwwroot.'/blog/index.php?userid='.$user->id.'">'.get_string('blogs', 'blog').'</a><br />';
}
// Link to notes.
if (!empty($CFG->enablenotes) && (has_capability('moodle/notes:manage', $context)
|| has_capability('moodle/notes:view', $context))) {
$output .= '<a href="'.$CFG->wwwroot.'/notes/index.php?course=' . $course->id. '&user='.$user->id.'">'.
get_string('notes', 'notes').'</a><br />';
}
if (has_capability('moodle/site:viewreports', $context) ||
has_capability('moodle/user:viewuseractivitiesreport', $usercontext)) {
$output .= '<a href="'. $CFG->wwwroot .'/course/user.php?id='. $course->id .'&user='. $user->id .'">'.
$string->activity .'</a><br />';
}
$output .= '<a href="'. $CFG->wwwroot .'/user/profile.php?id='. $user->id .'">'. $string->fullprofile .'...</a>';
if (!empty($messageselect)) {
$output .= '<br /><input type="checkbox" name="user'.$user->id.'" /> ';
}
$output .= '</td></tr></table>';
if ($return) {
return $output;
} else {
echo $output;
}
}
/**
* File browsing support class
*
* @copyright 2011 Henning Bostelmann and others (see README.txt)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class scheduler_file_info extends file_info {
/** @var stdClass Course object */
protected $course;
/** @var stdClass Course module object */
protected $cm;
/** @var array Available file areas */
protected $areas;
/** @var string File area to browse */
protected $filearea;
/** @var scheduler The scheduler that this file area refers to */
protected $scheduler;
/**
* Constructor
*
* @param file_browser $browser file_browser instance
* @param stdClass $course course object
* @param stdClass $cm course module object
* @param stdClass $context module context
* @param array $areas available file areas
* @param string $filearea file area to browse
*/
public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
parent::__construct($browser, $context);
$this->course = $course;
$this->cm = $cm;
$this->areas = $areas;
$this->filearea = $filearea;
$this->scheduler = scheduler::load_by_coursemodule_id($cm->id);
}
/**
* Returns list of standard virtual file/directory identification.
* The difference from stored_file parameters is that null values
* are allowed in all fields
* @return array with keys contextid, filearea, itemid, filepath and filename
*/
public function get_params() {
return array('contextid' => $this->context->id,
'component' => 'mod_scheduler',
'filearea' => $this->filearea,
'itemid' => null,
'filepath' => null,
'filename' => null);
}
/**
* Returns localised visible name.
* @return string
*/
public function get_visible_name() {
return $this->areas[$this->filearea];
}
/**
* Can I add new files or directories?
* @return bool
*/
public function is_writable() {
return false;
}
/**
* Is directory?
* @return bool
*/
public function is_directory() {
return true;
}
/**
* Returns list of children.
* @return array of file_info instances
*/
public function get_children() {
return $this->get_filtered_children('*', false, true);
}
/**
* Helper function to return files matching extensions or their count
*
* @param string|array $extensions either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
* @param bool|int $countonly if false returns the children, if an int returns just the
* count of children but stops counting when $countonly number of children is reached
* @param bool $returnemptyfolders if true returns items that don't have matching files inside
* @return array|int array of file_info instances or the count
* @uses $DB
*/
private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
global $DB;
$params = array('contextid' => $this->context->id,
'component' => 'mod_scheduler',
'filearea' => $this->filearea);
$sql = "SELECT DISTINCT f.itemid AS id
FROM {files} f
WHERE f.contextid = :contextid
AND f.component = :component
AND f.filearea = :filearea";
if (!$returnemptyfolders) {
$sql .= ' AND filename <> :emptyfilename';
$params['emptyfilename'] = '.';
}
list($sql2, $params2) = $this->build_search_files_sql($extensions, 'f');
$sql .= ' '.$sql2;
$params = array_merge($params, $params2);
$rs = $DB->get_recordset_sql($sql, $params);
$children = array();
foreach ($rs as $record) {
if ($child = $this->browser->get_file_info($this->context, 'mod_scheduler', $this->filearea, $record->id)) {
if ($returnemptyfolders || $child->count_non_empty_children($extensions)) {
$children[] = $child;
}
}
if ($countonly !== false && count($children) >= $countonly) {
break;
}
}
$rs->close();
if ($countonly !== false) {
return count($children);
}
return $children;
}
/**
* Returns list of children which are either files matching the specified extensions
* or folders that contain at least one such file.
*
* @param string|array $extensions either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
* @return array of file_info instances
*/
public function get_non_empty_children($extensions = '*') {
return $this->get_filtered_children($extensions, false);
}
/**
* Returns the number of children which are either files matching the specified extensions
* or folders containing at least one such file.
*
* @param string|array $extensions for example '*' or array('.gif','.jpg')
* @param int $limit stop counting after at least $limit non-empty children are found
* @return int
*/
public function count_non_empty_children($extensions = '*', $limit = 1) {
return $this->get_filtered_children($extensions, $limit);
}
/**
* Returns parent file_info instance
*
* @return file_info or null for root
*/
public function get_parent() {
return $this->browser->get_file_info($this->context);
}
}