Skip to content

Commit

Permalink
Merge branch 'MDL-41039-master' of git://github.com/FMCorz/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed Oct 1, 2013
2 parents 6579a7b + 0a8b091 commit 18f04ab
Show file tree
Hide file tree
Showing 9 changed files with 743 additions and 112 deletions.
52 changes: 21 additions & 31 deletions mod/quiz/attemptlib.php
Expand Up @@ -1422,7 +1422,7 @@ public function process_finish($timestamp, $processsubmitted) {
quiz_save_best_grade($this->get_quiz(), $this->attempt->userid);

// Trigger event.
$this->fire_state_transition_event('quiz_attempt_submitted', $timestamp);
$this->fire_state_transition_event('\mod_quiz\event\attempt_submitted', $timestamp);

// Tell any access rules that care that the attempt is over.
$this->get_access_manager($timestamp)->current_attempt_finished();
Expand Down Expand Up @@ -1459,7 +1459,7 @@ public function process_going_overdue($timestamp, $studentisonline) {
$this->attempt->timecheckstate = $timestamp;
$DB->update_record('quiz_attempts', $this->attempt);

$this->fire_state_transition_event('quiz_attempt_overdue', $timestamp);
$this->fire_state_transition_event('\mod_quiz\event\attempt_becameoverdue', $timestamp);

$transaction->allow_commit();
}
Expand All @@ -1478,45 +1478,35 @@ public function process_abandon($timestamp, $studentisonline) {
$this->attempt->timecheckstate = null;
$DB->update_record('quiz_attempts', $this->attempt);

$this->fire_state_transition_event('quiz_attempt_abandoned', $timestamp);
$this->fire_state_transition_event('\mod_quiz\event\attempt_abandoned', $timestamp);

$transaction->allow_commit();
}

/**
* Fire a state transition event.
* @param string $event the type of event. Should be listed in db/events.php.
* the same event information.
* @param string $eventclass the event class name.
* @param int $timestamp the timestamp to include in the event.
* @return void
*/
protected function fire_state_transition_event($event, $timestamp) {
protected function fire_state_transition_event($eventclass, $timestamp) {
global $USER;

// Trigger event.
$eventdata = new stdClass();
$eventdata->component = 'mod_quiz';
$eventdata->attemptid = $this->attempt->id;
$eventdata->timestamp = $timestamp;
$eventdata->userid = $this->attempt->userid;
$eventdata->quizid = $this->get_quizid();
$eventdata->cmid = $this->get_cmid();
$eventdata->courseid = $this->get_courseid();

// I don't think if (CLI_SCRIPT) is really the right logic here. The
// question is really 'is $USER currently set to a real user', but I cannot
// see standard Moodle function to answer that question. For example,
// cron fakes $USER.
if (CLI_SCRIPT) {
$eventdata->submitterid = null;
} else {
$eventdata->submitterid = $USER->id;
}

if ($event == 'quiz_attempt_submitted') {
// Backwards compatibility for this event type. $eventdata->timestamp is now preferred.
$eventdata->timefinish = $timestamp;
}

events_trigger($event, $eventdata);
$params = array(
'context' => $this->get_quizobj()->get_context(),
'courseid' => $this->get_courseid(),
'objectid' => $this->attempt->id,
'relateduserid' => $this->attempt->userid,
'other' => array(
'submitterid' => CLI_SCRIPT ? null : $USER->id
)
);

$event = $eventclass::create($params);
$event->add_record_snapshot('quiz', $this->get_quiz());
$event->add_record_snapshot('quiz_attempts', $this->get_attempt());
$event->trigger();
}

/**
Expand Down
114 changes: 114 additions & 0 deletions mod/quiz/classes/event/attempt_abandoned.php
@@ -0,0 +1,114 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Quiz module event class.
*
* @package mod_quiz
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_quiz\event;
defined('MOODLE_INTERNAL') || die();

/**
* Event for when a quiz attempt is abandoned.
*
* @package mod_quiz
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class attempt_abandoned extends \core\event\base {

/**
* Init method.
*/
protected function init() {
$this->data['objecttable'] = 'quiz_attempts';
$this->data['crud'] = 'u';
$this->data['level'] = self::LEVEL_PARTICIPATING;
}

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return 'A quiz with the id of ' . $this->other['quizid'] . ' has been marked as abandoned for the user with the id of ' .
$this->relateduserid;
}

/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventquizattemptabandoned', 'mod_quiz');
}

