Projet

Général

Profil

Paste
Télécharger (2,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / votingapi / votingapi.devel.inc @ f419e8de

1
<?php
2

    
3
/**
4
 * @file
5
 * Generate fake votingapi votes for development testing.
6
 */
7

    
8
/**
9
 * Utility function to generate votes.
10
 */
11
function votingapi_generate_votes($entity_type = 'node', $vote_type = 'percent', $options = array()) {
12
  module_load_include('inc', 'devel_generate');
13
  $options += array(
14
    'age' => 36000,
15
    'types' => array(),
16
    'kill' => FALSE,
17
  );
18

    
19
  if (!empty($options['kill_votes'])) {
20
    db_truncate('votingapi_vote');
21
    db_truncate('votingapi_cache');
22
  }
23

    
24
  if (($schema = drupal_get_schema($entity_type)) && ($entity_id_column = array_shift($schema['primary key']))) {
25
    // oh look we found a schema yay
26
  }
27
  else {
28
    $entity_type = 'node';
29
    $entity_id_column = 'nid';
30
  }
31
  $uids = devel_get_users();
32

    
33
  $query = db_select($entity_type, 'e')->fields('e', array($entity_id_column));
34
  if ($entity_type == 'node' && !empty($options['types'])) {
35
    $query->condition('e.type', $options['types'], 'IN');
36
  }
37
  $results = $query->execute()->fetchAll(PDO::FETCH_ASSOC);
38

    
39
  foreach ($results as $entity) {
40
    _votingapi_cast_votes($entity_type, $entity[$entity_id_column], $options['age'], $uids, $vote_type);
41
  }
42
}
43

    
44
/**
45
 * Utility function to generate votes on a node by a set of users.
46
 */
47
function _votingapi_cast_votes($etype, $eid, $timestamp = 0, $uids = array(), $style = 'percent') {
48
  $votes = array();
49
  static $tags;
50
  if (!isset($tags)) {
51
    $tags = explode(' ', devel_create_greeking(30));
52
  }
53
  foreach ($uids as $uid) {
54
    switch ($style) {
55
      case 'percent':
56
        $votes[] = array(
57
          'uid' => $uid,
58
          'entity_type' => $etype,
59
          'entity_id' => $eid,
60
          'value_type' => 'percent',
61
          'timestamp' => REQUEST_TIME - mt_rand(0, $timestamp),
62
          'value' => mt_rand(1, 5) * 20,
63
          'tag' => $tags[mt_rand(0, 20)],
64
        );
65
        break;
66
      case 'points':
67
        if (rand(0, 3)) {
68
          $votes[] = array(
69
            'uid' => $uid,
70
            'entity_type' => $etype,
71
            'entity_id' => $eid,
72
            'value_type' => 'points',
73
            'timestamp' => REQUEST_TIME - mt_rand(0, $timestamp),
74
            'value' => rand(0, 1) ? 1 : -1,
75
          );
76
        }
77
        break;
78
    }
79
  }
80
  votingapi_set_votes($votes, array());
81
}