More moving functions around and removal of hardcoded SQL
[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) {
66 $val1 = $val[0]; $val2 = $val[1];
67 if (! $result = $db->Execute("INSERT INTO survey_answers
68 (time, user, survey, question, answer1, answer2)
69 VALUES ('$timenow', '$USER->id', '$survey->id', '$key', '$val1', '$val2')") ) {
70 error("Encountered a problem trying to store your results. Sorry.");
71 }
72 }
73
74// Print the page and finish up.
75
98899fc0 76 $strsurveys = get_string("modulenameplural", "survey");
77 $strsurveysaved = get_string("surveysaved", "survey");
78
79 print_header("$course->shortname: $strsurveysaved", "$course->fullname",
f9903ed0 80 "<A HREF=/course/view.php?id=$course->id>$course->shortname</A> ->
98899fc0 81 <A HREF=index.php?id=$course->id>$strsurveys</A> -> $survey->name -> $strsurveysaved", "");
f9903ed0 82
83
98899fc0 84 notice(get_string("thanksforanswers","survey", $USER->firstname), "$CFG->wwwroot/course/view.php?id=$course->id");
f9903ed0 85
86 exit;
87
88
89?>