Skip to content

Commit

Permalink
Merge branch 'MDL-52888-master' of git://github.com/jleyva/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Mar 21, 2016
2 parents 7cc45b2 + 3e5c19a commit 3720f10
Show file tree
Hide file tree
Showing 8 changed files with 421 additions and 50 deletions.
23 changes: 4 additions & 19 deletions mod/quiz/attempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,7 @@
}

// Log this page view.
$params = array(
'objectid' => $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_viewed::create($params);
$event->add_record_snapshot('quiz_attempts', $attemptobj->get_attempt());
$event->trigger();
$attemptobj->fire_attempt_viewed_event();

// Get the list of questions needed by this page.
$slots = $attemptobj->get_slots($page);
Expand All @@ -116,13 +105,9 @@
throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'noquestionsfound');
}

// Update attempt page.
if ($attemptobj->get_currentpage() != $page) {
if ($attemptobj->get_navigation_method() == QUIZ_NAVMETHOD_SEQ && $attemptobj->get_currentpage() > $page) {
// Prevent out of sequence access.
redirect($attemptobj->start_attempt_url(null, $attemptobj->get_currentpage()));
}
$DB->set_field('quiz_attempts', 'currentpage', $page, array('id' => $attemptid));
// Update attempt page, redirecting the user if $page is not valid.
if (!$attemptobj->set_currentpage($page)) {
redirect($attemptobj->start_attempt_url(null, $attemptobj->get_currentpage()));
}

// Initialise the JavaScript.
Expand Down
97 changes: 97 additions & 0 deletions mod/quiz/attemptlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,103 @@ public function process_attempt($timenow, $finishattempt, $timeup, $thispage) {
return $becomingabandoned ? self::ABANDONED : self::FINISHED;
}

/**
* Check a page access to see if is an out of sequence access.
*
* @param int $page page number
* @return boolean false is is an out of sequence access, true otherwise.
* @since Moodle 3.1
*/
public function check_page_access($page) {
global $DB;

if ($this->get_currentpage() != $page) {
if ($this->get_navigation_method() == QUIZ_NAVMETHOD_SEQ && $this->get_currentpage() > $page) {
return false;
}
}
return true;
}

/**
* Update attempt page.
*
* @param int $page page number
* @return boolean true if everything was ok, false otherwise (out of sequence access).
* @since Moodle 3.1
*/
public function set_currentpage($page) {
global $DB;

if ($this->check_page_access($page)) {
$DB->set_field('quiz_attempts', 'currentpage', $page, array('id' => $this->get_attemptid()));
return true;
}
return false;
}

/**
* Trigger the attempt_viewed event.
*
* @since Moodle 3.1
*/
public function fire_attempt_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_viewed::create($params);
$event->add_record_snapshot('quiz_attempts', $this->get_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();
}

/**
* 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();
}

}


Expand Down
181 changes: 175 additions & 6 deletions mod/quiz/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -845,12 +845,8 @@ protected static function validate_attempt($params, $checkaccessrules = true, $f
}

// Prevent out of sequence access.
if ($attemptobj->get_currentpage() != $params['page']) {
if ($attemptobj->get_navigation_method() == QUIZ_NAVMETHOD_SEQ &&
$attemptobj->get_currentpage() > $params['page']) {

throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'Out of sequence access');
}
if (!$attemptobj->check_page_access($params['page'])) {
throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'Out of sequence access');
}

// Check slots.
Expand Down Expand Up @@ -1386,4 +1382,177 @@ public static function get_attempt_review_returns() {
);
}

/**
* Describes the parameters for view_attempt.
*
* @return external_external_function_parameters
* @since Moodle 3.1
*/
public static function view_attempt_parameters() {
return new external_function_parameters (
array(
'attemptid' => new external_value(PARAM_INT, 'attempt id'),
'page' => new external_value(PARAM_INT, 'page number'),
)
);
}

/**
* Trigger the attempt viewed event.
*
* @param int $attemptid attempt id
* @param int $page page number
* @return array of warnings and status result
* @since Moodle 3.1
*/
public static function view_attempt($attemptid, $page) {

$warnings = array();

$params = array(
'attemptid' => $attemptid,
'page' => $page,
);
$params = self::validate_parameters(self::view_attempt_parameters(), $params);
list($attemptobj, $messages) = self::validate_attempt($params);

// Log action.
$attemptobj->fire_attempt_viewed_event();

// Update attempt page, throwing an exception if $page is not valid.
if (!$attemptobj->set_currentpage($params['page'])) {
throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'Out of sequence access');
}

$result = array();
$result['status'] = true;
$result['warnings'] = $warnings;
return $result;
}

/**
* Describes the view_attempt return value.
*
* @return external_single_structure
* @since Moodle 3.1
*/
public static function view_attempt_returns() {
return new external_single_structure(
array(
'status' => new external_value(PARAM_BOOL, 'status: true if success'),
'warnings' => new external_warnings(),
)
);
}

/**
* 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(),
)
);
}

/**
* 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(),
)
);
}

}
27 changes: 27 additions & 0 deletions mod/quiz/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,31 @@
'capabilities' => 'mod/quiz:reviewmyattempts',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),

'mod_quiz_view_attempt' => array(
'classname' => 'mod_quiz_external',
'methodname' => 'view_attempt',
'description' => 'Trigger the attempt viewed event.',
'type' => 'write',
'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)
),

'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)
),
);
13 changes: 1 addition & 12 deletions mod/quiz/review.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,4 @@
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();
13 changes: 1 addition & 12 deletions mod/quiz/summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,4 @@
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();
Loading

0 comments on commit 3720f10

Please sign in to comment.