174 lines
7.4 KiB
PHP
174 lines
7.4 KiB
PHP
<?php
|
|
|
|
namespace Komponent\Component\Testquestions\Administrator\Model;
|
|
|
|
defined('_JEXEC') or die;
|
|
|
|
use Joomla\CMS\Factory;
|
|
use Joomla\CMS\MVC\Model\ListModel;
|
|
|
|
class ResultsModel extends ListModel
|
|
{
|
|
public function getItems()
|
|
{
|
|
$items = parent::getItems();
|
|
|
|
foreach ($items as $item) {
|
|
$item->has_assessment = false;
|
|
$item->ai_input_tokens = 0;
|
|
$item->ai_output_tokens = 0;
|
|
$item->ai_total_tokens = 0;
|
|
$item->ai_cost_usd = 0.0;
|
|
$item->ai_tokens_by_provider = ['openai' => 0, 'grok' => 0, 'groq' => 0, 'gemini' => 0];
|
|
|
|
$answers = json_decode((string) ($item->answers ?? ''), true);
|
|
if (!is_array($answers)) {
|
|
continue;
|
|
}
|
|
|
|
foreach ($answers as $answer) {
|
|
if (!is_array($answer)) {
|
|
continue;
|
|
}
|
|
|
|
// Manual assessment and AI both save awarded_points in the answer JSON.
|
|
if (array_key_exists('awarded_points', $answer)) {
|
|
$item->has_assessment = true;
|
|
}
|
|
|
|
$usages = $answer['ai_usage_history'] ?? (isset($answer['ai_usage']) ? [$answer['ai_usage']] : []);
|
|
if (!is_array($usages)) {
|
|
continue;
|
|
}
|
|
|
|
foreach ($usages as $usage) {
|
|
if (!is_array($usage)) {
|
|
continue;
|
|
}
|
|
$item->ai_input_tokens += (int) ($usage['input_tokens'] ?? 0);
|
|
$item->ai_output_tokens += (int) ($usage['output_tokens'] ?? 0);
|
|
$item->ai_total_tokens += (int) ($usage['total_tokens'] ?? 0);
|
|
$provider = (string) ($usage['provider'] ?? '');
|
|
if (isset($item->ai_tokens_by_provider[$provider])) {
|
|
$item->ai_tokens_by_provider[$provider] += (int) ($usage['total_tokens'] ?? 0);
|
|
}
|
|
if (is_numeric($usage['cost_usd'] ?? null)) {
|
|
$item->ai_cost_usd += (float) $usage['cost_usd'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
|
|
public function getAiUsageTotals(): array
|
|
{
|
|
$db = Factory::getContainer()->get('DatabaseDriver');
|
|
$query = clone $this->getListQuery();
|
|
$query->clear('select')->clear('order')->select($db->quoteName('r.answers'));
|
|
$answersRows = $db->setQuery($query)->loadColumn();
|
|
$emptyProviderTotals = ['input_tokens' => 0, 'output_tokens' => 0, 'total_tokens' => 0, 'cost_usd' => 0.0];
|
|
$totals = [
|
|
'input_tokens' => 0,
|
|
'output_tokens' => 0,
|
|
'total_tokens' => 0,
|
|
'cost_usd' => 0.0,
|
|
'providers' => ['openai' => $emptyProviderTotals, 'grok' => $emptyProviderTotals, 'groq' => $emptyProviderTotals, 'gemini' => $emptyProviderTotals],
|
|
];
|
|
|
|
foreach ($answersRows as $answersJson) {
|
|
$answers = json_decode((string) $answersJson, true);
|
|
if (!is_array($answers)) {
|
|
continue;
|
|
}
|
|
foreach ($answers as $answer) {
|
|
if (!is_array($answer)) {
|
|
continue;
|
|
}
|
|
$usages = $answer['ai_usage_history'] ?? (isset($answer['ai_usage']) ? [$answer['ai_usage']] : []);
|
|
if (!is_array($usages)) {
|
|
continue;
|
|
}
|
|
foreach ($usages as $usage) {
|
|
if (!is_array($usage)) {
|
|
continue;
|
|
}
|
|
$totals['input_tokens'] += (int) ($usage['input_tokens'] ?? 0);
|
|
$totals['output_tokens'] += (int) ($usage['output_tokens'] ?? 0);
|
|
$totals['total_tokens'] += (int) ($usage['total_tokens'] ?? 0);
|
|
if (is_numeric($usage['cost_usd'] ?? null)) {
|
|
$totals['cost_usd'] += (float) $usage['cost_usd'];
|
|
}
|
|
$provider = (string) ($usage['provider'] ?? '');
|
|
if (isset($totals['providers'][$provider])) {
|
|
$totals['providers'][$provider]['input_tokens'] += (int) ($usage['input_tokens'] ?? 0);
|
|
$totals['providers'][$provider]['output_tokens'] += (int) ($usage['output_tokens'] ?? 0);
|
|
$totals['providers'][$provider]['total_tokens'] += (int) ($usage['total_tokens'] ?? 0);
|
|
if (is_numeric($usage['cost_usd'] ?? null)) {
|
|
$totals['providers'][$provider]['cost_usd'] += (float) $usage['cost_usd'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $totals;
|
|
}
|
|
|
|
protected function populateState($ordering = 'r.created', $direction = 'desc')
|
|
{
|
|
parent::populateState($ordering, $direction);
|
|
$this->setState('filter.search', $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search', '', 'string'));
|
|
$this->setState('filter.set_id', $this->getUserStateFromRequest($this->context . '.filter.set_id', 'filter_set_id', 0, 'int'));
|
|
$this->setState('filter.date_from', $this->getUserStateFromRequest($this->context . '.filter.date_from', 'filter_date_from', '', 'string'));
|
|
$this->setState('filter.date_to', $this->getUserStateFromRequest($this->context . '.filter.date_to', 'filter_date_to', '', 'string'));
|
|
}
|
|
|
|
protected function getListQuery()
|
|
{
|
|
$db = Factory::getContainer()->get('DatabaseDriver');
|
|
$query = $db->getQuery(true)
|
|
->select(['r.*', 's.title AS set_title', 'sub.title AS subject_title'])
|
|
->from($db->quoteName('#__testquestions_results', 'r'))
|
|
->leftJoin($db->quoteName('#__testquestions_sets', 's') . ' ON s.id = r.set_id')
|
|
->leftJoin($db->quoteName('#__testquestions_subjects', 'sub') . ' ON sub.id = s.subject_id');
|
|
|
|
$search = trim((string) $this->getState('filter.search'));
|
|
if ($search !== '') {
|
|
$needle = $db->quote('%' . $db->escape($search, true) . '%', false);
|
|
$query->where('(r.participant_name LIKE ' . $needle . ' OR s.title LIKE ' . $needle . ')');
|
|
}
|
|
|
|
$setId = (int) $this->getState('filter.set_id');
|
|
if ($setId > 0) {
|
|
$query->where('r.set_id = ' . $setId);
|
|
}
|
|
|
|
$dateFrom = (string) $this->getState('filter.date_from');
|
|
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $dateFrom)) {
|
|
$query->where('r.created >= ' . $db->quote($dateFrom . ' 00:00:00'));
|
|
}
|
|
|
|
$dateTo = (string) $this->getState('filter.date_to');
|
|
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $dateTo)) {
|
|
$query->where('r.created <= ' . $db->quote($dateTo . ' 23:59:59'));
|
|
}
|
|
|
|
return $query->order('r.created DESC, r.id DESC');
|
|
}
|
|
|
|
public function getSets(): array
|
|
{
|
|
$db = Factory::getContainer()->get('DatabaseDriver');
|
|
|
|
return $db->setQuery(
|
|
$db->getQuery(true)
|
|
->select(['s.id', 's.title', 's.published', 'sub.title AS subject_title'])
|
|
->from($db->quoteName('#__testquestions_sets', 's'))
|
|
->leftJoin($db->quoteName('#__testquestions_subjects', 'sub') . ' ON sub.id = s.subject_id')
|
|
->order('sub.title ASC, s.title ASC, s.id ASC')
|
|
)->loadObjectList();
|
|
}
|
|
}
|