示例#1
0
 /**
  * Submit the answers for a given survey.
  *
  * @param int $surveyid the survey instance id
  * @param array $answers the survey answers
  * @return array of warnings and status result
  * @since Moodle 3.0
  * @throws moodle_exception
  */
 public static function submit_answers($surveyid, $answers)
 {
     global $DB, $USER;
     $params = self::validate_parameters(self::submit_answers_parameters(), array('surveyid' => $surveyid, 'answers' => $answers));
     $warnings = array();
     // Request and permission validation.
     $survey = $DB->get_record('survey', array('id' => $params['surveyid']), '*', MUST_EXIST);
     list($course, $cm) = get_course_and_cm_from_instance($survey, 'survey');
     $context = context_module::instance($cm->id);
     self::validate_context($context);
     require_capability('mod/survey:participate', $context);
     if (survey_already_done($survey->id, $USER->id)) {
         throw new moodle_exception("alreadysubmitted", "survey");
     }
     // Build the answers array. Data is cleaned inside the survey_save_answers function.
     $answers = array();
     foreach ($params['answers'] as $answer) {
         $key = $answer['key'];
         $answers[$key] = $answer['value'];
     }
     survey_save_answers($survey, $answers, $course, $context);
     $result = array();
     $result['status'] = true;
     $result['warnings'] = $warnings;
     return $result;
 }
示例#2
0
 /**
  * Test survey_save_answers
  */
 public function test_survey_save_answers()
 {
     global $DB;
     $this->resetAfterTest();
     $this->setAdminUser();
     // Setup test data.
     $course = $this->getDataGenerator()->create_course();
     $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
     $context = context_module::instance($survey->cmid);
     // Build our questions and responses array.
     $realquestions = array();
     $questions = survey_get_questions($survey);
     $i = 5;
     foreach ($questions as $q) {
         if ($q->type > 0) {
             if ($q->multi) {
                 $subquestions = survey_get_subquestions($q);
                 foreach ($subquestions as $sq) {
                     $key = 'q' . $sq->id;
                     $realquestions[$key] = $i % 5 + 1;
                     $i++;
                 }
             } else {
                 $key = 'q' . $q->id;
                 $realquestions[$key] = $i % 5 + 1;
                 $i++;
             }
         }
     }
     $sink = $this->redirectEvents();
     survey_save_answers($survey, $realquestions, $course, $context);
     // Check the stored answers, they must match.
     $dbanswers = $DB->get_records_menu('survey_answers', array('survey' => $survey->id), '', 'question, answer1');
     foreach ($realquestions as $key => $value) {
         $id = str_replace('q', '', $key);
         $this->assertEquals($value, $dbanswers[$id]);
     }
     // Check events.
     $events = $sink->get_events();
     $this->assertCount(1, $events);
     $event = array_shift($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_survey\\event\\response_submitted', $event);
     $this->assertEquals($context, $event->get_context());
     $this->assertEquals($survey->id, $event->other['surveyid']);
 }
示例#3
0
$id = required_param('id', PARAM_INT);
// Course Module ID
if (!($cm = get_coursemodule_from_id('survey', $id))) {
    print_error('invalidcoursemodule');
}
if (!($course = $DB->get_record("course", array("id" => $cm->course)))) {
    print_error('coursemisconf');
}
$PAGE->set_url('/mod/survey/save.php', array('id' => $id));
require_login($course, false, $cm);
$context = context_module::instance($cm->id);
require_capability('mod/survey:participate', $context);
if (!($survey = $DB->get_record("survey", array("id" => $cm->instance)))) {
    print_error('invalidsurveyid', 'survey');
}
$strsurveysaved = get_string('surveysaved', 'survey');
$PAGE->set_title($strsurveysaved);
$PAGE->set_heading($course->fullname);
echo $OUTPUT->header();
echo $OUTPUT->heading($survey->name);
if (survey_already_done($survey->id, $USER->id)) {
    notice(get_string("alreadysubmitted", "survey"), get_local_referer(false));
    exit;
}
survey_save_answers($survey, $formdata, $course, $context);
$params = array('context' => $context, 'courseid' => $course->id, 'other' => array('surveyid' => $survey->id));
$event = \mod_survey\event\response_submitted::create($params);
$event->trigger();
// Print the page and finish up.
notice(get_string("thanksforanswers", "survey", $USER->firstname), "{$CFG->wwwroot}/course/view.php?id={$course->id}");
exit;