Some more styles added
[moodle.git] / mod / survey / save.php
CommitLineData
d1290cec 1<?php // $Id$
f9903ed0 2
f950af3c 3 require_once('../../config.php');
4 require_once('lib.php');
f9903ed0 5
6
7// Make sure this is a legitimate posting
8
607809b3 9 if (!$formdata = data_submitted("$CFG->wwwroot/mod/survey/view.php")) {
f9903ed0 10 error("You are not supposed to use this script like that.");
11 }
12
da5c172a 13 if (isguest()) {
607809b3 14 error("Guests are not allowed to answer surveys", $_SERVER["HTTP_REFERER"]);
da5c172a 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
8f583218 33 add_to_log($course->id, "survey", "submit", "view.php?id=$cm->id", "$survey->id", "$cm->id");
f9903ed0 34
35 if (survey_already_done($survey->id, $USER->id)) {
607809b3 36 notice(get_string("alreadysubmitted", "survey"), $_SERVER["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
607809b3 47 foreach ($formdata as $key => $val) {
f9903ed0 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;
5cc1aff3 71 if (!empty($val[0])) {
72 $newdata->answer1 = $val[0];
73 } else {
74 $newdata->answer1 = "";
75 }
76 if (!empty($val[1])) {
77 $newdata->answer2 = $val[1];
78 } else {
79 $newdata->answer2 = "";
80 }
e323955d 81
82 if (! insert_record("survey_answers", $newdata)) {
f9903ed0 83 error("Encountered a problem trying to store your results. Sorry.");
84 }
85 }
86
87// Print the page and finish up.
88
98899fc0 89 $strsurveys = get_string("modulenameplural", "survey");
90 $strsurveysaved = get_string("surveysaved", "survey");
91
f950af3c 92 print_header_simple("$strsurveysaved", "",
93 "<a href=\"index.php?id=$course->id\">$strsurveys</a> -> $survey->name -> $strsurveysaved", "");
f9903ed0 94
95
98899fc0 96 notice(get_string("thanksforanswers","survey", $USER->firstname), "$CFG->wwwroot/course/view.php?id=$course->id");
f9903ed0 97
98 exit;
99
100
101?>