teacher_time_report.php 12 KB

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