| 1 |
<?php |
<?php |
| 2 |
// $Id: feedback.module,v 1.74 2009/03/07 00:36:20 sun Exp $ |
// $Id: feedback.module,v 1.75 2009/03/21 20:22:14 sun Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 9 |
/** |
/** |
| 10 |
* Implementation of hook_perm(). |
* Implementation of hook_perm(). |
| 11 |
*/ |
*/ |
| 12 |
function feedback_perm() { |
function feedback_permission() { |
| 13 |
return array('access feedback form', 'view feedback messages'); |
return array( |
| 14 |
|
'access feedback form' => array( |
| 15 |
|
'title' => t('Access feedback form'), |
| 16 |
|
'description' => t('Submit feedback messages.'), |
| 17 |
|
), |
| 18 |
|
'view feedback messages' => array( |
| 19 |
|
'title' => t('View feedback messages'), |
| 20 |
|
'description' => t('View, process, and delete submitted feedback messages.'), |
| 21 |
|
), |
| 22 |
|
); |
| 23 |
} |
} |
| 24 |
|
|
| 25 |
/** |
/** |
| 37 |
* Implementation of hook_menu(). |
* Implementation of hook_menu(). |
| 38 |
*/ |
*/ |
| 39 |
function feedback_menu() { |
function feedback_menu() { |
|
$items = array(); |
|
| 40 |
$items['admin/reports/feedback'] = array( |
$items['admin/reports/feedback'] = array( |
| 41 |
'title' => 'Feedback messages', |
'title' => 'Feedback messages', |
| 42 |
'description' => 'View feedback messages.', |
'description' => 'View feedback messages.', |
| 52 |
*/ |
*/ |
| 53 |
function feedback_init() { |
function feedback_init() { |
| 54 |
if (user_access('access feedback form')) { |
if (user_access('access feedback form')) { |
| 55 |
drupal_add_css(drupal_get_path('module', 'feedback') . '/feedback.css'); |
$path = drupal_get_path('module', 'feedback'); |
| 56 |
drupal_add_js(drupal_get_path('module', 'feedback') . '/feedback.js'); |
drupal_add_css($path . '/feedback.css'); |
| 57 |
|
drupal_add_js($path . '/feedback.js'); |
| 58 |
} |
} |
| 59 |
} |
} |
| 60 |
|
|
| 61 |
/** |
/** |
| 62 |
* Implementation of hook_block(). |
* Implementation of hook_block_list(). |
| 63 |
*/ |
*/ |
| 64 |
function feedback_block($op = 'list', $delta = 0, $edit = array()) { |
function feedback_block_list() { |
| 65 |
if ($op == 'list') { |
$blocks['form'] = array( |
| 66 |
$blocks['form'] = array('info' => t('Feedback form')); |
'info' => t('Feedback form'), |
| 67 |
return $blocks; |
); |
| 68 |
} |
return $blocks; |
| 69 |
else if ($op == 'view') { |
} |
| 70 |
$block = array(); |
|
| 71 |
switch($delta) { |
/** |
| 72 |
case 'form': |
* Implementation of hook_block_view(). |
| 73 |
if (!user_access('access feedback form') || $_GET['q'] == 'admin/reports/feedback') { |
*/ |
| 74 |
break; |
function feedback_block_view($delta = '') { |
| 75 |
} |
$block = array(); |
| 76 |
$block['subject'] = '<span class="feedback-link">' . t('Feedback') . '</span>'; |
switch($delta) { |
| 77 |
$block['content'] = drupal_get_form('feedback_form'); |
case 'form': |
| 78 |
|
if (!user_access('access feedback form') || $_GET['q'] == 'admin/reports/feedback') { |
| 79 |
break; |
break; |
| 80 |
} |
} |
| 81 |
return $block; |
$block['subject'] = '<span class="feedback-link">' . t('Feedback') . '</span>'; |
| 82 |
|
$block['content'] = drupal_get_form('feedback_form'); |
| 83 |
|
break; |
| 84 |
} |
} |
| 85 |
|
return $block; |
| 86 |
} |
} |
| 87 |
|
|
| 88 |
/** |
/** |
| 89 |
* Implementation of hook_footer(). |
* Implementation of hook_page_build(). |
| 90 |
*/ |
*/ |
| 91 |
function feedback_footer($main = 0) { |
function feedback_page_build(&$page) { |
| 92 |
if (user_access('access feedback form') && $_GET['q'] != 'admin/reports/feedback') { |
if (user_access('access feedback form') && $_GET['q'] != 'admin/reports/feedback') { |
| 93 |
$block = (object)module_invoke('feedback', 'block', 'view', 'form'); |
$block = (object) feedback_block_view('form'); |
| 94 |
$block->module = 'feedback'; |
$block->module = 'feedback'; |
| 95 |
$block->delta = 'form'; |
$block->delta = 'form'; |
| 96 |
$block->region = 'footer'; |
$block->region = 'footer'; |
| 97 |
return theme('block', $block); |
$build = $block->content; |
| 98 |
|
unset($block->content); |
| 99 |
|
$build += array( |
| 100 |
|
'#block' => $block, |
| 101 |
|
'#weight' => 0, |
| 102 |
|
'#sorted' => TRUE, |
| 103 |
|
); |
| 104 |
|
$build['#theme_wrappers'][] ='block'; |
| 105 |
|
|
| 106 |
|
$page['page_bottom']['feedback'] = $build; |
| 107 |
} |
} |
| 108 |
} |
} |
| 109 |
|
|
| 110 |
/** |
/** |
| 111 |
* Form builder function for a user feedback form. |
* Form builder function for a user feedback form. |
| 112 |
*/ |
*/ |
| 113 |
function feedback_form() { |
function feedback_form($form, &$form_state) { |
| 114 |
$form = array(); |
$form['#attributes']['class'] = array('feedback-form'); |
|
$form['#attributes']['class'] = 'feedback-form'; |
|
| 115 |
|
|
| 116 |
// Store the path on which this form is displayed. |
// Store the path on which this form is displayed. |
| 117 |
$form['location'] = array('#type' => 'value', '#value' => $_GET['q']); |
$form['location'] = array('#type' => 'value', '#value' => $_GET['q']); |
| 120 |
|
|
| 121 |
$form['help'] = array( |
$form['help'] = array( |
| 122 |
'#prefix' => '<div class="feedback-help">', |
'#prefix' => '<div class="feedback-help">', |
| 123 |
'#value' => t('If you experience a bug or would like to see an addition on the current page, feel free to leave us a message.'), |
'#markup' => t('If you experience a bug or would like to see an addition on the current page, feel free to leave us a message.'), |
| 124 |
'#suffix' => '</div>', |
'#suffix' => '</div>', |
| 125 |
); |
); |
| 126 |
if (user_access('view feedback messages')) { |
if (user_access('view feedback messages')) { |
| 127 |
if (arg(0) != 'node') { |
if (arg(0) != 'node') { |
| 128 |
$feedbacks = feedback_load(array('status' => 0, 'location_masked' => feedback_mask_path($_GET['q']))); |
$feedbacks = feedback_load(array('f.status' => 0, 'f.location_masked' => feedback_mask_path($_GET['q']))); |
| 129 |
} |
} |
| 130 |
else { |
else { |
| 131 |
$feedbacks = feedback_load(array('status' => 0, 'location' => $_GET['q'])); |
$feedbacks = feedback_load(array('f.status' => 0, 'f.location' => $_GET['q'])); |
| 132 |
} |
} |
| 133 |
if ($feedbacks) { |
if ($feedbacks) { |
| 134 |
$rows = ''; |
$rows = ''; |
| 138 |
} |
} |
| 139 |
$form['messages'] = array( |
$form['messages'] = array( |
| 140 |
'#prefix' => '<div class="feedback-messages">', |
'#prefix' => '<div class="feedback-messages">', |
| 141 |
'#value' => $rows, |
'#markup' => $rows, |
| 142 |
'#suffix' => '</div>', |
'#suffix' => '</div>', |
| 143 |
); |
); |
| 144 |
} |
} |
| 145 |
} |
} |
| 146 |
$form['message'] = array( |
$form['message'] = array( |
| 147 |
'#type' => 'textarea', |
'#type' => 'textarea', |
| 148 |
'#attributes' => array('class' => 'feedback-message'), |
'#attributes' => array('class' => array('feedback-message')), |
| 149 |
'#title' => t('Message'), |
'#title' => t('Message'), |
| 150 |
'#required' => TRUE, |
'#required' => TRUE, |
| 151 |
'#wysiwyg' => FALSE, |
'#wysiwyg' => FALSE, |
| 165 |
feedback_add_entry($form_state['values']['message'], $form_state['values']['location']); |
feedback_add_entry($form_state['values']['message'], $form_state['values']['location']); |
| 166 |
$message = t('Thanks for your feedback!'); |
$message = t('Thanks for your feedback!'); |
| 167 |
if ($form_state['values']['ajax']) { |
if ($form_state['values']['ajax']) { |
| 168 |
echo drupal_to_js(array('message' => $message)); |
drupal_json_output(array('message' => $message)); |
| 169 |
exit; |
exit; |
| 170 |
} |
} |
| 171 |
else { |
else { |
| 197 |
/** |
/** |
| 198 |
* Load feedback entries from the database. |
* Load feedback entries from the database. |
| 199 |
* |
* |
| 200 |
* @param $array |
* @param $conditions |
| 201 |
* A keyed array of optional where clause conditions. |
* A keyed array of optional query conditions. |
| 202 |
*/ |
*/ |
| 203 |
function feedback_load($array) { |
function feedback_load($conditions) { |
| 204 |
$where = $args = array(); |
$query = db_select('feedback', 'f')->fields('f'); |
| 205 |
if (!empty($array)) { |
$query->join('users', 'u', 'f.uid = u.uid'); |
| 206 |
foreach ($array as $column => $value) { |
$query->fields('u', array('name')); |
| 207 |
$where[] = 'f.'. $column .' = '. (is_numeric($value) ? '%d' : "'%s'"); |
|
| 208 |
$args[] = $value; |
if (!empty($conditions)) { |
| 209 |
} |
foreach ($conditions as $key => $value) { |
| 210 |
} |
$query->condition($key, $value); |
| 211 |
$sql = "SELECT f.*, u.name FROM {feedback} f INNER JOIN {users} u ON f.uid = u.uid"; |
} |
|
if (!empty($where)) { |
|
|
$sql .= ' WHERE '. implode(' AND ', $where); |
|
|
} |
|
|
$result = db_query($sql, $args); |
|
|
$entries = array(); |
|
|
while ($entry = db_fetch_object($result)) { |
|
|
$entries[$entry->fid] = $entry; |
|
| 212 |
} |
} |
| 213 |
|
$entries = $query->execute()->fetchAllAssoc('fid'); |
| 214 |
return $entries; |
return $entries; |
| 215 |
} |
} |
| 216 |
|
|
| 240 |
function feedback_add_entry($message, $location) { |
function feedback_add_entry($message, $location) { |
| 241 |
global $user; |
global $user; |
| 242 |
|
|
| 243 |
db_query("INSERT INTO {feedback} (uid, message, location, location_masked, url, timestamp, useragent) VALUES (%d, '%s', '%s', '%s', '%s', %d, '%s')", $user->uid, trim($message), $location, feedback_mask_path($location), url($location, array('absolute' => TRUE)), time(), $_SERVER['HTTP_USER_AGENT']); |
return db_insert('feedback') |
| 244 |
|
->fields(array( |
| 245 |
|
'uid' => $user->uid, |
| 246 |
|
'message' => trim($message), |
| 247 |
|
'location' => $location, |
| 248 |
|
'location_masked' => feedback_mask_path($location), |
| 249 |
|
'url' => url($location, array('absolute' => TRUE)), |
| 250 |
|
'timestamp' => REQUEST_TIME, |
| 251 |
|
'useragent' => $_SERVER['HTTP_USER_AGENT'], |
| 252 |
|
)) |
| 253 |
|
->execute(); |
| 254 |
} |
} |
| 255 |
|
|
| 256 |
/** |
/** |
| 257 |
* Implementation of hook_user(). |
* Implementation of hook_user_cancel(). |
| 258 |
*/ |
*/ |
| 259 |
function feedback_user($op, &$edit, &$account) { |
function feedback_user_cancel($edit, $account, $method) { |
| 260 |
if ($op == 'delete') { |
switch ($method) { |
| 261 |
db_query('UPDATE {feedback} SET uid = 0 WHERE uid = %d', $account->uid); |
case 'user_cancel_reassign': |
| 262 |
|
db_update('feedback') |
| 263 |
|
->fields(array('uid' => 0)) |
| 264 |
|
->condition('uid', $account->uid) |
| 265 |
|
->execute(); |
| 266 |
|
break; |
| 267 |
|
|
| 268 |
|
case 'user_cancel_delete': |
| 269 |
|
db_delete('feedback') |
| 270 |
|
->condition('uid', $account->uid) |
| 271 |
|
->execute(); |
| 272 |
|
break; |
| 273 |
} |
} |
| 274 |
} |
} |
| 275 |
|
|
| 276 |
/** |
/** |
| 277 |
* Build a (sortable) form containing a checkbox for each feedback entry. |
* Build a (sortable) form containing a checkbox for each feedback entry. |
| 278 |
*/ |
*/ |
| 279 |
function feedback_admin_view_form() { |
function feedback_admin_view_form($form, &$form_state) { |
|
$form = array(); |
|
| 280 |
$status_headings = array( |
$status_headings = array( |
| 281 |
0 => t('Open feedback messages'), |
0 => t('Open feedback messages'), |
| 282 |
1 => t('Processed feedback messages'), |
1 => t('Processed feedback messages'), |
| 297 |
} |
} |
| 298 |
|
|
| 299 |
$form['feedback-messages'] = array('#tree' => TRUE); |
$form['feedback-messages'] = array('#tree' => TRUE); |
| 300 |
|
$query = db_select('feedback', 'f')->extend('PagerDefault')->extend('TableSort'); |
| 301 |
|
$query->join('users', 'u', 'f.uid = u.uid'); |
| 302 |
|
$query->fields('f') |
| 303 |
|
->fields('u', array('name')) |
| 304 |
|
->limit(50); |
| 305 |
foreach (array(0, 1) as $status) { |
foreach (array(0, 1) as $status) { |
| 306 |
$sql = "SELECT f.*, u.name FROM {feedback} f INNER JOIN {users} u ON f.uid = u.uid WHERE f.status = %d"; |
$status_query = clone $query; |
| 307 |
$count_query = "SELECT COUNT(fid) FROM {feedback} WHERE status = %d"; |
$result = $status_query->element($status) |
| 308 |
$tablesort = tablesort_sql($form['#feedback_header']); |
->condition('f.status', $status) |
| 309 |
$result = pager_query($sql . $tablesort, 50, $status, $count_query, $status); |
->execute()->fetchAllAssoc('fid'); |
| 310 |
|
|
| 311 |
$form['feedback-messages'][$status] = array( |
$form['feedback-messages'][$status] = array( |
| 312 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 313 |
'#title' => $status_headings[$status], |
'#title' => $status_headings[$status], |
| 314 |
'#collapsible' => TRUE, |
'#collapsible' => TRUE, |
| 315 |
'#collapsed' => $status, |
'#collapsed' => $status, |
| 316 |
'#attributes' => array('class' => 'feedback-messages'), |
'#attributes' => array('class' => array('feedback-messages')), |
| 317 |
); |
); |
| 318 |
while ($entry = db_fetch_object($result)) { |
foreach ($result as $fid => $entry) { |
| 319 |
$form['feedback-messages'][$status][$entry->fid] = array( |
$form['feedback-messages'][$status][$fid] = array( |
| 320 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 321 |
'#return_value' => 1, |
'#return_value' => 1, |
| 322 |
'#default_value' => FALSE, |
'#default_value' => FALSE, |
| 323 |
); |
); |
| 324 |
$form['feedback-messages'][$status][$entry->fid]['location'] = array('#value' => l(truncate_utf8($entry->location, 32, FALSE, TRUE), $entry->url)); |
$form['feedback-messages'][$status][$fid]['location'] = array('#markup' => l(truncate_utf8($entry->location, 32, FALSE, TRUE), $entry->url)); |
| 325 |
$form['feedback-messages'][$status][$entry->fid]['date'] = array('#value' => format_date($entry->timestamp, 'small')); |
$form['feedback-messages'][$status][$fid]['date'] = array('#markup' => format_date($entry->timestamp, 'small')); |
| 326 |
$form['feedback-messages'][$status][$entry->fid]['user'] = array('#value' => theme('username', $entry)); |
$form['feedback-messages'][$status][$fid]['user'] = array('#markup' => theme('username', $entry)); |
| 327 |
$form['feedback-messages'][$status][$entry->fid]['message'] = array('#value' => feedback_format_message($entry)); |
$form['feedback-messages'][$status][$fid]['message'] = array('#markup' => feedback_format_message($entry)); |
| 328 |
} |
} |
| 329 |
} |
} |
| 330 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Submit')); |
$form['submit'] = array('#type' => 'submit', '#value' => t('Submit')); |
| 334 |
/** |
/** |
| 335 |
* Output a sortable table containing all feedback entries. |
* Output a sortable table containing all feedback entries. |
| 336 |
*/ |
*/ |
| 337 |
function theme_feedback_admin_view_form(&$form) { |
function theme_feedback_admin_view_form($form) { |
| 338 |
$output = ''; |
$output = ''; |
| 339 |
foreach (element_children($form['feedback-messages']) as $status) { |
foreach (element_children($form['feedback-messages']) as $status) { |
| 340 |
$item = &$form['feedback-messages'][$status]; |
$item = &$form['feedback-messages'][$status]; |
| 361 |
} |
} |
| 362 |
// Inject the table. |
// Inject the table. |
| 363 |
$item['messages'] = array( |
$item['messages'] = array( |
| 364 |
'#value' => theme('table', $form['#feedback_header'], $rows) . theme('pager', array(), 50, $status), |
'#markup' => theme('table', $form['#feedback_header'], $rows) . theme('pager', array(), $status), |
| 365 |
'#weight' => -1, |
'#weight' => -1, |
| 366 |
); |
); |
| 367 |
// Render the fieldset. |
// Render the fieldset. |
| 368 |
$output .= drupal_render($item); |
$output .= drupal_render($item); |
| 369 |
} |
} |
| 370 |
// Render internal FAPI and potential extra form elements. |
// Render internal FAPI and potential extra form elements. |
| 371 |
$output .= drupal_render($form); |
$output .= drupal_render_children($form); |
| 372 |
return $output; |
return $output; |
| 373 |
} |
} |
| 374 |
|
|
| 387 |
} |
} |
| 388 |
// Update status of entries in database. |
// Update status of entries in database. |
| 389 |
foreach ($update as $fid => $value) { |
foreach ($update as $fid => $value) { |
| 390 |
db_query("UPDATE {feedback} SET status = %d WHERE fid = %d", $value, $fid); |
db_update('feedback') |
| 391 |
|
->fields(array( |
| 392 |
|
'status' => $value, |
| 393 |
|
)) |
| 394 |
|
->condition('fid', $fid) |
| 395 |
|
->execute(); |
| 396 |
} |
} |
| 397 |
} |
} |
| 398 |
|
|