} else { $courseselect = $course->id; } if ($date) { $daystart = usergetmidnight($date); } else { $daystart = usergetmidnight(time()); } $dayfinish = $daystart + 86400; $hours = array(); for ($i = 0; $i <= 23; $i++) { $logs[$i] = 0; $hour = $daystart + $i * 3600; $hours[$i] = $i; } $rawlogs = report_log_userday($user->id, $courseselect, $daystart, $logreader); if (empty($rawlogs)) { return; } foreach ($rawlogs as $rawlog) { $logs[$rawlog->hour] = $rawlog->num; } $graph = new graph(750, 400); $a = new stdClass(); $a->coursename = format_string($course->shortname, true, array('context' => $coursecontext)); $a->username = fullname($user, true); $graph->parameter['title'] = get_string("hitsoncoursetoday", "", $a); $graph->x_data = $hours; $graph->y_data['logs'] = $logs; $graph->y_order = array('logs'); if (!empty($CFG->preferlinegraphs)) {
/** * Fetch logs of the current day and structure in series and labels to be sent to Chart API. * * @param stdClass $course the course object * @param stdClass $user user object * @param int $date A time of a day (in GMT). * @param string $logreader the log reader where the logs are. * @return array $logs structured array to be sent to chart API, split in two indexes (series and labels). */ function report_log_usertoday_data($course, $user, $date, $logreader) { $site = get_site(); $logs = []; if ($course->id == $site->id) { $courseselect = 0; } else { $courseselect = $course->id; } if ($date) { $daystart = usergetmidnight($date); } else { $daystart = usergetmidnight(time()); } for ($i = 0; $i <= 23; $i++) { $hour = $daystart + $i * 3600; $logs['series'][$i] = 0; $logs['labels'][$i] = userdate($hour, "%H:00"); } $rawlogs = report_log_userday($user->id, $courseselect, $daystart, $logreader); foreach ($rawlogs as $rawlog) { if (isset($logs['labels'][$rawlog->hour])) { $logs['series'][$rawlog->hour] = $rawlog->num; } } return $logs; }