瀏覽代碼

Merge pull request #808 from AngelFQC/BT10175

Fix Certificate and Teacher Time reports - refs BT#10175
Angel Fernando Quiroz Campos 9 年之前
父節點
當前提交
3fcad20b7b

+ 93 - 26
main/admin/teacher_time_report.php

@@ -24,22 +24,56 @@ $toolName = get_lang('TeacherTimeReport');
 // Access restrictions.
 api_protect_admin_script();
 
+$form = new FormValidator('teacher_time_report');
+
 $startDate = new DateTime(api_get_local_time());
 $startDate->modify('first day of this month');
 
 $limitDate = new DateTime(api_get_local_time());
 
-$selectedCourse = isset($_REQUEST['course']) ? $_REQUEST['course'] : null;
-$selectedSession = isset($_REQUEST['session']) ? $_REQUEST['session'] : 0;
-$selectedTeacher = isset($_REQUEST['teacher']) ? $_REQUEST['teacher'] : 0;
-$selectedFrom = isset($_REQUEST['from']) && !empty($_REQUEST['from']) ? $_REQUEST['from'] : $startDate->format('Y-m-d');
-$selectedUntil = isset($_REQUEST['from']) && !empty($_REQUEST['until']) ? $_REQUEST['until'] : $limitDate->format('Y-m-d');
+$selectedCourse = null;
+$selectedSession = 0;
+$selectedTeacher = 0;
+$selectedFrom = $startDate->format('Y-m-d');
+$selectedUntil = $limitDate->format('Y-m-d');
+
+if ($form->validate()) {
+    $formValues = $form->getSubmitValues();
+
+    $selectedCourse = $formValues['course'];
+    $selectedSession = $formValues['session'];
+    $selectedTeacher = $formValues['teacher'];
+
+    if (!empty($formValues['from'])) {
+        $selectedFrom = $formValues['from'];
+    }
+
+    if (!empty($formValues['until'])) {
+        $selectedUntil = $formValues['until'];
+    }
+}
+
+$optionsCourses = [0 => get_lang('None')];
+$optionsSessions = [0 => get_lang('None')];
+$optionsTeachers = [0 => get_lang('None')];
 
 $courseList = CourseManager::get_courses_list(0, 0, 'title');
 $sessionsList = SessionManager::get_sessions_list(array(), array('name'));
 
 $teacherList = UserManager::getTeachersList();
 
+foreach ($courseList as $courseItem) {
+    $optionsCourses[$courseItem['code']] = $courseItem['title'];
+}
+
+foreach ($sessionsList as $sessionItem) {
+    $optionsSessions[$sessionItem['id']] = $sessionItem['name'];
+}
+
+foreach ($teacherList as $teacherItem) {
+    $optionsTeachers[$teacherItem['user_id']] = $teacherItem['completeName'];
+}
+
 $withFilter = false;
 
 $reportTitle = get_lang('TimeReportIncludingAllCoursesAndSessionsByTeacher');
