certificate_report.php 11 KB

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