Skip to content
Permalink
Browse files

MDL-60944 tool_analytics: Adding create and delete features

Extra modifications to refine the preexisting patch.
  • Loading branch information...
dmonllao committed Jan 23, 2019
1 parent 349c441 commit e4453adc55cce4f72cded3349dbd0034f357cc60

Some generated files are not rendered by default. Learn more.

@@ -36,6 +36,15 @@ define(['jquery', 'core/str', 'core/log', 'core/notification', 'core/modal_facto
component: 'tool_analytics'
}

},
'delete': {
title: {
key: 'delete',
component: 'tool_analytics'
}, body: {
key: 'deletemodelconfirmation',
component: 'tool_analytics'
}
}
};

@@ -45,13 +45,25 @@ public function definition() {
$mform = $this->_form;
if ($this->_customdata['model']->is_trained()) {
if ($this->_customdata['trainedmodel']) {
$message = get_string('edittrainedwarning', 'tool_analytics');
$mform->addElement('html', $OUTPUT->notification($message, \core\output\notification::NOTIFY_WARNING));
}
$mform->addElement('advcheckbox', 'enabled', get_string('enabled', 'tool_analytics'));
if (!empty($this->_customdata['targets'])) {
$targets = array('' => '');
foreach ($this->_customdata['targets'] as $classname => $target) {
$optionname = \tool_analytics\output\helper::class_to_option($classname);
$targets[$optionname] = $target->get_name();
}
$mform->addElement('select', 'target', get_string('target', 'tool_analytics'), $targets);
$mform->addHelpButton('target', 'target', 'tool_analytics');
$mform->addRule('target', get_string('required'), 'required', null, 'client');
}
$indicators = array();
foreach ($this->_customdata['indicators'] as $classname => $indicator) {
$optionname = \tool_analytics\output\helper::class_to_option($classname);
@@ -88,11 +100,13 @@ public function definition() {
$predictionprocessors);
$mform->addHelpButton('predictionsprocessor', 'predictionsprocessor', 'analytics');
$mform->addElement('hidden', 'id', $this->_customdata['id']);
$mform->setType('id', PARAM_INT);
if (!empty($this->_customdata['id'])) {
$mform->addElement('hidden', 'id', $this->_customdata['id']);
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'action', 'edit');
$mform->setType('action', PARAM_ALPHANUMEXT);
$mform->addElement('hidden', 'action', 'edit');
$mform->setType('action', PARAM_ALPHANUMEXT);
}
$this->add_action_buttons();
}
@@ -22,9 +22,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_analytics;
defined('MOODLE_INTERNAL') || die();
namespace tool_analytics\output\form;
defined('MOODLE_INTERNAL') || die();
/**
* Model upload form.
@@ -33,15 +33,24 @@
* @copyright 2017 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class import_model_form extends \moodleform {
function definition () {
class import_model extends \moodleform {
/**
* Form definition.
*
* @return null
*/
public function definition () {
$mform = $this->_form;
$mform->addElement('header', 'settingsheader', get_string('analyticsimportmodel', 'tool_analytics'));
$mform->addElement('header', 'settingsheader', get_string('importmodel', 'tool_analytics'));
$mform->addElement('filepicker', 'modelfile', get_string('file'), null, ['accepted_types' => '.json']);
$mform->addRule('modelfile', null, 'required');
$this->add_action_buttons(false, get_string('submit'));
$mform->addElement('advcheckbox', 'ignoreversionmismatches', get_string('ignoreversionmismatches', 'tool_analytics'),
get_string('ignoreversionmismatchescheckbox', 'tool_analytics'));
$this->add_action_buttons(true, get_string('import'));
}
}
}
@@ -58,4 +58,38 @@ public static function option_to_class($option) {
// Really unlikely but yeah, I'm a bad booyyy.
return str_replace('2015102400ouuu', '\\', $option);
}
/**
* Sets an analytics > analytics models > $title breadcrumb.
*
* @param string $title
* @param \moodle_url $url
* @param \context|false $context Defaults to context_system
* @return null
*/
public static function set_navbar(string $title, \moodle_url $url, ?\context $context = null) {
global $PAGE;
if (!$context) {
$context = \context_system::instance();
}
$PAGE->set_context($context);
$PAGE->set_url($url);
if ($siteadmin = $PAGE->settingsnav->find('root', \navigation_node::TYPE_SITE_ADMIN)) {
$PAGE->navbar->add($siteadmin->get_content(), $siteadmin->action());
}
if ($analytics = $PAGE->settingsnav->find('analytics', \navigation_node::TYPE_SETTING)) {
$PAGE->navbar->add($analytics->get_content(), $analytics->action());
}
if ($analyticmodels = $PAGE->settingsnav->find('analyticmodels', \navigation_node::TYPE_SETTING)) {
$PAGE->navbar->add($analyticmodels->get_content(), $analyticmodels->action());
}
$PAGE->navbar->add($title, $url);
$PAGE->set_pagelayout('report');
$PAGE->set_title($title);
$PAGE->set_heading($title);
}
}
@@ -62,6 +62,8 @@ public function export_for_template(\renderer_base $output) {
global $PAGE;
$data = new \stdClass();
$data->importmodelurl = new \moodle_url('/admin/tool/analytics/importmodel.php');
$data->createmodelurl = new \moodle_url('/admin/tool/analytics/createmodel.php');
$onlycli = get_config('analytics', 'onlycli');
if ($onlycli === false) {
@@ -232,15 +234,15 @@ public function export_for_template(\renderer_base $output) {
$urlparams['action'] = 'exportdata';
$url = new \moodle_url('model.php', $urlparams);
$icon = new \action_menu_link_secondary($url, new \pix_icon('i/export',
get_string('exporttrainingdata', 'tool_analytics')), get_string('export', 'tool_analytics'));
get_string('exporttrainingdata', 'tool_analytics')), get_string('exporttrainingdata', 'tool_analytics'));
$actionsmenu->add($icon);
}
// Export model.
if (!$model->is_static() && $model->get_indicators() && !empty($modeldata->timesplitting)) {
$urlparams['action'] = 'exportmodel';
$url = new \moodle_url('model.php', $urlparams);
$icon = new \action_menu_link_secondary($url, new \pix_icon('i/export',
$icon = new \action_menu_link_secondary($url, new \pix_icon('i/backup',
get_string('exportmodel', 'tool_analytics')), get_string('exportmodel', 'tool_analytics'));
$actionsmenu->add($icon);
}
@@ -267,6 +269,15 @@ public function export_for_template(\renderer_base $output) {
$actionsmenu->add($icon);
}
$actionid = 'delete-' . $model->get_id();
$PAGE->requires->js_call_amd('tool_analytics/model', 'confirmAction', [$actionid, 'delete']);
$urlparams['action'] = 'delete';
$url = new \moodle_url('model.php', $urlparams);
$icon = new \action_menu_link_secondary($url, new \pix_icon('t/delete',
get_string('delete', 'tool_analytics')), get_string('delete', 'tool_analytics'),
['data-action-id' => $actionid]);
$actionsmenu->add($icon);
$modeldata->actions = $actionsmenu->export_for_template($output);
$data->models[] = $modeldata;
@@ -0,0 +1,91 @@
<?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/>.
/**
* Create model form.
*
* @package tool_analytics
* @copyright 2019 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');
require_login();
\core_analytics\manager::check_can_manage_models();
$returnurl = new \moodle_url('/admin/tool/analytics/index.php');
$url = new \moodle_url('/admin/tool/analytics/createmodel.php');
$title = get_string('createmodel', 'tool_analytics');
\tool_analytics\output\helper::set_navbar($title, $url);
// Static targets are not editable, we discard them.
$targets = array_filter(\core_analytics\manager::get_all_targets(), function($target) {
return (!$target->based_on_assumptions());
});
$customdata = array(
'trainedmodel' => false,
'targets' => $targets,
'indicators' => \core_analytics\manager::get_all_indicators(),
'timesplittings' => \core_analytics\manager::get_enabled_time_splitting_methods(),
'predictionprocessors' => \core_analytics\manager::get_all_prediction_processors(),
);
$mform = new \tool_analytics\output\form\edit_model(null, $customdata);
if ($mform->is_cancelled()) {
redirect($returnurl);
} else if ($data = $mform->get_data()) {
// Converting option names to class names.
$targetclass = \tool_analytics\output\helper::option_to_class($data->target);
$target = \core_analytics\manager::get_target($targetclass);
$indicators = array();
foreach ($data->indicators as $indicator) {
$indicatorinstance = \core_analytics\manager::get_indicator(
\tool_analytics\output\helper::option_to_class($indicator)
);
$indicators[$indicatorinstance->get_id()] = $indicatorinstance;
}
$timesplitting = \tool_analytics\output\helper::option_to_class($data->timesplitting);
$predictionsprocessor = \tool_analytics\output\helper::option_to_class($data->predictionsprocessor);
// Insert the model into db.
$model = \core_analytics\model::create($target, []);
// Filter out indicators that can not be used by this target.
$invalidindicators = array_diff_key($indicators, $model->get_potential_indicators());
if ($invalidindicators) {
$indicators = array_diff_key($indicators, $invalidindicators);
}
// Update the model with the valid list of indicators.
$model->update($data->enabled, $indicators, $timesplitting, $predictionsprocessor);
$message = '';
$messagetype = \core\output\notification::NOTIFY_SUCCESS;
if (!empty($invalidindicators)) {
$message = get_string('invalidindicatorsremoved', 'tool_analytics');
}
redirect($returnurl, $message, 0, $messagetype);
}
echo $OUTPUT->header();
$mform->display();
echo $OUTPUT->footer();
@@ -23,35 +23,41 @@
*/
require_once(__DIR__ . '/../../../config.php');
require_once($CFG->libdir . '/adminlib.php');
admin_externalpage_setup('analyticsmodelimport', '', null, '', array('pagelayout' => 'report'));
echo $OUTPUT->header();
require_login();
\core_analytics\manager::check_can_manage_models();
$returnurl = new \moodle_url('/admin/tool/analytics/index.php');
$url = new \moodle_url('/admin/tool/analytics/importmodel.php');
$title = get_string('importmodel', 'tool_analytics');
\tool_analytics\output\helper::set_navbar($title, $url);
$form = new \tool_analytics\output\form\import_model();
if ($form->is_cancelled()) {
redirect($returnurl);
} else if ($data = $form->get_data()) {
$modelconfig = new \core_analytics\model_config();
$form = new tool_analytics\import_model_form();
if ($data = $form->get_data()) {
$content = json_decode($form->get_file_content('modelfile'));
if (empty($content->moodleversion)) {
// Should never happen.
echo $OUTPUT->notification(get_string('missingmoodleversion', 'tool_analytics'), 'error');
} else {
if ($content->moodleversion != $CFG->version) {
$a = new stdClass();
$a->importedversion = $content->moodleversion;
$a->version = $CFG->version;
echo $OUTPUT->notification(get_string('versionnotsame', 'tool_analytics', $a), 'warning');
}
$model = \core_analytics\model::create_from_json($content);
if ($model) {
echo $OUTPUT->notification(get_string('success'), 'notifysuccess');
} else {
echo $OUTPUT->notification(get_string('error'), 'error');
}
$json = $form->get_file_content('modelfile');
if ($error = $modelconfig->check_json_data($json)) {
// The provided file is not ok.
redirect($url, $error, 0, \core\output\notification::NOTIFY_ERROR);
}
$modeldata = json_decode($json);
if ($error = $modelconfig->check_dependencies($modeldata, $data->ignoreversionmismatches)) {
// The file is not available until the form is validated so we need an alternative method to show errors.
redirect($url, $error, 0, \core\output\notification::NOTIFY_ERROR);
}
echo $OUTPUT->single_button(new moodle_url("$CFG->wwwroot/$CFG->admin/tool/analytics/index.php"),
get_string('continue'), 'get');
} else {
$form->display();
$model = \core_analytics\model::create_from_import($modeldata, true);
redirect($returnurl, get_string('importedsuccessfully', 'tool_analytics'), 0,
\core\output\notification::NOTIFY_SUCCESS);
}
echo $OUTPUT->header();
$form->display();
echo $OUTPUT->footer();

0 comments on commit e4453ad

Please sign in to comment.
You can’t perform that action at this time.