Newer
Older
Alastair Aitchison
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
// $Id$
/**
* @file
* Allows users to be notified when their question is answered.
*
* TODO
* Tests!
* Check submit handler to delete record from notify table when question is deleted (or mass-deleted) from queue
*/
/**
* Implements hook_trigger_info().
*
* List the triggers defined by this module when actions
* can be fired.
*/
function question_notify_trigger_info() {
return array(
'question' => array(
'question_submission' => array(
'label' => t('After a question is submitted to the queue'),
),
'question_response' => array(
'label' => t('After a reply is made to a question'),
),
),
);
}
/**
* Implementation of hook_action_info().
*
* Define actions that can be triggered on certain events.
*/
function question_action_info() {
return array(
'question_email_questioner_action' => array(
'label' => t('Send email to questioner'),
'type' => 'question',
'configurable' => TRUE,
'triggers' => array('question_submission', 'question_response'),
),
);
}
/**
* Configuration form for the "Send email to questioner" action
*/
function question_email_questioner_action_form($context) {
$form['email'] = array(
'#title' => t('Email body'),
'#type' => 'textfield',
'#description' => t('Enter the body message of the email here.'),
'#default_value' => 'A question you asked has been answered',
);
return $form;
}
function question_email_questioner_action_submit($form, $form_state) {
return array('message' => $form_state['values']['email']);
}
function question_email_questioner_action($object, $context) {
// Check that the email is valid
if(valid_email_address($object->mail)) {
// Try to retrieve account information for this email address
$account = user_load(array('mail' => $email));
// If the account is not anonymous
if ($account) {
$language = user_preferred_language($account);
}
else {
$language = language_default();
}
// Build the array of parameters to complete the email template
$params['question'] = $node->title;
$params['nid'] = $node->nid;
// Attempt to send the e-mail to the asker.
$mail_sent = drupal_mail('question', 'question_submission', $object->mail, $email, $language, $params);
// Check whether successful
if ($mail_sent) {
watchdog('Question', 'Asker notification email sent to @to for question @quest',
array('@to' => $object->mail, '@quest' => check_plain($node->title)), WATCHDOG_NOTICE);
}
else {
watchdog('Question', 'Asker notification email to @to failed for the "@quest" question.',
array('@to' => $email, '@quest' => check_plain($node->title)), WATCHDOG_ERROR);
}
}
}
/**
* Implementation of hook_mail().
*
* This function defines the templates for al emails sent from question module.
*/
function question_mail($key, &$message, $params) {
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
$language = $message['language'];
$variables = user_mail_tokens($params['account'], $language);
switch ($key) {
case 'question_submission':
$subject = t('You have been assigned a question on !site', $variables, $language->language);
$body = t('Dear !username,', array('!username' => 'user'), $language->language) . '\n<br/><br/>';
$body .= t('The following question has been posted on the Teenage Health Freak site.', $language->language) . '\n<br/><br/>';
$body .= $params['question'];
break;
case 'question_response':
$subject = t('You have been assigned a question on !site', $variables, $language->language);
$body = t('Dear !username,', array('!username' => 'user'), $language->language) . '\n<br/><br/>';
$body .= t('The following question has been posted on the Teenage Health Freak site.', $language->language) . '\n<br/><br/>';
$body .= $params['question'];
break;
}
$message['body'] = $body;
$message['subject'] = $subject;
}
/************************************************
* *
* USER QUESTION SUBMISSION *
* *
************************************************/
/**
* Implements hook_form_FORM_ID_alter().
*
* Add new form fields to the question submission form
* to record user's notification email address.
*/
function question_notify_form_question_ask_form_alter(&$form, &$form_state) {
// Registered users sign up for notifications with a checkbox
if (user_is_logged_in()) {
$form['notify'] = array(
'#type' => 'checkbox',
'#title' => t('Notify by e-mail'),
'#default_value' => FALSE,
'#weight' => 0,
'#description' => t('Check this box to receive an email when your question is answered.'),
);
}
// Anonymous users must provide a notification email address
else {
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('Your email address'),
'#size' => 40,
'#maxlength' => 60,
'#description' => t('If you would like to be notified when your question is answered, please enter your email address'),
);
}
// Add validation and submit handlers for the new fields
$form['#validate'][] = 'question_notify_question_ask_form_validate';
$form['#submit'][] = 'question_notify_question_ask_form_submit';
}
/**
* Implements _form_validate().
*/
function question_notify_question_ask_form_validate(&$form, &$form_state) {
// Check validity of notification email address
if (!empty($form_state['values']['email'])) {
if (!valid_email_address($form_state['values']['email'])) {
form_set_error('email', t('Please enter a valid email address'));
}
}
}
/**
* Implements _form_submit().
*/
function question_notify_question_ask_form_submit($form, &$form_state) {
// If an authenticated user asked to be notified, get their user email address
if (isset($form_state['values']['notify']) && $form_state['values']['notify']) {
global $user;
$mail = $user->mail;
}
// If an anonymous user asked to be notified, get the (validated) email address
elseif (!empty($form_state['values']['email'])) {
$mail = $form_state['values']['email'];
}
$row = array();
if (isset($mail)) {
// Make an array containing all the values to insert
$row = array(
'qid' => $form_state['storage']['qid'],
'mail' => $mail,
);
// Insert the row
db_insert('question_notify')->fields($row)->execute();
}
// Trigger all actions associated with a question submission
$aids = trigger_get_assigned_actions('question_submission');
$context = array(
'group' => 'question',
'hook' => 'question_submission'
);
actions_do(array_keys($aids), (object) $row, $context);
}
/************************************************
* *
* QUESTION QUEUE MANAGEMENT *
* *
************************************************ /
/**
* Implements hook_form_FORM_ID_alter().
*
* Alter the question queue form.
*/
function question_notify_form_question_queue_admin_alter(&$form, &$form_state) {
// Add an additional submit handler
$form['#submit'][] = 'question_notify_question_queue_multiple_delete_confirm_submit';
}
/**
* Delete row from question_fields table when corresponding
* question is deleted from the queue.
*/
function question_notify_question_queue_multiple_delete_confirm_submit($form, &$form_state) {
if ($form['operation']['#value'] = 'delete' && $form_state['values']['confirm']) {
foreach ($form_state['values']['questions'] as $qid => $value) {
$num_deleted = db_delete('question_notify')
->condition('qid', $qid)
->execute();
}
}
}
function question_notify_form_question_delete_confirm_alter(&$form, &$form_state) {
// Add an additional submit handler
$form['#submit'][] = 'question_notify_question_queue_delete_confirm_submit';
}
function question_notify_question_queue_delete_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
$qid = $form_state['values']['qid'];
$num_deleted = db_delete('question_notify')
->condition('qid', $qid)
->execute();
}
}
/************************************************
* *
* RESPOND TO QUESTIONS *
* *
************************************************/
/**
* Implements hook_form_FORM_ID_alter().
*/
function question_notify_form_question_node_form_alter(&$form, &$form_state) {
// Add additional submit handler
$form['#submit'][] = 'question_notify_question_node_form_submit';
}
/**
* Implements _form_submit().
*/
function question_notify_question_node_form_submit(&$elements, &$form_state, $form_id = NULL) {
// If this node came from the queue
if (isset($elements['qid'])) {
// If the user asked to be notifed, get the right email address
$mail = db_query("SELECT mail FROM {question_notify} WHERE qid = :qid", array(':qid' => $elements['qid']['#value']))->fetchField();
// Then delete the notification record from the queue
$num_deleted = db_delete('question_notify')
->condition('qid', $elements['qid']['#value'])
->execute();
}
$options = new stdClass();
$options->mail = $mail;
// Trigger all actions associated with a question response
$aids = trigger_get_assigned_actions('question_response');
$context = array(
'group' => 'question',
'hook' => 'question_response'
);
actions_do(array_keys($aids), (object) $options, $context);
}