forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
save.php
97 lines (69 loc) · 2.72 KB
/
save.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
<?php // $Id$
require_once('../../config.php');
require_once('lib.php');
// Make sure this is a legitimate posting
if (!$formdata = data_submitted("$CFG->wwwroot/mod/survey/view.php")) {
print_error("You are not supposed to use this script like that.");
}
$id = required_param('id', PARAM_INT); // Course Module ID
if (! $cm = get_coursemodule_from_id('survey', $id)) {
print_error("Course Module ID was incorrect");
}
if (! $course = get_record("course", "id", $cm->course)) {
print_error("Course is misconfigured");
}
require_login($course->id, false, $cm);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
require_capability('mod/survey:participate', $context);
if (! $survey = get_record("survey", "id", $cm->instance)) {
print_error("Survey ID was incorrect");
}
add_to_log($course->id, "survey", "submit", "view.php?id=$cm->id", "$survey->id", "$cm->id");
$navigation = build_navigation($strsurveysaved, $cm);
print_header_simple("$strsurveysaved", "", $navigation, "");
if (survey_already_done($survey->id, $USER->id)) {
notice(get_string("alreadysubmitted", "survey"), $_SERVER["HTTP_REFERER"]);
exit;
}
// Sort through the data and arrange it
// This is necessary because some of the questions
// may have two answers, eg Question 1 -> 1 and P1
$answers = array();
foreach ($formdata as $key => $val) {
if ($key <> "userid" && $key <> "id") {
if ( substr($key,0,1) == "q") {
$key = clean_param(substr($key,1), PARAM_ALPHANUM); // keep everything but the 'q', number or Pnumber
}
if ( substr($key,0,1) == "P") {
$realkey = (int) substr($key,1);
$answers[$realkey][1] = $val;
} else {
$answers[$key][0] = $val;
}
}
}
// Now store the data.
$timenow = time();
foreach ($answers as $key => $val) {
$newdata->time = $timenow;
$newdata->userid = $USER->id;
$newdata->survey = $survey->id;
$newdata->question = $key;
if (!empty($val[0])) {
$newdata->answer1 = $val[0];
} else {
$newdata->answer1 = "";
}
if (!empty($val[1])) {
$newdata->answer2 = $val[1];
} else {
$newdata->answer2 = "";
}
if (! insert_record("survey_answers", $newdata)) {
print_error("Encountered a problem trying to store your results. Sorry.");
}
}
// Print the page and finish up.
notice(get_string("thanksforanswers","survey", $USER->firstname), "$CFG->wwwroot/course/view.php?id=$course->id");
exit;
?>