Skip to content

Learnpath: Add recalculate score for LP item view with exercises - refs #2960 #5803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion public/main/exercise/recalculate_all.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
$result = ExerciseLib::get_exam_results_data(
0,
0,
1,
null,
'asc',
$exerciseId,
'',
Expand Down
62 changes: 62 additions & 0 deletions public/main/lp/learnpath.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\ResourceLink;
use Chamilo\CoreBundle\Entity\TrackEExercise;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Entity\Session as SessionEntity;
use Chamilo\CoreBundle\ServiceHelper\ThemeHelper;
Expand Down Expand Up @@ -8796,4 +8797,65 @@ private function getSavedFinalItem()

return $document ? $repo->getResourceFileContent($document) : '';
}

/**
* Recalculates the results for all exercises associated with the learning path (LP) for the given user.
*/
public function recalculateResultsForLp(int $userId): void
{
$em = Database::getManager();
$lpItemRepo = $em->getRepository(CLpItem::class);
$lpItems = $lpItemRepo->findBy(['lp' => $this->lp_id]);

if (empty($lpItems)) {
Display::addFlash(Display::return_message(get_lang('No items found'), 'error'));
return;
}

$lpItemIds = array_map(fn($item) => $item->getIid(), $lpItems);
$lpItemViewRepo = $em->getRepository(CLpItemView::class);
$lpItemViews = $lpItemViewRepo->createQueryBuilder('v')
->where('v.item IN (:lpItemIds)')
->setParameter('lpItemIds', $lpItemIds)
->getQuery()
->getResult();

if (empty($lpItemViews)) {
Display::addFlash(Display::return_message(get_lang('No item views found'), 'error'));
return;
}

$lpViewIds = array_map(fn($view) => $view->getIid(), $lpItemViews);
$trackEExerciseRepo = $em->getRepository(TrackEExercise::class);
$trackExercises = $trackEExerciseRepo->createQueryBuilder('te')
->where('te.origLpId = :lpId')
->andWhere('te.origLpItemId IN (:lpItemIds)')
->andWhere('te.origLpItemViewId IN (:lpViewIds)')
->andWhere('te.user = :userId')
->setParameter('lpId', $this->lp_id)
->setParameter('lpItemIds', $lpItemIds)
->setParameter('lpViewIds', $lpViewIds)
->setParameter('userId', $userId)
->getQuery()
->getResult();

if (empty($trackExercises)) {
Display::addFlash(Display::return_message(get_lang('No exercise attempts found'), 'error'));
return;
}

foreach ($trackExercises as $trackExercise) {
$exeId = $trackExercise->getExeId();
$exerciseId = $trackExercise->getQuiz()->getIid();
$courseId = $trackExercise->getCourse()->getId();

$result = ExerciseLib::recalculateResult($exeId, $userId, $exerciseId, $courseId);

if ($result) {
Display::addFlash(Display::return_message(get_lang('Results recalculated'), 'success'));
} else {
Display::addFlash(Display::return_message(get_lang('Error recalculating results'), 'error'));
}
}
}
}
20 changes: 20 additions & 0 deletions public/main/lp/lp_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,26 @@
$redirectTo = '';

switch ($action) {
case 'recalculate':
if (!isset($oLP) || !$lp_found) {
Display::addFlash(Display::return_message(get_lang('NoLpFound'), 'error'));
header("Location: $listUrl");
exit;
}

$userId = isset($_GET['user_id']) ? (int) $_GET['user_id'] : 0;

if (0 === $userId) {
Display::addFlash(Display::return_message(get_lang('NoUserIdProvided'), 'error'));
header("Location: $listUrl");
exit;
}

$oLP->recalculateResultsForLp($userId);

$url = api_get_self().'?action=report&lp_id='.$lpId.'&'.api_get_cidreq();
header("Location: $url");
exit;
case 'author_view':
$teachers = [];
$field = new ExtraField('user');
Expand Down
6 changes: 6 additions & 0 deletions public/main/lp/lp_report.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@
['data-id' => $userId, 'data-username' => $userInfo['username'], 'class' => 'delete_attempt']
);

$actions .= Display::url(
Display::getMdiIcon('file-document-refresh', 'ch-tool-icon', null, 32, get_lang('Recalculate result')),
api_get_path(WEB_CODE_PATH) . 'lp/lp_controller.php?'.api_get_cidreq().'&action=recalculate&user_id='.$userId.'&lp_id='.$lpId,
['title' => get_lang('Recalculate result')]
);

$row[] = $actions;
$row['username'] = $userInfo['username'];
$userList[] = $row;
Expand Down
5 changes: 5 additions & 0 deletions src/CourseBundle/Entity/CLpView.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class CLpView
#[ORM\Column(name: 'progress', type: 'integer', nullable: true)]
protected ?int $progress = null;

public function getIid(): ?int
{
return $this->iid;
}

public function setViewCount(int $viewCount): self
{
$this->viewCount = $viewCount;
Expand Down
Loading