forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scales.php
472 lines (403 loc) · 18.1 KB
/
scales.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<?php // $Id$
// Allows a creator to edit custom scales, and also display help about scales
require_once("../config.php");
require_once("lib.php");
$id = required_param('id', PARAM_INT); // course id
$scaleid = optional_param('scaleid', 0, PARAM_INT); // scale id
$action = optional_param('action', 'undefined', PARAM_ALPHA); // action to execute
$name = optional_param('name', '', PARAM_CLEAN); // scale name
$description = optional_param('description', '', PARAM_CLEAN);// scale description
$list = optional_param('list', 0, PARAM_BOOL); // show listing in help window
if (! $course = get_record("course", "id", $id)) {
error("Course ID was incorrect");
}
require_login($course->id);
$context = get_context_instance(CONTEXT_COURSE, $course->id);
$strscale = get_string("scale");
$strscales = get_string("scales");
$strcustomscale = get_string("scalescustom");
$strstandardscale = get_string("scalesstandard");
$strcustomscales = get_string("scalescustom");
$strstandardscales = get_string("scalesstandard");
$strname = get_string("name");
$strdescription = get_string("description");
$strsavechanges = get_string("savechanges");
$strchangessaved = get_string("changessaved");
$strdeleted = get_string("deleted");
$strdelete = get_string("delete");
$stredit = get_string("edit");
$strdown = get_string("movedown");
$strup = get_string("moveup");
$strmoved = get_string("changessaved");
$srtcreatenewscale = get_string("scalescustomcreate");
$strhelptext = get_string("helptext");
$stractivities = get_string("activities");
$stroptions = get_string("action");
$strtype = get_string("group");
/// init this here so we can pass it by reference to every call to site_scale_used to avoid getting the courses out of the db over and over again
$courses = array();
/// If scale data is being submitted, then save it and continue
$focus = "";
$errors = NULL;
if ($action == 'sendform' and confirm_sesskey()) {
if ($form = data_submitted()) {
if (empty($form->name)) {
$errors[$scaleid]->name = true;
$focus = "form$scaleid.save";
}
if (empty($form->scalescale)) {
$errors[$scaleid]->scalescale = true;
$focus = "form$scaleid.save";
}
if (!$errors) {
$newscale=NULL;
$newscale->name = $form->name;
$newscale->scale = $form->scalescale;
$newscale->description = $form->description;
$newscale->courseid = $form->courseid;
$newscale->userid = $USER->id;
$newscale->timemodified = time();
if (empty($scaleid)) {
$newscale->courseid = $course->id;
if (!insert_record("scale", $newscale)) {
error("Could not insert the new scale!");
}
} else {
$newscale->id = $scaleid;
if (!update_record("scale", $newscale)) {
error("Could not update that scale!");
}
}
$notify = "$newscale->name: $strchangessaved";
$focus = "form$scaleid.save";
} else {
if (!empty($scaleid)) {
$action = "edit";
} else {
$action = "new";
}
}
}
}
//If action is details, show the popup info
if ($action == "details") {
//Check for teacher edit
require_capability('moodle/course:managescales', $context);
//Check for scale
if (! $scale = get_record("scale", "id", $scaleid)) {
error("Scale ID was incorrect");
}
// $scales_course_uses = course_scale_used($course->id,$scale->id);
// $scales_site_uses = site_scale_used($scale->id,$courses);
$scalemenu = make_menu_from_list($scale->scale);
print_header("$course->shortname: $strscales", "$course->fullname",
"$course->shortname -> $strscales -> $scale->name", "", "", true, " ", " ");
close_window_button();
echo "<p/>";
print_simple_box_start("center");
print_heading($scale->name);
echo "<center>";
choose_from_menu($scalemenu, "", "", "");
echo "</center>";
echo text_to_html($scale->description);
print_simple_box_end();
echo "<p/>";
close_window_button();
print_footer();
exit;
}
//If action is edit or new, show the form
if ($action == "edit" || $action == "new") {
$sesskey = !empty($USER->id) ? $USER->sesskey : '';
require_capability('moodle/course:managescales', $context);
//Check for scale if action = edit
if ($action == "edit") {
if (! $scale = get_record("scale", "id", $scaleid)) {
error("Scale ID was incorrect");
}
} else {
$scale = new object();
$scale->id = 0;
$scale->courseid = $course->id;
$scale->name = "";
$scale->scale = "";
$scale->description = "";
}
//Calculate the uses
if ($scale->courseid == 0) {
$scale_uses = site_scale_used($scale->id,$courses);
} else {
$scale_uses = course_scale_used($course->id,$scale->id);
}
//Check for scale_uses
if (!empty($scale_uses)) {
error("Scale is in use and cannot be modified",$CFG->wwwroot.'/course/scales.php?id='.$course->id);
}
//Check for standard scales
if ($scale->courseid == 0 and !has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
error("Only administrators can edit this scale",$CFG->wwwroot.'/course/scales.php?id='.$course->id);
}
//Print out the headers
print_header("$course->shortname: $strscales", "$course->fullname",
"<a href=\"view.php?id=$course->id\">$course->shortname</a>".
" -> <a href=\"scales.php?id=$course->id\">$strscales</a>".
" -> ".get_string("editinga","",$strscale), $focus);
//Title
print_heading_with_help($strscales, "scales");
if (!empty($errors) and ($form->scaleid == $scale->id)) {
$scale->name = $form->name;
$scale->scale = $form->scalescale;
$scale->description = $form->description;
}
echo "<form method=\"post\" action=\"scales.php\" id=\"form$scale->id\">";
echo "<table cellpadding=\"9\" cellspacing=\"0\" align=\"center\" class=\"generalbox\">";
echo "<tr valign=\"top\">";
if (!empty($errors[$scale->id]->name)) {
$class = "class=\"highlight\"";
} else {
$class = "";
}
echo "<td align=\"right\"><b>$strname:</b></td>";
echo "<td $class><input type=\"text\" name=\"name\" size=\"50\" value=\"".s($scale->name)."\" />";
echo "</td>";
echo "</tr>";
echo "<tr valign=\"top\">";
if (!empty($errors[$scale->id]->scalescale)) {
$class = "class=\"highlight\"";
} else {
$class = "";
}
echo "<td align=\"right\"><b>$strscale:</b></td>";
echo "<td $class><textarea name=\"scalescale\" cols=\"50\" rows=\"2\" wrap=\"virtual\">".s($scale->scale)."</textarea>";
echo "</td>";
echo "</tr>";
echo "<tr valign=\"top\">";
echo "<td align=\"right\"><b>$strdescription:</b>";
helpbutton("text", $strhelptext);
echo "</td>";
echo "<td><textarea name=\"description\" cols=\"50\" rows=\"8\" wrap=\"virtual\">".s($scale->description)."</textarea>";
echo "</td>";
echo "</tr>";
if ($scale->id) {
echo "<tr valign=\"top\">";
echo "<td align=\"right\">";
echo "</td>";
echo "<td>".get_string("usedinnplaces","",$scale_uses);
echo "</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td colspan=\"2\" align=\"center\">";
echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
echo "<input type=\"hidden\" name=\"sesskey\" value=\"$sesskey\" />";
echo "<input type=\"hidden\" name=\"courseid\" value=\"$scale->courseid\" />";
echo "<input type=\"hidden\" name=\"scaleid\" value=\"$scale->id\" />";
echo "<input type=\"hidden\" name=\"action\" value=\"sendform\" />";
echo "<input type=\"submit\" name=\"save\" value=\"$strsavechanges\" />";
echo "</td></tr></table>";
echo "</form>";
echo "<br />";
print_footer($course);
exit;
}
//If action is delete, do it
if ($action == "delete" and confirm_sesskey()) {
//Check for teacher edit
require_capability('moodle/course:managescales', $context);
//Check for scale if action = edit
if (! $scale = get_record("scale", "id", $scaleid)) {
error("Scale ID was incorrect");
}
//Calculate the uses
if ($scale->courseid == 0) {
$scale_uses = site_scale_used($scale->id,$courses);
} else {
$scale_uses = course_scale_used($course->id,$scale->id);
}
//Check for scale_uses
if (!empty($scale_uses)) {
error("Scale is in use and cannot be deleted",$CFG->wwwroot.'/course/scales.php?id='.$course->id);
}
//Check for standard scales
if ($scale->courseid == 0 and !has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
error("Only administrators can delete this scale",$CFG->wwwroot.'/course/scales.php?id='.$course->id);
}
if (delete_records("scale", "id", $scaleid)) {
$notify = "$scale->name: $strdeleted";
}
}
//If action is down or up, do it
if (($action == "down" || $action == "up") and confirm_sesskey()) {
//Check for teacher edit
require_capability('moodle/course:managescales', $context);
//Check for scale if action = edit
if (! $scale = get_record("scale", "id", $scaleid)) {
error("Scale ID was incorrect");
}
//Calculate the uses
if ($scale->courseid == 0) {
$scale_uses = site_scale_used($scale->id,$courses);
} else {
$scale_uses = course_scale_used($course->id,$scale->id);
}
//Check for scale_uses
if (!empty($scale_uses)) {
error("Scale is in use and cannot be moved",$CFG->wwwroot.'/course/scales.php?id='.$course->id);
}
if ($action == "down") {
$scale->courseid = 0;
} else {
$scale->courseid = $course->id;
}
if (set_field("scale", "courseid", $scale->courseid, "id", $scale->id)) {
$notify = "$scale->name: $strmoved";
}
}
if ($list) { /// Just list the scales (in a helpwindow)
require_capability('moodle/course:viewscales', $context);
print_header($strscales);
if (!empty($scaleid)) {
if ($scale = get_record("scale", "id", "$scaleid")) {
$scalemenu = make_menu_from_list($scale->scale);
print_simple_box_start("center");
print_heading($scale->name);
echo "<center>";
choose_from_menu($scalemenu, "", "", "");
echo "</center>";
echo text_to_html($scale->description);
print_simple_box_end();
}
echo "<br />";
close_window_button();
exit;
}
if ($scales = get_records("scale", "courseid", "$course->id", "name ASC")) {
print_heading($strcustomscales);
if (has_capability('moodle/course:managescales', get_context_instance(CONTEXT_COURSE, $course->id))) {
echo "<p align=\"center\">(";
print_string("scalestip");
echo ")</p>";
}
foreach ($scales as $scale) {
$scalemenu = make_menu_from_list($scale->scale);
print_simple_box_start("center");
print_heading($scale->name);
echo "<center>";
choose_from_menu($scalemenu, "", "", "");
echo "</center>";
echo text_to_html($scale->description);
print_simple_box_end();
echo "<hr />";
}
} else {
if (has_capability('moodle/course:managescales', $context)) {
echo "<p align=\"center\">(";
print_string("scalestip");
echo ")</p>";
}
}
if ($scales = get_records("scale", "courseid", "0", "name ASC")) {
print_heading($strstandardscales);
foreach ($scales as $scale) {
$scalemenu = make_menu_from_list($scale->scale);
print_simple_box_start("center");
print_heading($scale->name);
echo "<center>";
choose_from_menu($scalemenu, "", "", "");
echo "</center>";
echo text_to_html($scale->description);
print_simple_box_end();
echo "<hr />";
}
}
close_window_button();
exit;
}
/// The rest is all about editing the scales
require_capability('moodle/course:managescales', $context);
/// Print out the main page
print_header("$course->shortname: $strscales", "$course->fullname",
"<a href=\"view.php?id=$course->id\">$course->shortname</a>
-> $strscales");
print_heading_with_help($strscales, "scales");
$options = array();
$options['id'] = $course->id;
$options['scaleid'] = 0;
$options['action'] = 'new';
print_simple_box_start('center');
print_single_button($CFG->wwwroot.'/course/scales.php',$options,$srtcreatenewscale,'POST');
print_simple_box_end();
echo "<p />";
if (!empty($notify)) {
notify($notify, "green");
}
$scales = array();
$customscales = get_records("scale", "courseid", "$course->id", "name ASC");
$standardscales = get_records("scale", "courseid", "0", "name ASC");
if ($customscales) {
foreach($customscales as $scale) {
$scales[] = $scale;
}
}
if ($standardscales) {
foreach($standardscales as $scale) {
$scales[] = $scale;
}
}
if ($scales) {
//Calculate the base path
$path = "$CFG->wwwroot/course";
$data = array();
$incustom = true;
foreach($scales as $scale) {
//Check the separator
if (empty($scale->courseid) && $incustom) {
$incustom = false;
$line = "hr";
$data[] = $line;
}
$line = array();
$line[] = "<a target=\"scale\" title=\"$scale->name\" href=\"$CFG->wwwroot/course/scales.php?id=$course->id&scaleid=$scale->id&action=details\" "."onclick=\"return openpopup('/course/scales.php?id=$course->id\&scaleid=$scale->id&action=details', 'scale', 'menubar=0,location=0,scrollbars,resizable,width=600,height=450', 0);\">".$scale->name."</a><br /><font size=\"-1\">".str_replace(",",", ",$scale->scale)."</font>";
if (!empty($scale->courseid)) {
$scales_uses = course_scale_used($course->id,$scale->id);
} else {
$scales_uses = site_scale_used($scale->id,$courses);
}
$line[] = $scales_uses;
if ($incustom) {
$line[] = $strcustomscale;
} else {
$line[] = $strstandardscale;
}
$buttons = "";
if (empty($scales_uses) && ($incustom || has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM, SITEID)))) {
$buttons .= "<a title=\"$stredit\" href=\"$path/scales.php?id=$course->id&scaleid=$scale->id&action=edit\"><img".
" src=\"$CFG->pixpath/t/edit.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\" alt=\"$stredit\" /></a> ";
if ($incustom && has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
$buttons .= "<a title=\"$strdown\" href=\"$path/scales.php?id=$course->id&scaleid=$scale->id&action=down&sesskey=$USER->sesskey\"><img".
" src=\"$CFG->pixpath/t/down.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\" alt=\"$strdown\" /></a> ";
}
if (!$incustom && has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
$buttons .= "<a title=\"$strup\" href=\"$path/scales.php?id=$course->id&scaleid=$scale->id&action=up&sesskey=$USER->sesskey\"><img".
" src=\"$CFG->pixpath/t/up.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\" alt=\"$strup\" /></a> ";
}
$buttons .= "<a title=\"$strdelete\" href=\"$path/scales.php?id=$course->id&scaleid=$scale->id&action=delete&sesskey=$USER->sesskey\"><img".
" src=\"$CFG->pixpath/t/delete.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\" alt=\"$strdelete\" /></a> ";
}
$line[] = $buttons;
$data[] = $line;
}
$head = $strscale.",".$stractivities.",".$strtype.",".$stroptions;
$table->head = explode(",",$head);
$size = "50%,20%,20%,10%";
$table->size = explode(",",$size);
$align = "left,center,center,center";
$table->align = explode(",",$align);
$wrap = ",nowrap,nowrap,nowrap";
$table->wrap = explode(",",$wrap);
$table->width = "90%";
$table->data = $data;
print_table ($table);
}
print_footer($course);
?>