Merge branch 'MDL-72496-master_assert_tag' of https://github.com/call-learning/moodle
[moodle.git] / admin / tool / analytics / cli / evaluate_model.php
blob9509b6ad2ee26ebcd748a41dbc4ccc5dbd4e72e3
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Evaluates the provided model.
20 * @package tool_analytics
21 * @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 define('CLI_SCRIPT', true);
27 require_once(__DIR__ . '/../../../../config.php');
28 require_once($CFG->libdir.'/clilib.php');
30 $help = "Evaluates the provided model.
32 Options:
33 --modelid Model id
34 --list List models
35 --non-interactive Not interactive questions
36 --analysisinterval Restrict the evaluation to 1 single analysis interval (Optional)
37 --mode 'configuration' or 'trainedmodel'. You can only use mode=trainedmodel when the trained" .
38 " model was imported" . "
39 --reuse-prev-analysed Reuse recently analysed courses instead of analysing the whole site. Set it to false while" .
40 " coding indicators. Defaults to true (Optional)" . "
41 -h, --help Print out this help
43 Example:
44 \$ php admin/tool/analytics/cli/evaluate_model.php --modelid=1 --analysisinterval='\\core\\analytics\\time_splitting\\quarters'
47 // Now get cli options.
48 list($options, $unrecognized) = cli_get_params(
49 array(
50 'help' => false,
51 'modelid' => false,
52 'list' => false,
53 'analysisinterval' => false,
54 'mode' => 'configuration',
55 'reuse-prev-analysed' => true,
56 'non-interactive' => false,
58 array(
59 'h' => 'help',
63 if ($options['help']) {
64 echo $help;
65 exit(0);
68 if (!\core_analytics\manager::is_analytics_enabled()) {
69 echo get_string('analyticsdisabled', 'analytics') . PHP_EOL;
70 exit(0);
73 if ($options['list']) {
74 \tool_analytics\clihelper::list_models();
75 exit(0);
78 if ($options['modelid'] === false) {
79 // All actions but --list require a modelid.
80 echo $help;
81 exit(0);
84 if ($options['mode'] !== 'configuration' && $options['mode'] !== 'trainedmodel') {
85 cli_error('Error: The provided mode is not supported');
88 if ($options['mode'] == 'trainedmodel' && $options['analysisinterval']) {
89 cli_error('Sorry, no analysis interval can be specified when using \'trainedmodel\' mode.');
92 // We need admin permissions.
93 \core\session\manager::set_user(get_admin());
95 $model = new \core_analytics\model($options['modelid']);
97 mtrace(get_string('analysingsitedata', 'tool_analytics'));
99 if ($options['reuse-prev-analysed']) {
100 mtrace(get_string('evaluationinbatches', 'tool_analytics'));
103 $renderer = $PAGE->get_renderer('tool_analytics');
105 $analyseroptions = array(
106 'timesplitting' => $options['analysisinterval'],
107 'reuseprevanalysed' => $options['reuse-prev-analysed'],
108 'mode' => $options['mode'],
110 // Evaluate its suitability to predict accurately.
111 $results = $model->evaluate($analyseroptions);
113 // Reset the page as some indicators may call external functions that overwrite the page context.
114 \tool_analytics\output\helper::reset_page();
116 echo $renderer->render_evaluate_results($results, $model->get_analyser()->get_logs());
118 // Check that we have, at leasa,t 1 valid dataset (not necessarily good) to use.
119 foreach ($results as $result) {
120 if ($result->status !== \core_analytics\model::NO_DATASET &&
121 $result->status !== \core_analytics\model::GENERAL_ERROR) {
122 $validdatasets = true;
126 if (!empty($validdatasets) && !$model->is_enabled() && $options['non-interactive'] === false) {
128 // Select a dataset, train and enable the model.
129 $input = cli_input(get_string('clienablemodel', 'tool_analytics'));
130 while (!\core_analytics\manager::is_valid($input, '\core_analytics\local\time_splitting\base') && $input !== 'none') {
131 mtrace(get_string('errorunexistingtimesplitting', 'analytics'));
132 $input = cli_input(get_string('clienablemodel', 'tool_analytics'));
135 if ($input === 'none') {
136 exit(0);
139 // Refresh the instance to prevent unexpected issues.
140 $model = new \core_analytics\model($options['modelid']);
142 // Set the time splitting method file and enable it.
143 $model->enable($input);
145 mtrace(get_string('trainandpredictmodel', 'tool_analytics'));
147 // Train the model with the selected time splitting method and start predicting.
148 $model->train();
149 $model->predict();
152 exit(0);