Loads an array with the word and status statistics of a job.
$tjids: An array of job ids.
An array of objects with the keys word_count, tags_count, count_pending, count_accepted, count_reviewed and count_translated.
function tmgmt_job_statistics_load(array $tjids) {
$statistics =& drupal_static(__FUNCTION__, array());
// First try to get the values from the cache.
$return = array();
$tjids_to_load = array();
foreach ($tjids as $tjid) {
if (isset($statistics[$tjid])) {
// Info exists in cache, get it from there.
$return[$tjid] = $statistics[$tjid];
}
else {
// Info doesn't exist in cache, add job to the list that needs to be
// fetched.
$tjids_to_load[] = $tjid;
}
}
// If there are remaining jobs, build a query to fetch them.
if (!empty($tjids_to_load)) {
// Build the query to fetch the statistics.
$query = \Drupal::database()
->select('tmgmt_job_item', 'tji')
->fields('tji', array(
'tjid',
));
$query
->addExpression('SUM(word_count)', 'word_count');
$query
->addExpression('SUM(tags_count)', 'tags_count');
$query
->addExpression('SUM(file_count)', 'file_count');
$query
->addExpression('SUM(count_accepted)', 'count_accepted');
$query
->addExpression('SUM(count_reviewed)', 'count_reviewed');
$query
->addExpression('SUM(count_pending)', 'count_pending');
$query
->addExpression('SUM(count_translated)', 'count_translated');
$result = $query
->groupBy('tjid')
->condition('tjid', (array) $tjids_to_load, 'IN')
->execute();
foreach ($result as $row) {
$return[$row->tjid] = $statistics[$row->tjid] = $row;
}
}
return $return;
}