Incorrect variable name used for parameter.
[moodle-linuxchix.git] / mod / choice / lib.php
blob55acee6e8ea582178b3fc6319fe543f833a73b3c
1 <?php // $Id$
3 $COLUMN_HEIGHT = 300;
5 define('CHOICE_PUBLISH_ANONYMOUS', '0');
6 define('CHOICE_PUBLISH_NAMES', '1');
8 define('CHOICE_RELEASE_NOT', '0');
9 define('CHOICE_RELEASE_AFTER_ANSWER', '1');
10 define('CHOICE_RELEASE_AFTER_CLOSE', '2');
11 define('CHOICE_RELEASE_ALWAYS', '3');
13 define('CHOICE_DISPLAY_HORIZONTAL', '0');
14 define('CHOICE_DISPLAY_VERTICAL', '1');
16 $CHOICE_PUBLISH = array (CHOICE_PUBLISH_ANONYMOUS => get_string('publishanonymous', 'choice'),
17 CHOICE_PUBLISH_NAMES => get_string('publishnames', 'choice'));
19 $CHOICE_RELEASE = array (CHOICE_RELEASE_NOT => get_string('publishnot', 'choice'),
20 CHOICE_RELEASE_AFTER_ANSWER => get_string('publishafteranswer', 'choice'),
21 CHOICE_RELEASE_AFTER_CLOSE => get_string('publishafterclose', 'choice'),
22 CHOICE_RELEASE_ALWAYS => get_string('publishalways', 'choice'));
24 $CHOICE_DISPLAY = array (CHOICE_DISPLAY_HORIZONTAL => get_string('displayhorizontal', 'choice'),
25 CHOICE_DISPLAY_VERTICAL => get_string('displayvertical','choice'));
27 /// Standard functions /////////////////////////////////////////////////////////
29 function choice_user_outline($course, $user, $mod, $choice) {
30 if ($answer = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user->id)) {
31 $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'";
32 $result->time = $answer->timemodified;
33 return $result;
35 return NULL;
39 function choice_user_complete($course, $user, $mod, $choice) {
40 if ($answer = get_record('choice_answers', "choiceid", $choice->id, "userid", $user->id)) {
41 $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'";
42 $result->time = $answer->timemodified;
43 echo get_string("answered", "choice").": $result->info. ".get_string("updated", '', userdate($result->time));
44 } else {
45 print_string("notanswered", "choice");
50 function choice_add_instance($choice) {
51 // Given an object containing all the necessary data,
52 // (defined by the form in mod.html) this function
53 // will create a new instance and return the id number
54 // of the new instance.
56 $choice->timemodified = time();
58 if (!empty($choice->timerestrict) and $choice->timerestrict) {
59 $choice->timeopen = make_timestamp($choice->openyear, $choice->openmonth, $choice->openday,
60 $choice->openhour, $choice->openminute, 0);
61 $choice->timeclose = make_timestamp($choice->closeyear, $choice->closemonth, $choice->closeday,
62 $choice->closehour, $choice->closeminute, 0);
63 } else {
64 $choice->timeopen = 0;
65 $choice->timeclose = 0;
68 //insert answers
69 if ($choice->id = insert_record("choice", $choice)) {
70 foreach ($choice as $name => $value) {
71 if (strstr($name, "newoption")) { /// New option
72 $value = trim($value);
73 if ($value) {
74 $option = NULL;
75 $option->text = $value;
76 $option->choiceid = $choice->id;
77 $option->maxanswers = $choice->{'newlimit'.substr($name, 9)};
78 $option->timemodified = time();
79 insert_record("choice_options", $option);
84 return $choice->id;
88 function choice_update_instance($choice) {
89 // Given an object containing all the necessary data,
90 // (defined by the form in mod.html) this function
91 // will update an existing instance with new data.
93 $choice->id = $choice->instance;
94 $choice->timemodified = time();
97 if (!empty($choice->timerestrict) and $choice->timerestrict) {
98 $choice->timeopen = make_timestamp($choice->openyear, $choice->openmonth, $choice->openday,
99 $choice->openhour, $choice->openminute, 0);
100 $choice->timeclose = make_timestamp($choice->closeyear, $choice->closemonth, $choice->closeday,
101 $choice->closehour, $choice->closeminute, 0);
102 } else {
103 $choice->timeopen = 0;
104 $choice->timeclose = 0;
107 //update answers
109 foreach ($choice as $name => $value) {
110 $value = trim($value);
112 if (strstr($name, "oldoption")) { // Old option
113 if ($value) {
114 $option = NULL;
115 $option->id = substr($name, 9); // Get the ID of the answer that needs to be updated.
116 $option->text = $value;
117 $option->choiceid = $choice->id;
118 $option->maxanswers = $choice->{'oldlimit'.substr($name, 9)};
119 $option->timemodified = time();
120 update_record("choice_options", $option);
121 } else { //empty old option - needs to be deleted.
122 delete_records("choice_options", "id", substr($name, 9));
124 } else if (strstr($name, "newoption")) { /// New option
125 if ($value) {
126 $option = NULL;
127 $option->text = $value;
128 $option->choiceid = $choice->id;
129 $option->maxanswers = $choice->{'newlimit'.substr($name, 9)};
130 $option->timemodified = time();
131 insert_record("choice_options", $option);
136 return update_record('choice', $choice);
141 function choice_delete_instance($id) {
142 // Given an ID of an instance of this module,
143 // this function will permanently delete the instance
144 // and any data that depends on it.
146 if (! $choice = get_record("choice", "id", "$id")) {
147 return false;
150 $result = true;
152 if (! delete_records("choice_answers", "choiceid", "$choice->id")) {
153 $result = false;
156 if (! delete_records("choice_options", "choiceid", "$choice->id")) {
157 $result = false;
160 if (! delete_records("choice", "id", "$choice->id")) {
161 $result = false;
165 return $result;
168 function choice_get_participants($choiceid) {
169 //Returns the users with data in one choice
170 //(users with records in choice_responses, students)
172 global $CFG;
174 //Get students
175 $students = get_records_sql("SELECT DISTINCT u.id, u.id
176 FROM {$CFG->prefix}user u,
177 {$CFG->prefix}choice_answers a
178 WHERE a.choiceid = '$choiceid' and
179 u.id = a.userid");
181 //Return students array (it contains an array of unique users)
182 return ($students);
186 function choice_get_option_text($choice, $id) {
187 // Returns text string which is the answer that matches the id
188 if ($result = get_record("choice_options", "id", $id)) {
189 return $result->text;
190 } else {
191 return get_string("notanswered", "choice");
195 function choice_get_choice($choiceid) {
196 // Gets a full choice record
198 if ($choice = get_record("choice", "id", $choiceid)) {
199 if ($options = get_records("choice_options", "choiceid", $choiceid, "id")) {
200 foreach ($options as $option) {
201 $choice->option[$option->id] = $option->text;
202 $choice->maxanswers[$option->id] = $option->maxanswers;
204 return $choice;
207 return false;