certificate_report.php 10 KB

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