Мне нужно создать расширение блока Moodle, которое будет отображать
пользователю время, проведенное им на курсах. Данные берутся из
Attendance Register. Так вот, я пытаюсь рассчитать время, но все равно
показывает 0, хотя Attendance Register указывает, что я провел 24
минуты. Что я делаю не так, почему вместо 24 мне показывает 0? Вот мой
код:
<?php
defined('MOODLE_INTERNAL') || die();
class block_time_spent extends block_base {
public function init() {
$this->title = get_string('pluginname', 'block_time_spent');
}
public function get_content() {
global $USER, $DB, $OUTPUT;
if ($this->content !== null) {
return $this->content;
}
$this->content = new stdClass();
$filterCategory = optional_param('filterCategory', null, PARAM_INT);
$courses = enrol_get_all_users_courses($USER->id);
$chart = new \core\chart_pie();
$labels = [];
$timeSpentData = [];
foreach ($courses as $course) {
if (has_capability('mod/attendance:view', context_course::instance($course->id))) {
if ($filterCategory !== null && $course->category != $filterCategory) {
continue;
}
$attendanceregisterRecord = $DB->get_record('attendanceregister', array('course' => $course->id));
if ($attendanceregisterRecord) {
$totalDuration = 0;
$sessions = $DB->get_records('attendanceregister_session', array('register' => $attendanceregisterRecord->id, 'userid' => $USER->id));
if ($sessions) {
foreach ($sessions as $session) {
$totalDuration += $session->duration;
}
}
if ($filterCategory !== null) {
$label = $course->fullname;
} else {
$category = \core_course_category::get($course->category, IGNORE_MISSING);
$label = $category ? $category->get_formatted_name() : 'Unknown';
}
$index = array_search($label, $labels);
if ($index === false) {
$labels[] = $label;
$timeSpentData[] = $totalDuration;
} else {
$timeSpentData[$index] += $totalDuration;
}
}
}
}
$chart->set_labels($labels);
$chart->add_series(new \core\chart_series(get_string('time_spent', 'block_time_spent'), $timeSpentData));
// Add category filter form.
$categoryFilterForm = html_writer::start_tag('form', array('action' => '', 'method' => 'get'));
$categoryFilterForm .= html_writer::start_tag('div');
$categoryFilterForm .= html_writer::label('Filter by category: ', 'filterCategory', false);
$categoryFilterForm .= html_writer::select(\core_course_category::make_categories_list(), 'filterCategory', $filterCategory);
$categoryFilterForm .= html_writer::empty_tag('input', array('type' => 'submit', 'value' => 'Filter'));
$categoryFilterForm .= html_writer::end_tag('div');
$categoryFilterForm .= html_writer::end_tag('form');
$this->content->text = $categoryFilterForm;
$this->content->text .= $OUTPUT->render($chart);
return $this->content;
}
}
defined('MOODLE_INTERNAL') || die();
class block_time_spent extends block_base {
public function init() {
$this->title = get_string('pluginname', 'block_time_spent');
}
public function get_content() {
global $USER, $DB, $OUTPUT;
if ($this->content !== null) {
return $this->content;
}
$this->content = new stdClass();
$filterCategory = optional_param('filterCategory', null, PARAM_INT);
$courses = enrol_get_all_users_courses($USER->id);
$chart = new \core\chart_pie();
$labels = [];
$timeSpentData = [];
foreach ($courses as $course) {
if (has_capability('mod/attendance:view', context_course::instance($course->id))) {
if ($filterCategory !== null && $course->category != $filterCategory) {
continue;
}
$attendanceregisterRecord = $DB->get_record('attendanceregister', array('course' => $course->id));
if ($attendanceregisterRecord) {
$totalDuration = 0;
$sessions = $DB->get_records('attendanceregister_session', array('register' => $attendanceregisterRecord->id, 'userid' => $USER->id));
if ($sessions) {
foreach ($sessions as $session) {
$totalDuration += $session->duration;
}
}
if ($filterCategory !== null) {
$label = $course->fullname;
} else {
$category = \core_course_category::get($course->category, IGNORE_MISSING);
$label = $category ? $category->get_formatted_name() : 'Unknown';
}
$index = array_search($label, $labels);
if ($index === false) {
$labels[] = $label;
$timeSpentData[] = $totalDuration;
} else {
$timeSpentData[$index] += $totalDuration;
}
}
}
}
$chart->set_labels($labels);
$chart->add_series(new \core\chart_series(get_string('time_spent', 'block_time_spent'), $timeSpentData));
// Add category filter form.
$categoryFilterForm = html_writer::start_tag('form', array('action' => '', 'method' => 'get'));
$categoryFilterForm .= html_writer::start_tag('div');
$categoryFilterForm .= html_writer::label('Filter by category: ', 'filterCategory', false);
$categoryFilterForm .= html_writer::select(\core_course_category::make_categories_list(), 'filterCategory', $filterCategory);
$categoryFilterForm .= html_writer::empty_tag('input', array('type' => 'submit', 'value' => 'Filter'));
$categoryFilterForm .= html_writer::end_tag('div');
$categoryFilterForm .= html_writer::end_tag('form');
$this->content->text = $categoryFilterForm;
$this->content->text .= $OUTPUT->render($chart);
return $this->content;
}
}