Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Choices now support up to 6 choices. Backward compatible to 2-choice …

…format.
  • Loading branch information...
commit cd3fccff00a2999a2061e628936221022186b01f 1 parent 2415d95
martin authored
3  lang/en/choice.php
@@ -6,8 +6,7 @@
6 6 #------------------------------------------------------------
7 7
8 8 $string[answered] = "Answered";
9   -$string[choice1] = "First choice";
10   -$string[choice2] = "Second choice";
  9 +$string[choice] = "Choice \$a";
11 10 $string[choicename] = "Choice name";
12 11 $string[choicetext] = "Choice text";
13 12 $string[notanswered] = "Not answered yet";
41 mod/choice/lib.php
... ... @@ -1,18 +1,11 @@
1 1 <?PHP // $Id$
2 2
3   -function choice_user_summary($course, $user, $mod, $choice) {
4   - global $CFG;
5   -}
6   -
  3 +$CHOICE_MAX_NUMBER = 6;
7 4
8 5 function choice_user_outline($course, $user, $mod, $choice) {
9 6 if ($current = get_record_sql("SELECT * FROM choice_answers
10 7 WHERE choice='$choice->id' AND user='$user->id'")) {
11   - if ($current->answer == "1") {
12   - $result->info = "'$choice->answer1'";
13   - } else if ($current->answer == "2") {
14   - $result->info = "'$choice->answer2'";
15   - }
  8 + $result->info = "'".choice_get_answer($choice, $current->answer)."'";
16 9 $result->time = $current->timemodified;
17 10 return $result;
18 11 }
@@ -23,11 +16,7 @@ function choice_user_outline($course, $user, $mod, $choice) {
23 16 function choice_user_complete($course, $user, $mod, $choice) {
24 17 if ($current = get_record_sql("SELECT * FROM choice_answers
25 18 WHERE choice='$choice->id' AND user='$user->id'")) {
26   - if ($current->answer == "1") {
27   - $result->info = "'$choice->answer1'";
28   - } else if ($current->answer == "2") {
29   - $result->info = "'$choice->answer2'";
30   - }
  19 + $result->info = "'".choice_get_answer($choice, $current->answer)."'";
31 20 $result->time = $current->timemodified;
32 21 echo get_string("answered", "choice").": $result->info , last updated ".userdate($result->time);
33 22 } else {
@@ -90,11 +79,35 @@ function choice_get_answer($choice, $code) {
90 79 return "$choice->answer1";
91 80 case 2:
92 81 return "$choice->answer2";
  82 + case 3:
  83 + return "$choice->answer3";
  84 + case 4:
  85 + return "$choice->answer4";
  86 + case 5:
  87 + return "$choice->answer5";
  88 + case 6:
  89 + return "$choice->answer6";
93 90 default:
94 91 return get_string("notanswered", "choice");
95 92 }
96 93 }
97 94
  95 +function choice_get_choice($choiceid) {
  96 +// Gets a full choice record
  97 +
  98 + if ($choice = get_record("choice", "id", $choiceid)) {
  99 + $choice->answer[1] = $choice->answer1;
  100 + $choice->answer[2] = $choice->answer2;
  101 + $choice->answer[3] = $choice->answer3;
  102 + $choice->answer[4] = $choice->answer4;
  103 + $choice->answer[5] = $choice->answer5;
  104 + $choice->answer[6] = $choice->answer6;
  105 + return $choice;
  106 + } else {
  107 + return false;
  108 + }
  109 +}
  110 +
98 111
99 112 ?>
100 113
50 mod/choice/mod.html
@@ -13,30 +13,52 @@
13 13 <td align=right><P><B><? print_string("choicetext","choice") ?>:</B></P></TD>
14 14 <td>
15 15 <textarea name="text" rows=15 cols=30 wrap="virtual"><? p($form->text) ?></textarea>
  16 + <? helpbutton("text", get_string("helptext")); ?>
16 17 </td>
17 18 </tr>
18 19
19 20 <tr valign=top>
20   - <td align=right><P><B><? print_string("choice1","choice") ?>:</B></P></TD>
  21 + <td align=right><P><B><? print_string("choice","choice","1") ?>:</B></P></TD>
21 22 <td>
22   - <input type="text" name="answer1" size=30 value="<?
23   - if ($form->answer1) {
24   - p($form->answer1);
25   - } else {
26   - print_string("yes");
27   - } ?>">
  23 + <input type="text" name="answer1" size=30 value="<? p($form->answer1) ?>">
  24 + <? helpbutton("options", get_string("modulenameplural", "choice"), "choice") ?>
28 25 </td>
29 26 </tr>
30 27
31 28 <tr valign=top>
32   - <td align=right><P><B><? print_string("choice2","choice") ?>:</B></P></TD>
  29 + <td align=right><P><B><? print_string("choice","choice","2") ?>:</B></P></TD>
33 30 <td>
34   - <input type="text" name="answer2" size=30 value="<?
35   - if ($form->answer2) {
36   - p($form->answer2);
37   - } else {
38   - print_string("no");
39   - } ?>">
  31 + <input type="text" name="answer2" size=30 value="<? p($form->answer2) ?>">
  32 + <? helpbutton("options", get_string("modulenameplural", "choice"), "choice") ?>
  33 + </td>
  34 +</tr>
  35 +
  36 +<tr valign=top>
  37 + <td align=right><P><B><? print_string("choice","choice","3") ?>:</B></P></TD>
  38 + <td>
  39 + <input type="text" name="answer3" size=30 value="<? p($form->answer3) ?>">
  40 + <? helpbutton("options", get_string("modulenameplural", "choice"), "choice") ?>
  41 + </td>
  42 +</tr>
  43 +<tr valign=top>
  44 + <td align=right><P><B><? print_string("choice","choice","4") ?>:</B></P></TD>
  45 + <td>
  46 + <input type="text" name="answer4" size=30 value="<? p($form->answer4) ?>">
  47 + <? helpbutton("options", get_string("modulenameplural", "choice"), "choice") ?>
  48 + </td>
  49 +</tr>
  50 +<tr valign=top>
  51 + <td align=right><P><B><? print_string("choice","choice","5") ?>:</B></P></TD>
  52 + <td>
  53 + <input type="text" name="answer5" size=30 value="<? p($form->answer5) ?>">
  54 + <? helpbutton("options", get_string("modulenameplural", "choice"), "choice") ?>
  55 + </td>
  56 +</tr>
  57 +<tr valign=top>
  58 + <td align=right><P><B><? print_string("choice","choice","6") ?>:</B></P></TD>
  59 + <td>
  60 + <input type="text" name="answer6" size=30 value="<? p($form->answer6) ?>">
  61 + <? helpbutton("options", get_string("modulenameplural", "choice"), "choice") ?>
40 62 </td>
41 63 </tr>
42 64
13 mod/choice/report.php
@@ -19,7 +19,7 @@
19 19 error("Only teachers can look at this page");
20 20 }
21 21
22   - if (! $choice = get_record("choice", "id", $cm->instance)) {
  22 + if (!$choice = choice_get_choice($cm->instance)) {
23 23 error("Course module is incorrect");
24 24 }
25 25
@@ -43,21 +43,24 @@
43 43 foreach ($allanswers as $aa) {
44 44 $answers[$aa->user] = $aa;
45 45 }
46   -
47 46 } else {
48 47 $answers = array () ;
49 48 }
50 49
51 50 $timenow = time();
52 51
53   - for ($i=0; $i<=2; $i++) { // number of choices (presently hardcoded)
54   - $useranswer[$i] = array();
  52 + foreach ($choice->answer as $key => $answer) {
  53 + $useranswer[$key] = array();
55 54 }
56   -
57 55 foreach ($users as $user) {
58 56 $answer = $answers[$user->id];
59 57 $useranswer[(int)$answer->answer][] = $user;
60 58 }
  59 + foreach ($choice->answer as $key => $answer) {
  60 + if (!$choice->answer[$key]) {
  61 + unset($useranswer[$key]); // Throw away any data that doesn't apply
  62 + }
  63 + }
61 64 ksort($useranswer);
62 65
63 66 $tablewidth = (int) (100.0 / count($useranswer));
6 mod/choice/version.php
@@ -5,13 +5,17 @@
5 5 // This fragment is called by /admin/index.php
6 6 ////////////////////////////////////////////////////////////////////////////////
7 7
8   -$module->version = 2002082000;
  8 +$module->version = 2002090800;
9 9 $module->cron = 0;
10 10
11 11 function choice_upgrade($oldversion) {
12 12 // This function does anything necessary to upgrade
13 13 // older versions to match current functionality
14 14
  15 + if ($oldversion < 2002090800) {
  16 + execute_sql(" ALTER TABLE `choice` CHANGE `answer1` `answer1` VARCHAR( 255 )");
  17 + execute_sql(" ALTER TABLE `choice` CHANGE `answer2` `answer2` VARCHAR( 255 )");
  18 + }
15 19 return true;
16 20 }
17 21
1  mod/choice/view.html
@@ -2,6 +2,7 @@
2 2 <P>
3 3 <FORM name="form" method="post" action="view.php">
4 4 <TABLE WIDTH=70% CELLPADDING=20 CELLSPACING=20><TR>
  5 +<? foreach (
5 6 <TD ALIGN=CENTER NOWRAP WIDTH=50%>
6 7 <INPUT type="radio" name=answer value="1" <?=$answer1checked ?> >
7 8 <? p($choice->answer1) ?>
25 mod/choice/view.php
@@ -15,17 +15,13 @@
15 15
16 16 require_login($course->id);
17 17
18   - if (! $choice = get_record("choice", "id", $cm->instance)) {
  18 + if (!$choice = choice_get_choice($cm->instance)) {
19 19 error("Course module is incorrect");
20 20 }
21 21
22 22 if ($current = get_record_sql("SELECT * FROM choice_answers
23 23 WHERE choice='$choice->id' AND user='$USER->id'")) {
24   - if ($current->answer == "1") {
25   - $answer1checked = "CHECKED";
26   - } else if ($current->answer == "2") {
27   - $answer2checked = "CHECKED";
28   - }
  24 + $answerchecked[$current->answer] = "CHECKED";
29 25 }
30 26
31 27 if (match_referer() && isset($HTTP_POST_VARS)) { // form submitted
@@ -76,7 +72,22 @@
76 72
77 73 print_simple_box( text_to_html($choice->text) , "center");
78 74
79   - require("view.html");
  75 + echo "<CENTER><P><FORM name=\"form\" method=\"post\" action=\"view.php\">";
  76 + echo "<TABLE CELLPADDING=20 CELLSPACING=20><TR>";
  77 +
  78 + foreach ($choice->answer as $key => $answer) {
  79 + if ($answer) {
  80 + echo "<TD ALIGN=CENTER>";
  81 + echo "<INPUT type=radio name=answer value=\"$key\" ".$answerchecked[$key].">";
  82 + p($answer);
  83 + echo "</TD>";
  84 + }
  85 + }
  86 +
  87 + echo "</TR></TABLE>";
  88 + echo "<INPUT type=hidden name=id value=\"$cm->id\">";
  89 + echo "<INPUT type=submit value=\"".get_string("savemychoice","choice")."\">";
  90 + echo "</P></FORM></CENTER>";
80 91
81 92 print_footer($course);
82 93

0 comments on commit cd3fccf

Please sign in to comment.
Something went wrong with that request. Please try again.