recalculate.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once __DIR__.'/../inc/global.inc.php';
  4. $isAllowedToEdit = api_is_allowed_to_edit(true, true);
  5. if (!$isAllowedToEdit) {
  6. api_not_allowed(true);
  7. exit;
  8. }
  9. if (!isset($_REQUEST['user'], $_REQUEST['exercise'], $_REQUEST['id'])) {
  10. api_not_allowed(true);
  11. exit;
  12. }
  13. $em = Database::getManager();
  14. $trackedExercise = $em
  15. ->getRepository('ChamiloCoreBundle:TrackEExercises')
  16. ->find(intval($_REQUEST['id']));
  17. if ($trackedExercise->getExeUserId() != intval($_REQUEST['user']) ||
  18. $trackedExercise->getExeExoId() != intval($_REQUEST['exercise'])
  19. ) {
  20. api_not_allowed(true);
  21. exit;
  22. }
  23. $attempts = $em->getRepository('ChamiloCoreBundle:TrackEAttempt')
  24. ->findBy([
  25. 'exeId' => $trackedExercise->getExeId(),
  26. 'userId' => $trackedExercise->getExeUserId()
  27. ]);
  28. $newResult = 0;
  29. /** @var \Chamilo\CoreBundle\Entity\TrackEAttempt $attempt */
  30. foreach ($attempts as $attempt) {
  31. $questionId = $attempt->getQuestionId();
  32. $question = $em->find('ChamiloCourseBundle:CQuizQuestion', $questionId);
  33. if (!$question) {
  34. continue;
  35. }
  36. $answers = $em->getRepository('ChamiloCourseBundle:CQuizAnswer')->findBy([
  37. 'questionId' => $questionId,
  38. 'correct' => 1
  39. ]);
  40. $newMarks = 0;
  41. foreach ($answers as $answer) {
  42. if ($answer->getId() != $attempt->getAnswer()) {
  43. continue;
  44. }
  45. $newMarks += $answer->getPonderation();
  46. }
  47. $newResult += $newMarks;
  48. $attempt->setMarks($newMarks);
  49. $em->merge($attempt);
  50. }
  51. $trackedExercise->setExeResult($newResult);
  52. $em->merge($trackedExercise);
  53. $em->flush();