/** * 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']); }
/** * Test submit_answers */ public function test_submit_answers() { global $DB; // Test user with full capabilities. $this->setUser($this->student); // Build our questions and responses array. $realquestions = array(); $questions = survey_get_questions($this->survey); $i = 5; foreach ($questions as $q) { if ($q->type >= 0) { if ($q->multi) { $subquestions = survey_get_subquestions($q); foreach ($subquestions as $sq) { $realquestions[] = array('key' => 'q' . $sq->id, 'value' => $i % 5 + 1); $i++; } } else { $realquestions[] = array('key' => 'q' . $q->id, 'value' => $i % 5 + 1); $i++; } } } $result = mod_survey_external::submit_answers($this->survey->id, $realquestions); $result = external_api::clean_returnvalue(mod_survey_external::submit_answers_returns(), $result); $this->assertTrue($result['status']); $this->assertCount(0, $result['warnings']); $dbanswers = $DB->get_records_menu('survey_answers', array('survey' => $this->survey->id), '', 'question, answer1'); foreach ($realquestions as $q) { $id = str_replace('q', '', $q['key']); $this->assertEquals($q['value'], $dbanswers[$id]); } // Submit again, we expect an error here. try { mod_survey_external::submit_answers($this->survey->id, $realquestions); $this->fail('Exception expected due to answers already submitted.'); } catch (moodle_exception $e) { $this->assertEquals('alreadysubmitted', $e->errorcode); } // Test user with no capabilities. // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles. assign_capability('mod/survey:participate', CAP_PROHIBIT, $this->studentrole->id, $this->context->id); accesslib_clear_all_caches_for_unit_testing(); try { mod_survey_external::submit_answers($this->survey->id, $realquestions); $this->fail('Exception expected due to missing capability.'); } catch (moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); } // Test not-enrolled user. $usernotenrolled = self::getDataGenerator()->create_user(); $this->setUser($usernotenrolled); try { mod_survey_external::submit_answers($this->survey->id, $realquestions); $this->fail('Exception expected due to not enrolled user.'); } catch (moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } }
echo $OUTPUT->spacer(array('height' => 30, 'width' => 1), true); } } } } echo $OUTPUT->footer(); exit; } echo "<form method=\"post\" action=\"save.php\" id=\"surveyform\">"; echo '<div>'; echo "<input type=\"hidden\" name=\"id\" value=\"{$id}\" />"; echo "<input type=\"hidden\" name=\"sesskey\" value=\"" . sesskey() . "\" />"; echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox boxaligncenter bowidthnormal', 'intro'); echo '<div>' . get_string('allquestionrequireanswer', 'survey') . '</div>'; // Get all the major questions in order. $questions = survey_get_questions($survey); global $qnum; // TODO: ugly globals hack for survey_print_*(). global $checklist; // TODO: ugly globals hack for survey_print_*(). $qnum = 0; $checklist = array(); foreach ($questions as $question) { if ($question->type >= 0) { $question = survey_translate_question($question); if ($question->multi) { survey_print_multi($question); } else { survey_print_single($question); } }
/** * Get the complete list of questions for the survey, including subquestions. * * @param int $surveyid the survey instance id * @return array of warnings and the question list * @since Moodle 3.0 * @throws moodle_exception */ public static function get_questions($surveyid) { global $DB, $USER; $params = self::validate_parameters(self::get_questions_parameters(), array('surveyid' => $surveyid)); $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); $mainquestions = survey_get_questions($survey); foreach ($mainquestions as $question) { if ($question->type >= 0) { // Parent is used in subquestions. $question->parent = 0; $questions[] = survey_translate_question($question); // Check if the question has subquestions. if ($question->multi) { $subquestions = survey_get_subquestions($question); foreach ($subquestions as $sq) { $sq->parent = $question->id; $questions[] = survey_translate_question($sq); } } } } $result = array(); $result['questions'] = $questions; $result['warnings'] = $warnings; return $result; }