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
+84
View File
@@ -0,0 +1,84 @@
<?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/>.
/**
* Enables the provided model.
*
* @package tool_analytics
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('CLI_SCRIPT', true);
require_once(__DIR__ . '/../../../../config.php');
require_once($CFG->libdir.'/clilib.php');
$help = "Enables the provided model.
Options:
--modelid Model id
--list List models
--analysisinterval Time splitting method full class name
-h, --help Print out this help
Example:
\$ php admin/tool/analytics/cli/enable_model.php --modelid=1 --analysisinterval=\"\\core\\analytics\\time_splitting\\quarters\"
";
// Now get cli options.
list($options, $unrecognized) = cli_get_params(
array(
'help' => false,
'list' => false,
'modelid' => false,
'analysisinterval' => false
),
array(
'h' => 'help',
)
);
if ($options['help']) {
echo $help;
exit(0);
}
if (!\core_analytics\manager::is_analytics_enabled()) {
echo get_string('analyticsdisabled', 'analytics') . PHP_EOL;
exit(0);
}
if ($options['list'] || $options['modelid'] === false) {
\tool_analytics\clihelper::list_models();
exit(0);
}
if ($options['analysisinterval'] === false) {
echo $help;
exit(0);
}
// We need admin permissions.
\core\session\manager::set_user(get_admin());
$model = new \core_analytics\model($options['modelid']);
// Evaluate its suitability to predict accurately.
$model->enable($options['analysisinterval']);
cli_heading(get_string('success'));
exit(0);
+152
View File
@@ -0,0 +1,152 @@
<?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/>.
/**
* Evaluates the provided model.
*
* @package tool_analytics
* @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('CLI_SCRIPT', true);
require_once(__DIR__ . '/../../../../config.php');
require_once($CFG->libdir.'/clilib.php');
$help = "Evaluates the provided model.
Options:
--modelid Model id
--list List models
--non-interactive Not interactive questions
--analysisinterval Restrict the evaluation to 1 single analysis interval (Optional)
--mode 'configuration' or 'trainedmodel'. You can only use mode=trainedmodel when the trained" .
" model was imported" . "
--reuse-prev-analysed Reuse recently analysed courses instead of analysing the whole site. Set it to false while" .
" coding indicators. Defaults to true (Optional)" . "
-h, --help Print out this help
Example:
\$ php admin/tool/analytics/cli/evaluate_model.php --modelid=1 --analysisinterval='\\core\\analytics\\time_splitting\\quarters'
";
// Now get cli options.
list($options, $unrecognized) = cli_get_params(
array(
'help' => false,
'modelid' => false,
'list' => false,
'analysisinterval' => false,
'mode' => 'configuration',
'reuse-prev-analysed' => true,
'non-interactive' => false,
),
array(
'h' => 'help',
)
);
if ($options['help']) {
echo $help;
exit(0);
}
if (!\core_analytics\manager::is_analytics_enabled()) {
echo get_string('analyticsdisabled', 'analytics') . PHP_EOL;
exit(0);
}
if ($options['list']) {
\tool_analytics\clihelper::list_models();
exit(0);
}
if ($options['modelid'] === false) {
// All actions but --list require a modelid.
echo $help;
exit(0);
}
if ($options['mode'] !== 'configuration' && $options['mode'] !== 'trainedmodel') {
cli_error('Error: The provided mode is not supported');
}
if ($options['mode'] == 'trainedmodel' && $options['analysisinterval']) {
cli_error('Sorry, no analysis interval can be specified when using \'trainedmodel\' mode.');
}
// We need admin permissions.
\core\session\manager::set_user(get_admin());
$model = new \core_analytics\model($options['modelid']);
mtrace(get_string('analysingsitedata', 'tool_analytics'));
if ($options['reuse-prev-analysed']) {
mtrace(get_string('evaluationinbatches', 'tool_analytics'));
}
$renderer = $PAGE->get_renderer('tool_analytics');
$analyseroptions = array(
'timesplitting' => $options['analysisinterval'],
'reuseprevanalysed' => $options['reuse-prev-analysed'],
'mode' => $options['mode'],
);
// Evaluate its suitability to predict accurately.
$results = $model->evaluate($analyseroptions);
// Reset the page as some indicators may call external functions that overwrite the page context.
\tool_analytics\output\helper::reset_page();
echo $renderer->render_evaluate_results($results, $model->get_analyser()->get_logs());
// Check that we have, at leasa,t 1 valid dataset (not necessarily good) to use.
foreach ($results as $result) {
if ($result->status !== \core_analytics\model::NO_DATASET &&
$result->status !== \core_analytics\model::GENERAL_ERROR) {
$validdatasets = true;
}
}
if (!empty($validdatasets) && !$model->is_enabled() && $options['non-interactive'] === false) {
// Select a dataset, train and enable the model.
$input = cli_input(get_string('clienablemodel', 'tool_analytics'));
while (!\core_analytics\manager::is_valid($input, '\core_analytics\local\time_splitting\base') && $input !== 'none') {
mtrace(get_string('errorunexistingtimesplitting', 'analytics'));
$input = cli_input(get_string('clienablemodel', 'tool_analytics'));
}
if ($input === 'none') {
exit(0);
}
// Refresh the instance to prevent unexpected issues.
$model = new \core_analytics\model($options['modelid']);
// Set the time splitting method file and enable it.
$model->enable($input);
mtrace(get_string('trainandpredictmodel', 'tool_analytics'));
// Train the model with the selected time splitting method and start predicting.
$model->train();
$model->predict();
}
exit(0);
@@ -0,0 +1,229 @@
<?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/>.
/**
* Guesses course start and end dates based on activity logs.
*
* @package tool_analytics
* @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('CLI_SCRIPT', true);
require_once(__DIR__ . '/../../../../config.php');
require_once($CFG->libdir.'/clilib.php');
require_once($CFG->dirroot . '/course/lib.php');
require_once($CFG->dirroot . '/course/format/weeks/lib.php');
$help = "Guesses course start and end dates based on activity logs.
IMPORTANT: Don't use this script if you keep previous academic years users enrolled in courses. Guesses would not be accurate.
Options:
--guessstart Guess the course start date (default to true)
--guessend Guess the course end date (default to true)
--guessall Guess all start and end dates, even if they are already set (default to false)
--update Update the db or just notify the guess (default to false)
--filter Analyser dependant. e.g. A courseid would evaluate the model using a single course (Optional)
-h, --help Print out this help
Example:
\$ php admin/tool/analytics/cli/guess_course_start_and_end_dates.php --update=1 --filter=123,321
";
// Now get cli options.
list($options, $unrecognized) = cli_get_params(
array(
'help' => false,
'guessstart' => true,
'guessend' => true,
'guessall' => false,
'update' => false,
'filter' => false
),
array(
'h' => 'help',
)
);
if ($options['help']) {
echo $help;
exit(0);
}
if ($options['guessstart'] === false && $options['guessend'] === false && $options['guessall'] === false) {
echo $help;
exit(0);
}
// Reformat them as an array.
if ($options['filter'] !== false) {
$options['filter'] = explode(',', clean_param($options['filter'], PARAM_SEQUENCE));
}
// We need admin permissions.
\core\session\manager::set_user(get_admin());
$conditions = array('id != 1');
if (!$options['guessall']) {
if ($options['guessstart']) {
$conditions[] = '(startdate is null or startdate = 0)';
}
if ($options['guessend']) {
$conditions[] = '(enddate is null or enddate = 0)';
}
}
$coursessql = '';
$params = null;
if ($options['filter']) {
list($coursessql, $params) = $DB->get_in_or_equal($options['filter'], SQL_PARAMS_NAMED);
$conditions[] = 'id ' . $coursessql;
}
$courses = $DB->get_recordset_select('course', implode(' AND ', $conditions), $params, 'sortorder ASC');
foreach ($courses as $course) {
tool_analytics_calculate_course_dates($course, $options);
}
$courses->close();
/**
* tool_analytics_calculate_course_dates
*
* @param stdClass $course
* @param array $options CLI options
* @return void
*/
function tool_analytics_calculate_course_dates($course, $options) {
global $DB, $OUTPUT;
$courseman = new \core_analytics\course($course);
$notification = $course->shortname . ' (id = ' . $course->id . '): ';
$originalenddate = null;
$guessedstartdate = null;
$guessedenddate = null;
$samestartdate = null;
$lowerenddate = null;
if ($options['guessstart'] || $options['guessall']) {
$originalstartdate = $course->startdate;
$guessedstartdate = $courseman->guess_start();
$samestartdate = ($guessedstartdate == $originalstartdate);
$lowerenddate = ($course->enddate && ($course->enddate < $guessedstartdate));
if ($samestartdate) {
if (!$guessedstartdate) {
$notification .= PHP_EOL . ' ' . get_string('cantguessstartdate', 'tool_analytics');
} else {
// No need to update.
$notification .= PHP_EOL . ' ' . get_string('samestartdate', 'tool_analytics') . ': ' . userdate($guessedstartdate);
}
} else if (!$guessedstartdate) {
$notification .= PHP_EOL . ' ' . get_string('cantguessstartdate', 'tool_analytics');
} else if ($lowerenddate) {
$notification .= PHP_EOL . ' ' . get_string('cantguessstartdate', 'tool_analytics') . ': ' .
get_string('enddatebeforestartdate', 'error') . ' - ' . userdate($guessedstartdate);
} else {
// Update it to something we guess.
// We set it to $course even if we don't update because may be needed to guess the end one.
$course->startdate = $guessedstartdate;
$notification .= PHP_EOL . ' ' . get_string('startdate') . ': ' . userdate($guessedstartdate);
// Two different course updates because week's end date may be recalculated after setting the start date.
if ($options['update']) {
update_course($course);
// Refresh course data as end date may have been updated.
$course = $DB->get_record('course', array('id' => $course->id));
$courseman = new \core_analytics\course($course);
}
}
}
if ($options['guessend'] || $options['guessall']) {
if (!empty($lowerenddate) && !empty($guessedstartdate)) {
$course->startdate = $guessedstartdate;
}
$originalenddate = $course->enddate;
$format = course_get_format($course);
$formatoptions = $format->get_format_options();
// Change this for a course formats API level call in MDL-60702.
if ((get_class($format) == 'format_weeks' || is_subclass_of($format, 'format_weeks')) &&
method_exists($format, 'update_end_date') && $formatoptions['automaticenddate']) {
// Special treatment for weeks-based formats with automatic end date.
if ($options['update']) {
$format::update_end_date($course->id);
$course->enddate = $DB->get_field('course', 'enddate', array('id' => $course->id));
$notification .= PHP_EOL . ' ' . get_string('weeksenddateautomaticallyset', 'tool_analytics') . ': ' .
userdate($course->enddate);
} else {
// We can't provide more info without actually updating it in db.
$notification .= PHP_EOL . ' ' . get_string('weeksenddatedefault', 'tool_analytics');
}
} else {
$guessedenddate = $courseman->guess_end();
if ($guessedenddate == $originalenddate) {
if (!$guessedenddate) {
$notification .= PHP_EOL . ' ' . get_string('cantguessenddate', 'tool_analytics');
} else {
// No need to update.
$notification .= PHP_EOL . ' ' . get_string('sameenddate', 'tool_analytics') . ': ' . userdate($guessedenddate);
}
} else if (!$guessedenddate) {
$notification .= PHP_EOL . ' ' . get_string('cantguessenddate', 'tool_analytics');
} else {
// Update it to something we guess.
$course->enddate = $guessedenddate;
$updateit = false;
if ($course->enddate < $course->startdate) {
$notification .= PHP_EOL . ' ' . get_string('errorendbeforestart', 'course', userdate($course->enddate));
} else if ($course->startdate + (YEARSECS + (WEEKSECS * 4)) > $course->enddate) {
$notification .= PHP_EOL . ' ' . get_string('coursetoolong', 'course');
} else {
$notification .= PHP_EOL . ' ' . get_string('enddate') . ': ' . userdate($course->enddate);
$updateit = true;
}
if ($options['update'] && $updateit) {
update_course($course);
}
}
}
}
mtrace($notification);
}
mtrace(get_string('success'));
exit(0);