webform_anonymous.module in Webform Anonymous 6
Same filename and directory in other branches
This module enables a webform setting to anonymize the webform's results from everyone.
@author Daniel Imhoff
File
webform_anonymous.moduleView source
<?php
/**
* @file
* This module enables a webform setting to anonymize the webform's results from
* everyone.
*
* @author Daniel Imhoff
*/
/**
* Implements hook_perm().
*/
function webform_anonymous_perm() {
return array(
'anonymize webform results',
);
}
/**
* Implements hook_form_FORM_ID_alter() for webform_configure_form().
*/
function webform_anonymous_form_webform_configure_form_alter(&$form, &$form_state) {
if (user_access('anonymize webform results')) {
$anonymous_setting = _webform_anonymous_webform_anonymous_information($form['#node']->nid);
if (!empty($anonymous_setting)) {
$locker = user_load($anonymous_setting['locked_uid']);
}
// Add a custom submit handler.
$form['#submit'][] = 'webform_anonymous_form_webform_configure_form_submit';
// Add a Yes or No question to make the results of this webform anonymous
// or not.
$form['submission']['anonymous'] = array(
'#type' => 'radios',
'#title' => t('Anonymize results'),
'#description' => t('Make the results of this webform anonymous to everyone.'),
'#options' => array(
1 => t('Yes'),
0 => t('No'),
),
'#default_value' => !empty($anonymous_setting) && $anonymous_setting['anonymous'] ? 1 : 0,
'#disabled' => !empty($anonymous_setting) && $anonymous_setting['locked'] ? 1 : 0,
);
if (empty($anonymous_setting) || !$anonymous_setting['locked']) {
$description = t('Warning: Once locked, the results of this webform will be irreversibly anonymous.');
}
else {
$description = t('The results of this webform have been locked in an anonymous state by !user.', array(
'!user' => l($locker->name, 'user/' . $locker->uid),
));
}
// A checkbox in case the webform creator wants to lock the results in an
// anonymous state.
$form['submission']['anonymous_locked'] = array(
'#type' => 'checkbox',
'#title' => t('Lock anonymity'),
'#description' => $description,
'#default_value' => !empty($anonymous_setting) && $anonymous_setting['locked'] ? 1 : 0,
'#disabled' => !empty($anonymous_setting) && $anonymous_setting['locked'] ? 1 : 0,
);
}
}
/**
* Additional submit handler for saving webform anonymous settings.
*/
function webform_anonymous_form_webform_configure_form_submit(&$form, &$form_state) {
// Fetch the anonymous setting of this webform.
$anonymous_setting = _webform_anonymous_webform_anonymous_information($form['#node']->nid);
// No need to do all this work if it wasn't changed.
if (empty($anonymous_setting) || $form_state['values']['anonymous'] !== $anonymous_setting['anonymous'] || (string) $form_state['values']['anonymous_locked'] !== $anonymous_setting['locked']) {
global $user;
if (!$form_state['values']['anonymous']) {
$form_state['values']['anonymous_locked'] = 0;
}
$record = array(
'nid' => $form['#node']->nid,
'anonymous' => $form_state['values']['anonymous'],
'locked' => $form_state['values']['anonymous_locked'],
'locked_uid' => !empty($anonymous_setting) && $form_state['values']['anonymous_locked'] !== $anonymous_setting['locked'] ? $user->uid : 0,
);
// Insert or update the anonymous status of this webform.
drupal_write_record('webform_anonymous', $record, empty($anonymous_setting) ? array() : 'nid');
}
}
/**
* Implements hook_webform_submission_load().
*/
function webform_anonymous_webform_submission_load(&$submissions) {
$submission = current($submissions);
$anonymous_setting = _webform_anonymous_webform_anonymous_information($submission->nid);
if (!empty($anonymous_setting) && $anonymous_setting['anonymous']) {
foreach ($submissions as $sid => $submission) {
// If called for, anonymize uid and ip of submission before display.
$submission->uid = 0;
$submission->name = 'Anonymous';
$submission->remote_addr = '0.0.0.0';
}
}
}
/**
* Fetch the anonymous settings and information of a webform by the node ID.
*
* @param int $nid
* The node ID that the webform belongs.
*
* @return array
* A row of information represented as an associative array.
*/
function _webform_anonymous_webform_anonymous_information($nid) {
return db_fetch_array(db_query('SELECT * FROM {webform_anonymous} w WHERE w.nid = %d', $nid));
}
Functions
Name![]() |
Description |
---|---|
webform_anonymous_form_webform_configure_form_alter | Implements hook_form_FORM_ID_alter() for webform_configure_form(). |
webform_anonymous_form_webform_configure_form_submit | Additional submit handler for saving webform anonymous settings. |
webform_anonymous_perm | Implements hook_perm(). |
webform_anonymous_webform_submission_load | Implements hook_webform_submission_load(). |
_webform_anonymous_webform_anonymous_information | Fetch the anonymous settings and information of a webform by the node ID. |