FIxed bug 3383 ... intro texts no longer center-justified in standard
[moodle.git] / course / scales.php
blob9fefb77de58579b12b2ea55d9f3601470316017c
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 require_variable($id); // course id
8 optional_variable($scaleid); // scale id
9 optional_variable($action,"undefined"); // action to execute
10 optional_variable($name); // scale name
11 optional_variable($description); // scale description
12 optional_variable($scalescale); // scale scale
14 if (! $course = get_record("course", "id", $id)) {
15 error("Course ID was incorrect");
18 require_login($course->id);
20 $strscale = get_string("scale");
21 $strscales = get_string("scales");
22 $strcustomscale = get_string("scalescustom");
23 $strstandardscale = get_string("scalesstandard");
24 $strcustomscales = get_string("scalescustom");
25 $strstandardscales = get_string("scalesstandard");
26 $strname = get_string("name");
27 $strdescription = get_string("description");
28 $strsavechanges = get_string("savechanges");
29 $strchangessaved = get_string("changessaved");
30 $strdeleted = get_string("deleted");
31 $strdelete = get_string("delete");
32 $stredit = get_string("edit");
33 $strdown = get_string("movedown");
34 $strup = get_string("moveup");
35 $strmoved = get_string("changessaved");
36 $srtcreatenewscale = get_string("scalescustomcreate");
37 $strhelptext = get_string("helptext");
38 $stractivities = get_string("activities");
39 $stroptions = get_string("action");
40 $strtype = get_string("group");
42 /// 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
43 $courses = array();
45 /// If scale data is being submitted, then save it and continue
47 $errors = NULL;
49 if ($action == 'sendform' and confirm_sesskey()) {
50 if ($form = data_submitted()) {
51 if (empty($form->name)) {
52 $errors[$scaleid]->name = true;
53 $focus = "form$scaleid.save";
55 if (empty($form->scalescale)) {
56 $errors[$scaleid]->scalescale = true;
57 $focus = "form$scaleid.save";
60 if (!$errors) {
61 $newscale=NULL;
62 $newscale->name = $form->name;
63 $newscale->scale = $form->scalescale;
64 $newscale->description = $form->description;
65 $newscale->courseid = $form->courseid;
66 $newscale->userid = $USER->id;
67 $newscale->timemodified = time();
69 if (empty($scaleid)) {
70 $newscale->courseid = $course->id;
71 if (!insert_record("scale", $newscale)) {
72 error("Could not insert the new scale!");
74 } else {
75 $newscale->id = $scaleid;
76 if (!update_record("scale", $newscale)) {
77 error("Could not update that scale!");
81 $notify = "$newscale->name: $strchangessaved";
82 $focus = "form$scaleid.save";
83 } else {
84 if (!empty($scaleid)) {
85 $action = "edit";
86 } else {
87 $action = "new";
93 //If action is details, show the popup info
94 if ($action == "details") {
95 //Check for teacher edit
96 if (! isteacheredit($course->id)) {
97 error("Only editing teachers can view scales !");
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();
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 //Check for teacher edit
134 if (! isteacheredit($course->id)) {
135 error("Only editing teachers can modify scales !");
137 //Check for scale if action = edit
138 if ($action == "edit") {
139 if (! $scale = get_record("scale", "id", $scaleid)) {
140 error("Scale ID was incorrect");
142 } else {
143 $scale->id = 0;
144 $scale->courseid = $course->id;
145 $scale->name = "";
146 $scale->scale = "";
147 $scale->description = "";
150 //Calculate the uses
151 if ($scale->courseid == 0) {
152 $scale_uses = site_scale_used($scale->id,$courses);
153 } else {
154 $scale_uses = course_scale_used($course->id,$scale->id);
157 //Check for scale_uses
158 if (!empty($scale_uses)) {
159 error("Scale is in use and cannot be modified",$CFG->wwwroot.'/course/scales.php?id='.$course->id);
162 //Check for standard scales
163 if ($scale->courseid == 0 and !isadmin()) {
164 error("Only administrators can edit this scale",$CFG->wwwroot.'/course/scales.php?id='.$course->id);
167 //Print out the headers
168 if (!isset($focus)) {
169 $focus = "";
171 print_header("$course->shortname: $strscales", "$course->fullname",
172 "<a href=\"view.php?id=$course->id\">$course->shortname</a>".
173 " -> <a href=\"scales.php?id=$course->id\">$strscales</a>".
174 " -> ".get_string("editinga","",$strscale), $focus);
176 //Title
177 print_heading_with_help($strscales, "scales");
179 if (!empty($errors) and ($form->scaleid == $scale->id)) {
180 $scale->name = $form->name;
181 $scale->scale = $form->scalescale;
182 $scale->description = $form->description;
184 echo "<form method=\"post\" action=\"scales.php\" name=\"form$scale->id\">";
185 echo "<table cellpadding=\"9\" cellspacing=\"0\" align=\"center\" class=\"generalbox\">";
186 echo "<tr valign=\"top\">";
187 if (!empty($errors[$scale->id]->name)) {
188 $class = "class=\"highlight\"";
189 } else {
190 $class = "";
192 echo "<td align=\"right\"><b>$strname:</b></td>";
193 echo "<td $class><input type=\"text\" name=\"name\" size=\"50\" value=\"".s($scale->name)."\" />";
194 echo "</td>";
195 echo "</tr>";
196 echo "<tr valign=\"top\">";
197 if (!empty($errors[$scale->id]->scalescale)) {
198 $class = "class=\"highlight\"";
199 } else {
200 $class = "";
202 echo "<td align=\"right\"><b>$strscale:</b></td>";
203 echo "<td $class><textarea name=\"scalescale\" cols=\"50\" rows=\"2\" wrap=\"virtual\">".s($scale->scale)."</textarea>";
204 echo "</td>";
205 echo "</tr>";
206 echo "<tr valign=\"top\">";
207 echo "<td align=\"right\"><b>$strdescription:</b>";
208 helpbutton("text", $strhelptext);
209 echo "</td>";
210 echo "<td><textarea name=\"description\" cols=\"50\" rows=\"8\" wrap=\"virtual\">".s($scale->description)."</textarea>";
211 echo "</td>";
212 echo "</tr>";
213 echo "<tr>";
214 if ($scale->id) {
215 echo "<tr valign=\"top\">";
216 echo "<td align=\"right\">";
217 echo "</td>";
218 echo "<td>".get_string("usedinnplaces","",$scale_uses);
219 echo "</td>";
220 echo "</tr>";
222 echo "<tr>";
223 echo "<td colspan=\"2\" align=\"center\">";
224 echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
225 echo "<input type=\"hidden\" name=\"sesskey\" value=\"$sesskey\" />";
226 echo "<input type=\"hidden\" name=\"courseid\" value=\"$scale->courseid\" />";
227 echo "<input type=\"hidden\" name=\"scaleid\" value=\"$scale->id\" />";
228 echo "<input type=\"hidden\" name=\"action\" value=\"sendform\" />";
229 echo "<input type=\"submit\" name=\"save\" value=\"$strsavechanges\" />";
230 echo "</td></tr></table>";
231 echo "</form>";
232 echo "<br />";
234 print_footer($course);
236 exit;
239 //If action is delete, do it
240 if ($action == "delete" and confirm_sesskey()) {
241 //Check for teacher edit
242 if (! isteacheredit($course->id)) {
243 error("Only editing teachers can delete scales !");
245 //Check for scale if action = edit
246 if (! $scale = get_record("scale", "id", $scaleid)) {
247 error("Scale ID was incorrect");
250 //Calculate the uses
251 if ($scale->courseid == 0) {
252 $scale_uses = site_scale_used($scale->id,$courses);
253 } else {
254 $scale_uses = course_scale_used($course->id,$scale->id);
257 //Check for scale_uses
258 if (!empty($scale_uses)) {
259 error("Scale is in use and cannot be deleted",$CFG->wwwroot.'/course/scales.php?id='.$course->id);
262 //Check for standard scales
263 if ($scale->courseid == 0 and !isadmin()) {
264 error("Only administrators can delete this scale",$CFG->wwwroot.'/course/scales.php?id='.$course->id);
267 if (delete_records("scale", "id", $scaleid)) {
268 $notify = "$scale->name: $strdeleted";
272 //If action is down or up, do it
273 if (($action == "down" || $action == "up") and confirm_sesskey()) {
274 //Check for teacher edit
275 if (! isadmin()) {
276 error("Only administrators can move scales",$CFG->wwwroot.'/course/scales.php?id='.$course->id);
278 //Check for scale if action = edit
279 if (! $scale = get_record("scale", "id", $scaleid)) {
280 error("Scale ID was incorrect");
283 //Calculate the uses
284 if ($scale->courseid == 0) {
285 $scale_uses = site_scale_used($scale->id,$courses);
286 } else {
287 $scale_uses = course_scale_used($course->id,$scale->id);
290 //Check for scale_uses
291 if (!empty($scale_uses)) {
292 error("Scale is in use and cannot be moved",$CFG->wwwroot.'/course/scales.php?id='.$course->id);
295 if ($action == "down") {
296 $scale->courseid = 0;
297 } else {
298 $scale->courseid = $course->id;
301 if (set_field("scale", "courseid", $scale->courseid, "id", $scale->id)) {
302 $notify = "$scale->name: $strmoved";
306 if (isset($_GET['list'])) { /// Just list the scales (in a helpwindow)
308 print_header($strscales);
310 if (isset($_GET['scale'])) {
311 if ($scale = get_record("scale", "id", "$scale")) {
312 $scalemenu = make_menu_from_list($scale->scale);
314 print_simple_box_start("center");
315 print_heading($scale->name);
316 echo "<center>";
317 choose_from_menu($scalemenu, "", "", "");
318 echo "</center>";
319 echo text_to_html($scale->description);
320 print_simple_box_end();
322 echo "<br />";
323 close_window_button();
324 exit;
327 if ($scales = get_records("scale", "courseid", "$course->id", "name ASC")) {
328 print_heading($strcustomscales);
330 if (isteacheredit($course->id)) {
331 echo "<p align=\"center\">(";
332 print_string("scalestip");
333 echo ")</p>";
336 foreach ($scales as $scale) {
337 $scalemenu = make_menu_from_list($scale->scale);
339 print_simple_box_start("center");
340 print_heading($scale->name);
341 echo "<center>";
342 choose_from_menu($scalemenu, "", "", "");
343 echo "</center>";
344 echo text_to_html($scale->description);
345 print_simple_box_end();
346 echo "<hr />";
349 } else {
350 if (isteacheredit($course->id)) {
351 echo "<p align=\"center\">(";
352 print_string("scalestip");
353 echo ")</p>";
357 if ($scales = get_records("scale", "courseid", "0", "name ASC")) {
358 print_heading($strstandardscales);
359 foreach ($scales as $scale) {
360 $scalemenu = make_menu_from_list($scale->scale);
362 print_simple_box_start("center");
363 print_heading($scale->name);
364 echo "<center>";
365 choose_from_menu($scalemenu, "", "", "");
366 echo "</center>";
367 echo text_to_html($scale->description);
368 print_simple_box_end();
369 echo "<hr />";
373 close_window_button();
374 exit;
378 /// The rest is all about editing the scales
380 if (!isteacheredit($course->id)) {
381 error("Only editing teachers can modify scales !");
384 /// Print out the main page
386 print_header("$course->shortname: $strscales", "$course->fullname",
387 "<a href=\"view.php?id=$course->id\">$course->shortname</a>
388 -> $strscales");
390 print_heading_with_help($strscales, "scales");
392 $options = array();
393 $options['id'] = $course->id;
394 $options['scaleid'] = 0;
395 $options['action'] = 'new';
397 print_simple_box_start('center');
398 print_single_button($CFG->wwwroot.'/course/scales.php',$options,$srtcreatenewscale,'POST');
399 print_simple_box_end();
400 echo "<p>";
402 if (!empty($notify)) {
403 notify($notify, "green");
406 $scales = array();
407 $customscales = get_records("scale", "courseid", "$course->id", "name ASC");
408 $standardscales = get_records("scale", "courseid", "0", "name ASC");
410 if ($customscales) {
411 foreach($customscales as $scale) {
412 $scales[] = $scale;
416 if ($standardscales) {
417 foreach($standardscales as $scale) {
418 $scales[] = $scale;
422 if ($scales) {
423 //Calculate the base path
424 $path = "$CFG->wwwroot/course";
426 $data = array();
427 $incustom = true;
428 foreach($scales as $scale) {
429 //Check the separator
430 if (empty($scale->courseid) && $incustom) {
431 $incustom = false;
432 $line = "hr";
433 $data[] = $line;
435 $line = array();
436 $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>";
437 if (!empty($scale->courseid)) {
438 $scales_uses = course_scale_used($course->id,$scale->id);
439 } else {
440 $scales_uses = site_scale_used($scale->id,$courses);
442 $line[] = $scales_uses;
443 if ($incustom) {
444 $line[] = $strcustomscale;
445 } else {
446 $line[] = $strstandardscale;
448 $buttons = "";
449 if (empty($scales_uses) && ($incustom || isadmin())) {
450 $buttons .= "<a title=\"$stredit\" href=\"$path/scales.php?id=$course->id&amp;scaleid=$scale->id&amp;action=edit\"><img".
451 " src=\"$CFG->pixpath/t/edit.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\" alt=\"\" /></a> ";
452 if ($incustom && isadmin()) {
453 $buttons .= "<a title=\"$strdown\" href=\"$path/scales.php?id=$course->id&amp;scaleid=$scale->id&amp;action=down&amp;sesskey=$USER->sesskey\"><img".
454 " src=\"$CFG->pixpath/t/down.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\" alt=\"\" /></a> ";
456 if (!$incustom && isadmin()) {
457 $buttons .= "<a title=\"$strup\" href=\"$path/scales.php?id=$course->id&amp;scaleid=$scale->id&amp;action=up&amp;sesskey=$USER->sesskey\"><img".
458 " src=\"$CFG->pixpath/t/up.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\" alt=\"\" /></a> ";
460 $buttons .= "<a title=\"$strdelete\" href=\"$path/scales.php?id=$course->id&amp;scaleid=$scale->id&amp;action=delete&amp;sesskey=$USER->sesskey\"><img".
461 " src=\"$CFG->pixpath/t/delete.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\" alt=\"\" /></a> ";
463 $line[] = $buttons;
465 $data[] = $line;
467 $head = $strscale.",".$stractivities.",".$strtype.",".$stroptions;
468 $table->head = explode(",",$head);
469 $size = "50%,20%,20%,10%";
470 $table->size = explode(",",$size);
471 $align = "left,center,center,center";
472 $table->align = explode(",",$align);
473 $wrap = ",nowrap,nowrap,nowrap";
474 $table->wrap = explode(",",$wrap);
475 $table->width = "90%";
476 $table->data = $data;
477 print_table ($table);
480 print_footer($course);