Skip to content

Commit

Permalink
Plugin: ExerciseMonitoring: Show snapshot logs in ExerciseFocused plu…
Browse files Browse the repository at this point in the history
…gin report - refs BT#21074
  • Loading branch information
AngelFQC committed Oct 6, 2023
1 parent e04802b commit c48f466
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 5 deletions.
17 changes: 17 additions & 0 deletions plugin/exercisefocused/src/Traits/ReportingFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Display;
use Doctrine\ORM\Query\Expr\Join;
use Exception;
use ExerciseMonitoringPlugin;
use ExtraField;
use ExtraFieldValue;
use FormValidator;
Expand Down Expand Up @@ -192,9 +193,13 @@ protected function formatResults(array $queryResults): array

protected function createTable(array $tableData): HTML_Table
{
$monitoringPluginEnabled = ExerciseMonitoringPlugin::create()->isEnabled(true);

$detailIcon = Display::return_icon('forum_listview.png', get_lang('Detail'));
$webcamIcon = Display::return_icon('webcam.png', $this->plugin->get_lang('MonitoringDetail'));

$urlDetail = api_get_path(WEB_PLUGIN_PATH).'exercisefocused/pages/detail.php?'.api_get_cidreq().'&';
$monitoringDetail = api_get_path(WEB_PLUGIN_PATH).'exercisemonitoring/pages/detail.php?'.api_get_cidreq().'&';

$table = new HTML_Table(['class' => 'table table-hover table-striped data_table']);
$table->setHeaderContents(0, 0, get_lang('Username'));
Expand All @@ -219,6 +224,18 @@ protected function createTable(array $tableData): HTML_Table
]
);

if ($monitoringPluginEnabled) {
$url .= Display::url(
$webcamIcon,
$monitoringDetail.http_build_query(['id' => $result['id']]),
[
'class' => 'ajax',
'data-title' => get_lang('MonitoringDetail'),
'data-size' => 'lg',
]
);
}

$table->setCellContents($row, 0, $result['username']);
$table->setCellContents($row, 1, $result['user_fullname']);
$table->setCellContents($row, 2, $result['quiz_title']);
Expand Down
32 changes: 32 additions & 0 deletions plugin/exercisemonitoring/pages/detail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

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

use Chamilo\PluginBundle\ExerciseMonitoring\Controller\DetailController;
use Chamilo\PluginBundle\ExerciseMonitoring\Entity\Log;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
use Symfony\Component\HttpFoundation\Response as HttpResponse;

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

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

$em = Database::getManager();
$logRepository = $em->getRepository(Log::class);

$detailController = new DetailController(
ExerciseMonitoringPlugin::create(),
HttpRequest::createFromGlobals(),
$em,
$logRepository
);

try {
$response = $detailController();
} catch (Exception $e) {
$response = HttpResponse::create('', HttpResponse::HTTP_FORBIDDEN);
}

$response->send();
161 changes: 161 additions & 0 deletions plugin/exercisemonitoring/src/Controller/DetailController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

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

namespace Chamilo\PluginBundle\ExerciseMonitoring\Controller;

use Chamilo\CoreBundle\Entity\TrackEAttempt;
use Chamilo\CoreBundle\Entity\TrackEExercises;
use Chamilo\CourseBundle\Entity\CQuiz;
use Chamilo\CourseBundle\Entity\CQuizQuestion;
use Chamilo\PluginBundle\ExerciseMonitoring\Entity\Log;
use Chamilo\UserBundle\Entity\User;
use Display;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\Expr\Join;
use Exception;
use Exercise;
use ExerciseMonitoringPlugin;
use HTML_Table;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
use Symfony\Component\HttpFoundation\Response as HttpResponse;

class DetailController

Check warning on line 26 in plugin/exercisemonitoring/src/Controller/DetailController.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

plugin/exercisemonitoring/src/Controller/DetailController.php#L26

