MDL-14978 removed ancient upgradelogs script
[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")) {
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
19 if (! $course = get_record("course", "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
bee7ee38 28 if (! $survey = get_record("survey", "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
38e179a4 34 $navigation = build_navigation($strsurveysaved, $cm);
dc0e9265 35 print_header_simple("$strsurveysaved", "", $navigation, "");
36
37
f9903ed0 38 if (survey_already_done($survey->id, $USER->id)) {
607809b3 39 notice(get_string("alreadysubmitted", "survey"), $_SERVER["HTTP_REFERER"]);
f9903ed0 40 exit;
41 }
42
43
44// Sort through the data and arrange it
ec81373f 45// This is necessary because some of the questions
f9903ed0 46// may have two answers, eg Question 1 -> 1 and P1
47
ec81373f 48 $answers = array();
f9903ed0 49
607809b3 50 foreach ($formdata as $key => $val) {
f9903ed0 51 if ($key <> "userid" && $key <> "id") {
ec81373f 52 if ( substr($key,0,1) == "q") {
8f414ab8 53 $key = clean_param(substr($key,1), PARAM_ALPHANUM); // keep everything but the 'q', number or Pnumber
f9903ed0 54 }
55 if ( substr($key,0,1) == "P") {
8f414ab8 56 $realkey = (int) substr($key,1);
f9903ed0 57 $answers[$realkey][1] = $val;
58 } else {
59 $answers[$key][0] = $val;
60 }
61 }
62 }
ec81373f 63
f9903ed0 64
65// Now store the data.
66
67 $timenow = time();
68 foreach ($answers as $key => $val) {
e323955d 69
70 $newdata->time = $timenow;
ebc3bd2b 71 $newdata->userid = $USER->id;
e323955d 72 $newdata->survey = $survey->id;
73 $newdata->question = $key;
5cc1aff3 74 if (!empty($val[0])) {
75 $newdata->answer1 = $val[0];
76 } else {
77 $newdata->answer1 = "";
78 }
79 if (!empty($val[1])) {
80 $newdata->answer2 = $val[1];
81 } else {
82 $newdata->answer2 = "";
83 }
e323955d 84
85 if (! insert_record("survey_answers", $newdata)) {
5a2a5331 86 print_error("Encountered a problem trying to store your results. Sorry.");
f9903ed0 87 }
88 }
89
90// Print the page and finish up.
91
98899fc0 92 notice(get_string("thanksforanswers","survey", $USER->firstname), "$CFG->wwwroot/course/view.php?id=$course->id");
ec81373f 93
f9903ed0 94 exit;
ec81373f 95
f9903ed0 96
97?>