Esempio n. 1
0
// 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');
}
$params = array('context' => $context, 'courseid' => $course->id, 'other' => array('surveyid' => $survey->id));
$event = \mod_survey\event\response_submitted::create($params);
$event->trigger();
$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"), clean_param($_SERVER["HTTP_REFERER"], PARAM_LOCALURL));
    exit;
}
// Sort through the data and arrange it
// This is necessary because some of the questions
// may have two answers, eg Question 1 -> 1 and P1
$answers = array();
foreach ($formdata as $key => $val) {
Esempio n. 2
0
/**
 * Save the answer for the given survey
 *
 * @param  stdClass $survey   a survey object
 * @param  array $answersrawdata the answers to be saved
 * @param  stdClass $course   a course object (required for trigger the submitted event)
 * @param  stdClass $context  a context object (required for trigger the submitted event)
 * @since Moodle 3.0
 */
function survey_save_answers($survey, $answersrawdata, $course, $context) {
    global $DB, $USER;

    $answers = array();

    // Sort through the data and arrange it.
    // This is necessary because some of the questions may have two answers, eg Question 1 -> 1 and P1.
    foreach ($answersrawdata as $key => $val) {
        if ($key != "userid" && $key != "id") {
            if (substr($key, 0, 1) == "q") {
                $key = clean_param(substr($key, 1), PARAM_ALPHANUM);   // Keep everything but the 'q', number or P number.
            }
            if (substr($key, 0, 1) == "P") {
                $realkey = (int) substr($key, 1);
                $answers[$realkey][1] = $val;
            } else {
                $answers[$key][0] = $val;
            }
        }
    }

    // Now store the data.
    $timenow = time();
    $answerstoinsert = array();
    foreach ($answers as $key => $val) {
        if ($key != 'sesskey') {
            $newdata = new stdClass();
            $newdata->time = $timenow;
            $newdata->userid = $USER->id;
            $newdata->survey = $survey->id;
            $newdata->question = $key;
            if (!empty($val[0])) {
                $newdata->answer1 = $val[0];
            } else {
                $newdata->answer1 = "";
            }
            if (!empty($val[1])) {
                $newdata->answer2 = $val[1];
            } else {
                $newdata->answer2 = "";
            }

            $answerstoinsert[] = $newdata;
        }
    }

    if (!empty($answerstoinsert)) {
        $DB->insert_records("survey_answers", $answerstoinsert);
    }

    $params = array(
        'context' => $context,
        'courseid' => $course->id,
        'other' => array('surveyid' => $survey->id)
    );
    $event = \mod_survey\event\response_submitted::create($params);
    $event->trigger();
}
Esempio n. 3
0
 /**
  * Test response submitted event.
  */
 public function test_response_submitted()
 {
     // There is no proper API to call to generate chapters for a book, so what we are
     // doing here is simply making sure that the events returns the right information.
     $course = $this->getDataGenerator()->create_course();
     $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
     $params = array('context' => context_module::instance($survey->cmid), 'courseid' => $course->id, 'other' => array('surveyid' => $survey->id));
     $event = \mod_survey\event\response_submitted::create($params);
     // Triggering and capturing the event.
     $sink = $this->redirectEvents();
     $event->trigger();
     $events = $sink->get_events();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_survey\\event\\response_submitted', $event);
     $this->assertEquals(context_module::instance($survey->cmid), $event->get_context());
     $this->assertEquals($survey->id, $event->other['surveyid']);
     $expected = array($course->id, "survey", "submit", 'view.php?id=' . $survey->cmid, $survey->id, $survey->cmid);
     $this->assertEventLegacyLogData($expected, $event);
     $this->assertEventContextNotUsed($event);
 }