diff -urNp lovehate/lovehate.info /usr/local/code/drupal5/modules/lovehate/lovehate.info
--- lovehate/lovehate.info	1969-12-31 16:00:00.000000000 -0800
+++ /usr/local/code/drupal5/modules/lovehate/lovehate.info	2006-12-01 19:51:25.000000000 -0800
@@ -0,0 +1,2 @@
+; $Id$
+name = Lovehatedescription = "Lets users vote on vocabulary terms to announce their loves and hates."dependencies = votingapipackage = Voting
\ No newline at end of file
diff -urNp lovehate/lovehate.module /usr/local/code/drupal5/modules/lovehate/lovehate.module
--- lovehate/lovehate.module	2006-07-20 09:25:34.000000000 -0700
+++ /usr/local/code/drupal5/modules/lovehate/lovehate.module	2006-12-01 19:49:05.000000000 -0800
@@ -1,577 +1 @@
-<?php
-/* $Id: lovehate.module,v 1.24 2006/07/20 16:25:34 eaton Exp $ */
-
-/**
- * @file
- * Lets users vote on vocabulary terms to announce their loves and hates.
- */
- 
-// ************
-// System hooks
-// ************
-
-function lovehate_help($section) {
-  switch ($section) {
-    case 'admin/modules#description':
-      return t('Lets users vote on vocabulary terms to announce their loves and hates.');
-      break;
-  } 
-}
-
-/**
- * hook_perm impelementation.
- */
-function lovehate_perm() {
-  return array('view loves and hates', 'choose loves and hates');
-}
-
-/**
- * hook_menu impelementation.
- */
-function lovehate_menu($may_cache) {
-  global $user;
-  $items = array();
-
-  if ($may_cache) {
-    $items[] = array(
-      'path' => 'lovehate',
-      'title' => t('loves and hates'),
-      'callback' => 'lovehate_page',
-      'access' => user_access('view loves and hates'),
-      'type' => MENU_SUGGESTED_ITEM,
-    );
-    $items[] = array(
-      'path' => 'lovehate/vote',
-      'title' => t('lovehate vote'),
-      'callback' => 'lovehate_vote',
-      'access' => user_access('choose loves and hates'),
-      'type' => MENU_CALLBACK,
-    );
-    $items[] = array(
-      'path' => 'lovehate/ajaxvote',
-      'title' => t('lovehate ajax vote'),
-      'callback' => 'lovehate_ajax_vote',
-      'access' => user_access('choose loves and hates'),
-      'type' => MENU_CALLBACK,
-    );
-  }
-  else {
-    if (arg(0) == 'user' && is_numeric(arg(1))) {
-      $items[] = array(
-        'path' => 'user/'. arg(1) .'/lovehate',
-        'title' => t('loves and hates'),
-        'callback' => 'lovehate_user_page',
-        'access' => user_access('view loves and hates'),
-        'type' => MENU_IS_LOCAL_TASK
-      );
-    }
-  }
-
-  return $items;
-}
-
-// Intercept term deletion and remove old votes.
-function lovehate_taxonomy($op, $type, $object = NULL) {
-  if ($op == 'delete' && $type == 'term') {
-    $votes = _votingapi_get_raw_votes('term', $object['tid']);
-    votingapi_delete_votes($votes);
-    votingapi_recalculate_results('term', $object['tid']);
-  }
-}
-
-function lovehate_term_path($term) {
-  return 'lovehate/' . $term->tid;
-}
-
-/**
- * hook_settings impelementation.
- */
-function lovehate_settings() {
-  $form = array();
-
-  // preload variables for the form
-  $vid = variable_get('lovehate_vocabulary', 0);
-  $vocabularies = array();
-  $result = db_query("SELECT vid, name FROM {vocabulary} WHERE module = 'taxonomy' OR module = 'lovehate'");
-  while ($vocabulary = db_fetch_object($result)) {
-    $vocabularies[$vocabulary->vid] = $vocabulary->name;
-  }
-  
-  db_query("UPDATE {vocabulary} SET module = 'taxonomy' WHERE module = 'lovehate'");
-  if ($vid && variable_get('lovehate_vocabulary_control', 0)) {
-    db_query("UPDATE {vocabulary} SET module = 'lovehate' WHERE vid = %d AND module = 'taxonomy'", $vid);
-  }
-
-  $form['lovehate_vocabulary'] = array(
-    '#type' => 'select',
-    '#title' => t('The vocabulary containing terms to love or hate'),
-    '#default_value' => $vid,
-    '#options' => $vocabularies,
-    '#description' => t('Users will be able to vote on whether they love or hate each term from this vocabulary.'),
-  );
-
-  $form['lovehate_vocabulary_control'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Take control of the selected vocabulary'),
-    '#description' => t('When selected, links to terms in the Love/Hate vocabulary will automatically be redirected to the Love/Hate page. Vocabularies already controlled by another module (like Forum or Image Gallery) cannot be changed.'),
-    '#return_value' => 1,
-    '#default_value' => variable_get('lovehate_vocabulary_control', 0),
-  );
-
-  $form['lovehate_item_count'] = array(
-    '#type' => 'select',
-    '#title' => t('User terms count'),
-    '#description' => t('The number of items to display for a given user\'s loves and hates.'),
-    '#default_value' => variable_get('lovehate_item_count', 50),
-    '#options' => drupal_map_assoc(array(5, 10, 15, 20, 25, 50, 75, 100, 200)),
-  );
-
-  $form['lovehate_user_count'] = array(
-    '#type' => 'select',
-    '#title' => t('Term users count'),
-    '#description' => t('The number of users to display for a given term\'s lovers/haters list.'),
-    '#default_value' => variable_get('lovehate_user_count', 50),
-    '#options' => drupal_map_assoc(array(5, 10, 15, 20, 25, 50, 75, 100, 200)),
-  );
-  
-  return $form;
-}
-
-function lovehate_block($op = 'list', $delta = 0, $edit = array()) {
-  switch ($op) {
-    case 'configure': {
-      $form = array();
-  		$form['lovehate_block_title'] = array(
-  		  '#type' => 'textfield',
-  		  '#default_value' => variable_get('lovehate_block_title', t('Popular topics')),
-  		  '#size' => 30,
-  		  '#maxlength' => 120,
-  		  '#description' => t('Enter the title for the Love/Hate block'),
-  		  '#title' => 'Block title',
-  		);
-  		$form['lovehate_block_count'] = array(
-  		  '#type' => 'select',
-  		  '#default_value' => variable_get('lovehate_block_count', 10),
-  		  '#options' => array(5=>5, 10=>10, 15=>15, 20=>20),
-  		  '#description' => t('Enter the number of loves and hates to list'),
-  		  '#title' => 'Block count',
-  		);
-      return $form;
-    }
-    case 'save': {
-  		variable_set('lovehate_block_title', $edit['lovehate_block_title']);
-  		variable_set('lovehate_block_count', $edit['lovehate_block_count']);
-      break;
-    }
-    case 'list': {
-      $block[0]['info'] = "Love/Hate: ".variable_get('amazon_read_blocktitle', t('Popular topics'));
-      return $block;
-    }
-    case 'view': {
-      $terms = lovehate_get_top_terms(variable_get('lovehate_block_count', 10));
-      if (count($terms)) {
-        $block['subject'] = variable_get('amazon_read_blocktitle', t('Popular topics'));
-        $block['content'] = theme('lovehate_block_list', $terms);
-        return $block;
-      }
-      break;
-    }
-  }
-}
-
-/**
- * Hunmonk's module dependency check: see http://drupal.org/node/54463
- */
-function lovehate_form_alter($form_id, &$form) {
-  if ($form_id == 'system_modules' && !$_POST) {
-    lovehate_system_module_validate($form);
-  }
-}
-
-/**
- * Hunmonk's module dependency check: see http://drupal.org/node/54463
- */
-function lovehate_system_module_validate(&$form) {
-  $module = 'lovehate';
-  $dependencies = array('votingapi', 'taxonomy');
-  foreach ($dependencies as $dependency) {
-      if (!in_array($dependency, $form['status']['#default_value'])) {
-        $missing_dependency = TRUE;
-        $missing_dependency_list[] = $dependency;
-      }
-  }
-  if (in_array($module, $form['status']['#default_value']) && isset($missing_dependency)) {
-    db_query("UPDATE {system} SET status = 0 WHERE type = 'module' AND name = '%s'", $module);
-    $key = array_search($module, $form['status']['#default_value']);
-    unset($form['status']['#default_value'][$key]);
-    drupal_set_message(t('The module %module was deactivated--it requires the following disabled/non-existant modules to function properly: %dependencies', array('%module' => $module, '%dependencies' => implode(', ', $missing_dependency_list))), 'error');
-  }
-}
-
-
-// ************************************************
-// Voting functions -- vote casting and calculation
-// ************************************************
-
-function lovehate_votingapi_calculate(&$cache, $votes, $content_type, $content_id) {
-  // Maintain an additional 'lovers' and 'haters' count for each term
-  if ($content_type == 'term' && count($votes)) {
-    $lovers = 0;
-    $haters = 0;
-
-    foreach($votes as $vote) {
-      if ($vote->value_type == 'points') {
-        if ($vote->value == 1) {
-          $lovers += 1;
-        }
-        else {
-          $haters += 1;
-        }
-      }
-    }
-
-    $cache['vote']['points']['haters'] = $haters;
-    $cache['vote']['points']['lovers'] = $lovers;
-    unset($cache['vote']['points']['count']);
-    unset($cache['vote']['points']['average']);
-  }
-}
-
-function _lovehate_cast_vote($tid, $score) {
-  global $user;
-
-  // bail out if the user doens't have access, if the args aren't numeric
-  if (!user_access('choose loves and hates')) {
-    return;
-  }  if (!is_numeric($tid) || !is_numeric($score)) {
-    return;
-  }
-  if ($score > 1 || $score < -1) {
-    return;
-  }
-
-  if ($score == 0) {
-    votingapi_unset_vote('term', $tid);
-  }
-  else if ($score == -1 || $score == 1) {
-    // add the vote
-    $vote->value = $score;
-    $vote->value_type = 'points';
-    $vote->tag = 'vote';
-    votingapi_set_vote('term', $tid, $vote);
-  }
-  else {
-    // We only allow those three values. bail if it's anything else.
-    return;
-  }
-}
-
-function lovehate_vote($tid = NULL, $score = NULL) {
-  if ($tid === NULL) {
-    $tid = arg(2);
-  }
-  if ($score == NULL) {
-    $score = arg(3);
-  }
-
-  _lovehate_cast_vote($tid, $score);
-  drupal_goto();
-}
-
-function lovehate_ajax_vote($tid = NULL, $score = NULL) {
-  if ($tid === NULL) {
-    $tid = arg(2);
-  }
-  if ($score == NULL) {
-    $score = arg(3);
-  }
-
-  _lovehate_cast_vote($tid, $score);
-  $newscore = lovehate_get_term_score($tid);
-  print $newscore->lovers . ',' . $newscore->haters . ',' . $newscore->score; 
-  exit();
-}
-
-// ********************************************
-// Utility functions for handling lovehate data
-// ********************************************
-
-function lovehate_get_node_terms($nid, $precache = FALSE) {
-  $vid = variable_get('lovehate_vocabulary', 0);
-  if ($vid == 0) {
-    return array();
-  }
-  else {
-    $terms = taxonomy_node_get_terms_by_vocabulary($nid, $vid);
-    if ($precache) {
-      foreach ($terms as $tid => $term) {
-        $terms[$tid]->lovehate = lovehate_get_term_score($tid);
-      }
-    }
-    return $terms;
-  }
-}
-
-function lovehate_calculate_score_from_terms($terms) {
-  $score->user = 0;
-  $score->overall = 0;
-  foreach ($terms as $term) {
-    $score->user += $term->lovehate->current_user;
-    $score->overall += $term->lovehate->score;
-  }
-  return $score;
-}
-
-function lovehate_get_all_terms($uid = NULL, $precache = FALSE) {
-  $terms = array();
-  $vid = variable_get('lovehate_vocabulary', 0);
-  if ($vid != 0) {
-    $result = db_query('SELECT * FROM {term_data} WHERE vid = %d', $vid);
-    while ($term = db_fetch_object($result)) {
-      $term->lovehate = lovehate_get_term_score($term->tid, $uid);
-      $terms[$term->tid] = $term;
-    }
-    if ($precache) {
-      foreach ($terms as $tid => $term) {
-        $terms[$tid]->lovehate = lovehate_get_term_score($tid, $uid);
-      }
-    }
-  }
-  return $terms;
-}
-
-function lovehate_get_term_score($tid, $uid = NULL) {
-  $score->lovers = 0;
-  $score->haters = 0;
-  $score->score = 0;
-
-  $votes = votingapi_get_voting_results('term', $tid);
-  foreach ($votes as $vote) {
-    if ($vote->function == 'lovers') {
-      $score->lovers = $vote->value;
-    }
-    if ($vote->function == 'haters') {
-      $score->haters = $vote->value;
-    }
-    if ($vote->function == 'sum') {
-      $score->score = $vote->value;
-    }
-  }
-  
-  if ($uid != NULL) {
-    $current_user_vote = votingapi_get_user_votes('term', $tid, $uid);
-    if (isset($current_user_vote[0])) {
-      $score->current_user = $current_user_vote[0]->value;
-    }
-  }
-  return $score;
-}
-
-function lovehate_get_term_users($tid) {
-  $users->lovers = array();
-  $users->haters = array();
-  $votes = votingapi_get_content_votes('term', $tid);
-
-  foreach ($votes as $uid=>$uid_votes) {
-    foreach ($uid_votes as $vote) {
-      if ($vote->value == 1) {
-        $users->lovers[] = $uid;
-      }
-      else if ($vote->value == -1) {
-        $users->haters[] = $uid;
-      }
-    }
-  }
-  return $users;
-}
-
-function lovehate_get_user_terms($uid = NULL) {
-  if ($uid == NULL) {
-    global $user;
-    $uid = $user->uid;
-  }
-
-  $terms->loves = array();
-  $terms->hates = array();
-  $terms->other = array();
-
-  $tmp = lovehate_get_all_terms($uid, TRUE);
-
-  foreach ($tmp as $term) {
-    if ($term->lovehate->current_user == 1) {
-      $terms->loves[$term->tid] = $term;
-    }
-    else if ($term->lovehate->current_user == -1) {
-      $terms->hates[$term->tid] = $term;
-    }
-    else {
-      $terms->other[$term->tid] = $term;
-    }
-  }
-
-  return $terms;
-}
-
-function lovehate_get_top_terms($count = 10) {
-  $ordered_terms = array();
-  $terms = lovehate_get_all_terms(NULL, TRUE);
-  foreach ($terms as $term) {
-    $ordered_terms[$term->lovehate->score] = $term;
-  }
-  krsort($ordered_terms, SORT_NUMERIC);
-  if ($count > count($ordered_terms)) {
-    $ordered_terms = array_slice($ordered_terms, 0, $count);
-  }
-  return $ordered_terms;
-}
-
-
-// *************************
-// Page generation functions
-// *************************
-
-function lovehate_page($tid = 0) {
-  // we're in page-mode, not individual term mode.
-  if ($tid == 0) {
-    $terms = lovehate_get_all_terms();
-    return theme('lovehate_overview_page', $terms);
-  }
-  else {
-    $term = taxonomy_get_term($tid);
-    $term->lovehate = lovehate_get_term_score($tid);
-    $score = lovehate_get_term_score($tid);
-    $users = lovehate_get_term_users($tid);
-
-    return theme('lovehate_detail_page', $term, $score, $users->lovers, $users->haters);
-  }
-  
-  return $output;
-}
-
-function lovehate_user_page() {
-  $uid = arg(1);
-  $user_data = lovehate_get_user_terms($uid);
-  
-  $output = theme('lovehate_user_page', $uid, $user_data->loves, $user_data->hates, $user_data->other);
-
-  return $output;
-}
-
-// *****************
-// Theming functions
-// *****************
-
-function theme_lovehate_overview_page($terms = array()) {
-  $output = "";
-  foreach ($terms as $term) {
-    $output .= theme('lovehate_term', $term, TRUE, TRUE);
-  }
-  return $output;
-}
-
-function theme_lovehate_detail_page($term, $score = NULL, $lovers = array(), $haters = array()) {
-  global $user;
-
-  drupal_set_title($term->name);
-  $breadcrumbs = drupal_get_breadcrumb();
-  $breadcrumbs[] = l(t('loves and hates'), 'lovehate');
-  drupal_set_breadcrumb($breadcrumbs);
-
-  $output .= '<p>' . $term->description . '</p>';
-
-  if ($user->uid && user_access('choose loves and hates')) {
-    if ($score->current_user == 1) {
-      $output .= '<p>You LOVE ' . $term->name . '! ' . theme('lovehate_vote_widget', $term) . '</p>';
-    }
-    else if ($score->current_user == -1) {
-      $output .= '<p>You HATE ' . $term->name . '! ' . theme('lovehate_vote_widget', $term) . '</p>';
-    }
-    else {
-      $output .= '<p>You don\'t care about ' . $term->name . '. ' . theme('lovehate_vote_widget', $term) . '</p>';
-    }
-  }
-  
-  $output .= '<h4>Lovers</h4>';
-  $output .= theme('lovehate_users', $lovers);
-
-  $output .= '<h4>Haters</h4>';
-  $output .= theme('lovehate_users', $haters);
-  
-  return $output;
-}
-
-function theme_lovehate_user_page($uid = 0, $loves = array(), $hates = array(), $other = array()) {
-  $output = '<div id="loves"><h2>Love it!</h2>';
-  foreach ($loves as $term) {
-    $output .= theme('lovehate_term', $term);
-  }
-  $output .= '</div>';
-
-  $output .= '<div id="hates"><h2>Hate it!</h2>';
-  foreach ($hates as $term) {
-    $output .= theme('lovehate_term', $term);
-  }
-  $output .= '</div>';
-
-  $output .= '<div id="other"><h2>Who cares?</h2>';
-  foreach ($other as $term) {
-    $output .= theme('lovehate_term', $term);
-  }
-  $output .= '</div>';
-  return $output;
-}
-
-function theme_lovehate_term($term, $show_description = FALSE, $show_count = FALSE) {
-  $output = '<div class="lovehate_term_detail">';
-  $output .= '<h3>' . l($term->name, 'lovehate/' . $term->tid);
-  if (user_access('choose loves and hates')) {
-    $output .= theme('lovehate_vote_widget', $term);
-  }
-  $output .= '</h3>';
-  if ($show_description) {
-    $output .= '<p>' . $term->description . '</p>';
-  }
-  if ($show_count) {
-    $output .= '<p>Lovers: ' . $term->lovehate->lovers . ' | Haters: ' . $term->lovehate->haters . '</p>';
-  }
-  $output .= '</div>';
-  return $output;
-}
-
-function theme_lovehate_users($users) {
-  $limit = variable_get('lovehate_user_count', 50);
-  $count = 0;
-  $names = array();
-  foreach ($users as $uid) {
-    $name = user_load(array('uid'=>$uid));
-    $names[] = theme('username', $name);
-    $count++;
-    if ($count > $limit) {
-      break;
-    }
-  }
-  $output .= '<p>' . implode(', ', $names) . '</p>';
-  
-  return $output;
-}
-
-function theme_lovehate_vote_widget($term) {
-  $options = array();
-  if ($term->lovehate->current_user != 1) {
-    $options[] = '<span class="lovehate_love_widget">' . l('+', "lovehate/vote/$term->tid/1", array(), drupal_get_destination()) . '</span>';
-  }
-  if ($term->lovehate->current_user != 0) {
-    $options[] = '<span class="lovehate_neither_widget">' . l('x', "lovehate/vote/$term->tid/0", array(), drupal_get_destination()) . '</span>';
-  }
-  if ($term->lovehate->current_user != -1) {
-    $options[] =  '<span class="lovehate_hate_widget">' . l('-', "lovehate/vote/$term->tid/-1", array(), drupal_get_destination()) . '</span>';
-  }
-  return ' (' . implode('/', $options) . ')';
-}
-
-function theme_lovehate_block_list($terms) {
-  foreach($terms as $term) {
-    $links[] = l($term->name, "lovehate/$term->tid", array('title' => 'Total score: '.$term->lovehate->score));
-  }
-  return theme('item_list', $links);
-}
\ No newline at end of file
+<?php/* $Id: lovehate.module,v 1.24 2006/07/20 16:25:34 eaton Exp $ *//** * hook_perm impelementation. */function lovehate_perm() {  return array('view loves and hates', 'choose loves and hates');}/** * hook_menu impelementation. */function lovehate_menu($may_cache) {  global $user;  $items = array();  if ($may_cache) {    $items[] = array(      'path' => 'lovehate',      'title' => t('loves and hates'),      'callback' => 'lovehate_page',      'access' => user_access('view loves and hates'),      'type' => MENU_SUGGESTED_ITEM,    );    $items[] = array(      'path' => 'lovehate/vote',      'title' => t('lovehate vote'),      'callback' => 'lovehate_vote',      'access' => user_access('choose loves and hates'),      'type' => MENU_CALLBACK,    );    $items[] = array(      'path' => 'lovehate/ajaxvote',      'title' => t('lovehate ajax vote'),      'callback' => 'lovehate_ajax_vote',      'access' => user_access('choose loves and hates'),      'type' => MENU_CALLBACK,    );  }  else {    if (arg(0) == 'user' && is_numeric(arg(1))) {      $items[] = array(        'path' => 'user/'. arg(1) .'/lovehate',        'title' => t('loves and hates'),        'callback' => 'lovehate_user_page',        'access' => user_access('view loves and hates'),        'type' => MENU_IS_LOCAL_TASK      );    }  }  return $items;}// Intercept term deletion and remove old votes.function lovehate_taxonomy($op, $type, $object = NULL) {  if ($op == 'delete' && $type == 'term') {    $votes = _votingapi_get_raw_votes('term', $object['tid']);    votingapi_delete_votes($votes);    votingapi_recalculate_results('term', $object['tid']);  }}function lovehate_term_path($term) {  return 'lovehate/' . $term->tid;}/** * hook_settings impelementation. */function lovehate_settings() {  $form = array();  // preload variables for the form  $vid = variable_get('lovehate_vocabulary', 0);  $vocabularies = array();  $result = db_query("SELECT vid, name FROM {vocabulary} WHERE module = 'taxonomy' OR module = 'lovehate'");  while ($vocabulary = db_fetch_object($result)) {    $vocabularies[$vocabulary->vid] = $vocabulary->name;  }    db_query("UPDATE {vocabulary} SET module = 'taxonomy' WHERE module = 'lovehate'");  if ($vid && variable_get('lovehate_vocabulary_control', 0)) {    db_query("UPDATE {vocabulary} SET module = 'lovehate' WHERE vid = %d AND module = 'taxonomy'", $vid);  }  $form['lovehate_vocabulary'] = array(    '#type' => 'select',    '#title' => t('The vocabulary containing terms to love or hate'),    '#default_value' => $vid,    '#options' => $vocabularies,    '#description' => t('Users will be able to vote on whether they love or hate each term from this vocabulary.'),  );  $form['lovehate_vocabulary_control'] = array(    '#type' => 'checkbox',    '#title' => t('Take control of the selected vocabulary'),    '#description' => t('When selected, links to terms in the Love/Hate vocabulary will automatically be redirected to the Love/Hate page. Vocabularies already controlled by another module (like Forum or Image Gallery) cannot be changed.'),    '#return_value' => 1,    '#default_value' => variable_get('lovehate_vocabulary_control', 0),  );  $form['lovehate_item_count'] = array(    '#type' => 'select',    '#title' => t('User terms count'),    '#description' => t('The number of items to display for a given user\'s loves and hates.'),    '#default_value' => variable_get('lovehate_item_count', 50),    '#options' => drupal_map_assoc(array(5, 10, 15, 20, 25, 50, 75, 100, 200)),  );  $form['lovehate_user_count'] = array(    '#type' => 'select',    '#title' => t('Term users count'),    '#description' => t('The number of users to display for a given term\'s lovers/haters list.'),    '#default_value' => variable_get('lovehate_user_count', 50),    '#options' => drupal_map_assoc(array(5, 10, 15, 20, 25, 50, 75, 100, 200)),  );    return system_settings_form($form);}function lovehate_block($op = 'list', $delta = 0, $edit = array()) {  switch ($op) {    case 'configure': {      $form = array();  		$form['lovehate_block_title'] = array(  		  '#type' => 'textfield',  		  '#default_value' => variable_get('lovehate_block_title', t('Popular topics')),  		  '#size' => 30,  		  '#maxlength' => 120,  		  '#description' => t('Enter the title for the Love/Hate block'),  		  '#title' => 'Block title',  		);  		$form['lovehate_block_count'] = array(  		  '#type' => 'select',  		  '#default_value' => variable_get('lovehate_block_count', 10),  		  '#options' => array(5=>5, 10=>10, 15=>15, 20=>20),  		  '#description' => t('Enter the number of loves and hates to list'),  		  '#title' => 'Block count',  		);      return system_settings_form($form);    }    case 'save': {  		variable_set('lovehate_block_title', $edit['lovehate_block_title']);  		variable_set('lovehate_block_count', $edit['lovehate_block_count']);      break;    }    case 'list': {      $block[0]['info'] = "Love/Hate: ".variable_get('amazon_read_blocktitle', t('Popular topics'));      return $block;    }    case 'view': {      $terms = lovehate_get_top_terms(variable_get('lovehate_block_count', 10));      if (count($terms)) {        $block['subject'] = variable_get('amazon_read_blocktitle', t('Popular topics'));        $block['content'] = theme('lovehate_block_list', $terms);        return $block;      }      break;    }  }}/** * Hunmonk's module dependency check: see http://drupal.org/node/54463 */function lovehate_form_alter($form_id, &$form) {  if ($form_id == 'system_modules' && !$_POST) {    lovehate_system_module_validate($form);  }}/** * Hunmonk's module dependency check: see http://drupal.org/node/54463 */function lovehate_system_module_validate(&$form) {  $module = 'lovehate';  $dependencies = array('votingapi', 'taxonomy');  foreach ($dependencies as $dependency) {      if (!in_array($dependency, $form['status']['#default_value'])) {        $missing_dependency = TRUE;        $missing_dependency_list[] = $dependency;      }  }  if (in_array($module, $form['status']['#default_value']) && isset($missing_dependency)) {    db_query("UPDATE {system} SET status = 0 WHERE type = 'module' AND name = '%s'", $module);    $key = array_search($module, $form['status']['#default_value']);    unset($form['status']['#default_value'][$key]);    drupal_set_message(t('The module %module was deactivated--it requires the following disabled/non-existant modules to function properly: %dependencies', array('%module' => $module, '%dependencies' => implode(', ', $missing_dependency_list))), 'error');  }}// ************************************************// Voting functions -- vote casting and calculation// ************************************************function lovehate_votingapi_calculate(&$cache, $votes, $content_type, $content_id) {  // Maintain an additional 'lovers' and 'haters' count for each term  if ($content_type == 'term' && count($votes)) {    $lovers = 0;    $haters = 0;    foreach($votes as $vote) {      if ($vote->value_type == 'points') {        if ($vote->value == 1) {          $lovers += 1;        }        else {          $haters += 1;        }      }    }    $cache['vote']['points']['haters'] = $haters;    $cache['vote']['points']['lovers'] = $lovers;    unset($cache['vote']['points']['count']);    unset($cache['vote']['points']['average']);  }}function _lovehate_cast_vote($tid, $score) {  global $user;  // bail out if the user doens't have access, if the args aren't numeric  if (!user_access('choose loves and hates')) {    return;  }  if (!is_numeric($tid) || !is_numeric($score)) {    return;  }  if ($score > 1 || $score < -1) {    return;  }  if ($score == 0) {    votingapi_unset_vote('term', $tid);  }  else if ($score == -1 || $score == 1) {    // add the vote    $vote->value = $score;    $vote->value_type = 'points';    $vote->tag = 'vote';    votingapi_set_vote('term', $tid, $vote);  }  else {    // We only allow those three values. bail if it's anything else.    return;  }}function lovehate_vote($tid = NULL, $score = NULL) {  if ($tid === NULL) {    $tid = arg(2);  }  if ($score == NULL) {    $score = arg(3);  }  _lovehate_cast_vote($tid, $score);  drupal_goto();}function lovehate_ajax_vote($tid = NULL, $score = NULL) {  if ($tid === NULL) {    $tid = arg(2);  }  if ($score == NULL) {    $score = arg(3);  }  _lovehate_cast_vote($tid, $score);  $newscore = lovehate_get_term_score($tid);  print $newscore->lovers . ',' . $newscore->haters . ',' . $newscore->score;   exit();}// ********************************************// Utility functions for handling lovehate data// ********************************************function lovehate_get_node_terms($nid, $precache = FALSE) {  $vid = variable_get('lovehate_vocabulary', 0);  if ($vid == 0) {    return array();  }  else {    $terms = taxonomy_node_get_terms_by_vocabulary($nid, $vid);    if ($precache) {      foreach ($terms as $tid => $term) {        $terms[$tid]->lovehate = lovehate_get_term_score($tid);      }    }    return $terms;  }}function lovehate_calculate_score_from_terms($terms) {  $score->user = 0;  $score->overall = 0;  foreach ($terms as $term) {    $score->user += $term->lovehate->current_user;    $score->overall += $term->lovehate->score;  }  return $score;}function lovehate_get_all_terms($uid = NULL, $precache = FALSE) {  $terms = array();  $vid = variable_get('lovehate_vocabulary', 0);  if ($vid != 0) {    $result = db_query('SELECT * FROM {term_data} WHERE vid = %d', $vid);    while ($term = db_fetch_object($result)) {      $term->lovehate = lovehate_get_term_score($term->tid, $uid);      $terms[$term->tid] = $term;    }    if ($precache) {      foreach ($terms as $tid => $term) {        $terms[$tid]->lovehate = lovehate_get_term_score($tid, $uid);      }    }  }  return $terms;}function lovehate_get_term_score($tid, $uid = NULL) {  $score->lovers = 0;  $score->haters = 0;  $score->score = 0;  $votes = votingapi_get_voting_results('term', $tid);  foreach ($votes as $vote) {    if ($vote->function == 'lovers') {      $score->lovers = $vote->value;    }    if ($vote->function == 'haters') {      $score->haters = $vote->value;    }    if ($vote->function == 'sum') {      $score->score = $vote->value;    }  }    if ($uid != NULL) {    $current_user_vote = votingapi_get_user_votes('term', $tid, $uid);    if (isset($current_user_vote[0])) {      $score->current_user = $current_user_vote[0]->value;    }  }  return $score;}function lovehate_get_term_users($tid) {  $users->lovers = array();  $users->haters = array();  $votes = votingapi_get_content_votes('term', $tid);  foreach ($votes as $uid=>$uid_votes) {    foreach ($uid_votes as $vote) {      if ($vote->value == 1) {        $users->lovers[] = $uid;      }      else if ($vote->value == -1) {        $users->haters[] = $uid;      }    }  }  return $users;}function lovehate_get_user_terms($uid = NULL) {  if ($uid == NULL) {    global $user;    $uid = $user->uid;  }  $terms->loves = array();  $terms->hates = array();  $terms->other = array();  $tmp = lovehate_get_all_terms($uid, TRUE);  foreach ($tmp as $term) {    if ($term->lovehate->current_user == 1) {      $terms->loves[$term->tid] = $term;    }    else if ($term->lovehate->current_user == -1) {      $terms->hates[$term->tid] = $term;    }    else {      $terms->other[$term->tid] = $term;    }  }  return $terms;}function lovehate_get_top_terms($count = 10) {  $ordered_terms = array();  $terms = lovehate_get_all_terms(NULL, TRUE);  foreach ($terms as $term) {    $ordered_terms[$term->lovehate->score] = $term;  }  krsort($ordered_terms, SORT_NUMERIC);  if ($count > count($ordered_terms)) {    $ordered_terms = array_slice($ordered_terms, 0, $count);  }  return $ordered_terms;}// *************************// Page generation functions// *************************function lovehate_page($tid = 0) {  // we're in page-mode, not individual term mode.  if ($tid == 0) {    $terms = lovehate_get_all_terms();    return theme('lovehate_overview_page', $terms);  }  else {    $term = taxonomy_get_term($tid);    $term->lovehate = lovehate_get_term_score($tid);    $score = lovehate_get_term_score($tid);    $users = lovehate_get_term_users($tid);    return theme('lovehate_detail_page', $term, $score, $users->lovers, $users->haters);  }    return $output;}function lovehate_user_page() {  $uid = arg(1);  $user_data = lovehate_get_user_terms($uid);    $output = theme('lovehate_user_page', $uid, $user_data->loves, $user_data->hates, $user_data->other);  return $output;}// *****************// Theming functions// *****************function theme_lovehate_overview_page($terms = array()) {  $output = "";  foreach ($terms as $term) {    $output .= theme('lovehate_term', $term, TRUE, TRUE);  }  return $output;}function theme_lovehate_detail_page($term, $score = NULL, $lovers = array(), $haters = array()) {  global $user;  drupal_set_title($term->name);  $breadcrumbs = drupal_get_breadcrumb();  $breadcrumbs[] = l(t('loves and hates'), 'lovehate');  drupal_set_breadcrumb($breadcrumbs);  $output .= '<p>' . $term->description . '</p>';  if ($user->uid && user_access('choose loves and hates')) {    if ($score->current_user == 1) {      $output .= '<p>You LOVE ' . $term->name . '! ' . theme('lovehate_vote_widget', $term) . '</p>';    }    else if ($score->current_user == -1) {      $output .= '<p>You HATE ' . $term->name . '! ' . theme('lovehate_vote_widget', $term) . '</p>';    }    else {      $output .= '<p>You don\'t care about ' . $term->name . '. ' . theme('lovehate_vote_widget', $term) . '</p>';    }  }    $output .= '<h4>Lovers</h4>';  $output .= theme('lovehate_users', $lovers);  $output .= '<h4>Haters</h4>';  $output .= theme('lovehate_users', $haters);    return $output;}function theme_lovehate_user_page($uid = 0, $loves = array(), $hates = array(), $other = array()) {  $output = '<div id="loves"><h2>Love it!</h2>';  foreach ($loves as $term) {    $output .= theme('lovehate_term', $term);  }  $output .= '</div>';  $output .= '<div id="hates"><h2>Hate it!</h2>';  foreach ($hates as $term) {    $output .= theme('lovehate_term', $term);  }  $output .= '</div>';  $output .= '<div id="other"><h2>Who cares?</h2>';  foreach ($other as $term) {    $output .= theme('lovehate_term', $term);  }  $output .= '</div>';  return $output;}function theme_lovehate_term($term, $show_description = FALSE, $show_count = FALSE) {  $output = '<div class="lovehate_term_detail">';  $output .= '<h3>' . l($term->name, 'lovehate/' . $term->tid);  if (user_access('choose loves and hates')) {    $output .= theme('lovehate_vote_widget', $term);  }  $output .= '</h3>';  if ($show_description) {    $output .= '<p>' . $term->description . '</p>';  }  if ($show_count) {    $output .= '<p>Lovers: ' . $term->lovehate->lovers . ' | Haters: ' . $term->lovehate->haters . '</p>';  }  $output .= '</div>';  return $output;}function theme_lovehate_users($users) {  $limit = variable_get('lovehate_user_count', 50);  $count = 0;  $names = array();  foreach ($users as $uid) {    $name = user_load(array('uid'=>$uid));    $names[] = theme('username', $name);    $count++;    if ($count > $limit) {      break;    }  }  $output .= '<p>' . implode(', ', $names) . '</p>';    return $output;}function theme_lovehate_vote_widget($term) {  $options = array();  if ($term->lovehate->current_user != 1) {    $options[] = '<span class="lovehate_love_widget">' . l('+', "lovehate/vote/$term->tid/1", array(), drupal_get_destination()) . '</span>';  }  if ($term->lovehate->current_user != 0) {    $options[] = '<span class="lovehate_neither_widget">' . l('x', "lovehate/vote/$term->tid/0", array(), drupal_get_destination()) . '</span>';  }  if ($term->lovehate->current_user != -1) {    $options[] =  '<span class="lovehate_hate_widget">' . l('-', "lovehate/vote/$term->tid/-1", array(), drupal_get_destination()) . '</span>';  }  return ' (' . implode('/', $options) . ')';}function theme_lovehate_block_list($terms) {  foreach($terms as $term) {    $links[] = l($term->name, "lovehate/$term->tid", array('title' => 'Total score: '.$term->lovehate->score));  }  return theme('item_list', $links);}
\ No newline at end of file
