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