model.php 6.73 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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/>.

/**
 * Model-related actions.
 *
20
 * @package    tool_analytics
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 * @copyright  2017 David Monllao {@link http://www.davidmonllao.com}
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

require_once(__DIR__ . '/../../../config.php');

$id = required_param('id', PARAM_INT);
$action = required_param('action', PARAM_ALPHANUMEXT);

$context = context_system::instance();

require_login();

$model = new \core_analytics\model($id);
35
\core_analytics\manager::check_can_manage_models();
36
37

$params = array('id' => $id, 'action' => $action);
38
$url = new \moodle_url('/admin/tool/analytics/model.php', $params);
39
40
41
42

switch ($action) {

    case 'edit':
43
        $title = get_string('editmodel', 'tool_analytics', $model->get_target()->get_name());
44
45
        break;
    case 'evaluate':
46
        $title = get_string('evaluatemodel', 'tool_analytics');
47
        break;
48
    case 'getpredictions':
49
        $title = get_string('getpredictions', 'tool_analytics');
50
51
        break;
    case 'log':
52
        $title = get_string('viewlog', 'tool_analytics');
53
        break;
54
55
56
57
58
59
    case 'enable':
        $title = get_string('enable');
        break;
    case 'disable':
        $title = get_string('disable');
        break;
60
61
62
    case 'export':
        $title = get_string('export', 'tool_analytics');
        break;
63

64
    default:
65
        throw new moodle_exception('errorunknownaction', 'analytics');
66
67
68
69
70
71
72
73
}

$PAGE->set_context($context);
$PAGE->set_url($url);
$PAGE->set_pagelayout('report');
$PAGE->set_title($title);
$PAGE->set_heading($title);

74
75
76
77
78
79
$onlycli = get_config('analytics', 'onlycli');
if ($onlycli === false) {
    // Default applied if no config found.
    $onlycli = 1;
}

80
81
switch ($action) {

82
83
84
85
86
87
88
89
    case 'enable':
        $model->enable();
        redirect(new \moodle_url('/admin/tool/analytics/index.php'));

    case 'disable':
        $model->update(0, false, false);
        redirect(new \moodle_url('/admin/tool/analytics/index.php'));

90
91
    case 'edit':

92
93
        if ($model->is_static()) {
            echo $OUTPUT->header();
94
            throw new moodle_exception('errornostaticedit', 'tool_analytics');
95
96
        }

97
98
99
100
101
102
        $customdata = array(
            'id' => $model->get_id(),
            'model' => $model,
            'indicators' => $model->get_potential_indicators(),
            'timesplittings' => \core_analytics\manager::get_enabled_time_splitting_methods()
        );
103
        $mform = new \tool_analytics\output\form\edit_model(null, $customdata);
104
105

        if ($mform->is_cancelled()) {
106
            redirect(new \moodle_url('/admin/tool/analytics/index.php'));
107
108
109
110
111
112
113

        } else if ($data = $mform->get_data()) {
            confirm_sesskey();

            // Converting option names to class names.
            $indicators = array();
            foreach ($data->indicators as $indicator) {
114
                $indicatorclass = \tool_analytics\output\helper::option_to_class($indicator);
115
116
                $indicators[] = \core_analytics\manager::get_indicator($indicatorclass);
            }
117
            $timesplitting = \tool_analytics\output\helper::option_to_class($data->timesplitting);
118
            $model->update($data->enabled, $indicators, $timesplitting);
119
            redirect(new \moodle_url('/admin/tool/analytics/index.php'));
120
121
122
123
124
125
        }

        echo $OUTPUT->header();

        $modelobj = $model->get_model_obj();

126
        $callable = array('\tool_analytics\output\helper', 'class_to_option');
127
        $modelobj->indicators = array_map($callable, json_decode($modelobj->indicators));
128
        $modelobj->timesplitting = \tool_analytics\output\helper::class_to_option($modelobj->timesplitting);
129
130
131
132
133
134
        $mform->set_data($modelobj);
        $mform->display();
        break;

    case 'evaluate':
        echo $OUTPUT->header();
135
136

        if ($model->is_static()) {
137
            throw new moodle_exception('errornostaticevaluate', 'tool_analytics');
138
139
        }

140
141
142
143
        if ($onlycli) {
            throw new moodle_exception('erroronlycli', 'tool_analytics');
        }

144
145
146
147
        // Web interface is used by people who can not use CLI nor code stuff, always use
        // cached stuff as they will change the model through the web interface as well
        // which invalidates the previously analysed stuff.
        $results = $model->evaluate(array('reuseprevanalysed' => true));
148
        $renderer = $PAGE->get_renderer('tool_analytics');
149
150
151
        echo $renderer->render_evaluate_results($results, $model->get_analyser()->get_logs());
        break;

152
    case 'getpredictions':
153
154
        echo $OUTPUT->header();

155
156
157
158
        if ($onlycli) {
            throw new moodle_exception('erroronlycli', 'tool_analytics');
        }

159
160
161
162
163
164
165
166
        $trainresults = $model->train();
        $trainlogs = $model->get_analyser()->get_logs();

        // Looks dumb to get a new instance but better be conservative.
        $model = new \core_analytics\model($model->get_model_obj());
        $predictresults = $model->predict();
        $predictlogs = $model->get_analyser()->get_logs();

167
        $renderer = $PAGE->get_renderer('tool_analytics');
168
        echo $renderer->render_get_predictions_results($trainresults, $trainlogs, $predictresults, $predictlogs);
169
170
171
172
        break;

    case 'log':
        echo $OUTPUT->header();
173
174

        if ($model->is_static()) {
175
            throw new moodle_exception('errornostaticlog', 'tool_analytics');
176
177
        }

178
179
        $renderer = $PAGE->get_renderer('tool_analytics');
        $modellogstable = new \tool_analytics\output\model_logs('model-' . $model->get_id(), $model);
180
181
        echo $renderer->render_table($modellogstable);
        break;
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

    case 'export':

        if ($model->is_static() || !$model->is_trained()) {
            throw new moodle_exception('errornoexport', 'tool_analytics');
        }

        $file = $model->get_training_data();
        if (!$file) {
            redirect(new \moodle_url('/admin/tool/analytics/index.php'), get_string('errortrainingdataexport', 'tool_analytics'),
                null, \core\output\notification::NOTIFY_ERROR);
        }

        $filename = 'training-data.' . $model->get_id() . '.' . time() . '.csv';
        send_file($file, $filename, null, 0, false, true);
        break;
198
199
200
}

echo $OUTPUT->footer();