Fix recalculate exercise in order to get the correct result BT#14870

pull/2729/head
Julio Montoya 7 years ago
parent acd5199c7a
commit 592ec636bb
  1. 14
      main/exercise/exercise.class.php
  2. 115
      main/exercise/recalculate.php

@ -3318,6 +3318,7 @@ class Exercise
* @param int $propagate_neg * @param int $propagate_neg
* @param array $hotspot_delineation_result * @param array $hotspot_delineation_result
* @param bool $showTotalScoreAndUserChoicesInLastAttempt * @param bool $showTotalScoreAndUserChoicesInLastAttempt
* @param bool $updateResults
* *
* @todo reduce parameters of this function * @todo reduce parameters of this function
* *
@ -3334,7 +3335,8 @@ class Exercise
$show_result = true, $show_result = true,
$propagate_neg = 0, $propagate_neg = 0,
$hotspot_delineation_result = [], $hotspot_delineation_result = [],
$showTotalScoreAndUserChoicesInLastAttempt = true $showTotalScoreAndUserChoicesInLastAttempt = true,
$updateResults = false
) { ) {
$debug = false; $debug = false;
//needed in order to use in the exercise_attempt() for the time //needed in order to use in the exercise_attempt() for the time
@ -3425,8 +3427,8 @@ class Exercise
if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) { if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
$choiceTmp = $choice; $choiceTmp = $choice;
$choice = $choiceTmp["choice"]; $choice = isset($choiceTmp['choice']) ? $choiceTmp['choice'] : '';
$choiceDegreeCertainty = $choiceTmp["choiceDegreeCertainty"]; $choiceDegreeCertainty = isset($choiceTmp['choiceDegreeCertainty']) ? $choiceTmp['choiceDegreeCertainty'] : '';
} }
if ($answerType == FREE_ANSWER || if ($answerType == FREE_ANSWER ||
@ -5654,7 +5656,8 @@ class Exercise
$quesId, $quesId,
$exeId, $exeId,
$i, $i,
$this->id $this->id,
$updateResults
); );
} }
} else { } else {
@ -5664,7 +5667,8 @@ class Exercise
$quesId, $quesId,
$exeId, $exeId,
$i, $i,
$this->id $this->id,
$updateResults
); );
} }
if ($debug) { if ($debug) {

@ -1,69 +1,106 @@
<?php <?php
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Entity\TrackEExercises;
require_once __DIR__.'/../inc/global.inc.php'; require_once __DIR__.'/../inc/global.inc.php';
$isAllowedToEdit = api_is_allowed_to_edit(true, true); $isAllowedToEdit = api_is_allowed_to_edit(true, true);
if (!$isAllowedToEdit) { if (!$isAllowedToEdit) {
api_not_allowed(true);
exit; exit;
} }
if (!isset($_REQUEST['user'], $_REQUEST['exercise'], $_REQUEST['id'])) { if (!isset($_REQUEST['user'], $_REQUEST['exercise'], $_REQUEST['id'])) {
api_not_allowed(true);
exit; exit;
} }
$courseId = api_get_course_int_id();
$sessionId = api_get_session_id();
$em = Database::getManager(); $em = Database::getManager();
$trackedExercise = $em /** @var TrackEExercises $trackedExercise */
->getRepository('ChamiloCoreBundle:TrackEExercises') $trackedExercise = $em->getRepository('ChamiloCoreBundle:TrackEExercises')->find($_REQUEST['id']);
->find(intval($_REQUEST['id']));
if ($trackedExercise->getExeUserId() != intval($_REQUEST['user']) || if (empty($trackedExercise)) {
$trackedExercise->getExeExoId() != intval($_REQUEST['exercise'])
) {
api_not_allowed(true);
exit; exit;
} }
$attempts = $em->getRepository('ChamiloCoreBundle:TrackEAttempt') $studentId = $trackedExercise->getExeUserId();
->findBy([ $exerciseId = $trackedExercise->getExeExoId();
'exeId' => $trackedExercise->getExeId(), $exeId = $trackedExercise->getExeId();
'userId' => $trackedExercise->getExeUserId(),
]);
$newResult = 0; if ($studentId != intval($_REQUEST['user']) ||
/** @var \Chamilo\CoreBundle\Entity\TrackEAttempt $attempt */ $exerciseId != intval($_REQUEST['exercise'])
foreach ($attempts as $attempt) { ) {
$questionId = $attempt->getQuestionId(); exit;
}
$question = $em->find('ChamiloCourseBundle:CQuizQuestion', $questionId);
if (!$question) { $questionList = $trackedExercise->getDataTracking();
continue;
}
$answers = $em->getRepository('ChamiloCourseBundle:CQuizAnswer')->findBy([ if (empty($questionList)) {
'questionId' => $questionId, exit;
'correct' => 1, }
]);
$newMarks = 0; $questionList = explode(',', $questionList);
foreach ($answers as $answer) {
if ($answer->getId() != $attempt->getAnswer()) { $exercise = new Exercise($courseId);
continue; $exercise->read($exerciseId);
} $totalScore = 0;
$newMarks += $answer->getPonderation(); $totalWeight = 0;
foreach ($questionList as $questionId) {
$question = Question::read($questionId, $courseId);
$totalWeight += $question->selectWeighting();
// We're inside *one* question. Go through each possible answer for this question
if ($question->type === MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
$result = $exercise->manage_answer(
$exeId,
$questionId,
[],
'exercise_result',
[],
false,
true,
false,
$exercise->selectPropagateNeg(),
[],
[],
true
);
} else {
$result = $exercise->manage_answer(
$exeId,
$questionId,
[],
'exercise_result',
[],
false,
true,
false,
$exercise->selectPropagateNeg(),
[],
[],
true
);
} }
$newResult += $newMarks; // Adding the new score.
$attempt->setMarks($newMarks); $totalScore += $result['score'];
$em->merge($attempt);
} }
$trackedExercise->setExeResult($newResult); $remindList = $trackedExercise->getQuestionsToCheck();
if (!empty($remindList)) {
$remindList = explode(',', $remindList);
}
$table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
$sql = "UPDATE $table SET
exe_result = '$totalScore',
exe_weighting = '$totalWeight'
WHERE exe_id = $exeId";
Database::query($sql);
$em->merge($trackedExercise); echo $totalScore.'/'.$totalWeight;
$em->flush();
Loading…
Cancel
Save