certificate_report.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * List all certificates filtered by session/course and month/year.
  6. *
  7. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  8. *
  9. * @package chamilo.gradebook
  10. */
  11. $cidReset = true;
  12. require_once __DIR__.'/../inc/global.inc.php';
  13. api_block_anonymous_users();
  14. $is_allowedToTrack = api_is_platform_admin(true) || api_is_student_boss();
  15. if (!$is_allowedToTrack) {
  16. api_not_allowed(true);
  17. }
  18. $this_section = SECTION_TRACKING;
  19. $interbreadcrumb[] = [
  20. "url" => api_is_student_boss() ? "#" : api_get_path(WEB_CODE_PATH)."mySpace/index.php?".api_get_cidreq(),
  21. "name" => get_lang("MySpace"),
  22. ];
  23. $selectedSession = isset($_POST['session']) && !empty($_POST['session']) ? intval($_POST['session']) : 0;
  24. $selectedCourse = isset($_POST['course']) && !empty($_POST['course']) ? intval($_POST['course']) : 0;
  25. $selectedMonth = isset($_POST['month']) && !empty($_POST['month']) ? intval($_POST['month']) : 0;
  26. $selectedYear = isset($_POST['year']) && !empty($_POST['year']) ? trim($_POST['year']) : null;
  27. $selectedStudent = isset($_POST['student']) && !empty($_POST['student']) ? intval($_POST['student']) : 0;
  28. $userId = api_get_user_id();
  29. $sessions = $courses = $months = $students = [0 => get_lang('Select')];
  30. $userList = [];
  31. if (api_is_student_boss()) {
  32. $userGroup = new UserGroup();
  33. $userList = $userGroup->getGroupUsersByUser($userId);
  34. $sessionsList = SessionManager::getSessionsFollowedForGroupAdmin($userId);
  35. } else {
  36. $sessionsList = SessionManager::getSessionsCoachedByUser(
  37. $userId,
  38. false,
  39. api_is_platform_admin()
  40. );
  41. }
  42. foreach ($sessionsList as $session) {
  43. $sessions[$session['id']] = $session['name'];
  44. }
  45. if ($selectedSession > 0) {
  46. if (!SessionManager::isValidId($selectedSession)) {
  47. Display::addFlash(Display::return_message(get_lang('NoSession')));
  48. header("Location: $selfUrl");
  49. exit;
  50. }
  51. $coursesList = SessionManager::get_course_list_by_session_id($selectedSession);
  52. if (is_array($coursesList)) {
  53. foreach ($coursesList as &$course) {
  54. $course['real_id'] = $course['id'];
  55. }
  56. }
  57. } else {
  58. if (api_is_student_boss()) {
  59. $coursesList = CourseManager::getCoursesFollowedByGroupAdmin($userId);
  60. } else {
  61. $coursesList = CourseManager::get_courses_list_by_user_id(
  62. $userId,
  63. false,
  64. true
  65. );
  66. if (is_array($coursesList)) {
  67. foreach ($coursesList as &$course) {
  68. $courseInfo = api_get_course_info_by_id($course['real_id']);
  69. $course = array_merge($course, $courseInfo);
  70. }
  71. }
  72. }
  73. }
  74. foreach ($coursesList as $course) {
  75. if (isset($course['real_id'])) {
  76. $courses[$course['real_id']] = $course['title'];
  77. } else {
  78. $courses[$course['id']] = $course['title'];
  79. }
  80. }
  81. for ($key = 1; $key <= 12; $key++) {
  82. $months[$key] = sprintf("%02d", $key);
  83. }
  84. $exportAllLink = null;
  85. $certificateStudents = [];
  86. $searchSessionAndCourse = $selectedSession > 0 && $selectedCourse > 0;
  87. $searchCourseOnly = $selectedSession <= 0 && $selectedCourse > 0;
  88. $searchStudentOnly = $selectedStudent > 0;
  89. if ($searchSessionAndCourse || $searchCourseOnly) {
  90. $selectedCourseInfo = api_get_course_info_by_id($selectedCourse);
  91. if (empty($selectedCourseInfo)) {
  92. Display::addFlash(Display::return_message(get_lang('NoCourse')));
  93. header("Location: $selfUrl");
  94. exit;
  95. }
  96. $gradebookCategories = Category::load(
  97. null,
  98. null,
  99. $selectedCourseInfo['code'],
  100. null,
  101. false,
  102. $selectedSession
  103. );
  104. $gradebook = null;
  105. if (!empty($gradebookCategories)) {
  106. $gradebook = current($gradebookCategories);
  107. }
  108. if (!is_null($gradebook)) {
  109. $exportAllLink = api_get_path(WEB_CODE_PATH)."gradebook/gradebook_display_certificate.php?";
  110. $exportAllLink .= http_build_query([
  111. "action" => "export_all_certificates",
  112. "cidReq" => $selectedCourseInfo['code'],
  113. "id_session" => 0,
  114. "gidReq" => 0,
  115. "cat_id" => $gradebook->get_id(),
  116. ]);
  117. $sessionName = api_get_session_name($selectedSession);
  118. $courseName = api_get_course_info($selectedCourseInfo['code'])['title'];
  119. $studentList = GradebookUtils::get_list_users_certificates($gradebook->get_id());
  120. $certificateStudents = [];
  121. if (is_array($studentList) && !empty($studentList)) {
  122. foreach ($studentList as $student) {
  123. if (api_is_student_boss() && !in_array($student['user_id'], $userList)) {
  124. continue;
  125. }
  126. $certificateStudent = [
  127. 'fullName' => api_get_person_name($student['firstname'], $student['lastname']),
  128. 'sessionName' => $sessionName,
  129. 'courseName' => $courseName,
  130. 'certificates' => [],
  131. ];
  132. $studentCertificates = GradebookUtils::get_list_gradebook_certificates_by_user_id(
  133. $student['user_id'],
  134. $gradebook->get_id()
  135. );
  136. if (!is_array($studentCertificates) || empty($studentCertificates)) {
  137. continue;
  138. }
  139. foreach ($studentCertificates as $certificate) {
  140. $creationDate = new DateTime($certificate['created_at']);
  141. $creationMonth = $creationDate->format('m');
  142. $creationYear = $creationDate->format('Y');
  143. $creationMonthYear = $creationDate->format('m Y');
  144. if ($selectedMonth > 0 && empty($selectedYear)) {
  145. if ($creationMonth != $selectedMonth) {
  146. continue;
  147. }
  148. } elseif ($selectedMonth <= 0 && !empty($selectedYear)) {
  149. if ($creationYear != $selectedYear) {
  150. continue;
  151. }
  152. } elseif ($selectedMonth > 0 && !empty($selectedYear)) {
  153. if ($creationMonthYear != sprintf("%02d %s", $selectedMonth, $selectedYear)) {
  154. continue;
  155. }
  156. }
  157. $certificateStudent['certificates'][] = [
  158. 'createdAt' => api_convert_and_format_date($certificate['created_at']),
  159. 'id' => $certificate['id'],
  160. ];
  161. }
  162. if (count($certificateStudent['certificates']) > 0) {
  163. $certificateStudents[] = $certificateStudent;
  164. }
  165. }
  166. }
  167. }
  168. } elseif ($searchStudentOnly) {
  169. $selectedStudentInfo = api_get_user_info($selectedStudent);
  170. if (empty($selectedStudentInfo)) {
  171. Display::addFlash(Display::return_message(get_lang('NoUser')));
  172. header('Location: '.$selfUrl);
  173. exit;
  174. }
  175. $sessionList = SessionManager::getSessionsFollowedByUser($selectedStudent);
  176. foreach ($sessionList as $session) {
  177. $sessionCourseList = SessionManager::get_course_list_by_session_id($session['id']);
  178. foreach ($sessionCourseList as $sessionCourse) {
  179. $gradebookCategories = Category::load(
  180. null,
  181. null,
  182. $sessionCourse['code'],
  183. null,
  184. false,
  185. $session['id']
  186. );
  187. $gradebook = null;
  188. if (!empty($gradebookCategories)) {
  189. $gradebook = current($gradebookCategories);
  190. }
  191. if (!is_null($gradebook)) {
  192. $sessionName = $session['name'];
  193. $courseName = $sessionCourse['title'];
  194. $certificateStudent = [
  195. 'fullName' => $selectedStudentInfo['complete_name'],
  196. 'sessionName' => $sessionName,
  197. 'courseName' => $courseName,
  198. 'certificates' => [],
  199. ];
  200. $studentCertificates = GradebookUtils::get_list_gradebook_certificates_by_user_id(
  201. $selectedStudent,
  202. $gradebook->get_id()
  203. );
  204. if (!is_array($studentCertificates) || empty($studentCertificates)) {
  205. continue;
  206. }
  207. foreach ($studentCertificates as $certificate) {
  208. $certificateStudent['certificates'][] = [
  209. 'createdAt' => api_convert_and_format_date($certificate['created_at']),
  210. 'id' => $certificate['id'],
  211. ];
  212. }
  213. if (count($certificateStudent['certificates']) > 0) {
  214. $certificateStudents[] = $certificateStudent;
  215. }
  216. }
  217. }
  218. }
  219. }
  220. /* View */
  221. $template = new Template(get_lang('GradebookListOfStudentsCertificates'));
  222. $form = new FormValidator(
  223. 'certificate_report_form',
  224. 'post',
  225. api_get_path(WEB_CODE_PATH).'gradebook/certificate_report.php'
  226. );
  227. $form->addSelect('session', get_lang('Sessions'), $sessions, ['id' => 'session']);
  228. $form->addSelect('course', get_lang('Courses'), $courses, ['id' => 'course']);
  229. $form->addGroup(
  230. [
  231. $form->createElement(
  232. 'select',
  233. 'month',
  234. null,
  235. $months,
  236. ['id' => 'month']
  237. ),
  238. $form->createElement(
  239. 'text',
  240. 'year',
  241. null,
  242. ['id' => 'year', 'placeholder' => get_lang('Year')]
  243. ),
  244. ],
  245. null,
  246. get_lang('Date')
  247. );
  248. $form->addButtonSearch();
  249. $form->setDefaults([
  250. 'session' => $selectedSession,
  251. 'course' => $selectedCourse,
  252. 'month' => $selectedMonth,
  253. 'year' => $selectedYear,
  254. ]);
  255. if (api_is_student_boss()) {
  256. foreach ($userList as $studentId) {
  257. $students[$studentId] = api_get_user_info($studentId)['complete_name_with_username'];
  258. }
  259. $searchForm = new FormValidator(
  260. 'certificate_report_form',
  261. 'post',
  262. api_get_path(WEB_CODE_PATH).'gradebook/certificate_report.php'
  263. );
  264. $searchForm->addSelect('student', get_lang('Students'), $students, ['id' => 'student']);
  265. $searchForm->addButtonSearch();
  266. $searchForm->setDefaults([
  267. 'student' => $selectedStudent,
  268. ]);
  269. $template->assign('search_form', $searchForm->returnForm());
  270. }
  271. $template->assign('search_by_session_form', $form->returnForm());
  272. $template->assign('sessions', $sessions);
  273. $template->assign('courses', $courses);
  274. $template->assign('months', $months);
  275. $template->assign('export_all_link', $exportAllLink);
  276. $template->assign('certificate_students', $certificateStudents);
  277. $templateName = $template->get_template('gradebook/certificate_report.tpl');
  278. $content = $template->fetch($templateName);
  279. $template->assign('content', $content);
  280. $template->display_one_col_template();