teacher_time_report.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. /**
  5. * Generate a teacher time report in platform or sessions/courses
  6. *
  7. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  8. * @package chamilo.admin
  9. */
  10. // Resetting the course id.
  11. $cidReset = true;
  12. // Setting the section (for the tabs).
  13. $this_section = SECTION_PLATFORM_ADMIN;
  14. $interbreadcrumb[] = array('url' => Container::getRouter()->generate('administration') , "name" => get_lang('PlatformAdmin'));
  15. $toolName = get_lang('TeacherTimeReport');
  16. // Access restrictions.
  17. api_protect_admin_script();
  18. $form = new FormValidator('teacher_time_report');
  19. $startDate = new DateTime(api_get_local_time());
  20. $startDate->modify('first day of this month');
  21. $limitDate = new DateTime(api_get_local_time());
  22. $selectedCourse = null;
  23. $selectedSession = 0;
  24. $selectedTeacher = 0;
  25. $selectedFrom = $startDate->format('Y-m-d');
  26. $selectedUntil = $limitDate->format('Y-m-d');
  27. if ($form->validate()) {
  28. $formValues = $form->getSubmitValues();
  29. $selectedCourse = $formValues['course'];
  30. $selectedSession = $formValues['session'];
  31. $selectedTeacher = $formValues['teacher'];
  32. if (!empty($formValues['from'])) {
  33. $selectedFrom = $formValues['from'];
  34. }
  35. if (!empty($formValues['until'])) {
  36. $selectedUntil = $formValues['until'];
  37. }
  38. }
  39. $optionsCourses = [0 => get_lang('None')];
  40. $optionsSessions = [0 => get_lang('None')];
  41. $optionsTeachers = [0 => get_lang('None')];
  42. $courseList = CourseManager::get_courses_list(0, 0, 'title');
  43. $sessionsList = SessionManager::get_sessions_list(array(), array('name'));
  44. $teacherList = UserManager::getTeachersList();
  45. foreach ($courseList as $courseItem) {
  46. $optionsCourses[$courseItem['code']] = $courseItem['title'];
  47. }
  48. foreach ($sessionsList as $sessionItem) {
  49. $optionsSessions[$sessionItem['id']] = $sessionItem['name'];
  50. }
  51. foreach ($teacherList as $teacherItem) {
  52. $optionsTeachers[$teacherItem['user_id']] = $teacherItem['completeName'];
  53. }
  54. $withFilter = false;
  55. $reportTitle = get_lang('TimeReportIncludingAllCoursesAndSessionsByTeacher');
  56. $reportSubTitle = sprintf(get_lang('TimeSpentBetweenXAndY'), $selectedFrom, $selectedUntil);
  57. $timeReport = new TeacherTimeReport();
  58. if (!empty($selectedCourse)) {
  59. $withFilter = true;
  60. $course = api_get_course_info($selectedCourse);
  61. $reportTitle = sprintf(get_lang('TimeReportForCourseX'), $course['title']);
  62. $teachers = CourseManager::get_teacher_list_from_course_code($selectedCourse);
  63. foreach ($teachers as $teacher) {
  64. $totalTime = UserManager::getTimeSpentInCourses(
  65. $teacher['user_id'],
  66. $course['real_id'],
  67. 0,
  68. $selectedFrom,
  69. $selectedUntil
  70. );
  71. $formattedTime = api_format_time($totalTime);
  72. $timeReport->data[] = array(
  73. 'session' => null,
  74. 'course' => array(
  75. 'id' => $course['real_id'],
  76. 'name' => $course['title']
  77. ),
  78. 'coach' => array(
  79. 'userId' => $teacher['user_id'],
  80. 'lastname' => $teacher['lastname'],
  81. 'firstname' => $teacher['firstname'],
  82. 'username' => $teacher['username'],
  83. 'completeName' => api_get_person_name($teacher['firstname'], $teacher['lastname'])
  84. ),
  85. 'totalTime' => $formattedTime
  86. );
  87. }
  88. $sessionsByCourse = SessionManager::get_session_by_course($course['real_id']);
  89. foreach ($sessionsByCourse as $session) {
  90. $coaches = CourseManager::get_coachs_from_course($session['id'], $course['real_id']);
  91. if ($coaches) {
  92. foreach ($coaches as $coach) {
  93. $totalTime = UserManager::getTimeSpentInCourses(
  94. $coach['user_id'],
  95. $course['real_id'],
  96. $session['id'],
  97. $selectedFrom,
  98. $selectedUntil
  99. );
  100. $formattedTime = api_format_time($totalTime);
  101. $timeReport->data[] = array(
  102. 'session' => array(
  103. 'id' => $session['id'],
  104. 'name' => $session['name']
  105. ),
  106. 'course' => array(
  107. 'id' => $course['real_id'],
  108. 'name' => $course['title']
  109. ),
  110. 'coach' => array(
  111. 'userId' => $coach['user_id'],
  112. 'lastname' => $coach['lastname'],
  113. 'firstname' => $coach['firstname'],
  114. 'username' => $coach['username'],
  115. 'completeName' => api_get_person_name($coach['firstname'], $coach['lastname'])
  116. ),
  117. 'totalTime' => $formattedTime
  118. );
  119. }
  120. }
  121. }
  122. }
  123. if (!empty($selectedSession)) {
  124. $withFilter = true;
  125. $session = api_get_session_info($selectedSession);
  126. $sessionData = array(
  127. 'id' => $session['id'],
  128. 'name' => $session['name']
  129. );
  130. $reportTitle = sprintf(get_lang('TimeReportForSessionX'), $session['name']);
  131. $courses = SessionManager::get_course_list_by_session_id($selectedSession);
  132. foreach ($courses as $course) {
  133. $courseData = array(
  134. 'id' => $course['id'],
  135. 'name' => $course['title']
  136. );
  137. $coaches = CourseManager::get_coachs_from_course($selectedSession, $course['id']);
  138. if ($coaches) {
  139. foreach ($coaches as $coach) {
  140. $totalTime = UserManager::getTimeSpentInCourses(
  141. $coach['user_id'],
  142. $course['id'],
  143. $selectedSession,
  144. $selectedFrom,
  145. $selectedUntil
  146. );
  147. $formattedTime = api_format_time($totalTime);
  148. $timeReport->data[] = array(
  149. 'session' => $sessionData,
  150. 'course' => $courseData,
  151. 'coach' => array(
  152. 'userId' => $coach['user_id'],
  153. 'lastname' => $coach['lastname'],
  154. 'firstname' => $coach['firstname'],
  155. 'username' => $coach['username'],
  156. 'completeName' => api_get_person_name($coach['firstname'], $coach['lastname'])
  157. ),
  158. 'totalTime' => $formattedTime
  159. );
  160. }
  161. }
  162. }
  163. }
  164. if (!empty($selectedTeacher)) {
  165. $withFilter = true;
  166. $teacher = api_get_user_info();
  167. $teacherData = array(
  168. 'userId' => $teacher['user_id'],
  169. 'lastname' => $teacher['lastname'],
  170. 'firstname' => $teacher['firstname'],
  171. 'username' => $teacher['username'],
  172. 'completeName' => $teacher['complete_name']
  173. );
  174. $reportTitle = sprintf(get_lang('TimeReportForTeacherX'), $teacher['complete_name']);
  175. $courses = CourseManager::get_courses_list_by_user_id($selectedTeacher, false);
  176. if (!empty($courses)) {
  177. foreach ($courses as $course) {
  178. $courseInfo = api_get_course_info_by_id($course['real_id']);
  179. $totalTime = UserManager::getTimeSpentInCourses(
  180. $selectedTeacher,
  181. $course['real_id'],
  182. 0,
  183. $selectedFrom,
  184. $selectedUntil
  185. );
  186. $formattedTime = api_format_time($totalTime);
  187. $timeReport->data[] = array(
  188. 'session' => null,
  189. 'course' => array(
  190. 'id' => $courseInfo['real_id'],
  191. 'name' => $courseInfo['title']
  192. ),
  193. 'coach' => $teacherData,
  194. 'totalTime' => $formattedTime
  195. );
  196. }
  197. }
  198. $coursesInSession = SessionManager::getCoursesListByCourseCoach($selectedTeacher);
  199. foreach ($coursesInSession as $userCourseSubscription) {
  200. $course = $userCourseSubscription->getCourse();
  201. $session = $userCourseSubscription->getSession();
  202. $totalTime = UserManager::getTimeSpentInCourses(
  203. $selectedTeacher,
  204. $course->getId(),
  205. $session->getId(),
  206. $selectedFrom,
  207. $selectedUntil
  208. );
  209. $formattedTime = api_format_time($totalTime);
  210. $timeReport->data[] = array(
  211. 'session' => [
  212. 'id' => $session->getId(),
  213. 'name' => $session->getName()
  214. ],
  215. 'course' => array(
  216. 'id' => $course->getId(),
  217. 'name' => $course->getTitle()
  218. ),
  219. 'coach' => $teacherData,
  220. 'totalTime' => $formattedTime
  221. );
  222. }
  223. }
  224. if (empty($selectedCourse) && empty($selectedSession) && empty($selectedTeacher)) {
  225. foreach ($teacherList as &$teacher) {
  226. $timeReport->data[] = array(
  227. 'coach' => array(
  228. 'username' => $teacher['username'],
  229. 'completeName' => $teacher['completeName'],
  230. ),
  231. 'totalTime' => SessionManager::getTotalUserTimeInPlatform(
  232. $teacher['user_id'],
  233. $selectedFrom,
  234. $selectedUntil
  235. )
  236. );
  237. }
  238. }
  239. $timeReport->sortData($withFilter);
  240. if (isset($_GET['export'])) {
  241. $dataToExport = $timeReport->prepareDataToExport($withFilter);
  242. $fileName = get_lang('TeacherTimeReport') . ' ' . api_get_local_time();
  243. switch ($_GET['export']) {
  244. case 'pdf':
  245. $params = array(
  246. 'add_signatures' => false,
  247. 'filename' => $fileName,
  248. 'pdf_title' => "$reportTitle - $reportSubTitle",
  249. 'pdf_description' => get_lang('TeacherTimeReport'),
  250. 'format' => 'A4-L',
  251. 'orientation' => 'L'
  252. );
  253. $pdfContent = Export::convert_array_to_html($dataToExport);
  254. Export::export_html_to_pdf($pdfContent, $params);
  255. break;
  256. case 'xls':
  257. array_unshift($dataToExport, array(
  258. $reportTitle
  259. ), array(
  260. $reportSubTitle
  261. ), array());
  262. Export::export_table_xls_html($dataToExport, $fileName);
  263. break;
  264. }
  265. die;
  266. }
  267. $form->addSelect(
  268. 'course',
  269. get_lang('Course'),
  270. $optionsCourses,
  271. ['id' => 'courses']
  272. );
  273. $form->addSelect(
  274. 'session',
  275. get_lang('Session'),
  276. $optionsSessions,
  277. ['id' => 'session']
  278. );
  279. $form->addSelect(
  280. 'teacher',
  281. get_lang('Teacher'),
  282. $optionsTeachers,
  283. ['id' => 'teacher']
  284. );
  285. $form->addDateRangePicker(
  286. 'daterange',
  287. get_lang('Date'),
  288. false,
  289. [
  290. 'id' => 'daterange',
  291. 'maxDate' => $limitDate->format('Y-m-d'),
  292. 'format' => 'YYYY-MM-DD',
  293. 'timePicker' => 'false',
  294. 'value' => "$selectedFrom / $selectedUntil"
  295. ]
  296. );
  297. $form->addButtonFilter(get_lang('Filter'));
  298. $form->addHidden('from', '');
  299. $form->addHidden('until', '');
  300. $form->setDefaults([
  301. 'course' => $selectedCourse,
  302. 'session' => $selectedSession,
  303. 'teacher' => $selectedTeacher,
  304. 'date_range' => "$selectedFrom / $selectedUntil",
  305. 'from' => $selectedFrom,
  306. 'until' => $selectedUntil
  307. ]);
  308. $tpl = new Template($toolName);
  309. $tpl->assign('report_title', $reportTitle);
  310. $tpl->assign('report_sub_title', $reportSubTitle);
  311. $tpl->assign('selected_course', $selectedCourse);
  312. $tpl->assign('selected_session', $selectedSession);
  313. $tpl->assign('selected_teacher', $selectedTeacher);
  314. $tpl->assign('selected_from', $selectedFrom);
  315. $tpl->assign('selected_until', $selectedUntil);
  316. $tpl->assign('with_filter', $withFilter);
  317. $tpl->assign('courses', $courseList);
  318. $tpl->assign('sessions', $sessionsList);
  319. $tpl->assign('form', $form->returnForm());
  320. $tpl->assign('rows', $timeReport->data);
  321. $templateName = $tpl->get_template('admin/teacher_time_report.tpl');
  322. $contentTemplate = $tpl->fetch($templateName);
  323. $tpl->assign('content', $contentTemplate);
  324. $tpl->display_one_col_template();