certificate_report.php 11 KB

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