ratings MDL-21657 Improving capabilities for ratings.
[moodle.git] / mod / survey / save.php
CommitLineData
a5cb6242 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * This file is responsible for saving the results of a users survey and displaying
20 * the final message.
21 *
22 * @package mod-survey
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
f9903ed0 26
f950af3c 27 require_once('../../config.php');
28 require_once('lib.php');
f9903ed0 29
30
31// Make sure this is a legitimate posting
32
c2dcbfe3 33 if (!$formdata = data_submitted() or !confirm_sesskey()) {
83b10e57 34 print_error('cannotcallscript');
f9903ed0 35 }
36
2d5b322c 37 $id = required_param('id', PARAM_INT); // Course Module ID
38
f9d5371b 39 if (! $cm = get_coursemodule_from_id('survey', $id)) {
83b10e57 40 print_error('invalidcoursemodule');
f9903ed0 41 }
42
deb3a60d 43 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
83b10e57 44 print_error('coursemisconf');
f9903ed0 45 }
46
a6855934 47 $PAGE->set_url('/mod/survey/save.php', array('id'=>$id));
ec81373f 48 require_login($course->id, false, $cm);
e5dd8e3b 49
bee7ee38 50 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
0468976c 51 require_capability('mod/survey:participate', $context);
e5dd8e3b 52
deb3a60d 53 if (! $survey = $DB->get_record("survey", array("id"=>$cm->instance))) {
83b10e57 54 print_error('invalidsurveyid', 'survey');
f9903ed0 55 }
56
8f583218 57 add_to_log($course->id, "survey", "submit", "view.php?id=$cm->id", "$survey->id", "$cm->id");
f9903ed0 58
9fa45d37 59 $strsurveysaved = get_string('surveysaved', 'survey');
60
207e24b8 61 $PAGE->set_title($strsurveysaved);
62 echo $OUTPUT->header();
dc0e9265 63
f9903ed0 64 if (survey_already_done($survey->id, $USER->id)) {
607809b3 65 notice(get_string("alreadysubmitted", "survey"), $_SERVER["HTTP_REFERER"]);
f9903ed0 66 exit;
67 }
68
69
70// Sort through the data and arrange it
ec81373f 71// This is necessary because some of the questions
f9903ed0 72// may have two answers, eg Question 1 -> 1 and P1
73
ec81373f 74 $answers = array();
f9903ed0 75
607809b3 76 foreach ($formdata as $key => $val) {
f9903ed0 77 if ($key <> "userid" && $key <> "id") {
ec81373f 78 if ( substr($key,0,1) == "q") {
8f414ab8 79 $key = clean_param(substr($key,1), PARAM_ALPHANUM); // keep everything but the 'q', number or Pnumber
f9903ed0 80 }
81 if ( substr($key,0,1) == "P") {
8f414ab8 82 $realkey = (int) substr($key,1);
f9903ed0 83 $answers[$realkey][1] = $val;
84 } else {
85 $answers[$key][0] = $val;
86 }
87 }
88 }
ec81373f 89
f9903ed0 90
91// Now store the data.
92
93 $timenow = time();
94 foreach ($answers as $key => $val) {
e323955d 95
96 $newdata->time = $timenow;
ebc3bd2b 97 $newdata->userid = $USER->id;
e323955d 98 $newdata->survey = $survey->id;
99 $newdata->question = $key;
5cc1aff3 100 if (!empty($val[0])) {
101 $newdata->answer1 = $val[0];
102 } else {
103 $newdata->answer1 = "";
104 }
105 if (!empty($val[1])) {
106 $newdata->answer2 = $val[1];
107 } else {
108 $newdata->answer2 = "";
109 }
e323955d 110
6d40f12e 111 $DB->insert_record("survey_answers", $newdata);
f9903ed0 112 }
113
114// Print the page and finish up.
115
98899fc0 116 notice(get_string("thanksforanswers","survey", $USER->firstname), "$CFG->wwwroot/course/view.php?id=$course->id");
ec81373f 117
f9903ed0 118 exit;
ec81373f 119
f9903ed0 120
e5dd8e3b 121