mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +02:00
MDL-65588 analytics: New models for student accesses
This commit is contained in:
parent
7d8ed90757
commit
2d9280e0df
21 changed files with 1079 additions and 89 deletions
135
lib/classes/analytics/indicator/any_course_access.php
Normal file
135
lib/classes/analytics/indicator/any_course_access.php
Normal file
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Any access indicator.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\analytics\indicator;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Any access indicator.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class any_course_access extends \core_analytics\local\indicator\binary {
|
||||
|
||||
/**
|
||||
* Returns the name.
|
||||
*
|
||||
* If there is a corresponding '_help' string this will be shown as well.
|
||||
*
|
||||
* @return \lang_string
|
||||
*/
|
||||
public static function get_name() : \lang_string {
|
||||
return new \lang_string('indicator:anycourseaccess');
|
||||
}
|
||||
|
||||
/**
|
||||
* required_sample_data
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public static function required_sample_data() {
|
||||
return array('course', 'user');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store userid => timeaccess relation if the provided analysable is a course.
|
||||
*
|
||||
* @param \core_analytics\analysable $analysable
|
||||
* @return null
|
||||
*/
|
||||
public function fill_per_analysable_caches(\core_analytics\analysable $analysable) {
|
||||
global $DB;
|
||||
|
||||
if ($analysable instanceof \core_analytics\course) {
|
||||
// Indexed by userid (there is a UNIQUE KEY at DB level).
|
||||
$this->lastaccesses = $DB->get_records('user_lastaccess', ['courseid' => $analysable->get_id()],
|
||||
'', 'userid, timeaccess');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* calculate_sample
|
||||
*
|
||||
* @param int $sampleid
|
||||
* @param string $sampleorigin
|
||||
* @param int $starttime
|
||||
* @param int $endtime
|
||||
* @return float
|
||||
*/
|
||||
protected function calculate_sample($sampleid, $sampleorigin, $starttime = false, $endtime = false) {
|
||||
|
||||
$course = $this->retrieve('course', $sampleid);
|
||||
$user = $this->retrieve('user', $sampleid);
|
||||
|
||||
// We first try using user_lastaccess as it is much faster than the log table.
|
||||
if (empty($this->lastaccesses[$user->id]->timeaccess)) {
|
||||
// The user never accessed.
|
||||
return self::get_min_value();
|
||||
} else if (!$starttime && !$endtime) {
|
||||
// No time restrictions, so all good as long as there is a record.
|
||||
return self::get_max_value();
|
||||
} else if ($starttime && $this->lastaccesses[$user->id]->timeaccess < $starttime) {
|
||||
// The last access is prior to $starttime.
|
||||
return self::get_min_value();
|
||||
} else if ($endtime && $this->lastaccesses[$user->id]->timeaccess < $endtime) {
|
||||
// The last access is before the $endtime.
|
||||
return self::get_max_value();
|
||||
} else if ($starttime && !$endtime && $starttime <= $this->lastaccesses[$user->id]->timeaccess) {
|
||||
// No end time, so max value as long as the last access is after $starttime.
|
||||
return self::get_max_value();
|
||||
}
|
||||
|
||||
// If the last access is after $endtime we can not know for sure if the user accessed or not
|
||||
// between $starttime and $endtime, we need to check the logs table in this case. Note that
|
||||
// it is unlikely that we will reach this point as this indicator will be used in models whose
|
||||
// dates are in the past.
|
||||
|
||||
if (!$logstore = \core_analytics\manager::get_analytics_logstore()) {
|
||||
throw new \coding_exception('No available log stores');
|
||||
}
|
||||
|
||||
// Filter by context to use the logstore_standard_log db table index.
|
||||
$select = "userid = :userid AND courseid = :courseid";
|
||||
$params = ['courseid' => $course->id, 'userid' => $user->id];
|
||||
|
||||
if ($starttime) {
|
||||
$select .= " AND timecreated > :starttime";
|
||||
$params['starttime'] = $starttime;
|
||||
}
|
||||
if ($endtime) {
|
||||
$select .= " AND timecreated <= :endtime";
|
||||
$params['endtime'] = $endtime;
|
||||
}
|
||||
|
||||
$nlogs = $logstore->get_events_select_count($select, $params);
|
||||
if ($nlogs) {
|
||||
return self::get_max_value();
|
||||
} else {
|
||||
return self::get_min_value();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Time splitting method that generates predictions one month after the analysable start.
|
||||
*
|
||||
* @package core_analytics
|
||||
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\analytics\time_splitting;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Time splitting method that generates predictions one month after the analysable start.
|
||||
*
|
||||
* @package core_analytics
|
||||
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class one_month_after_start extends \core_analytics\local\time_splitting\after_start {
|
||||
|
||||
/**
|
||||
* The time splitting method name.
|
||||
*
|
||||
* @return \lang_string
|
||||
*/
|
||||
public static function get_name() : \lang_string {
|
||||
return new \lang_string('timesplitting:onemonthafterstart');
|
||||
}
|
||||
|
||||
/**
|
||||
* The period we should wait until we generate predictions for this.
|
||||
*
|
||||
* @param \core_analytics\analysable $analysable Not used in this implementation.
|
||||
* @return \DateInterval
|
||||
*/
|
||||
protected function wait_period(\core_analytics\analysable $analysable) {
|
||||
return new \DateInterval('P1M');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Time splitting method that generates predictions one week after the analysable start.
|
||||
*
|
||||
* @package core_analytics
|
||||
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\analytics\time_splitting;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Time splitting method that generates predictions one week after the analysable start.
|
||||
*
|
||||
* @package core_analytics
|
||||
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class one_week_after_start extends \core_analytics\local\time_splitting\after_start {
|
||||
|
||||
/**
|
||||
* The time splitting method name.
|
||||
*
|
||||
* @return \lang_string
|
||||
*/
|
||||
public static function get_name() : \lang_string {
|
||||
return new \lang_string('timesplitting:oneweekafterstart');
|
||||
}
|
||||
|
||||
/**
|
||||
* The period we should wait until we generate predictions for this.
|
||||
*
|
||||
* @param \core_analytics\analysable $analysable Not used in this implementation.
|
||||
* @return \DateInterval
|
||||
*/
|
||||
protected function wait_period(\core_analytics\analysable $analysable) {
|
||||
return new \DateInterval('P1W');
|
||||
}
|
||||
}
|
55
lib/classes/analytics/time_splitting/past_3_days.php
Normal file
55
lib/classes/analytics/time_splitting/past_3_days.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Time splitting method that generates predictions every 3 days.
|
||||
*
|
||||
* @package core_analytics
|
||||
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\analytics\time_splitting;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Time splitting method that generates predictions every 3 days.
|
||||
*
|
||||
* @package core_analytics
|
||||
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class past_3_days extends \core_analytics\local\time_splitting\past_periodic {
|
||||
|
||||
/**
|
||||
* The time splitting method name.
|
||||
*
|
||||
* @return \lang_string
|
||||
*/
|
||||
public static function get_name() : \lang_string {
|
||||
return new \lang_string('timesplitting:past3days');
|
||||
}
|
||||
|
||||
/**
|
||||
* Once every 3 days.
|
||||
*
|
||||
* @return \DateInterval
|
||||
*/
|
||||
public function periodicity() {
|
||||
return new \DateInterval('P3D');
|
||||
}
|
||||
}
|
55
lib/classes/analytics/time_splitting/past_month.php
Normal file
55
lib/classes/analytics/time_splitting/past_month.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Time splitting method that generates monthly predictions.
|
||||
*
|
||||
* @package core_analytics
|
||||
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\analytics\time_splitting;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Time splitting method that generates monthly predictions.
|
||||
*
|
||||
* @package core_analytics
|
||||
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class past_month extends \core_analytics\local\time_splitting\past_periodic {
|
||||
|
||||
/**
|
||||
* The time splitting method name.
|
||||
*
|
||||
* @return \lang_string
|
||||
*/
|
||||
public static function get_name() : \lang_string {
|
||||
return new \lang_string('timesplitting:pastmonth');
|
||||
}
|
||||
|
||||
/**
|
||||
* Once a month.
|
||||
*
|
||||
* @return \DateInterval
|
||||
*/
|
||||
public function periodicity() {
|
||||
return new \DateInterval('P1M');
|
||||
}
|
||||
}
|
54
lib/classes/analytics/time_splitting/past_week.php
Normal file
54
lib/classes/analytics/time_splitting/past_week.php
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Time splitting method that generates weekly predictions.
|
||||
*
|
||||
* @package core_analytics
|
||||
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\analytics\time_splitting;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Time splitting method that generates weekly predictions.
|
||||
*
|
||||
* @package core_analytics
|
||||
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class past_week extends \core_analytics\local\time_splitting\past_periodic {
|
||||
|
||||
/**
|
||||
* The time splitting method name.
|
||||
* @return \lang_string
|
||||
*/
|
||||
public static function get_name() : \lang_string {
|
||||
return new \lang_string('timesplitting:pastweek');
|
||||
}
|
||||
|
||||
/**
|
||||
* Once per week.
|
||||
*
|
||||
* @return \DateInterval
|
||||
*/
|
||||
public function periodicity() {
|
||||
return new \DateInterval('P1W');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Time splitting method that generates predictions 3 days after the analysable start.
|
||||
*
|
||||
* @package core_analytics
|
||||
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\analytics\time_splitting;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Time splitting method that generates predictions 3 days after the analysable start.
|
||||
*
|
||||
* @package core_analytics
|
||||
* @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class ten_percent_after_start extends \core_analytics\local\time_splitting\after_start {
|
||||
|
||||
/**
|
||||
* The time splitting method name.
|
||||
*
|
||||
* @return \lang_string
|
||||
*/
|
||||
public static function get_name() : \lang_string {
|
||||
return new \lang_string('timesplitting:tenpercentafterstart');
|
||||
}
|
||||
|
||||
/**
|
||||
* Extended as we require and end date here.
|
||||
*
|
||||
* @param \core_analytics\analysable $analysable
|
||||
* @return bool
|
||||
*/
|
||||
public function is_valid_analysable(\core_analytics\analysable $analysable) {
|
||||
|
||||
// We require an end date to calculate the 10%.
|
||||
if (!$analysable->get_end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return parent::is_valid_analysable($analysable);
|
||||
}
|
||||
|
||||
/**
|
||||
* The period we should wait until we generate predictions for this.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @param \core_analytics\analysable $analysable
|
||||
* @return \DateInterval
|
||||
*/
|
||||
protected function wait_period(\core_analytics\analysable $analysable) {
|
||||
|
||||
if (!$analysable->get_end() || !$analysable->get_start()) {
|
||||
throw new \coding_exception('Analysables with no start or end should be discarded in is_valid_analysable.');
|
||||
}
|
||||
|
||||
$diff = $analysable->get_end() - $analysable->get_start();
|
||||
|
||||
// A 10% of $diff.
|
||||
return new \DateInterval('PT' . intval($diff / 10) . 'S');
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue