Skip to content

Commit

Permalink
MDL-28166 send quiz event notifications asynchronously.
Browse files Browse the repository at this point in the history
This avoids the problem that you cannot send messages in transactions.

It also means that the quiz submission will not be prevented, and the
message will still be sent eventually, if any part of the messaging
system is giving intermittent errors when the student wants to submit
their quiz.
  • Loading branch information
timhunt committed Jul 6, 2011
1 parent 81f8e0f commit cbb8b55
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 145 deletions.
26 changes: 16 additions & 10 deletions mod/quiz/attemptlib.php
Expand Up @@ -995,14 +995,6 @@ public function check_file_access($slot, $reviewing, $contextid, $component,
$component, $filearea, $args, $forcedownload);
}

/**
* Triggers the sending of the notification emails at the end of this attempt.
*/
public function quiz_send_notification_emails() {
quiz_send_notification_emails($this->get_course(), $this->get_quiz(), $this->attempt,
$this->quizobj->get_context(), $this->get_cm());
}

/**
* Get the navigation panel object for this attempt.
*
Expand Down Expand Up @@ -1080,7 +1072,7 @@ public function save_question_flags() {
}

public function finish_attempt($timestamp) {
global $DB;
global $DB, $USER;
$this->quba->process_all_actions($timestamp);
$this->quba->finish_all_questions($timestamp);

Expand All @@ -1093,7 +1085,21 @@ public function finish_attempt($timestamp) {

if (!$this->is_preview()) {
quiz_save_best_grade($this->get_quiz());
$this->quiz_send_notification_emails();

// Trigger event
$eventdata = new stdClass();
$eventdata->component = 'mod_quiz';
$eventdata->attemptid = $this->attempt->id;
$eventdata->timefinish = $this->attempt->timefinish;
$eventdata->userid = $this->attempt->userid;
$eventdata->submitterid = $USER->id;
$eventdata->quizid = $this->get_quizid();
$eventdata->cmid = $this->get_cmid();
$eventdata->courseid = $this->get_courseid();
events_trigger('quiz_attempt_submitted', $eventdata);

// Clear the password check flag in the session.
$this->get_access_manager($timestamp)->clear_password_access();
}
}

Expand Down
4 changes: 2 additions & 2 deletions mod/quiz/db/access.php
Expand Up @@ -149,14 +149,14 @@
'archetypes' => array()
),

// Receive email confirmation of own quiz submission
// Receive a confirmation message of own quiz submission.
'mod/quiz:emailconfirmsubmission' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
'archetypes' => array()
),

// Receive email notification of other peoples quiz submissions
// Receive a notification message of other peoples' quiz submissions.
'mod/quiz:emailnotifysubmission' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
Expand Down
61 changes: 61 additions & 0 deletions mod/quiz/db/events.php
@@ -0,0 +1,61 @@
<?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/>.

/**
* Post-install code for the quiz module.
*
* @package mod
* @subpackage quiz
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/


defined('MOODLE_INTERNAL') || die();


$handlers = array(
// Handle our own quiz_attempt_submitted event, as a way to send confirmation
// messages asynchronously.
'quiz_attempt_submitted' => array (
'handlerfile' => '/mod/quiz/locallib.php',
'handlerfunction' => 'quiz_attempt_submitted_handler',
'schedule' => 'cron',
),
);

/* List of events generated by the quiz module, with the fields on the event object.
quiz_attempt_started
->component = 'mod_quiz';
->attemptid = // The id of the new quiz attempt.
->timestart = // The timestamp of when the attempt was started.
->userid = // The user id that the attempt belongs to.
->quizid = // The quiz id of the quiz the attempt belongs to.
->cmid = // The course_module id of the quiz the attempt belongs to.
->courseid = // The course id of the course the quiz belongs to.
quiz_attempt_submitted
->component = 'mod_quiz';
->attemptid = // The id of the quiz attempt that was submitted.
->timefinish = // The timestamp of when the attempt was submitted.
->userid = // The user id that the attempt belongs to.
->submitterid = // The user id of the user who sumitted the attempt.
->quizid = // The quiz id of the quiz the attempt belongs to.
->cmid = // The course_module id of the quiz the attempt belongs to.
->courseid = // The course id of the course the quiz belongs to.
*/
6 changes: 3 additions & 3 deletions mod/quiz/lang/en/quiz.php
Expand Up @@ -256,7 +256,7 @@
in course \'{$a->coursename}\'
at {$a->submissiontime}.
This email confirms that we have safely received your answers.
This message confirms that we have safely received your answers.
You can access this quiz at {$a->quizurl}.';
$string['emailconfirmsmall'] = 'Thank you for submitting your answers to \'{$a->quizname}\'';
Expand Down Expand Up @@ -550,8 +550,8 @@
$string['quizcloses'] = 'Quiz closes';
$string['quizcloseson'] = 'This quiz will close at {$a}';
$string['quiz:deleteattempts'] = 'Delete quiz attempts';
$string['quiz:emailconfirmsubmission'] = 'Get email confirmation when submitting';
$string['quiz:emailnotifysubmission'] = 'Get email notification of submissions';
$string['quiz:emailconfirmsubmission'] = 'Get a confirmation message when submitting';
$string['quiz:emailnotifysubmission'] = 'Get a notification message when an attempt is submitted';
$string['quiz:grade'] = 'Grade quizzes manually';
$string['quiz:ignoretimelimits'] = 'Ignores time limit on quizzes';
$string['quizisclosed'] = 'This quiz is closed';
Expand Down

0 comments on commit cbb8b55

Please sign in to comment.