teacher_time_report.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Generate a teacher time report in platform or sessions/courses.
  5. *
  6. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  7. */
  8. // Resetting the course id.
  9. $cidReset = true;
  10. // Including some necessary library files.
  11. require_once __DIR__.'/../inc/global.inc.php';
  12. // Setting the section (for the tabs).
  13. $this_section = SECTION_PLATFORM_ADMIN;
  14. $interbreadcrumb[] = [
  15. 'url' => 'index.php',
  16. 'name' => get_lang('Administration'),
  17. ];
  18. $toolName = get_lang('Trainers time report');
  19. // Access restrictions.
  20. api_protect_admin_script();
  21. $form = new FormValidator('teacher_time_report', 'get');
  22. $startDate = new DateTime(api_get_local_time());
  23. $startDate->modify('first day of this month');
  24. $limitDate = new DateTime(api_get_local_time());
  25. $selectedCourse = null;
  26. $selectedSession = 0;
  27. $selectedTeacher = 0;
  28. $selectedFrom = $startDate->format('Y-m-d');
  29. $selectedUntil = $limitDate->format('Y-m-d');
  30. if ($form->validate()) {
  31. $formValues = $form->getSubmitValues();
  32. $selectedCourse = $formValues['course'];
  33. $selectedSession = $formValues['session'];
  34. $selectedTeacher = $formValues['teacher'];
  35. if (!empty($formValues['from'])) {
  36. $selectedFrom = Security::remove_XSS($formValues['from']);
  37. }
  38. if (!empty($formValues['until'])) {
  39. $selectedUntil = Security::remove_XSS($formValues['until']);
  40. }
  41. }
  42. $optionsCourses = [0 => get_lang('None')];
  43. $optionsSessions = [0 => get_lang('None')];
  44. $optionsTeachers = [0 => get_lang('None')];
  45. $courseList = CourseManager::get_courses_list(
  46. 0,
  47. 0,
  48. 'title',
  49. 'ASC',
  50. -1,
  51. null,
  52. api_get_current_access_url_id()
  53. );
  54. $sessionList = SessionManager::get_sessions_list([], ['name']);
  55. $teacherList = UserManager::get_user_list(['status' => COURSEMANAGER]);
  56. foreach ($courseList as $courseItem) {
  57. $optionsCourses[$courseItem['code']] = $courseItem['title'];
  58. }
  59. foreach ($sessionList as $sessionItem) {
  60. $optionsSessions[$sessionItem['id']] = $sessionItem['name'];
  61. }
  62. foreach ($teacherList as $teacherItem) {
  63. $optionsTeachers[$teacherItem['user_id']] = $teacherItem['complete_name'];
  64. }
  65. $withFilter = false;
  66. $reportTitle = get_lang('Time report including all courses and sessions, by teacher');
  67. $reportSubTitle = sprintf(
  68. get_lang('Time spent between %s and %s'),
  69. $selectedFrom,
  70. $selectedUntil
  71. );
  72. $timeReport = new TeacherTimeReport();
  73. if (!empty($selectedCourse)) {
  74. $withFilter = true;
  75. $course = api_get_course_info($selectedCourse);
  76. if (empty($course)) {
  77. api_not_allowed(true);
  78. }
  79. $reportTitle = sprintf(get_lang('Time report for course %s'), $course['title']);
  80. $teachers = CourseManager::get_teacher_list_from_course_code($selectedCourse);
  81. foreach ($teachers as $teacher) {
  82. $totalTime = UserManager::getTimeSpentInCourses(
  83. $teacher['user_id'],
  84. $course['real_id'],
  85. 0,
  86. $selectedFrom,
  87. $selectedUntil
  88. );
  89. $formattedTime = api_format_time($totalTime);
  90. $timeReport->data[] = [
  91. 'session' => null,
  92. 'course' => [
  93. 'id' => $course['real_id'],
  94. 'name' => $course['title'],
  95. ],
  96. 'coach' => [
  97. 'userId' => $teacher['user_id'],
  98. 'lastname' => $teacher['lastname'],
  99. 'firstname' => $teacher['firstname'],
  100. 'username' => $teacher['username'],
  101. 'complete_name' => api_get_person_name(
  102. $teacher['firstname'],
  103. $teacher['lastname']
  104. ),
  105. ],
  106. 'total_time' => $formattedTime,
  107. ];
  108. }
  109. $sessionsByCourse = SessionManager::get_session_by_course($course['real_id']);
  110. foreach ($sessionsByCourse as $session) {
  111. $coaches = CourseManager::get_coachs_from_course(
  112. $session['id'],
  113. $course['real_id']
  114. );
  115. if (!empty($coaches)) {
  116. foreach ($coaches as $coach) {
  117. $totalTime = UserManager::getTimeSpentInCourses(
  118. $coach['user_id'],
  119. $course['real_id'],
  120. $session['id'],
  121. $selectedFrom,
  122. $selectedUntil
  123. );
  124. $formattedTime = api_format_time($totalTime);
  125. $timeReport->data[] = [
  126. 'session' => [
  127. 'id' => $session['id'],
  128. 'name' => $session['name'],
  129. ],
  130. 'course' => [
  131. 'id' => $course['real_id'],
  132. 'name' => $course['title'],
  133. ],
  134. 'coach' => [
  135. 'userId' => $coach['user_id'],
  136. 'lastname' => $coach['lastname'],
  137. 'firstname' => $coach['firstname'],
  138. 'username' => $coach['username'],
  139. 'complete_name' => api_get_person_name(
  140. $coach['firstname'],
  141. $coach['lastname']
  142. ),
  143. ],
  144. 'total_time' => $formattedTime,
  145. ];
  146. }
  147. }
  148. }
  149. }
  150. if (!empty($selectedSession)) {
  151. $withFilter = true;
  152. $session = api_get_session_info($selectedSession);
  153. $sessionData = [
  154. 'id' => $session['id'],
  155. 'name' => $session['name'],
  156. ];
  157. $reportTitle = sprintf(get_lang('Time report for session %s'), $session['name']);
  158. $courses = SessionManager::get_course_list_by_session_id($selectedSession);
  159. foreach ($courses as $course) {
  160. $courseData = [
  161. 'id' => $course['id'],
  162. 'name' => $course['title'],
  163. ];
  164. $coaches = CourseManager::get_coachs_from_course(
  165. $selectedSession,
  166. $course['id']
  167. );
  168. if (!empty($coaches)) {
  169. foreach ($coaches as $coach) {
  170. $totalTime = UserManager::getTimeSpentInCourses(
  171. $coach['user_id'],
  172. $course['id'],
  173. $selectedSession,
  174. $selectedFrom,
  175. $selectedUntil
  176. );
  177. $formattedTime = api_format_time($totalTime);
  178. $timeReport->data[] = [
  179. 'session' => $sessionData,
  180. 'course' => $courseData,
  181. 'coach' => [
  182. 'userId' => $coach['user_id'],
  183. 'lastname' => $coach['lastname'],
  184. 'firstname' => $coach['firstname'],
  185. 'username' => $coach['username'],
  186. 'complete_name' => api_get_person_name(
  187. $coach['firstname'],
  188. $coach['lastname']
  189. ),
  190. ],
  191. 'total_time' => $formattedTime,
  192. ];
  193. }
  194. }
  195. }
  196. }
  197. if (!empty($selectedTeacher)) {
  198. $withFilter = true;
  199. $teacher = api_get_user_info($selectedTeacher);
  200. $teacherData = [
  201. 'userId' => $teacher['user_id'],
  202. 'lastname' => $teacher['lastname'],
  203. 'firstname' => $teacher['firstname'],
  204. 'username' => $teacher['username'],
  205. 'complete_name' => $teacher['complete_name'],
  206. ];
  207. $reportTitle = sprintf(
  208. get_lang('Time report for teacher %s'),
  209. $teacher['complete_name']
  210. );
  211. $courses = CourseManager::get_courses_list_by_user_id(
  212. $selectedTeacher,
  213. false
  214. );
  215. if (!empty($courses)) {
  216. foreach ($courses as $course) {
  217. $courseInfo = api_get_course_info_by_id($course['real_id']);
  218. $totalTime = UserManager::getTimeSpentInCourses(
  219. $selectedTeacher,
  220. $course['real_id'],
  221. 0,
  222. $selectedFrom,
  223. $selectedUntil
  224. );
  225. $formattedTime = api_format_time($totalTime);
  226. $timeReport->data[] = [
  227. 'session' => null,
  228. 'course' => [
  229. 'id' => $courseInfo['real_id'],
  230. 'name' => $courseInfo['title'],
  231. ],
  232. 'coach' => $teacherData,
  233. 'total_time' => $formattedTime,
  234. ];
  235. }
  236. }
  237. $coursesInSession = SessionManager::getCoursesListByCourseCoach($selectedTeacher);
  238. foreach ($coursesInSession as $userCourseSubscription) {
  239. $course = $userCourseSubscription->getCourse();
  240. $session = $userCourseSubscription->getSession();
  241. $totalTime = UserManager::getTimeSpentInCourses(
  242. $selectedTeacher,
  243. $course->getId(),
  244. $session->getId(),
  245. $selectedFrom,
  246. $selectedUntil
  247. );
  248. $formattedTime = api_format_time($totalTime);
  249. $timeReport->data[] = [
  250. 'session' => [
  251. 'id' => $session->getId(),
  252. 'name' => $session->getName(),
  253. ],
  254. 'course' => [
  255. 'id' => $course->getId(),
  256. 'name' => $course->getTitle(),
  257. ],
  258. 'coach' => $teacherData,
  259. 'total_time' => $formattedTime,
  260. ];
  261. }
  262. }
  263. if (empty($selectedCourse) && empty($selectedSession) &&
  264. empty($selectedTeacher)
  265. ) {
  266. foreach ($teacherList as &$teacher) {
  267. $timeReport->data[] = [
  268. 'coach' => [
  269. 'username' => $teacher['username'],
  270. 'complete_name' => $teacher['complete_name'],
  271. ],
  272. 'total_time' => SessionManager::getTotalUserTimeInPlatform(
  273. $teacher['user_id'],
  274. $selectedFrom,
  275. $selectedUntil
  276. ),
  277. ];
  278. }
  279. }
  280. $timeReport->sortData($withFilter);
  281. if (isset($_GET['export'])) {
  282. $dataToExport = $timeReport->prepareDataToExport($withFilter);
  283. $fileName = get_lang('Trainers time report').' '.api_get_local_time();
  284. switch ($_GET['export']) {
  285. case 'pdf':
  286. $params = [
  287. 'filename' => $fileName,
  288. 'pdf_title' => "$reportTitle - $reportSubTitle",
  289. 'pdf_description' => get_lang('Trainers time report'),
  290. 'format' => 'A4-L',
  291. 'orientation' => 'L',
  292. ];
  293. $pdfContent = Export::convert_array_to_html($dataToExport);
  294. Export::export_html_to_pdf($pdfContent, $params);
  295. break;
  296. case 'xls':
  297. array_unshift($dataToExport, [
  298. $reportTitle,
  299. ], [
  300. $reportSubTitle,
  301. ], []);
  302. Export::export_table_xls_html($dataToExport, $fileName);
  303. break;
  304. }
  305. die;
  306. }
  307. $form->addSelect(
  308. 'course',
  309. get_lang('Course'),
  310. $optionsCourses,
  311. ['id' => 'courses']
  312. );
  313. $form->addSelect(
  314. 'session',
  315. get_lang('Session'),
  316. $optionsSessions,
  317. ['id' => 'session']
  318. );
  319. $form->addSelect(
  320. 'teacher',
  321. get_lang('Teacher'),
  322. $optionsTeachers,
  323. ['id' => 'teacher']
  324. );
  325. $form->addDateRangePicker(
  326. 'daterange',
  327. get_lang('Date'),
  328. false,
  329. [
  330. 'id' => 'daterange',
  331. 'maxDate' => $limitDate->format('Y-m-d'),
  332. 'format' => 'YYYY-MM-DD',
  333. 'timePicker' => 'false',
  334. 'value' => "$selectedFrom / $selectedUntil",
  335. ]
  336. );
  337. $form->addButtonFilter(get_lang('Filter'));
  338. $form->addHidden('from', '');
  339. $form->addHidden('until', '');
  340. $form->setDefaults([
  341. 'course' => $selectedCourse,
  342. 'session' => $selectedSession,
  343. 'teacher' => $selectedTeacher,
  344. 'date_range' => "$selectedFrom / $selectedUntil",
  345. 'from' => $selectedFrom,
  346. 'until' => $selectedUntil,
  347. ]);
  348. $leftActions = Display::url(
  349. Display::return_icon(
  350. 'session.png',
  351. get_lang('Course sessions'),
  352. [],
  353. ICON_SIZE_MEDIUM
  354. ),
  355. api_get_path(WEB_CODE_PATH).'admin/teachers_time_by_session_report.php'
  356. );
  357. $exportUrlParams = [
  358. 'from' => $selectedFrom,
  359. 'until' => $selectedUntil,
  360. 'course' => $selectedCourse,
  361. 'session' => $selectedSession,
  362. 'teacher' => $selectedTeacher,
  363. '_qf__teacher_time_report' => '',
  364. ];
  365. $rightActions = Display::url(
  366. Display::return_icon(
  367. 'pdf.png',
  368. get_lang('Export to PDF'),
  369. [],
  370. ICON_SIZE_MEDIUM
  371. ),
  372. api_get_self().'?export=pdf&'.http_build_query($exportUrlParams)
  373. );
  374. $rightActions .= Display::url(
  375. Display::return_icon(
  376. 'export_excel.png',
  377. get_lang('Excel export'),
  378. [],
  379. ICON_SIZE_MEDIUM
  380. ),
  381. api_get_self().'?export=xls&'.http_build_query($exportUrlParams)
  382. );
  383. $tpl = new Template($toolName);
  384. $tpl->assign('report_title', $reportTitle);
  385. $tpl->assign('report_sub_title', $reportSubTitle);
  386. $tpl->assign('selected_course', $selectedCourse);
  387. $tpl->assign('selected_session', $selectedSession);
  388. $tpl->assign('selected_teacher', $selectedTeacher);
  389. $tpl->assign('selected_from', $selectedFrom);
  390. $tpl->assign('selected_until', $selectedUntil);
  391. $tpl->assign('with_filter', $withFilter);
  392. $tpl->assign('courses', $courseList);
  393. $tpl->assign('sessions', $sessionList);
  394. $tpl->assign('form', $form->returnForm());
  395. $tpl->assign('rows', $timeReport->data);
  396. $templateName = $tpl->get_template('admin/teacher_time_report.tpl');
  397. $contentTemplate = $tpl->fetch($templateName);
  398. $tpl->assign('header', get_lang('Trainers time report'));
  399. $tpl->assign(
  400. 'actions',
  401. Display::toolbarAction(
  402. 'teacher_time_report_actions',
  403. [$leftActions, $rightActions]
  404. )
  405. );
  406. $tpl->assign('content', $contentTemplate);
  407. $tpl->display_one_col_template();