Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/survey/save.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027     require_once('../../config.php');
00028     require_once('lib.php');
00029 
00030 
00031 // Make sure this is a legitimate posting
00032 
00033     if (!$formdata = data_submitted() or !confirm_sesskey()) {
00034         print_error('cannotcallscript');
00035     }
00036 
00037     $id = required_param('id', PARAM_INT);    // Course Module ID
00038 
00039     if (! $cm = get_coursemodule_from_id('survey', $id)) {
00040         print_error('invalidcoursemodule');
00041     }
00042 
00043     if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
00044         print_error('coursemisconf');
00045     }
00046 
00047     $PAGE->set_url('/mod/survey/save.php', array('id'=>$id));
00048     require_login($course->id, false, $cm);
00049 
00050     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00051     require_capability('mod/survey:participate', $context);
00052 
00053     if (! $survey = $DB->get_record("survey", array("id"=>$cm->instance))) {
00054         print_error('invalidsurveyid', 'survey');
00055     }
00056 
00057     add_to_log($course->id, "survey", "submit", "view.php?id=$cm->id", "$survey->id", "$cm->id");
00058 
00059     $strsurveysaved = get_string('surveysaved', 'survey');
00060 
00061     $PAGE->set_title($strsurveysaved);
00062     $PAGE->set_heading($course->fullname);
00063     echo $OUTPUT->header();
00064 
00065     if (survey_already_done($survey->id, $USER->id)) {
00066         notice(get_string("alreadysubmitted", "survey"), $_SERVER["HTTP_REFERER"]);
00067         exit;
00068     }
00069 
00070 
00071 // Sort through the data and arrange it
00072 // This is necessary because some of the questions
00073 // may have two answers, eg Question 1 -> 1 and P1
00074 
00075     $answers = array();
00076 
00077     foreach ($formdata as $key => $val) {
00078         if ($key <> "userid" && $key <> "id") {
00079             if ( substr($key,0,1) == "q") {
00080                 $key = clean_param(substr($key,1), PARAM_ALPHANUM);   // keep everything but the 'q', number or Pnumber
00081             }
00082             if ( substr($key,0,1) == "P") {
00083                 $realkey = (int) substr($key,1);
00084                 $answers[$realkey][1] = $val;
00085             } else {
00086                 $answers[$key][0] = $val;
00087             }
00088         }
00089     }
00090 
00091 
00092 // Now store the data.
00093 
00094     $timenow = time();
00095     foreach ($answers as $key => $val) {
00096         if ($key != 'sesskey') {
00097             $newdata = new stdClass();
00098             $newdata->time = $timenow;
00099             $newdata->userid = $USER->id;
00100             $newdata->survey = $survey->id;
00101             $newdata->question = $key;
00102             if (!empty($val[0])) {
00103                 $newdata->answer1 = $val[0];
00104             } else {
00105                 $newdata->answer1 = "";
00106             }
00107             if (!empty($val[1])) {
00108                 $newdata->answer2 = $val[1];
00109             } else {
00110                 $newdata->answer2 = "";
00111             }
00112 
00113             $DB->insert_record("survey_answers", $newdata);
00114         }
00115     }
00116 
00117 // Print the page and finish up.
00118 
00119     notice(get_string("thanksforanswers","survey", $USER->firstname), "$CFG->wwwroot/course/view.php?id=$course->id");
00120 
00121     exit;
00122 
00123 
00124 
 All Data Structures Namespaces Files Functions Variables Enumerations