/**
* Does this event replace a legacy event?
*
* @return string legacy event name
*/
static public function get_legacy_eventname() {
return 'quiz_attempt_abandoned';
}

/**
* Returns relevant URL.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/quiz/review.php', array('attempt' => $this->objectid));
}

/**
* Legacy event data if get_legacy_eventname() is not empty.
*
* @return stdClass
*/
protected function get_legacy_eventdata() {
$attempt = $this->get_record_snapshot('quiz_attempts', $this->objectid);

$legacyeventdata = new \stdClass();
$legacyeventdata->component = 'mod_quiz';
$legacyeventdata->attemptid = $this->objectid;
$legacyeventdata->timestamp = $attempt->timemodified;
$legacyeventdata->userid = $this->relateduserid;
$legacyeventdata->quizid = $attempt->quiz;
$legacyeventdata->cmid = $this->context->instanceid;
$legacyeventdata->courseid = $this->courseid;
$legacyeventdata->submitterid = $this->other['submitterid'];

return $legacyeventdata;

}

/**
* Custom validation.
*/
protected function validate_data() {
if (!array_key_exists('submitterid', $this->other)) {
throw new \coding_exception('Other must contain the key submitterid');
} else if (!isset($this->relateduserid)) {
throw new \coding_exception('relateduserid must be set');
}
}
}
119 changes: 119 additions & 0 deletions mod/quiz/classes/event/attempt_becameoverdue.php
@@ -0,0 +1,119 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Quiz module event class.
*
* @package mod_quiz
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_quiz\event;
defined('MOODLE_INTERNAL') || die();

/**
* Event for when a quiz attempt is overdue.
*
* Please note that the name of this event is not following the event naming convention.
* Its name should not be used as a reference for other events to be created.
*
* @package mod_quiz
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class attempt_becameoverdue extends \core\event\base {

/**
* Init method.
*/
protected function init() {
$this->data['objecttable'] = 'quiz_attempts';
$this->data['crud'] = 'u';
$this->data['level'] = self::LEVEL_PARTICIPATING;
}

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return 'A quiz with the id of ' . $this->other['quizid'] . ' has been marked as overdue for the user with ' .
'the id of '. $this->relateduserid;
}

/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventquizattempttimelimitexceeded', 'mod_quiz');
}

/**
* Does this event replace a legacy event?
*
* @return string legacy event name
*/
static public function get_legacy_eventname() {
return 'quiz_attempt_overdue';
}

/**
* Returns relevant URL.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/quiz/review.php', array('attempt' => $this->objectid));
}

/**
* Legacy event data if get_legacy_eventname() is not empty.
*
* @return stdClass
*/
protected function get_legacy_eventdata() {
$attempt = $this->get_record_snapshot('quiz_attempts', $this->objectid);

$legacyeventdata = new \stdClass();
$legacyeventdata->component = 'mod_quiz';
$legacyeventdata->attemptid = $this->objectid;
$legacyeventdata->timestamp = $attempt->timemodified;
$legacyeventdata->userid = $this->relateduserid;
$legacyeventdata->quizid = $attempt->quiz;
$legacyeventdata->cmid = $this->context->instanceid;
$legacyeventdata->courseid = $this->courseid;
$legacyeventdata->submitterid = $this->other['submitterid'];

return $legacyeventdata;
}

/**
* Custom validation.
*
* @throws coding_exception
* @return void
*/
protected function validate_data() {
if (!array_key_exists('submitterid', $this->other)) {
throw new \coding_exception('Other must contain the key submitterid');
} else if (!isset($this->relateduserid)) {
throw new \coding_exception('relateduserid must be set');
}
}
}

0 comments on commit 18f04ab

Please sign in to comment.