-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlocallib.php
384 lines (304 loc) · 10.4 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
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
<?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 library of functions for stamp collection module
*
* @package mod_stampcoll
* @copyright 2011 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Full-featured Stamps collection module API
*
* This wraps the stampcoll database record with a set of methods that are called
* from the module itself. The class should be initialized right after you get
* $stampcoll, $cm and $course records at the begining of the script.
*/
class stampcoll {
/** default number of users per page when displaying reports */
const USERS_PER_PAGE = 30;
/** @var stdClass course module record */
public $cm;
/** @var stdClass course record */
public $course;
/** @var stdClass context object */
public $context;
/** @var int stampcoll instance identifier */
public $id;
/** @var string stampcoll activity name */
public $name;
/** @var string introduction or description of the activity */
public $intro;
/** @var int format of the {@link $intro} */
public $introformat;
/** @var null|string null to use the default image name, the file name otherwise */
public $image;
/** @var int the last modification time stamps */
public $timemodified;
/** @var bool should the users without any stamp be listed, too */
public $displayzero;
/**
* Initializes the stampcoll API instance using the data from DB
*
* Makes deep copy of all passed records properties. Replaces integer $course attribute
* with a full database record (course should not be stored in instances table anyway).
*
* @param stdClass $dbrecord stamps collection instance data from the {stampcoll} table
* @param stdClass|cm_info $cm course module record as returned by {@link get_coursemodule_from_id()}
* @param stdClass $course course record from {course} table
* @param stdClass $context the context of the stampcoll instance
*/
public function __construct(stdClass $dbrecord, stdClass $cm, stdClass $course, stdClass $context=null) {
foreach ($dbrecord as $field => $value) {
if (property_exists('stampcoll', $field)) {
$this->{$field} = $value;
}
}
$this->cm = $cm;
$this->course = $course;
if (is_null($context)) {
$this->context = context_module::instance($this->cm->id);
} else {
$this->context = $context;
}
}
/**
* @return moodle_url to view the stampcoll instance
*/
public function view_url() {
return new moodle_url('/mod/stampcoll/view.php', array('id' => $this->cm->id));
}
/**
* @return moodle_url to manage stamps in this stampcoll instance
*/
public function managestamps_url() {
return new moodle_url('/mod/stampcoll/managestamps.php', array('cmid' => $this->cm->id));
}
}
/**
* Represents a single stamp to be rendered
*/
class stampcoll_stamp implements renderable {
/** @var stampcoll stampcoll instance */
public $stampcoll;
/** @var int */
public $id;
/** @var int */
public $holderid;
/** @var int|null */
public $giverid = null;
/** @var int|null */
public $modifierid = null;
/** @var string */
public $text = null;
/** @var int */
public $timecreated = null;
/** @var int|null */
public $timemodified = null;
/**
* Maps the data from the database record to the instance properties
*
* @param stampcoll $stampcoll module instance
* @param stdClass $record database record from mdl_stamcoll_stamps table
*/
public function __construct(stampcoll $stampcoll, stdClass $record) {
$this->stampcoll = $stampcoll;
if (!isset($record->id) or !isset($record->userid)) {
throw new coding_exception('the stamp record must provide id and userid');
}
$this->id = $record->id;
$this->holderid = $record->userid;
$this->timecreated = $record->timecreated;
if (isset($record->giver)) {
$this->giverid = $record->giver;
}
if (isset($record->modifier)) {
$this->modifierid = $record->modifier;
}
if (isset($record->text)) {
$this->text = $record->text;
}
if (isset($record->timemodified)) {
$this->timemodified = $record->timemodified;
}
}
}
/**
* Base collection of stamps to be displayed
*/
abstract class stampcoll_collection {
/** @var stampcoll module instance */
public $stampcoll;
/** @var array internal list of registered users */
protected $users = array();
/** @var array internal list of stamp holder ids, in order they should appear */
protected $holderids = array();
/** @var array internal list of stamps, indexed by the holder */
protected $stamps = array();
/**
* The base constructor
*
* @param stampcoll $stampcoll module instance
*/
public function __construct(stampcoll $stampcoll) {
$this->stampcoll = $stampcoll;
}
/**
* Registers stamp holder information
*
* @param stdClass $userinfo {@see self::register_user() for expected fields}
* @return int|bool
*/
public function register_holder(stdClass $userinfo) {
if (!isset($userinfo->id)) {
throw new coding_exception('userinfo must define user id');
}
if (!in_array($userinfo->id, $this->holderids)) {
$this->holderids[] = $userinfo->id;
}
return $this->register_user($userinfo);
}
/**
* Register general user (stamp holder or giver) information
*
* @param stdClass $userinfo {@see user_picture::unalias() for expected fields}
* @return int|bool
*/
public function register_user(stdClass $userinfo) {
if (!isset($userinfo->id)) {
throw new coding_exception('userinfo must define user id');
}
if (isset($this->users[$userinfo->id])) {
return 1;
}
$this->users[$userinfo->id] = $userinfo;
return true;
}
/**
* Returns the previously registered user info
*
* @param mixed $userid
* @return stdClass|bool false if has not been registered, object otherwise
*/
public function get_user_info($userid) {
if (!isset($this->users[$userid])) {
return false;
}
return $this->users[$userid];
}
/**
* Adds given stamp to the collection
*
* @param stdClass $stamp
* @return void
*/
public function add_stamp(stdClass $stamp) {
if (!isset($stamp->userid)) {
throw new coding_exception('stamp object must define user id');
}
if (!isset($this->stamps[$stamp->userid])) {
$this->stamps[$stamp->userid] = array();
}
$this->stamps[$stamp->userid][] = $stamp;
}
/**
* Returns the previously added stamps for the given holder
*
* @param int $holderid the user id
* @return array of {@link stampcoll_stamp} instances
*/
public function list_stamps($holderid) {
if (!isset($this->stamps[$holderid])) {
return array();
}
$stamps = array();
foreach ($this->stamps[$holderid] as $record) {
$stamps[] = new stampcoll_stamp($this->stampcoll, $record);
}
return $stamps;
}
}
/**
* Collection of single user's stamps
*/
class stampcoll_singleuser_collection extends stampcoll_collection implements renderable {
/** @var int user id of the holder of the collection */
private $holderid = null;
/**
* Registers the holder of the collection
*
* @param stampcoll $stamcoll module instance
* @param stdClass $holder user object
*/
public function __construct(stampcoll $stampcoll, stdClass $holder) {
parent::__construct($stampcoll);
$this->register_user($holder);
$this->holderid = $holder->id;
}
/**
* Returns the information of the collection holder
*
* @return stdClass
*/
public function get_holder() {
return $this->get_user_info($this->holderid);
}
}
/**
* Collection of multiple users' stamps
*/
class stampcoll_multiuser_collection extends stampcoll_collection implements renderable {
/** @var string how are the data sorted */
public $sortedby = 'lastname';
/** @var string how are the data sorted */
public $sortedhow = 'ASC';
/** @var int page number to display */
public $page = 0;
/** @var int number of stamo holders to display per page */
public $perpage = 30;
/** @var int the total number or stamp holders to display */
public $totalcount = null;
/**
* Initializes the list of users to display
*
* Users data must be provided by subsequential calls of {@see register_user()}.
*
* @param stampcoll $stamcoll module instance
* @param array $holderids ordered list of user ids
*/
public function __construct(stampcoll $stampcoll, array $holderids = array()) {
parent::__construct($stampcoll);
$this->holderids = $holderids;
}
/**
* Returns the list of stamp holders in order they were registered
*
* @return array of stdClass
*/
public function list_stamp_holders() {
$holders = array();
foreach ($this->holderids as $holderid) {
$holders[] = $this->users[$holderid];
}
return $holders;
}
}
/**
* Collection of multiple users' stamps used at the managestamps.php screen
*/
class stampcoll_management_collection extends stampcoll_multiuser_collection implements renderable {
}