generate.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\Course;
  4. use Chamilo\CoreBundle\Entity\Session;
  5. use Doctrine\Common\Collections\Criteria;
  6. use Chamilo\CoreBundle\Entity\SessionRelUser;
  7. use Chamilo\CoreBundle\Entity\CourseRelUser;
  8. use Chamilo\UserBundle\Entity\User;
  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('NotAllowed'));
  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. $dateStart = new DateTime($values['range_start'], new DateTimeZone('UTC'));
  80. $dateEnd = new DateTime($values['range_end'], new DateTimeZone('UTC'));
  81. $fileData = [];
  82. $fileData[] = sprintf(
  83. "1 %s %s%s",
  84. $fieldProvider ? $fieldProvider['value'] : null,
  85. $values['course'],
  86. $dateStart->format('m/d/Y')
  87. );
  88. /** @var User $student */
  89. foreach ($students as $student) {
  90. $userFinishedCourse = Category::userFinishedCourse(
  91. $student->getId(),
  92. $gradebook,
  93. true
  94. );
  95. if (!$userFinishedCourse) {
  96. continue;
  97. }
  98. $fieldStudent = $uFieldValue->get_values_by_handler_and_field_variable(
  99. $student->getId(),
  100. GradingElectronicPlugin::EXTRAFIELD_STUDENT_ID
  101. );
  102. $score = Category::getCurrentScore(
  103. $student->getId(),
  104. $gradebook,
  105. true
  106. );
  107. $fileData[] = sprintf(
  108. "2 %sPASS%s %s %s",
  109. $fieldStudent ? $fieldStudent['value'] : null,
  110. $fieldHours ? $fieldHours['value'] : null,
  111. $score,
  112. $dateEnd->format('m/d/Y')
  113. );
  114. if (!$gradebook->getGenerateCertificates()) {
  115. continue;
  116. }
  117. Category::generateUserCertificate(
  118. $gradebook->get_id(),
  119. $student->getId(),
  120. true
  121. );
  122. }
  123. $fileName = implode('_', [
  124. $gradingElectronic->get_title(),
  125. $values['course'],
  126. $values['range_start'],
  127. $values['range_end'],
  128. ]);
  129. $fileName = api_replace_dangerous_char($fileName).'.txt';
  130. $fileData[] = null;
  131. file_put_contents(
  132. api_get_path(SYS_ARCHIVE_PATH).$fileName,
  133. implode("\r\n", $fileData)
  134. );
  135. echo Display::toolbarButton(
  136. get_lang('Download'),
  137. api_get_path(WEB_ARCHIVE_PATH).$fileName,
  138. 'download',
  139. 'success',
  140. ['target' => '_blank', 'download' => $fileName]
  141. );
  142. } catch (Exception $e) {
  143. echo Display::return_message($e->getMessage(), 'error');
  144. }