teacher_time_report.php 13 KB

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