/[drupal]/contributions/modules/votingapi/votingapi.module
ViewVC logotype

Diff of /contributions/modules/votingapi/votingapi.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.46.2.23.2.2, Mon Jul 20 16:16:35 2009 UTC revision 1.46.2.23.2.3, Mon Jul 20 22:24:45 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: votingapi.module,v 1.46.2.23.2.1 2009/07/01 07:26:12 eaton Exp $  // $Id: votingapi.module,v 1.46.2.23.2.2 2009/07/20 16:16:35 eaton Exp $
3    
4  /**  /**
5   * @file   * @file
# Line 265  function votingapi_delete_votes($votes = Line 265  function votingapi_delete_votes($votes =
265      foreach ($votes as $vote) {      foreach ($votes as $vote) {
266        $vids[] = $vote['vote_id'];        $vids[] = $vote['vote_id'];
267      }      }
268      db_query("DELETE FROM {votingapi_vote} WHERE vote_id IN (" . db_placeholders($vids) . ")", $vids);      db_delete('votingapi_vote')->condition('vote_id', $vids, 'IN')->execute();
269    }    }
270  }  }
271    
# Line 282  function votingapi_delete_results($vote_ Line 282  function votingapi_delete_results($vote_
282      foreach ($vote_results as $vote) {      foreach ($vote_results as $vote) {
283        $vids[] = $vote['vote_cache_id'];        $vids[] = $vote['vote_cache_id'];
284      }      }
285      db_query("DELETE FROM {votingapi_cache} WHERE vote_cache_id IN (" . db_placeholders($vids) . ")", $vids);      db_delete('votingapi_cache')->condition('vote_cache_id', $vids, 'IN')->execute();
286    }    }
287  }  }
288    
# Line 311  function votingapi_select_votes($criteri Line 311  function votingapi_select_votes($criteri
311    if (!empty($criteria['vote_source']) && $anon_window > 0) {    if (!empty($criteria['vote_source']) && $anon_window > 0) {
312      $criteria['timestamp'] = REQUEST_TIME - $anon_window;      $criteria['timestamp'] = REQUEST_TIME - $anon_window;
313    }    }
314    $votes = array();    $query = db_select('votingapi_vote')->fields('votingapi_vote');
315    $result = _votingapi_select('vote', $criteria, $limit);    foreach ($criteria as $key => $value) {
316    while ($vote = db_fetch_array($result)) {      $query->condition($key, $value, is_array($value) ? 'IN' : '=');
     $votes[] = $vote;  
317    }    }
318    return $votes;    if (!empty($limit)) {
319        $query->range(0, $limit);
320      }
321      return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
322  }  }
323    
324  /**  /**
# Line 339  function votingapi_select_votes($criteri Line 341  function votingapi_select_votes($criteri
341   *   An array of vote results matching the criteria.   *   An array of vote results matching the criteria.
342   */   */
343  function votingapi_select_results($criteria = array(), $limit = 0) {  function votingapi_select_results($criteria = array(), $limit = 0) {
344    $cached = array();    $query = db_select('votingapi_cache')->fields('votingapi_cache');
345    $result = _votingapi_select('cache', $criteria, $limit);    foreach ($criteria as $key => $value) {
346    while ($cache = db_fetch_array($result)) {      $query->condition($key, $value, is_array($value) ? 'IN' : '=');
347      $cached[] = $cache;    }
348      if (!empty($limit)) {
349        $query->range(0, $limit);
350    }    }
351    return $cached;    return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
352  }  }
353    
354  /**  /**
# Line 371  function votingapi_recalculate_results($ Line 375  function votingapi_recalculate_results($
375    // bail out. The cron run will pick up the results.    // bail out. The cron run will pick up the results.
376    
377    if (variable_get('votingapi_calculation_schedule', 'immediate') != 'cron' || $force_calculation == TRUE) {    if (variable_get('votingapi_calculation_schedule', 'immediate') != 'cron' || $force_calculation == TRUE) {
378      $criteria = array('content_type' => $content_type, 'content_id' => $content_id);      $query = db_delete('votingapi_cache')
379      _votingapi_delete('cache', $criteria);        ->condition('content_type', $content_type)
380          ->condition('content_id', $content_id)
381          ->execute();
382    
383      // Bulk query to pull the majority of the results we care about.      // Bulk query to pull the majority of the results we care about.
384      $cache = _votingapi_get_standard_results($content_type, $content_id);      $cache = _votingapi_get_standard_results($content_type, $content_id);
385    
# Line 479  function _votingapi_get_standard_results Line 485  function _votingapi_get_standard_results
485    $sql  = "SELECT v.value_type, v.tag, ";    $sql  = "SELECT v.value_type, v.tag, ";
486    $sql .= "COUNT(v.value) as value_count, SUM(v.value) as value_sum  ";    $sql .= "COUNT(v.value) as value_count, SUM(v.value) as value_sum  ";
487    $sql .= "FROM {votingapi_vote} v ";    $sql .= "FROM {votingapi_vote} v ";
488    $sql .= "WHERE v.content_type = '%s' AND v.content_id = %d AND v.value_type IN ('points', 'percent') ";    $sql .= "WHERE v.content_type = :type AND v.content_id = :id AND v.value_type IN ('points', 'percent') ";
489    $sql .= "GROUP BY v.value_type, v.tag";    $sql .= "GROUP BY v.value_type, v.tag";
490    $results = db_query($sql, $content_type, $content_id);    $results = db_query($sql, array(':type' => $content_type, ':id' => $content_id));
491    
492    while ($result = db_fetch_array($results)) {    foreach ($results as $result) {
493      $cache[$result['tag']][$result['value_type']]['count'] = $result['value_count'];      $cache[$result->tag][$result->value_type]['count'] = $result->value_count;
494      $cache[$result['tag']][$result['value_type']]['average'] = $result['value_sum'] / $result['value_count'];      $cache[$result->tag][$result->value_type]['average'] = $result->value_sum / $result->value_count;
495      if ($result['value_type'] == 'points') {      if ($result->value_type == 'points') {
496        $cache[$result['tag']][$result['value_type']]['sum'] = $result['value_sum'];        $cache[$result->tag][$result->value_type]['sum'] = $result->value_sum;
497      }      }
498    }    }
499    
500    $sql  = "SELECT v.tag, v.value, v.value_type, COUNT(1) AS score ";    $sql  = "SELECT v.tag, v.value, v.value_type, COUNT(1) AS score ";
501    $sql .= "FROM {votingapi_vote} v ";    $sql .= "FROM {votingapi_vote} v ";
502    $sql .= "WHERE v.content_type = '%s' AND v.content_id = %d AND v.value_type = 'option' ";    $sql .= "WHERE v.content_type = :type AND v.content_id = :id AND v.value_type = 'option' ";
503    $sql .= "GROUP BY v.value, v.tag, v.value_type";    $sql .= "GROUP BY v.value, v.tag, v.value_type";
504    $results = db_query($sql, $content_type, $content_id);    $results = db_query($sql, array(':type' => $content_type, ':id' => $content_id));
505    while ($result = db_fetch_array($results)) {  
506      $cache[$result['tag']][$result['value_type']]['option-' . $result['value']] = $result['score'];    foreach ($results as $result) {
507        $cache[$result->tag][$result->value_type]['option-' . $result->value] = $result->score;
508    }    }
509    
510    return $cache;    return $cache;
# Line 544  function _votingapi_prep_vote(&$vote) { Line 551  function _votingapi_prep_vote(&$vote) {
551      );      );
552    }    }
553  }  }
   
   
 /**  
  * Internal helper function constructs SELECT queries. Don't use unless you're me.  
  */  
 function _votingapi_select($table = 'vote', $criteria = array(), $limit = 0) {  
   $query = "SELECT * FROM {votingapi_" . $table . "} v WHERE 1 = 1";  
   $details = _votingapi_query('vote', $criteria);  
   
   $query .= $details['query'];  
   return $limit ? db_query_range($query, $details['args'], 0, $limit) : db_query($query, $details['args']);  
 }  
   
 /**  
  * Internal helper function constructs DELETE queries. Don't use unless you're me.  
  */  
 function _votingapi_delete($table = 'vote', $criteria = array(), $limit = 0) {  
   $query = "DELETE FROM {votingapi_" . $table . "} WHERE 1 = 1";  
   $details = _votingapi_query('vote', $criteria, '');  
   
   $query .= $details['query'];  
   db_query($query, $details['args']);  
 }  
   
 /**  
  * Internal helper function constructs WHERE clauses. Don't use unless you're me.  
  */  
 function _votingapi_query($table = 'vote', $criteria = array(), $alias = 'v.') {  
   $criteria += array(  
     'vote_id' => NULL,  
     'vote_cache_id' => NULL,  
     'content_id' => NULL,  
     'content_type' => NULL,  
     'value_type' => NULL,  
     'value' => NULL,  
     'tag' => NULL,  
     'uid' => NULL,  
     'timestamp' => NULL,  
     'vote_source' => NULL,  
     'function' => NULL,  
   );  
   
   $query = '';  
   $args = array();  
   if (!empty($criteria['vote_id'])) {  
     _votingapi_query_builder($alias . 'vote_id', $criteria['vote_id'], $query, $args);  
   }  
   elseif (!empty($criteria['vote_cache_id'])) {  
     _votingapi_query_builder($alias . 'vote_cache_id', $criteria['vote_cache_id'], $query, $args);  
   }  
   else {  
     _votingapi_query_builder($alias . 'content_type', $criteria['content_type'], $query, $args, TRUE);  
     _votingapi_query_builder($alias . 'content_id', $criteria['content_id'], $query, $args);  
     _votingapi_query_builder($alias . 'value_type', $criteria['value_type'], $query, $args, TRUE);  
     _votingapi_query_builder($alias . 'tag', $criteria['tag'], $query, $args, TRUE);  
     _votingapi_query_builder($alias . 'function', $criteria['function'], $query, $args, TRUE);  
     _votingapi_query_builder($alias . 'uid', $criteria['uid'], $query, $args);  
     _votingapi_query_builder($alias . 'vote_source', $criteria['vote_source'], $query, $args, TRUE);  
     _votingapi_query_builder($alias . 'timestamp', $criteria['timestamp'], $query, $args);  
   }  
   return array('query' => $query, 'args' => $args);  
 }  
   
 /**  
  * Internal helper function constructs individual elements of WHERE clauses.  
  * Don't use unless you're me.  
  */  
 function _votingapi_query_builder($name, $value, &$query, &$args, $col_is_string = FALSE) {  
   if (!isset($value)) {  
     // Do nothing  
   }  
   elseif ($name === 'timestamp') {  
     $query .= " AND timestamp >= %d";  
     $args[] = $value;  
   }  
   elseif ($name === 'v.timestamp') {  
     $query .= " AND v.timestamp >= %d";  
     $args[] = $value;  
   }  
   else {  
     if (is_array($value)) {  
       if ($col_is_string) {  
         $query .= " AND $name IN (" . db_placeholders($value, 'varchar') . ")";  
         $args = array_merge($args, $value);  
       }  
       else {  
         $query .= " AND $name IN (" . db_placeholders($value, 'int') . ")";  
         $args = array_merge($args, $value);  
       }  
     }  
     else {  
       if ($col_is_string) {  
         $query .= " AND $name = '%s'";  
         $args[] = $value;  
       }  
       else {  
         $query .= " AND $name = %d";  
         $args[] = $value;  
       }  
     }  
   }  
 }  
   

Legend:
Removed from v.1.46.2.23.2.2  
changed lines
  Added in v.1.46.2.23.2.3

  ViewVC Help
Powered by ViewVC 1.1.2