tmgmt_handler_field_tmgmt_progress.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Field handler which shows the progressbar.
  4. *
  5. * @ingroup views_field_handlers
  6. */
  7. class tmgmt_handler_field_tmgmt_progress extends views_handler_field_entity {
  8. /**
  9. * Prefetch statistics for all jobs.
  10. */
  11. function pre_render(&$values) {
  12. parent::pre_render($values);
  13. // In case of jobs, pre-fetch the statistics in a single query and add them
  14. // to the static cache.
  15. if ($this->entity_type == 'tmgmt_job') {
  16. $tjids = array();
  17. foreach ($values as $value) {
  18. // Do not load statistics for aborted jobs.
  19. if ($value->tmgmt_job_state == TMGMT_JOB_STATE_ABORTED) {
  20. continue;
  21. }
  22. $tjids[] = $value->tjid;
  23. }
  24. tmgmt_job_statistics_load($tjids);
  25. }
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. function render($values) {
  31. /** @var TMGMTJobItem|TMGMTJob $object */
  32. $object = $this->get_value($values);
  33. // If job has been aborted the status info is not applicable.
  34. if ($object->isAborted()) {
  35. return t('N/A');
  36. }
  37. $counts = array(
  38. '@accepted' => $object->getCountAccepted(),
  39. '@reviewed' => $object->getCountReviewed(),
  40. '@translated' => $object->getCountTranslated(),
  41. '@pending' => $object->getCountPending(),
  42. );
  43. $id = $object->internalIdentifier();
  44. if (module_exists('google_chart_tools')) {
  45. draw_chart($this->build_progressbar_settings($id, $counts));
  46. return '<div id="progress' . $id . '"></div>';
  47. }
  48. $title = t('Accepted: @accepted, reviewed: @reviewed, translated: @translated, pending: @pending.', $counts);
  49. return sprintf('<span title="%s">%s</span>', $title, implode('/', $counts));
  50. }
  51. /**
  52. * Creates a settings array for the google chart tools.
  53. *
  54. * The settings are preset with values to display a progress bar for either
  55. * a job or job item.
  56. *
  57. * @param $id
  58. * The id of the chart.
  59. * @param $counts
  60. * Array with the counts for accepted, translated and pending.
  61. * @param $prefix
  62. * Prefix to id.
  63. * @return
  64. * Settings array.
  65. */
  66. function build_progressbar_settings($id, $counts, $prefix = 'progress') {
  67. $settings['chart'][$prefix . $id] = array(
  68. 'header' => array(t('Accepted'), t('Reviewed'), t('Translated'), t('Pending')),
  69. 'rows' => array(
  70. array($counts['@accepted'], $counts['@reviewed'], $counts['@translated'], $counts['@pending']),
  71. ),
  72. 'columns' => array(''),
  73. 'chartType' => 'PieChart',
  74. 'containerId' => $prefix . $id,
  75. 'options' => array(
  76. 'backgroundColor' => 'transparent',
  77. 'colors' => array('#00b600', '#60ff60', '#ffff00', '#6060ff'),
  78. 'forceIFrame' => FALSE,
  79. 'chartArea' => array(
  80. 'left' => 0,
  81. 'top' => 0,
  82. 'width' => '50%',
  83. 'height' => '100%',
  84. ),
  85. 'fontSize' => 9,
  86. 'title' => t('Progress'),
  87. 'titlePosition' => 'none',
  88. 'width' => 60,
  89. 'height' => 50,
  90. 'isStacked' => TRUE,
  91. 'legend' => array('position' => 'none'),
  92. 'pieSliceText' => 'none',
  93. )
  94. );
  95. return $settings;
  96. }
  97. }