The class DetailController has a coupling between objects value of 13. Consider to reduce the number of dependencies under 13.
{
/**
* @var ExerciseMonitoringPlugin
*/
private $plugin;

/**
* @var HttpRequest
*/
private $request;

/**
* @var EntityManager
*/
private $em;

/**
* @var EntityRepository
*/
private $logRepository;

public function __construct(
ExerciseMonitoringPlugin $plugin,
HttpRequest $request,
EntityManager $em,
EntityRepository $logRepository
) {
$this->plugin = $plugin;
$this->request = $request;
$this->em = $em;
$this->logRepository = $logRepository;
}

/**
* @throws Exception
*/
public function __invoke(): HttpResponse
{
if (!$this->plugin->isEnabled(true)) {
throw new Exception();
}

$trackExe = $this->em->find(
TrackEExercises::class,
$this->request->query->getInt('id')
);

if (!$trackExe) {
throw new Exception();
}

$exercise = $this->em->find(CQuiz::class, $trackExe->getExeExoId());

Check warning on line 78 in plugin/exercisemonitoring/src/Controller/DetailController.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

plugin/exercisemonitoring/src/Controller/DetailController.php#L78

Avoid unused local variables such as '$exercise'.
$user = api_get_user_entity($trackExe->getExeUserId());

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

$qb = $this->logRepository->createQueryBuilder('l');
$qb
->select(['l.imageFilename', 'l.createdAt']);

if (ONE_PER_PAGE == $objExercise->selectType()) {
$qb
->addSelect(['qq.question'])
->innerJoin(CQuizQuestion::class, 'qq', Join::WITH, 'l.level = qq.iid');
}

$logs = $qb
->andWhere(
$qb->expr()->eq('l.exe', $trackExe->getExeId())
)
->addOrderBy('l.createdAt')
->getQuery()
->getResult();

$content = $this->generateHeader($objExercise, $user, $trackExe)
.'<hr>'
.$this->generateSnapshotList($logs, $trackExe->getExeUserId());

return HttpResponse::create($content);
}

private function generateHeader(Exercise $objExercise, User $student, TrackEExercises $trackExe): string
{
$startDate = api_get_local_time($trackExe->getStartDate(), null, null, true, true, true);
$endDate = api_get_local_time($trackExe->getExeDate(), null, null, true, true, true);

return Display::page_subheader2($objExercise->selectTitle())
.Display::tag('p', $student->getCompleteNameWithUsername(), ['class' => 'lead'])
.Display::tag(
'p',
sprintf(get_lang('QuizRemindStartDate'), $startDate)
.sprintf(get_lang('QuizRemindEndDate'), $endDate)
.sprintf(get_lang('QuizRemindDuration'), api_format_time($trackExe->getExeDuration()))
);
}

/**
* @param array $logs
* @param int $userId
*
* @return string
*/
private function generateSnapshotList(array $logs, int $userId): string
{
$pluginDirName = api_get_path(WEB_UPLOAD_PATH).'plugins/exercisemonitoring';

$html = '';

foreach ($logs as $log) {
$userDirName = $pluginDirName.'/'.$userId;

$date = api_get_local_time($log['createdAt'], null, null, true, true, true);

$html .= '<div class="col-xs-12 col-sm-6 col-md-3">';
$html .= '<div class="thumbnail">';
$html .= Display::img(
$userDirName.'/'.$log['imageFilename'],
$date
);
$html .= '<div class="caption">';
$html .= Display::tag('p', $date, ['class' => 'text-center']);

if (!empty($log['question'])) {
$html .= Display::tag('div', $log['question'], ['class' => 'text-center']);
}

$html .= '</div>';
$html .= '</div>';
$html .= '</div>';
}

return '<div class="row">'.$html.'</div>';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ public function __invoke(): HttpResponse
$existingExeId = (int) ChamiloSession::read('exe_id');

$levelId = $this->request->request->getInt('level_id');
$exercise = $this->em->find(
CQuiz::class,
$this->request->request->getInt('exercise_id')
);
$exerciseId = $this->request->request->getInt('exercise_id');

$exercise = $this->em->find(CQuiz::class, $exerciseId);

$objExercise = new Exercise();
$objExercise->read($exerciseId);

$trackingExercise = $this->em->find(TrackEExercises::class, $existingExeId);

Expand All @@ -54,7 +56,7 @@ public function __invoke(): HttpResponse
$imgSubmit->move($userDirName, $newFilename);
}

if (ONE_PER_PAGE === $exercise->getType()) {
if (ONE_PER_PAGE == $objExercise->selectType()) {
$question = $this->em->find(CQuizQuestion::class, $levelId);
$level = $question->getIid();
}
Expand Down

0 comments on commit c48f466

Please sign in to comment.