Skip to content

Commit

Permalink
Plugin: ExerciseFocused: Allow export for exercise with one question …
Browse files Browse the repository at this point in the history
…per page - refs BT#21074
  • Loading branch information
AngelFQC committed Oct 18, 2023
1 parent b3db1cc commit 7bd2fc7
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 1 deletion.
232 changes: 232 additions & 0 deletions plugin/exercisefocused/pages/export.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
<?php

/* For licensing terms, see /license.txt */

use Chamilo\CoreBundle\Entity\TrackEAttempt;
use Chamilo\CoreBundle\Entity\TrackEExercises;
use Chamilo\CourseBundle\Entity\CQuiz;
use Chamilo\PluginBundle\ExerciseFocused\Entity\Log;
use Chamilo\UserBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\Expr\Join;
use Symfony\Component\HttpFoundation\Request as HttpRequest;

require_once __DIR__.'/../../../main/inc/global.inc.php';

api_protect_course_script(true);

if (!api_is_allowed_to_edit()) {
api_not_allowed(true);
}

$plugin = ExerciseFocusedPlugin::create();
$request = HttpRequest::createFromGlobals();
$em = Database::getManager();
$logRepository = $em->getRepository(Log::class);
$attempsRepository = $em->getRepository(TrackEAttempt::class);

if (!$plugin->isEnabled(true)) {
api_not_allowed(true);
}

$params = $request->query->all();

$results = findResults($params, $em, $plugin);

$data = [];

/** @var array<string, mixed> $result */
foreach ($results as $result) {
/** @var TrackEExercises $trackExe */
$trackExe = $result['exe'];
$user = api_get_user_entity($trackExe->getExeUserId());

$outfocusedLimitCount = $logRepository->countByActionInExe($trackExe, Log::TYPE_OUTFOCUSED_LIMIT);
$timeLimitCount = $logRepository->countByActionInExe($trackExe, Log::TYPE_TIME_LIMIT);

$exercise = new Exercise($trackExe->getCId());
$exercise->read($trackExe->getExeExoId());

$quizType = (int) $exercise->selectType();

if ($trackExe->getSessionId()) {
$data[] = [
get_lang('SessionName'),
api_get_session_entity($trackExe->getSessionId())->getName(),
];
}
$data[] = [
get_lang('Course'),
api_get_course_entity($trackExe->getCId())->getTitle(),
];
$data[] = [
get_lang('ExerciseName'),
$exercise->getUnformattedTitle(),
];
$data[] = [
get_lang('Learner'),
$user->getUsername(),
$user->getFirstname(),
$user->getLastname(),
];
$data[] = [
get_lang('StartDate'),
api_get_local_time($result['exe']->getStartDate(), null, null, true, true, true),
get_lang('EndDate'),
api_get_local_time($result['exe']->getExeDate(), null, null, true, true, true),
];
$data[] = [
$plugin->get_lang('Motive'),
$plugin->calculateMotive($outfocusedLimitCount, $timeLimitCount),
];
$data[] = [];

if (ONE_PER_PAGE === $quizType) {
$questionList = explode(',', $trackExe->getDataTracking());

$data[] = [
get_lang('Level'),
get_lang('DateExo'),
get_lang('Score'),
$plugin->get_lang('Outfocused'),
$plugin->get_lang('Returns'),
];

foreach ($questionList as $idx => $questionId) {
$attempt = $attempsRepository->findOneBy(
['exeId' => $trackExe->getExeId(), 'questionId' => $questionId],
['tms' => 'DESC']
);

$result = $exercise->manage_answer(
$trackExe->getExeId(),
$questionId,
null,
'exercise_result',
false,
false,
true,
false,
$exercise->selectPropagateNeg()
);

$data[] = [
get_lang('QuestionNumber').' '.($idx + 1),
api_get_local_time($attempt->getTms()),
$result['score'].' / '.$result['weight'],
$logRepository->countByActionAndLevel($trackExe, Log::TYPE_OUTFOCUSED, $questionId),
$logRepository->countByActionAndLevel($trackExe, Log::TYPE_RETURN, $questionId),
];
}
} elseif (ALL_ON_ONE_PAGE === $quizType) {

}

$data[] = [];
}

//var_dump($data);
//Export::export_table_xls_html($data);
Export::arrayToXls($data);

function getSessionIdFromFormValues(array $formValues, array $fieldVariableList): array
{
$fieldItemIdList = [];
$objFieldValue = new ExtraFieldValue('session');

foreach ($fieldVariableList as $fieldVariable) {
if (!isset($formValues["extra_$fieldVariable"])) {
continue;
}

$itemValue = $objFieldValue->get_item_id_from_field_variable_and_field_value(
$fieldVariable,
$formValues["extra_$fieldVariable"]
);

if ($itemValue) {
$fieldItemIdList[] = (int) $itemValue['item_id'];
}
}

return array_unique($fieldItemIdList);
}

function findResults(array $formValues, EntityManagerInterface $em, ExerciseFocusedPlugin $plugin)
{
$cId = api_get_course_int_id();

$qb = $em->createQueryBuilder();
$qb
->select('te AS exe, q.title, te.startDate , u.firstname, u.lastname, u.username')
->from(TrackEExercises::class, 'te')
->innerJoin(CQuiz::class, 'q', Join::WITH, 'te.exeExoId = q.iid')
->innerJoin(User::class, 'u', Join::WITH, 'te.exeUserId = u.id');

$params = [];

if ($cId) {
$qb->andWhere($qb->expr()->eq('te.cId', ':cId'));

$params['cId'] = $cId;
}

$sessionItemIdList = getSessionIdFromFormValues(
$formValues,
$plugin->getSessionFieldList()
);

if ($sessionItemIdList) {
$qb->andWhere($qb->expr()->in('te.sessionId', ':sessionItemIdList'));

$params['sessionItemIdList'] = $sessionItemIdList;
} else {
$qb->andWhere($qb->expr()->isNull('te.sessionId'));
}

if (!empty($formValues['username'])) {
$qb->andWhere($qb->expr()->eq('u.username', ':username'));

$params['username'] = $formValues['username'];
}

if (!empty($formValues['firstname'])) {
$qb->andWhere($qb->expr()->eq('u.firstname', ':firstname'));

$params['firstname'] = $formValues['firstname'];
}

if (!empty($formValues['lastname'])) {
$qb->andWhere($qb->expr()->eq('u.lastname', ':lastname'));

$params['lastname'] = $formValues['lastname'];
}

if (!empty($formValues['start_date'])) {
$qb->andWhere(
$qb->expr()->andX(
$qb->expr()->gte('te.startDate', ':start_date'),
$qb->expr()->lte('te.exeDate', ':end_date')
)
);

$params['start_date'] = api_get_utc_datetime($formValues['start_date'].' 00:00:00', false, true);
$params['end_date'] = api_get_utc_datetime($formValues['start_date'].' 23:59:59', false, true);
}

if (empty($params)) {
return [];
}

if ($cId && !empty($formValues['id'])) {
$qb->andWhere($qb->expr()->eq('q.iid', ':q_id'));

$params['q_id'] = $formValues['id'];
}

$qb->setParameters($params);

$query = $qb->getQuery();

return $query->getResult();
}
13 changes: 12 additions & 1 deletion plugin/exercisefocused/src/Controller/ReportingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,27 @@ private function generateTabSearch(CQuiz $exercise, string $courseCode, int $ses
$form->addHidden('id', $exercise->getId());

$tableHtml = '';
$actions = '';

if ($form->validate()) {
$formValues = $form->exportValues();

$action = Display::url(
Display::return_icon('export_excel.png', get_lang('ExportExcel'), [], ICON_SIZE_MEDIUM),
api_get_path(WEB_PLUGIN_PATH).'exercisefocused/pages/export.php?'.http_build_query($formValues)
);

$actions = Display::toolbarAction(
'em-actions',
[$action]
);

$results = $this->findResults($formValues);

$tableHtml = $this->createTable($results)->toHtml();
}

return $form->returnForm().$tableHtml;
return $form->returnForm().$actions.$tableHtml;
}

private function generateTabSampling(CQuiz $exercise): string
Expand Down
16 changes: 16 additions & 0 deletions plugin/exercisefocused/src/ExerciseFocusedPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/* For licensing terms, see /license.txt */

use Chamilo\CoreBundle\Entity\TrackEExercises;
use Chamilo\CourseBundle\Entity\CTool;
use Chamilo\PluginBundle\ExerciseFocused\Entity\Log;
use Doctrine\ORM\Tools\SchemaTool;
Expand Down Expand Up @@ -177,6 +178,21 @@ public function isEnableForExercise(int $exerciseId): bool
return $values && (bool) $values['value'];
}

public function calculateMotive(int $outfocusedLimitCount, int $timeLimitCount)
{
$motive = get_lang('ExerciseFinished');

if ($outfocusedLimitCount > 0) {
$motive = $this->plugin->get_lang('MaxOutfocusedReached');
}

if ($timeLimitCount > 0) {
$motive = $this->plugin->get_lang('TimeLimitReached');
}

return $motive;
}

protected function createLinkToCourseTool($name, $courseId, $iconName = null, $link = null, $sessionId = 0, $category = 'plugin'): ?CTool
{
$tool = parent::createLinkToCourseTool($name, $courseId, $iconName, $link, $sessionId, $category);
Expand Down
9 changes: 9 additions & 0 deletions plugin/exercisefocused/src/Repository/LogRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ public function countByActionInExe(TrackEExercises $exe, string $action): int
'action' => $action,
]);
}

public function countByActionAndLevel(TrackEExercises $exe, string $action, int $level): int
{
return $this->count([
'exe' => $exe,
'action' => $action,
'level' => $level,
]);
}
}

0 comments on commit 7bd2fc7

Please sign in to comment.