MDL-52888 mod_quiz: New Web Service mod_quiz_view_attempt_summary

This commit is contained in:
Juan Leyva 2016-01-25 15:08:34 +01:00
parent 899983ee58
commit d9ef6ae0c2
6 changed files with 120 additions and 13 deletions

View File

@ -2225,6 +2225,27 @@ class quiz_attempt {
$event->trigger();
}
/**
* Trigger the attempt_summary_viewed event.
*
* @since Moodle 3.1
*/
public function fire_attempt_summary_viewed_event() {
$params = array(
'objectid' => $this->get_attemptid(),
'relateduserid' => $this->get_userid(),
'courseid' => $this->get_courseid(),
'context' => context_module::instance($this->get_cmid()),
'other' => array(
'quizid' => $this->get_quizid()
)
);
$event = \mod_quiz\event\attempt_summary_viewed::create($params);
$event->add_record_snapshot('quiz_attempts', $this->get_attempt());
$event->trigger();
}
}

View File

@ -1445,4 +1445,59 @@ class mod_quiz_external extends external_api {
);
}
/**
* Describes the parameters for view_attempt_summary.
*
* @return external_external_function_parameters
* @since Moodle 3.1
*/
public static function view_attempt_summary_parameters() {
return new external_function_parameters (
array(
'attemptid' => new external_value(PARAM_INT, 'attempt id'),
)
);
}
/**
* Trigger the attempt summary viewed event.
*
* @param int $attemptid attempt id
* @return array of warnings and status result
* @since Moodle 3.1
*/
public static function view_attempt_summary($attemptid) {
$warnings = array();
$params = array(
'attemptid' => $attemptid,
);
$params = self::validate_parameters(self::view_attempt_summary_parameters(), $params);
list($attemptobj, $messages) = self::validate_attempt($params);
// Log action.
$attemptobj->fire_attempt_summary_viewed_event();
$result = array();
$result['status'] = true;
$result['warnings'] = $warnings;
return $result;
}
/**
* Describes the view_attempt_summary return value.
*
* @return external_single_structure
* @since Moodle 3.1
*/
public static function view_attempt_summary_returns() {
return new external_single_structure(
array(
'status' => new external_value(PARAM_BOOL, 'status: true if success'),
'warnings' => new external_warnings(),
)
);
}
}

View File

@ -137,4 +137,13 @@ $functions = array(
'capabilities' => 'mod/quiz:attempt',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
'mod_quiz_view_attempt_summary' => array(
'classname' => 'mod_quiz_external',
'methodname' => 'view_attempt_summary',
'description' => 'Trigger the attempt summary viewed event.',
'type' => 'write',
'capabilities' => 'mod/quiz:attempt',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
);

View File

@ -93,15 +93,4 @@ $PAGE->set_heading($attemptobj->get_course()->fullname);
echo $output->summary_page($attemptobj, $displayoptions);
// Log this page view.
$params = array(
'objectid' => $attemptobj->get_attemptid(),
'relateduserid' => $attemptobj->get_userid(),
'courseid' => $attemptobj->get_courseid(),
'context' => context_module::instance($attemptobj->get_cmid()),
'other' => array(
'quizid' => $attemptobj->get_quizid()
)
);
$event = \mod_quiz\event\attempt_summary_viewed::create($params);
$event->add_record_snapshot('quiz_attempts', $attemptobj->get_attempt());
$event->trigger();
$attemptobj->fire_attempt_summary_viewed_event();

View File

@ -1296,4 +1296,37 @@ class mod_quiz_external_testcase extends externallib_advanced_testcase {
}
/**
* Test test_view_attempt_summary
*/
public function test_view_attempt_summary() {
global $DB;
// Create a new quiz with two questions and one attempt started.
list($quiz, $context, $quizobj, $attempt, $attemptobj, $quba) = $this->create_quiz_with_questions(true, false);
// Test user with full capabilities.
$this->setUser($this->student);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$result = mod_quiz_external::view_attempt_summary($attempt->id, 0);
$result = external_api::clean_returnvalue(mod_quiz_external::view_attempt_summary_returns(), $result);
$this->assertTrue($result['status']);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = array_shift($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_quiz\event\attempt_summary_viewed', $event);
$this->assertEquals($context, $event->get_context());
$moodlequiz = new \moodle_url('/mod/quiz/summary.php', array('attempt' => $attempt->id));
$this->assertEquals($moodlequiz, $event->get_url());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
}
}

View File

@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2015111613;
$plugin->version = 2015111614;
$plugin->requires = 2015111000;
$plugin->component = 'mod_quiz';
$plugin->cron = 60;