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
;
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
));
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);
64 $choice->timeopen
= 0;
65 $choice->timeclose
= 0;
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);
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);
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);
103 $choice->timeopen
= 0;
104 $choice->timeclose
= 0;
109 foreach ($choice as $name => $value) {
110 $value = trim($value);
112 if (strstr($name, "oldoption")) { // Old option
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
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")) {
152 if (! delete_records("choice_answers", "choiceid", "$choice->id")) {
156 if (! delete_records("choice_options", "choiceid", "$choice->id")) {
160 if (! delete_records("choice", "id", "$choice->id")) {
168 function choice_get_participants($choiceid) {
169 //Returns the users with data in one choice
170 //(users with records in choice_responses, 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
181 //Return students array (it contains an array of unique users)
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
;
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
;