webform_handler_field_submission_count.inc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Views handler to display the number of submissions in a webform.
  4. *
  5. * Field handler to present the submission count of a node to the user.
  6. */
  7. class webform_handler_field_submission_count extends views_handler_field {
  8. /**
  9. * {@inheritdoc}
  10. */
  11. public function construct() {
  12. parent::construct();
  13. $this->count_type = $this->definition['count_type'];
  14. if ($this->count_type == 'node') {
  15. $this->additional_fields['nid'] = 'nid';
  16. $this->additional_fields['type'] = 'type';
  17. }
  18. elseif ($this->count_type == 'users') {
  19. $this->additional_fields['uid'] = 'uid';
  20. }
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function option_definition() {
  26. $options = parent::option_definition();
  27. $options['label'] = array('default' => '# of Submissions', 'translatable' => TRUE);
  28. return $options;
  29. }
  30. /**
  31. *
  32. */
  33. public function query() {
  34. $this->ensure_my_table();
  35. $this->add_additional_fields();
  36. }
  37. /**
  38. *
  39. */
  40. public function render($values) {
  41. global $user;
  42. $output = NULL;
  43. if ($this->count_type == 'node' && variable_get('webform_node_' . $values->{$this->aliases['type']}, FALSE)) {
  44. module_load_include('inc', 'webform', 'includes/webform.submissions');
  45. $node = node_load($values->{$this->aliases['nid']});
  46. if (webform_results_access($node, $user)) {
  47. $count = webform_get_submission_count($node->nid);
  48. $output = l($count, "node/$node->nid/webform-results");
  49. }
  50. elseif (webform_submission_access($node, NULL, 'list', $user)) {
  51. $count = webform_get_submission_count($node->nid, $user->uid);
  52. $output = l($count, "node/$node->nid/submissions");
  53. }
  54. else {
  55. $output = webform_get_submission_count($node->nid);
  56. }
  57. }
  58. elseif ($this->count_type == 'users') {
  59. $output = db_select('webform_submissions')
  60. ->condition('uid', $values->{$this->aliases['uid']})
  61. ->countQuery()->execute()->fetchField();
  62. }
  63. return $output;
  64. }
  65. }