|
351 | 351 | 'report=users_active' => get_lang('Users statistics'),
|
352 | 352 | 'report=users_online' => get_lang('Users online'),
|
353 | 353 | 'report=new_user_registrations' => get_lang('New users registrations'),
|
| 354 | + 'report=subscription_by_day' => get_lang('Course/Session subscriptions by day'), |
354 | 355 | ],
|
355 | 356 | get_lang('System') => [
|
356 | 357 | 'report=activities' => get_lang('Important activities'),
|
|
370 | 371 | $content = '';
|
371 | 372 |
|
372 | 373 | switch ($report) {
|
| 374 | + case 'subscription_by_day': |
| 375 | + $form = new FormValidator('subscription_by_day', 'get', api_get_self()); |
| 376 | + $form->addDateRangePicker('daterange', get_lang('Date range'), true, [ |
| 377 | + 'format' => 'YYYY-MM-DD', |
| 378 | + 'timePicker' => 'false', |
| 379 | + 'validate_format' => 'Y-m-d' |
| 380 | + ]); |
| 381 | + $form->addHidden('report', 'subscription_by_day'); |
| 382 | + $form->addButtonSearch(get_lang('Search')); |
| 383 | + $validated = $form->validate() || isset($_REQUEST['daterange']); |
| 384 | + |
| 385 | + if ($validated) { |
| 386 | + $values = $form->getSubmitValues(); |
| 387 | + $dateStart = Security::remove_XSS($values['daterange_start']); |
| 388 | + $dateEnd = Security::remove_XSS($values['daterange_end']); |
| 389 | + |
| 390 | + $dates = []; |
| 391 | + $period = new DatePeriod( |
| 392 | + new DateTime($dateStart), |
| 393 | + new DateInterval('P1D'), |
| 394 | + (new DateTime($dateEnd))->modify('+1 day') |
| 395 | + ); |
| 396 | + foreach ($period as $date) { |
| 397 | + $key = $date->format('Y-m-d'); |
| 398 | + $dates[$key] = [ |
| 399 | + 'subscriptions' => 0, |
| 400 | + 'unsubscriptions' => 0, |
| 401 | + ]; |
| 402 | + } |
| 403 | + |
| 404 | + $subscriptions = Statistics::getSubscriptionsByDay($dateStart, $dateEnd); |
| 405 | + foreach ($subscriptions as $item) { |
| 406 | + $dates[$item['date']]['subscriptions'] = $item['count']; |
| 407 | + } |
| 408 | + |
| 409 | + $unsubscriptions = Statistics::getUnsubscriptionsByDay($dateStart, $dateEnd); |
| 410 | + foreach ($unsubscriptions as $item) { |
| 411 | + $dates[$item['date']]['unsubscriptions'] = $item['count']; |
| 412 | + } |
| 413 | + |
| 414 | + $labels = array_keys($dates); |
| 415 | + $subscriptionsData = array_map(fn($v) => $v['subscriptions'] ?? 0, $dates); |
| 416 | + $unsubscriptionsData = array_map(fn($v) => $v['unsubscriptions'] ?? 0, $dates); |
| 417 | + |
| 418 | + $chartData = [ |
| 419 | + 'labels' => $labels, |
| 420 | + 'datasets' => [ |
| 421 | + ['label' => get_lang('Subscriptions'), 'data' => $subscriptionsData], |
| 422 | + ['label' => get_lang('Unsubscriptions'), 'data' => $unsubscriptionsData], |
| 423 | + ] |
| 424 | + ]; |
| 425 | + |
| 426 | + $htmlHeadXtra[] = Statistics::getJSChartTemplateWithData( |
| 427 | + $chartData, |
| 428 | + 'bar', |
| 429 | + 'title: { text: "'.get_lang('Subscriptions vs Unsubscriptions by day').'", display: true }', |
| 430 | + 'subscriptions_chart', |
| 431 | + true |
| 432 | + ); |
| 433 | + |
| 434 | + $content .= '<canvas id="subscriptions_chart"></canvas>'; |
| 435 | + $table = new HTML_Table(['class' => 'table table-hover table-striped table-bordered data_table']); |
| 436 | + |
| 437 | + $table->setHeaderContents(0, 0, get_lang('Date')); |
| 438 | + $table->setHeaderContents(0, 1, get_lang('Subscriptions')); |
| 439 | + $table->setHeaderContents(0, 2, get_lang('Unsubscriptions')); |
| 440 | + |
| 441 | + $row = 1; |
| 442 | + foreach ($dates as $date => $values) { |
| 443 | + $table->setCellContents($row, 0, $date); |
| 444 | + $table->setCellContents($row, 1, $values['subscriptions'] ?? 0); |
| 445 | + $table->setCellContents($row, 2, $values['unsubscriptions'] ?? 0); |
| 446 | + $row++; |
| 447 | + } |
| 448 | + $content .= $table->toHtml(); |
| 449 | + } |
| 450 | + |
| 451 | + $content = $form->returnForm() . $content; |
| 452 | + break; |
373 | 453 | case 'session_by_date':
|
374 | 454 | $sessions = [];
|
375 | 455 | if ($validated) {
|
|
0 commit comments