votingapi.devel.inc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * @file
  4. * Generate fake votingapi votes for development testing.
  5. */
  6. /**
  7. * Utility function to generate votes.
  8. */
  9. function votingapi_generate_votes($entity_type = 'node', $vote_type = 'percent', $options = array()) {
  10. module_load_include('inc', 'devel_generate');
  11. $options += array(
  12. 'age' => 36000,
  13. 'types' => array(),
  14. 'kill' => FALSE,
  15. );
  16. if (!empty($options['kill_votes'])) {
  17. db_truncate('votingapi_vote');
  18. db_truncate('votingapi_cache');
  19. }
  20. if (($schema = drupal_get_schema($entity_type)) && ($entity_id_column = array_shift($schema['primary key']))) {
  21. // oh look we found a schema yay
  22. }
  23. else {
  24. $entity_type = 'node';
  25. $entity_id_column = 'nid';
  26. }
  27. $uids = devel_get_users();
  28. $query = db_select($entity_type, 'e')->fields('e', array($entity_id_column));
  29. if ($entity_type == 'node' && !empty($options['types'])) {
  30. $query->condition('e.type', $options['types'], 'IN');
  31. }
  32. $results = $query->execute()->fetchAll(PDO::FETCH_ASSOC);
  33. foreach ($results as $entity) {
  34. _votingapi_cast_votes($entity_type, $entity[$entity_id_column], $options['age'], $uids, $vote_type);
  35. }
  36. }
  37. /**
  38. * Utility function to generate votes on a node by a set of users.
  39. */
  40. function _votingapi_cast_votes($etype, $eid, $timestamp = 0, $uids = array(), $style = 'percent') {
  41. $votes = array();
  42. static $tags;
  43. if (!isset($tags)) {
  44. $tags = explode(' ', devel_create_greeking(30));
  45. }
  46. foreach ($uids as $uid) {
  47. switch ($style) {
  48. case 'percent':
  49. $votes[] = array(
  50. 'uid' => $uid,
  51. 'entity_type' => $etype,
  52. 'entity_id' => $eid,
  53. 'value_type' => 'percent',
  54. 'timestamp' => REQUEST_TIME - mt_rand(0, $timestamp),
  55. 'value' => mt_rand(1, 5) * 20,
  56. 'tag' => $tags[mt_rand(0, 20)],
  57. );
  58. break;
  59. case 'points':
  60. if (rand(0, 3)) {
  61. $votes[] = array(
  62. 'uid' => $uid,
  63. 'entity_type' => $etype,
  64. 'entity_id' => $eid,
  65. 'value_type' => 'points',
  66. 'timestamp' => REQUEST_TIME - mt_rand(0, $timestamp),
  67. 'value' => rand(0, 1) ? 1 : -1,
  68. );
  69. }
  70. break;
  71. }
  72. }
  73. votingapi_set_votes($votes, array());
  74. }