2 // Allows a creator to edit custom scales, and also display help about scales
4 require_once("../config.php");
5 require_once("lib.php");
7 $id = required_param('id', PARAM_INT
); // course id
8 $scaleid = optional_param('scaleid', 0, PARAM_INT
); // scale id
9 $action = optional_param('action', 'undefined', PARAM_ALPHA
); // action to execute
10 $name = optional_param('name', '', PARAM_CLEAN
); // scale name
11 $description = optional_param('description', '', PARAM_CLEAN
);// scale description
12 $list = optional_param('list', 0, PARAM_BOOL
); // show listing in help window
14 if (! $course = get_record("course", "id", $id)) {
15 error("Course ID was incorrect");
18 require_login($course->id
);
19 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
21 $strscale = get_string("scale");
22 $strscales = get_string("scales");
23 $strcustomscale = get_string("scalescustom");
24 $strstandardscale = get_string("scalesstandard");
25 $strcustomscales = get_string("scalescustom");
26 $strstandardscales = get_string("scalesstandard");
27 $strname = get_string("name");
28 $strdescription = get_string("description");
29 $strsavechanges = get_string("savechanges");
30 $strchangessaved = get_string("changessaved");
31 $strdeleted = get_string("deleted");
32 $strdelete = get_string("delete");
33 $stredit = get_string("edit");
34 $strdown = get_string("movedown");
35 $strup = get_string("moveup");
36 $strmoved = get_string("changessaved");
37 $srtcreatenewscale = get_string("scalescustomcreate");
38 $strhelptext = get_string("helptext");
39 $stractivities = get_string("activities");
40 $stroptions = get_string("action");
41 $strtype = get_string("group");
43 /// 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
46 /// If scale data is being submitted, then save it and continue
50 if ($action == 'sendform' and confirm_sesskey()) {
51 if ($form = data_submitted()) {
52 if (empty($form->name
)) {
53 $errors[$scaleid]->name
= true;
54 $focus = "form$scaleid.save";
56 if (empty($form->scalescale
)) {
57 $errors[$scaleid]->scalescale
= true;
58 $focus = "form$scaleid.save";
63 $newscale->name
= $form->name
;
64 $newscale->scale
= $form->scalescale
;
65 $newscale->description
= $form->description
;
66 $newscale->courseid
= $form->courseid
;
67 $newscale->userid
= $USER->id
;
68 $newscale->timemodified
= time();
70 if (empty($scaleid)) {
71 $newscale->courseid
= $course->id
;
72 if (!insert_record("scale", $newscale)) {
73 error("Could not insert the new scale!");
76 $newscale->id
= $scaleid;
77 if (!update_record("scale", $newscale)) {
78 error("Could not update that scale!");
82 $notify = "$newscale->name: $strchangessaved";
83 $focus = "form$scaleid.save";
85 if (!empty($scaleid)) {
94 //If action is details, show the popup info
95 if ($action == "details") {
96 //Check for teacher edit
97 require_capability('moodle/course:managescales', $context);
100 if (! $scale = get_record("scale", "id", $scaleid)) {
101 error("Scale ID was incorrect");
104 // $scales_course_uses = course_scale_used($course->id,$scale->id);
105 // $scales_site_uses = site_scale_used($scale->id,$courses);
106 $scalemenu = make_menu_from_list($scale->scale
);
108 print_header("$course->shortname: $strscales", $course->fullname
,
109 "$course->shortname -> $strscales -> $scale->name", "", "", true, " ", " ");
111 close_window_button();
114 print_simple_box_start("center");
115 print_heading($scale->name
);
117 choose_from_menu($scalemenu, "", "", "");
119 echo text_to_html($scale->description
);
120 print_simple_box_end();
123 close_window_button();
128 //If action is edit or new, show the form
129 if ($action == "edit" ||
$action == "new") {
131 $sesskey = !empty($USER->id
) ?
$USER->sesskey
: '';
133 require_capability('moodle/course:managescales', $context);
135 //Check for scale if action = edit
136 if ($action == "edit") {
137 if (! $scale = get_record("scale", "id", $scaleid)) {
138 error("Scale ID was incorrect");
141 $scale = new object();
143 $scale->courseid
= $course->id
;
146 $scale->description
= "";
150 if ($scale->courseid
== 0) {
151 $scale_uses = site_scale_used($scale->id
,$courses);
153 $scale_uses = course_scale_used($course->id
,$scale->id
);
156 //Check for scale_uses
157 if (!empty($scale_uses)) {
158 error("Scale is in use and cannot be modified",$CFG->wwwroot
.'/course/scales.php?id='.$course->id
);
161 //Check for standard scales
162 if ($scale->courseid
== 0 and !has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM
, SITEID
))) {
163 error("Only administrators can edit this scale",$CFG->wwwroot
.'/course/scales.php?id='.$course->id
);
166 //Print out the headers
167 print_header("$course->shortname: $strscales", $course->fullname
,
168 "<a href=\"view.php?id=$course->id\">$course->shortname</a>".
169 " -> <a href=\"scales.php?id=$course->id\">$strscales</a>".
170 " -> ".get_string("editinga","",$strscale), $focus);
173 print_heading_with_help($strscales, "scales");
175 if (!empty($errors) and ($form->scaleid
== $scale->id
)) {
176 $scale->name
= $form->name
;
177 $scale->scale
= $form->scalescale
;
178 $scale->description
= $form->description
;
180 echo "<form method=\"post\" action=\"scales.php\" id=\"form$scale->id\">";
181 echo "<table cellpadding=\"9\" cellspacing=\"0\" align=\"center\" class=\"generalbox\">";
182 echo "<tr valign=\"top\">";
183 if (!empty($errors[$scale->id
]->name
)) {
184 $class = "class=\"highlight\"";
188 echo "<td align=\"right\"><b>$strname:</b></td>";
189 echo "<td $class><input type=\"text\" name=\"name\" size=\"50\" value=\"".s($scale->name
)."\" />";
192 echo "<tr valign=\"top\">";
193 if (!empty($errors[$scale->id
]->scalescale
)) {
194 $class = "class=\"highlight\"";
198 echo "<td align=\"right\"><b>$strscale:</b></td>";
199 echo "<td $class><textarea name=\"scalescale\" cols=\"50\" rows=\"2\" wrap=\"virtual\">".s($scale->scale
)."</textarea>";
202 echo "<tr valign=\"top\">";
203 echo "<td align=\"right\"><b>$strdescription:</b>";
204 helpbutton("text", $strhelptext);
206 echo "<td><textarea name=\"description\" cols=\"50\" rows=\"8\" wrap=\"virtual\">".s($scale->description
)."</textarea>";
210 echo "<tr valign=\"top\">";
211 echo "<td align=\"right\">";
213 echo "<td>".get_string("usedinnplaces","",$scale_uses);
218 echo "<td colspan=\"2\" align=\"center\">";
219 echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
220 echo "<input type=\"hidden\" name=\"sesskey\" value=\"$sesskey\" />";
221 echo "<input type=\"hidden\" name=\"courseid\" value=\"$scale->courseid\" />";
222 echo "<input type=\"hidden\" name=\"scaleid\" value=\"$scale->id\" />";
223 echo "<input type=\"hidden\" name=\"action\" value=\"sendform\" />";
224 echo "<input type=\"submit\" name=\"save\" value=\"$strsavechanges\" />";
225 echo "</td></tr></table>";
229 print_footer($course);
234 //If action is delete, do it
235 if ($action == "delete" and confirm_sesskey()) {
236 //Check for teacher edit
237 require_capability('moodle/course:managescales', $context);
238 //Check for scale if action = edit
239 if (! $scale = get_record("scale", "id", $scaleid)) {
240 error("Scale ID was incorrect");
244 if ($scale->courseid
== 0) {
245 $scale_uses = site_scale_used($scale->id
,$courses);
247 $scale_uses = course_scale_used($course->id
,$scale->id
);
250 //Check for scale_uses
251 if (!empty($scale_uses)) {
252 error("Scale is in use and cannot be deleted",$CFG->wwwroot
.'/course/scales.php?id='.$course->id
);
255 //Check for standard scales
256 if ($scale->courseid
== 0 and !has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM
, SITEID
))) {
257 error("Only administrators can delete this scale",$CFG->wwwroot
.'/course/scales.php?id='.$course->id
);
260 if (delete_records("scale", "id", $scaleid)) {
261 $notify = "$scale->name: $strdeleted";
265 //If action is down or up, do it
266 if (($action == "down" ||
$action == "up") and confirm_sesskey()) {
267 //Check for teacher edit
268 require_capability('moodle/course:managescales', $context);
269 //Check for scale if action = edit
270 if (! $scale = get_record("scale", "id", $scaleid)) {
271 error("Scale ID was incorrect");
275 if ($scale->courseid
== 0) {
276 $scale_uses = site_scale_used($scale->id
,$courses);
278 $scale_uses = course_scale_used($course->id
,$scale->id
);
281 //Check for scale_uses
282 if (!empty($scale_uses)) {
283 error("Scale is in use and cannot be moved",$CFG->wwwroot
.'/course/scales.php?id='.$course->id
);
286 if ($action == "down") {
287 $scale->courseid
= 0;
289 $scale->courseid
= $course->id
;
292 if (set_field("scale", "courseid", $scale->courseid
, "id", $scale->id
)) {
293 $notify = "$scale->name: $strmoved";
297 if ($list) { /// Just list the scales (in a helpwindow)
298 require_capability('moodle/course:viewscales', $context);
299 print_header($strscales);
301 if (!empty($scaleid)) {
302 if ($scale = get_record("scale", "id", "$scaleid")) {
303 $scalemenu = make_menu_from_list($scale->scale
);
305 print_simple_box_start("center");
306 print_heading($scale->name
);
308 choose_from_menu($scalemenu, "", "", "");
310 echo text_to_html($scale->description
);
311 print_simple_box_end();
314 close_window_button();
318 if ($scales = get_records("scale", "courseid", "$course->id", "name ASC")) {
319 print_heading($strcustomscales);
321 if (has_capability('moodle/course:managescales', get_context_instance(CONTEXT_COURSE
, $course->id
))) {
322 echo "<p align=\"center\">(";
323 print_string("scalestip");
327 foreach ($scales as $scale) {
328 $scalemenu = make_menu_from_list($scale->scale
);
330 print_simple_box_start("center");
331 print_heading($scale->name
);
333 choose_from_menu($scalemenu, "", "", "");
335 echo text_to_html($scale->description
);
336 print_simple_box_end();
341 if (has_capability('moodle/course:managescales', $context)) {
342 echo "<p align=\"center\">(";
343 print_string("scalestip");
348 if ($scales = get_records("scale", "courseid", "0", "name ASC")) {
349 print_heading($strstandardscales);
350 foreach ($scales as $scale) {
351 $scalemenu = make_menu_from_list($scale->scale
);
353 print_simple_box_start("center");
354 print_heading($scale->name
);
356 choose_from_menu($scalemenu, "", "", "");
358 echo text_to_html($scale->description
);
359 print_simple_box_end();
364 close_window_button();
369 /// The rest is all about editing the scales
371 require_capability('moodle/course:managescales', $context);
373 /// Print out the main page
375 print_header("$course->shortname: $strscales", $course->fullname
,
376 "<a href=\"view.php?id=$course->id\">$course->shortname</a>
379 print_heading_with_help($strscales, "scales");
382 $options['id'] = $course->id
;
383 $options['scaleid'] = 0;
384 $options['action'] = 'new';
386 print_simple_box_start('center');
387 print_single_button($CFG->wwwroot
.'/course/scales.php',$options,$srtcreatenewscale,'POST');
388 print_simple_box_end();
391 if (!empty($notify)) {
392 notify($notify, "green");
396 $customscales = get_records("scale", "courseid", "$course->id", "name ASC");
397 $standardscales = get_records("scale", "courseid", "0", "name ASC");
400 foreach($customscales as $scale) {
405 if ($standardscales) {
406 foreach($standardscales as $scale) {
412 //Calculate the base path
413 $path = "$CFG->wwwroot/course";
417 foreach($scales as $scale) {
418 //Check the separator
419 if (empty($scale->courseid
) && $incustom) {
425 $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>";
426 if (!empty($scale->courseid
)) {
427 $scales_uses = course_scale_used($course->id
,$scale->id
);
429 $scales_uses = site_scale_used($scale->id
,$courses);
431 $line[] = $scales_uses;
433 $line[] = $strcustomscale;
435 $line[] = $strstandardscale;
438 if (empty($scales_uses) && ($incustom ||
has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM
, SITEID
)))) {
439 $buttons .= "<a title=\"$stredit\" href=\"$path/scales.php?id=$course->id&scaleid=$scale->id&action=edit\"><img".
440 " src=\"$CFG->pixpath/t/edit.gif\" class=\"iconsmall\" alt=\"$stredit\" /></a> ";
441 if ($incustom && has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM
, SITEID
))) {
442 $buttons .= "<a title=\"$strdown\" href=\"$path/scales.php?id=$course->id&scaleid=$scale->id&action=down&sesskey=$USER->sesskey\"><img".
443 " src=\"$CFG->pixpath/t/down.gif\" class=\"iconsmall\" alt=\"$strdown\" /></a> ";
445 if (!$incustom && has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM
, SITEID
))) {
446 $buttons .= "<a title=\"$strup\" href=\"$path/scales.php?id=$course->id&scaleid=$scale->id&action=up&sesskey=$USER->sesskey\"><img".
447 " src=\"$CFG->pixpath/t/up.gif\" class=\"iconsmall\" alt=\"$strup\" /></a> ";
449 $buttons .= "<a title=\"$strdelete\" href=\"$path/scales.php?id=$course->id&scaleid=$scale->id&action=delete&sesskey=$USER->sesskey\"><img".
450 " src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a> ";
456 $head = $strscale.",".$stractivities.",".$strtype.",".$stroptions;
457 $table->head
= explode(",",$head);
458 $size = "50%,20%,20%,10%";
459 $table->size
= explode(",",$size);
460 $align = "left,center,center,center";
461 $table->align
= explode(",",$align);
462 $wrap = ",nowrap,nowrap,nowrap";
463 $table->wrap
= explode(",",$wrap);
464 $table->width
= "90%";
465 $table->data
= $data;
466 print_table ($table);
469 print_footer($course);