MDL-51629 mod_survey: New WS mod_survey_submit_answers
[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 *
da9bddbe 22 * @package mod_survey
a5cb6242 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));
cdbea7ee 48 require_login($course, false, $cm);
e5dd8e3b 49
1c969116 50 $context = context_module::instance($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
9fa45d37 57 $strsurveysaved = get_string('surveysaved', 'survey');
58
207e24b8 59 $PAGE->set_title($strsurveysaved);
5617e8c9 60 $PAGE->set_heading($course->fullname);
207e24b8 61 echo $OUTPUT->header();
5def24cf 62 echo $OUTPUT->heading($survey->name);
dc0e9265 63
f9903ed0 64 if (survey_already_done($survey->id, $USER->id)) {
dcee0b94 65 notice(get_string("alreadysubmitted", "survey"), get_local_referer(false));
f9903ed0 66 exit;
67 }
68
55dca60f 69 survey_save_answers($survey, $formdata, $course, $context);
f9903ed0 70
66a582d0
JL
71 $params = array(
72 'context' => $context,
73 'courseid' => $course->id,
74 'other' => array('surveyid' => $survey->id)
75 );
76 $event = \mod_survey\event\response_submitted::create($params);
77 $event->trigger();
78
f9903ed0 79// Print the page and finish up.
80
98899fc0 81 notice(get_string("thanksforanswers","survey", $USER->firstname), "$CFG->wwwroot/course/view.php?id=$course->id");
ec81373f 82
f9903ed0 83 exit;
ec81373f 84
f9903ed0 85
e5dd8e3b 86