Skip to content

Commit c49cefd

Browse files
committed
Plugin: ExerciseFocused: Add setting to generate random sampling - refs BT#21074
1 parent 93097cf commit c49cefd

File tree

4 files changed

+124
-39
lines changed

4 files changed

+124
-39
lines changed

plugin/exercisefocused/lang/english.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
$strings['outfocused_limit_help'] = "Number of outfocused allowed. After this limit the exercise will be closed.";
1414
$strings['session_field_filters'] = "Session field as filter";
1515
$strings['session_field_filters_help'] = "Extra field names separeted by a comma.";
16+
$strings['percentage_sampling'] = "Percentage of sampling attempts";
17+
$strings['percentage_sampling_help'] = "A percentage of attempts will be selected for random review";
1618

1719
$strings['ReportByAttempts'] = "Exercise focused: Report by attempts";
1820
$strings['YouHaveLeftTheExercise'] = "You have left the exercise";
@@ -28,3 +30,4 @@
2830
$strings['Return'] = "Return";
2931
$strings['Motive'] = "Motive";
3032
$strings['AlertBeforeLeaving'] = "Before leaving the exercise, consider that this action will be tracked.";
33+
$strings['RandomSampling'] = "Random sampling";

plugin/exercisefocused/src/Controller/ReportingController.php

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ public function __invoke(): HttpResponse
1919
{
2020
parent::__invoke();
2121

22-
$exerciseId = $this->request->query->getInt('id');
23-
$exercise = $this->em->find(CQuiz::class, $exerciseId);
22+
$exercise = $this->em->find(
23+
CQuiz::class,
24+
$this->request->query->getInt('id')
25+
);
2426

2527
if (!$exercise) {
2628
throw new Exception();
@@ -29,6 +31,36 @@ public function __invoke(): HttpResponse
2931
$courseCode = api_get_course_id();
3032
$sessionId = api_get_session_id();
3133

34+
$tab1 = $this->generateTabSearch($exercise, $courseCode, $sessionId);
35+
36+
$tab2 = $this->generateTabSampling($exercise);
37+
38+
$content = Display::tabs(
39+
[
40+
$this->plugin->get_lang('ReportByAttempts'),
41+
$this->plugin->get_lang('RandomSampling'),
42+
],
43+
[$tab1, $tab2],
44+
'exercise-focused-tabs',
45+
[],
46+
[],
47+
1
48+
);
49+
50+
$this->setBreadcrumb($exercise->getId());
51+
52+
return $this->renderView(
53+
get_lang('ReportByAttempts'),
54+
$content,
55+
$exercise->getTitle()
56+
);
57+
}
58+
59+
/**
60+
* @throws Exception
61+
*/
62+
private function generateTabSearch(CQuiz $exercise, string $courseCode, int $sessionId): string
63+
{
3264
$form = $this->createForm();
3365
$form->updateAttributes(['action' => api_get_self().'?'.api_get_cidreq().'&id='.$exercise->getId()]);
3466
$form->addHidden('cidReq', $courseCode);
@@ -38,27 +70,24 @@ public function __invoke(): HttpResponse
3870
$form->addHidden('origin', api_get_origin());
3971
$form->addHidden('id', $exercise->getId());
4072

41-
$results = [];
73+
$tableHtml = '';
4274

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

4678
$results = $this->findResults($formValues);
47-
}
4879

49-
$table = $this->createTable($results);
80+
$tableHtml = $this->createTable($results)->toHtml();
81+
}
5082

51-
$this->setBreadcrumb($exercise->getId());
83+
return $form->returnForm().$tableHtml;
84+
}
5285

53-
$content = $form->returnForm()
54-
.Display::page_subheader(get_lang('ReportByAttempts'))
55-
.$table->toHtml();
86+
private function generateTabSampling(CQuiz $exercise): string
87+
{
88+
$results = $this->findRandomResults($exercise->getId());
5689

57-
return $this->renderView(
58-
get_lang('ReportByAttempts'),
59-
$content,
60-
$exercise->getTitle()
61-
);
90+
return $this->createTable($results)->toHtml();
6291
}
6392

