navigation MDL-14632 Fixed bug when viewing participants list and incorrect navbar
[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()) {
83b10e57 10 print_error('cannotcallscript');
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)) {
83b10e57 16 print_error('invalidcoursemodule');
f9903ed0 17 }
18
deb3a60d 19 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
83b10e57 20 print_error('coursemisconf');
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))) {
83b10e57 29 print_error('invalidsurveyid', 'survey');
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
207e24b8 36 $PAGE->set_title($strsurveysaved);
37 echo $OUTPUT->header();
dc0e9265 38
f9903ed0 39 if (survey_already_done($survey->id, $USER->id)) {
607809b3 40 notice(get_string("alreadysubmitted", "survey"), $_SERVER["HTTP_REFERER"]);
f9903ed0 41 exit;
42 }
43
44
45// Sort through the data and arrange it
ec81373f 46// This is necessary because some of the questions
f9903ed0 47// may have two answers, eg Question 1 -> 1 and P1
48
ec81373f 49 $answers = array();
f9903ed0 50
607809b3 51 foreach ($formdata as $key => $val) {
f9903ed0 52 if ($key <> "userid" && $key <> "id") {
ec81373f 53 if ( substr($key,0,1) == "q") {
8f414ab8 54 $key = clean_param(substr($key,1), PARAM_ALPHANUM); // keep everything but the 'q', number or Pnumber
f9903ed0 55 }
56 if ( substr($key,0,1) == "P") {
8f414ab8 57 $realkey = (int) substr($key,1);
f9903ed0 58 $answers[$realkey][1] = $val;
59 } else {
60 $answers[$key][0] = $val;
61 }
62 }
63 }
ec81373f 64
f9903ed0 65
66// Now store the data.
67
68 $timenow = time();
69 foreach ($answers as $key => $val) {
e323955d 70
71 $newdata->time = $timenow;
ebc3bd2b 72 $newdata->userid = $USER->id;
e323955d 73 $newdata->survey = $survey->id;
74 $newdata->question = $key;
5cc1aff3 75 if (!empty($val[0])) {
76 $newdata->answer1 = $val[0];
77 } else {
78 $newdata->answer1 = "";
79 }
80 if (!empty($val[1])) {
81 $newdata->answer2 = $val[1];
82 } else {
83 $newdata->answer2 = "";
84 }
e323955d 85
6d40f12e 86 $DB->insert_record("survey_answers", $newdata);
f9903ed0 87 }
88
89// Print the page and finish up.
90
98899fc0 91 notice(get_string("thanksforanswers","survey", $USER->firstname), "$CFG->wwwroot/course/view.php?id=$course->id");
ec81373f 92
f9903ed0 93 exit;
ec81373f 94
f9903ed0 95
96?>