123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- /* For licensing terms, see /license.txt */
- use Chamilo\CoreBundle\Entity\Course;
- use Chamilo\CoreBundle\Entity\CourseRelUser;
- use Chamilo\CoreBundle\Entity\Session;
- use Chamilo\CoreBundle\Entity\SessionRelUser;
- use Chamilo\UserBundle\Entity\User;
- use Doctrine\Common\Collections\Criteria;
- require_once '../../main/inc/global.inc.php';
- $allowed = api_is_teacher() || api_is_platform_admin() || api_is_course_tutor();
- $gradingElectronic = GradingElectronicPlugin::create();
- try {
- if (!$allowed) {
- throw new Exception(get_lang('NotAllowed'));
- }
- $toolIsEnabled = $gradingElectronic->get('tool_enable') === 'true';
- if (!$toolIsEnabled) {
- throw new Exception($gradingElectronic->get_lang('PluginDisabled'));
- }
- $form = $gradingElectronic->getForm();
- if (!$form->validate()) {
- throw new Exception(
- implode('<br>', $form->_errors)
- );
- }
- $em = Database::getManager();
- /** @var Course $course */
- $course = $em->find('ChamiloCoreBundle:Course', api_get_course_int_id());
- /** @var Session $session */
- $session = $em->find('ChamiloCoreBundle:Session', api_get_session_id());
- $values = $form->exportValues();
- $cFieldValue = new ExtraFieldValue('course');
- $uFieldValue = new ExtraFieldValue('user');
- $cFieldValue->save([
- 'variable' => GradingElectronicPlugin::EXTRAFIELD_COURSE_ID,
- 'item_id' => $course->getId(),
- 'value' => $values['course'],
- ]);
- $item = $cFieldValue->get_item_id_from_field_variable_and_field_value(
- GradingElectronicPlugin::EXTRAFIELD_COURSE_ID,
- $values['course']
- );
- $fieldProvider = $cFieldValue->get_values_by_handler_and_field_variable(
- $course->getId(),
- GradingElectronicPlugin::EXTRAFIELD_COURSE_PROVIDER_ID
- );
- $fieldHours = $cFieldValue->get_values_by_handler_and_field_variable(
- $course->getId(),
- GradingElectronicPlugin::EXTRAFIELD_COURSE_HOURS
- );
- $students = [];
- if ($session) {
- $criteria = Criteria::create()->where(
- Criteria::expr()->eq('relationType', Session::STUDENT)
- );
- $subscriptions = $session->getUsers()->matching($criteria);
- /** @var SessionRelUser $subscription */
- foreach ($subscriptions as $subscription) {
- $students[] = $subscription->getUser();
- }
- } else {
- $subscriptions = $course->getStudents();
- /** @var CourseRelUser $subscription */
- foreach ($subscriptions as $subscription) {
- $students[] = $subscription->getUser();
- }
- }
- $cats = Category::load(
- null,
- null,
- $course->getCode(),
- null,
- null,
- $session ? $session->getId() : 0,
- 'ORDER By id'
- );
- /** @var \Category $gradebook */
- $gradebook = $cats[0];
- /** @var \ExerciseLink $exerciseLink */
- /** commented until we get clear understanding of how to use the dates refs BT#12404
- $exerciseLink = $gradebook->get_links()[0];
- $exerciseId = $exerciseLink->get_ref_id();
- $exerciseInfo = ExerciseLib::get_exercise_by_id($exerciseId, $course->getId());
- */
- $dateStart = new DateTime($values['range_start'].' 00:00:00', new DateTimeZone('UTC'));
- $dateEnd = new DateTime($values['range_end'].' 23:59:59', new DateTimeZone('UTC'));
- $fileData = [];
- $fileData[] = sprintf(
- "1 %s %s%s",
- $fieldProvider ? $fieldProvider['value'] : null,
- $values['course'],
- $dateStart->format('m/d/Y')
- );
- /** @var User $student */
- foreach ($students as $student) {
- $userFinishedCourse = Category::userFinishedCourse(
- $student->getId(),
- $gradebook,
- true
- );
- if (!$userFinishedCourse) {
- continue;
- }
- /** commented until we get clear understanding of how to use the dates refs BT#12404
- $exerciseResult = Event::get_best_exercise_results_by_user(
- $exerciseId,
- $course->getId(),
- $session ? $session->getId() : 0,
- $student->getId()
- );
- $exerciseResult = current($exerciseResult);
- if (!$exerciseResult) {
- continue;
- }
- $attemptDate = new DateTime($exerciseResult['exe_date'], new DateTimeZone('UTC'));
- $dateIsRange = $attemptDate >= $dateStart && $attemptDate <= $dateEnd;
- if (!$dateEnd) {
- continue;
- }
- */
- $fieldStudent = $uFieldValue->get_values_by_handler_and_field_variable(
- $student->getId(),
- GradingElectronicPlugin::EXTRAFIELD_STUDENT_ID
- );
- $scoretotal = $gradebook->calc_score($student->getId());
- $scoredisplay = ScoreDisplay::instance();
- $score = $scoredisplay->display_score(
- $scoretotal,
- SCORE_SIMPLE
- );
- /** old method to get the score
- $score = Category::getCurrentScore(
- $student->getId(),
- $gradebook,
- true
- );
- */
- $fileData[] = sprintf(
- "2 %sPASS%s %s %s",
- $fieldStudent ? $fieldStudent['value'] : null,
- $fieldHours ? $fieldHours['value'] : null,
- $score,
- $dateEnd->format('m/d/Y')
- );
- if (!$gradebook->getGenerateCertificates()) {
- continue;
- }
- Category::generateUserCertificate(
- $gradebook->get_id(),
- $student->getId(),
- true
- );
- }
- $fileName = implode('_', [
- $gradingElectronic->get_title(),
- $values['course'],
- $values['range_start'],
- $values['range_end'],
- ]);
- $fileName = api_replace_dangerous_char($fileName).'.txt';
- $fileData[] = null;
- file_put_contents(
- api_get_path(SYS_ARCHIVE_PATH).$fileName,
- implode("\r\n", $fileData)
- );
- echo Display::toolbarButton(
- get_lang('Download'),
- api_get_path(WEB_ARCHIVE_PATH).$fileName,
- 'download',
- 'success',
- ['target' => '_blank', 'download' => $fileName]
- );
- } catch (Exception $e) {
- echo Display::return_message($e->getMessage(), 'error');
- }
|