teacher_time_report.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  16. $toolName = get_lang('TeacherTimeReport');
  17. // Access restrictions.
  18. api_protect_admin_script();
  19. $form = new FormValidator('teacher_time_report');
  20. $startDate = new DateTime(api_get_local_time());
  21. $startDate->modify('first day of this month');
  22. $limitDate = new DateTime(api_get_local_time());
  23. $selectedCourse = null;
  24. $selectedSession = 0;
  25. $selectedTeacher = 0;
  26. $selectedFrom = $startDate->format('Y-m-d');
  27. $selectedUntil = $limitDate->format('Y-m-d');
  28. if ($form->validate()) {
  29. $formValues = $form->getSubmitValues();
  30. $selectedCourse = $formValues['course'];
  31. $selectedSession = $formValues['session'];
  32. $selectedTeacher = $formValues['teacher'];
  33. if (!empty($formValues['from'])) {
  34. $selectedFrom = $formValues['from'];
  35. }
  36. if (!empty($formValues['until'])) {
  37. $selectedUntil = $formValues['until'];
  38. }
  39. }
  40. $optionsCourses = [0 => get_lang('None')];
  41. $optionsSessions = [0 => get_lang('None')];
  42. $optionsTeachers = [0 => get_lang('None')];
  43. $courseList = CourseManager::get_courses_list(0, 0, 'title');
  44. $sessionsList = SessionManager::get_sessions_list(array(), array('name'));
  45. $teacherList = UserManager::getTeachersList();
  46. foreach ($courseList as $courseItem) {
  47. $optionsCourses[$courseItem['code']] = $courseItem['title'];
  48. }
  49. foreach ($sessionsList as $sessionItem) {
  50. $optionsSessions[$sessionItem['id']] = $sessionItem['name'];
  51. }
  52. foreach ($teacherList as $teacherItem) {
  53. $optionsTeachers[$teacherItem['user_id']] = $teacherItem['completeName'];
  54. }
  55. $withFilter = false;
  56. $reportTitle = get_lang('TimeReportIncludingAllCoursesAndSessionsByTeacher');
  57. $reportSubTitle = sprintf(get_lang('TimeSpentBetweenXAndY'), $selectedFrom, $selectedUntil);
  58. $timeReport = new TeacherTimeReport();
  59. if (!empty($selectedCourse)) {
  60. $withFilter = true;
  61. $course = api_get_course_info($selectedCourse);
  62. $reportTitle = sprintf(get_lang('TimeReportForCourseX'), $course['title']);
  63. $teachers = CourseManager::get_teacher_list_from_course_code($selectedCourse);
  64. foreach ($teachers as $teacher) {
  65. $totalTime = UserManager::getTimeSpentInCourses(
  66. $teacher['user_id'],
  67. $course['real_id'],
  68. 0,
  69. $selectedFrom,
  70. $selectedUntil
  71. );
  72. $formattedTime = api_format_time($totalTime);
  73. $timeReport->data[] = array(
  74. 'session' => null,
  75. 'course' => array(
  76. 'id' => $course['real_id'],
  77. 'name' => $course['title']
  78. ),
  79. 'coach' => array(
  80. 'userId' => $teacher['user_id'],
  81. 'lastname' => $teacher['lastname'],
  82. 'firstname' => $teacher['firstname'],
  83. 'username' => $teacher['username'],
  84. 'completeName' => api_get_person_name($teacher['firstname'], $teacher['lastname'])
  85. ),
  86. 'totalTime' => $formattedTime
  87. );
  88. }
  89. $sessionsByCourse = SessionManager::get_session_by_course($course['real_id']);
  90. foreach ($sessionsByCourse as $session) {
  91. $coaches = CourseManager::get_coachs_from_course($session['id'], $course['real_id']);
  92. if (!empty($coaches)) {
  93. foreach ($coaches as $coach) {
  94. $totalTime = UserManager::getTimeSpentInCourses(
  95. $coach['user_id'],
  96. $course['real_id'],
  97. $session['id'],
  98. $selectedFrom,
  99. $selectedUntil
  100. );
  101. $formattedTime = api_format_time($totalTime);
  102. $timeReport->data[] = array(
  103. 'session' => array(
  104. 'id' => $session['id'],
  105. 'name' => $session['name']
  106. ),
  107. 'course' => array(
  108. 'id' => $course['real_id'],
  109. 'name' => $course['title']
  110. ),
  111. 'coach' => array(
  112. 'userId' => $coach['user_id'],
  113. 'lastname' => $coach['lastname'],
  114. 'firstname' => $coach['firstname'],
  115. 'username' => $coach['username'],
  116. 'completeName' => api_get_person_name($coach['firstname'], $coach['lastname'])
  117. ),
  118. 'totalTime' => $formattedTime
  119. );
  120. }
  121. }
  122. }
  123. }
  124. if (!empty($selectedSession)) {
  125. $withFilter = true;
  126. $session = api_get_session_info($selectedSession);
  127. $sessionData = array(
  128. 'id' => $session['id'],
  129. 'name' => $session['name']
  130. );
  131. $reportTitle = sprintf(get_lang('TimeReportForSessionX'), $session['name']);
  132. $courses = SessionManager::get_course_list_by_session_id($selectedSession);
  133. foreach ($courses as $course) {
  134. $courseData = array(
  135. 'id' => $course['id'],
  136. 'name' => $course['title']
  137. );
  138. $coaches = CourseManager::get_coachs_from_course($selectedSession, $course['id']);
  139. if (!empty($coaches)) {
  140. foreach ($coaches as $coach) {
  141. $totalTime = UserManager::getTimeSpentInCourses(
  142. $coach['user_id'],
  143. $course['id'],
  144. $selectedSession,
  145. $selectedFrom,
  146. $selectedUntil
  147. );
  148. $formattedTime = api_format_time($totalTime);
  149. $timeReport->data[] = array(
  150. 'session' => $sessionData,
  151. 'course' => $courseData,
  152. 'coach' => array(
  153. 'userId' => $coach['user_id'],
  154. 'lastname' => $coach['lastname'],
  155. 'firstname' => $coach['firstname'],
  156. 'username' => $coach['username'],
  157. 'completeName' => api_get_person_name($coach['firstname'], $coach['lastname'])
  158. ),
  159. 'totalTime' => $formattedTime
  160. );
  161. }
  162. }
  163. }
  164. }
  165. if (!empty($selectedTeacher)) {
  166. $withFilter = true;
  167. $teacher = api_get_user_info();
  168. $teacherData = array(
  169. 'userId' => $teacher['user_id'],
  170. 'lastname' => $teacher['lastname'],
  171. 'firstname' => $teacher['firstname'],
  172. 'username' => $teacher['username'],
  173. 'completeName' => $teacher['complete_name']
  174. );
  175. $reportTitle = sprintf(get_lang('TimeReportForTeacherX'), $teacher['complete_name']);
  176. $courses = CourseManager::get_courses_list_by_user_id($selectedTeacher, false);
  177. if (!empty($courses)) {
  178. foreach ($courses as $course) {
  179. $courseInfo = api_get_course_info_by_id($course['real_id']);
  180. $totalTime = UserManager::getTimeSpentInCourses(
  181. $selectedTeacher,
  182. $course['real_id'],
  183. 0,
  184. $selectedFrom,
  185. $selectedUntil
  186. );
  187. $formattedTime = api_format_time($totalTime);
  188. $timeReport->data[] = array(
  189. 'session' => null,
  190. 'course' => array(
  191. 'id' => $courseInfo['real_id'],
  192. 'name' => $courseInfo['title']
  193. ),
  194. 'coach' => $teacherData,
  195. 'totalTime' => $formattedTime
  196. );
  197. }
  198. }
  199. $coursesInSession = SessionManager::getCoursesListByCourseCoach($selectedTeacher);
  200. foreach ($coursesInSession as $userCourseSubscription) {
  201. $course = $userCourseSubscription->getCourse();
  202. $session = $userCourseSubscription->getSession();
  203. $totalTime = UserManager::getTimeSpentInCourses(
  204. $selectedTeacher,
  205. $course->getId(),
  206. $session->getId(),
  207. $selectedFrom,
  208. $selectedUntil
  209. );
  210. $formattedTime = api_format_time($totalTime);
  211. $timeReport->data[] = array(
  212. 'session' => [
  213. 'id' => $session->getId(),
  214. 'name' => $session->getName()
  215. ],
  216. 'course' => array(
  217. 'id' => $course->getId(),
  218. 'name' => $course->getTitle()
  219. ),
  220. 'coach' => $teacherData,
  221. 'totalTime' => $formattedTime
  222. );
  223. }
  224. }
  225. if (empty($selectedCourse) && empty($selectedSession) && empty($selectedTeacher)) {
  226. foreach ($teacherList as &$teacher) {
  227. $timeReport->data[] = array(
  228. 'coach' => array(
  229. 'username' => $teacher['username'],
  230. 'completeName' => $teacher['completeName'],
  231. ),
  232. 'totalTime' => SessionManager::getTotalUserTimeInPlatform(
  233. $teacher['user_id'],
  234. $selectedFrom,
  235. $selectedUntil
  236. )
  237. );
  238. }
  239. }
  240. $timeReport->sortData($withFilter);
  241. if (isset($_GET['export'])) {
  242. $dataToExport = $timeReport->prepareDataToExport($withFilter);
  243. $fileName = get_lang('TeacherTimeReport').' '.api_get_local_time();
  244. switch ($_GET['export']) {
  245. case 'pdf':
  246. $params = array(
  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('courseCoaches', $teacherList);
  320. $tpl->assign('form', $form->returnForm());
  321. $tpl->assign('rows', $timeReport->data);
  322. $templateName = $tpl->get_template('admin/teacher_time_report.tpl');
  323. $contentTemplate = $tpl->fetch($templateName);
  324. $tpl->assign('content', $contentTemplate);
  325. $tpl->display_one_col_template();