6493
/**

plugin/exercisefocused/src/ExerciseFocusedPlugin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class ExerciseFocusedPlugin extends Plugin
1414
public const SETTING_ENABLE_OUTFOCUSED_LIMIT = 'enable_outfocused_limit';
1515
public const SETTING_OUTFOCUSED_LIMIT = 'outfocused_limit';
1616
public const SETTING_SESSION_FIELD_FILTERS = 'session_field_filters';
17+
public const SETTING_PERCENTAGE_SAMPLING = 'percentage_sampling';
1718

1819
public const FIELD_SELECTED = 'exercisefocused_selected';
1920

@@ -27,6 +28,7 @@ protected function __construct()
2728
self::SETTING_ENABLE_OUTFOCUSED_LIMIT => 'boolean',
2829
self::SETTING_OUTFOCUSED_LIMIT => 'text',
2930
self::SETTING_SESSION_FIELD_FILTERS => 'text',
31+
self::SETTING_PERCENTAGE_SAMPLING => 'text',
3032
];
3133

3234
parent::__construct(

plugin/exercisefocused/src/Traits/ReportingFilterTrait.php

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Chamilo\CourseBundle\Entity\CQuiz;
99
use Chamilo\PluginBundle\ExerciseFocused\Entity\Log;
1010
use Chamilo\UserBundle\Entity\User;
11+
use Database;
1112
use Display;
1213
use Doctrine\ORM\Query\Expr\Join;
1314
use Exception;
@@ -61,29 +62,6 @@ protected function createForm(): FormValidator
6162
return $form;
6263
}
6364

64-
private function getSessionIdFromFormValues(array $formValues, array $fieldVariableList): array
65-
{
66-
$fieldItemIdList = [];
67-
$objFieldValue = new ExtraFieldValue('session');
68-
69-
foreach ($fieldVariableList as $fieldVariable) {
70-
if (!isset($formValues["extra_$fieldVariable"])) {
71-
continue;
72-
}
73-
74-
$itemValue = $objFieldValue->get_item_id_from_field_variable_and_field_value(
75-
$fieldVariable,
76-
$formValues["extra_$fieldVariable"]
77-
);
78-
79-
if ($itemValue) {
80-
$fieldItemIdList[] = (int) $itemValue['item_id'];
81-
}
82-
}
83-
84-
return array_unique($fieldItemIdList);
85-
}
86-
8765
/**
8866
* @throws Exception
8967
*/
@@ -161,10 +139,16 @@ protected function findResults(array $formValues = []): array
161139

162140
$qb->setParameters($params);
163141

142+
return $this->formatResults(
143+
$qb->getQuery()->getResult()
144+
);
145+
}
146+
147+
protected function formatResults(array $queryResults): array
148+
{
164149
$results = [];
165150

166-
foreach ($qb->getQuery()->getResult() as $value) {
167-
api_get_local_time();
151+
foreach ($queryResults as $value) {
168152
$results[] = [
169153
'id' => $value['exe']->getExeId(),
170154
'quiz_title' => $value['title'],
@@ -239,4 +223,71 @@ protected function createTable(array $tableData): HTML_Table
239223

240224
return $table;
241225
}
226+
227+
protected function findRandomResults(int $exerciseId): array
228+
{
229+
$percentage = (int) $this->plugin->get(\ExerciseFocusedPlugin::SETTING_PERCENTAGE_SAMPLING);
230+
231+
if (empty($percentage)) {
232+
return [];
233+
}
234+
235+
$cId = api_get_course_int_id();
236+
$sId = api_get_session_id();
237+
238+
$tblTrackExe = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
239+
240+
$sessionCondition = api_get_session_condition($sId);
241+
242+
$result = Database::query(
243+
"SELECT exe_id FROM $tblTrackExe
244+
WHERE c_id = $cId
245+
AND exe_exo_id = $exerciseId
246+
$sessionCondition
247+
ORDER BY RAND() LIMIT $percentage"
248+
);
249+
250+
$exeIdList = array_column(
251+
Database::store_result($result),
252+
'exe_id'
253+
);
254+
255+
$qb = $this->em->createQueryBuilder();
256+
$qb
257+
->select('te AS exe, q.title, te.startDate, u.firstname, u.lastname, u.username')
258+
->from(TrackEExercises::class, 'te')
259+
->innerJoin(CQuiz::class, 'q', Join::WITH, 'te.exeExoId = q.iid')
260+
->innerJoin(User::class, 'u', Join::WITH, 'te.exeUserId = u.id')
261+
->andWhere(
262+
$qb->expr()->in('te.exeId', $exeIdList)
263+
)
264+
->addOrderBy('te.startDate');
265+
266+
return $this->formatResults(
267+
$qb->getQuery()->getResult()
268+
);
269+
}
270+
271+
private function getSessionIdFromFormValues(array $formValues, array $fieldVariableList): array
272+
{
273+
$fieldItemIdList = [];
274+
$objFieldValue = new ExtraFieldValue('session');
275+
276+
foreach ($fieldVariableList as $fieldVariable) {
277+
if (!isset($formValues["extra_$fieldVariable"])) {
278+
continue;
279+
}
280+
281+
$itemValue = $objFieldValue->get_item_id_from_field_variable_and_field_value(
282+
$fieldVariable,
283+
$formValues["extra_$fieldVariable"]
284+
);
285+
286+
if ($itemValue) {
287+
$fieldItemIdList[] = (int) $itemValue['item_id'];
288+
}
289+
}
290+
291+
return array_unique($fieldItemIdList);
292+
}
242293
}

0 commit comments

Comments
 (0)