$col_names = array(); foreach ($rows as $n => $row) { $arr = str_getcsv($row); if (empty($col_names)) { $col_names = $arr; } else { $line = array(); foreach ($arr as $i => $val) { $line[$col_names[$i]] = $val; } // Clean up data & fill in the blank if (!isset($line['distance_meters']) || empty($line['distance_meters'])) { $line['distance_meters'] = convertDistanceToMeters($line['distance']); } if (!isset($line['time_seconds']) || empty($line['time_seconds'])) { $line['time_seconds'] = convertTimeToSeconds($line['time']); } $line['date'] = cleanupDate($line['date']); $line['pace'] = formatPace($line['time_seconds'], $line['distance_meters']); $races_arr[] = $line; } } // var_dump($races_arr); // debug $json_file = explode(".", $csv_file)[0] . '.json'; if (!file_exists($json_file)) { $json_fh = fopen($json_file, 'w'); fwrite($json_fh, json_encode($races_arr)); echo count($races_arr) . " lines written to {$json_file}\n"; } else { throw new InvalidArgumentException("JSON output file already exists: {$json_file}."); }
public function getUsersActiveTimeInLesson($constraints = array()) { $lessonUsers = array(); $constraints = array('return_objects' => false) + (array) $constraints; foreach ($this->getLessonUsers($constraints) as $key => $value) { $lessonUsers[$key] = 0; } $result = eF_getTableData("users_to_content", "users_LOGIN, sum(total_time) as total_time", "lessons_ID=" . $this->lesson['id'], "", "users_LOGIN"); foreach ($result as $value) { if (isset($lessonUsers[$value['users_LOGIN']])) { $lessonUsers[$value['users_LOGIN']] = $value['total_time']; } } //Calculate SCORM times, as these are not counted by the system $result = eF_getTableData("scorm_data", "users_LOGIN, total_time", "content_ID in (select id from content where lessons_ID=" . $this->lesson['id'] . " and active=1 and (ctg_type='scorm' or ctg_type='scorm_test'))"); foreach ($result as $value) { if (isset($lessonUsers[$value['users_LOGIN']])) { $value['total_time'] = convertTimeToSeconds($value['total_time']); $lessonUsers[$value['users_LOGIN']] += $value['total_time']; } } // ct.time_spent < 2*60*60 added in order to prevent too big values because of leaving test (closing browser etc) $result = eF_getTableData("completed_tests ct, tests t", "users_LOGIN, sum(ct.time_spent) as total_time", "ct.time_spent < 2*60*60 and ct.status!='deleted' and ct.tests_ID=t.id and t.lessons_ID=" . $this->lesson['id'], "", "users_LOGIN"); foreach ($result as $value) { if (isset($lessonUsers[$value['users_LOGIN']])) { $lessonUsers[$value['users_LOGIN']] += $value['total_time']; } } return $lessonUsers; }
public static function upgradeFromUsersOnline() { //Check if the users_online table actually exists. If not, then there is no need for upgrade try { $result = $GLOBALS['db']->GetAll("describe users_online"); } catch (Exception $e) { return false; } //Get the first log entry $result = eF_getTableData("logs", "timestamp", "", "timestamp", "", "1"); $dateParts = getdate($result[0]['timestamp']); $firstDay = mktime(0, 0, 0, $dateParts['mon'], $dateParts['mday'], $dateParts['year']); //Delete old upgrade attempts eF_deleteTableData("user_times"); //Get system times for users $timeNow = time(); for ($t = $firstDay; $t <= $timeNow - 86400; $t += 86400) { $userTimes[$t] = EfrontTimes::getDeprecatedUserTimesPerDay(array('from' => $t, 'to' => $t + 86400)); } foreach ($userTimes as $timestamp => $users) { foreach ($users as $login => $times) { $fields = array('session_timestamp' => $timestamp, 'session_id' => 'from 3.6.6 upgrade', 'session_expired' => 1, 'users_LOGIN' => $login, 'timestamp_now' => $timestamp, 'time' => $times['total_seconds'], 'lessons_ID' => NULL, 'courses_ID' => NULL, 'entity' => 'system', 'entity_ID' => 0); eF_insertTableData("user_times", $fields); } } //Get times spent in SCORM $scormTimes = eF_getTableData("scorm_data sd, content c", "sd.total_time, sd.users_LOGIN, c.lessons_ID", "c.id=sd.content_ID"); $scormSeconds = array(); foreach ($scormTimes as $value) { if (!isset($scormSeconds[$value['lessons_ID']][$value['users_LOGIN']])) { $scormSeconds[$value['lessons_ID']][$value['users_LOGIN']] = 0; } $scormSeconds[$value['lessons_ID']][$value['users_LOGIN']] += convertTimeToSeconds($value['total_time']); } //Get times spent in lessons, as reported by system function $userTimes = EfrontStats::getUsersTimeAll(); foreach ($userTimes as $lessonId => $users) { foreach ($users as $login => $user) { if ($user['total_seconds'] || $scormSeconds[$lessonId][$login]) { //If SCO times are bigger than lesson times, then use SCO times if ($user['total_seconds'] < $scormSeconds[$lessonId][$login]) { $user['total_seconds'] = $scormSeconds[$lessonId][$login]; } $fields = array('session_timestamp' => time(), 'session_id' => 'from 3.6.6 upgrade', 'session_expired' => 1, 'users_LOGIN' => $login, 'timestamp_now' => time(), 'time' => $user['total_seconds'], 'lessons_ID' => $lessonId, 'courses_ID' => NULL, 'entity' => 'lesson', 'entity_ID' => $lessonId); eF_insertTableData("user_times", $fields); } } } $GLOBALS['db']->Execute("drop table users_online"); }