Changes to improve robustness of uploads, and make things compatible
[moodle.git] / mod / survey / save.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
3 require('../../config.php');
4 require('lib.php');
5
6
7// Make sure this is a legitimate posting
8
9 if (!isset($HTTP_POST_VARS)) {
10 error("You are not supposed to use this script like that.");
11 }
12
da5c172a 13 if (isguest()) {
14 error("Guests are not allowed to answer surveys", $HTTP_REFERER);
15 }
16
f9903ed0 17 require_variable($id); // Course Module ID
18
19 if (! $cm = get_record("course_modules", "id", $id)) {
20 error("Course Module ID was incorrect");
21 }
22
23 if (! $course = get_record("course", "id", $cm->course)) {
24 error("Course is misconfigured");
25 }
26
27 require_login($course->id);
28
29 if (! $survey = get_record("survey", "id", $cm->instance)) {
30 error("Survey ID was incorrect");
31 }
32
5eb45fa0 33 add_to_log($course->id, "survey", "submit", "view.php?id=$cm->id", "$survey->id");
f9903ed0 34
35 if (survey_already_done($survey->id, $USER->id)) {
98899fc0 36 notice(get_string("alreadysubmitted", "survey"), $HTTP_REFERER);
f9903ed0 37 exit;
38 }
39
40
41// Sort through the data and arrange it
42// This is necessary because some of the questions
43// may have two answers, eg Question 1 -> 1 and P1
44
45 $answers = array();
46
47 foreach ($HTTP_POST_VARS as $key => $val) {
48 if ($key <> "userid" && $key <> "id") {
49 if ( substr($key,0,1) == "q") {
50 $key = substr($key,1); // keep everything but the 'q'
51 }
52 if ( substr($key,0,1) == "P") {
53 $realkey = substr($key,1);
54 $answers[$realkey][1] = $val;
55 } else {
56 $answers[$key][0] = $val;
57 }
58 }
59 }
60
61
62// Now store the data.
63
64 $timenow = time();
65 foreach ($answers as $key => $val) {
e323955d 66
67 $newdata->time = $timenow;
ebc3bd2b 68 $newdata->userid = $USER->id;
e323955d 69 $newdata->survey = $survey->id;
70 $newdata->question = $key;
71 $newdata->answer1 = $val[0];
72 $newdata->answer2 = $val[1];
73
74 if (! insert_record("survey_answers", $newdata)) {
f9903ed0 75 error("Encountered a problem trying to store your results. Sorry.");
76 }
77 }
78
79// Print the page and finish up.
80
98899fc0 81 $strsurveys = get_string("modulenameplural", "survey");
82 $strsurveysaved = get_string("surveysaved", "survey");
83
84 print_header("$course->shortname: $strsurveysaved", "$course->fullname",
f9903ed0 85 "<A HREF=/course/view.php?id=$course->id>$course->shortname</A> ->
98899fc0 86 <A HREF=index.php?id=$course->id>$strsurveys</A> -> $survey->name -> $strsurveysaved", "");
f9903ed0 87
88
98899fc0 89 notice(get_string("thanksforanswers","survey", $USER->firstname), "$CFG->wwwroot/course/view.php?id=$course->id");
f9903ed0 90
91 exit;
92
93
94?>