From d4252007682186e7b1f1fd87dcf65e9521bf92f1 Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Mon, 16 Jul 2018 15:13:30 +0200 Subject: [PATCH] WIP Lp calendar - Fix php warning, errors, format code, fix query. BT#14608 --- main/admin/usergroup_users.php | 3 +- main/inc/ajax/model.ajax.php | 2 +- main/inc/lib/tracking.lib.php | 37 ++++++++++++------------- main/inc/lib/usergroup.lib.php | 17 +++++++++--- plugin/lp_calendar/LpCalendarPlugin.php | 29 ++++++++++--------- plugin/lp_calendar/ajax.php | 10 ++++--- plugin/lp_calendar/calendar.php | 4 --- plugin/lp_calendar/start.php | 2 -- 8 files changed, 53 insertions(+), 51 deletions(-) diff --git a/main/admin/usergroup_users.php b/main/admin/usergroup_users.php index 29588f6a35..fb8580227e 100644 --- a/main/admin/usergroup_users.php +++ b/main/admin/usergroup_users.php @@ -34,7 +34,7 @@ switch ($action) { case 'add_calendar': $calendars = LpCalendarPlugin::getCalendars(0, 1000, ''); if (empty($calendars)) { - echo Display::return_message(get_lang('Nodata')); + echo Display::return_message(get_lang('NoData'), 'warning'); exit; } $userInfo = api_get_user_info($userId); @@ -132,7 +132,6 @@ $column_model = [ ], ]; - if (api_get_plugin_setting('lp_calendar', 'enabled') === 'true') { $columns = [ get_lang('Name'), diff --git a/main/inc/ajax/model.ajax.php b/main/inc/ajax/model.ajax.php index ca62750267..0cf9f5b5e1 100755 --- a/main/inc/ajax/model.ajax.php +++ b/main/inc/ajax/model.ajax.php @@ -797,7 +797,7 @@ switch ($action) { 'name', 'calendar', 'examn', - 'time_sent', + 'time_spent', 'lp_day_completed', 'days_diff', 'actions', diff --git a/main/inc/lib/tracking.lib.php b/main/inc/lib/tracking.lib.php index 539442cec2..f68bf89ede 100755 --- a/main/inc/lib/tracking.lib.php +++ b/main/inc/lib/tracking.lib.php @@ -1624,18 +1624,18 @@ class Tracking $courseId, $session_id = 0 ) { - $courseId = intval($courseId); + $courseId = (int) $courseId; if (empty($courseId) || empty($user_id)) { return 0; } - $session_id = intval($session_id); + $session_id = (int) $session_id; if (is_array($user_id)) { $user_id = array_map('intval', $user_id); $conditionUser = " AND user_id IN (".implode(',', $user_id).") "; } else { - $user_id = intval($user_id); + $user_id = (int) $user_id; $conditionUser = " AND user_id = $user_id "; } @@ -4627,6 +4627,7 @@ class Tracking * @param string $extra_params * @param bool $show_courses * @param bool $showAllSessions + * @param bool $returnArray * * @return string */ @@ -4666,8 +4667,8 @@ class Tracking $trackingColumns = $trackingColumnsConfig; } - $user_id = intval($user_id); - $session_id = intval($session_id); + $user_id = (int) $user_id; + $session_id = (int) $session_id; $urlId = api_get_current_access_url_id(); if (api_is_multiple_url_enabled()) { @@ -4680,7 +4681,7 @@ class Tracking WHERE cu.user_id = $user_id AND relation_type<> ".COURSE_RELATION_TYPE_RRHH." AND - access_url_id = ".$urlId." + access_url_id = $urlId ORDER BY title"; } else { $sql = "SELECT c.id, c.code, title @@ -4688,7 +4689,7 @@ class Tracking INNER JOIN $tbl_course c ON (c_id = c.id) WHERE u.user_id= $user_id AND - relation_type<>".COURSE_RELATION_TYPE_RRHH." + relation_type <> ".COURSE_RELATION_TYPE_RRHH." ORDER BY title"; } @@ -4700,11 +4701,11 @@ class Tracking $courseIdList[] = $row['id']; } - $orderBy = " ORDER BY name "; + $orderBy = ' ORDER BY name '; $extraInnerJoin = null; if (SessionManager::orderCourseIsEnabled() && !empty($session_id)) { - $orderBy = " ORDER BY s.id, position "; + $orderBy = ' ORDER BY s.id, position '; $tableSessionRelCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); $extraInnerJoin = " INNER JOIN $tableSessionRelCourse src ON (cu.c_id = src.c_id AND src.session_id = $session_id) "; @@ -4981,14 +4982,14 @@ class Tracking // Exercise is not necessary to be visible to show results check the result_disable configuration instead //$visible_return = $exercise_obj->is_visible(); if ($exercise_data['results_disabled'] == 0 || $exercise_data['results_disabled'] == 2) { - $best_average = intval( + $best_average = (int) ExerciseLib::get_best_average_score_by_exercise( $exercise_data['id'], $course_data['real_id'], $my_session_id, $user_count ) - ); + ; $exercise_graph_list[] = $best_average; $all_exercise_graph_list[] = $best_average; @@ -5175,7 +5176,7 @@ class Tracking // Checking selected session. if (isset($_GET['session_id'])) { - $session_id_from_get = intval($_GET['session_id']); + $session_id_from_get = (int) $_GET['session_id']; $session_data = $course_in_session[$session_id_from_get]; $course_list = $session_data['course_list']; @@ -5411,13 +5412,12 @@ class Tracking } $html .= ''; } + } - $pluginCalendar = api_get_plugin_setting('lp_calendar', 'enabled') === 'true'; - if ($pluginCalendar) { - $course_in_session[0] = $courseIdList; - $plugin = LpCalendarPlugin::create(); - $html .= LpCalendarPlugin::getUserStatsPanel($user_id, $course_in_session); - } + $pluginCalendar = api_get_plugin_setting('lp_calendar', 'enabled') === 'true'; + if ($pluginCalendar) { + $course_in_session[0] = $courseIdList; + $html .= LpCalendarPlugin::getUserStatsPanel($user_id, $course_in_session); } return $html; @@ -6862,7 +6862,6 @@ class Tracking } } } - } } diff --git a/main/inc/lib/usergroup.lib.php b/main/inc/lib/usergroup.lib.php index df63cd3910..f965eadb11 100755 --- a/main/inc/lib/usergroup.lib.php +++ b/main/inc/lib/usergroup.lib.php @@ -129,11 +129,20 @@ class UserGroup extends Model true ); - $stats = LpCalendarPlugin::getUserStats($userId, $courseAndSessionList) + $stats = LpCalendarPlugin::getUserStats($userId, $courseAndSessionList); + $data['examn'] = 'examn'; - $data['time_spent'] = 'time_spent'; - $data['lp_day_completed'] = 'lp_day_completed'; - $data['days_diff'] = 'days_diff'; + $totalTime = 0; + foreach ($courseAndSessionList as $sessionId => $course) { + foreach ($course as $courseId) { + $totalTime += Tracking::get_time_spent_on_the_course($userId, $courseId,$sessionId); + } + } + + $data['time_spent'] = api_time_to_hms($totalTime); + + $data['lp_day_completed'] = $stats['completed']; + $data['days_diff'] = $stats['diff']; } diff --git a/plugin/lp_calendar/LpCalendarPlugin.php b/plugin/lp_calendar/LpCalendarPlugin.php index db121411de..909f6b4745 100644 --- a/plugin/lp_calendar/LpCalendarPlugin.php +++ b/plugin/lp_calendar/LpCalendarPlugin.php @@ -1,25 +1,20 @@ hasPersonalEvents = true; $version = '0.1'; $author = 'Julio Montoya'; parent::__construct($version, $author, ['enabled' => 'boolean']); @@ -33,12 +28,12 @@ class LpCalendarPlugin extends Plugin return [ //self::EVENT_TYPE_FREE => 'green', self::EVENT_TYPE_TAKEN => 'red', - self::EVENT_TYPE_EXAM => 'yellow' + self::EVENT_TYPE_EXAM => 'yellow', ]; } /** - * Get the class instance + * Get the class instance. * * @return $this */ @@ -50,7 +45,7 @@ class LpCalendarPlugin extends Plugin } /** - * Get the plugin directory name + * Get the plugin directory name. */ public function get_name() { @@ -58,7 +53,7 @@ class LpCalendarPlugin extends Plugin } /** - * Install the plugin. Setup the database + * Install the plugin. Setup the database. */ public function install() { @@ -121,7 +116,7 @@ class LpCalendarPlugin extends Plugin } /** - * uninstall plugin. Clear the database + * Uninstall the plugin. */ public function uninstall() { @@ -389,7 +384,7 @@ class LpCalendarPlugin extends Plugin * @param int $type * @param bool $getCount * - * @return array + * @return array|int */ public static function getUserEvents($userId, $start, $end, $type = 0, $getCount = false) { @@ -400,6 +395,10 @@ class LpCalendarPlugin extends Plugin return self::getCalendarsEventsByDate($calendar, $start, $end, $type, $getCount); } + if ($getCount) { + return 0; + } + return []; } @@ -667,7 +666,7 @@ class LpCalendarPlugin extends Plugin $userId, strtotime(date('Y-01-01')), time(), - LpCalendarPlugin::EVENT_TYPE_TAKEN, + self::EVENT_TYPE_TAKEN, true ); diff --git a/plugin/lp_calendar/ajax.php b/plugin/lp_calendar/ajax.php index 1604844088..1c719fe515 100644 --- a/plugin/lp_calendar/ajax.php +++ b/plugin/lp_calendar/ajax.php @@ -1,4 +1,5 @@ $calendarId, 'start_date' => $startDate, 'end_date' => $startDate, - 'type' => LpCalendarPlugin::EVENT_TYPE_TAKEN + 'type' => LpCalendarPlugin::EVENT_TYPE_TAKEN, ]; Database::insert('learning_calendar_events', $params); } break; case 'get_events': - $sql = "SELECT * FROM learning_calendar_events"; + $sql = "SELECT * FROM learning_calendar_events + WHERE calendar_id = $calendarId "; $result = Database::query($sql); $list = []; while ($row = Database::fetch_array($result, 'ASSOC')) { $list[] = [ 'start_date' => $row['start_date'], 'end_date' => $row['start_date'], - 'color' => $eventTypeList[$row['type']] + 'color' => $eventTypeList[$row['type']], ]; } echo json_encode($list); break; -} \ No newline at end of file +} diff --git a/plugin/lp_calendar/calendar.php b/plugin/lp_calendar/calendar.php index 804bdfc5cf..9defcf23c4 100644 --- a/plugin/lp_calendar/calendar.php +++ b/plugin/lp_calendar/calendar.php @@ -35,10 +35,6 @@ $plugin = LpCalendarPlugin::create(); $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : ''; $formToString = ''; -switch ($action) { - -} - $template = new Template(); $actionLeft = Display::url( diff --git a/plugin/lp_calendar/start.php b/plugin/lp_calendar/start.php index c85b1f3a4a..852dc62c8c 100644 --- a/plugin/lp_calendar/start.php +++ b/plugin/lp_calendar/start.php @@ -1,7 +1,6 @@