@@ -212,29 +246,27 @@ if (!empty($selectedTeacher)) {
 
     $coursesInSession = SessionManager::getCoursesListByCourseCoach($selectedTeacher);
 
-    foreach ($coursesInSession as $course) {
-        $session = api_get_session_info($course['id_session']);
-        $sessionData = array(
-            'id' => $session['id'],
-            'name' => $session['name']
-        );
-
-        $courseInfo = api_get_course_info_by_id($course['c_id']);
+    foreach ($coursesInSession as $userCourseSubscription) {
+        $course = $userCourseSubscription->getCourse();
+        $session = $userCourseSubscription->getSession();
 
         $totalTime = UserManager::getTimeSpentInCourses(
             $selectedTeacher,
-            $course['c_id'],
-            $session['id'],
+            $course->getId(),
+            $session->getId(),
             $selectedFrom,
             $selectedUntil
         );
         $formattedTime = api_format_time($totalTime);
 
         $timeReport->data[] = array(
-            'session' => $sessionData,
+            'session' => [
+                'id' => $session->getId(),
+                'name' => $session->getName()
+            ],
             'course' => array(
-                'id' => $course['c_id'],
-                'name' => $courseInfo['title']
+                'id' => $course->getId(),
+                'name' => $course->getTitle()
             ),
             'coach' => $teacherData,
             'totalTime' => $formattedTime
@@ -294,19 +326,52 @@ if (isset($_GET['export'])) {
     die;
 }
 
-// view
-//hack for daterangepicker
-$startDate->modify('+1 day');
-$limitDate->modify('+1 day');
+$form->addSelect(
+    'course',
+    get_lang('Course'),
+    $optionsCourses,
+    ['id' => 'courses']
+);
+$form->addSelect(
+    'session',
+    get_lang('Session'),
+    $optionsSessions,
+    ['id' => 'session']
+);
+$form->addSelect(
+    'teacher',
+    get_lang('Teacher'),
+    $optionsTeachers,
+    ['id' => 'teacher']
+);
+$form->addDateRangePicker(
+    'daterange',
+    get_lang('Date'),
+    false,
+    [
+        'id' => 'daterange',
+        'maxDate' => $limitDate->format('Y-m-d'),
+        'format' => 'YYYY-MM-DD',
+        'timePicker' => 'false',
+        'value' => "$selectedFrom / $selectedUntil"
+    ]
+);
+$form->addButtonFilter(get_lang('Filter'));
+$form->addHidden('from', '');
+$form->addHidden('until', '');
+$form->setDefaults([
+    'course' => $selectedCourse,
+    'session' => $selectedSession,
+    'teacher' => $selectedTeacher,
+    'date_range' => "$selectedFrom / $selectedUntil",
+    'from' => $selectedFrom,
+    'until' => $selectedUntil
+]);
 
 $tpl = new Template($toolName);
 $tpl->assign('reportTitle', $reportTitle);
 $tpl->assign('reportSubTitle', $reportSubTitle);
 
-$tpl->assign('filterStartDate', $startDate->format('Y-m-d'));
-$tpl->assign('filterEndDate', $limitDate->format('Y-m-d'));
-$tpl->assign('filterMaxDate', $limitDate->format('Y-m-d'));
-
 $tpl->assign('selectedCourse', $selectedCourse);
 $tpl->assign('selectedSession', $selectedSession);
 $tpl->assign('selectedTeacher', $selectedTeacher);
@@ -319,6 +384,8 @@ $tpl->assign('courses', $courseList);
 $tpl->assign('sessions', $sessionsList);
 $tpl->assign('courseCoaches', $teacherList);
 
+$tpl->assign('form', $form->returnForm());
+
 $tpl->assign('rows', $timeReport->data);
 
 $contentTemplate = $tpl->get_template('admin/teacher_time_report.tpl');

+ 28 - 2
main/inc/lib/formvalidator/Element/DateRangePicker.php

@@ -61,15 +61,41 @@ class DateRangePicker extends HTML_QuickForm_text
                     endDate: '".$dates['end']."', ";
         }
 
+        $minDate = null;
+        if (!empty($this->getAttribute('minDate'))) {
+            $minDate = "
+                minDate: '{$this->getAttribute('minDate')}',
+            ";
+        }
+
+        $maxDate = null;
+        if (!empty($this->getAttribute('maxDate'))) {
+            $maxDate = "
+                maxDate: '{$this->getAttribute('maxDate')}',
+            ";
+        }
+
+        $format = 'YYYY-MM-DD HH:mm';
+        if (!empty($this->getAttribute('format'))) {
+            $format = $this->getAttribute('format');
+        }
+
+        $timePicker = 'true';
+        if (!empty($this->getAttribute('timePicker'))) {
+            $timePicker = $this->getAttribute('timePicker');
+        }
+
         //timeFormat: 'hh:mm'
         $js .= "<script>
             $(function() {
                 $('#$id').daterangepicker({
-                    format: 'YYYY-MM-DD HH:mm',
-                    timePicker: true,
+                    format: '$format',
+                    timePicker: $timePicker,
                     timePickerIncrement: 30,
                     timePicker12Hour: false,
                     $defaultDates
+                    $maxDate
+                    $minDate
                     ranges: {
                          '".addslashes(get_lang('Today'))."': [moment(), moment()],
                          '".addslashes(get_lang('ThisWeek'))."': [moment().weekday(1), moment().weekday(5)],

+ 16 - 9
main/inc/lib/sessionmanager.lib.php

@@ -1,6 +1,8 @@
 <?php
 /* For licensing terms, see /license.txt */
 
+use Chamilo\CoreBundle\Entity\SessionRelCourseRelUser;
+
 /**
  * Class SessionManager
  *
@@ -5620,8 +5622,11 @@ class SessionManager
         $courseSessionList = self::getCoursesListByCourseCoach($coachId);
         $sessionsByCoach = array();
         if (!empty($courseSessionList)) {
-            foreach ($courseSessionList as $courseSession) {
-                $sessionsByCoach[$courseSession['id_session']] = api_get_session_info($courseSession['id_session']);
+            foreach ($courseSessionList as $userCourseSubscription) {
+                $session = $userCourseSubscription->getSession();
+                $sessionsByCoach[$session->getId()] = api_get_session_info(
+                    $session->getId()
+                );
             }
         }
 
@@ -5777,13 +5782,15 @@ class SessionManager
      */
     public static function getCoursesListByCourseCoach($coachId)
     {
-        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
-        return  Database::select('*', $table, array(
-            'where' => array(
-                'user_id = ? AND ' => $coachId,
-                'status = ?' => 2,
-            ),
-        ));
+        $entityManager = Database::getManager();
+        $scuRepo = $entityManager->getRepository(
+            'ChamiloCoreBundle:SessionRelCourseRelUser'
+        );
+
+        return $scuRepo->findBy([
+            'user' => $coachId,
+            'status' => SessionRelCourseRelUser::STATUS_COURSE_COACH
+        ]);
     }
 
 	/**

+ 4 - 65
main/template/default/admin/teacher_time_report.tpl

@@ -18,25 +18,11 @@
                 $('#session').prop('selectedIndex', 0);
             });
 
-            $('#daterange').daterangepicker({
-                format: 'YYYY-MM-DD',
-                startDate: new Date('{{ filterStartDate }}'),
-                endDate: new Date('{{ filterEndDate }}'),
-                maxDate: new Date('{{ filterMaxDate }}'),
-                separator: ' / ',
-                locale: {
-                    applyLabel: "{{ 'Ok' | get_lang }}",
-                    cancelLabel: "{{ 'Cancel' | get_lang }}",
-                    fromLabel: "{{ 'From' | get_lang }}",
-                    toLabel: "{{ 'Until' | get_lang }}",
-                    customRangeLabel: "{{ 'CustomRange' | get_lang }}"
-                }
-            });
             $('#daterange').on('apply.daterangepicker', function (ev, picker) {
-                $('#from').val(picker.startDate.format('YYYY-MM-DD'));
-                $('#until').val(picker.endDate.format('YYYY-MM-DD'));
+                $('[name="from"]').val(picker.startDate.format('YYYY-MM-DD'));
+                $('[name="until"]').val(picker.endDate.format('YYYY-MM-DD'));
             }).on('cancel.daterangepicker', function (ev, picker) {
-                $('#daterange, #from, #until').val('');
+                $('#daterange, [name="from"], [name="until"]').val('');
             });
         });
     </script>
@@ -52,54 +38,7 @@
             </span>
         </div>
         <h1 class="page-header">{{ 'TeacherTimeReport' | get_lang }}</h1>
-        <form class="form-horizontal" method="post">
-            <div class="control-group">
-                <label class="control-label" for="course">{{ 'Course' | get_lang }}</label>
-                <div class="controls">
-                    <select name="course" id="course">
-                        <option value="0">{{ 'None' | get_lang }}</option>
-                        {% for course in courses %}
-                            <option value="{{ course.code }}" {{ (course.code == selectedCourse) ? 'selected' : '' }}>{{ course.title }}</option>
-                        {% endfor %}
-                    </select>
-                </div>
-            </div>
-            <div class="control-group">
-                <label class="control-label" for="session">{{ 'Session' | get_lang }}</label>
-                <div class="controls">
-                    <select name="session" id="session">
-                        <option value="0">{{ 'None' | get_lang }}</option>
-                        {% for session in sessions %}
-                            <option value="{{ session.id }}" {{ (session.id == selectedSession) ? 'selected' : '' }}>{{ session.name }}</option>
-                        {% endfor %}
-                    </select>
-                </div>
-            </div>
-            <div class="control-group">
-                <label class="control-label" for="inputPassword">{{ 'Teacher' | get_lang }}</label>
-                <div class="controls">
-                    <select name="teacher" id="teacher">
-                        <option value="0">{{ 'None' | get_lang }}</option>
-                        {% for teacher in courseCoaches %}
-                            <option value="{{ teacher.user_id }}" {{ (teacher.user_id == selectedTeacher) ? 'selected' : '' }}>{{ teacher.completeName }}</option>
-                        {% endfor %}
-                    </select>
-                </div>
-            </div>
-            <div class="control-group">
-                <label class="control-label" for="inputPassword">{{ 'Date' | get_lang }}</label>
-                <div class="controls">
-                    <input type="text" id="daterange"  value="{{ selectedFrom }} / {{ selectedUntil }}">
-                    <input type="hidden" id="from" name="from" value="{{ selectedFrom }}">
-                    <input type="hidden" id="until" name="until" value="{{ selectedUntil }}">
-                </div>
-            </div>
-            <div class="control-group">
-                <div class="controls">
-                    <button type="submit" class="btn btn-default"><i class="fa fa-search"></i> {{ 'Filter' | get_lang }}</button>
-                </div>
-            </div>
-        </form>
+        {{ form }}
         <h2 class="page-header">{{ reportTitle }} <small>{{ reportSubTitle }}</small></h2>
         <table class="table">
             <thead>