Index: votingapi.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/votingapi/Attic/votingapi.admin.inc,v
retrieving revision 1.1.2.2
diff -u -p -r1.1.2.2 votingapi.admin.inc
--- votingapi.admin.inc 1 Oct 2008 15:47:46 -0000 1.1.2.2
+++ votingapi.admin.inc 18 May 2009 04:19:29 -0000
@@ -19,8 +19,11 @@ function votingapi_settings_form($form_s
'#default_value' => variable_get('votingapi_anonymous_window', 86400),
'#options' => $period
);
-
- $form['votingapi_calculation_schedule'] = array(
+ $form['results'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Voting Results Settings'),
+ );
+ $form['results']['votingapi_calculation_schedule'] = array(
'#type' => 'radios',
'#title' => t('Vote tallying'),
'#description' => t('On high-traffic sites, administrators can use this setting to postpone the calculation of vote results.'),
@@ -31,11 +34,131 @@ function votingapi_settings_form($form_s
'manual' => t('Do not tally results automatically: I am using a module that manages its own vote results.')
),
);
+ $form['results']['votingapi_recalculate_all'] = array(
+ '#type' => 'submit',
+ '#value' => t('Rebuild all Voting Results'),
+ '#submit' => array('votingapi_rebuild_submit'),
+ );
+
+ /**
+ * hmm
+ * '#attributes' => array('class' => 'description'),
+ * didn't work
+ */
+ $form['results']['help'] = array(
+ '#prefix' => '
',
+ '#type' => 'markup',
+ '#value' => t('This action rebuilds all voting results, and may be a lengthy process.'),
+ '#suffix' => '
',
+ );
return system_settings_form($form);
}
/**
+ * Form submit function for the Rebuild all Voting Results button
+ */
+function votingapi_rebuild_submit($form, &$form_state) {
+ $form_state['redirect'] = 'admin/settings/votingapi/rebuild';
+}
+
+/**
+ * generate a confirmation form for rebuilding all the votes
+ */
+function votingapi_rebuild_form($form_state = NULL) {
+ return confirm_form(array(),
+ t('Are you sure you want to rebuild all the voting results on site content?'),
+ 'admin/settings/votingapi',
+ t('This action rebuilds all voting results, and may be a lengthy process.'),
+ t('Recalculate Results'),
+ t('Cancel')
+ );
+}
+
+/**
+ * form submit function that recalculates voting results for all content
+ * Sometimes the votingapi_vote table gets out of sync with the votingapi_cache
+ * table and then the cache table needs to be rebuild. This can be a lengthy
+ * process and it uses the batch api to rebuild those results
+ *
+ * http://api.drupal.org/api/group/batch/6
+ */
+function votingapi_rebuild_form_submit($form, &$form_state) {
+ $types_result = db_query("SELECT DISTINCT(content_type) FROM {votingapi_vote}");
+
+ // set up the operations for each content_type
+ while ($type_object = db_fetch_object($types_result)) {
+ $operations[] = array('_votingapi_results_rebuild', array($type_object->content_type));
+ }
+ $batch = array(
+ 'title' => t('Recalculating'),
+ 'operations' => $operations,
+ 'init_message' => 'Preparing to recalculate',
+ 'finished' => '_votingapi_results_rebuild_finished',
+ 'file' => drupal_get_path('module', 'votingapi') . '/votingapi.admin.inc',
+ );
+ // set the batch and let FAPI call batch_process
+ batch_set($batch);
+
+ // set a sensable redirect
+ $form_state['redirect'] = 'admin/settings/votingapi';
+}
+
+
+/**
+ * Batch API callback
+ */
+function _votingapi_results_rebuild($type, &$context) {
+ if (empty($context['sandbox']['max'])) {
+ // set up the stuff!
+ $context['sandbox']['progress'] = 0;
+ $context['sandbox']['current_content_id'] = 0;
+
+ // since $context will be populated across invokations per content type
+ // we only set count to 0 when it hasn't been set yet
+ if (!isset($context['results']['count'])) {
+ $context['results']['count'] = 0;
+ }
+ // get the max for this content type
+ $context['sandbox']['max'] = db_result(db_query("SELECT MAX(content_id) FROM {votingapi_vote} WHERE content_type = '%s'", $type));
+ }
+
+ $limit = 100;
+
+ // grab the content_id's
+ $result = db_query_range("SELECT DISTINCT(content_id) FROM {votingapi_vote} WHERE content_type = '%s' AND content_id > %d ORDER BY content_id ASC", $type, $context['sandbox']['current_content_id'], 0, $limit);
+ while ($row = db_fetch_object($result)) {
+ $context['sandbox']['progress']++;
+ $context['sandbox']['current_content_id'] = $row->content_id;
+ // force recalc
+ votingapi_recalculate_results($type, $row->content_id, TRUE);
+ $context['results']['count']++;
+ }
+
+ // check for if not finished
+ if ($context['sandbox']['current_content_id'] != $context['sandbox']['max']) {
+ $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
+ }
+ else {
+ // if we are finished with this run, delete the context[sandbox]
+ unset($context['sandbox']);
+ }
+}
+
+/**
+ * Finish batch callback
+ */
+function _votingapi_results_rebuild_finished($success, $results, $operations) {
+ if ($success) {
+ $message = format_plural($results['count'], 'One piece of content recalculated.', '@count pieces of content recalculated');
+ }
+ else {
+ $message = t('Finished with an error.');
+ }
+ drupal_set_message($message);
+}
+
+/**
* Developer tool to generate dummy votes.
*/
function votingapi_generate_votes_form() {
Index: votingapi.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/votingapi/votingapi.module,v
retrieving revision 1.46.2.15
diff -u -p -r1.46.2.15 votingapi.module
--- votingapi.module 9 Oct 2008 21:12:35 -0000 1.46.2.15
+++ votingapi.module 18 May 2009 04:19:29 -0000
@@ -26,6 +26,16 @@ function votingapi_menu() {
'file' => 'votingapi.admin.inc',
'type' => MENU_NORMAL_ITEM
);
+
+ $items['admin/settings/votingapi/rebuild'] = array(
+ 'title' => 'Voting API',
+ 'description' => 'Rebuild all the results for across all content types',
+ 'page callback' => 'drupal_get_form',
+ 'page arguments' => array('votingapi_rebuild_form'),
+ 'access callback' => 'user_access',
+ 'access arguments' => array('administer voting api'),
+ 'file' => 'votingapi.admin.inc',
+ );
if (module_exists('devel_generate')) {
$items['admin/generate/votingapi'] = array(