mirrored from git://git.moodle.org/moodle.git
/
lib.php
487 lines (374 loc) · 15.5 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?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",
);
define("SURVEY_COLLES_ACTUAL", "1");
define("SURVEY_COLLES_PREFERRED", "2");
define("SURVEY_COLLES_PREFERRED_ACTUAL", "3");
define("SURVEY_ATTLS", "4");
define("SURVEY_CIQ", "5");
// STANDARD 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_user_outline($course, $user, $mod, $survey) {
if ($answers = get_records_select("survey_answers", "survey='$survey->id' AND userid='$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)) {
if ($survey->template == SURVEY_CIQ) { // print out answers for critical incidents
$table = NULL;
$table->align = array("left", "left");
$questions = get_records_list("survey_questions", "id", $survey->questions);
$questionorder = explode(",", $survey->questions);
foreach ($questionorder as $key=>$val) {
$question = $questions[$val];
$questiontext = get_string($question->shorttext, "survey");
if ($answer = survey_get_user_answer($survey->id, $question->id, $user->id)) {
$answertext = "$answer->answer1";
} else {
$answertext = "No answer";
}
$table->data[] = array("<b>$questiontext</b>", $answertext);
}
print_table($table);
} else {
survey_print_graph("id=$mod->id&sid=$user->id&type=student.png");
}
} else {
print_string("notdone", "survey");
}
}
function survey_print_recent_activity($course, $isteacher, $timestart) {
global $CFG;
$content = false;
$surveys = NULL;
if (!$logs = get_records_select('log', 'time > \''.$timestart.'\' AND '.
'course = \''.$course->id.'\' AND '.
'module = \'survey\' AND '.
'action = \'submit\' ', 'time ASC')) {
return false;
}
foreach ($logs as $log) {
//Create a temp valid module structure (course,id)
$tempmod->course = $log->course;
$tempmod->id = $log->info;
//Obtain the visible property from the instance
$modvisible = instance_is_visible($log->module,$tempmod);
//Only if the mod is visible
if ($modvisible) {
$surveys[$log->id] = survey_log_info($log);
$surveys[$log->id]->time = $log->time;
$surveys[$log->id]->url = str_replace('&', '&', $log->url);
}
}
if ($surveys) {
$content = true;
print_headline(get_string('newsurveyresponses', 'survey').':');
foreach ($surveys as $survey) {
print_recent_activity_note($survey->time, $survey, $isteacher, $survey->name,
$CFG->wwwroot.'/mod/survey/'.$survey->url);
}
}
return $content;
}
function survey_get_participants($surveyid) {
//Returns the users with data in one survey
//(users with records in survey_analysis and survey_answers, students)
global $CFG;
//Get students from survey_analysis
$st_analysis = get_records_sql("SELECT DISTINCT u.id, u.id
FROM {$CFG->prefix}user u,
{$CFG->prefix}survey_analysis a
WHERE a.survey = '$surveyid' and
u.id = a.userid");
//Get students from survey_answers
$st_answers = get_records_sql("SELECT DISTINCT u.id, u.id
FROM {$CFG->prefix}user u,
{$CFG->prefix}survey_answers a
WHERE a.survey = '$surveyid' and
u.id = a.userid");
//Add st_answers to st_analysis
if ($st_answers) {
foreach ($st_answers as $st_answer) {
$st_analysis[$st_answer->id] = $st_answer;
}
}
//Return st_analysis array (it contains an array of unique users)
return ($st_analysis);
}
// SQL FUNCTIONS ////////////////////////////////////////////////////////
function survey_log_info($log) {
global $CFG;
return get_record_sql("SELECT s.name, u.firstname, u.lastname, u.picture
FROM {$CFG->prefix}survey s,
{$CFG->prefix}user u
WHERE s.id = '$log->info'
AND u.id = '$log->userid'");
}
function survey_get_responses($surveyid, $groupid) {
global $CFG;
if ($groupid) {
$groupsdb = ", {$CFG->prefix}groups_members AS gm";
$groupsql = "AND gm.groupid = $groupid AND u.id = gm.userid";
} else {
$groupsdb = "";
$groupsql = "";
}
return get_records_sql("SELECT MAX(a.time) as time,
u.id, u.firstname, u.lastname, u.picture
FROM {$CFG->prefix}survey_answers AS a,
{$CFG->prefix}user AS u $groupsdb
WHERE a.survey = $surveyid
AND a.userid = u.id $groupsql
GROUP BY u.id, u.firstname, u.lastname, u.picture
ORDER BY time ASC");
}
function survey_get_analysis($survey, $user) {
global $CFG;
return get_record_sql("SELECT notes
FROM {$CFG->prefix}survey_analysis
WHERE survey='$survey'
AND userid='$user'");
}
function survey_update_analysis($survey, $user, $notes) {
global $CFG;
return execute_sql("UPDATE {$CFG->prefix}survey_analysis
SET notes='$notes'
WHERE survey='$survey'
AND userid='$user'");
}
function survey_get_user_answers($surveyid, $questionid, $groupid, $sort="sa.answer1,sa.answer2 ASC") {
global $CFG;
if ($groupid) {
$groupsql = "AND gm.groupid = $groupid AND u.id = gm.userid";
} else {
$groupsql = "";
}
return get_records_sql("SELECT sa.*,u.firstname,u.lastname,u.picture
FROM {$CFG->prefix}survey_answers sa,
{$CFG->prefix}user u,
{$CFG->prefix}groups_members gm
WHERE sa.survey = '$surveyid'
AND sa.question = $questionid
AND u.id = sa.userid $groupsql
ORDER BY $sort");
}
function survey_get_user_answer($surveyid, $questionid, $userid) {
global $CFG;
return get_record_sql("SELECT sa.*
FROM {$CFG->prefix}survey_answers sa
WHERE sa.survey = '$surveyid'
AND sa.question = '$questionid'
AND sa.userid = '$userid'");
}
// MODULE FUNCTIONS ////////////////////////////////////////////////////////
function survey_add_analysis($survey, $user, $notes) {
global $CFG;
$record->survey = $survey;
$record->userid = $user;
$record->notes = $notes;
return insert_record("survey_analysis", $record, false);
}
function survey_already_done($survey, $user) {
return record_exists("survey_answers", "survey", $survey, "userid", $user);
}
function survey_count_responses($surveyid, $groupid) {
if ($responses = survey_get_responses($surveyid, $groupid)) {
return count($responses);
} else {
return 0;
}
}
function survey_print_all_responses($cmid, $results, $courseid) {
$table->head = array ("", get_string("name"), get_string("time"));
$table->align = array ("", "left", "left");
$table->size = array (35, "", "" );
foreach ($results as $a) {
$table->data[] = array(print_user_picture($a->id, $courseid, $a->picture, false, true, false),
"<a href=\"report.php?action=student&student=$a->id&id=$cmid\">".fullname($a)."</a>",
userdate($a->time));
}
print_table($table);
}
function survey_get_template_name($templateid) {
global $db;
if ($templateid) {
if ($ss = get_record("surveys", "id", $templateid)) {
return $ss->name;
}
} else {
return "";
}
}
function survey_shorten_name ($name, $numwords) {
$words = explode(" ", $name);
for ($i=0; $i < $numwords; $i++) {
$output .= $words[$i]." ";
}
return $output;
}
function survey_print_multi($question) {
GLOBAL $db, $qnum, $checklist;
$stripreferthat = get_string("ipreferthat", "survey");
$strifoundthat = get_string("ifoundthat", "survey");
echo "<br />\n";
echo "<font size=\"4\"><b>$question->text</b></font><br />";
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><td colspan=\"2\">$question->intro</td>";
} else {
echo "<tr><td colspan=\"3\">$question->intro</td>";
}
while (list ($key, $val) = each ($options)) {
echo "<td width=\"10%\" align=\"center\"><font size=\"1\">$val</font></td>\n";
}
echo "<td align=\"center\"> </td></tr>\n";
$subquestions = get_records_list("survey_questions", "id", $question->multi);
foreach ($subquestions as $q) {
$qnum++;
$rowclass = survey_question_rowclass($qnum);
if ($q->text) {
$q->text = get_string($q->text, "survey");
}
echo "<tr class=\"$rowclass\">";
if ($oneanswer) {
echo "<td width=\"10\" valign=\"top\"><b>$qnum</b></td>";
echo "<td valign=\"top\">$q->text</td>";
for ($i=1;$i<=$numoptions;$i++) {
echo "<td width=\"10%\" align=\"center\"><input type=\"radio\" name=\"q$P$q->id\" value=\"$i\" alt=\"$i\" /></td>";
}
echo "<td bgcolor=\"white\"><input type=\"radio\" name=\"q$P$q->id\" value=\"0\" checked=\"checked\" alt=\"0\" /></td>";
$checklist["q$P$q->id"] = $numoptions;
} else {
echo "<td width=\"10\" valign=\"middle\" rowspan=\"2\"><b>$qnum</b></td>";
echo "<td width=\"10%\" nowrap=\"nowrap\"><font size=\"1\">$stripreferthat </font></td>";
echo "<td width=\"40%\" valign=\"middle\" rowspan=\"2\">$q->text</td>";
for ($i=1;$i<=$numoptions;$i++) {
echo "<td width=\"10%\" align=\"center\"><input type=\"radio\" name=\"qP$q->id\" value=\"$i\" alt=\"$i\"/></td>";
}
echo "<td><input type=\"radio\" name=\"qP$q->id\" value=\"0\" checked=\"checked\" alt=\"0\" /></td>";
echo "</tr>";
echo "<tr class=\"$rowclass\">";
echo "<td width=\"10%\" nowrap=\"nowrap\"><font size=\"1\">$strifoundthat </font></td>";
for ($i=1;$i<=$numoptions;$i++) {
echo "<td width=\"10%\" align=\"center\"><input type=\"radio\" name=\"q$q->id\" value=\"$i\" alt=\"$i\" /></td>";
}
echo "<td width=\"5%\"><input type=\"radio\" name=\"q$q->id\" value=\"0\" checked=\"checked\" alt=\"0\" /></td>";
$checklist["qP$q->id"] = $numoptions;
$checklist["q$q->id"] = $numoptions;
}
echo "</tr>\n";
}
echo "</table>";
}
function survey_print_single($question) {
GLOBAL $db, $qnum;
$rowclass = survey_question_rowclass(0);
$qnum++;
echo "<br />\n";
echo "<table align=\"center\" width=\"90%\" cellpadding=\"4\" cellspacing=\"0\">\n";
echo "<tr class=\"$rowclass\">";
echo "<td valign=\"top\"><b>$qnum</b></td>";
echo "<td width=\"50%\" valign=\"top\">$question->text</td>\n";
echo "<td width=\"50%\" valign=\"top\"><font size=\"+1\">\n";
if ($question->type == 0) { // Plain text field
echo "<textarea rows=\"3\" cols=\"30\" 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=\"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);
notify("This question type not supported yet");
}
echo "</font></td></tr></table>";
}
function survey_question_rowclass($qnum) {
if ($qnum) {
return $qnum % 2 ? 'r0' : 'r1';
} else {
return 'r0';
}
}
function survey_print_graph($url) {
global $CFG, $SURVEY_GHEIGHT, $SURVEY_GWIDTH;
if (empty($CFG->gdversion)) {
echo "(".get_string("gdneed").")";
} else {
echo "<img height=\"$SURVEY_GHEIGHT\" width=\"$SURVEY_GWIDTH\" border=\"1\"".
" src=\"$CFG->wwwroot/mod/survey/graph.php?$url\" alt=\"\" />";
}
}
?>