mirrored from git://git.moodle.org/moodle.git
/
lib.php
344 lines (261 loc) · 10.3 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?PHP // $Id$
// Graph size
$SURVEY_GHEIGHT = 500;
$SURVEY_GWIDTH = 900;
$SURVEY_QTYPE = array (
"-3" => "Virtual Actual and Preferred",
"-2" => "Virtual Preferred",
"-1" => "Virtual Actual",
"0" => "Text",
"1" => "Actual",
"2" => "Preferred",
"3" => "Actual and Preferred",
);
// FUNCTIONS ////////////////////////////////////////////////////////
function survey_add_instance($survey) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will create a new instance and return the id number
// of the new instance.
if (!$template = get_record("survey", "id", $survey->template)) {
return 0;
}
$survey->questions = $template->questions;
$survey->timecreated = time();
$survey->timemodified = $survey->timecreated;
return insert_record("survey", $survey);
}
function survey_update_instance($survey) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will update an existing instance with new data.
if (!$template = get_record("survey", "id", $survey->template)) {
return 0;
}
$survey->id = $survey->instance;
$survey->questions = $template->questions;
$survey->timemodified = time();
return update_record("survey", $survey);
}
function survey_delete_instance($id) {
// Given an ID of an instance of this module,
// this function will permanently delete the instance
// and any data that depends on it.
if (! $survey = get_record("survey", "id", "$id")) {
return false;
}
$result = true;
if (! delete_records("survey_analysis", "survey", "$survey->id")) {
$result = false;
}
if (! delete_records("survey_answers", "survey", "$survey->id")) {
$result = false;
}
if (! delete_records("survey", "id", "$survey->id")) {
$result = false;
}
return $result;
}
function survey_print_recent_activity(&$logs, $isteacher=false) {
global $CFG, $COURSE_TEACHER_COLOR;
$content = false;
$surveys = NULL;
foreach ($logs as $log) {
if ($log->module == "survey" and $log->action == "submit") {
$surveys[$log->id] = get_record_sql("SELECT s.name, u.firstname, u.lastname
FROM survey s, user u
WHERE s.id = '$log->info' AND e.user = u.id");
$surveys[$log->info]->time = $log->time;
$surveys[$log->info]->url = $log->url;
}
}
if ($surveys) {
$content = true;
print_headline(get_string("newsurveyresponses", "survey").":");
foreach ($surveys as $survey) {
$date = userdate($survey->time, "%e %b, %H:%M");
echo "<P><FONT SIZE=1>$date - $survey->firstname $survey->lastname<BR>";
echo "\"<A HREF=\"$CFG->wwwroot/mod/survey/$survey->url\">";
echo "$survey->name";
echo "</A>\"</FONT></P>";
}
}
return $content;
}
function survey_already_done($survey, $user) {
return record_exists_sql("SELECT * FROM survey_answers WHERE survey='$survey' AND user='$user'");
}
function survey_get_responses($survey) {
return get_records_sql("SELECT a.time as time, count(*) as numanswers, u.*
FROM survey_answers AS a, user AS u
WHERE a.answer1 <> '0' AND a.answer2 <> '0'
AND a.survey = $survey
AND a.user = u.id
GROUP BY a.user ORDER BY a.time ASC");
}
function survey_count_responses($survey) {
if ($responses = survey_get_responses($survey)) {
return count($responses);
} else {
return 0;
}
}
function survey_print_all_responses($survey, $results) {
global $THEME;
echo "<TABLE CELLPADDING=5 CELLSPACING=2 ALIGN=CENTER>";
echo "<TR><TD>Name<TD>Time<TD>Answered</TR>";
foreach ($results as $a) {
echo "<TR>";
echo "<TD><A HREF=\"report.php?action=student&student=$a->id&id=$survey\">$a->firstname $a->lastname</A></TD>";
echo "<TD>".userdate($a->time, "%e %B %Y, %I:%M %p")."</TD>";
echo "<TD align=right>$a->numanswers</TD>";
echo "</TR>";
}
echo "</TABLE>";
}
function survey_get_template_name($templateid) {
global $db;
if ($templateid) {
if ($ss = $db->Execute("SELECT name FROM surveys WHERE id = $templateid")) {
return $ss->fields["name"];
}
} else {
return "";
}
}
function survey_get_analysis($survey, $user) {
global $db;
return get_record_sql("SELECT notes from survey_analysis WHERE survey='$survey' and user='$user'");
}
function survey_update_analysis($survey, $user, $notes) {
global $db;
return $db->Execute("UPDATE survey_analysis SET notes='$notes' WHERE survey='$survey' and user='$user'");
}
function survey_add_analysis($survey, $user, $notes) {
global $db;
return $db->Execute("INSERT INTO survey_analysis SET notes='$notes', survey='$survey', user='$user'");
}
function survey_shorten_name ($name, $numwords) {
$words = explode(" ", $name);
for ($i=0; $i < $numwords; $i++) {
$output .= $words[$i]." ";
}
return $output;
}
function survey_user_summary($course, $user, $mod, $survey) {
global $CFG;
}
function survey_user_outline($course, $user, $mod, $survey) {
if ($answers = get_records_sql("SELECT * FROM survey_answers WHERE survey='$survey->id' AND user='$user->id'")) {
$lastanswer = array_pop($answers);
$result->info = get_string("done", "survey");
$result->time = $lastanswer->time;
return $result;
}
return NULL;
}
function survey_user_complete($course, $user, $mod, $survey) {
global $CFG;
if (survey_already_done($survey->id, $user->id)) {
echo "<IMG SRC=\"$CFG->wwwroot/mod/survey/graph.php?id=$mod->id&sid=$user->id&type=student.png\">";
} else {
print_string("notdone", "survey");
}
}
function survey_print_multi($question) {
GLOBAL $db, $qnum, $checklist, $THEME;
$stripreferthat = get_string("ipreferthat", "survey");
$strifoundthat = get_string("ifoundthat", "survey");
echo "<P> </P>\n";
echo "<P><FONT SIZE=4><B>$question->text</B></FONT></P>";
echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=1 BORDER=0>";
$options = explode( ",", $question->options);
$numoptions = count($options);
$oneanswer = ($question->type == 1 || $question->type == 2) ? true : false;
if ($question->type == 2) {
$P = "P";
} else {
$P = "";
}
if ($oneanswer) {
echo "<TR WIDTH=100% ><TD COLSPAN=2><P>$question->intro</P></TD>";
} else {
echo "<TR WIDTH=100% ><TD COLSPAN=3><P>$question->intro</P></TD>";
}
while (list ($key, $val) = each ($options)) {
echo "<TD width=10% ALIGN=CENTER><FONT SIZE=1><P>$val</P></FONT></TD>\n";
}
echo "<TD ALIGN=CENTER BGCOLOR=\"$THEME->body\"> </TD></TR>\n";
$subquestions = get_records_sql("SELECT * FROM survey_questions WHERE id in ($question->multi) ");
foreach ($subquestions as $q) {
$qnum++;
$bgcolor = survey_question_color($qnum);
echo "<TR BGCOLOR=$bgcolor>";
if ($oneanswer) {
echo "<TD WIDTH=10 VALIGN=top><P><B>$qnum</B></P></TD>";
echo "<TD VALIGN=top><P>$q->text</P></TD>";
for ($i=1;$i<=$numoptions;$i++) {
echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$P$q->id VALUE=$i></TD>";
}
echo "<TD BGCOLOR=white><INPUT TYPE=radio NAME=q$P$q->id VALUE=0 checked></TD>";
$checklist["q$P$q->id"] = $numoptions;
} else {
echo "<TD WIDTH=10 VALIGN=middle rowspan=2><P><B>$qnum</B></P></TD>";
echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>$stripreferthat </FONT></P></TD>";
echo "<TD WIDTH=40% VALIGN=middle rowspan=2><P>$q->text</P></TD>";
for ($i=1;$i<=$numoptions;$i++) {
echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=qP$q->id VALUE=$i></TD>";
}
echo "<TD BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=qP$q->id VALUE=0 checked></TD>";
echo "</TR>";
echo "<TR BGCOLOR=$bgcolor>";
echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>$strifoundthat </P></TD>";
for ($i=1;$i<=$numoptions;$i++) {
echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$q->id VALUE=$i></TD>";
}
echo "<TD WIDTH=5% BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=q$q->id VALUE=0 checked></TD>";
$checklist["qP$q->id"] = $numoptions;
$checklist["q$q->id"] = $numoptions;
}
echo "</TR>\n";
}
echo "</TABLE>";
}
function survey_print_single($question) {
GLOBAL $db, $qnum;
$bgcolor = survey_question_color(0);
$qnum++;
echo "<P> </P>\n";
echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=0>\n";
echo "<TR BGCOLOR=$bgcolor>";
echo "<TD VALIGN=top><B>$qnum</B></TD>";
echo "<TD WIDTH=50% VALIGN=top><P>$question->text</P></TD>\n";
echo "<TD WIDTH=50% VALIGN=top><P><FONT SIZE=+1>\n";
if ($question->type == 0) { // Plain text field
echo "<TEXTAREA ROWS=3 COLS=30 WRAP=virtual NAME=\"$question->id\">$question->options</TEXTAREA>";
} else if ($question->type > 0) { // Choose one of a number
$strchoose = get_string("choose");
echo "<SELECT NAME=$question->id>";
echo "<OPTION VALUE=0 SELECTED>$strchoose...</OPTION>";
$options = explode( ",", $question->options);
foreach ($options as $key => $val) {
$key++;
echo "<OPTION VALUE=\"$key\">$val</OPTION>";
}
echo "</SELECT>";
} else if ($question->type < 0) { // Choose several of a number
$options = explode( ",", $question->options);
echo "<P>THIS TYPE OF QUESTION NOT SUPPORTED YET</P>";
}
echo "</FONT></TD></TR></TABLE>";
}
function survey_question_color($qnum) {
global $THEME;
if ($qnum) {
return $qnum % 2 ? $THEME->cellcontent : $THEME->cellcontent2;
//return $qnum % 2 ? "#CCFFCC" : "#CCFFFF";
} else {
return $THEME->cellcontent;
}
}
?>