forked from mudrd8mz/moodle-mod_stampcoll
-
Notifications
You must be signed in to change notification settings - Fork 1
/
editstamps.php
331 lines (280 loc) · 13.9 KB
/
editstamps.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
<?php // $Id$
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once(dirname(__FILE__).'/lib.php');
$id = required_param('id',PARAM_INT); // Course Module ID
$page = optional_param('page', 0, PARAM_INT); // Page of the batch view
if (! $cm = get_coursemodule_from_id('stampcoll', $id)) {
error("Course Module ID was incorrect");
}
if (! $course = get_record("course", "id", $cm->course)) {
error("Course is misconfigured");
}
require_login($course->id, false, $cm);
/// Get capabilities
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
include(dirname(__FILE__).'/caps.php');
if (!$cap_givestamps) {
// Illegal access to the page
error("You are not allowed to use this page");
}
if (!$stampcoll = stampcoll_get_stampcoll($cm->instance)) {
error("Course module is incorrect");
}
if (!$allstamps = stampcoll_get_stamps($stampcoll->id)) {
$allstamps = array();
}
/// First we check to see if the preferences form has just been submitted
/// to request user_preference updates
if (isset($_POST['updatepref'])){
$perpage = optional_param('perpage', STAMPCOLL_USERS_PER_PAGE, PARAM_INT);
$perpage = ($perpage <= 0) ? STAMPCOLL_USERS_PER_PAGE : $perpage ;
set_user_preference('stampcoll_perpage', $perpage);
if (isset($_POST['showupdateforms']) && $_POST['showupdateforms'] == "1") {
set_user_preference('stampcoll_showupdateforms', 1);
} else {
set_user_preference('stampcoll_showupdateforms', 0);
}
redirect("editstamps.php?id=$cm->id");
exit;
}
$strstampcoll = get_string("modulename", "stampcoll");
$strstampcolls = get_string("modulenameplural", "stampcoll");
$navigation = build_navigation(get_string('editstamps', 'stampcoll') , $cm);
print_header_simple(format_string($stampcoll->name), '',
$navigation, '', '', true,
update_module_button($cm->id, $course->id, $strstampcoll), navmenu($course, $cm));
/// Print the tabs
$currenttab = 'edit';
include(dirname(__FILE__).'/tabs.php');
/// Submit any new data if there is any
if (($form = data_submitted()) && $cap_givestamps ) {
if (isset($form->addstamp) and $form->addstamp == '1') {
if (!isset($form->sesskey) || !confirm_sesskey($form->sesskey)) {
error('Sesskey error');
}
$newstamp->stampcollid = $stampcoll->id;
$newstamp->userid = $form->userid;
if (!isset($form->text)) {
$form->text = '';
}
if (empty($stampcoll->anonymous)) {
$newstamp->giver = $USER->id;
}
$newstamp->text = $form->text;
$newstamp->timemodified = time();
if (! $newstamp->id = insert_record("stampcoll_stamps", $newstamp)) {
error("Could not save new stamp");
}
add_to_log($course->id, "stampcoll", "add stamp", "view.php?id=$cm->id", $newstamp->userid, $cm->id);
redirect("editstamps.php?id=$cm->id&page=$form->page");
exit;
}
if (isset($form->updatestamp) and $form->updatestamp == '1') {
if (!isset($form->sesskey) || !confirm_sesskey($form->sesskey)) {
error('Sesskey error');
}
$updatedstamp = stampcoll_get_stamp($form->stampid);
if (!($cap_managestamps || $updatedstamp->giver == $USER->id)) {
error('You are not allowed to update this stamp');
}
if (!isset($form->text)) {
$form->text = '';
}
$updatedstamp->text = $form->text;
$updatedstamp->timemodified = time();
if (! update_record("stampcoll_stamps", $updatedstamp)) {
error("Could not update stamp");
}
$updatedstamp = stampcoll_get_stamp($updatedstamp->id);
add_to_log($course->id, "stampcoll", "update stamp", "view.php?id=$cm->id", $updatedstamp->userid, $cm->id);
redirect("editstamps.php?id=$cm->id&page=$form->page");
exit;
}
if (isset($form->deletestamp)) {
if (! $cap_managestamps) {
error('You are not allowed to managestamps');
}
if (!isset($form->sesskey) || !confirm_sesskey($form->sesskey)) {
error('Sesskey error');
}
if (! $stamp = stampcoll_get_stamp($form->deletestamp)) {
error("Could not find stamp");
}
if (! delete_records("stampcoll_stamps", "id", $form->deletestamp)) {
error("Could not delete stamp");
}
add_to_log($course->id, "stampcoll", "delete stamp", "view.php?id=$cm->id", $stamp->userid, $cm->id);
if (isset($form->page)) {
redirect("editstamps.php?id=$cm->id&page=".$form->page);
} else {
redirect("editstamps.php?id=$cm->id");
}
}
}
/// Should be a stamp deleted?
if (isset($_GET['d']) && $cap_managestamps) {
if (!isset($_GET['sesskey']) || !confirm_sesskey($_GET['sesskey'])) {
error('Sesskey error');
}
if (! $stamp = stampcoll_get_stamp($_GET['d'])) {
error("Invalid stamp ID");
}
print_box_start();
print_heading(get_string("confirmdel", "stampcoll"));
$form = '<div align="center"><form name="delform" action="editstamps.php?id='.$cm->id.'" method="post">';
$form .= '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
$form .= '<input type="hidden" name="deletestamp" value="'.$stamp->id.'" />';
if (isset($_GET['page'])) {
$form .= '<input type="hidden" name="page" value="'.$_GET['page'].'" />';
}
$form .= '<input type="submit" value="'.get_string('yes').'" />';
$form .= '<input type="button" value="'.get_string('no').'" onclick="javascript:history.go(-1);" />';
$form .= '</form></div>';
echo $form;
print_box_end();
print_box_start('delstampbox');
echo '<div class="picture">'.stampcoll_stamp($stamp, $stampcoll->image).'</div>';
echo '<div class="comment">'.format_text($stamp->text).'</div>';
echo '<div class="timemodified">'.get_string('timemodified', 'stampcoll').': '.
userdate($stamp->timemodified).'</div>';
print_box_end();
print_footer($course);
exit;
}
/// Load all stamps into an array
$userstamps = array();
foreach ($allstamps as $s) {
$userstamps[$s->userid][] = $s;
}
unset($allstamps);
unset($s);
/// Groups and users
groups_print_activity_menu($cm, $CFG->wwwroot.'/mod/stampcol/editstamps.php?page='.$page.'&id='.$cm->id);
$currentgroup = groups_get_activity_group($cm);
$users = stampcoll_get_users_can_collect($cm, $context, $currentgroup);
if (!$users) {
print_heading(get_string("nousersyet"));
}
/// Get perpage param from database
$perpage = get_user_preferences('stampcoll_perpage', STAMPCOLL_USERS_PER_PAGE);
$showupdateforms = get_user_preferences('stampcoll_showupdateforms', 1);
$tablecolumns = array('picture', 'fullname', 'count', 'comment');
$tableheaders = array('', get_string('fullname'), get_string('numberofstamps', 'stampcoll'), '');
require_once($CFG->libdir.'/tablelib.php');
$table = new flexible_table('mod-stampcoll-editstamps');
$table->define_columns($tablecolumns);
$table->define_headers($tableheaders);
$table->define_baseurl($CFG->wwwroot.'/mod/stampcoll/editstamps.php?id='.$cm->id.'&currentgroup='.$currentgroup);
$table->sortable(true, 'lastname'); // default sort - do not use "count" here!
if (!$cap_viewotherstamps) {
// prevent sorting by stamps count and so guessing the number of them
$table->no_sorting('count');
}
$table->collapsible(false);
$table->initialbars(true);
$table->column_suppress('picture');
$table->column_suppress('fullname');
$table->column_class('picture', 'picture');
$table->column_class('fullname', 'fullname');
$table->column_class('count', 'count');
$table->column_class('comment', 'comment');
$table->set_attribute('cellspacing', '0');
$table->set_attribute('id', 'stamps');
$table->set_attribute('class', 'stamps');
$table->set_attribute('width', '90%');
$table->set_attribute('align', 'center');
$table->setup();
if (empty($users)) {
print_heading(get_string('nousers','stampcoll'));
return true;
}
/// Construct the SQL
if ($where = $table->get_sql_where()) {
$where .= ' AND ';
}
if ($sort = $table->get_sql_sort()) {
$sort = ' ORDER BY '.$sort;
}
$select = 'SELECT u.id, u.firstname, u.lastname, u.picture, COUNT(s.id) AS count ';
$sql = 'FROM '.$CFG->prefix.'user AS u '.
'LEFT JOIN '.$CFG->prefix.'stampcoll_stamps s ON u.id = s.userid AND s.stampcollid = '.$stampcoll->id.' '.
'WHERE '.$where.'u.id IN ('.implode(',', array_keys($users)).') GROUP BY u.id, u.firstname, u.lastname, u.picture ';
$table->pagesize($perpage, count($users));
if (($ausers = get_records_sql($select.$sql.$sort, $table->get_page_start(), $table->get_page_size())) !== false) {
foreach ($ausers as $auser) {
$picture = print_user_picture($auser->id, $course->id, $auser->picture, false, true);
$fullname = fullname($auser);
$count = '';
if ($auser->id == $USER->id && $cap_viewownstamps) {
$count = $auser->count;
}
if ($auser->id != $USER->id && $cap_viewotherstamps) {
$count = $auser->count;
}
$comment = '<form name="addform" action="editstamps.php?id='.$cm->id.'" method="post">';
$comment .= '<input name="text" type="text" size="35" maxlength="250" />';
$comment .= '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
$comment .= '<input type="hidden" name="userid" value="'.$auser->id.'" />';
$comment .= '<input type="hidden" name="page" value="'.$page.'" />';
$comment .= '<input type="hidden" name="addstamp" value="1" />';
$comment .= '<input type="submit" value="'.get_string('addstampbutton', 'stampcoll').'" /></form>';
$row = array($picture, $fullname, $count, $comment);
$table->add_data($row);
if ($cap_viewotherstamps && $showupdateforms && isset($userstamps[$auser->id])) {
foreach ($userstamps[$auser->id] as $userstamp) {
$count = stampcoll_stamp($userstamp, '', true, false, $CFG->pixpath.'/t/preview.gif');
$count .= ' ';
if ($cap_managestamps) {
$count .= '<a href="editstamps.php?id='.$cm->id.'&d='.$userstamp->id.'&sesskey='.sesskey().'&page='.$page.'" title="'.get_string('deletestamp', 'stampcoll').'">';
$count .= '<img src="'.$CFG->pixpath.'/t/delete.gif" height="11" width="11" border="0" alt="'.get_string('deletestamp', 'stampcoll').'" />';
$count .= '</a> ';
}
if ($cap_managestamps || ($userstamp->giver == $USER->id)) {
$comment = '<form name="updateform" action="editstamps.php?id='.$cm->id.'" method="post">';
$comment .= '<input name="text" type="text" size="35" maxlength="250" value="' . format_string($userstamp->text) . '" />';
$comment .= '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
$comment .= '<input type="hidden" name="stampid" value="'.$userstamp->id.'" />';
$comment .= '<input type="hidden" name="page" value="'.$page.'" />';
$comment .= '<input type="hidden" name="updatestamp" value="1" />';
$comment .= '<input type="submit" value="'.get_string('updatestampbutton', 'stampcoll').'" /></form>';
} else {
$comment = format_string($userstamp->text);
}
$row = array($picture, $fullname, $count, $comment);
$table->add_data($row);
}
}
}
}
$table->print_html(); /// Print the whole table
/// Mini form for setting user preference
echo '<br />';
echo '<form name="options" action="editstamps.php?id='.$cm->id.'" method="post">';
echo '<input type="hidden" id="updatepref" name="updatepref" value="1" />';
echo '<table id="optiontable" align="center">';
echo '<tr align="right"><td>';
echo '<label for="perpage">'.get_string('studentsperpage','stampcoll').'</label>';
echo ':</td>';
echo '<td align="left">';
echo '<input type="text" id="perpage" name="perpage" size="1" value="'.$perpage.'" />';
helpbutton('studentperpage', get_string('studentsperpage','stampcoll'), 'stampcoll');
echo '</td></tr>';
echo '<tr align="right"><td>';
echo '<label for="showupdateforms">'.get_string('showupdateforms','stampcoll').'</label>';
echo ':</td>';
echo '<td align="left">';
echo '<input type="checkbox" id="showupdateforms" name="showupdateforms" value="1" ';
if ($showupdateforms) {
echo 'checked="checked" ';
}
echo '/>';
helpbutton('showupdateforms', get_string('showupdateforms','stampcoll'), 'stampcoll');
echo '</td></tr>';
echo '<tr>';
echo '<td colspan="2" align="right">';
echo '<input type="submit" value="'.get_string('savepreferences').'" />';
echo '</td></tr></table>';
echo '</form>';
///End of mini form
print_footer($course);
?>