3 require_once('../../config.php');
4 require_once('lib.php');
7 // Make sure this is a legitimate posting
9 if (!$formdata = data_submitted("$CFG->wwwroot/mod/survey/view.php")) {
10 error("You are not supposed to use this script like that.");
13 $id = required_param('id', PARAM_INT
); // Course Module ID
15 if (! $cm = get_coursemodule_from_id('survey', $id)) {
16 error("Course Module ID was incorrect");
19 if (! $course = get_record("course", "id", $cm->course
)) {
20 error("Course is misconfigured");
23 require_login($course->id
, false, $cm);
25 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
26 require_capability('mod/survey:participate', $context);
28 if (! $survey = get_record("survey", "id", $cm->instance
)) {
29 error("Survey ID was incorrect");
32 add_to_log($course->id
, "survey", "submit", "view.php?id=$cm->id", "$survey->id", "$cm->id");
34 $strsurveysaved = get_string('surveysaved', 'survey');
36 $navigation = build_navigation('', $cm);
37 print_header_simple("$strsurveysaved", "", $navigation, "");
40 if (survey_already_done($survey->id
, $USER->id
)) {
41 notice(get_string("alreadysubmitted", "survey"), $_SERVER["HTTP_REFERER"]);
46 // Sort through the data and arrange it
47 // This is necessary because some of the questions
48 // may have two answers, eg Question 1 -> 1 and P1
52 foreach ($formdata as $key => $val) {
53 if ($key <> "userid" && $key <> "id") {
54 if ( substr($key,0,1) == "q") {
55 $key = clean_param(substr($key,1), PARAM_ALPHANUM
); // keep everything but the 'q', number or Pnumber
57 if ( substr($key,0,1) == "P") {
58 $realkey = (int) substr($key,1);
59 $answers[$realkey][1] = $val;
61 $answers[$key][0] = $val;
67 // Now store the data.
70 foreach ($answers as $key => $val) {
72 $newdata->time
= $timenow;
73 $newdata->userid
= $USER->id
;
74 $newdata->survey
= $survey->id
;
75 $newdata->question
= $key;
76 if (!empty($val[0])) {
77 $newdata->answer1
= $val[0];
79 $newdata->answer1
= "";
81 if (!empty($val[1])) {
82 $newdata->answer2
= $val[1];
84 $newdata->answer2
= "";
87 if (! insert_record("survey_answers", $newdata)) {
88 error("Encountered a problem trying to store your results. Sorry.");
92 // Print the page and finish up.
94 notice(get_string("thanksforanswers","survey", $USER->firstname
), "$CFG->wwwroot/course/view.php?id=$course->id");