certificate_report.php 11 KB

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