recalculate.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 (
  18. $trackedExercise->getExeUserId() != intval($_REQUEST['user']) ||
  19. $trackedExercise->getExeExoId() != intval($_REQUEST['exercise'])
  20. ) {
  21. api_not_allowed(true);
  22. exit;
  23. }
  24. $attemps = $em->getRepository('ChamiloCoreBundle:TrackEAttempt')
  25. ->findBy([
  26. 'exeId' => $trackedExercise->getExeId(),
  27. 'userId' => $trackedExercise->getExeUserId()
  28. ]);
  29. $newResult = 0;
  30. foreach ($attemps as $attemp) {
  31. $questionId = $attemp->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() != $attemp->getAnswer()) {
  43. continue;
  44. }
  45. $newMarks += $answer->getPonderation();
  46. }
  47. $newResult += $newMarks;
  48. $attemp->setMarks($newMarks);
  49. $em->merge($attemp);
  50. }
  51. $trackedExercise->setExeResult($newResult);
  52. $em->merge($trackedExercise);
  53. $em->flush();