teacher_time_report.php 13 KB

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