polished.
[moodle.git] / mod / choice / lib.php
CommitLineData
1c61c8d6 1<?php // $Id$
0d22e7cc 2
6d6c9389 3$COLUMN_HEIGHT = 300;
4
8f7dc7f1 5define('CHOICE_PUBLISH_ANONYMOUS', '0');
6define('CHOICE_PUBLISH_NAMES', '1');
6d6c9389 7
c100a341 8define('CHOICE_SHOWRESULTS_NOT', '0');
9define('CHOICE_SHOWRESULTS_AFTER_ANSWER', '1');
10define('CHOICE_SHOWRESULTS_AFTER_CLOSE', '2');
11define('CHOICE_SHOWRESULTS_ALWAYS', '3');
f9af7fd0 12
6fd87e3b 13define('CHOICE_DISPLAY_HORIZONTAL', '0');
14define('CHOICE_DISPLAY_VERTICAL', '1');
15
8f7dc7f1 16$CHOICE_PUBLISH = array (CHOICE_PUBLISH_ANONYMOUS => get_string('publishanonymous', 'choice'),
17 CHOICE_PUBLISH_NAMES => get_string('publishnames', 'choice'));
6d6c9389 18
c100a341 19$CHOICE_SHOWRESULTS = array (CHOICE_SHOWRESULTS_NOT => get_string('publishnot', 'choice'),
20 CHOICE_SHOWRESULTS_AFTER_ANSWER => get_string('publishafteranswer', 'choice'),
21 CHOICE_SHOWRESULTS_AFTER_CLOSE => get_string('publishafterclose', 'choice'),
22 CHOICE_SHOWRESULTS_ALWAYS => get_string('publishalways', 'choice'));
f9af7fd0 23
6fd87e3b 24$CHOICE_DISPLAY = array (CHOICE_DISPLAY_HORIZONTAL => get_string('displayhorizontal', 'choice'),
25 CHOICE_DISPLAY_VERTICAL => get_string('displayvertical','choice'));
6d6c9389 26
27/// Standard functions /////////////////////////////////////////////////////////
28
0d22e7cc 29function choice_user_outline($course, $user, $mod, $choice) {
d1a67362 30 if ($answer = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user->id)) {
7cfc54b9 31 $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'";
d1a67362 32 $result->time = $answer->timemodified;
0d22e7cc 33 return $result;
34 }
35 return NULL;
36}
37
38
39function choice_user_complete($course, $user, $mod, $choice) {
d1a67362 40 if ($answer = get_record('choice_answers', "choiceid", $choice->id, "userid", $user->id)) {
7cfc54b9 41 $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'";
d1a67362 42 $result->time = $answer->timemodified;
7cfc54b9 43 echo get_string("answered", "choice").": $result->info. ".get_string("updated", '', userdate($result->time));
0d22e7cc 44 } else {
c4016bc1 45 print_string("notanswered", "choice");
0d22e7cc 46 }
47}
48
04eba58f 49
50function 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.
55
56 $choice->timemodified = time();
57
ff458eb5 58 if (!empty($choice->timerestrict) and $choice->timerestrict) {
d30772b2 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;
66 }
67
6fd87e3b 68 //insert answers
a77a6009 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);
af834f3e 73 if (isset($value) && $value <> '') {
a77a6009 74 $option = NULL;
75 $option->text = $value;
76 $option->choiceid = $choice->id;
c9ff813c 77 $option->maxanswers = $choice->{'newlimit'.substr($name, 9)};
a77a6009 78 $option->timemodified = time();
79 insert_record("choice_options", $option);
80 }
6fd87e3b 81 }
82 }
83 }
a77a6009 84 return $choice->id;
04eba58f 85}
86
87
88function 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.
92
93 $choice->id = $choice->instance;
94 $choice->timemodified = time();
95
d30772b2 96
ff458eb5 97 if (!empty($choice->timerestrict) and $choice->timerestrict) {
d30772b2 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;
105 }
106
6fd87e3b 107 //update answers
108
a77a6009 109 foreach ($choice as $name => $value) {
110 $value = trim($value);
111
d1a67362 112 if (strstr($name, "oldoption")) { // Old option
af834f3e 113 if (isset($value) && $value <> '') {
a77a6009 114 $option = NULL;
f6eb1099 115 $option->id = substr($name, 9); // Get the ID of the answer that needs to be updated.
a77a6009 116 $option->text = $value;
117 $option->choiceid = $choice->id;
c9ff813c 118 $option->maxanswers = $choice->{'oldlimit'.substr($name, 9)};
a77a6009 119 $option->timemodified = time();
c9ff813c 120 update_record("choice_options", $option);
b9135eca 121 } else { //empty old option - needs to be deleted.
122 delete_records("choice_options", "id", substr($name, 9));
c9ff813c 123 }
a77a6009 124 } else if (strstr($name, "newoption")) { /// New option
af834f3e 125 if (isset($value)&& $value <> '') {
a77a6009 126 $option = NULL;
127 $option->text = $value;
128 $option->choiceid = $choice->id;
c9ff813c 129 $option->maxanswers = $choice->{'newlimit'.substr($name, 9)};
a77a6009 130 $option->timemodified = time();
131 insert_record("choice_options", $option);
132 }
6fd87e3b 133 }
6fd87e3b 134 }
d30772b2 135
a77a6009 136 return update_record('choice', $choice);
6fd87e3b 137
04eba58f 138}
139
140
141function 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.
145
146 if (! $choice = get_record("choice", "id", "$id")) {
147 return false;
148 }
149
150 $result = true;
151
a77a6009 152 if (! delete_records("choice_answers", "choiceid", "$choice->id")) {
04eba58f 153 $result = false;
154 }
155
a77a6009 156 if (! delete_records("choice_options", "choiceid", "$choice->id")) {
04eba58f 157 $result = false;
158 }
a77a6009 159
160 if (! delete_records("choice", "id", "$choice->id")) {
6fd87e3b 161 $result = false;
162 }
a77a6009 163
04eba58f 164
165 return $result;
166}
167
05855091 168function choice_get_participants($choiceid) {
169//Returns the users with data in one choice
6fd87e3b 170//(users with records in choice_responses, students)
05855091 171
172 global $CFG;
173
174 //Get students
95e72c12 175 $students = get_records_sql("SELECT DISTINCT u.id, u.id
05855091 176 FROM {$CFG->prefix}user u,
a77a6009 177 {$CFG->prefix}choice_answers a
178 WHERE a.choiceid = '$choiceid' and
179 u.id = a.userid");
05855091 180
181 //Return students array (it contains an array of unique users)
182 return ($students);
183}
184
04eba58f 185
a77a6009 186function 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;
6fd87e3b 190 } else {
191 return get_string("notanswered", "choice");
192 }
c4016bc1 193}
194
cd3fccff 195function choice_get_choice($choiceid) {
6fd87e3b 196// Gets a full choice record
a77a6009 197
198 if ($choice = get_record("choice", "id", $choiceid)) {
199 if ($options = get_records("choice_options", "choiceid", $choiceid, "id")) {
200 foreach ($options as $option) {
dfde3b20 201 $choice->option[$option->id] = $option->text;
c9ff813c 202 $choice->maxanswers[$option->id] = $option->maxanswers;
a77a6009 203 }
204 return $choice;
205 }
206 }
207 return false;
cd3fccff 208}
209
f3221af9 210function choice_get_view_actions() {
211 return array('view','view all','report');
212}
213
214function choice_get_post_actions() {
215 return array('choose','choose again');
216}
217
5a9726e7 218?>