first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+139
View File
@@ -0,0 +1,139 @@
<?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/>.
/**
* Courses analyser working at course level (insights for the course teachers).
*
* @package core
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\analytics\analyser;
defined('MOODLE_INTERNAL') || die();
/**
* Courses analyser working at course level (insights for the course teachers).
*
* @package core
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class courses extends \core_analytics\local\analyser\by_course {
/**
* Samples origin is course table.
*
* @return string
*/
public function get_samples_origin() {
return 'course';
}
/**
* Just one sample per analysable.
*
* @return bool
*/
public static function one_sample_per_analysable() {
return true;
}
/**
* Returns the analysable of a sample
*
* @param int $sampleid
* @return \core_analytics\analysable
*/
public function get_sample_analysable($sampleid) {
return \core_analytics\course::instance($sampleid);
}
/**
* This provides samples' course and context.
*
* @return string[]
*/
protected function provided_sample_data() {
return array('course', 'context');
}
/**
* Returns the context of a sample.
*
* @param int $sampleid
* @return \context
*/
public function sample_access_context($sampleid) {
return \context_course::instance($sampleid);
}
/**
* This will return just one course as we analyse 'by_course'.
*
* @param \core_analytics\analysable $course
* @return array
*/
public function get_all_samples(\core_analytics\analysable $course) {
$context = \context_course::instance($course->get_id());
// Just 1 sample per analysable.
return array(
array($course->get_id() => $course->get_id()),
array($course->get_id() => array('course' => $course->get_course_data(), 'context' => $context))
);
}
/**
* Returns samples data from sample ids.
*
* @param int[] $sampleids
* @return array
*/
public function get_samples($sampleids) {
global $DB;
list($sql, $params) = $DB->get_in_or_equal($sampleids, SQL_PARAMS_NAMED);
$courses = $DB->get_records_select('course', "id $sql", $params);
$courseids = array_keys($courses);
$sampleids = array_combine($courseids, $courseids);
$courses = array_map(function($course) {
return array('course' => $course, 'context' => \context_course::instance($course->id));
}, $courses);
// No related data attached.
return array($sampleids, $courses);
}
/**
* Returns the sample description
*
* @param int $sampleid
* @param int $contextid
* @param array $sampledata
* @return array array(string, \renderable)
*/
public function sample_description($sampleid, $contextid, $sampledata) {
$description = format_string(
get_course_display_name_for_list($sampledata['course']), true, array('context' => $sampledata['context']));
$courseimage = new \pix_icon('i/course', get_string('course'));
return array($description, $courseimage);
}
}
@@ -0,0 +1,139 @@
<?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/>.
/**
* Site courses analyser working at system level (insights for the site admin).
*
* @package core
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\analytics\analyser;
defined('MOODLE_INTERNAL') || die();
/**
* Site courses analyser working at system level (insights for the site admin).
*
* @package core
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class site_courses extends \core_analytics\local\analyser\sitewide {
/**
* Samples origin is course table.
*
* @return string
*/
public function get_samples_origin() {
return 'course';
}
/**
* Returns the sample analysable
*
* @param int $sampleid
* @return \core_analytics\analysable
*/
public function get_sample_analysable($sampleid) {
return new \core_analytics\site();
}
/**
* Data this analyer samples provide.
*
* @return string[]
*/
protected function provided_sample_data() {
return array('course', 'context');
}
/**
* Returns the sample context.
*
* @param int $sampleid
* @return \context
*/
public function sample_access_context($sampleid) {
return \context_system::instance();
}
/**
* Returns all site courses.
*
* @param \core_analytics\analysable $site
* @return array
*/
public function get_all_samples(\core_analytics\analysable $site) {
global $DB;
// Getting courses from DB instead of from the site as these samples
// will be stored in memory and we just want the id.
$select = 'id != 1';
$courses = get_courses('all', 'c.sortorder ASC');
unset($courses[SITEID]);
$courseids = array_keys($courses);
$sampleids = array_combine($courseids, $courseids);
$courses = array_map(function($course) {
return array('course' => $course, 'context' => \context_course::instance($course->id));
}, $courses);
// No related data attached.
return array($sampleids, $courses);
}
/**
* Return all complete samples data from sample ids.
*
* @param int[] $sampleids
* @return array
*/
public function get_samples($sampleids) {
global $DB;
list($sql, $params) = $DB->get_in_or_equal($sampleids, SQL_PARAMS_NAMED);
$courses = $DB->get_records_select('course', "id $sql", $params);
$courseids = array_keys($courses);
$sampleids = array_combine($courseids, $courseids);
$courses = array_map(function($course) {
return array('course' => $course, 'context' => \context_course::instance($course->id));
}, $courses);
// No related data attached.
return array($sampleids, $courses);
}
/**
* Returns the description of a sample.
*
* @param int $sampleid
* @param int $contextid
* @param array $sampledata
* @return array array(string, \renderable)
*/
public function sample_description($sampleid, $contextid, $sampledata) {
$description = format_string(
get_course_display_name_for_list($sampledata['course']), true, array('context' => $sampledata['context']));
$courseimage = new \pix_icon('i/course', get_string('course'));
return array($description, $courseimage);
}
}
@@ -0,0 +1,251 @@
<?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/>.
/**
* Student enrolments analyser.
*
* @package core
* @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\analytics\analyser;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/lib/enrollib.php');
/**
* Student enrolments analyser.
*
* It does return all student enrolments including the suspended ones.
*
* @package core
* @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class student_enrolments extends \core_analytics\local\analyser\by_course {
/**
* @var array Cache for user_enrolment id - course id relation.
*/
protected $samplecourses = array();
/**
* Defines the origin of the samples in the database.
*
* @return string
*/
public function get_samples_origin() {
return 'user_enrolments';
}
/**
* Returns the student enrolment course context.
*
* @param int $sampleid
* @return \context
*/
public function sample_access_context($sampleid) {
return \context_course::instance($this->get_sample_courseid($sampleid));
}
/**
* Returns the student enrolment course.
*
* @param int $sampleid
* @return \core_analytics\analysable
*/
public function get_sample_analysable($sampleid) {
$course = enrol_get_course_by_user_enrolment_id($sampleid);
return \core_analytics\course::instance($course);
}
/**
* Data provided by get_all_samples & get_samples.
*
* @return string[]
*/
protected function provided_sample_data() {
return array('user_enrolments', 'context', 'course', 'user');
}
/**
* We need to delete associated data if a user requests his data to be deleted.
*
* @return bool
*/
public function processes_user_data() {
return true;
}
/**
* Join the samples origin table with the user id table.
*
* @param string $sampletablealias
* @return string
*/
public function join_sample_user($sampletablealias) {
return "JOIN {user_enrolments} ue ON {$sampletablealias}.sampleid = ue.id " .
"JOIN {user} u ON u.id = ue.userid";
}
/**
* All course student enrolments.
*
* It does return all student enrolments including the suspended ones.
*
* @param \core_analytics\analysable $course
* @return array
*/
public function get_all_samples(\core_analytics\analysable $course) {
$enrolments = enrol_get_course_users($course->get_id());
// We fetch all enrolments, but we are only interested in students.
$studentids = $course->get_students();
$samplesdata = array();
foreach ($enrolments as $userenrolmentid => $user) {
if (empty($studentids[$user->id])) {
// Not a student or an analysed one.
continue;
}
$sampleid = $userenrolmentid;
$samplesdata[$sampleid]['user_enrolments'] = (object)array(
'id' => $user->ueid,
'status' => $user->uestatus,
'enrolid' => $user->ueenrolid,
'userid' => $user->id,
'timestart' => $user->uetimestart,
'timeend' => $user->uetimeend,
'modifierid' => $user->uemodifierid,
'timecreated' => $user->uetimecreated,
'timemodified' => $user->uetimemodified
);
unset($user->ueid);
unset($user->uestatus);
unset($user->ueenrolid);
unset($user->uetimestart);
unset($user->uetimeend);
unset($user->uemodifierid);
unset($user->uetimecreated);
unset($user->uetimemodified);
// This student has been already analysed. We analyse each student once.
unset($studentids[$user->id]);
$samplesdata[$sampleid]['course'] = $course->get_course_data();
$samplesdata[$sampleid]['context'] = $course->get_context();
$samplesdata[$sampleid]['user'] = $user;
// Fill the cache.
$this->samplecourses[$sampleid] = $course->get_id();
}
$enrolids = array_keys($samplesdata);
return array(array_combine($enrolids, $enrolids), $samplesdata);
}
/**
* Returns all samples from the samples ids.
*
* @param int[] $sampleids
* @return array
*/
public function get_samples($sampleids) {
global $DB;
$enrolments = enrol_get_course_users(false, false, array(), $sampleids);
// Some course enrolments.
list($enrolsql, $params) = $DB->get_in_or_equal($sampleids, SQL_PARAMS_NAMED);
$samplesdata = array();
foreach ($enrolments as $userenrolmentid => $user) {
$sampleid = $userenrolmentid;
$samplesdata[$sampleid]['user_enrolments'] = (object)array(
'id' => $user->ueid,
'status' => $user->uestatus,
'enrolid' => $user->ueenrolid,
'userid' => $user->id,
'timestart' => $user->uetimestart,
'timeend' => $user->uetimeend,
'modifierid' => $user->uemodifierid,
'timecreated' => $user->uetimecreated,
'timemodified' => $user->uetimemodified
);
unset($user->ueid);
unset($user->uestatus);
unset($user->ueenrolid);
unset($user->uetimestart);
unset($user->uetimeend);
unset($user->uemodifierid);
unset($user->uetimecreated);
unset($user->uetimemodified);
// Enrolment samples are grouped by the course they belong to, so all $sampleids belong to the same
// course, $courseid and $coursemodinfo will only query the DB once and cache the course data in memory.
$courseid = $this->get_sample_courseid($sampleid);
$coursemodinfo = get_fast_modinfo($courseid);
$coursecontext = \context_course::instance($courseid);
$samplesdata[$sampleid]['course'] = $coursemodinfo->get_course();
$samplesdata[$sampleid]['context'] = $coursecontext;
$samplesdata[$sampleid]['user'] = $user;
// Fill the cache.
$this->samplecourses[$sampleid] = $coursemodinfo->get_course()->id;
}
$enrolids = array_keys($samplesdata);
return array(array_combine($enrolids, $enrolids), $samplesdata);
}
/**
* Returns the student enrolment course id.
*
* @param int $sampleid
* @return int
*/
protected function get_sample_courseid($sampleid) {
global $DB;
if (empty($this->samplecourses[$sampleid])) {
$course = enrol_get_course_by_user_enrolment_id($sampleid);
$this->samplecourses[$sampleid] = $course->id;
}
return $this->samplecourses[$sampleid];
}
/**
* Returns the visible name of a sample + a renderable to display as sample picture.
*
* @param int $sampleid
* @param int $contextid
* @param array $sampledata
* @return array array(string, \renderable)
*/
public function sample_description($sampleid, $contextid, $sampledata) {
$description = fullname($sampledata['user'], true, array('context' => $contextid));
return array($description, new \user_picture($sampledata['user']));
}
}
+192
View File
@@ -0,0 +1,192 @@
<?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/>.
/**
* Users analyser (insights for users).
*
* @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\analyser;
defined('MOODLE_INTERNAL') || die();
/**
* Users analyser (insights for users).
*
* @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 users extends \core_analytics\local\analyser\base {
/**
* The site users are the analysable elements returned by this analyser.
*
* @param string|null $action 'prediction', 'training' or null if no specific action needed.
* @param \context[] $contexts Only analysables that depend on the provided contexts. All analysables in the system if empty.
* @return \Iterator
*/
public function get_analysables_iterator(?string $action = null, array $contexts = []) {
global $DB, $CFG;
$siteadmins = explode(',', $CFG->siteadmins);
list($sql, $params) = $this->get_iterator_sql('user', CONTEXT_USER, $action, 'u', $contexts);
$sql .= " AND u.deleted = :deleted AND u.confirmed = :confirmed AND u.suspended = :suspended";
$params = $params + ['deleted' => 0, 'confirmed' => 1, 'suspended' => 0];
$ordersql = $this->order_sql('timecreated', 'ASC', 'u');
$recordset = $DB->get_recordset_sql($sql, $params);
if (!$recordset->valid()) {
$this->add_log(get_string('nousersfound'));
return new \ArrayIterator([]);
}
return new \core\dml\recordset_walk($recordset, function($record) use ($siteadmins) {
if (in_array($record->id, $siteadmins) || isguestuser($record->id)) {
// Skip admins and the guest user.
return false;
}
$context = \context_helper::preload_from_record($record);
return \core_analytics\user::instance($record, $context);
});
}
/**
* Just one sample per analysable.
*
* @return bool
*/
public static function one_sample_per_analysable() {
return true;
}
/**
* Samples origin is user table.
*
* @return string
*/
public function get_samples_origin() {
return 'user';
}
/**
* Returns the analysable of a sample
*
* @param int $sampleid
* @return \core_analytics\analysable
*/
public function get_sample_analysable($sampleid) {
return \core_analytics\user::instance($sampleid);
}
/**
* This provides samples' user and context.
*
* @return string[]
*/
protected function provided_sample_data() {
return ['user', 'context'];
}
/**
* Returns the context of a sample.
*
* @param int $sampleid
* @return \context
*/
public function sample_access_context($sampleid) {
return \context_user::instance($sampleid);
}
/**
* This will return just one user as we analyse users separately.
*
* @param \core_analytics\analysable $user
* @return array
*/
public function get_all_samples(\core_analytics\analysable $user) {
$context = \context_user::instance($user->get_id());
// Just 1 sample per analysable.
return [
[$user->get_id() => $user->get_id()],
[$user->get_id() => ['user' => $user->get_user_data(), 'context' => $context]]
];
}
/**
* Returns samples data from sample ids.
*
* @param int[] $sampleids
* @return array
*/
public function get_samples($sampleids) {
global $DB;
list($sql, $params) = $DB->get_in_or_equal($sampleids, SQL_PARAMS_NAMED);
$users = $DB->get_records_select('user', "id $sql", $params);
$userids = array_keys($users);
$sampleids = array_combine($userids, $userids);
$users = array_map(function($user) {
return ['user' => $user, 'context' => \context_user::instance($user->id)];
}, $users);
// No related data attached.
return [$sampleids, $users];
}
/**
* Returns the description of a sample.
*
* @param int $sampleid
* @param int $contextid
* @param array $sampledata
* @return array array(string, \renderable)
*/
public function sample_description($sampleid, $contextid, $sampledata) {
$description = fullname($sampledata['user']);
return [$description, new \user_picture($sampledata['user'])];
}
/**
* We need to delete associated data if a user requests his data to be deleted.
*
* @return bool
*/
public function processes_user_data() {
return true;
}
/**
* Join the samples origin table with the user id table.
*
* @param string $sampletablealias
* @return string
*/
public function join_sample_user($sampletablealias) {
return "JOIN {user} u ON u.id = {$sampletablealias}.sampleid";
}
}