"MDL-14129, add lang string"
[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
294ce987 9 if (!$formdata = data_submitted()) {
5a2a5331 10 print_error("You are not supposed to use this script like that.");
f9903ed0 11 }
12
2d5b322c 13 $id = required_param('id', PARAM_INT); // Course Module ID
14
f9d5371b 15 if (! $cm = get_coursemodule_from_id('survey', $id)) {
5a2a5331 16 print_error("Course Module ID was incorrect");
f9903ed0 17 }
18
deb3a60d 19 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
5a2a5331 20 print_error("Course is misconfigured");
f9903ed0 21 }
22
ec81373f 23 require_login($course->id, false, $cm);
bbbf2d40 24
bee7ee38 25 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
0468976c 26 require_capability('mod/survey:participate', $context);
bbbf2d40 27
deb3a60d 28 if (! $survey = $DB->get_record("survey", array("id"=>$cm->instance))) {
5a2a5331 29 print_error("Survey ID was incorrect");
f9903ed0 30 }
31
8f583218 32 add_to_log($course->id, "survey", "submit", "view.php?id=$cm->id", "$survey->id", "$cm->id");
f9903ed0 33
9fa45d37 34 $strsurveysaved = get_string('surveysaved', 'survey');
35
36 $navigation = build_navigation('', $cm);
dc0e9265 37 print_header_simple("$strsurveysaved", "", $navigation, "");
38
39
f9903ed0 40 if (survey_already_done($survey->id, $USER->id)) {
607809b3 41 notice(get_string("alreadysubmitted", "survey"), $_SERVER["HTTP_REFERER"]);
f9903ed0 42 exit;
43 }
44
45
46// Sort through the data and arrange it
ec81373f 47// This is necessary because some of the questions
f9903ed0 48// may have two answers, eg Question 1 -> 1 and P1
49
ec81373f 50 $answers = array();
f9903ed0 51
607809b3 52 foreach ($formdata as $key => $val) {
f9903ed0 53 if ($key <> "userid" && $key <> "id") {
ec81373f 54 if ( substr($key,0,1) == "q") {
8f414ab8 55 $key = clean_param(substr($key,1), PARAM_ALPHANUM); // keep everything but the 'q', number or Pnumber
f9903ed0 56 }
57 if ( substr($key,0,1) == "P") {
8f414ab8 58 $realkey = (int) substr($key,1);
f9903ed0 59 $answers[$realkey][1] = $val;
60 } else {
61 $answers[$key][0] = $val;
62 }
63 }
64 }
ec81373f 65
f9903ed0 66
67// Now store the data.
68
69 $timenow = time();
70 foreach ($answers as $key => $val) {
e323955d 71
72 $newdata->time = $timenow;
ebc3bd2b 73 $newdata->userid = $USER->id;
e323955d 74 $newdata->survey = $survey->id;
75 $newdata->question = $key;
5cc1aff3 76 if (!empty($val[0])) {
77 $newdata->answer1 = $val[0];
78 } else {
79 $newdata->answer1 = "";
80 }
81 if (!empty($val[1])) {
82 $newdata->answer2 = $val[1];
83 } else {
84 $newdata->answer2 = "";
85 }
e323955d 86
deb3a60d 87 if (! $DB->insert_record("survey_answers", $newdata)) {
5a2a5331 88 print_error("Encountered a problem trying to store your results. Sorry.");
f9903ed0 89 }
90 }
91
92// Print the page and finish up.
93
98899fc0 94 notice(get_string("thanksforanswers","survey", $USER->firstname), "$CFG->wwwroot/course/view.php?id=$course->id");
ec81373f 95
f9903ed0 96 exit;
ec81373f 97
f9903ed0 98
99?>