MDL-51703 mod_survey: Trigger response_submitted event in correct place
[moodle.git] / mod / survey / save.php
blob4b3580e7437d02057f780c3f2f3f32d4a7aa7770
1 <?php
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.
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/>.
18 /**
19 * This file is responsible for saving the results of a users survey and displaying
20 * the final message.
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
27 require_once('../../config.php');
28 require_once('lib.php');
31 // Make sure this is a legitimate posting
33 if (!$formdata = data_submitted() or !confirm_sesskey()) {
34 print_error('cannotcallscript');
37 $id = required_param('id', PARAM_INT); // Course Module ID
39 if (! $cm = get_coursemodule_from_id('survey', $id)) {
40 print_error('invalidcoursemodule');
43 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
44 print_error('coursemisconf');
47 $PAGE->set_url('/mod/survey/save.php', array('id'=>$id));
48 require_login($course, false, $cm);
50 $context = context_module::instance($cm->id);
51 require_capability('mod/survey:participate', $context);
53 if (! $survey = $DB->get_record("survey", array("id"=>$cm->instance))) {
54 print_error('invalidsurveyid', 'survey');
57 $strsurveysaved = get_string('surveysaved', 'survey');
59 $PAGE->set_title($strsurveysaved);
60 $PAGE->set_heading($course->fullname);
61 echo $OUTPUT->header();
62 echo $OUTPUT->heading($survey->name);
64 if (survey_already_done($survey->id, $USER->id)) {
65 notice(get_string("alreadysubmitted", "survey"), get_local_referer(false));
66 exit;
70 // Sort through the data and arrange it
71 // This is necessary because some of the questions
72 // may have two answers, eg Question 1 -> 1 and P1
74 $answers = array();
76 foreach ($formdata as $key => $val) {
77 if ($key <> "userid" && $key <> "id") {
78 if ( substr($key,0,1) == "q") {
79 $key = clean_param(substr($key,1), PARAM_ALPHANUM); // keep everything but the 'q', number or Pnumber
81 if ( substr($key,0,1) == "P") {
82 $realkey = (int) substr($key,1);
83 $answers[$realkey][1] = $val;
84 } else {
85 $answers[$key][0] = $val;
91 // Now store the data.
93 $timenow = time();
94 foreach ($answers as $key => $val) {
95 if ($key != 'sesskey') {
96 $newdata = new stdClass();
97 $newdata->time = $timenow;
98 $newdata->userid = $USER->id;
99 $newdata->survey = $survey->id;
100 $newdata->question = $key;
101 if (!empty($val[0])) {
102 $newdata->answer1 = $val[0];
103 } else {
104 $newdata->answer1 = "";
106 if (!empty($val[1])) {
107 $newdata->answer2 = $val[1];
108 } else {
109 $newdata->answer2 = "";
112 $DB->insert_record("survey_answers", $newdata);
116 $params = array(
117 'context' => $context,
118 'courseid' => $course->id,
119 'other' => array('surveyid' => $survey->id)
121 $event = \mod_survey\event\response_submitted::create($params);
122 $event->trigger();
124 // Print the page and finish up.
126 notice(get_string("thanksforanswers","survey", $USER->firstname), "$CFG->wwwroot/course/view.php?id=$course->id");
128 exit;