generate.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\Course;
  4. use Chamilo\CoreBundle\Entity\CourseRelUser;
  5. use Chamilo\CoreBundle\Entity\Session;
  6. use Chamilo\CoreBundle\Entity\SessionRelUser;
  7. use Chamilo\UserBundle\Entity\User;
  8. use Doctrine\Common\Collections\Criteria;
  9. require_once '../../main/inc/global.inc.php';
  10. $allowed = api_is_teacher() || api_is_platform_admin() || api_is_course_tutor();
  11. $gradingElectronic = GradingElectronicPlugin::create();
  12. try {
  13. if (!$allowed) {
  14. throw new Exception(get_lang('You are not allowed to see this page. Either your connection has expired or you are trying to access a page for which you do not have the sufficient privileges.'));
  15. }
  16. $toolIsEnabled = $gradingElectronic->get('tool_enable') === 'true';
  17. if (!$toolIsEnabled) {
  18. throw new Exception($gradingElectronic->get_lang('PluginDisabled'));
  19. }
  20. $form = $gradingElectronic->getForm();
  21. if (!$form->validate()) {
  22. throw new Exception(
  23. implode('<br>', $form->_errors)
  24. );
  25. }
  26. $em = Database::getManager();
  27. /** @var Course $course */
  28. $course = $em->find('ChamiloCoreBundle:Course', api_get_course_int_id());
  29. /** @var Session $session */
  30. $session = $em->find('ChamiloCoreBundle:Session', api_get_session_id());
  31. $values = $form->exportValues();
  32. $cFieldValue = new ExtraFieldValue('course');
  33. $uFieldValue = new ExtraFieldValue('user');
  34. $cFieldValue->save([
  35. 'variable' => GradingElectronicPlugin::EXTRAFIELD_COURSE_ID,
  36. 'item_id' => $course->getId(),
  37. 'value' => $values['course'],
  38. ]);
  39. $item = $cFieldValue->get_item_id_from_field_variable_and_field_value(
  40. GradingElectronicPlugin::EXTRAFIELD_COURSE_ID,
  41. $values['course']
  42. );
  43. $fieldProvider = $cFieldValue->get_values_by_handler_and_field_variable(
  44. $course->getId(),
  45. GradingElectronicPlugin::EXTRAFIELD_COURSE_PROVIDER_ID
  46. );
  47. $fieldHours = $cFieldValue->get_values_by_handler_and_field_variable(
  48. $course->getId(),
  49. GradingElectronicPlugin::EXTRAFIELD_COURSE_HOURS
  50. );
  51. $students = [];
  52. if ($session) {
  53. $criteria = Criteria::create()->where(
  54. Criteria::expr()->eq('relationType', Session::STUDENT)
  55. );
  56. $subscriptions = $session->getUsers()->matching($criteria);
  57. /** @var SessionRelUser $subscription */
  58. foreach ($subscriptions as $subscription) {
  59. $students[] = $subscription->getUser();
  60. }
  61. } else {
  62. $subscriptions = $course->getStudents();
  63. /** @var CourseRelUser $subscription */
  64. foreach ($subscriptions as $subscription) {
  65. $students[] = $subscription->getUser();
  66. }
  67. }
  68. $cats = Category::load(
  69. null,
  70. null,
  71. $course->getCode(),
  72. null,
  73. null,
  74. $session ? $session->getId() : 0,
  75. 'ORDER By id'
  76. );
  77. /** @var \Category $gradebook */
  78. $gradebook = $cats[0];
  79. /** @var \ExerciseLink $exerciseLink */
  80. /** commented until we get clear understanding of how to use the dates refs BT#12404
  81. $exerciseLink = $gradebook->get_links()[0];
  82. $exerciseId = $exerciseLink->get_ref_id();
  83. $exerciseInfo = ExerciseLib::get_exercise_by_id($exerciseId, $course->getId());
  84. */
  85. $dateStart = new DateTime($values['range_start'].' 00:00:00', new DateTimeZone('UTC'));
  86. $dateEnd = new DateTime($values['range_end'].' 23:59:59', new DateTimeZone('UTC'));
  87. $fileData = [];
  88. $fileData[] = sprintf(
  89. '1 %s %s%s',
  90. $fieldProvider ? $fieldProvider['value'] : null,
  91. $values['course'],
  92. $dateStart->format('m/d/Y')
  93. );
  94. /** @var User $student */
  95. foreach ($students as $student) {
  96. $userFinishedCourse = Category::userFinishedCourse(
  97. $student->getId(),
  98. $gradebook,
  99. true
  100. );
  101. if (!$userFinishedCourse) {
  102. continue;
  103. }
  104. /** commented until we get clear understanding of how to use the dates refs BT#12404
  105. $exerciseResult = Event::get_best_exercise_results_by_user(
  106. $exerciseId,
  107. $course->getId(),
  108. $session ? $session->getId() : 0,
  109. $student->getId()
  110. );
  111. $exerciseResult = current($exerciseResult);
  112. if (!$exerciseResult) {
  113. continue;
  114. }
  115. $attemptDate = new DateTime($exerciseResult['exe_date'], new DateTimeZone('UTC'));
  116. $dateIsRange = $attemptDate >= $dateStart && $attemptDate <= $dateEnd;
  117. if (!$dateEnd) {
  118. continue;
  119. }
  120. */
  121. $fieldStudent = $uFieldValue->get_values_by_handler_and_field_variable(
  122. $student->getId(),
  123. GradingElectronicPlugin::EXTRAFIELD_STUDENT_ID
  124. );
  125. $scoretotal = $gradebook->calc_score($student->getId());
  126. $scoredisplay = ScoreDisplay::instance();
  127. $score = $scoredisplay->display_score(
  128. $scoretotal,
  129. SCORE_SIMPLE
  130. );
  131. /** old method to get the score
  132. $score = Category::getCurrentScore(
  133. $student->getId(),
  134. $gradebook,
  135. true
  136. );
  137. */
  138. $fileData[] = sprintf(
  139. "2 %sPASS%s %s %s",
  140. $fieldStudent ? $fieldStudent['value'] : null,
  141. $fieldHours ? $fieldHours['value'] : null,
  142. $score,
  143. $dateEnd->format('m/d/Y')
  144. );
  145. if (!$gradebook->getGenerateCertificates()) {
  146. continue;
  147. }
  148. Category::generateUserCertificate(
  149. $gradebook->get_id(),
  150. $student->getId(),
  151. true
  152. );
  153. }
  154. $fileName = implode('_', [
  155. $gradingElectronic->get_title(),
  156. $values['course'],
  157. $values['range_start'],
  158. $values['range_end'],
  159. ]);
  160. $fileName = api_replace_dangerous_char($fileName).'.txt';
  161. $fileData[] = null;
  162. file_put_contents(
  163. api_get_path(SYS_ARCHIVE_PATH).$fileName,
  164. implode("\r\n", $fileData)
  165. );
  166. echo Display::toolbarButton(
  167. get_lang('Download'),
  168. api_get_path(WEB_ARCHIVE_PATH).$fileName,
  169. 'download',
  170. 'success',
  171. ['target' => '_blank', 'download' => $fileName]
  172. );
  173. } catch (Exception $e) {
  174. echo Display::return_message($e->getMessage(), 'error');
  175. }