certificate_report.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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?".api_get_cidreq(),
  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. exit;
  152. }
  153. $sessionList = SessionManager::getSessionsFollowedByUser($selectedStudent);
  154. foreach ($sessionList as $session) {
  155. $sessionCourseList = SessionManager::get_course_list_by_session_id($session['id']);
  156. foreach ($sessionCourseList as $sessionCourse) {
  157. $gradebookCategories = Category::load(null, null, $sessionCourse['code'], null, false, $session['id']);
  158. $gradebook = null;
  159. if (!empty($gradebookCategories)) {
  160. $gradebook = current($gradebookCategories);
  161. }
  162. if (!is_null($gradebook)) {
  163. $sessionName = $session['name'];
  164. $courseName = $sessionCourse['title'];
  165. $certificateStudent = [
  166. 'fullName' => $selectedStudentInfo['complete_name'],
  167. 'sessionName' => $sessionName,
  168. 'courseName' => $courseName,
  169. 'certificates' => []
  170. ];
  171. $studentCertificates = GradebookUtils::get_list_gradebook_certificates_by_user_id(
  172. $selectedStudent,
  173. $gradebook->get_id()
  174. );
  175. if (!is_array($studentCertificates) || empty($studentCertificates)) {
  176. continue;
  177. }
  178. foreach ($studentCertificates as $certificate) {
  179. $certificateStudent['certificates'][] = array(
  180. 'createdAt' => api_convert_and_format_date($certificate['created_at']),
  181. 'id' => $certificate['id']
  182. );
  183. }
  184. if (count($certificateStudent['certificates']) > 0) {
  185. $certificateStudents[] = $certificateStudent;
  186. }
  187. }
  188. }
  189. }
  190. }
  191. /* View */
  192. $template = new Template(get_lang('GradebookListOfStudentsCertificates'));
  193. if (Session::has('reportErrorMessage')) {
  194. $template->assign('errorMessage', Session::read('reportErrorMessage'));
  195. }
  196. $searchBySessionCourseDateForm = new FormValidator(
  197. 'certificate_report_form',
  198. 'post',
  199. api_get_path(WEB_CODE_PATH) . 'gradebook/certificate_report.php'
  200. );
  201. $searchBySessionCourseDateForm->addSelect('session', get_lang('Sessions'), $sessions, ['id' => 'session']);
  202. $searchBySessionCourseDateForm->addSelect('course', get_lang('Courses'), $courses, ['id' => 'course']);
  203. $searchBySessionCourseDateForm->addGroup(
  204. [
  205. $searchBySessionCourseDateForm->createElement('select', 'month', null, $months, ['id' => 'month']),
  206. $searchBySessionCourseDateForm->createElement(
  207. 'text',
  208. 'year',
  209. null,
  210. ['id' => 'year', 'placeholder' => get_lang('Year')]
  211. )
  212. ],
  213. null,
  214. get_lang('Date')
  215. );
  216. $searchBySessionCourseDateForm->addButtonSearch();
  217. $searchBySessionCourseDateForm->setDefaults([
  218. 'session' => $selectedSession,
  219. 'course' => $selectedCourse,
  220. 'month' => $selectedMonth,
  221. 'year' => $selectedYear
  222. ]);
  223. if (api_is_student_boss()) {
  224. foreach ($userList as $studentId) {
  225. $students[$studentId] = api_get_user_info($studentId)['complete_name_with_username'];
  226. }
  227. $searchByStudentForm = new FormValidator(
  228. 'certificate_report_form',
  229. 'post',
  230. api_get_path(WEB_CODE_PATH) . 'gradebook/certificate_report.php'
  231. );
  232. $searchByStudentForm->addSelect('student', get_lang('Students'), $students, ['id' => 'student']);
  233. $searchByStudentForm->addButtonSearch();
  234. $searchByStudentForm->setDefaults([
  235. 'student' => $selectedStudent
  236. ]);
  237. $template->assign('searchByStudentForm', $searchByStudentForm->returnForm());
  238. }
  239. $template->assign('searchBySessionCourseDateForm', $searchBySessionCourseDateForm->returnForm());
  240. $template->assign('sessions', $sessions);
  241. $template->assign('courses', $courses);
  242. $template->assign('months', $months);
  243. $template->assign('exportAllLink', $exportAllLink);
  244. $template->assign('certificateStudents', $certificateStudents);
  245. $content = $template->fetch("default/gradebook/certificate_report.tpl");
  246. $template->assign('content', $content);
  247. $template->display_one_col_template();
  248. Session::erase('reportErrorMessage');