This file was here by mistake
[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
13 require_variable($id); // Course Module ID
14
15 if (! $cm = get_record("course_modules", "id", $id)) {
16 error("Course Module ID was incorrect");
17 }
18
19 if (! $course = get_record("course", "id", $cm->course)) {
20 error("Course is misconfigured");
21 }
22
23 require_login($course->id);
24
25 if (! $survey = get_record("survey", "id", $cm->instance)) {
26 error("Survey ID was incorrect");
27 }
28
5eb45fa0 29 add_to_log($course->id, "survey", "submit", "view.php?id=$cm->id", "$survey->id");
f9903ed0 30
31 if (survey_already_done($survey->id, $USER->id)) {
32 notice("You've already submitted this survey!", $HTTP_REFERER);
33 exit;
34 }
35
36
37// Sort through the data and arrange it
38// This is necessary because some of the questions
39// may have two answers, eg Question 1 -> 1 and P1
40
41 $answers = array();
42
43 foreach ($HTTP_POST_VARS as $key => $val) {
44 if ($key <> "userid" && $key <> "id") {
45 if ( substr($key,0,1) == "q") {
46 $key = substr($key,1); // keep everything but the 'q'
47 }
48 if ( substr($key,0,1) == "P") {
49 $realkey = substr($key,1);
50 $answers[$realkey][1] = $val;
51 } else {
52 $answers[$key][0] = $val;
53 }
54 }
55 }
56
57
58// Now store the data.
59
60 $timenow = time();
61 foreach ($answers as $key => $val) {
62 $val1 = $val[0]; $val2 = $val[1];
63 if (! $result = $db->Execute("INSERT INTO survey_answers
64 (time, user, survey, question, answer1, answer2)
65 VALUES ('$timenow', '$USER->id', '$survey->id', '$key', '$val1', '$val2')") ) {
66 error("Encountered a problem trying to store your results. Sorry.");
67 }
68 }
69
70// Print the page and finish up.
71
72 print_header("$course->shortname: Survey sent", "$course->fullname",
73 "<A HREF=/course/view.php?id=$course->id>$course->shortname</A> ->
74 <A HREF=index.php?id=$course->id>Surveys</A> -> $survey->name -> Survey sent", "");
75
76
77 notice("Thanks for your answers, $USER->firstname.", "$CFG->wwwroot/course/view.php?id=$course->id");
78
79 exit;
80
81
82?>