recalculate.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once '../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();