[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/survey/ -> save.php (source)

   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   */
  26  
  27      require_once('../../config.php');
  28      require_once ('lib.php');
  29  
  30  
  31  // Make sure this is a legitimate posting
  32  
  33      if (!$formdata = data_submitted() or !confirm_sesskey()) {
  34          print_error('cannotcallscript');
  35      }
  36  
  37      $id = required_param('id', PARAM_INT);    // Course Module ID
  38  
  39      if (! $cm = get_coursemodule_from_id('survey', $id)) {
  40          print_error('invalidcoursemodule');
  41      }
  42  
  43      if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
  44          print_error('coursemisconf');
  45      }
  46  
  47      $PAGE->set_url('/mod/survey/save.php', array('id'=>$id));
  48      require_login($course, false, $cm);
  49  
  50      $context = context_module::instance($cm->id);
  51      require_capability('mod/survey:participate', $context);
  52  
  53      if (! $survey = $DB->get_record("survey", array("id"=>$cm->instance))) {
  54          print_error('invalidsurveyid', 'survey');
  55      }
  56  
  57      $params = array(
  58          'context' => $context,
  59          'courseid' => $course->id,
  60          'other' => array('surveyid' => $survey->id)
  61      );
  62      $event = \mod_survey\event\response_submitted::create($params);
  63      $event->trigger();
  64  
  65      $strsurveysaved = get_string('surveysaved', 'survey');
  66  
  67      $PAGE->set_title($strsurveysaved);
  68      $PAGE->set_heading($course->fullname);
  69      echo $OUTPUT->header();
  70      echo $OUTPUT->heading($survey->name);
  71  
  72      if (survey_already_done($survey->id, $USER->id)) {
  73          notice(get_string("alreadysubmitted", "survey"), $_SERVER["HTTP_REFERER"]);
  74          exit;
  75      }
  76  
  77  
  78  // Sort through the data and arrange it
  79  // This is necessary because some of the questions
  80  // may have two answers, eg Question 1 -> 1 and P1
  81  
  82      $answers = array();
  83  
  84      foreach ($formdata as $key => $val) {
  85          if ($key <> "userid" && $key <> "id") {
  86              if ( substr($key,0,1) == "q") {
  87                  $key = clean_param(substr($key,1), PARAM_ALPHANUM);   // keep everything but the 'q', number or Pnumber
  88              }
  89              if ( substr($key,0,1) == "P") {
  90                  $realkey = (int) substr($key,1);
  91                  $answers[$realkey][1] = $val;
  92              } else {
  93                  $answers[$key][0] = $val;
  94              }
  95          }
  96      }
  97  
  98  
  99  // Now store the data.
 100  
 101      $timenow = time();
 102      foreach ($answers as $key => $val) {
 103          if ($key != 'sesskey') {
 104              $newdata = new stdClass();
 105              $newdata->time = $timenow;
 106              $newdata->userid = $USER->id;
 107              $newdata->survey = $survey->id;
 108              $newdata->question = $key;
 109              if (!empty($val[0])) {
 110                  $newdata->answer1 = $val[0];
 111              } else {
 112                  $newdata->answer1 = "";
 113              }
 114              if (!empty($val[1])) {
 115                  $newdata->answer2 = $val[1];
 116              } else {
 117                  $newdata->answer2 = "";
 118              }
 119  
 120              $DB->insert_record("survey_answers", $newdata);
 121          }
 122      }
 123  
 124  // Print the page and finish up.
 125  
 126      notice(get_string("thanksforanswers","survey", $USER->firstname), "$CFG->wwwroot/course/view.php?id=$course->id");
 127  
 128      exit;
 129  
 130  
 131  


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1