Commit 87017982 authored by djdevin's avatar djdevin Committed by djdevin
Browse files

Issue #2353599 by djdevin: Convert emails to rules

parent a6fc17d3
......@@ -867,7 +867,8 @@ function quiz_question_answering_form_finalize(&$form, &$form_state) {
$form_state['redirect'] = "node/{$quiz->nid}/quiz-results/{$quiz_result->result_id}/view";
}
quiz_end_actions($quiz, $score, $_SESSION['quiz'][$quiz->nid]);
// Call hook_quiz_finished().
module_invoke_all('quiz_finished', $quiz, $score, $_SESSION['quiz'][$quiz->nid]);
// Remove all information about this quiz from the session.
// @todo but for anon, we might have to keep some so they could access
......
......@@ -178,60 +178,6 @@ function quiz_admin_settings($form, &$form_state) {
'#collapsed' => TRUE,
);
$form['quiz_email_settings']['taker'] = array(
'#type' => 'fieldset',
'#title' => t('Email for Quiz takers'),
'#collapsible' => FALSE,
);
$form['quiz_email_settings']['taker']['quiz_email_results'] = array(
'#type' => 'checkbox',
'#title' => t('Email results to quiz takers'),
'#default_value' => variable_get('quiz_email_results', 0),
'#description' => t('Check this to send users their results at the end of a quiz.')
);
$form['quiz_email_settings']['taker']['quiz_email_results_subject_taker'] = array(
'#type' => 'textfield',
'#title' => t('Configure email subject'),
'#description' => t('This format will be used when sending quiz results at the end of a quiz.'),
'#default_value' => variable_get('quiz_email_results_subject_taker', quiz_email_results_format('subject', 'taker')),
);
$form['quiz_email_settings']['taker']['quiz_email_results_body_taker'] = array(
'#type' => 'textarea',
'#title' => t('Configure email format'),
'#description' => t('This format will be used when sending quiz results at the end of a quiz. !title(quiz title), !sitename, !taker(quiz takers username), !date(time when quiz was finished), !minutes(How many minutes the quiz taker spent taking the quiz), !desc(description of the quiz), !correct(points attained), !total(max score for the quiz), !percentage(percentage score), !url(url to the result page) and !author are placeholders.'),
'#default_value' => variable_get('quiz_email_results_body_taker', quiz_email_results_format('body', 'taker')),
);
$form['quiz_email_settings']['author'] = array(
'#type' => 'fieldset',
'#title' => t('Email for Quiz authors'),
'#collapsible' => FALSE,
);
$form['quiz_email_settings']['author']['quiz_results_to_quiz_author'] = array(
'#type' => 'checkbox',
'#title' => t('Email all results to quiz author'),
'#default_value' => variable_get('quiz_results_to_quiz_author', 0),
'#description' => t('Check this to send quiz results for all users to the quiz author.'),
);
$form['quiz_email_settings']['author']['quiz_email_results_subject'] = array(
'#type' => 'textfield',
'#title' => t('Configure email subject'),
'#description' => t('This format will be used when sending quiz results at the end of a quiz. Authors and quiz takers gets the same format.'),
'#default_value' => variable_get('quiz_email_results_subject', quiz_email_results_format('subject', 'author')),
);
$form['quiz_email_settings']['author']['quiz_email_results_body'] = array(
'#type' => 'textarea',
'#title' => t('Configure email format'),
'#description' => t('This format will be used when sending @quiz results at the end of a quiz. !title(quiz title), !sitename, !taker(quiz takers username), !date(time when quiz was finished), !minutes(How many minutes the quiz taker spent taking the quiz), !desc(description of the quiz), !correct(points attained), !total(max score for the quiz), !percentage(percentage score), !url(url to the result page) and !author are placeholders.'),
'#default_value' => variable_get('quiz_email_results_body', quiz_email_results_format('body', 'author')),
);
$form['#validate'][] = 'quiz_settings_form_validate';
$form['#submit'][] = 'quiz_settings_form_submit';
......
......@@ -2090,120 +2090,6 @@ function quiz_end_scoring($result_id) {
return $score;
}
/**
* Actions to take at the end of a quiz
*
* @param $quiz
* The quiz node
* @param $result_id
* Result id
* @param $score
* Score as a number
*/
function quiz_end_actions($quiz, $score, $session_data) {
// Call hook_quiz_finished().
module_invoke_all('quiz_finished', $quiz, $score, $session_data);
}
/**
* Implements hook_quiz_finished().
*
* Performs actions like sending quiz results over email at the end of quiz.
*/
function quiz_quiz_finished($quiz, $score, $session_data) {
// @TODO convert to entity/rules
$result_id = $session_data['result_id'];
// Load data about the quiz taker
$taker = db_query('SELECT u.uid, u.mail FROM {users} u JOIN {quiz_node_results} qnr ON u.uid = qnr.uid WHERE result_id = :result_id', array(':result_id' => $result_id))->fetch();
if (variable_get('quiz_results_to_quiz_author', 0)) {
$author_mail = db_query('SELECT mail FROM {users} WHERE uid = :uid', array(':uid' => $quiz->uid))->fetchField();
drupal_mail('quiz', 'notice', $author_mail, NULL, array($quiz, $score, $result_id, 'author'));
}
if (variable_get('quiz_email_results', 0) && variable_get('quiz_use_passfail', 1) && $taker->uid != 0 && $score['is_evaluated']) {
drupal_mail('quiz', 'notice', $taker->mail, NULL, array($quiz, $score, $result_id, 'taker'));
drupal_set_message(t('Your results have been sent to your email address.'));
}
}
/**
* Implements hook_quiz_scored().
*
* Performs actions like sending quiz results over email at the end of quiz.
*/
function quiz_quiz_scored($quiz, $score, $result_id) {
// @TODO Rules
$taker = db_query('SELECT u.uid, u.mail FROM {users} u JOIN {quiz_node_results} qnr ON u.uid = qnr.uid WHERE result_id = :rid', array(':rid' => $result_id))->fetch();
if (variable_get('quiz_email_results', 0) && $taker->uid != 0 && $score['is_evaluated']) {
drupal_mail('quiz', 'notice', $taker->mail, NULL, array($quiz, $score, $result_id, 'taker'));
drupal_set_message(t("The results has been sent to the user's email address."));
}
}
/**
* Implements hook_mail().
*/
function quiz_mail($key, &$message, $params) {
global $user;
switch ($key) {
case 'notice':
list($quiz, $score, $result_id, $target) = $params;
$quiz_body = field_get_items('node', $quiz, 'body');
$substitutions = array(
'!title' => $quiz->title,
'!sitename' => variable_get('site_name', 'Quiz'),
'!taker' => $user->name,
'!author' => $quiz->name,
'!title' => check_plain($quiz->title),
'!date' => format_date(REQUEST_TIME),
'!desc' => $quiz_body ? $quiz_body[0]['value'] : '',
'!correct' => isset($score['numeric_score']) ? $score['numeric_score'] : 0,
'!total' => $score['possible_score'],
'!percentage' => $score['percentage_score'],
'!url' => url("user/{$user->uid}/quiz-results/{$result_id}/view", array('absolute' => TRUE)),
'!minutes' => db_query("SELECT CEIL((time_end - time_start)/60) FROM {quiz_node_results} WHERE result_id = :result_id AND time_end", array(':result_id' => $result_id))->fetchField()
);
$type = $target != 'author' ? '_taker' : '';
$test = variable_get('quiz_email_results_body' . $type, quiz_email_results_format('body', $target));
$message['subject'] = t(variable_get('quiz_email_results_subject' . $type, quiz_email_results_format('subject', $target)), $substitutions, array('langcode' => $user->language));
$message['body'][] = t($test, $substitutions, array('langcode' => $user->language));
}
}
/**
* This functions returns the default email subject and body format which will
* be used at the end of quiz.
*/
function quiz_email_results_format($type, $target) {
global $user;
if ($type == 'subject') {
if ($target == 'author') {
return t('!title Results Notice from !sitename');
}
if ($target == 'taker') {
return t('!title Results Notice from !sitename');
}
}
if ($type == 'body') {
if ($target == 'author') {
return t('Dear !author') . "\n\n" .
t('!taker attended the @quiz !title on !date', array('@quiz' => QUIZ_NAME)) . "\n" .
t('Test Description : !desc') . "\n" .
t('!taker got !correct out of !total points in !minutes minutes. Score given in percentage is !percentage') . "\n" .
t('You can access the result here !url') . "\n";
}
if ($target == 'taker') {
return t('Dear !taker') . "\n\n" .
t('You attended the @quiz !title on !date', array('@quiz' => QUIZ_NAME)) . "\n" .
t('Test Description : !desc') . "\n" .
t('You got !correct out of !total points in !minutes minutes. Score given in percentage is !percentage') . "\n" .
t('You can access the result here !url') . "\n";
}
}
}
/**
* Update a score for a quiz.
*
......
......@@ -15,5 +15,38 @@ function quiz_default_rules_configuration() {
$configs["quiz_feedback_$key"] = $set;
}
$configs["rules_send_results_at_the_end_of_a_quiz"] = rules_import('
{ "rules_send_results_at_the_end_of_a_quiz" : {
"LABEL" : "Send quiz results at the end of a quiz",
"PLUGIN" : "reaction rule",
"ACTIVE" : false,
"OWNER" : "rules",
"TAGS" : [ "quiz" ],
"REQUIRES" : [ "rules", "entity" ],
"ON" : { "quiz_result_update" : [] },
"IF" : [
{ "data_is" : { "data" : [ "quiz-result-unchanged:is-evaluated" ], "value" : "0" } },
{ "data_is" : { "data" : [ "quiz-result:is-evaluated" ], "value" : "1" } }
],
"DO" : [
{ "mail" : {
"to" : [ "quiz-result:uid:mail" ],
"subject" : "[site:name]: Your results for [quiz-result:nid:title]",
"message" : "[quiz-result:uid:name],\r\n\r\nYou finished the quiz [quiz-result:nid:title] on [quiz-result:time-start]\r\n\r\nYour score: [quiz-result:score]%\r\n\r\nYou can access the result here: [site:url]node\/[quiz-result:nid:nid]\/quiz-results\/[quiz-result:result-id]",
"language" : [ "" ]
}
},
{ "mail" : {
"to" : [ "quiz-result:nid:author:mail" ],
"subject" : "[site:name]: [quiz-result:uid:name]\u0027s results for [quiz-result:nid:title]",
"message" : "[quiz-result:nid:author:name],\r\n\r\n[quiz-result:uid:name] finished the quiz [quiz-result:nid:title] on [quiz-result:time-start]\r\n\r\nTheir score: [quiz-result:score]%\r\n\r\nYou can access the result here: [site:url]node\/[quiz-result:nid:nid]\/quiz\/results\/[quiz-result:result-id]\/view",
"language" : [ "" ]
}
}
]
}
}
');
return $configs;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment