MDL-52888 mod_quiz: New Web Service mod_quiz_view_attempt_review

This commit is contained in:
Juan Leyva 2016-01-25 15:19:26 +01:00
parent d9ef6ae0c2
commit 3e5c19a0ed
6 changed files with 120 additions and 13 deletions

View File

@ -2246,6 +2246,27 @@ class quiz_attempt {
$event->trigger();
}
/**
* Trigger the attempt_reviewed event.
*
* @since Moodle 3.1
*/
public function fire_attempt_reviewed_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_reviewed::create($params);
$event->add_record_snapshot('quiz_attempts', $this->get_attempt());
$event->trigger();
}
}

View File

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

View File

@ -146,4 +146,13 @@ $functions = array(
'capabilities' => 'mod/quiz:attempt',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
'mod_quiz_view_attempt_review' => array(
'classname' => 'mod_quiz_external',
'methodname' => 'view_attempt_review',
'description' => 'Trigger the attempt reviewed event.',
'type' => 'write',
'capabilities' => 'mod/quiz:reviewmyattempts',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
);

View File

@ -259,15 +259,4 @@ $PAGE->blocks->add_fake_block($navbc, reset($regions));
echo $output->review_page($attemptobj, $slots, $page, $showall, $lastpage, $options, $summarydata);
// Trigger an event for this review.
$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_reviewed::create($params);
$event->add_record_snapshot('quiz_attempts', $attemptobj->get_attempt());
$event->trigger();
$attemptobj->fire_attempt_reviewed_event();

View File

@ -1329,4 +1329,37 @@ class mod_quiz_external_testcase extends externallib_advanced_testcase {
}
/**
* Test test_view_attempt_summary
*/
public function test_view_attempt_review() {
global $DB;
// Create a new quiz with two questions and one attempt finished.
list($quiz, $context, $quizobj, $attempt, $attemptobj, $quba) = $this->create_quiz_with_questions(true, true);
// Test user with full capabilities.
$this->setUser($this->student);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$result = mod_quiz_external::view_attempt_review($attempt->id, 0);
$result = external_api::clean_returnvalue(mod_quiz_external::view_attempt_review_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_reviewed', $event);
$this->assertEquals($context, $event->get_context());
$moodlequiz = new \moodle_url('/mod/quiz/review.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 = 2015111614;
$plugin->version = 2015111615;
$plugin->requires = 2015111000;
$plugin->component = 'mod_quiz';
$plugin->cron = 60;