Skip to content

Commit

Permalink
Merge branch 'BT20901' into proctoring-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Sep 25, 2023
2 parents a4c873f + aaac2af commit 20edf9c
Show file tree
Hide file tree
Showing 14 changed files with 824 additions and 0 deletions.
4 changes: 4 additions & 0 deletions plugin/exercisemonitoring/admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

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

43 changes: 43 additions & 0 deletions plugin/exercisemonitoring/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

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

$plugin = ExerciseMonitoringPlugin::create();

$_template['show_overview_region'] = $plugin->isEnabled(true)
&& strpos($_SERVER['SCRIPT_NAME'], '/main/exercise/overview.php') !== false;

$_template['enabled'] = false;

if ($_template['show_overview_region']) {
$exerciseId = (int) $_GET['exerciseId'];

$objFieldValue = new ExtraFieldValue('exercise');
$values = $objFieldValue->get_values_by_handler_and_field_variable($exerciseId, ExerciseMonitoringPlugin::FIELD_SELECTED);

if ($values && (bool) $values['value']) {
$_template['enabled'] = true;

$existingExeId = (int) ChamiloSession::read('exe_id');
}

$_template['exercise_id'] = $exerciseId;
}

$_template['show_submit_region'] = $plugin->isEnabled(true)
&& strpos($_SERVER['SCRIPT_NAME'], '/main/exercise/exercise_submit.php') !== false;

if ($_template['show_submit_region']) {
$exerciseId = (int) $_GET['exerciseId'];

$objFieldValue = new ExtraFieldValue('exercise');
$values = $objFieldValue->get_values_by_handler_and_field_variable($exerciseId, ExerciseMonitoringPlugin::FIELD_SELECTED);

if ($values && (bool) $values['value']) {
$existingExeId = (int) ChamiloSession::read('exe_id');

$_template['enabled'] = true;
$_template['exercise_id'] = $exerciseId;
$_template['exe_id'] = $existingExeId;
}
}
5 changes: 5 additions & 0 deletions plugin/exercisemonitoring/install.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

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

ExerciseMonitoringPlugin::create()->install();
13 changes: 13 additions & 0 deletions plugin/exercisemonitoring/lang/english.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

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

$strings['plugin_title'] = "Exercise monitoring";
$strings['plugin_comment'] = "Random photo taking mechanism during an exercise";

$strings['tool_enable'] = "Enable tool";

$strings['ExerciseMonitored'] = "Exercise monitored";
$strings['Retry'] = "Retry";
$strings['IdDocumentSnapshot'] = "Snapshot of the ID document";
$strings['LearnerSnapshot'] = "Snapshot of the student";
18 changes: 18 additions & 0 deletions plugin/exercisemonitoring/pages/exercise_submit.ajax.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

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

use Symfony\Component\HttpFoundation\Request as HttpRequest;

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

api_protect_course_script();

$plugin = ExerciseMonitoringPlugin::create();
$request = HttpRequest::createFromGlobals();
$em = Database::getManager();

$exerciseSubmitController = new ExerciseSubmitController($plugin, $request, $em);

$response = $exerciseSubmitController();
$response->send();
18 changes: 18 additions & 0 deletions plugin/exercisemonitoring/pages/start.ajax.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

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

use Symfony\Component\HttpFoundation\Request as HttpRequest;

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

api_protect_course_script();

$plugin = ExerciseMonitoringPlugin::create();
$request = HttpRequest::createFromGlobals();
$em = Database::getManager();

$startController = new StartController($plugin, $request, $em);

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

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

$plugin_info = ExerciseMonitoringPlugin::create()->get_info();

$plugin_info['templates'] = [
'templates/modal.html.twig',
'templates/exercise_submit.html.twig',
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

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

use Chamilo\CoreBundle\Entity\TrackEExercises;
use Chamilo\CourseBundle\Entity\CQuiz;
use Chamilo\CourseBundle\Entity\CQuizQuestion;
use Chamilo\PluginBundle\ExerciseMonitoring\Entity\Log;
use Doctrine\ORM\EntityManager;
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 ExerciseSubmitController
{
private $plugin;
private $request;
private $em;

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

/**
* @throws \Doctrine\ORM\OptimisticLockException
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\TransactionRequiredException
*/
public function __invoke(): HttpResponse
{
$userDirName = $this->createDirectory();

$existingExeId = (int) ChamiloSession::read('exe_id');
$imgIddoc = $this->request->files->get('snapshot');

$exercise = $this->em->find(
CQuiz::class,
$this->request->request->getInt('exercise_id')
);
$question = $this->em->find(
CQuizQuestion::class,
$this->request->request->getInt('question_id')
);
$trackingExercise = $this->em->find(TrackEExercises::class, $existingExeId);

if ($imgIddoc) {
$newFilename = uniqid().'_exercise.jpg';

$imgIddoc->move($userDirName, $newFilename);

$log = new Log();
$log
->setExercise($exercise)
->setExe($trackingExercise)
->setLevel($question->getIid())
->setImageFilename($newFilename)
;

$this->em->persist($log);
}

$this->em->flush();

return HttpResponse::create();
}

private function createDirectory(): string
{
$user = api_get_user_entity(api_get_user_id());

$pluginDirName = api_get_path(SYS_UPLOAD_PATH).'plugins/exercisemonitoring';
$userDirName = $pluginDirName.'/'.$user->getId();

$fs = new Filesystem();
$fs->mkdir(
[$pluginDirName, $userDirName],
api_get_permissions_for_new_directories()
);

return $userDirName;
}
}
87 changes: 87 additions & 0 deletions plugin/exercisemonitoring/src/Controller/StartController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

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

use Chamilo\CourseBundle\Entity\CQuiz;
use Chamilo\PluginBundle\ExerciseMonitoring\Entity\Log;
use Doctrine\ORM\EntityManager;
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 StartController
{
private $plugin;
private $request;
private $em;

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

public function __invoke(): HttpResponse
{
$userDirName = $this->createDirectory();

/** @var UploadedFile $imgIddoc */
$imgIddoc = $this->request->files->get('iddoc');
/** @var UploadedFile $imgLearner */
$imgLearner = $this->request->files->get('learner');

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

if ($imgIddoc) {
$newFilename = uniqid().'_iddoc.jpg';

$imgIddoc->move($userDirName, $newFilename);

$log = new Log();
$log
->setExercise($exercise)
->setLevel(-1)
->setImageFilename($newFilename)
;

$this->em->persist($log);
}

if ($imgLearner) {
$newFilename = uniqid().'_learner.jpg';

$imgLearner->move($userDirName, $newFilename);

$log = new Log();
$log
->setExercise($exercise)
->setLevel(0)
->setImageFilename($newFilename)
;

$this->em->persist($log);
}

$this->em->flush();

return HttpResponse::create();
}

private function createDirectory(): string
{
$user = api_get_user_entity(api_get_user_id());

$pluginDirName = api_get_path(SYS_UPLOAD_PATH).'plugins/exercisemonitoring';
$userDirName = $pluginDirName.'/'.$user->getId();

$fs = new Filesystem();
$fs->mkdir(
[$pluginDirName, $userDirName],
api_get_permissions_for_new_directories()
);

return $userDirName;
}
}
Loading

0 comments on commit 20edf9c

Please sign in to comment.