Bumping version for 1.8.9 release. Thinking we might have to go to x.x.10 for the...
[moodle.git] / course / scales.php
blob9e922a3bdf97fa84c34d7b7a361e3f44f5cb86ad
1 <?php // $Id$
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
44 $courses = array();
46 /// If scale data is being submitted, then save it and continue
47 $focus = "";
48 $errors = NULL;
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";
61 if (!$errors) {
62 $newscale=NULL;
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!");
75 } else {
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";
84 } else {
85 if (!empty($scaleid)) {
86 $action = "edit";
87 } else {
88 $action = "new";
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);
99 //Check for scale
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, "&nbsp;", "&nbsp;");
111 close_window_button();
113 echo "<p/>";
114 print_simple_box_start("center");
115 print_heading($scale->name);
116 echo "<center>";
117 choose_from_menu($scalemenu, "", "", "");
118 echo "</center>";
119 echo text_to_html($scale->description);
120 print_simple_box_end();
121 echo "<p/>";
123 close_window_button();
124 print_footer();
125 exit;
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");
140 } else {
141 $scale = new object();
142 $scale->id = 0;
143 $scale->courseid = $course->id;
144 $scale->name = "";
145 $scale->scale = "";
146 $scale->description = "";
149 //Check if the scale is in use
150 if ($scale->courseid == 0) {
151 $scale_used = site_scale_used($scale->id,$courses);
152 } else {
153 $scale_used = course_scale_used($course->id,$scale->id);
156 //Check for scale use
157 if ($scale_used) {
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);
172 //Title
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\"";
185 } else {
186 $class = "";
188 echo "<td align=\"right\"><b>$strname:</b></td>";
189 echo "<td $class><input type=\"text\" name=\"name\" size=\"50\" value=\"".s($scale->name)."\" />";
190 echo "</td>";
191 echo "</tr>";
192 echo "<tr valign=\"top\">";
193 if (!empty($errors[$scale->id]->scalescale)) {
194 $class = "class=\"highlight\"";
195 } else {
196 $class = "";
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>";
200 echo "</td>";
201 echo "</tr>";
202 echo "<tr valign=\"top\">";
203 echo "<td align=\"right\"><b>$strdescription:</b>";
204 helpbutton("text", $strhelptext);
205 echo "</td>";
206 echo "<td><textarea name=\"description\" cols=\"50\" rows=\"8\" wrap=\"virtual\">".s($scale->description)."</textarea>";
207 echo "</td>";
208 echo "</tr>";
209 if ($scale->id) {
210 echo "<tr valign=\"top\">";
211 echo "<td align=\"right\">";
212 echo "</td>";
213 echo "<td>$stractivities: ".($scale_used ? get_string("yes") : get_string("no"));
214 echo "</td>";
215 echo "</tr>";
217 echo "<tr>";
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>";
226 echo "</form>";
227 echo "<br />";
229 print_footer($course);
231 exit;
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");
243 //Check if the scale is in use
244 if ($scale->courseid == 0) {
245 $scale_used = site_scale_used($scale->id,$courses);
246 } else {
247 $scale_used = course_scale_used($course->id,$scale->id);
250 //Check for scale use
251 if ($scale_used) {
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");
274 //Check if the scale is in use
275 if ($scale->courseid == 0) {
276 $scale_used = site_scale_used($scale->id,$courses);
277 } else {
278 $scale_used = course_scale_used($course->id,$scale->id);
281 //Check for scale use
282 if ($scale_used) {
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;
288 } else {
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);
307 echo "<center>";
308 choose_from_menu($scalemenu, "", "", "");
309 echo "</center>";
310 echo text_to_html($scale->description);
311 print_simple_box_end();
313 echo "<br />";
314 close_window_button();
315 exit;
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");
324 echo ")</p>";
327 foreach ($scales as $scale) {
328 $scalemenu = make_menu_from_list($scale->scale);
330 print_simple_box_start("center");
331 print_heading($scale->name);
332 echo "<center>";
333 choose_from_menu($scalemenu, "", "", "");
334 echo "</center>";
335 echo text_to_html($scale->description);
336 print_simple_box_end();
337 echo "<hr />";
340 } else {
341 if (has_capability('moodle/course:managescales', $context)) {
342 echo "<p align=\"center\">(";
343 print_string("scalestip");
344 echo ")</p>";
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);
355 echo "<center>";
356 choose_from_menu($scalemenu, "", "", "");
357 echo "</center>";
358 echo text_to_html($scale->description);
359 print_simple_box_end();
360 echo "<hr />";
364 close_window_button();
365 exit;
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>
377 -> $strscales");
379 print_heading_with_help($strscales, "scales");
381 $options = array();
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();
389 echo "<p />";
391 if (!empty($notify)) {
392 notify($notify, "green");
395 $scales = array();
396 $customscales = get_records("scale", "courseid", "$course->id", "name ASC");
397 $standardscales = get_records("scale", "courseid", "0", "name ASC");
399 if ($customscales) {
400 foreach($customscales as $scale) {
401 $scales[] = $scale;
405 if ($standardscales) {
406 foreach($standardscales as $scale) {
407 $scales[] = $scale;
411 if ($scales) {
412 //Calculate the base path
413 $path = "$CFG->wwwroot/course";
415 $data = array();
416 $incustom = true;
417 foreach($scales as $scale) {
418 //Check the separator
419 if (empty($scale->courseid) && $incustom) {
420 $incustom = false;
421 $line = "hr";
422 $data[] = $line;
424 $line = array();
425 $line[] = "<a target=\"scale\" title=\"$scale->name\" href=\"$CFG->wwwroot/course/scales.php?id=$course->id&amp;scaleid=$scale->id&amp;action=details\" "."onclick=\"return openpopup('/course/scales.php?id=$course->id\&amp;scaleid=$scale->id&amp;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 $scale_used = course_scale_used($course->id,$scale->id);
428 } else {
429 $scale_used = site_scale_used($scale->id,$courses);
431 $line[] = $scale_used ? get_string("yes") : get_string("no");
432 if ($incustom) {
433 $line[] = $strcustomscale;
434 } else {
435 $line[] = $strstandardscale;
437 $buttons = "";
438 if (!$scale_used && ($incustom || has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM, SITEID)))) {
439 $buttons .= "<a title=\"$stredit\" href=\"$path/scales.php?id=$course->id&amp;scaleid=$scale->id&amp;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&amp;scaleid=$scale->id&amp;action=down&amp;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&amp;scaleid=$scale->id&amp;action=up&amp;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&amp;scaleid=$scale->id&amp;action=delete&amp;sesskey=$USER->sesskey\"><img".
450 " src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a> ";
452 $line[] = $buttons;
454 $data[] = $line;
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);