forked from mudrd8mz/moodle-mod_stampcoll
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.php
274 lines (240 loc) · 8.49 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
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php // $Id$
/// MODULE CONSTANTS //////////////////////////////////////////////////////
/**
* Default number of students per page
*/
define('STAMPCOLL_USERS_PER_PAGE', 30);
/**
* Default stamp image URL
*/
define('STAMPCOLL_IMAGE_URL', $CFG->wwwroot.'/mod/stampcoll/defaultstamp.gif');
/// MODULE FUNCTIONS //////////////////////////////////////////////////////
/**
* @todo Documenting this function. Capabilities checking
*/
function stampcoll_user_outline($course, $user, $mod, $stampcoll) {
if ($stamps = get_records_select("stampcoll_stamps", "userid=$user->id AND stampcollid=$stampcoll->id")) {
$result = new stdClass();
$result->info = get_string('numberofcollectedstamps', 'stampcoll', count($stamps));
$result->time = 0; // empty
return $result;
}
return NULL;
}
/**
* @todo Documenting this function
*/
function stampcoll_user_complete($course, $user, $mod, $stampcoll) {
global $USER;
$context = get_context_instance(CONTEXT_MODULE, $mod->id);
if ($USER->id == $user->id) {
if (!has_capability('mod/stampcoll:viewownstamps', $context)) {
echo get_string('notallowedtoviewstamps', 'stampcoll');
return true;
}
} else {
if (!has_capability('mod/stampcoll:viewotherstamps', $context)) {
echo get_string('notallowedtoviewstamps', 'stampcoll');
return true;
}
}
if (!$allstamps = stampcoll_get_stamps($stampcoll->id)) {
// no stamps yet in this instance
echo get_string('nostampscollected', "stampcoll");
return true;
}
$userstamps = array();
foreach ($allstamps as $s) {
if ($s->userid == $user->id) {
$userstamps[] = $s;
}
}
unset($allstamps);
unset($s);
if (empty($userstamps)) {
echo get_string('nostampscollected', 'stampcoll');
} else {
echo get_string('numberofcollectedstamps', 'stampcoll', count($userstamps));
echo '<div class="stamppictures">';
foreach ($userstamps as $s) {
echo stampcoll_stamp($s, $stampcoll->image);
}
echo '</div>';
unset($s);
}
}
/**
* Create a new instance of stamp collection and return the id number.
*
* @param object $stampcoll Object containing data defined by the form in mod.html
* @return int ID number of the new instance
*/
function stampcoll_add_instance($stampcoll) {
$stampcoll->timemodified = time();
$stampcoll->text = trim($stampcoll->text);
return insert_record("stampcoll", $stampcoll);
}
/**
* Update an existing instance of stamp collection with new data.
*
* @param object $stampcoll Object containing data defined by the form in mod.html
* @return boolean
*/
function stampcoll_update_instance($stampcoll) {
$stampcoll->id = $stampcoll->instance;
$stampcoll->timemodified = time();
$stampcoll->text = trim($stampcoll->text);
return update_record('stampcoll', $stampcoll);
}
/**
* Delete the instance of stamp collection and any data that depends on it.
*
* @param int $id ID of an instance to be deleted
* @return bool
*/
function stampcoll_delete_instance($id) {
if (! $stampcoll = get_record("stampcoll", "id", "$id")) {
return false;
}
$result = true;
if (! delete_records("stampcoll_stamps", "stampcollid", "$stampcoll->id")) {
$result = false;
}
if (! delete_records("stampcoll", "id", "$stampcoll->id")) {
$result = false;
}
return $result;
}
/**
* Return the users with data in one stamp collection.
*
* Return users with records in stampcoll_stamps.
*
* @uses $CFG
* @param int $stampcollid ID of an module instance
* @return array Array of unique users
*/
function stampcoll_get_participants($stampcollid) {
global $CFG;
$students = get_records_sql("SELECT DISTINCT u.id, u.id
FROM {$CFG->prefix}user u,
{$CFG->prefix}stampcoll_stamps s
WHERE s.stampcollid = '$stampcollid' AND
u.id = s.userid");
return ($students);
}
/**
* Get all users who can collect stamps in the given Stamp Collection
*
* Returns array of users with the capability mod/stampcoll:collectstamps. Caller may specify the group.
* If groupmembersonly used, do not return users who are not in any group.
*
* @uses $CFG;
* @param object $cm Course module record
* @param object $context Current context
* @param int $currentgroup ID of group the users must be in
* @return array Array of users
*/
function stampcoll_get_users_can_collect($cm, $context, $currentgroup=false) {
global $CFG;
$users = get_users_by_capability($context, 'mod/stampcoll:collectstamps', 'u.id,u.picture,u.firstname,u.lastname',
'', '', '', $currentgroup, '', false, true);
/// If groupmembersonly used, remove users who are not in any group
/// XXX this has not been tested yet !!!
if ($users && !empty($CFG->enablegroupings) && $cm->groupmembersonly) {
if ($groupingusers = groups_get_grouping_members($cm->groupingid, 'u.id,u.picture,u.firstname,u.lastname' )) {
$users = array_intersect($users, $groupingusers);
}
}
return $users;
}
/**
* Return full record of the stamp collection.
*
* @param int $stampcallid ID of an module instance
* @return object Object containing instance data
*/
function stampcoll_get_stampcoll($stampcollid) {
return get_record("stampcoll", "id", $stampcollid);
}
/**
* Return all stamps in module instance.
*
* @param int $stamcallid ID of an module instance
* @return array|false Array of found stamps (as objects) or false if no stamps or error occured
*/
function stampcoll_get_stamps($stampcollid) {
return get_records("stampcoll_stamps", "stampcollid", $stampcollid, "id");
}
/**
* Return one stamp.
*
* @param int $stamid ID of an stamp record
* @return object|false Found stamp (as object) or false if not such stamp or error occured
*/
function stampcoll_get_stamp($stampid) {
return get_record("stampcoll_stamps", "id", $stampid);
}
/**
* Return HTML displaying the hoverable stamp image.
*
* @param int $stamp The stamp object
* @param string $image The value of stampcollection image or absolute path to the file
* @param bool $tooltip Show stamp details when mouse hover
* @param bool $anonymous Hide the author of the stamp
* @param string $imagaeurl Optional: use <img scr="$imageurl"> instead of $image
* @return string HTML code displaying the image
*/
function stampcoll_stamp($stamp, $image='', $tooltip=true, $anonymous=false, $imageurl=null) {
global $CFG, $COURSE;
$image_location = $CFG->dataroot . '/'. $COURSE->id . '/'. $image;
if (empty($image) || $image == 'default' || !file_exists($image_location)) {
if ($imageurl) {
$src = $imageurl;
} else {
$src = STAMPCOLL_IMAGE_URL;
}
} else {
if ($CFG->slasharguments) {
$src = $CFG->wwwroot . '/file.php/' . $COURSE->id . '/' . $image;
} else {
$src = $CFG->wwwroot . '/file.php?file=/' . $COURSE->id . '/' . $image;
}
}
$alt = get_string('stampimage', 'stampcoll');
$date = userdate($stamp->timemodified);
if (!empty($stamp->giver) && $tooltip && !$anonymous) {
$author = fullname(get_record('user', 'id', $stamp->giver, '', '', '', '', 'lastname,firstname')). '<br />';
$author = get_string('givenby', 'stampcoll', $author);
} else {
$author = '';
}
if ($tooltip) {
$recepient = fullname(get_record('user', 'id', $stamp->userid, '', '', '', '', 'lastname,firstname')). '<br />';
$recepient = get_string('givento', 'stampcoll', $recepient);
} else {
$recepient = '';
}
$caption = $author . $recepient . $date;
$comment = format_string($stamp->text);
$tooltip_start = '<a class="stampimagewrapper" href="javascript:void(0);"
onmouseover="return overlib(\'' . s($comment) . '\', CAPTION, \'' . s($caption) . '\' );"
onmouseout="nd();">';
$tooltip_end = '</a>';
$img = '<img class="stampimage" src="' . $src . '" alt="'. $alt .'" />';
if ($tooltip) {
return $tooltip_start . $img . $tooltip_end;
} else {
return $popup;
}
}
/**
* Returns installed module version
*
* @return int Version defined in the module's version.php
*/
function stampcoll_modversion() {
require(dirname(__FILE__).'/version.php');
return $module->version;
}
?>