Skip to content

Commit b628d55

Browse files
Merge pull request #5803 from christianbeeznest/GH-2960
Learnpath: Add recalculate score for LP item view with exercises - refs #2960
2 parents fd44325 + 6ef3744 commit b628d55

File tree

5 files changed

+94
-1
lines changed

5 files changed

+94
-1
lines changed

public/main/exercise/recalculate_all.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
$result = ExerciseLib::get_exam_results_data(
2727
0,
2828
0,
29-
1,
29+
null,
3030
'asc',
3131
$exerciseId,
3232
'',

public/main/lp/learnpath.class.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Chamilo\CoreBundle\Entity\Course;
66
use Chamilo\CoreBundle\Entity\ResourceLink;
7+
use Chamilo\CoreBundle\Entity\TrackEExercise;
78
use Chamilo\CoreBundle\Entity\User;
89
use Chamilo\CoreBundle\Entity\Session as SessionEntity;
910
use Chamilo\CoreBundle\ServiceHelper\ThemeHelper;
@@ -8796,4 +8797,65 @@ private function getSavedFinalItem()
87968797

87978798
return $document ? $repo->getResourceFileContent($document) : '';
87988799
}
8800+
8801+
/**
8802+
* Recalculates the results for all exercises associated with the learning path (LP) for the given user.
8803+
*/
8804+
public function recalculateResultsForLp(int $userId): void
8805+
{
8806+
$em = Database::getManager();
8807+
$lpItemRepo = $em->getRepository(CLpItem::class);
8808+
$lpItems = $lpItemRepo->findBy(['lp' => $this->lp_id]);
8809+
8810+
if (empty($lpItems)) {
8811+
Display::addFlash(Display::return_message(get_lang('No items found'), 'error'));
8812+
return;
8813+
}
8814+
8815+
$lpItemIds = array_map(fn($item) => $item->getIid(), $lpItems);
8816+
$lpItemViewRepo = $em->getRepository(CLpItemView::class);
8817+
$lpItemViews = $lpItemViewRepo->createQueryBuilder('v')
8818+
->where('v.item IN (:lpItemIds)')
8819+
->setParameter('lpItemIds', $lpItemIds)
8820+
->getQuery()
8821+
->getResult();
8822+
8823+
if (empty($lpItemViews)) {
8824+
Display::addFlash(Display::return_message(get_lang('No item views found'), 'error'));
8825+
return;
8826+
}
8827+
8828+
$lpViewIds = array_map(fn($view) => $view->getIid(), $lpItemViews);
8829+
$trackEExerciseRepo = $em->getRepository(TrackEExercise::class);
8830+
$trackExercises = $trackEExerciseRepo->createQueryBuilder('te')
8831+
->where('te.origLpId = :lpId')
8832+
->andWhere('te.origLpItemId IN (:lpItemIds)')
8833+
->andWhere('te.origLpItemViewId IN (:lpViewIds)')
8834+
->andWhere('te.user = :userId')
8835+
->setParameter('lpId', $this->lp_id)
8836+
->setParameter('lpItemIds', $lpItemIds)
8837+
->setParameter('lpViewIds', $lpViewIds)
8838+
->setParameter('userId', $userId)
8839+
->getQuery()
8840+
->getResult();
8841+
8842+
if (empty($trackExercises)) {
8843+
Display::addFlash(Display::return_message(get_lang('No exercise attempts found'), 'error'));
8844+
return;
8845+
}
8846+
8847+
foreach ($trackExercises as $trackExercise) {
8848+
$exeId = $trackExercise->getExeId();
8849+
$exerciseId = $trackExercise->getQuiz()->getIid();
8850+
$courseId = $trackExercise->getCourse()->getId();
8851+
8852+
$result = ExerciseLib::recalculateResult($exeId, $userId, $exerciseId, $courseId);
8853+
8854+
if ($result) {
8855+
Display::addFlash(Display::return_message(get_lang('Results recalculated'), 'success'));
8856+
} else {
8857+
Display::addFlash(Display::return_message(get_lang('Error recalculating results'), 'error'));
8858+
}
8859+
}
8860+
}
87998861
}

public/main/lp/lp_controller.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,26 @@
203203
$redirectTo = '';
204204

205205
switch ($action) {
206+
case 'recalculate':
207+
if (!isset($oLP) || !$lp_found) {
208+
Display::addFlash(Display::return_message(get_lang('NoLpFound'), 'error'));
209+
header("Location: $listUrl");
210+
exit;
211+
}
212+
213+
$userId = isset($_GET['user_id']) ? (int) $_GET['user_id'] : 0;
214+
215+
if (0 === $userId) {
216+
Display::addFlash(Display::return_message(get_lang('NoUserIdProvided'), 'error'));
217+
header("Location: $listUrl");
218+
exit;
219+
}
220+
221+
$oLP->recalculateResultsForLp($userId);
222+
223+
$url = api_get_self().'?action=report&lp_id='.$lpId.'&'.api_get_cidreq();
224+
header("Location: $url");
225+
exit;
206226
case 'author_view':
207227
$teachers = [];
208228
$field = new ExtraField('user');

public/main/lp/lp_report.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,12 @@
359359
['data-id' => $userId, 'data-username' => $userInfo['username'], 'class' => 'delete_attempt']
360360
);
361361

362+
$actions .= Display::url(
363+
Display::getMdiIcon('file-document-refresh', 'ch-tool-icon', null, 32, get_lang('Recalculate result')),
364+
api_get_path(WEB_CODE_PATH) . 'lp/lp_controller.php?'.api_get_cidreq().'&action=recalculate&user_id='.$userId.'&lp_id='.$lpId,
365+
['title' => get_lang('Recalculate result')]
366+
);
367+
362368
$row[] = $actions;
363369
$row['username'] = $userInfo['username'];
364370
$userList[] = $row;

src/CourseBundle/Entity/CLpView.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ class CLpView
5151
#[ORM\Column(name: 'progress', type: 'integer', nullable: true)]
5252
protected ?int $progress = null;
5353

54+
public function getIid(): ?int
55+
{
56+
return $this->iid;
57+
}
58+
5459
public function setViewCount(int $viewCount): self
5560
{
5661
$this->viewCount = $viewCount;

0 commit comments

Comments
 (0)