From b73e1cdb57670b0e72b7e69d4c49a16eb0aed964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Monlla=C3=B3?= Date: Mon, 25 Mar 2019 14:48:09 +0100 Subject: [PATCH 1/2] MDL-65177 tool_analytics: Static models can now be edited Only the time-splitting method. --- .../classes/output/form/edit_model.php | 73 +++++++++++-------- .../analytics/classes/output/models_list.php | 29 ++++---- admin/tool/analytics/createmodel.php | 3 +- .../tool/analytics/lang/en/tool_analytics.php | 1 - admin/tool/analytics/model.php | 32 ++++---- 5 files changed, 77 insertions(+), 61 deletions(-) diff --git a/admin/tool/analytics/classes/output/form/edit_model.php b/admin/tool/analytics/classes/output/form/edit_model.php index 31d0f578606..b529d50932f 100644 --- a/admin/tool/analytics/classes/output/form/edit_model.php +++ b/admin/tool/analytics/classes/output/form/edit_model.php @@ -45,13 +45,14 @@ class edit_model extends \moodleform { $mform = $this->_form; - if ($this->_customdata['trainedmodel']) { + if ($this->_customdata['trainedmodel'] && $this->_customdata['staticmodel'] === false) { $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')); + // Target. if (!empty($this->_customdata['targets'])) { $targets = array('' => ''); foreach ($this->_customdata['targets'] as $classname => $target) { @@ -64,42 +65,48 @@ class edit_model extends \moodleform { $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); - $indicators[$optionname] = $indicator->get_name(); + // Indicators. + if (!$this->_customdata['staticmodel']) { + $indicators = array(); + foreach ($this->_customdata['indicators'] as $classname => $indicator) { + $optionname = \tool_analytics\output\helper::class_to_option($classname); + $indicators[$optionname] = $indicator->get_name(); + } + $options = array( + 'multiple' => true + ); + $mform->addElement('autocomplete', 'indicators', get_string('indicators', 'tool_analytics'), $indicators, $options); + $mform->setType('indicators', PARAM_ALPHANUMEXT); + $mform->addHelpButton('indicators', 'indicators', 'tool_analytics'); } - $options = array( - 'multiple' => true - ); - $mform->addElement('autocomplete', 'indicators', get_string('indicators', 'tool_analytics'), $indicators, $options); - $mform->setType('indicators', PARAM_ALPHANUMEXT); - $mform->addHelpButton('indicators', 'indicators', 'tool_analytics'); + // Time-splitting methods. $timesplittings = array('' => ''); foreach ($this->_customdata['timesplittings'] as $classname => $timesplitting) { $optionname = \tool_analytics\output\helper::class_to_option($classname); $timesplittings[$optionname] = $timesplitting->get_name(); } - $mform->addElement('select', 'timesplitting', get_string('timesplittingmethod', 'analytics'), $timesplittings); $mform->addHelpButton('timesplitting', 'timesplittingmethod', 'analytics'); - $defaultprocessor = \core_analytics\manager::get_predictions_processor_name( - \core_analytics\manager::get_predictions_processor() - ); - $predictionprocessors = ['' => get_string('defaultpredictoroption', 'analytics', $defaultprocessor)]; - foreach ($this->_customdata['predictionprocessors'] as $classname => $predictionsprocessor) { - if ($predictionsprocessor->is_ready() !== true) { - continue; + // Predictions processor. + if (!$this->_customdata['staticmodel']) { + $defaultprocessor = \core_analytics\manager::get_predictions_processor_name( + \core_analytics\manager::get_predictions_processor() + ); + $predictionprocessors = ['' => get_string('defaultpredictoroption', 'analytics', $defaultprocessor)]; + foreach ($this->_customdata['predictionprocessors'] as $classname => $predictionsprocessor) { + if ($predictionsprocessor->is_ready() !== true) { + continue; + } + $optionname = \tool_analytics\output\helper::class_to_option($classname); + $predictionprocessors[$optionname] = \core_analytics\manager::get_predictions_processor_name($predictionsprocessor); } - $optionname = \tool_analytics\output\helper::class_to_option($classname); - $predictionprocessors[$optionname] = \core_analytics\manager::get_predictions_processor_name($predictionsprocessor); - } - $mform->addElement('select', 'predictionsprocessor', get_string('predictionsprocessor', 'analytics'), - $predictionprocessors); - $mform->addHelpButton('predictionsprocessor', 'predictionsprocessor', 'analytics'); + $mform->addElement('select', 'predictionsprocessor', get_string('predictionsprocessor', 'analytics'), + $predictionprocessors); + $mform->addHelpButton('predictionsprocessor', 'predictionsprocessor', 'analytics'); + } if (!empty($this->_customdata['id'])) { $mform->addElement('hidden', 'id', $this->_customdata['id']); @@ -130,13 +137,15 @@ class edit_model extends \moodleform { } } - if (empty($data['indicators'])) { - $errors['indicators'] = get_string('errornoindicators', 'analytics'); - } else { - foreach ($data['indicators'] as $indicator) { - $realindicatorname = \tool_analytics\output\helper::option_to_class($indicator); - if (\core_analytics\manager::is_valid($realindicatorname, '\core_analytics\local\indicator\base') === false) { - $errors['indicators'] = get_string('errorinvalidindicator', 'analytics', $realindicatorname); + if (!$this->_customdata['staticmodel']) { + if (empty($data['indicators'])) { + $errors['indicators'] = get_string('errornoindicators', 'analytics'); + } else { + foreach ($data['indicators'] as $indicator) { + $realindicatorname = \tool_analytics\output\helper::option_to_class($indicator); + if (\core_analytics\manager::is_valid($realindicatorname, '\core_analytics\local\indicator\base') === false) { + $errors['indicators'] = get_string('errorinvalidindicator', 'analytics', $realindicatorname); + } } } } diff --git a/admin/tool/analytics/classes/output/models_list.php b/admin/tool/analytics/classes/output/models_list.php index 5152b6d273f..e1d115b2114 100644 --- a/admin/tool/analytics/classes/output/models_list.php +++ b/admin/tool/analytics/classes/output/models_list.php @@ -210,12 +210,10 @@ class models_list implements \renderable, \templatable { } // Edit model. - if (!$model->is_static()) { - $urlparams['action'] = 'edit'; - $url = new \moodle_url('model.php', $urlparams); - $icon = new \action_menu_link_secondary($url, new \pix_icon('t/edit', get_string('edit')), get_string('edit')); - $actionsmenu->add($icon); - } + $urlparams['action'] = 'edit'; + $url = new \moodle_url('model.php', $urlparams); + $icon = new \action_menu_link_secondary($url, new \pix_icon('t/edit', get_string('edit')), get_string('edit')); + $actionsmenu->add($icon); // Enable / disable. if ($model->is_enabled() || !empty($modeldata->timesplitting)) { @@ -280,14 +278,17 @@ class models_list implements \renderable, \templatable { $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); + // Delete model. + if (!$model->is_static()) { + $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); diff --git a/admin/tool/analytics/createmodel.php b/admin/tool/analytics/createmodel.php index bf1701c201e..e5b9b926bcf 100644 --- a/admin/tool/analytics/createmodel.php +++ b/admin/tool/analytics/createmodel.php @@ -40,9 +40,10 @@ $targets = array_filter(\core_analytics\manager::get_all_targets(), function($ta $customdata = array( 'trainedmodel' => false, + 'staticmodel' => false, 'targets' => $targets, 'indicators' => \core_analytics\manager::get_all_indicators(), - 'timesplittings' => \core_analytics\manager::get_enabled_time_splitting_methods(), + 'timesplittings' => \core_analytics\manager::get_all_time_splittings(), 'predictionprocessors' => \core_analytics\manager::get_all_prediction_processors(), ); $mform = new \tool_analytics\output\form\edit_model(null, $customdata); diff --git a/admin/tool/analytics/lang/en/tool_analytics.php b/admin/tool/analytics/lang/en/tool_analytics.php index b3a8ba42cf1..fed87809bfb 100644 --- a/admin/tool/analytics/lang/en/tool_analytics.php +++ b/admin/tool/analytics/lang/en/tool_analytics.php @@ -46,7 +46,6 @@ $string['errorcantenablenotimesplitting'] = 'You need to select a time-splitting $string['errornoenabledandtrainedmodels'] = 'There are no enabled and trained models to predict.'; $string['errornoenabledmodels'] = 'There are no enabled models to train.'; $string['errornoexport'] = 'Only trained models can be exported'; -$string['errornostaticedit'] = 'Models based on assumptions cannot be edited.'; $string['errornostaticevaluated'] = 'Models based on assumptions cannot be evaluated. They are always 100% correct according to how they were defined.'; $string['errornostaticlog'] = 'Models based on assumptions cannot be evaluated because there is no performance log.'; $string['erroronlycli'] = 'Execution only allowed via command line'; diff --git a/admin/tool/analytics/model.php b/admin/tool/analytics/model.php index ab2fe61796a..2d225b70ef4 100644 --- a/admin/tool/analytics/model.php +++ b/admin/tool/analytics/model.php @@ -103,21 +103,19 @@ switch ($action) { case 'delete': confirm_sesskey(); - $model->delete(); + if (!$model->is_static()) { + $model->delete(); + } redirect($returnurl); break; case 'edit': confirm_sesskey(); - if ($model->is_static()) { - echo $OUTPUT->header(); - throw new moodle_exception('errornostaticedit', 'tool_analytics'); - } - $customdata = array( 'id' => $model->get_id(), 'trainedmodel' => $model->is_trained(), + 'staticmodel' => $model->is_static(), 'indicators' => $model->get_potential_indicators(), 'timesplittings' => \core_analytics\manager::get_all_time_splittings(), 'predictionprocessors' => \core_analytics\manager::get_all_prediction_processors() @@ -129,14 +127,22 @@ switch ($action) { } else if ($data = $mform->get_data()) { - // Converting option names to class names. - $indicators = array(); - foreach ($data->indicators as $indicator) { - $indicatorclass = \tool_analytics\output\helper::option_to_class($indicator); - $indicators[] = \core_analytics\manager::get_indicator($indicatorclass); - } $timesplitting = \tool_analytics\output\helper::option_to_class($data->timesplitting); - $predictionsprocessor = \tool_analytics\output\helper::option_to_class($data->predictionsprocessor); + + if (!$model->is_static()) { + // Converting option names to class names. + $indicators = array(); + foreach ($data->indicators as $indicator) { + $indicatorclass = \tool_analytics\output\helper::option_to_class($indicator); + $indicators[] = \core_analytics\manager::get_indicator($indicatorclass); + } + $predictionsprocessor = \tool_analytics\output\helper::option_to_class($data->predictionsprocessor); + } else { + // These fields can not be modified. + $indicators = false; + $predictionsprocessor = false; + } + $model->update($data->enabled, $indicators, $timesplitting, $predictionsprocessor); redirect($returnurl); } From 5bbdc42039b26469f221ca0ad4cbe08ec6b9c715 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Monlla=C3=B3?= Date: Mon, 25 Mar 2019 15:04:12 +0100 Subject: [PATCH 2/2] MDL-65177 analytics: New time-splitting methods for upcoming events --- lang/en/moodle.php | 4 ++ .../time_splitting/upcoming_3_days.php | 53 +++++++++++++++++++ .../time_splitting/upcoming_fortnight.php | 53 +++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 lib/classes/analytics/time_splitting/upcoming_3_days.php create mode 100644 lib/classes/analytics/time_splitting/upcoming_fortnight.php diff --git a/lang/en/moodle.php b/lang/en/moodle.php index f531b32d335..82ee9068fd5 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -2021,6 +2021,10 @@ $string['timesplitting:quartersaccum'] = 'Quarters accumulative'; $string['timesplitting:quartersaccum_help'] = 'This time-splitting method divides the course into quarters (4 equal parts), with each prediction being based on the data of all previous quarters.'; $string['timesplitting:singlerange'] = 'Single range'; $string['timesplitting:singlerange_help'] = 'This time-splitting method considers the entire course as a single span.'; +$string['timesplitting:upcoming3days'] = 'Upcoming 3 days'; +$string['timesplitting:upcoming3days_help'] = 'This time-splitting method generates predictions every 3 days. The indicators calculations will be based on the upcoming 3 days.'; +$string['timesplitting:upcomingfortnight'] = 'Upcoming fortnight'; +$string['timesplitting:upcomingfortnight_help'] = 'This time-splitting method generates predictions every fortnight. The indicators calculations will be based on the upcoming fortnight.'; $string['timesplitting:upcomingweek'] = 'Upcoming week'; $string['timesplitting:upcomingweek_help'] = 'This time-splitting method generates predictions every week. The indicators calculations will be based on the upcoming week.'; $string['thanks'] = 'Thanks'; diff --git a/lib/classes/analytics/time_splitting/upcoming_3_days.php b/lib/classes/analytics/time_splitting/upcoming_3_days.php new file mode 100644 index 00000000000..bb86d056649 --- /dev/null +++ b/lib/classes/analytics/time_splitting/upcoming_3_days.php @@ -0,0 +1,53 @@ +. + +/** + * Time splitting method that generates insights every three days and calculates indicators using upcoming dates. + * + * @package core_analytics + * @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\time_splitting; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Time splitting method that generates insights every three days and calculates indicators using upcoming dates. + * + * @package core_analytics + * @copyright 2019 David Monllao {@link http://www.davidmonllao.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class upcoming_3_days extends \core_analytics\local\time_splitting\upcoming_periodic { + + /** + * The time splitting method name. + * @return \lang_string + */ + public static function get_name() : \lang_string { + return new \lang_string('timesplitting:upcoming3days'); + } + + /** + * Once every three days. + * @return \DateInterval + */ + public function periodicity() { + return new \DateInterval('P3D'); + } +} \ No newline at end of file diff --git a/lib/classes/analytics/time_splitting/upcoming_fortnight.php b/lib/classes/analytics/time_splitting/upcoming_fortnight.php new file mode 100644 index 00000000000..0b6aeb7bf92 --- /dev/null +++ b/lib/classes/analytics/time_splitting/upcoming_fortnight.php @@ -0,0 +1,53 @@ +. + +/** + * Time splitting method that generates insights every fortnight and calculates indicators using upcoming dates. + * + * @package core_analytics + * @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\time_splitting; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Time splitting method that generates insights every fortnight and calculates indicators using upcoming dates. + * + * @package core_analytics + * @copyright 2019 David Monllao {@link http://www.davidmonllao.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class upcoming_fortnight extends \core_analytics\local\time_splitting\upcoming_periodic { + + /** + * The time splitting method name. + * @return \lang_string + */ + public static function get_name() : \lang_string { + return new \lang_string('timesplitting:upcomingfortnight'); + } + + /** + * Every two weeks. + * @return \DateInterval + */ + public function periodicity() { + return new \DateInterval('P2W'); + } +} \ No newline at end of file