/** * Test choice_view * @return void */ public function test_choice_view() { global $CFG; $this->resetAfterTest(); $this->setAdminUser(); // Setup test data. $course = $this->getDataGenerator()->create_course(); $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id)); $context = context_module::instance($choice->cmid); $cm = get_coursemodule_from_instance('choice', $choice->id); // Trigger and capture the event. $sink = $this->redirectEvents(); choice_view($choice, $course, $cm, $context); $events = $sink->get_events(); $this->assertCount(1, $events); $event = array_shift($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\mod_choice\event\course_module_viewed', $event); $this->assertEquals($context, $event->get_context()); $url = new \moodle_url('/mod/choice/view.php', array('id' => $cm->id)); $this->assertEquals($url, $event->get_url()); $this->assertEventContextNotUsed($event); $this->assertNotEmpty($event->get_name()); }
$reason = current(array_keys($warnings)); throw new moodle_exception($reason, 'choice', '', $warnings[$reason]); } if ($answer) { choice_user_submit_response($answer, $choice, $USER->id, $course, $cm); redirect(new moodle_url('/mod/choice/view.php', array('id' => $cm->id, 'notify' => 'choicesaved', 'sesskey' => sesskey()))); } else { if (empty($answer) and $action === 'makechoice') { // We cannot use the 'makechoice' alone because there might be some legacy renderers without it, // outdated renderers will not get the 'mustchoose' message - bad luck. redirect(new moodle_url('/mod/choice/view.php', array('id' => $cm->id, 'notify' => 'mustchooseone', 'sesskey' => sesskey()))); } } } // Completion and trigger events. choice_view($choice, $course, $cm, $context); echo $OUTPUT->header(); echo $OUTPUT->heading(format_string($choice->name), 2, null); if ($notify and confirm_sesskey()) { if ($notify === 'choicesaved') { echo $OUTPUT->notification(get_string('choicesaved', 'choice'), 'notifysuccess'); } else { if ($notify === 'mustchooseone') { echo $OUTPUT->notification(get_string('mustchooseone', 'choice'), 'notifyproblem'); } } } /// Display the choice and possibly results $eventdata = array(); $eventdata['objectid'] = $choice->id; $eventdata['context'] = $context;
/** * Trigger the course module viewed event and update the module completion status. * * @param int $choiceid the choice instance id * @return array of warnings and status result * @since Moodle 3.0 * @throws moodle_exception */ public static function view_choice($choiceid) { global $CFG; $params = self::validate_parameters(self::view_choice_parameters(), array('choiceid' => $choiceid)); $warnings = array(); // Request and permission validation. if (!($choice = choice_get_choice($params['choiceid']))) { throw new moodle_exception("invalidcoursemodule", "error"); } list($course, $cm) = get_course_and_cm_from_instance($choice, 'choice'); $context = context_module::instance($cm->id); self::validate_context($context); // Trigger course_module_viewed event and completion. choice_view($choice, $course, $cm, $context); $result = array(); $result['status'] = true; $result['warnings'] = $warnings; return $result; }