-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathdata.php
More file actions
279 lines (240 loc) · 13.6 KB
/
data.php
File metadata and controls
279 lines (240 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
# Get your app keys on https://stepik.org/oauth2/applications (Client Type = confidential; Authorization Grant Type: authorization code)
define('OAUTH2_CLIENT_ID', '...');
define('OAUTH2_CLIENT_SECRET', '...');
# OAuth2 flow based on https://gist.github.com/aaronpk/3612742
$authorizeURL = 'https://stepik.org/oauth2/authorize/';
$tokenURL = 'https://stepik.org/oauth2/token/';
$apiURLBase = 'https://stepik.org/api/';
$scriptPath = 'http://vyahhi.net/data/'; # if you want more general than use = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
date_default_timezone_set('UTC');
session_start();
if (get('action') == 'logout') {
session_start();
session_unset();
session_destroy();
header('Location: ' . $scriptPath);
die();
}
# Start the login process by sending the user to Github's authorization page
if (get('action') == 'login') {
# Generate a random hash and store in the session for security
$_SESSION['state'] = hash('sha256', microtime(TRUE) . rand() . $_SERVER['REMOTE_ADDR']);
unset($_SESSION['access_token']);
$params = array(
'client_id' => OAUTH2_CLIENT_ID,
'redirect_uri' => $scriptPath,
'scope' => 'read',
'state' => $_SESSION['state'],
'response_type' => 'code'
);
# Redirect the user to Github's authorization page
header('Location: ' . $authorizeURL . '?' . http_build_query($params));
die();
}
# When Github redirects the user back here, there will be a "code" and "state" parameter in the query string
if (get('code')) {
# Verify the state matches our stored state
if (!get('state') || $_SESSION['state'] != get('state')) {
header('Location: ' . $scriptPath);
die();
}
# Exchange the auth code for a token
$token = apiRequest($tokenURL, array(
'client_id' => OAUTH2_CLIENT_ID,
'client_secret' => OAUTH2_CLIENT_SECRET,
'redirect_uri' => $scriptPath,
'state' => $_SESSION['state'],
'code' => get('code'),
'grant_type' => 'authorization_code'
));
$_SESSION['access_token'] = $token->access_token;
header('Location: ' . $scriptPath);
}
if (session('access_token')) {
# =========
# WELCOME!
# =========
$stepiks = apiRequest($apiURLBase . 'stepics/1'); # should be stepic with "c"!
$user_id = $stepiks->profiles[0]->id;
$first_name = $stepiks->profiles[0]->first_name;
$last_name = $stepiks->profiles[0]->last_name;
echo '<html><head>';
echo '<title>Личный кабинет онлайн-программы «Анализ данных»</title>';
echo '<link rel="stylesheet" href="https://stepik.org/static/frontend/cli-build/stepic.css">';
echo '<link rel="shortcut icon" href="https://static.tildacdn.com/tild6662-3335-4663-b334-643039313631/data_fav.ico" type="image/x-icon" />';
echo '</head><body style="padding:20px">';
echo '<h3>Личный кабинет онлайн-программы «<a href="http://data.stepik.org">Анализ данных</a>» на <a href="https://stepik.org">Stepik.org</a></h3>';
echo '<h4>Добро пожаловать, <a href="https://stepik.org/users/' . $user_id . '">' . $first_name . ' ' . $last_name . '</a>!</h4>';
echo '<hr>';
# =============
# COURSES LIST
# =============
$courses = array(
array('id' => 217, 'required' => True, 'sections' => array(2, 4), 'need_to_pass' => 85, 'exams' => 1536), # Алгоритмы: теория и практика. Методы
array('id' => 129, 'required' => True, 'sections' => array(1, 2, 3), 'need_to_pass' => 85, 'exams' => 1425), # Анализ данных в R
array('id' => 724, 'required' => True, 'sections' => array(1, 2, 3), 'need_to_pass' => 85, 'exams' => null), # Анализ данных в R. Часть 2
array('id' => 1240, 'required' => True, 'sections' => array(1, 2, 5, 6), 'need_to_pass' => 85, 'exams' => 1534), # Введение в базы данных
array('id' => 253, 'required' => True, 'sections' => array(1, 3, 4, 5), 'need_to_pass' => 85, 'exams' => 1535), # Введение в архитектуру ЭВМ. Элементы операционных систем.
array('id' => 902, 'required' => True, 'sections' => array(1, 2, 3, 4), 'need_to_pass' => 90, 'exams' => 1424), # Дискретная математика
array('id' => 73, 'required' => True, 'sections' => array(1, 2, 3), 'need_to_pass' => 90, 'exams' => 1427), # Введение в Linux
array('id' => 497, 'required' => True, 'sections' => array(1, 2, 3), 'need_to_pass' => 85, 'exams' => 1426), # Основы программирования на R
array('id' => 76, 'required' => True, 'sections' => array(1, 2, 3), 'need_to_pass' => 90, 'exams' => 1423), # Основы статистики
array('id' => 524, 'required' => True, 'sections' => array(1, 2, 3), 'need_to_pass' => 85, 'exams' => null), # Основы статистики. Часть 2
array('id' => 67, 'required' => True, 'sections' => array(1, 2, 3), 'need_to_pass' => 90, 'exams' => 1428), # Программирование на Python
array('id' => 512, 'required' => True, 'sections' => array(1, 2, 3), 'need_to_pass' => 90, 'exams' => null), # Python: основы и применение
array('id' => 1612, 'required' => True, 'sections' => array(2, 3, 4), 'need_to_pass' => 85, 'exams' => 2458), # Управление вычислениями
array('id' => 217, 'required' => False, 'sections' => array(6, 8), 'need_to_pass' => 85, 'exams' => 1537), # Алгоритмы: теория и практика. Методы
array('id' => 253, 'required' => False, 'sections' => array(2, 6, 7), 'need_to_pass' => 85, 'exams' => 1538), # Введение в архитектуру ЭВМ. Элементы операционных систем.
array('id' => 1240, 'required' => False, 'sections' => array(3, 4, 7), 'need_to_pass' => 85, 'exams' => 1544), # Введение в базы данных
array('id' => 95, 'required' => False, 'sections' => array(1, 2, 3, 4), 'need_to_pass' => 85, 'exams' => 1429), # Введение в математический анализ
array('id' => 401, 'required' => False, 'sections' => array(1, 2, 3, 4), 'need_to_pass' => 85, 'exams' => 1432), # Нейронные сети
array('id' => 7, 'required' => False, 'sections' => array(1, 2, 3, 4, 5, 6), 'need_to_pass' => 85, 'exams' => 1430), # Программирование на языке C++
array('id' => 187, 'required' => False, 'sections' => array(1, 2, 3, 4, 5, 6), 'need_to_pass' => 85, 'exams' => 1431), # Java. Базовый курс
array('id' => 2152, 'required' => False, 'sections' => array(1, 2, 3), 'need_to_pass' => 85, 'exams' => 2387) # Основы статистики. ч.3
# TO ADD WHEN READY: Machine Learning (not required)
);
echo '<h4>Курсы программы (<a href="https://static.tildacdn.com/tild3430-6633-4163-b839-326632353161/schemefix.png">взаимосвязи</a>):</h4>';
echo '<table border=1 cellspacing=0 cellpadding=10>';
echo '<tr><th colspan=6><b>Обязательные курсы:</b></th></tr>';
echo '<tr><th>Курс</th><th>Количество<br>модулей</th><th>Модули</th><th>Ваши баллы</th><th>Ваш процент</th><th>Допуск к экзамену</th></tr>';
$previousCourse = null;
foreach ($courses as $course) {
$info = apiRequest($apiURLBase . 'courses/' . $course['id'])->courses[0];
$section_ids = array();
foreach ($course['sections'] as $i) {
$section_ids[] = $info->sections[$i - 1];
}
$sections = apiRequest($apiURLBase . 'sections?ids[]=' . implode('&ids[]=', $section_ids))->sections;
$progress_ids = array();
foreach ($sections as $section) {
$progress_ids[] = $section->progress;
}
$progresses = apiRequest($apiURLBase . 'progresses?ids[]=' . implode('&ids[]=', $progress_ids))->progresses;
$score = 0;
$cost = 0;
foreach ($progresses as $progress) {
$score += $progress->score;
$cost += $progress->cost;
}
$percentage = $cost ? round(100 * $score / $cost, 2) : 0;
if ($percentage >= $course['need_to_pass']) {
try {
$exam = apiRequest($apiURLBase . 'courses/' . $course['exams'])->courses[0];
$progress = apiRequest($apiURLBase . 'progresses/' . $exam->progress)->progresses[0];
$exam_message = $progress->score . ' / ' . $progress->cost;
} catch (Exception $e) {
$exam_message = '<a href="https://stepik.org/courses/' . $exam->slug . '">можно начать</a>';
}
} else {
$exam_message = 'нет доступа (нужно ' . $course['need_to_pass'] . '% за курс)';
}
if ($previousCourse['required'] and !$course['required']) {
echo '<tr><th colspan=6><b>Курсы по выбору:</b></th></tr>';
echo '<tr><th>Курс</th><th>Количество<br>модулей</th><th>Модули</th><th>Ваши баллы</th><th>Ваш процент</th><th>Экзамен</th></tr>';
}
echo '<tr>';
echo '<td><a href="https://stepik.org/course/' . $info->slug . '">' . $info->title . '</a></td>';
echo '<td>' . count($course['sections']) . '</td>';
echo '<td>' . implode(", ", $course['sections']) . '</td>';
if ($cost) {
echo '<td>' . $score . ' / ' . $cost . '</td>';
echo '<td>' . $percentage . '%</td>';
} else {
echo '<td colspan=2>Запишитесь на курс</td>';
}
echo '<td>' . $exam_message . '</td>';
echo '</tr>';
$previousCourse = $course;
}
echo '</table>';
# =============
# PAYMENTS LOG
# =============
$subscription_plans = array(10 => 0, 1999 => 1, 5397 => 3, 6000 => 3, 9595 => 6, 16791 => 12);
$format = 'M d, Y (H:i)';
$page = 1;
$payments_list = array();
$payment_start = 0;
$payment_valid_until = 0;
do {
$payments = apiRequest($apiURLBase . 'payments?page=' . $page);
foreach ($payments->payments as $payment) {
if ($payment->destination_type != 'au_data') {
continue;
}
$payment_date = strtotime($payment->payment_date);
$payment_start = max($payment_date, $payment_start);
$months = $subscription_plans[intval($payment->amount)];
$payment_valid_until = paymentDue($payment_start, $months);
$payments_list[] = array(
intval($payment->amount),
date($format, $payment_date),
date($format, $payment_start),
date($format, $payment_valid_until)
);
$payment_start = $payment_valid_until;
}
$page += 1;
} while ($payments->meta->has_next);
if ($payment_valid_until >= time()) {
$color = ($payment_valid_until - time() >= 60 * 60 * 24 * 7) ? "#66cc66" : "cccc00"; # yellow if less than one week left
echo '<h4>Ваша подписка на программу <span style="background-color:' . $color . '"> активна до ' . date($format, $payment_valid_until) . ' </span></h4>';
} else {
echo '<h4>Ваша подписка на программу <span style="background-color:#ff6666"> не активна </span></h4>';
}
echo '<h4>Ваши платежи:</h4>';
echo '<table border=1 cellspacing=0 cellpadding=10>';
echo '<tr><th>Сумма (руб)</th><th>Дата и время платежа (UTC)</th><th>Действует от</th><th>Действует до</th></tr>';
foreach (array_reverse($payments_list) as $payment) {
echo '<tr><td>';
echo implode('</td><td>', $payment);
echo '</td></tr>';
}
echo '</table>';
# LOGOUT
echo '<p><a href="?action=logout">[Выйти из кабинета]</a></p>';
echo '</body></html>';
} else {
echo '<h3>Личный кабинет онлайн-программы «<a href="http://data.stepik.org">Анализ данных</a>» на <a href="https://stepik.org">Stepik.org</a></h3>';
echo '<h3>Вы не вошли</h3>';
echo '<p><a href="?action=login">Войти через Stepik.org</a></p>';
}
# =================
# HELPER FUNCTIONS
# =================
function apiRequest($url, $post = FALSE, $headers = array())
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if ($post)
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$headers[] = 'Accept: application/json';
if (session('access_token'))
$headers[] = 'Authorization: Bearer ' . session('access_token');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
return json_decode($response);
}
function get($key, $default = NULL)
{
return array_key_exists($key, $_GET) ? $_GET[$key] : $default;
}
function session($key, $default = NULL)
{
return array_key_exists($key, $_SESSION) ? $_SESSION[$key] : $default;
}
# Based on http://stackoverflow.com/a/24014541/92396 but fixed and simplified:
function paymentDue($timestamp, $months)
{
$date = new DateTime('@' . $timestamp);
$next = clone $date;
$next->modify('last day of +' . $months . ' month');
if ($date->format('d') > $next->format('d')) {
$add = $date->diff($next);
} else {
$add = new DateInterval('P' . $months . 'M');
}
$newDate = $date->add($add);
return $newDate->getTimestamp();
}