Merge branch 'MDL-64703-master' of git://github.com/bmbrands/moodle
authorAdrian Greeve <abgreeve@gmail.com>
Thu, 18 Apr 2019 01:10:52 +0000 (09:10 +0800)
committerAdrian Greeve <abgreeve@gmail.com>
Thu, 18 Apr 2019 01:12:43 +0000 (09:12 +0800)
356 files changed:
admin/settings/analytics.php
admin/settings/courses.php
admin/settings/development.php
admin/settings/subsystems.php
admin/tool/analytics/amd/build/model.min.js
admin/tool/analytics/amd/src/model.js
admin/tool/analytics/classes/clihelper.php
admin/tool/analytics/classes/output/form/edit_model.php
admin/tool/analytics/classes/output/helper.php
admin/tool/analytics/classes/output/invalid_analysables.php
admin/tool/analytics/classes/output/models_list.php
admin/tool/analytics/classes/output/restorable_models.php [new file with mode: 0644]
admin/tool/analytics/classes/task/predict_models.php
admin/tool/analytics/classes/task/train_models.php
admin/tool/analytics/cli/evaluate_model.php
admin/tool/analytics/createmodel.php
admin/tool/analytics/lang/en/tool_analytics.php
admin/tool/analytics/model.php
admin/tool/analytics/restoredefault.php [new file with mode: 0644]
admin/tool/analytics/templates/evaluation_options.mustache [new file with mode: 0644]
admin/tool/analytics/templates/models_list.mustache
admin/tool/analytics/templates/restorable_models.mustache [new file with mode: 0644]
admin/tool/analytics/tests/behat/restoredefault.feature [new file with mode: 0644]
admin/tool/analytics/version.php
admin/tool/dataprivacy/classes/api.php
admin/tool/dataprivacy/classes/external/data_request_exporter.php
admin/tool/dataprivacy/classes/output/data_registry_page.php
admin/tool/dataprivacy/classes/output/data_requests_table.php
admin/tool/dataprivacy/createdatarequest.php
admin/tool/dataprivacy/createdatarequest_form.php
admin/tool/dataprivacy/db/access.php
admin/tool/dataprivacy/lang/en/tool_dataprivacy.php
admin/tool/dataprivacy/lib.php
admin/tool/dataprivacy/resubmitrequest.php
admin/tool/dataprivacy/tests/api_test.php
admin/tool/dataprivacy/tests/behat/datadelete.feature
admin/tool/dataprivacy/version.php
admin/tool/generator/classes/make_testplan_form.php
admin/tool/generator/classes/testplan_backend.php
admin/tool/generator/upgrade.txt [new file with mode: 0644]
admin/tool/log/classes/helper/buffered_writer.php
admin/tool/log/classes/helper/reader.php
admin/tool/log/classes/local/privacy/helper.php
admin/tool/log/store/database/classes/log/store.php
admin/tool/log/store/database/db/upgrade.php
admin/tool/log/store/database/lang/en/logstore_database.php
admin/tool/log/store/database/settings.php
admin/tool/log/store/database/tests/store_test.php
admin/tool/log/store/database/version.php
admin/tool/log/store/standard/classes/log/store.php
admin/tool/log/store/standard/db/upgrade.php
admin/tool/log/store/standard/lang/en/logstore_standard.php
admin/tool/log/store/standard/settings.php
admin/tool/log/store/standard/tests/store_test.php
admin/tool/log/store/standard/version.php
admin/tool/log/upgrade.txt
admin/tool/mobile/classes/external.php
admin/tool/mobile/db/services.php
admin/tool/mobile/lang/en/tool_mobile.php
admin/tool/mobile/tests/externallib_test.php
admin/tool/mobile/upgrade.txt
admin/tool/recyclebin/classes/category_bin.php
admin/tool/recyclebin/classes/course_bin.php
admin/tool/recyclebin/tests/category_bin_test.php
admin/tool/recyclebin/tests/course_bin_test.php
admin/user/user_bulk_message.php
analytics/classes/analysable.php
analytics/classes/analysis.php [new file with mode: 0644]
analytics/classes/course.php
analytics/classes/dataset_manager.php
analytics/classes/insights_generator.php [new file with mode: 0644]
analytics/classes/local/analyser/base.php
analytics/classes/local/analyser/by_course.php
analytics/classes/local/analyser/sitewide.php
analytics/classes/local/analysis/result.php [new file with mode: 0644]
analytics/classes/local/analysis/result_array.php [new file with mode: 0644]
analytics/classes/local/analysis/result_file.php [new file with mode: 0644]
analytics/classes/local/target/base.php
analytics/classes/local/target/binary.php
analytics/classes/local/target/discrete.php
analytics/classes/local/time_splitting/base.php
analytics/classes/local/time_splitting/periodic.php [new file with mode: 0644]
analytics/classes/local/time_splitting/upcoming_periodic.php [new file with mode: 0644]
analytics/classes/manager.php
analytics/classes/model.php
analytics/classes/prediction_action.php
analytics/classes/user.php [new file with mode: 0644]
analytics/lib.php [new file with mode: 0644]
analytics/templates/insight_info_message.mustache [new file with mode: 0644]
analytics/templates/insight_info_message_prediction.mustache [new file with mode: 0644]
analytics/tests/dataset_manager_test.php
analytics/tests/fixtures/test_analysis.php [moved from analytics/tests/fixtures/test_analyser.php with 79% similarity]
analytics/tests/fixtures/test_indicator_null.php [new file with mode: 0644]
analytics/tests/fixtures/test_site_users_analyser.php
analytics/tests/fixtures/test_target_shortname.php
analytics/tests/fixtures/test_target_site_users.php
analytics/tests/fixtures/test_timesplitting_seconds.php [new file with mode: 0644]
analytics/tests/fixtures/test_timesplitting_weekly.php [new file with mode: 0644]
analytics/tests/manager_test.php
analytics/tests/model_test.php
analytics/tests/prediction_test.php
analytics/tests/privacy_test.php
analytics/upgrade.txt
auth/email/tests/behat/signup.feature
auth/nologin/auth.php
auth/oauth2/classes/auth.php
auth/oauth2/lang/en/auth_oauth2.php
auth/oauth2/tests/auth_test.php [new file with mode: 0644]
auth/upgrade.txt
backup/backup.class.php
backup/backup.php
backup/controller/backup_controller.class.php
backup/controller/restore_controller.class.php
backup/externallib.php [new file with mode: 0644]
backup/restore.php
backup/restorefile.php
backup/tests/async_backup_test.php [new file with mode: 0644]
backup/tests/async_restore_test.php [new file with mode: 0644]
backup/util/dbops/backup_controller_dbops.class.php
backup/util/dbops/restore_controller_dbops.class.php
backup/util/helper/async_helper.class.php [new file with mode: 0644]
backup/util/helper/tests/async_helper_test.php [new file with mode: 0644]
backup/util/includes/backup_includes.php
backup/util/includes/restore_includes.php
backup/util/ui/amd/build/async_backup.min.js [new file with mode: 0644]
backup/util/ui/amd/src/async_backup.js [new file with mode: 0644]
backup/util/ui/renderer.php
backup/util/ui/tests/behat/duplicate_activities.feature
badges/criteria/award_criteria_courseset.php
badges/cron.php [deleted file]
blocks/course_list/block_course_list.php
blocks/html/lib.php
blocks/myoverview/amd/build/view.min.js
blocks/myoverview/amd/src/view.js
blocks/myoverview/block_myoverview.php
blocks/myoverview/classes/output/main.php
blocks/myoverview/lang/en/block_myoverview.php
blocks/myoverview/lib.php
blocks/myoverview/settings.php [new file with mode: 0644]
blocks/myoverview/templates/courses-view.mustache
blocks/myoverview/templates/view-list.mustache
blocks/myoverview/templates/view-summary.mustache
blog/classes/external/post_exporter.php
blog/tests/external_test.php
blog/upgrade.txt
cache/classes/loaders.php
cache/stores/mongodb/addinstanceform.php
calendar/lib.php
calendar/view.php
course/amd/build/actions.min.js
course/amd/src/actions.js
course/classes/analytics/indicator/activities_due.php [new file with mode: 0644]
course/classes/category.php
course/classes/list_element.php
course/classes/management/helper.php
course/classes/management_renderer.php
course/classes/search/course.php
course/classes/search/customfield.php
course/completion_form.php
course/delete.php
course/externallib.php
course/format/renderer.php
course/format/topics/renderer.php
course/index.php
course/info.php
course/lib.php
course/management.php
course/renderer.php
course/search.php
course/tests/behat/app_course_completion.feature [new file with mode: 0644]
course/tests/behat/behat_course.php
course/tests/behat/course_browsing.feature [new file with mode: 0644]
course/tests/category_test.php
course/upgrade.txt
course/view.php
dataformat/pdf/classes/privacy/provider.php [new file with mode: 0644]
dataformat/pdf/classes/writer.php [new file with mode: 0644]
dataformat/pdf/lang/en/dataformat_pdf.php [new file with mode: 0644]
dataformat/pdf/version.php [new file with mode: 0644]
enrol/externallib.php
enrol/guest/classes/external.php
enrol/index.php
enrol/self/externallib.php
grade/import/csv/classes/load_data.php
grade/import/csv/tests/load_data_test.php
install/lang/ar/admin.php
install/lang/de_wp/langconfig.php [new file with mode: 0644]
install/lang/en_wp/langconfig.php [new file with mode: 0644]
install/lang/es_wp/langconfig.php [new file with mode: 0644]
install/lang/fi/moodle.php
install/lang/pt/admin.php
lang/en/admin.php
lang/en/analytics.php
lang/en/backup.php
lang/en/course.php
lang/en/error.php
lang/en/moodle.php
lang/en/role.php
lib/accesslib.php
lib/amd/build/form-course-selector.min.js
lib/amd/build/icon_system_fontawesome.min.js
lib/amd/build/icon_system_standard.min.js
lib/amd/src/form-course-selector.js
lib/amd/src/icon_system_fontawesome.js
lib/amd/src/icon_system_standard.js
lib/authlib.php
lib/badgeslib.php
lib/behat/classes/partial_named_selector.php
lib/classes/analytics/analyser/courses.php
lib/classes/analytics/analyser/site_courses.php
lib/classes/analytics/analyser/student_enrolments.php
lib/classes/analytics/analyser/users.php [new file with mode: 0644]
lib/classes/analytics/target/course_dropout.php
lib/classes/analytics/target/course_enrolments.php
lib/classes/analytics/target/course_gradetopass.php [new file with mode: 0644]
lib/classes/analytics/target/no_teaching.php
lib/classes/analytics/time_splitting/upcoming_3_days.php [new file with mode: 0644]
lib/classes/analytics/time_splitting/upcoming_fortnight.php [new file with mode: 0644]
lib/classes/analytics/time_splitting/upcoming_week.php [new file with mode: 0644]
lib/classes/component.php
lib/classes/lock/db_record_lock_factory.php
lib/classes/lock/file_lock_factory.php
lib/classes/lock/postgres_lock_factory.php
lib/classes/message/message.php
lib/classes/plugin_manager.php
lib/classes/progress/db_updater.php [new file with mode: 0644]
lib/classes/task/asynchronous_backup_task.php [new file with mode: 0644]
lib/classes/task/asynchronous_restore_task.php [new file with mode: 0644]
lib/classes/task/badges_cron_task.php
lib/classes/task/badges_message_task.php [new file with mode: 0644]
lib/datalib.php
lib/db/access.php
lib/db/analytics.php
lib/db/install.xml
lib/db/messages.php
lib/db/services.php
lib/db/tasks.php
lib/db/upgrade.php
lib/deprecatedlib.php
lib/filebrowser/file_info_context_coursecat.php
lib/filelib.php
lib/form/course.php
lib/messagelib.php
lib/mlbackend/php/classes/processor.php
lib/moodlelib.php
lib/navigationlib.php
lib/setuplib.php
lib/templates/async_backup_progress.mustache [moved from admin/tool/analytics/templates/evaluation_mode_selection.mustache with 50% similarity]
lib/templates/async_backup_progress_row.mustache [new file with mode: 0644]
lib/templates/async_backup_status.mustache [new file with mode: 0644]
lib/templates/async_restore_progress_row.mustache [new file with mode: 0644]
lib/testing/classes/util.php
lib/tests/analysers_test.php
lib/tests/behat/app_behat_runtime.js
lib/tests/component_test.php
lib/tests/fixtures/deprecated_analyser.php [new file with mode: 0644]
lib/tests/lock_test.php
lib/tests/moodlelib_test.php
lib/tests/targets_test.php
lib/tests/time_splittings_test.php
lib/upgrade.txt
lib/xhprof/readme_moodle.txt
lib/xhprof/xhprof_moodle.php
message/amd/src/message_drawer_view_overview_section.js
message/classes/api.php
message/classes/helper.php
message/externallib.php
message/lib.php
message/tests/behat/behat_message.php
message/tests/behat/message_drawer_manage_contacts.feature
message/tests/behat/message_send_messages.feature [new file with mode: 0644]
mod/book/lib.php
mod/book/tests/lib_test.php
mod/book/upgrade.txt
mod/chat/tests/lib_test.php
mod/data/classes/external/record_exporter.php
mod/data/tests/externallib_test.php
mod/data/upgrade.txt
mod/forum/amd/build/discussion.min.js [new file with mode: 0644]
mod/forum/amd/build/selectors.min.js
mod/forum/amd/src/discussion.js [new file with mode: 0644]
mod/forum/amd/src/selectors.js
mod/forum/backup/moodle2/backup_forum_stepslib.php
mod/forum/backup/moodle2/restore_forum_stepslib.php
mod/forum/classes/local/data_mappers/legacy/forum.php
mod/forum/classes/local/entities/forum.php
mod/forum/classes/local/exporters/author.php
mod/forum/classes/local/factories/entity.php
mod/forum/classes/local/factories/renderer.php
mod/forum/classes/local/managers/capability.php
mod/forum/classes/local/renderers/discussion.php
mod/forum/classes/local/renderers/discussion_list.php
mod/forum/db/access.php
mod/forum/db/install.xml
mod/forum/db/upgrade.php
mod/forum/externallib.php
mod/forum/lang/en/forum.php
mod/forum/lib.php
mod/forum/locallib.php
mod/forum/mod_form.php
mod/forum/templates/forum_discussion.mustache
mod/forum/templates/forum_discussion_post.mustache
mod/forum/templates/forum_discussion_threaded_post.mustache
mod/forum/tests/entities_forum_test.php
mod/forum/tests/externallib_test.php
mod/forum/tests/lib_test.php
mod/forum/tests/locallib_test.php [new file with mode: 0644]
mod/forum/tests/vaults_post_test.php
mod/forum/upgrade.txt
mod/forum/version.php
mod/glossary/classes/external.php
mod/glossary/tests/external_test.php
mod/glossary/upgrade.txt
mod/lesson/backup/moodle2/backup_lesson_stepslib.php
mod/lesson/backup/moodle2/restore_lesson_stepslib.php
mod/lesson/classes/privacy/provider.php
mod/lesson/continue.php
mod/lesson/editpage.php
mod/lesson/essay.php
mod/lesson/index.php
mod/lesson/lang/en/lesson.php
mod/lesson/lib.php
mod/lesson/locallib.php
mod/lesson/pagetypes/essay.php
mod/lesson/pagetypes/numerical.php
mod/lesson/pagetypes/shortanswer.php
mod/lesson/report.php
mod/lesson/tests/behat/all_other_answers_catch.feature [new file with mode: 0644]
mod/lesson/tests/behat/wrong_answer_continue.feature [new file with mode: 0644]
mod/lesson/upgrade.txt
mod/wiki/classes/external.php
mod/wiki/tests/externallib_test.php
mod/wiki/upgrade.txt
report/insights/classes/output/insight.php
report/insights/classes/output/insights_list.php
report/insights/done.php [new file with mode: 0644]
report/insights/insights.php
report/insights/lang/en/report_insights.php
report/insights/lib.php
report/insights/templates/insight_details.mustache
report/insights/templates/insights_list.mustache
repository/webdav/lib.php
search/classes/manager.php
tag/classes/external.php
tag/classes/external/tag_area_exporter.php [new file with mode: 0644]
tag/classes/external/tag_collection_exporter.php [new file with mode: 0644]
tag/classes/external/tag_item_exporter.php [new file with mode: 0644]
tag/classes/external/util.php [new file with mode: 0644]
tag/tests/external_test.php
theme/boost/scss/moodle/drawer.scss
theme/boost/style/moodle.css
theme/classic/style/moodle.css
user/classes/analytics/target/upcoming_activities_due.php [new file with mode: 0644]
version.php
webservice/lib.php
webservice/tests/lib_test.php

index 5f43a52..dfbb1d6 100644 (file)
@@ -75,7 +75,7 @@ if ($hassiteconfig) {
             $defaultreader, $options));
 
         // Enable/disable time splitting methods.
-        $alltimesplittings = \core_analytics\manager::get_all_time_splittings();
+        $alltimesplittings = \core_analytics\manager::get_time_splitting_methods_for_evaluation(true);
 
         $timesplittingoptions = array();
         $timesplittingdefaults = array('\core\analytics\time_splitting\quarters_accum',
index f214ab4..99892d6 100644 (file)
@@ -458,4 +458,28 @@ if ($hassiteconfig or has_any_capability($capabilities, $systemcontext)) {
 
     $ADMIN->add('backups', $temp);
 
+    // Create a page for asynchronous backup and restore configuration and defaults.
+    if (!empty($CFG->enableasyncbackup)) {  // Only add settings if async mode is enable at site level.
+        $temp = new admin_settingpage('asyncgeneralsettings', new lang_string('asyncgeneralsettings', 'backup'));
+
+        $temp->add(new admin_setting_configcheckbox(
+                'backup/backup_async_message_users',
+                new lang_string('asyncemailenable', 'backup'),
+                new lang_string('asyncemailenabledetail', 'backup'), 0));
+
+        $temp->add(new admin_setting_configtext(
+                'backup/backup_async_message_subject',
+                new lang_string('asyncmessagesubject', 'backup'),
+                new lang_string('asyncmessagesubjectdetail', 'backup'),
+                new lang_string('asyncmessagesubjectdefault', 'backup')));
+
+        $temp->add(new admin_setting_confightmleditor(
+                'backup/backup_async_message',
+                new lang_string('asyncmessagebody', 'backup'),
+                new lang_string('asyncmessagebodydetail', 'backup'),
+                new lang_string('asyncmessagebodydefault', 'backup')));
+
+        $ADMIN->add('backups', $temp);
+    }
+
 }
index 61d82c3..58493dd 100644 (file)
@@ -59,6 +59,8 @@ if ($hassiteconfig) { // speedup for non-admins, add all caps used on this page
     $temp->add(new admin_setting_configcheckbox('profilingallowme', new lang_string('profilingallowme', 'admin'), new lang_string('profilingallowme_help', 'admin'), false));
     // Allow PROFILEALL/PROFILEALLSTOP GPC.
     $temp->add(new admin_setting_configcheckbox('profilingallowall', new lang_string('profilingallowall', 'admin'), new lang_string('profilingallowall_help', 'admin'), false));
+    $temp->add(new admin_setting_configtext('profilingslow', new lang_string('profilingslow', 'admin'),
+        new lang_string('profilingslow_help', 'admin'), 0, PARAM_FLOAT));
     // TODO: Allow to skip PHP functions (XHPROF_FLAGS_NO_BUILTINS)
     // TODO: Allow to skip call_user functions (ignored_functions array)
     // Specify the life time (in minutes) of profiling runs.
index e5c660b..0b62749 100644 (file)
@@ -51,4 +51,7 @@ if ($hassiteconfig) { // speedup for non-admins, add all caps used on this page
 
     $optionalsubsystems->add(new admin_setting_configcheckbox('enablecoursepublishing',
         new lang_string('enablecoursepublishing', 'hub'), new lang_string('enablecoursepublishing_help', 'hub'), 0));
+
+    $optionalsubsystems->add(new admin_setting_configcheckbox('enableasyncbackup', new lang_string('enableasyncbackup', 'backup'),
+        new lang_string('enableasyncbackup_help', 'backup'), 0, 1, 0));
 }
index 9e958db..fd1374c 100644 (file)
Binary files a/admin/tool/analytics/amd/build/model.min.js and b/admin/tool/analytics/amd/build/model.min.js differ
index 8d35e86..7a33345 100644 (file)
@@ -101,50 +101,53 @@ define(['jquery', 'core/str', 'core/log', 'core/notification', 'core/modal_facto
         },
 
         /**
-         * Displays a select-evaluation-mode choice.
+         * Displays evaluation mode and time-splitting method choices.
          *
          * @param  {String}  actionId
          * @param  {Boolean} trainedOnlyExternally
          */
-        selectEvaluationMode: function(actionId, trainedOnlyExternally) {
+        selectEvaluationOptions: function(actionId, trainedOnlyExternally, timeSplittingMethods) {
             $('[data-action-id="' + actionId + '"]').on('click', function(ev) {
                 ev.preventDefault();
 
                 var a = $(ev.currentTarget);
 
-                if (!trainedOnlyExternally) {
-                    // We can not evaluate trained models if the model was trained using data from this site.
-                    // Default to evaluate the model configuration if that is the case.
-                    window.location.href = a.attr('href');
-                    return;
-                }
-
                 var stringsPromise = Str.get_strings([
                     {
                         key: 'evaluatemodel',
                         component: 'tool_analytics'
                     }, {
-                        key: 'evaluationmode',
+                        key: 'evaluate',
                         component: 'tool_analytics'
                     }
                 ]);
                 var modalPromise = ModalFactory.create({type: ModalFactory.types.SAVE_CANCEL});
-                var bodyPromise = Templates.render('tool_analytics/evaluation_mode_selection', {});
+                var bodyPromise = Templates.render('tool_analytics/evaluation_options', {
+                    trainedexternally: trainedOnlyExternally,
+                    timesplittingmethods: timeSplittingMethods
+                });
 
                 $.when(stringsPromise, modalPromise).then(function(strings, modal) {
 
 
                     modal.getRoot().on(ModalEvents.hidden, modal.destroy.bind(modal));
 
-                    modal.setTitle(strings[1]);
-                    modal.setSaveButtonText(strings[0]);
+                    modal.setTitle(strings[0]);
+                    modal.setSaveButtonText(strings[1]);
                     modal.setBody(bodyPromise);
 
                     modal.getRoot().on(ModalEvents.save, function() {
+
+                        // Evaluation mode.
                         var evaluationMode = $("input[name='evaluationmode']:checked").val();
                         if (evaluationMode == 'trainedmodel') {
                             a.attr('href', a.attr('href') + '&mode=trainedmodel');
                         }
+
+                        // Selected time-splitting id.
+                        var timeSplittingMethod = $("#id-evaluation-timesplitting").val();
+                        a.attr('href', a.attr('href') + '&timesplitting=' + timeSplittingMethod);
+
                         window.location.href = a.attr('href');
                         return;
                     });
index c46e6fc..8736580 100644 (file)
@@ -48,7 +48,7 @@ class clihelper {
         foreach ($models as $model) {
             $modelid = $model->get_id();
             $isenabled = $model->is_enabled() ? get_string('enabled', 'tool_analytics') : get_string('disabled', 'tool_analytics');
-            $name = $model->get_target()->get_name();
+            $name = $model->get_name();
             echo str_pad($modelid, 15, ' ') . ' ' . str_pad($name, 50, ' ') . ' ' . str_pad($isenabled, 15, ' ') . "\n";
         }
     }
index 2c0be86..b529d50 100644 (file)
@@ -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,41 +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);
 
+        // 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']);
@@ -129,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);
+                    }
                 }
             }
         }
index 6543022..1db9981 100644 (file)
@@ -45,7 +45,7 @@ class helper {
         // Form field is PARAM_ALPHANUMEXT and we are sending fully qualified class names
         // as option names, but replacing the backslash for a string that is really unlikely
         // to ever be part of a class name.
-        return str_replace('\\', '2015102400ouuu', $class);
+        return str_replace('\\', '__', $class);
     }
 
     /**
@@ -56,7 +56,7 @@ class helper {
      */
     public static function option_to_class($option) {
         // Really unlikely but yeah, I'm a bad booyyy.
-        return str_replace('2015102400ouuu', '\\', $option);
+        return str_replace('__', '\\', $option);
     }
 
     /**
@@ -86,10 +86,23 @@ class helper {
         if ($analyticmodels = $PAGE->settingsnav->find('analyticmodels', \navigation_node::TYPE_SETTING)) {
             $PAGE->navbar->add($analyticmodels->get_content(), $analyticmodels->action());
         }
-        $PAGE->navbar->add($title, $url);
+        $PAGE->navbar->add($title);
 
         $PAGE->set_pagelayout('report');
         $PAGE->set_title($title);
         $PAGE->set_heading($title);
     }
+
+    /**
+     * Resets the current page.
+     *
+     * Note that this function can only be used by analytics pages that work at the system context.
+     *
+     * @return null
+     */
+    public static function reset_page() {
+        global $PAGE;
+        $PAGE->reset_theme_and_output();
+        $PAGE->set_context(\context_system::instance());
+    }
 }
index 0bb3902..fd205f1 100644 (file)
@@ -76,13 +76,17 @@ class invalid_analysables implements \renderable, \templatable {
 
         $offset = $this->page * $this->perpage;
 
-        $analysables = $this->model->get_analyser(['notimesplitting' => true])->get_analysables();
+        $analysables = $this->model->get_analyser(['notimesplitting' => true])->get_analysables_iterator();
 
         $skipped = 0;
         $enoughresults = false;
         $morepages = false;
         $results = array();
-        foreach ($analysables as $key => $analysable) {
+        foreach ($analysables as $analysable) {
+
+            if (!$analysable) {
+                continue;
+            }
 
             $validtraining = $this->model->get_target()->is_valid_analysable($analysable, true);
             if ($validtraining === true) {
@@ -117,13 +121,11 @@ class invalid_analysables implements \renderable, \templatable {
                 $morepages = true;
                 break;
             }
-
-            unset($analysables[$key]);
         }
 
         // Prepare the context object.
         $data = new \stdClass();
-        $data->modelname = $this->model->get_target()->get_name();
+        $data->modelname = $this->model->get_name();
 
         if ($this->page > 0) {
             $prev = clone $PAGE->url;
index 5152b6d..c6831d0 100644 (file)
@@ -62,8 +62,33 @@ class models_list implements \renderable, \templatable {
         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');
+
+        $newmodelmenu = new \action_menu();
+        $newmodelmenu->set_menu_trigger(get_string('newmodel', 'tool_analytics'), 'btn btn-default');
+        $newmodelmenu->set_alignment(\action_menu::TL, \action_menu::BL);
+
+        $newmodelmenu->add(new \action_menu_link(
+            new \moodle_url('/admin/tool/analytics/createmodel.php'),
+            new \pix_icon('i/edit', ''),
+            get_string('createmodel', 'tool_analytics'),
+            false
+        ));
+
+        $newmodelmenu->add(new \action_menu_link(
+            new \moodle_url('/admin/tool/analytics/importmodel.php'),
+            new \pix_icon('i/import', ''),
+            get_string('importmodel', 'tool_analytics'),
+            false
+        ));
+
+        $newmodelmenu->add(new \action_menu_link(
+            new \moodle_url('/admin/tool/analytics/restoredefault.php'),
+            new \pix_icon('i/reload', ''),
+            get_string('restoredefault', 'tool_analytics'),
+            false
+        ));
+
+        $data->newmodelmenu = $newmodelmenu->export_for_template($output);
 
         $onlycli = get_config('analytics', 'onlycli');
         if ($onlycli === false) {
@@ -71,9 +96,20 @@ class models_list implements \renderable, \templatable {
             $onlycli = 1;
         }
 
+        // Evaluation options.
+        $timesplittingmethods = [
+            ['id' => 'all', 'text' => get_string('alltimesplittingmethods', 'tool_analytics')],
+        ];
+        foreach (\core_analytics\manager::get_time_splitting_methods_for_evaluation(true) as $timesplitting) {
+            $timesplittingmethods[] = [
+                'id' => \tool_analytics\output\helper::class_to_option($timesplitting->get_id()),
+                'text' => $timesplitting->get_name()->out(),
+            ];
+        }
+
         $data->models = array();
         foreach ($this->models as $model) {
-            $modeldata = $model->export();
+            $modeldata = $model->export($output);
 
             // Check if there is a help icon for the target to show.
             $identifier = $modeldata->target->get_identifier();
@@ -109,6 +145,8 @@ class models_list implements \renderable, \templatable {
                 $modeldata->indicators = $indicators;
             }
 
+            $modeldata->indicatorsnum = count($modeldata->indicators);
+
             // Check if there is a help icon for the time splitting method.
             if (!empty($modeldata->timesplitting)) {
                 $identifier = $modeldata->timesplitting->get_identifier();
@@ -192,7 +230,16 @@ class models_list implements \renderable, \templatable {
                 $trainedonlyexternally = !$model->trained_locally() && $model->is_trained();
 
                 $actionid = 'evaluate-' . $model->get_id();
-                $PAGE->requires->js_call_amd('tool_analytics/model', 'selectEvaluationMode', [$actionid, $trainedonlyexternally]);
+
+                $modeltimesplittingmethods = $timesplittingmethods;
+                // Include the current time-splitting method as the default selection method the model already have one.
+                if ($model->get_model_obj()->timesplitting) {
+                    $currenttimesplitting = ['id' => 'current', 'text' => get_string('currenttimesplitting', 'tool_analytics')];
+                    array_unshift($modeltimesplittingmethods, $currenttimesplitting);
+                }
+
+                $evaluateparams = [$actionid, $trainedonlyexternally, $modeltimesplittingmethods];
+                $PAGE->requires->js_call_amd('tool_analytics/model', 'selectEvaluationOptions', $evaluateparams);
                 $urlparams['action'] = 'evaluate';
                 $url = new \moodle_url('model.php', $urlparams);
                 $icon = new \action_menu_link_secondary($url, new \pix_icon('i/calc', get_string('evaluate', 'tool_analytics')),
@@ -210,12 +257,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,6 +325,7 @@ class models_list implements \renderable, \templatable {
                 $actionsmenu->add($icon);
             }
 
+            // Delete model.
             $actionid = 'delete-' . $model->get_id();
             $PAGE->requires->js_call_amd('tool_analytics/model', 'confirmAction', [$actionid, 'delete']);
             $urlparams['action'] = 'delete';
diff --git a/admin/tool/analytics/classes/output/restorable_models.php b/admin/tool/analytics/classes/output/restorable_models.php
new file mode 100644 (file)
index 0000000..a4e3456
--- /dev/null
@@ -0,0 +1,142 @@
+<?php
+// This file is part of Moodle - https://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/>.
+
+/**
+ * Provides {@link \tool_analytics\output\restorable_models} class.
+ *
+ * @package     tool_analytics
+ * @category    output
+ * @copyright   2019 David Mudrák <david@moodle.com>
+ * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace tool_analytics\output;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Represents the list of default models that can be eventually restored.
+ *
+ * @copyright 2019 David Mudrák <david@moodle.com>
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class restorable_models implements \renderable, \templatable {
+
+    /** @var array */
+    protected $models;
+
+    /**
+     * Instantiate an object of this class.
+     *
+     * @param array $models List of models as returned by {@link \core_analytics\manager::load_default_models_for_all_components()}
+     */
+    public function __construct(array $models) {
+
+        $this->models = $models;
+    }
+
+    /**
+     * Export the list of models to be rendered.
+     *
+     * @param renderer_base $output
+     * @return string
+     */
+    public function export_for_template(\renderer_base $output) {
+
+        $components = [];
+
+        foreach ($this->models as $componentname => $modelslist) {
+            $component = [
+                'name' => $this->component_name($componentname),
+                'component' => $componentname,
+                'models' => [],
+            ];
+
+            foreach ($modelslist as $definition) {
+                list($target, $indicators) = \core_analytics\manager::get_declared_target_and_indicators_instances($definition);
+
+                if (\core_analytics\model::exists($target, $indicators)) {
+                    continue;
+                }
+
+                $targetnamelangstring = $target->get_name();
+
+                $model = [
+                    'defid' => \core_analytics\manager::model_declaration_identifier($definition),
+                    'targetname' => $targetnamelangstring,
+                    'targetclass' => $definition['target'],
+                    'indicatorsnum' => count($definition['indicators']),
+                    'indicators' => [],
+                ];
+
+                if (get_string_manager()->string_exists($targetnamelangstring->get_identifier().'_help',
+                        $targetnamelangstring->get_component())) {
+                    $helpicon = new \help_icon($targetnamelangstring->get_identifier(), $targetnamelangstring->get_component());
+                    $model['targethelp'] = $helpicon->export_for_template($output);
+                }
+
+                foreach ($indicators as $indicator) {
+                    $indicatornamelangstring = $indicator->get_name();
+                    $indicatordata = [
+                        'name' => $indicatornamelangstring,
+                        'classname' => $indicator->get_id(),
+                    ];
+
+                    if (get_string_manager()->string_exists($indicatornamelangstring->get_identifier().'_help',
+                            $indicatornamelangstring->get_component())) {
+                        $helpicon = new \help_icon($indicatornamelangstring->get_identifier(),
+                            $indicatornamelangstring->get_component());
+                        $indicatordata['indicatorhelp'] = $helpicon->export_for_template($output);
+                    }
+
+                    $model['indicators'][] = $indicatordata;
+                }
+
+                $component['models'][] = $model;
+            }
+
+            if (!empty($component['models'])) {
+                $components[] = $component;
+            }
+        }
+
+        $result = [
+            'hasdata' => !empty($components),
+            'components' => array_values($components),
+            'submiturl' => new \moodle_url('/admin/tool/analytics/restoredefault.php'),
+            'backurl' => new \moodle_url('/admin/tool/analytics/index.php'),
+            'sesskey' => sesskey(),
+        ];
+
+        return $result;
+    }
+
+    /**
+     * Return a human readable name for the given frankenstyle component.
+     *
+     * @param string $component Frankenstyle component such as 'core', 'core_analytics' or 'mod_workshop'
+     * @return string Human readable name of the component
+     */
+    protected function component_name(string $component): string {
+
+        if ($component === 'core' || strpos($component, 'core_')) {
+            return get_string('componentcore', 'tool_analytics');
+
+        } else {
+            return get_string('pluginname', $component);
+        }
+    }
+}
index 83894be..6ea0d0d 100644 (file)
@@ -60,8 +60,12 @@ class predict_models extends \core\task\scheduled_task {
 
         foreach ($models as $model) {
             $result = $model->predict();
+
+            // Reset the page as some indicators may call external functions that overwrite the page context.
+            \tool_analytics\output\helper::reset_page();
+
             if ($result) {
-                echo $OUTPUT->heading(get_string('modelresults', 'tool_analytics', $model->get_target()->get_name()));
+                echo $OUTPUT->heading(get_string('modelresults', 'tool_analytics', $model->get_name()));
                 $renderer = $PAGE->get_renderer('tool_analytics');
                 echo $renderer->render_get_predictions_results(false, array(), $result, $model->get_analyser()->get_logs());
             }
index b017e9a..67c0a3a 100644 (file)
@@ -71,8 +71,12 @@ class train_models extends \core\task\scheduled_task {
             }
 
             $result = $model->train();
+
+            // Reset the page as some indicators may call external functions that overwrite the page context.
+            \tool_analytics\output\helper::reset_page();
+
             if ($result) {
-                echo $OUTPUT->heading(get_string('modelresults', 'tool_analytics', $model->get_target()->get_name()));
+                echo $OUTPUT->heading(get_string('modelresults', 'tool_analytics', $model->get_name()));
 
                 $renderer = $PAGE->get_renderer('tool_analytics');
                 echo $renderer->render_get_predictions_results($result, $model->get_analyser()->get_logs());
index 2836ca7..8473aba 100644 (file)
@@ -111,6 +111,9 @@ $analyseroptions = array(
 // 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();
+
 $renderer = $PAGE->get_renderer('tool_analytics');
 echo $renderer->render_evaluate_results($results, $model->get_analyser()->get_logs());
 
index bf1701c..e5b9b92 100644 (file)
@@ -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);
index dc0c5c9..7e4d208 100644 (file)
@@ -24,6 +24,7 @@
 
 $string['accuracy'] = 'Accuracy';
 $string['allpredictions'] = 'All predictions';
+$string['alltimesplittingmethods'] = 'All time-splitting methods';
 $string['analysingsitedata'] = 'Analysing the site';
 $string['analyticmodels'] = 'Analytics models';
 $string['bettercli'] = 'Evaluating models and generating predictions may involve heavy processing. It is recommended to run these actions from the command line.';
@@ -35,7 +36,12 @@ $string['clearmodelpredictions'] = 'Are you sure you want to clear all "{$a}" pr
 $string['clienablemodel'] = 'You can enable the model by selecting a time-splitting method by its ID. Note that you can also enable it later using the web interface (\'none\' to exit).';
 $string['clievaluationandpredictions'] = 'A scheduled task iterates through enabled models and gets predictions. Models evaluation via the web interface is disabled. You can allow these processes to be executed manually via the web interface by disabling the <a href="{$a}">\'onlycli\'</a> analytics setting.';
 $string['clievaluationandpredictionsnoadmin'] = 'A scheduled task iterates through enabled models and gets predictions. Models evaluation via the web interface is disabled. It may be enabled by a site administrator.';
+$string['component'] = 'Component';
+$string['componentcore'] = 'Core';
+$string['componentselect'] = 'Select all models provided by the component \'{$a}\'';
+$string['componentselectnone'] = 'Unselect all';
 $string['createmodel'] = 'Create model';
+$string['currenttimesplitting'] = 'Current time-splitting method';
 $string['delete'] = 'Delete';
 $string['deletemodelconfirmation'] = 'Are you sure you want to delete "{$a}"? These changes can not be reverted.';
 $string['disabled'] = 'Disabled';
@@ -46,7 +52,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';
@@ -78,6 +83,9 @@ $string['getpredictions'] = 'Get predictions';
 $string['goodmodel'] = 'This is a good model for using to obtain predictions. Enable it to start obtaining predictions.';
 $string['importmodel'] = 'Import model';
 $string['indicators'] = 'Indicators';
+$string['indicators_help'] = 'The indicators are what you think will lead to an accurate prediction of the target.';
+$string['indicators_link'] = 'Indicators';
+$string['indicatorsnum'] = 'Number of indicators: {$a}';
 $string['info'] = 'Info';
 $string['ignoreversionmismatches'] = 'Ignore version mismatches';
 $string['ignoreversionmismatchescheckbox'] = 'Ignore the differences between this site version and the original site version.';
@@ -93,8 +101,10 @@ $string['loginfo'] = 'Log extra info';
 $string['missingmoodleversion'] = 'Imported file does not define a moodle version number';
 $string['modelid'] = 'Model ID';
 $string['modelinvalidanalysables'] = 'Invalid analysable elements for "{$a}" model';
+$string['modelname'] = 'Model name';
 $string['modelresults'] = '{$a} results';
 $string['modeltimesplitting'] = 'Time splitting';
+$string['newmodel'] = 'New model';
 $string['nextpage'] = 'Next page';
 $string['nodatatoevaluate'] = 'There is no data to evaluate the model';
 $string['nodatatopredict'] = 'No new elements to get predictions for';
@@ -107,10 +117,18 @@ $string['predictmodels'] = 'Predict models';
 $string['predictorresultsin'] = 'Predictor logged information in {$a} directory';
 $string['predictionprocessfinished'] = 'Prediction process finished';
 $string['previouspage'] = 'Previous page';
+$string['restoredefault'] = 'Restore default models';
+$string['restoredefaultempty'] = 'Please select models to be restored.';
+$string['restoredefaultinfo'] = 'These default models are missing or have changed since being installed. You can restore selected default models.';
+$string['restoredefaultnone'] = 'All default models provided by the Moodle core and installed plugins have been already created. No new models were found, there is nothing to restore.';
+$string['restoredefaultsome'] = 'Succesfully re-created {$a->count} new model(s).';
+$string['restoredefaultsubmit'] = 'Restore selected';
 $string['samestartdate'] = 'Current start date is good';
 $string['sameenddate'] = 'Current end date is good';
+$string['selecttimesplittingforevaluation'] = 'Select the time-splitting method you want to use to evaluate the model configuration.';
 $string['target'] = 'Target';
 $string['target_help'] = 'The target is what the model will predict.';
+$string['target_link'] = 'Targets';
 $string['timesplittingnotdefined'] = 'Time splitting is not defined.';
 $string['timesplittingnotdefined_help'] = 'You need to select a time-splitting method before enabling the model.';
 $string['trainandpredictmodel'] = 'Training model and calculating predictions';
index 3cca64e..c6cc950 100644 (file)
@@ -40,7 +40,7 @@ $url = new \moodle_url('/admin/tool/analytics/model.php', $params);
 switch ($action) {
 
     case 'edit':
-        $title = get_string('editmodel', 'tool_analytics', $model->get_target()->get_name());
+        $title = get_string('editmodel', 'tool_analytics', $model->get_name());
         break;
     case 'evaluate':
         $title = get_string('evaluatemodel', 'tool_analytics');
@@ -110,14 +110,10 @@ switch ($action) {
     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 +125,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);
         }
@@ -156,8 +160,6 @@ switch ($action) {
     case 'evaluate':
         confirm_sesskey();
 
-        echo $OUTPUT->header();
-
         if ($model->is_static()) {
             throw new moodle_exception('errornostaticevaluate', 'tool_analytics');
         }
@@ -174,8 +176,26 @@ switch ($action) {
         $mode = optional_param('mode', false, PARAM_ALPHANUM);
         if ($mode == 'trainedmodel') {
             $options['mode'] = 'trainedmodel';
+        } else {
+
+            // All is the default in core_analytics\model::evaluate() as well.
+            $timesplitting = optional_param('timesplitting', 'all', PARAM_ALPHANUMEXT);
+            if ($timesplitting === 'current') {
+                $options['timesplitting'] = \core_analytics\manager::get_time_splitting($model->get_model_obj()->timesplitting);
+            } else if ($timesplitting !== 'all') {
+                $options['timesplitting'] = \core_analytics\manager::get_time_splitting(
+                    \tool_analytics\output\helper::option_to_class($timesplitting)
+                );
+            }
         }
+
         $results = $model->evaluate($options);
+
+        // We reset the theme and the output as some indicators may be using external functions
+        // which reset $PAGE.
+        \tool_analytics\output\helper::reset_page();
+        echo $OUTPUT->header();
+
         $renderer = $PAGE->get_renderer('tool_analytics');
         echo $renderer->render_evaluate_results($results, $model->get_analyser()->get_logs());
         break;
@@ -183,8 +203,6 @@ switch ($action) {
     case 'getpredictions':
         confirm_sesskey();
 
-        echo $OUTPUT->header();
-
         if ($onlycli) {
             throw new moodle_exception('erroronlycli', 'tool_analytics');
         }
@@ -202,6 +220,11 @@ switch ($action) {
             $predictlogs = array();
         }
 
+        // We reset the theme and the output as some indicators may be using external functions
+        // which reset $PAGE.
+        \tool_analytics\output\helper::reset_page();
+        echo $OUTPUT->header();
+
         $renderer = $PAGE->get_renderer('tool_analytics');
         echo $renderer->render_get_predictions_results($trainresults, $trainlogs, $predictresults, $predictlogs);
         break;
diff --git a/admin/tool/analytics/restoredefault.php b/admin/tool/analytics/restoredefault.php
new file mode 100644 (file)
index 0000000..28b1b02
--- /dev/null
@@ -0,0 +1,79 @@
+<?php
+// This file is part of Moodle - https://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/>.
+
+/**
+ * Check and create missing default prediction models.
+ *
+ * @package     tool_analytics
+ * @copyright   2019 David Mudrák <david@moodle.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();
+
+$confirmed = optional_param('confirmed', false, PARAM_BOOL);
+$restoreids = optional_param_array('restoreid', [], PARAM_ALPHANUM);
+
+$returnurl = new \moodle_url('/admin/tool/analytics/index.php');
+$myurl = new \moodle_url('/admin/tool/analytics/restoredefault.php');
+
+\tool_analytics\output\helper::set_navbar(get_string('restoredefault', 'tool_analytics'), $myurl);
+
+if (data_submitted()) {
+    require_sesskey();
+
+    if (empty($restoreids)) {
+        $message = get_string('restoredefaultempty', 'tool_analytics');
+        $type = \core\output\notification::NOTIFY_WARNING;
+        redirect($myurl, $message, null, $type);
+    }
+
+    $numcreated = 0;
+
+    foreach (\core_analytics\manager::load_default_models_for_all_components() as $componentname => $modelslist) {
+        foreach ($modelslist as $definition) {
+            if (!in_array(\core_analytics\manager::model_declaration_identifier($definition), $restoreids)) {
+                // This model has not been selected by the user.
+                continue;
+            }
+
+            list($target, $indicators) = \core_analytics\manager::get_declared_target_and_indicators_instances($definition);
+
+            if (\core_analytics\model::exists($target, $indicators)) {
+                // This model exists (normally this should not happen as we do not show such models in the UI to select).
+                continue;
+            }
+
+            \core_analytics\manager::create_declared_model($definition);
+            $numcreated++;
+        }
+    }
+
+    $message = get_string('restoredefaultsome', 'tool_analytics', ['count' => $numcreated]);
+    $type = \core\output\notification::NOTIFY_SUCCESS;
+
+    redirect($returnurl, $message, null, $type);
+}
+
+$models = \core_analytics\manager::load_default_models_for_all_components();
+$ui = new \tool_analytics\output\restorable_models($models);
+
+echo $OUTPUT->header();
+echo $PAGE->get_renderer('tool_analytics')->render($ui);
+echo $OUTPUT->footer();
diff --git a/admin/tool/analytics/templates/evaluation_options.mustache b/admin/tool/analytics/templates/evaluation_options.mustache
new file mode 100644 (file)
index 0000000..9d77c13
--- /dev/null
@@ -0,0 +1,82 @@
+{{!
+    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/>.
+}}
+{{!
+    @template tool_analytics/evaluation_options
+
+    Evaluation selector.
+
+    The purpose of this template is to render the evaluation mode options.
+
+    Classes required for JS:
+    * none
+
+    Data attributes required for JS:
+    * none
+
+    Example context (json):
+    {
+        "trainedexternally": "1",
+        "timesplittingmethods": [
+            {
+                "id": "ou",
+                "name": "Quarters"
+            }, {
+                "id": "yeah",
+                "name": "Tenths"
+            }
+        ]
+    }
+}}
+
+{{#trainedexternally}}
+    <div class="box mb-4">{{#str}} evaluationmodeinfo, tool_analytics {{/str}}</div>
+
+    <div class="custom-control custom-radio">
+        <input class="custom-control-input" type="radio" name="evaluationmode" id="id-mode-trainedmodel" value="trainedmodel" checked>
+        <label class="custom-control-label" for="id-mode-trainedmodel">{{#str}} evaluationmodetrainedmodel, tool_analytics {{/str}}</label>
+    </div>
+
+    <div class="custom-control custom-radio">
+        <input class="custom-control-input" type="radio" name="evaluationmode" id="id-mode-configuration" value="configuration">
+        <label class="custom-control-label" for="id-mode-configuration">{{#str}} evaluationmodeconfiguration, tool_analytics {{/str}}</label>
+    </div>
+{{/trainedexternally}}
+
+{{! Hidden by default if #trainedexternally as the default option is trainedmodel in this case.}}
+<div id="id-evaluation-timesplitting-container" class="m-t-1 {{#trainedexternally}}hidden{{/trainedexternally}}">
+    {{#str}} selecttimesplittingforevaluation, tool_analytics {{/str}}
+    <div>
+        <select id="id-evaluation-timesplitting" name="timesplitting" class="custom-select m-t-1">
+            {{#timesplittingmethods}}
+                <option value="{{id}}">{{text}}</option>
+            {{/timesplittingmethods}}
+        </select>
+    </div>
+</div>
+
+
+{{#js}}
+    require(['jquery'], function($) {
+        $("input[name='evaluationmode']:radio").change(function() {
+            if ($(this).val() == 'configuration') {
+                $('#id-evaluation-timesplitting-container').show();
+            } else {
+                $('#id-evaluation-timesplitting-container').hide();
+            }
+        });
+    });
+{{/js}}
\ No newline at end of file
index b49c9e2..39d8e6b 100644 (file)
     Template for models list.
 
     Classes required for JS:
-    * none
+    * The list od models wrapped within a id="predictionmodelslist" element.
 
     Data attributes required for JS:
-    * none
+    * [data-widget="toggle"] indicates the clickable element for expanding/collapsing
+      the list of indicators used by the given model.
 
     Context variables required for this template:
-    * none
+    * models: array - list of models to display
+        - id: int - model unique identifier
+        - name: object - data for the inplace editable element template
+        - target: string - name of the target associated with the model
+        - targetclass: string - fully qualified name of the target class
+        - targethelp: object - data for the help tooltip template
+        - enabled: bool - is the model enabled
+        - indicatorsnum: int - number of indicators
+        - indicators: array - list of indicators used by the model
+            + name: string - name of the indicator
+            + help: object - data for the help tooltip template
+        - insights: object - data for the single select template
+        - noinsights: string - text to display instead of insights
+    * warnings: array - list of data for notification warning template
+    * infos: array - list of data for notification info template
+    * createmodelurl: string - URL to create a new model
+    * importmodelurl: string - URL to import a model
 
     Example context (json):
     {
         "models": [
             {
+                "id": 11,
+                "name": {
+                    "component": "local_analyticsdemo",
+                    "itemtype": "modelname",
+                    "itemid": 42,
+                    "displayvalue": "Prevent devs at risk",
+                    "value": ""
+                },
                 "target": "Prevent devs at risk",
-                "targethelp": [
-                    {
-                        "title": "Help with something",
-                        "url": "http://example.org/help",
-                        "linktext": "",
-                        "icon":{
-                            "extraclasses": "iconhelp",
-                            "attributes": [
-                                {"name": "src", "value": "../../../pix/help.svg"},
-                                {"name": "alt", "value": "Help icon"}
-                            ]
-                        }
+                "targetclass": "\\local_analyticsdemo\\analytics\\target\\dev_risk",
+                "targethelp": {
+                    "title": "Help with Prevent devs at risk",
+                    "text": "This target blah blah ...",
+                    "url": "http://example.org/help",
+                    "linktext": "",
+                    "icon": {
+                        "extraclasses": "iconhelp",
+                        "attributes": [
+                            {"name": "src", "value": "../../../pix/help.svg"},
+                            {"name": "alt", "value": "Help icon"}
+                        ]
                     }
-                ],
+                },
                 "enabled": 1,
-                "indicators": [{
-                    "name": "Indicator 1",
-                    "help": [{
-                            "title": "Help with something",
+                "indicatorsnum": 2,
+                "indicators": [
+                    {
+                        "name": "Indicator 1",
+                        "help": {
+                            "text": "This indicator blah blah ...",
+                            "title": "Help with Indicator 1",
                             "url": "http://example.org/help",
                             "linktext": "",
-                            "icon":{
+                            "icon": {
                                 "extraclasses": "iconhelp",
                                 "attributes": [
                                     {"name": "src", "value": "../../../pix/help.svg"},
                                     {"name": "alt", "value": "Help icon"}
                                 ]
                             }
-                        }]
+                        }
                     },
                     {
-                    "name": "Indicator 2",
-                    "help": [{
-                            "title": "Help with something",
+                        "name": "Indicator 2",
+                        "help": {
+                            "text": "This indicator blah blah ...",
+                            "title": "Help with Indicator 2",
                             "url": "http://example.org/help",
                             "linktext": "",
-                            "icon":{
+                            "icon": {
                                 "extraclasses": "iconhelp",
                                 "attributes": [
                                     {"name": "src", "value": "../../../pix/help.svg"},
                                     {"name": "alt", "value": "Help icon"}
                                 ]
                             }
-                        }]
-                    }],
-                "timesplitting": "Quarters",
-                "timesplittinghelp": [
-                    {
-                        "title": "Help with something",
-                        "url": "http://example.org/help",
-                        "linktext": "",
-                        "icon":{
-                            "extraclasses": "iconhelp",
-                            "attributes": [
-                                {"name": "src", "value": "../../../pix/help.svg"},
-                                {"name": "alt", "value": "Help icon"}
-                            ]
                         }
                     }
                 ],
+                "timesplitting": "Quarters",
+                "timesplittinghelp": {
+                    "text": "This time splitting methof blah blah ...",
+                    "title": "Help with Quarters",
+                    "url": "http://example.org/help",
+                    "linktext": "",
+                    "icon": {
+                        "extraclasses": "iconhelp",
+                        "attributes": [
+                            {"name": "src", "value": "../../../pix/help.svg"},
+                            {"name": "alt", "value": "Help icon"}
+                        ]
+                    }
+                },
                 "noinsights": "No insights available yet"
             }
         ],
-        "warnings": {
-            "message": "Hey, this is a warning"
-        }
+        "warnings": [
+            {
+                "message": "Be ware, this is just an example!"
+            }
+        ],
+        "createmodelurl": "#",
+        "importmodelurl": "#"
     }
 }}
 
 
 <div class="box">
     <div class="top-nav d-flex">
-        <a href="{{createmodelurl}}" class="btn btn-secondary mr-2">{{#str}}createmodel, tool_analytics{{/str}}</a>
-        <a href="{{importmodelurl}}" class="btn btn-secondary">{{#str}}importmodel, tool_analytics{{/str}}</a>
+        {{#newmodelmenu}}
+        {{>core/action_menu}}
+        {{/newmodelmenu}}
     </div>
-    <table class="generaltable fullwidth">
+    <table id="predictionmodelslist" class="generaltable fullwidth">
         <caption>{{#str}}analyticmodels, tool_analytics{{/str}}</caption>
         <thead>
             <tr>
-                <th scope="col">{{#str}}target, tool_analytics{{/str}}</th>
+                <th scope="col">{{#str}}modelname, tool_analytics{{/str}}</th>
                 <th scope="col">{{#str}}enabled, tool_analytics{{/str}}</th>
                 <th scope="col">{{#str}}indicators, tool_analytics{{/str}}</th>
                 <th scope="col">{{#str}}modeltimesplitting, tool_analytics{{/str}}</th>
         {{#models}}
             <tr>
                 <td>
-                    <span class="target-name">{{target}}</span>
-                    {{#targethelp}}
-                        {{>core/help_icon}}
-                    {{/targethelp}}
+                    {{#name}}
+                        <span class="model-name">{{>core/inplace_editable}}</span>
+                    {{/name}}
+                    <div>
+                        <small class="target-class">{{targetclass}}</small>
+                        {{#targethelp}}
+                            {{>core/help_icon}}
+                        {{/targethelp}}
+                    </div>
                 </td>
                 <td>
                     {{#enabled}}
                     {{/enabled}}
                 </td>
                 <td>
-                    <ul>
+                    <a data-widget="toggle"
+                           title="{{#str}} clicktohideshow {{/str}}"
+                           aria-expanded="false"
+                           aria-controls="indicators-{{id}}"
+                           role="button"
+                           href="">
+                        {{#str}} indicatorsnum, tool_analytics, {{indicatorsnum}} {{/str}}
+                    </a>
+                    <ul class="hidden" id="indicators-{{id}}">
                     {{#indicators}}
                         <li>
                             {{name}}
         </tbody>
     </table>
 </div>
+{{#js}}
+require(['jquery'], function($) {
+
+    // Toggle the visibility of the indicators list.
+    $('#predictionmodelslist').on('click', '[data-widget="toggle"]', function(e) {
+        e.preventDefault();
+        var toggle = $(e.currentTarget);
+        var listid = toggle.attr('aria-controls');
+
+        $(document.getElementById(listid)).toggle();
+
+        if (toggle.attr('aria-expanded') == 'false') {
+            toggle.attr('aria-expanded', 'true');
+        } else {
+            toggle.attr('aria-expanded', 'false');
+        }
+    });
+});
+{{/js}}
diff --git a/admin/tool/analytics/templates/restorable_models.mustache b/admin/tool/analytics/templates/restorable_models.mustache
new file mode 100644 (file)
index 0000000..da85f7b
--- /dev/null
@@ -0,0 +1,225 @@
+{{!
+    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/>.
+}}
+{{!
+    @template tool_analytics/restorable_models
+
+    Displays the list of missing prediction models that can be restored.
+
+    Classes required for JS:
+    * The list should be wrapped within a id="restorablemodelslist" element.
+
+    Data attributes required for JS:
+    * [data-widget="toggle"] indicates the clickable element for expanding/collapsing
+      the list of indicators used by the given model.
+    * [data-select] indicates a clickable element used for selecting multiple checkboxes.
+    * [data-component] should be set for checkboxes that select the particular model.
+
+    Context variables required for this template:
+    * hasdata: boolean - do we have data to display
+    * submiturl: string - URL where the form should be submitted
+    * backurl: string - URL where the user should be sent without making any changes
+    * sesskey: string
+    * components: array - list of components to display
+        - name: string - human readable name of the component
+        - component: string - frankenstyle name of the component
+        - models: array - list of restorable models provided by the component
+            + defid: string - model definition identifier
+            + targetname: string - human readable name of the target
+            + targetclass: string - fully qualified classname of the target
+            + indicatorsnum: int - number of indicators
+            + indicators: array - list of indicators
+                ~ name: string - human readable name of the indicator
+                ~ classname: string - fully qualified classname of the indicator
+
+    Example context (json):
+    {
+        "hasdata": true,
+        "submiturl": "https://example.com/moodle/admin/tool/analytics/restoredefault.php",
+        "backurl": "https://example.com/moodle/admin/tool/analytics/index.php",
+        "sesskey": "abcdefg123456",
+        "components": [
+            {
+                "name": "Core",
+                "component": "core",
+                "models": [
+                    {
+                        "defid": "id24680aceg",
+                        "targetname": "No teaching",
+                        "targetclass": "\\core\\analytics\\target\\no_teaching",
+                        "indicatorsnum": 2,
+                        "indicators": [
+                            {
+                                "name": "There are no teachers",
+                                "classname": "\\core\\analytics\\indicator\\no_teacher"
+                            },
+                            {
+                                "name": "There are no students",
+                                "classname": "\\core\\analytics\\indicator\\no_students"
+                            }
+                        ]
+                    },
+                    {
+                        "defid": "id13579bdfi",
+                        "targetname": "Students at risk of dropping out",
+                        "targetclass": "\\core\\analytics\\target\\course_dropout",
+                        "indicatorsnum": 1,
+                        "indicators": [
+                            {
+                                "name": "Read actions amount",
+                                "classname": "\\core\\analytics\\indicator\\read_actions"
+                            }
+                        ]
+                    }
+                ]
+            },
+            {
+                "name": "Custom analytics plugin",
+                "component": "tool_customanalytics",
+                "models": [
+                    {
+                        "defid": "id566dsgffg655",
+                        "targetname": "Cheater",
+                        "targetclass": "\\tool_customanalytics\\analytics\\target\\cheater",
+                        "indicatorsnum": 1,
+                        "indicators": [
+                            {
+                                "name": "Copy-pasted submissions",
+                                "classname": "\\tool_customanalytics\\analytics\\indicator\\copy_paster_submissions"
+                            }
+                        ]
+                    }
+                ]
+            }
+        ]
+    }
+}}
+<div class="box">
+    {{^hasdata}}
+    <p>{{#str}} restoredefaultnone, tool_analytics {{/str}}</p>
+    <div><a href="{{backurl}}" class="btn btn-secondary">{{#str}} back {{/str}}</a></div>
+    {{/hasdata}}
+
+    {{#hasdata}}
+    <p>{{#str}} restoredefaultinfo, tool_analytics {{/str}}</p>
+    <form method="post" action="{{submiturl}}">
+        <table id="restorablemodelslist" class="generaltable fullwidth">
+            <colgroup>
+                <col width="10%">
+                <col width="45%">
+                <col width="45%">
+            </colgroup>
+            <thead>
+                <tr>
+                    <th scope="col"><a href="" data-select="*">{{#str}} selectall {{/str}}</a></th>
+                    <th scope="col">{{#str}} target, tool_analytics {{/str}}</th>
+                    <th scope="col">{{#str}} indicators, tool_analytics {{/str}}</th>
+                </tr>
+            </thead>
+            <tbody>
+            {{#components}}
+                <tr>
+                    <th scope="rowgroup" colspan="3">
+                        <span class="component-name">
+                            <a href=""
+                                    title="{{#str}} componentselect, tool_analytics, {{name}} {{/str}}"
+                                    data-select="{{component}}">
+                                {{name}}
+                            </a>
+                        </span>
+                        <div><small class="component-frankenstyle">{{component}}</small></div>
+                    </th>
+                </tr>
+                {{#models}}
+                <tr>
+                    <td>
+                        <input data-component="{{component}}" type="checkbox" name="restoreid[]" value="{{defid}}">
+                    </td>
+                    <td>
+                        <span class="target-name">{{targetname}}</span>
+                        {{#targethelp}}
+                            {{>core/help_icon}}
+                        {{/targethelp}}
+                        <div><small class="target-class">{{targetclass}}</small></div>
+                    </td>
+                    <td>
+                        <a data-widget="toggle"
+                                title="{{#str}} clicktohideshow {{/str}}"
+                                aria-expanded="false"
+                                aria-controls="indicators-{{defid}}"
+                                role="button"
+                                href="">
+                            {{#str}} indicatorsnum, tool_analytics, {{indicatorsnum}} {{/str}}
+                        </a>
+                        <ul class="hidden listunstyled" id="indicators-{{defid}}">
+                        {{#indicators}}
+                            <li>
+                                {{name}}
+                                {{#indicatorhelp}}
+                                    {{>core/help_icon}}
+                                {{/indicatorhelp}}
+                                <div><small>{{classname}}</small></div>
+                            </li>
+                        {{/indicators}}
+                        </ul>
+                    </td>
+                </tr>
+                {{/models}}
+            {{/components}}
+            </tbody>
+        </table>
+        <div>
+            <input class="btn btn-primary" type="submit" value="{{#str}} restoredefaultsubmit, tool_analytics {{/str}}">
+            <input class="btn btn-secondary" type="reset" value="{{#str}} componentselectnone, tool_analytics {{/str}}">
+            <a href="{{backurl}}" class="btn btn-secondary">{{#str}} back {{/str}}</a>
+            <input type="hidden" name="sesskey" value="{{sesskey}}">
+        </div>
+    </form>
+    {{/hasdata}}
+</div>
+
+{{#js}}
+require(['jquery'], function($) {
+
+    // Toggle the visibility of the indicators list.
+    $('#restorablemodelslist').on('click', '[data-widget="toggle"]', function(e) {
+        e.preventDefault();
+        var toggle = $(e.currentTarget);
+        var listid = toggle.attr('aria-controls');
+
+        $(document.getElementById(listid)).toggle();
+
+        if (toggle.attr('aria-expanded') == 'false') {
+            toggle.attr('aria-expanded', 'true');
+        } else {
+            toggle.attr('aria-expanded', 'false');
+        }
+    });
+
+    // Selecting all / all in component checkboxes.
+    $('#restorablemodelslist').on('click', '[data-select]', function(e) {
+        e.preventDefault();
+        var handler = $(e.currentTarget);
+        var component = handler.attr('data-select');
+
+        if (component == '*') {
+            $('input[data-component]').prop('checked', true);
+        } else {
+            $('input[data-component="' + component + '"]').prop('checked', true);
+        }
+    });
+});
+{{/js}}
diff --git a/admin/tool/analytics/tests/behat/restoredefault.feature b/admin/tool/analytics/tests/behat/restoredefault.feature
new file mode 100644 (file)
index 0000000..74ff239
--- /dev/null
@@ -0,0 +1,103 @@
+@tool @tool_analytics
+Feature: Restoring default models
+  In order to get prediction models into their initial state
+  As a manager
+  I need to be able to restore deleted default models
+
+  Background:
+    Given the following "users" exist:
+      | username       | firstname     | lastname | email              |
+      | manager        | Max           | Manager  | man@example.com    |
+    And the following "role assigns" exist:
+      | user           | role          | contextlevel  | reference             |
+      | manager        | manager               | System        |                                                       |
+
+  Scenario: Restore a single deleted default model
+    Given I log in as "manager"
+    And I navigate to "Analytics > Analytics models" in site administration
+    # Delete 'No teaching' model.
+    And I click on "Delete" "link" in the "No teaching" "table_row"
+    And I should see "Analytics models"
+    And I should not see "No teaching"
+    # Delete 'Students at risk of dropping out' model.
+    And I click on "Delete" "link" in the "Students at risk of dropping out" "table_row"
+    And I should see "Analytics models"
+    And I should not see "Students at risk of dropping out"
+    # Go to the page for restoring deleted models.
+    When I click on "Restore default models" "link"
+    And I should see "No teaching"
+    And I should see "Students at risk of dropping out"
+    # Select and restore the 'No teaching' model.
+    And I set the field with xpath "//tr[contains(normalize-space(.), 'No teaching')]//input[@type='checkbox']" to "1"
+    And I click on "Restore selected" "button"
+    Then I should see "Succesfully re-created 1 new model(s)."
+    And I should see "Analytics models"
+    And I should see "No teaching"
+    And I should not see "Students at risk of dropping out"
+
+  Scenario: Restore multiple deleted default models at once
+    Given I log in as "manager"
+    And I navigate to "Analytics > Analytics models" in site administration
+    # Delete 'No teaching' model.
+    And I click on "Delete" "link" in the "No teaching" "table_row"
+    And I should see "Analytics models"
+    And I should not see "No teaching"
+    # Delete 'Students at risk of dropping out' model.
+    And I click on "Delete" "link" in the "Students at risk of dropping out" "table_row"
+    And I should see "Analytics models"
+    And I should not see "Students at risk of dropping out"
+    # Go to the page for restoring deleted models.
+    When I click on "Restore default models" "link"
+    And I should see "No teaching"
+    And I should see "Students at risk of dropping out"
+    # Select and restore both models.
+    And I set the field with xpath "//tr[contains(normalize-space(.), 'No teaching')]//input[@type='checkbox']" to "1"
+    And I set the field with xpath "//tr[contains(normalize-space(.), 'Students at risk of dropping out')]//input[@type='checkbox']" to "1"
+    And I click on "Restore selected" "button"
+    Then I should see "Succesfully re-created 2 new model(s)."
+    And I should see "Analytics models"
+    And I should see "No teaching"
+    And I should see "Students at risk of dropping out"
+
+  Scenario: Going to the restore page while no models can be restored
+    Given I log in as "manager"
+    And I navigate to "Analytics > Analytics models" in site administration
+    And I should see "Analytics models"
+    And I should see "No teaching"
+    When I click on "Restore default models" "link"
+    Then I should see "All default models provided by the Moodle core and installed plugins have been already created. No new models were found, there is nothing to restore."
+    And I click on "Back" "link"
+    And I should see "Analytics models"
+
+  @javascript
+  Scenario: User can select and restore all missing models
+    Given I log in as "manager"
+    And I navigate to "Analytics > Analytics models" in site administration
+    # Delete 'No teaching' model.
+    And I click on "Actions" "link" in the "No teaching" "table_row"
+    And I click on "Delete" "link" in the "No teaching" "table_row"
+    And I click on "Delete" "button" in the "Delete" "dialogue"
+    And I should see "Analytics models"
+    And I should not see "No teaching"
+    # Delete 'Students at risk of dropping out' model.
+    And I click on "Actions" "link" in the "Students at risk of dropping out" "table_row"
+    And I click on "Delete" "link" in the "Students at risk of dropping out" "table_row"
+    And I click on "Delete" "button" in the "Delete" "dialogue"
+    And I should see "Analytics models"
+    And I should not see "No teaching"
+    And I should not see "Students at risk of dropping out"
+    # Go to the page for restoring deleted models.
+    And I click on "New model" "link"
+    And I click on "Restore default models" "link"
+    And I should see "No teaching"
+    And I should see "Students at risk of dropping out"
+    # Attempt to submit the form without selecting any model.
+    And I click on "Restore selected" "button"
+    And I should see "Please select models to be restored."
+    # Select all models.
+    When I click on "Select all" "link"
+    And I click on "Restore selected" "button"
+    Then I should see "Succesfully re-created 2 new model(s)."
+    And I should see "Analytics models"
+    And I should see "No teaching"
+    And I should see "Students at risk of dropping out"
index c3d9813..fae12cb 100644 (file)
@@ -24,6 +24,6 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2018120300; // The current plugin version (Date: YYYYMMDDXX).
-$plugin->requires  = 2018112800; // Requires this Moodle version.
+$plugin->version   = 2019032800; // The current plugin version (Date: YYYYMMDDXX).
+$plugin->requires  = 2019032200; // Requires this Moodle version.
 $plugin->component = 'tool_analytics'; // Full name of the plugin (used for diagnostics).
index 2c18ac7..80cb7ee 100644 (file)
@@ -614,6 +614,12 @@ class api {
             throw new moodle_exception('errorrequestnotwaitingforapproval', 'tool_dataprivacy');
         }
 
+        // Check if current user has permission to approve delete data request.
+        if ($request->get('type') == self::DATAREQUEST_TYPE_DELETE && !self::can_create_data_deletion_request_for_other()) {
+            throw new required_capability_exception(context_system::instance(),
+                'tool/dataprivacy:requestdeleteforotheruser', 'nopermissions', '');
+        }
+
         // Update the status and the DPO.
         $result = self::update_request_status($requestid, self::DATAREQUEST_STATUS_APPROVED, $USER->id);
 
@@ -653,6 +659,12 @@ class api {
             throw new moodle_exception('errorrequestnotwaitingforapproval', 'tool_dataprivacy');
         }
 
+        // Check if current user has permission to reject delete data request.
+        if ($request->get('type') == self::DATAREQUEST_TYPE_DELETE && !self::can_create_data_deletion_request_for_other()) {
+            throw new required_capability_exception(context_system::instance(),
+                'tool/dataprivacy:requestdeleteforotheruser', 'nopermissions', '');
+        }
+
         // Update the status and the DPO.
         return self::update_request_status($requestid, self::DATAREQUEST_STATUS_REJECTED, $USER->id);
     }
@@ -752,6 +764,48 @@ class api {
         return true;
     }
 
+    /**
+     * Check if user has permisson to create data deletion request for themselves.
+     *
+     * @param int|null $userid ID of the user.
+     * @return bool
+     * @throws coding_exception
+     */
+    public static function can_create_data_deletion_request_for_self(int $userid = null): bool {
+        global $USER;
+        $userid = $userid ?: $USER->id;
+        return has_capability('tool/dataprivacy:requestdelete', \context_user::instance($userid), $userid);
+    }
+
+    /**
+     * Check if user has permission to create data deletion request for another user.
+     *
+     * @param int|null $userid ID of the user.
+     * @return bool
+     * @throws coding_exception
+     * @throws dml_exception
+     */
+    public static function can_create_data_deletion_request_for_other(int $userid = null): bool {
+        global $USER;
+        $userid = $userid ?: $USER->id;
+        return has_capability('tool/dataprivacy:requestdeleteforotheruser', context_system::instance(), $userid);
+    }
+
+    /**
+     * Check if parent can create data deletion request for their children.
+     *
+     * @param int $userid ID of a user being requested.
+     * @param int|null $requesterid ID of a user making request.
+     * @return bool
+     * @throws coding_exception
+     */
+    public static function can_create_data_deletion_request_for_children(int $userid, int $requesterid = null): bool {
+        global $USER;
+        $requesterid = $requesterid ?: $USER->id;
+        return has_capability('tool/dataprivacy:makedatadeletionrequestsforchildren', \context_user::instance($userid),
+            $requesterid);
+    }
+
     /**
      * Checks whether a user can download a data request.
      *
index a309c8e..3c5ce09 100644 (file)
@@ -170,6 +170,10 @@ class data_request_exporter extends persistent_exporter {
                 $values['canreview'] = true;
                 // Whether the DPO can approve or deny the request.
                 $values['approvedeny'] = in_array($requesttype, [api::DATAREQUEST_TYPE_EXPORT, api::DATAREQUEST_TYPE_DELETE]);
+                // If the request's type is delete, check if user have permission to approve/deny it.
+                if ($requesttype == api::DATAREQUEST_TYPE_DELETE) {
+                    $values['approvedeny'] = api::can_create_data_deletion_request_for_other();
+                }
                 break;
             case api::DATAREQUEST_STATUS_APPROVED:
                 $values['statuslabelclass'] = 'badge-info';
index 7249adf..2f12f58 100644 (file)
@@ -234,10 +234,6 @@ class data_registry_page implements renderable, templatable {
 
             $coursecontext = \context_course::instance($course->id);
 
-            if (!$course->visible && !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
-                continue;
-            }
-
             $coursenode = [
                 'text' => shorten_text(format_string($course->shortname, true, ['context' => $coursecontext])),
                 'contextid' => $coursecontext->id,
index b86e464..303c774 100644 (file)
@@ -116,9 +116,17 @@ class data_requests_table extends table_sql {
      *
      * @param stdClass $data The row data.
      * @return string
+     * @throws \moodle_exception
+     * @throws coding_exception
      */
     public function col_select($data) {
         if ($data->status == \tool_dataprivacy\api::DATAREQUEST_STATUS_AWAITING_APPROVAL) {
+            if ($data->type == \tool_dataprivacy\api::DATAREQUEST_TYPE_DELETE
+                && !api::can_create_data_deletion_request_for_other()) {
+                // Don't show checkbox if request's type is delete and user don't have permission.
+                return false;
+            }
+
             $stringdata = [
                 'username' => $data->foruser->fullname,
                 'requesttype' => \core_text::strtolower($data->typenameshort)
@@ -206,6 +214,7 @@ class data_requests_table extends table_sql {
 
         $requestid = $data->id;
         $status = $data->status;
+        $persistent = $this->datarequests[$requestid];
 
         // Prepare actions.
         $actions = [];
@@ -232,6 +241,11 @@ class data_requests_table extends table_sql {
                 }
                 break;
             case api::DATAREQUEST_STATUS_AWAITING_APPROVAL:
+                // Only show "Approve" and "Deny" button for deletion request if current user has permission.
+                if ($persistent->get('type') == api::DATAREQUEST_TYPE_DELETE &&
+                    !api::can_create_data_deletion_request_for_other()) {
+                    break;
+                }
                 // Approve.
                 $actiondata['data-action'] = 'approve';
                 $actiontext = get_string('approverequest', 'tool_dataprivacy');
@@ -253,9 +267,11 @@ class data_requests_table extends table_sql {
         }
 
         if ($this->manage) {
-            $persistent = $this->datarequests[$requestid];
             $canreset = $persistent->is_active() || empty($this->ongoingrequests[$data->foruser->id]->{$data->type});
             $canreset = $canreset && $persistent->is_resettable();
+            // Prevent re-submmit deletion request if current user don't have permission.
+            $canreset = $canreset && ($persistent->get('type') != api::DATAREQUEST_TYPE_DELETE ||
+                    api::can_create_data_deletion_request_for_other());
             if ($canreset) {
                 $reseturl = new moodle_url('/admin/tool/dataprivacy/resubmitrequest.php', [
                         'requestid' => $requestid,
index 12db2ea..b32dc15 100644 (file)
@@ -76,6 +76,19 @@ if ($data = $mform->get_data()) {
         }
     }
 
+    if ($data->type == \tool_dataprivacy\api::DATAREQUEST_TYPE_DELETE) {
+        if ($data->userid == $USER->id) {
+            if (!\tool_dataprivacy\api::can_create_data_deletion_request_for_self()) {
+                throw new moodle_exception('nopermissions', 'error', '',
+                    get_string('errorcannotrequestdeleteforself', 'tool_dataprivacy'));
+            }
+        } else if (!\tool_dataprivacy\api::can_create_data_deletion_request_for_other()
+            && !\tool_dataprivacy\api::can_create_data_deletion_request_for_children($data->userid)) {
+            throw new moodle_exception('nopermissions', 'error', '',
+                get_string('errorcannotrequestdeleteforother', 'tool_dataprivacy'));
+        }
+    }
+
     \tool_dataprivacy\api::create_data_request($data->userid, $data->type, $data->comments);
 
     if ($manage) {
@@ -93,7 +106,7 @@ $PAGE->set_title($title);
 echo $OUTPUT->header();
 echo $OUTPUT->heading($title);
 
-echo $OUTPUT->box_start();
+echo $OUTPUT->box_start('createrequestform');
 $mform->display();
 echo $OUTPUT->box_end();
 
index 4c94a8a..c91213c 100644 (file)
@@ -45,6 +45,7 @@ class tool_dataprivacy_data_request_form extends moodleform {
      * Form definition.
      *
      * @throws coding_exception
+     * @throws dml_exception
      */
     public function definition() {
         global $USER;
@@ -108,6 +109,24 @@ class tool_dataprivacy_data_request_form extends moodleform {
         // Action buttons.
         $this->add_action_buttons();
 
+        $shouldfreeze = false;
+        if ($this->manage) {
+            $shouldfreeze = !api::can_create_data_deletion_request_for_other();
+        } else {
+            $shouldfreeze = !api::can_create_data_deletion_request_for_self();
+            if ($shouldfreeze && !empty($useroptions)) {
+                foreach ($useroptions as $userid => $useroption) {
+                    if (api::can_create_data_deletion_request_for_children($userid)) {
+                        $shouldfreeze = false;
+                        break;
+                    }
+                }
+            }
+        }
+
+        if ($shouldfreeze) {
+            $mform->freeze('type');
+        }
     }
 
     /**
@@ -120,6 +139,7 @@ class tool_dataprivacy_data_request_form extends moodleform {
      * @throws dml_exception
      */
     public function validation($data, $files) {
+        global $USER;
         $errors = [];
 
         $validrequesttypes = [
@@ -134,6 +154,19 @@ class tool_dataprivacy_data_request_form extends moodleform {
             $errors['type'] = get_string('errorrequestalreadyexists', 'tool_dataprivacy');
         }
 
+        // Check if current user can create data deletion request.
+        $userid = $data['userid'];
+        if ($data['type'] == api::DATAREQUEST_TYPE_DELETE) {
+            if ($userid == $USER->id) {
+                if (!api::can_create_data_deletion_request_for_self()) {
+                    $errors['type'] = get_string('errorcannotrequestdeleteforself', 'tool_dataprivacy');
+                }
+            } else if (!api::can_create_data_deletion_request_for_other()
+                && !api::can_create_data_deletion_request_for_children($userid)) {
+                $errors['type'] = get_string('errorcannotrequestdeleteforother', 'tool_dataprivacy');
+            }
+        }
+
         return $errors;
     }
 }
index ad31867..d70f4a5 100644 (file)
@@ -34,6 +34,15 @@ $capabilities = [
         'archetypes' => []
     ],
 
+    // Capability for create new delete data request. Usually given to the site's Protection Officer.
+    'tool/dataprivacy:requestdeleteforotheruser' => [
+        'riskbitmask' => RISK_SPAM | RISK_PERSONAL | RISK_XSS | RISK_DATALOSS,
+        'captype' => 'write',
+        'contextlevel' => CONTEXT_SYSTEM,
+        'archetypes' => [],
+        'clonepermissionsfrom' => 'tool/dataprivacy:managedatarequests'
+    ],
+
     // Capability for managing the data registry. Usually given to the site's Data Protection Officer.
     'tool/dataprivacy:managedataregistry' => [
         'riskbitmask' => RISK_SPAM | RISK_PERSONAL | RISK_XSS | RISK_DATALOSS,
@@ -50,6 +59,15 @@ $capabilities = [
         'archetypes' => []
     ],
 
+    // Capability for parents/guardians to make delete data requests on behalf of their children.
+    'tool/dataprivacy:makedatadeletionrequestsforchildren' => [
+        'riskbitmask' => RISK_SPAM | RISK_PERSONAL,
+        'captype' => 'write',
+        'contextlevel' => CONTEXT_USER,
+        'archetypes' => [],
+        'clonepermissionsfrom' => 'tool/dataprivacy:makedatarequestsforchildren'
+    ],
+
     // Capability for users to download the results of their own data request.
     'tool/dataprivacy:downloadownrequest' => [
         'riskbitmask' => 0,
@@ -67,4 +85,14 @@ $capabilities = [
         'contextlevel' => CONTEXT_USER,
         'archetypes' => []
     ],
+
+    // Capability for users to create delete data request for their own.
+    'tool/dataprivacy:requestdelete' => [
+        'riskbitmask' => RISK_DATALOSS,
+        'captype' => 'write',
+        'contextlevel' => CONTEXT_USER,
+        'archetypes' => [
+            'user' => CAP_ALLOW
+        ]
+    ]
 ];
index bb3fa70..5a0efd4 100644 (file)
@@ -70,6 +70,7 @@ $string['contextlevelname80'] = 'Blocks';
 $string['contextpurposecategorysaved'] = 'Purpose and category saved.';
 $string['contactdpoviaprivacypolicy'] = 'Please contact the privacy officer as described in the privacy policy.';
 $string['createcategory'] = 'Create data category';
+$string['createdeletedatarequest'] = 'Create data deletion request';
 $string['createnewdatarequest'] = 'Create a new data request';
 $string['createpurpose'] = 'Create data purpose';
 $string['creationauto'] = 'Automatically';
@@ -81,6 +82,9 @@ $string['dataprivacy:managedatarequests'] = 'Manage data requests';
 $string['dataprivacy:managedataregistry'] = 'Manage data registry';
 $string['dataprivacy:downloadownrequest'] = 'Download your own exported data';
 $string['dataprivacy:downloadallrequests'] = 'Download exported data for everyone';
+$string['dataprivacy:requestdeleteforotheruser'] = 'Request data deletion on behalf of another user';
+$string['dataprivacy:makedatadeletionrequestsforchildren'] = 'Request data deletion for minors';
+$string['dataprivacy:requestdelete'] = 'Request data deletion for yourself';
 $string['dataregistry'] = 'Data registry';
 $string['dataregistryinfo'] = 'The data registry enables categories (types of data) and purposes (the reasons for processing data) to be set for all content on the site - from users and courses down to activities and blocks. For each purpose, a retention period may be set. When a retention period has expired, the data is flagged and listed for deletion, awaiting admin confirmation.';
 $string['dataretentionexplanation'] = 'This summary shows the default categories and purposes for retaining user data. Certain areas may have more specific categories and purposes than those listed here.';
@@ -124,6 +128,8 @@ $string['editpurposes'] = 'Edit purposes';
 $string['effectiveretentionperiodcourse'] = '{$a} (after the course end date)';
 $string['effectiveretentionperioduser'] = '{$a} (since the last time the user accessed the site)';
 $string['emailsalutation'] = 'Dear {$a},';
+$string['errorcannotrequestdeleteforself'] = 'You don\'t have permission to create deletion request for yourself.';
+$string['errorcannotrequestdeleteforother'] = 'You don\'t have permission to create deletion request for this user.';
 $string['errorinvalidrequestcreationmethod'] = 'Invalid request creation method!';
 $string['errorinvalidrequeststatus'] = 'Invalid request status!';
 $string['errorinvalidrequesttype'] = 'Invalid request type!';
index 8655f1c..c069301 100644 (file)
@@ -77,8 +77,9 @@ function tool_dataprivacy_myprofile_navigation(tree $tree, $user, $iscurrentuser
 
         // Check if the user has an ongoing data deletion request.
         $hasdeleterequest = \tool_dataprivacy\api::has_ongoing_request($user->id, \tool_dataprivacy\api::DATAREQUEST_TYPE_DELETE);
-        // Show data deletion link only if the user doesn't have an ongoing data deletion request.
-        if (!$hasdeleterequest) {
+        // Show data deletion link only if the user doesn't have an ongoing data deletion request and has permission
+        // to create data deletion request.
+        if (!$hasdeleterequest && \tool_dataprivacy\api::can_create_data_deletion_request_for_self()) {
             $deleteparams = ['type' => \tool_dataprivacy\api::DATAREQUEST_TYPE_DELETE];
             $deleteurl = new moodle_url('/admin/tool/dataprivacy/createdatarequest.php', $deleteparams);
             $deletenode = new core_user\output\myprofile\node('privacyandpolicies', 'requestdatadeletion',
index ac7520e..4f3ef55 100644 (file)
@@ -44,6 +44,11 @@ $stringparams = (object) [
     ];
 
 if (null !== $confirm && confirm_sesskey()) {
+    if ($originalrequest->get('type') == \tool_dataprivacy\api::DATAREQUEST_TYPE_DELETE
+        && !\tool_dataprivacy\api::can_create_data_deletion_request_for_other()) {
+        throw new required_capability_exception(context_system::instance(),
+            'tool/dataprivacy:requestdeleteforotheruser', 'nopermissions', '');
+    }
     $originalrequest->resubmit_request();
     redirect($manageurl, get_string('resubmittedrequest', 'tool_dataprivacy', $stringparams));
 }
index 8e60223..f95dedd 100644 (file)
@@ -2109,4 +2109,90 @@ class tool_dataprivacy_api_testcase extends advanced_testcase {
 
         return $request;
     }
+
+    /**
+     * Test user cannot create data deletion request for themselves if they don't have
+     * "tool/dataprivacy:requestdelete" capability.
+     *
+     * @throws coding_exception
+     */
+    public function test_can_create_data_deletion_request_for_self_no() {
+        $this->resetAfterTest();
+        $userid = $this->getDataGenerator()->create_user()->id;
+        $roleid = $this->getDataGenerator()->create_role();
+        assign_capability('tool/dataprivacy:requestdelete', CAP_PROHIBIT, $roleid, context_user::instance($userid));
+        role_assign($roleid, $userid, context_user::instance($userid));
+        $this->setUser($userid);
+        $this->assertFalse(api::can_create_data_deletion_request_for_self());
+    }
+
+    /**
+     * Test user can create data deletion request for themselves if they have
+     * "tool/dataprivacy:requestdelete" capability.
+     *
+     * @throws coding_exception
+     */
+    public function test_can_create_data_deletion_request_for_self_yes() {
+        $this->resetAfterTest();
+        $userid = $this->getDataGenerator()->create_user()->id;
+        $this->setUser($userid);
+        $this->assertTrue(api::can_create_data_deletion_request_for_self());
+    }
+
+    /**
+     * Test user cannot create data deletion request for another user if they
+     * don't have "tool/dataprivacy:requestdeleteforotheruser" capability.
+     *
+     * @throws coding_exception
+     * @throws dml_exception
+     */
+    public function test_can_create_data_deletion_request_for_other_no() {
+        $this->resetAfterTest();
+        $userid = $this->getDataGenerator()->create_user()->id;
+        $this->setUser($userid);
+        $this->assertFalse(api::can_create_data_deletion_request_for_other());
+    }
+
+    /**
+     * Test user can create data deletion request for another user if they
+     * don't have "tool/dataprivacy:requestdeleteforotheruser" capability.
+     *
+     * @throws coding_exception
+     */
+    public function test_can_create_data_deletion_request_for_other_yes() {
+        $this->resetAfterTest();
+        $userid = $this->getDataGenerator()->create_user()->id;
+        $roleid = $this->getDataGenerator()->create_role();
+        $contextsystem = context_system::instance();
+        assign_capability('tool/dataprivacy:requestdeleteforotheruser', CAP_ALLOW, $roleid, $contextsystem);
+        role_assign($roleid, $userid, $contextsystem);
+        $this->setUser($userid);
+        $this->assertTrue(api::can_create_data_deletion_request_for_other($userid));
+    }
+
+    /**
+     * Check parents can create data deletion request for their children but not others.
+     *
+     * @throws coding_exception
+     * @throws dml_exception
+     */
+    public function test_can_create_data_deletion_request_for_children() {
+        $this->resetAfterTest();
+
+        $parent = $this->getDataGenerator()->create_user();
+        $child = $this->getDataGenerator()->create_user();
+        $otheruser = $this->getDataGenerator()->create_user();
+
+        $contextsystem = \context_system::instance();
+        $parentrole = $this->getDataGenerator()->create_role();
+        assign_capability('tool/dataprivacy:makedatarequestsforchildren', CAP_ALLOW,
+            $parentrole, $contextsystem);
+        assign_capability('tool/dataprivacy:makedatadeletionrequestsforchildren', CAP_ALLOW,
+            $parentrole, $contextsystem);
+        role_assign($parentrole, $parent->id, \context_user::instance($child->id));
+
+        $this->setUser($parent);
+        $this->assertTrue(api::can_create_data_deletion_request_for_children($child->id));
+        $this->assertFalse(api::can_create_data_deletion_request_for_children($otheruser->id));
+    }
 }
index e6c7fde..5806102 100644 (file)
@@ -6,18 +6,24 @@ Feature: Data delete from the privacy API
 
   Background:
     Given the following "users" exist:
-      | username | firstname      | lastname |
-      | victim   | Victim User    | 1        |
-      | parent   | Long-suffering | Parent   |
+      | username       | firstname       | lastname |
+      | victim         | Victim User     | 1        |
+      | parent         | Long-suffering  | Parent   |
+      | privacyofficer | Privacy Officer | One      |
     And the following "roles" exist:
       | shortname | name  | archetype |
       | tired     | Tired |           |
     And the following "permission overrides" exist:
-      | capability                                   | permission | role  | contextlevel | reference |
-      | tool/dataprivacy:makedatarequestsforchildren | Allow      | tired | System       |           |
+      | capability                                           | permission | role    | contextlevel | reference |
+      | tool/dataprivacy:makedatarequestsforchildren         | Allow      | tired   | System       |           |
+      | tool/dataprivacy:makedatadeletionrequestsforchildren | Allow      | tired   | System       |           |
+      | tool/dataprivacy:managedatarequests                  | Allow      | manager | System       |           |
     And the following "role assigns" exist:
       | user   | role  | contextlevel | reference |
       | parent | tired | User         | victim    |
+    And the following "system role assigns" exist:
+      | user           | role    | contextlevel |
+      | privacyofficer | manager | User         |
     And the following config values are set as admin:
       | contactdataprotectionofficer | 1  | tool_dataprivacy |
     And the following data privacy "categories" exist:
@@ -26,6 +32,10 @@ Feature: Data delete from the privacy API
     And the following data privacy "purposes" exist:
       | name         | retentionperiod |
       | Site purpose | P10Y           |
+    And the following config values are set as admin:
+      | contactdataprotectionofficer | 1  | tool_dataprivacy |
+      | privacyrequestexpiry         | 55 | tool_dataprivacy |
+      | dporoles                     | 1  | tool_dataprivacy |
     And I set the site category and purpose to "Site category" and "Site purpose"
 
   @javascript
@@ -115,3 +125,112 @@ Feature: Data delete from the privacy API
     And I run all adhoc tasks
     And I reload the page
     And I should see "You don't have any personal data requests"
+
+  @javascript
+  Scenario: As a Privacy Officer, I cannot create data deletion request unless I have permission.
+    Given I log in as "privacyofficer"
+    And I navigate to "Users > Privacy and policies > Data requests" in site administration
+    And I follow "New request"
+    And I open the autocomplete suggestions list
+    And I click on "Victim User 1" item in the autocomplete list
+    Then I should see "Export all of my personal data"
+    And "Type" "select" should not be visible
+    And the following "permission overrides" exist:
+      | capability                                 | permission | role    | contextlevel | reference |
+      | tool/dataprivacy:requestdeleteforotheruser | Allow      | manager | System       |           |
+    And I reload the page
+    And I open the autocomplete suggestions list
+    And I click on "Victim User 1" item in the autocomplete list
+    And "Type" "select" should be visible
+
+  @javascript
+  Scenario: As a student, I cannot create data deletion request unless I have permission.
+    Given I log in as "victim"
+    And I follow "Profile" in the user menu
+    And I follow "Data requests"
+    And I follow "New request"
+    Then "Type" "select" should exist
+    And the following "permission overrides" exist:
+      | capability                     | permission | role | contextlevel | reference |
+      | tool/dataprivacy:requestdelete | Prevent    | user | System       |           |
+    And I reload the page
+    And I should see "Export all of my personal data"
+    And "Type" "select" should not exist
+
+  @javascript
+  Scenario: As a parent, I cannot create data deletion request unless I have permission.
+    Given I log in as "parent"
+    And the following "permission overrides" exist:
+      | capability                                           | permission | role  | contextlevel | reference |
+      | tool/dataprivacy:makedatadeletionrequestsforchildren | Prevent    | tired | System       | victim    |
+    And I follow "Profile" in the user menu
+    And I follow "Data requests"
+    And I follow "New request"
+    And I open the autocomplete suggestions list
+    And I click on "Victim User 1" item in the autocomplete list
+    And I set the field "Type" to "Delete all of my personal data"
+    And I press "Save changes"
+    And I should see "You don't have permission to create deletion request for this user."
+    And the following "permission overrides" exist:
+      | capability                                           | permission | role  | contextlevel | reference |
+      | tool/dataprivacy:makedatadeletionrequestsforchildren | Allow      | tired | System       | victim    |
+      | tool/dataprivacy:requestdelete                       | Prevent    | user  | System       |           |
+    And I open the autocomplete suggestions list
+    And I click on "Long-suffering Parent" item in the autocomplete list
+    And I press "Save changes"
+    And I should see "You don't have permission to create deletion request for yourself."
+
+  @javascript
+  Scenario: As a student, link to create data deletion should not be shown if I don't have permission.
+    Given the following "permission overrides" exist:
+      | capability                     | permission | role | contextlevel | reference |
+      | tool/dataprivacy:requestdelete | Prohibit   | user | System       |           |
+    When I log in as "victim"
+    And I follow "Profile" in the user menu
+    Then I should not see "Delete my account"
+
+  @javascript
+  Scenario: As a Privacy Officer, I cannot Approve to Deny deletion data request without permission.
+    Given the following "permission overrides" exist:
+      | capability                                 | permission | role    | contextlevel | reference |
+      | tool/dataprivacy:requestdeleteforotheruser | Allow      | manager | System       |           |
+    When I log in as "privacyofficer"
+    And I navigate to "Users > Privacy and policies > Data requests" in site administration
+    And I follow "New request"
+    And I open the autocomplete suggestions list
+    And I click on "Victim User 1" item in the autocomplete list
+    And I set the field "Type" to "Delete all of my personal data"
+    And I press "Save changes"
+    And the following "permission overrides" exist:
+      | capability                                 | permission | role    | contextlevel | reference |
+      | tool/dataprivacy:requestdeleteforotheruser | Prohibit   | manager | System       |           |
+    And I reload the page
+    Then ".selectrequests" "css_element" should not exist
+    And I open the action menu in "region-main" "region"
+    And I should not see "Approve request"
+    And I should not see "Deny request"
+    And I choose "View the request" in the open action menu
+    And "Approve" "button" should not exist
+    And "Deny" "button" should not exist
+
+  @javascript
+  Scenario: As a Privacy Officer, I cannot re-submit deletion data request without permission.
+    Given the following "permission overrides" exist:
+      | capability                                 | permission | role    | contextlevel | reference |
+      | tool/dataprivacy:requestdeleteforotheruser | Allow      | manager | System       |           |
+    When I log in as "privacyofficer"
+    And I navigate to "Users > Privacy and policies > Data requests" in site administration
+    And I follow "New request"
+    And I open the autocomplete suggestions list
+    And I click on "Victim User 1" item in the autocomplete list
+    And I set the field "Type" to "Delete all of my personal data"
+    And I press "Save changes"
+    And I open the action menu in "region-main" "region"
+    And I follow "Deny request"
+    And I press "Deny request"
+    And the following "permission overrides" exist:
+      | capability                                 | permission | role    | contextlevel | reference |
+      | tool/dataprivacy:requestdeleteforotheruser | Prohibit   | manager | System       |           |
+    And I reload the page
+    And I open the action menu in "region-main" "region"
+    Then I should not see "Resubmit as new request"
index 411f0ac..8089b41 100644 (file)
@@ -24,6 +24,6 @@
 
 defined('MOODLE_INTERNAL') || die;
 
-$plugin->version   = 2019011500;
+$plugin->version   = 2019040800;
 $plugin->requires  = 2018112800;        // Moodle 3.5dev (Build 2018031600) and upwards.
 $plugin->component = 'tool_dataprivacy';
index 15552d5..ee40e70 100644 (file)
@@ -47,8 +47,7 @@ class tool_generator_make_testplan_form extends moodleform {
             tool_generator_testplan_backend::get_size_choices());
         $mform->setDefault('size', tool_generator_testplan_backend::DEFAULT_SIZE);
 
-        $mform->addElement('select', 'courseid', get_string('targetcourse', 'tool_generator'),
-            tool_generator_testplan_backend::get_course_options());
+        $mform->addElement('course', 'courseid', get_string('targetcourse', 'tool_generator'));
 
         $mform->addElement('advcheckbox', 'updateuserspassword', get_string('updateuserspassword', 'tool_generator'));
         $mform->addHelpButton('updateuserspassword', 'updateuserspassword', 'tool_generator');
index 2e43b4c..6b3c2e4 100644 (file)
@@ -71,25 +71,6 @@ class tool_generator_testplan_backend extends tool_generator_backend {
         return $options;
     }
 
-    /**
-     * Gets the list of courses that can be used used to generate a test.
-     *
-     * @return array The list of options as courseid => name
-     */
-    public static function get_course_options() {
-        $courses = get_courses('all', 'c.sortorder ASC', 'c.id, c.shortname, c.fullname');
-        if (!$courses) {
-            print_error('error_nocourses', 'tool_generator');
-        }
-
-        $options = array();
-        unset($courses[1]);
-        foreach ($courses as $course) {
-            $options[$course->id] = $course->fullname . '(' . $course->shortname . ')';
-        }
-        return $options;
-    }
-
     /**
      * Getter for moodle-performance-comparison project URL.
      *
diff --git a/admin/tool/generator/upgrade.txt b/admin/tool/generator/upgrade.txt
new file mode 100644 (file)
index 0000000..4d5b19f
--- /dev/null
@@ -0,0 +1,6 @@
+This files describes API changes in core libraries and APIs,
+information provided here is intended especially for developers.
+
+=== 3.7 ===
+
+* Function tool_generator_testplan_backend::get_course_options() is removed, the 'course' form element is used instead.
index 96cfd0c..e88a683 100644 (file)
@@ -43,6 +43,9 @@ trait buffered_writer {
     /** @var int $count Counter. */
     protected $count = 0;
 
+    /** @var bool If true, writes JSON instead of PHP serialized data for 'other' field */
+    protected $jsonformat = false;
+
     /**
      * Should the event be ignored (== not logged)?
      * @param \core\event\base $event
@@ -69,7 +72,11 @@ trait buffered_writer {
         // at the same time this lowers memory use because
         // snapshots and custom objects may be garbage collected.
         $entry = $event->get_data();
-        $entry['other'] = serialize($entry['other']);
+        if ($this->jsonformat) {
+            $entry['other'] = json_encode($entry['other']);
+        } else {
+            $entry['other'] = serialize($entry['other']);
+        }
         $entry['origin'] = $PAGE->requestorigin;
         $entry['ip'] = $PAGE->requestip;
         $entry['realuserid'] = \core\session\manager::is_loggedinas() ? $GLOBALS['USER']->realuser : null;
index 93e0af1..2e94774 100644 (file)
@@ -62,6 +62,25 @@ trait reader {
         return $this->store;
     }
 
+    /**
+     * Function decodes the other field into an array using either PHP serialisation or JSON.
+     *
+     * Note that this does not rely on the config setting, it supports both formats, so you can
+     * use it for data before/after making a change to the config setting.
+     *
+     * The return value is usually an array but it can also be null or a boolean or something.
+     *
+     * @param string $other Other value
+     * @return mixed Decoded value
+     */
+    public static function decode_other(?string $other) {
+        if ($other === 'N;' || preg_match('~^.:~', $other)) {
+            return unserialize($other);
+        } else {
+            return json_decode($other, true);
+        }
+    }
+
     /**
      * Adds ID column to $sort to make sure events from one request
      * within 1 second are returned in the same order.
index 8a057cf..95c09e9 100644 (file)
@@ -37,6 +37,7 @@ use core_privacy\local\request\transform;
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 class helper {
+    use \tool_log\helper\reader;
 
     /**
      * Returns an event from a standard record.
@@ -49,7 +50,7 @@ class helper {
         $extra = ['origin' => $data->origin, 'ip' => $data->ip, 'realuserid' => $data->realuserid];
         $data = (array) $data;
         $id = $data['id'];
-        $data['other'] = unserialize($data['other']);
+        $data['other'] = self::decode_other($data['other']);
         if ($data['other'] === false) {
             $data['other'] = [];
         }
index 10398df..5c85e3d 100644 (file)
@@ -57,6 +57,9 @@ class store implements \tool_log\log\writer, \core\log\sql_reader {
         $levels = $this->get_config('includelevels', '');
         $this->includeactions = $actions === '' ? array() : explode(',', $actions);
         $this->includelevels = $levels === '' ? array() : explode(',', $levels);
+        // JSON writing defaults to false (table format compatibility with older versions).
+        // Note: This variable is defined in the buffered_writer trait.
+        $this->jsonformat = (bool)$this->get_config('jsonformat', false);
     }
 
     /**
@@ -223,7 +226,7 @@ class store implements \tool_log\log\writer, \core\log\sql_reader {
         $extra = array('origin' => $data->origin, 'ip' => $data->ip, 'realuserid' => $data->realuserid);
         $data = (array)$data;
         $id = $data['id'];
-        $data['other'] = unserialize($data['other']);
+        $data['other'] = self::decode_other($data['other']);
         if ($data['other'] === false) {
             $data['other'] = array();
         }
index 04137ab..b5536ab 100644 (file)
@@ -39,5 +39,12 @@ function xmldb_logstore_database_upgrade($oldversion) {
     // Automatically generated Moodle v3.6.0 release upgrade line.
     // Put any upgrade step following this.
 
+    if ($oldversion < 2019032800) {
+        // For existing installations, set the new jsonformat option to off (no behaviour change).
+        // New installations default to on.
+        set_config('jsonformat', 0, 'logstore_database');
+        upgrade_plugin_savepoint(true, 2019032800, 'logstore', 'database');
+    }
+
     return true;
 }
index 31edf3a..3fd8017 100644 (file)
@@ -39,6 +39,8 @@ $string['includeactions'] = 'Include actions of these types';
 $string['includelevels'] = 'Include actions with these educational levels';
 $string['filters'] = 'Filter logs';
 $string['filters_help'] = 'Enable filters that exclude some actions from being logged.';
+$string['jsonformat'] = 'JSON format';
+$string['jsonformat_desc'] = 'Use standard JSON format instead of PHP serialised data in the \'other\' database field.';
 $string['logguests'] = 'Log guest actions';
 $string['other'] = 'Other';
 $string['participating'] = 'Participating';
index 4e45038..d406fd0 100644 (file)
@@ -59,6 +59,10 @@ if ($hassiteconfig) {
     $settings->add(new admin_setting_configtext('logstore_database/buffersize', get_string('buffersize',
         'logstore_database'), get_string('buffersize_help', 'logstore_database'), 50));
 
+    $settings->add(new admin_setting_configcheckbox('logstore_database/jsonformat',
+            new lang_string('jsonformat', 'logstore_database'),
+            new lang_string('jsonformat_desc', 'logstore_database'), 1));
+
     // Filters.
     $settings->add(new admin_setting_heading('filters', get_string('filters', 'logstore_database'), get_string('filters_help',
         'logstore_database')));
index a05c33f..10dab29 100644 (file)
@@ -28,11 +28,21 @@ require_once(__DIR__ . '/fixtures/event.php');
 require_once(__DIR__ . '/fixtures/store.php');
 
 class logstore_database_store_testcase extends advanced_testcase {
-    public function test_log_writing() {
+    /**
+     * Tests log writing.
+     *
+     * @param bool $jsonformat True to test with JSON format
+     * @dataProvider test_log_writing_provider
+     * @throws moodle_exception
+     */
+    public function test_log_writing(bool $jsonformat) {
         global $DB, $CFG;
         $this->resetAfterTest();
         $this->preventResetByRollback(); // Logging waits till the transaction gets committed.
 
+        // Apply JSON format system setting.
+        set_config('jsonformat', $jsonformat ? 1 : 0, 'logstore_database');
+
         $dbman = $DB->get_manager();
         $this->assertTrue($dbman->table_exists('logstore_standard_log'));
         $DB->delete_records('logstore_standard_log');
@@ -118,7 +128,11 @@ class logstore_database_store_testcase extends advanced_testcase {
 
         $log1 = reset($logs);
         unset($log1->id);
-        $log1->other = unserialize($log1->other);
+        if ($jsonformat) {
+            $log1->other = json_decode($log1->other, true);
+        } else {
+            $log1->other = unserialize($log1->other);
+        }
         $log1 = (array)$log1;
         $data = $event1->get_data();
         $data['origin'] = 'cli';
@@ -145,7 +159,11 @@ class logstore_database_store_testcase extends advanced_testcase {
 
         $log3 = array_shift($logs);
         unset($log3->id);
-        $log3->other = unserialize($log3->other);
+        if ($jsonformat) {
+            $log3->other = json_decode($log3->other, true);
+        } else {
+            $log3->other = unserialize($log3->other);
+        }
         $log3 = (array)$log3;
         $data = $event2->get_data();
         $data['origin'] = 'cli';
@@ -229,6 +247,19 @@ class logstore_database_store_testcase extends advanced_testcase {
         get_log_manager(true);
     }
 
+    /**
+     * Returns different JSON format settings so the test can be run with JSON format either on or
+     * off.
+     *
+     * @return [bool] Array of true/false
+     */
+    public static function test_log_writing_provider(): array {
+        return [
+            [false],
+            [true]
+        ];
+    }
+
     /**
      * Test method is_event_ignored.
      */
index dbf85d7..4159a06 100644 (file)
 /**
  * External database log store.
  *
- * @package    logstore_standard
+ * @package    logstore_database
  * @copyright  2013 Petr Skoda {@link http://skodak.org}
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version = 2018120300; // The current plugin version (Date: YYYYMMDDXX).
+$plugin->version = 2019032800; // The current plugin version (Date: YYYYMMDDXX).
 $plugin->requires = 2018112800; // Requires this Moodle version.
 $plugin->component = 'logstore_database'; // Full name of the plugin (used for diagnostics).
index fe93ff5..c296546 100644 (file)
@@ -38,6 +38,9 @@ class store implements \tool_log\log\writer, \core\log\sql_internal_table_reader
         $this->helper_setup($manager);
         // Log everything before setting is saved for the first time.
         $this->logguests = $this->get_config('logguests', 1);
+        // JSON writing defaults to false (table format compatibility with older versions).
+        // Note: This variable is defined in the buffered_writer trait.
+        $this->jsonformat = (bool)$this->get_config('jsonformat', false);
     }
 
     /**
@@ -120,7 +123,7 @@ class store implements \tool_log\log\writer, \core\log\sql_internal_table_reader
         $extra = array('origin' => $data->origin, 'ip' => $data->ip, 'realuserid' => $data->realuserid);
         $data = (array)$data;
         $id = $data['id'];
-        $data['other'] = unserialize($data['other']);
+        $data['other'] = self::decode_other($data['other']);
         if ($data['other'] === false) {
             $data['other'] = array();
         }
index d6cd26d..e1ac5a2 100644 (file)
@@ -39,5 +39,12 @@ function xmldb_logstore_standard_upgrade($oldversion) {
     // Automatically generated Moodle v3.6.0 release upgrade line.
     // Put any upgrade step following this.
 
+    if ($oldversion < 2019032800) {
+        // For existing installations, set the new jsonformat option to off (no behaviour change).
+        // New installations default to on.
+        set_config('jsonformat', 0, 'logstore_standard');
+        upgrade_plugin_savepoint(true, 2019032800, 'logstore', 'standard');
+    }
+
     return true;
 }
index 14a1d2b..12a4c42 100644 (file)
@@ -23,6 +23,8 @@
  */
 
 $string['buffersize'] = 'Write buffer size';
+$string['jsonformat'] = 'JSON format';
+$string['jsonformat_desc'] = 'Use standard JSON format instead of PHP serialised data in the \'other\' database field.';
 $string['pluginname'] = 'Standard log';
 $string['pluginname_desc'] = 'A log plugin stores log entries in a Moodle database table.';
 $string['privacy:metadata:log'] = 'A collection of past events';
index daa8131..fc4a894 100644 (file)
@@ -30,6 +30,10 @@ if ($hassiteconfig) {
         new lang_string('logguests', 'core_admin'),
         new lang_string('logguests_help', 'core_admin'), 1));
 
+    $settings->add(new admin_setting_configcheckbox('logstore_standard/jsonformat',
+            new lang_string('jsonformat', 'logstore_standard'),
+            new lang_string('jsonformat_desc', 'logstore_standard'), 1));
+
     $options = array(
         0    => new lang_string('neverdeletelogs'),
         1000 => new lang_string('numdays', '', 1000),
index def6684..25963af 100644 (file)
@@ -33,11 +33,21 @@ class logstore_standard_store_testcase extends advanced_testcase {
      */
     private $wedisabledgc = false;
 
-    public function test_log_writing() {
+    /**
+     * Tests log writing.
+     *
+     * @param bool $jsonformat True to test with JSON format
+     * @dataProvider test_log_writing_provider
+     * @throws moodle_exception
+     */
+    public function test_log_writing(bool $jsonformat) {
         global $DB;
         $this->resetAfterTest();
         $this->preventResetByRollback(); // Logging waits till the transaction gets committed.
 
+        // Apply JSON format system setting.
+        set_config('jsonformat', $jsonformat ? 1 : 0, 'logstore_standard');
+
         $this->setAdminUser();
         $user1 = $this->getDataGenerator()->create_user();
         $user2 = $this->getDataGenerator()->create_user();
@@ -82,7 +92,11 @@ class logstore_standard_store_testcase extends advanced_testcase {
 
         $log1 = reset($logs);
         unset($log1->id);
-        $log1->other = unserialize($log1->other);
+        if ($jsonformat) {
+            $log1->other = json_decode($log1->other, true);
+        } else {
+            $log1->other = unserialize($log1->other);
+        }
         $log1 = (array)$log1;
         $data = $event1->get_data();
         $data['origin'] = 'cli';
@@ -112,7 +126,11 @@ class logstore_standard_store_testcase extends advanced_testcase {
 
         $log3 = array_shift($logs);
         unset($log3->id);
-        $log3->other = unserialize($log3->other);
+        if ($jsonformat) {
+            $log3->other = json_decode($log3->other, true);
+        } else {
+            $log3->other = unserialize($log3->other);
+        }
         $log3 = (array)$log3;
         $data = $event2->get_data();
         $data['origin'] = 'restore';
@@ -200,6 +218,19 @@ class logstore_standard_store_testcase extends advanced_testcase {
         get_log_manager(true);
     }
 
+    /**
+     * Returns different JSON format settings so the test can be run with JSON format either on or
+     * off.
+     *
+     * @return [bool] Array of true/false
+     */
+    public static function test_log_writing_provider(): array {
+        return [
+            [false],
+            [true]
+        ];
+    }
+
     /**
      * Test logmanager::get_supported_reports returns all reports that require this store.
      */
@@ -332,6 +363,38 @@ class logstore_standard_store_testcase extends advanced_testcase {
         $this->assertEquals(1, $DB->count_records('logstore_standard_log'));
     }
 
+    /**
+     * Tests the decode_other function can cope with both JSON and PHP serialized format.
+     *
+     * @param mixed $value Value to encode and decode
+     * @dataProvider test_decode_other_provider
+     */
+    public function test_decode_other($value) {
+        $this->assertEquals($value, \logstore_standard\log\store::decode_other(serialize($value)));
+        $this->assertEquals($value, \logstore_standard\log\store::decode_other(json_encode($value)));
+    }
+
+    public function test_decode_other_with_wrongly_encoded_contents() {
+        $this->assertSame(null, \logstore_standard\log\store::decode_other(null));
+    }
+
+    /**
+     * List of possible values for 'other' field.
+     *
+     * I took these types from our logs based on the different first character of PHP serialized
+     * data - my query found only these types. The normal case is an array.
+     *
+     * @return array Array of parameters
+     */
+    public function test_decode_other_provider(): array {
+        return [
+            [['info' => 'd2819896', 'logurl' => 'discuss.php?d=2819896']],
+            [null],
+            ['just a string'],
+            [32768]
+        ];
+    }
+
     /**
      * Disable the garbage collector if it's enabled to ensure we don't adjust memory statistics.
      */
index 89ab35a..2a6b63d 100644 (file)
@@ -24,6 +24,6 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version = 2018120300; // The current plugin version (Date: YYYYMMDDXX).
+$plugin->version = 2019032800; // The current plugin version (Date: YYYYMMDDXX).
 $plugin->requires = 2018112800; // Requires this Moodle version.
 $plugin->component = 'logstore_standard'; // Full name of the plugin (used for diagnostics).
index e1b65e3..abaf3e2 100644 (file)
@@ -2,7 +2,14 @@ This files describes API changes in /admin/tool/log - plugins,
 information provided here is intended especially for developers.
 
 
+=== 3.7 ===
+
+* The new jsonformat option, which defaults to 'on' for a new install (and 'off' for existing installs) means that
+  the 'other' event field is now stored in JSON format instead of PHP serialize format in the database. The system
+  can read data in both formats but if any third-party software directly accesses the database field, it may need
+  to be modified (or require users to turn off jsonformat).
+
 === 3.6 ===
 
 * The legacy log store is in its first stage of deprecation and is due for removal in Moodle 4.0. Please use one of
-  the other log stores such as "standard" and "database".
\ No newline at end of file
+  the other log stores such as "standard" and "database".
index 1795635..e9d1c67 100644 (file)
@@ -262,6 +262,8 @@ class external extends external_api {
     /**
      * Creates an auto-login key for the current user. Is created only in https sites and is restricted by time and ip address.
      *
+     * Please note that it only works if the request comes from the Moodle mobile or desktop app.
+     *
      * @param string $privatetoken the user private token for validating the request
      * @return array with the settings and warnings
      * @since  Moodle 3.2
@@ -284,6 +286,12 @@ class external extends external_api {
             }
         }
 
+        // Only requests from the Moodle mobile or desktop app. This enhances security to avoid any type of XSS attack.
+        // This code goes intentionally here and not inside the check_autologin_prerequisites() function because it
+        // is used by other PHP scripts that can be opened in any browser.
+        if (!\core_useragent::is_moodle_app()) {
+            throw new moodle_exception('apprequired', 'tool_mobile');
+        }
         api::check_autologin_prerequisites($USER->id);
 
         if (isset($_GET['privatetoken']) or empty($privatetoken)) {
index 0b50d68..d53f7b4 100644 (file)
@@ -57,7 +57,8 @@ $functions = array(
         'classname'   => 'tool_mobile\external',
         'methodname'  => 'get_autologin_key',
         'description' => 'Creates an auto-login key for the current user.
-                            Is created only in https sites and is restricted by time and ip address.',
+                            Is created only in https sites and is restricted by time, ip address and only works if the request
+                            comes from the Moodle mobile or desktop app.',
         'type'        => 'write',
         'services'    => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
     ),
index a373878..0b01cd0 100644 (file)
@@ -27,6 +27,7 @@ $string['androidappid'] = 'Android app\'s unique identifier';
 $string['androidappid_desc'] = 'This setting may be left as default unless you have a custom Android app.';
 $string['apppolicy'] = 'App policy URL';
 $string['apppolicy_help'] = 'The URL of a policy for app users which is listed on the About page in the app. If the field is left empty, the site policy URL will be used instead.';
+$string['apprequired'] = 'This functionality is only available when accessed via the Moodle mobile or desktop app.';
 $string['autologinkeygenerationlockout'] = 'Auto-login key generation is blocked. You need to wait 6 minutes between requests.';
 $string['autologinnotallowedtoadmins'] = 'Auto-login is not allowed for site admins.';
 $string['cachedef_plugininfo'] = 'This stores the list of plugins with mobile addons';
index 4045ab1..1fee731 100644 (file)
@@ -213,6 +213,10 @@ class tool_mobile_external_testcase extends externallib_advanced_testcase {
         // Enable requeriments.
         $_GET['wstoken'] = $token->token;   // Mock parameters.
 
+        // Fake the app.
+        core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' .
+                'AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36 MoodleMobile');
+
         // Even if we force the password change for the current user we should be able to retrieve the key.
         set_user_preference('auth_forcepasswordchange', 1, $user->id);
 
@@ -240,6 +244,10 @@ class tool_mobile_external_testcase extends externallib_advanced_testcase {
         global $CFG;
         $this->resetAfterTest(true);
 
+        // Fake the app.
+        core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' .
+            'AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36 MoodleMobile');
+
         // Need to disable webservices to verify that's checked.
         $CFG->enablewebservices = 0;
         $CFG->enablemobilewebservice = 0;
@@ -256,6 +264,10 @@ class tool_mobile_external_testcase extends externallib_advanced_testcase {
     public function test_get_autologin_key_missing_https() {
         global $CFG;
 
+        // Fake the app.
+        core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' .
+            'AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36 MoodleMobile');
+
         // Need to simulate a non HTTPS site here.
         $CFG->wwwroot = str_replace('https:', 'http:', $CFG->wwwroot);
 
@@ -276,6 +288,10 @@ class tool_mobile_external_testcase extends externallib_advanced_testcase {
         $this->resetAfterTest(true);
         $this->setAdminUser();
 
+        // Fake the app.
+        core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' .
+            'AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36 MoodleMobile');
+
         $this->expectException('moodle_exception');
         $this->expectExceptionMessage(get_string('autologinnotallowedtoadmins', 'tool_mobile'));
         $result = external::get_autologin_key('');
@@ -296,6 +312,10 @@ class tool_mobile_external_testcase extends externallib_advanced_testcase {
         $token = external_generate_token_for_current_user($service);
         $_GET['wstoken'] = $token->token;   // Mock parameters.
 
+        // Fake the app.
+        core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' .
+            'AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36 MoodleMobile');
+
         $result = external::get_autologin_key($token->privatetoken);
         $result = external_api::clean_returnvalue(external::get_autologin_key_returns(), $result);
 
@@ -311,6 +331,20 @@ class tool_mobile_external_testcase extends externallib_advanced_testcase {
         $result = external::get_autologin_key($token->privatetoken);
     }
 
+    /**
+     * Test get_autologin_key missing app_request.
+     */
+    public function test_get_autologin_key_missing_app_request() {
+        global $CFG;
+
+        $this->resetAfterTest(true);
+        $this->setAdminUser();
+
+        $this->expectException('moodle_exception');
+        $this->expectExceptionMessage(get_string('apprequired', 'tool_mobile'));
+        $result = external::get_autologin_key('');
+    }
+
     /**
      * Test get_content.
      */
index ec720c3..d39aa5f 100644 (file)
@@ -4,6 +4,10 @@ Information provided here is intended especially for developers.
 === 3.7 ===
 
  * New external function tool_mobile::tool_mobile_call_external_function allows calling multiple external functions and returns all responses.
+ * External function tool_mobile::get_autologin_key now only works if the request comes from the Moodle mobile or desktop app.
+   This increases confidence that requests did originate from the mobile app, decreasing the likelihood of an XSS attack.
+   If you want to use this functionality, please override the Web Service via the override_webservice_execution callback although
+   this is not recommended or encouraged.
 
 === 3.5 ===
 
index b08b6a5..6477c4d 100644 (file)
@@ -108,6 +108,16 @@ class category_bin extends base_bin {
 
         require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
 
+        // As far as recycle bin is using MODE_AUTOMATED, it observes the backup_auto_storage
+        // setting (storing backups @ real location. For recycle bin we want to ensure that
+        // backup files are always stored in Moodle file area. In order to achieve that, we
+        // hack the setting here via $CFG->forced_plugin_settings, so it won't interfere other
+        // operations.  See MDL-65218 for more information.
+        // This hack will be removed once recycle bin switches to use its own backup mode, with
+        // own preferences and 100% appart from MODLE_AUTOMATED.
+        // TODO: Remove this as part of MDL-65228.
+        $CFG->forced_plugin_settings['backup'] = ['backup_auto_storage' => 0];
+
         // Backup the course.
         $user = get_admin();
         $controller = new \backup_controller(
@@ -115,11 +125,15 @@ class category_bin extends base_bin {
             $course->id,
             \backup::FORMAT_MOODLE,
             \backup::INTERACTIVE_NO,
-            \backup::MODE_GENERAL,
+            \backup::MODE_AUTOMATED,
             $user->id
         );
         $controller->execute_plan();
 
+        // We don't need the forced setting anymore, hence unsetting it.
+        // TODO: Remove this as part of MDL-65228.
+        unset($CFG->forced_plugin_settings['backup']);
+
         // Grab the result.
         $result = $controller->get_results();
         if (!isset($result['backup_destination'])) {
@@ -233,7 +247,7 @@ class category_bin extends base_bin {
             $tempdir,
             $course->id,
             \backup::INTERACTIVE_NO,
-            \backup::MODE_GENERAL,
+            \backup::MODE_AUTOMATED,
             $user->id,
             \backup::TARGET_NEW_COURSE
         );
index d58d1a7..deec65c 100644 (file)
@@ -112,6 +112,16 @@ class course_bin extends base_bin {
             return;
         }
 
+        // As far as recycle bin is using MODE_AUTOMATED, it observes the backup_auto_storage
+        // setting (storing backups @ real location. For recycle bin we want to ensure that
+        // backup files are always stored in Moodle file area. In order to achieve that, we
+        // hack the setting here via $CFG->forced_plugin_settings, so it won't interfere other
+        // operations.  See MDL-65218 for more information.
+        // This hack will be removed once recycle bin switches to use its own backup mode, with
+        // own preferences and 100% appart from MODLE_AUTOMATED.
+        // TODO: Remove this as part of MDL-65228.
+        $CFG->forced_plugin_settings['backup'] = ['backup_auto_storage' => 0];
+
         // Backup the activity.
         $user = get_admin();
         $controller = new \backup_controller(
@@ -124,6 +134,10 @@ class course_bin extends base_bin {
         );
         $controller->execute_plan();
 
+        // We don't need the forced setting anymore, hence unsetting it.
+        // TODO: Remove this as part of MDL-65228.
+        unset($CFG->forced_plugin_settings['backup']);
+
         // Grab the result.
         $result = $controller->get_results();
         if (!isset($result['backup_destination'])) {
@@ -344,4 +358,4 @@ class course_bin extends base_bin {
         $context = \context_course::instance($this->_courseid);
         return has_capability('tool/recyclebin:deleteitems', $context);
     }
-}
\ No newline at end of file
+}
index bada3d6..0e24c31 100644 (file)
@@ -176,4 +176,116 @@ class tool_recyclebin_category_bin_tests extends advanced_testcase {
         $course = reset($courses);
         $this->assertEquals('Test course 2', $course->fullname);
     }
+
+    /**
+     * Provider for test_course_restore_with_userdata() and test_course_restore_without_userdata()
+     *
+     * Used to verify that recycle bin is immune to various settings. Provides plugin, name, value for
+     * direct usage with set_config()
+     */
+    public function recycle_bin_settings_provider() {
+        return [
+            'backup/backup_auto_storage moodle' => [[
+                (object)['plugin' => 'backup', 'name' => 'backup_auto_storage', 'value' => 0],
+            ]],
+
+            'backup/backup_auto_storage external' => [[
+                (object)['plugin' => 'backup', 'name' => 'backup_auto_storage', 'value' => 1],
+                (object)['plugin' => 'backup', 'name' => 'backup_auto_destination', 'value' => true],
+            ]],
+
+            'backup/backup_auto_storage mixed' => [[
+                (object)['plugin' => 'backup', 'name' => 'backup_auto_storage', 'value' => 2],
+                (object)['plugin' => 'backup', 'name' => 'backup_auto_destination', 'value' => true],
+            ]],
+        ];
+    }
+
+    /**
+     * Tests that user data is restored when course is restored.
+     *
+     * @dataProvider recycle_bin_settings_provider
+     * @param array $settings array of plugin, name, value stdClass().
+     */
+    public function test_course_restore_with_userdata($settings) {
+        global $DB;
+
+        // Force configuration changes from provider.
+        foreach ($settings as $setting) {
+            // Need to create a directory for backup_auto_destination.
+            if ($setting->plugin === 'backup' && $setting->name === 'backup_auto_destination' && $setting->value === true) {
+                $setting->value = make_request_directory();
+            }
+            set_config($setting->name, $setting->value, $setting->plugin);
+        }
+
+        // We want user data to be included for this test.
+        set_config('backup_auto_users', true, 'backup');
+
+        $student = $this->getDataGenerator()->create_and_enrol($this->course, 'student');
+
+        // Delete course.
+        delete_course($this->course, false);
+        $this->assertFalse($DB->record_exists('course', ['id' => $this->course->id]));
+
+        // Verify there is now a backup @ cat recycle bin file area.
+        $recyclebin = new \tool_recyclebin\category_bin($this->course->category);
+        $this->assertEquals(1, count($recyclebin->get_items()));
+
+        // Restore the recycle bin item.
+        $recyclebin->restore_item(current($recyclebin->get_items()));
+
+        // Get the new course.
+        $newcourse = $DB->get_record('course', ['shortname' => $this->course->shortname], '*', MUST_EXIST);
+
+        // Check that it was removed from the recycle bin.
+        $this->assertEquals(0, count($recyclebin->get_items()));
+
+        // Verify that student DOES continue enrolled.
+        $this->assertTrue(is_enrolled(context_course::instance($newcourse->id), $student->id));
+    }
+
+    /**
+     * Tests that user data is not restored when course is restored.
+     *
+     * @dataProvider recycle_bin_settings_provider
+     * @param array $settings array of plugin, name, value stdClass().
+     */
+    public function test_course_restore_without_userdata($settings) {
+        global $DB;
+
+        // Force configuration changes from provider.
+        foreach ($settings as $setting) {
+            // Need to create a directory for backup_auto_destination.
+            if ($setting->plugin === 'backup' && $setting->name === 'backup_auto_destination' && $setting->value === true) {
+                $setting->value = make_request_directory();
+            }
+            set_config($setting->name, $setting->value, $setting->plugin);
+        }
+
+        // We want user data to be included for this test.
+        set_config('backup_auto_users', false, 'backup');
+
+        $student = $this->getDataGenerator()->create_and_enrol($this->course, 'student');
+
+        // Delete course.
+        delete_course($this->course, false);
+        $this->assertFalse($DB->record_exists('course', ['id' => $this->course->id]));
+
+        // Verify there is now a backup @ cat recycle bin file area.
+        $recyclebin = new \tool_recyclebin\category_bin($this->course->category);
+        $this->assertEquals(1, count($recyclebin->get_items()));
+
+        // Restore the recycle bin item.
+        $recyclebin->restore_item(current($recyclebin->get_items()));
+
+        // Get the new course.
+        $newcourse = $DB->get_record('course', ['shortname' => $this->course->shortname], '*', MUST_EXIST);
+
+        // Check that it was removed from the recycle bin.
+        $this->assertEquals(0, count($recyclebin->get_items()));
+
+        // Verify that student DOES NOT continue enrolled.
+        $this->assertFalse(is_enrolled(context_course::instance($newcourse->id), $student->id));
+    }
 }
index 9d99326..44f9db6 100644 (file)
@@ -174,10 +174,46 @@ class tool_recyclebin_course_bin_tests extends advanced_testcase {
         $this->assertEquals($book->name, $deletedbook->name);
     }
 
+    /**
+     * Provider for test_coursemodule_restore_with_userdata() and test_coursemodule_restore_without_userdata()
+     *
+     * Used to verify that recycle bin is immune to various settings. Provides plugin, name, value for
+     * direct usage with set_config()
+     */
+    public function recycle_bin_settings_provider() {
+        return [
+            'backup/backup_auto_storage moodle' => [[
+                (object)['plugin' => 'backup', 'name' => 'backup_auto_storage', 'value' => 0],
+            ]],
+
+            'backup/backup_auto_storage external' => [[
+                (object)['plugin' => 'backup', 'name' => 'backup_auto_storage', 'value' => 1],
+                (object)['plugin' => 'backup', 'name' => 'backup_auto_destination', 'value' => true],
+            ]],
+
+            'backup/backup_auto_storage mixed' => [[
+                (object)['plugin' => 'backup', 'name' => 'backup_auto_storage', 'value' => 2],
+                (object)['plugin' => 'backup', 'name' => 'backup_auto_destination', 'value' => true],
+            ]],
+        ];
+    }
+
     /**
      * Tests that user data is restored when module is restored.
+     *
+     * @dataProvider recycle_bin_settings_provider
+     * @param array $settings array of plugin, name, value stdClass().
      */
-    public function test_coursemodule_restore_with_userdata() {
+    public function test_coursemodule_restore_with_userdata($settings) {
+        // Force configuration changes from provider.
+        foreach ($settings as $setting) {
+            // Need to create a directory for backup_auto_destination.
+            if ($setting->plugin === 'backup' && $setting->name === 'backup_auto_destination' && $setting->value === true) {
+                $setting->value = make_request_directory();
+            }
+            set_config($setting->name, $setting->value, $setting->plugin);
+        }
+
         $student = $this->getDataGenerator()->create_and_enrol($this->course, 'student');
         $this->setUser($student);
 
@@ -211,8 +247,20 @@ class tool_recyclebin_course_bin_tests extends advanced_testcase {
 
     /**
      * Tests that user data is not restored when module is restored.
+     *
+     * @dataProvider recycle_bin_settings_provider
+     * @param array $settings array of plugin, name, value stdClass().
      */
-    public function test_coursemodule_restore_without_userdata() {
+    public function test_coursemodule_restore_without_userdata($settings) {
+        // Force configuration changes from provider.
+        foreach ($settings as $setting) {
+            // Need to create a directory for backup_auto_destination.
+            if ($setting->plugin === 'backup' && $setting->name === 'backup_auto_destination' && $setting->value === true) {
+                $setting->value = make_request_directory();
+            }
+            set_config($setting->name, $setting->value, $setting->plugin);
+        }
+
         $student = $this->getDataGenerator()->create_and_enrol($this->course, 'student');
         $this->setUser($student);
 
index adf90ff..0759217 100644 (file)
@@ -4,7 +4,7 @@ require_once($CFG->libdir.'/adminlib.php');
 require_once($CFG->dirroot.'/message/lib.php');
 require_once('user_message_form.php');
 
-$msg     = optional_param('msg', '', PARAM_CLEANHTML);
+$msg     = optional_param('msg', '', PARAM_RAW);
 $confirm = optional_param('confirm', 0, PARAM_BOOL);
 
 admin_externalpage_setup('userbulk');
@@ -45,6 +45,7 @@ if ($msgform->is_cancelled()) {
     $options->para     = false;
     $options->newlines = true;
     $options->smiley   = false;
+    $options->trusted = trusttext_trusted(\context_system::instance());
 
     $msg = format_text($formdata->messagebody['text'], $formdata->messagebody['format'], $options);
 
index e9dcaae..97faf59 100644 (file)
@@ -29,10 +29,6 @@ defined('MOODLE_INTERNAL') || die();
 /**
  * Any element analysers can analyse.
  *
- * Analysers get_analysers method return all analysable elements in the site;
- * it is important that analysable elements implement lazy loading to avoid
- * big memory footprints. See \core_analytics\course example.
- *
  * @package   core_analytics
  * @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
diff --git a/analytics/classes/analysis.php b/analytics/classes/analysis.php
new file mode 100644 (file)
index 0000000..bcb621c
--- /dev/null
@@ -0,0 +1,894 @@
+<?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/>.
+
+/**
+ * Runs an analysis of the site.
+ *
+ * @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;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Runs an analysis of the site.
+ *
+ * @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 analysis {
+
+    /**
+     * @var \core_analytics\local\analyser\base
+     */
+    private $analyser;
+
+    /**
+     * @var bool Whether to calculate the target or not in this run.
+     */
+    private $includetarget;
+
+    /**
+     * @var \core_analytics\local\analysis\result
+     */
+    private $result;
+
+    /**
+     * @var \core\lock\lock
+     */
+    private $lock;
+
+    /**
+     * Constructor.
+     *
+     * @param \core_analytics\local\analyser\base   $analyser
+     * @param bool                                  $includetarget Whether to calculate the target or not.
+     * @param \core_analytics\local\analysis\result $result
+     */
+    public function __construct(\core_analytics\local\analyser\base $analyser, bool $includetarget,
+            \core_analytics\local\analysis\result $result) {
+        $this->analyser = $analyser;
+        $this->includetarget = $includetarget;
+        $this->result = $result;
+    }
+
+    /**
+     * Runs the analysis.
+     *
+     * @return null
+     */
+    public function run() {
+
+        $options = $this->analyser->get_options();
+
+        // Time limit control.
+        $modeltimelimit = intval(get_config('analytics', 'modeltimelimit'));
+
+        $filesbytimesplitting = array();
+
+        $alreadyprocessedanalysables = $this->get_processed_analysables();
+
+        if ($this->includetarget) {
+            $action = 'training';
+        } else {
+            $action = 'prediction';
+        }
+        $analysables = $this->analyser->get_analysables_iterator($action);
+
+        $inittime = microtime(true);
+        foreach ($analysables as $analysable) {
+            $processed = false;
+
+            if (!$analysable) {
+                continue;
+            }
+
+            $analysableresults = $this->process_analysable($analysable);
+            if ($analysableresults) {
+                $processed = $this->result->add_analysable_results($analysableresults);
+                if (!$processed) {
+                    $errors = array();
+                    foreach ($analysableresults as $timesplittingid => $result) {
+                        $str = '';
+                        if (count($analysableresults) > 1) {
+                            $str .= $timesplittingid . ': ';
+                        }
+                        $str .= $result->message;
+                        $errors[] = $str;
+                    }
+
+                    $a = new \stdClass();
+                    $a->analysableid = $analysable->get_name();
+                    $a->errors = implode(', ', $errors);
+                    $this->analyser->add_log(get_string('analysablenotused', 'analytics', $a));
+                }
+            }
+
+            // Updated regardless of how well the analysis went.
+            if ($this->analyser->get_target()->always_update_analysis_time() || $processed) {
+                $this->update_analysable_analysed_time($alreadyprocessedanalysables, $analysable->get_id());
+            }
+
+            // Apply time limit.
+            if (!$options['evaluation']) {
+                $timespent = microtime(true) - $inittime;
+                if ($modeltimelimit <= $timespent) {
+                    break;
+                }
+            }
+        }
+    }
+
+    /**
+     * Get analysables that have been already processed.
+     *
+     * @return \stdClass[]
+     */
+    protected function get_processed_analysables(): array {
+        global $DB;
+
+        $params = array('modelid' => $this->analyser->get_modelid());
+        $params['action'] = ($this->includetarget) ? 'training' : 'prediction';
+        $select = 'modelid = :modelid and action = :action';
+
+        // Weird select fields ordering for performance (analysableid key matching, analysableid is also unique by modelid).
+        return $DB->get_records_select('analytics_used_analysables', $select,
+            $params, 'timeanalysed DESC', 'analysableid, modelid, action, timeanalysed, id AS primarykey');
+    }
+
+    /**
+     * Processes an analysable
+     *
+     * This method returns the general analysable status, an array of files by time splitting method and
+     * an error message if there is any problem.
+     *
+     * @param \core_analytics\analysable $analysable
+     * @return \stdClass[] Results objects by time splitting method
+     */
+    public function process_analysable(\core_analytics\analysable $analysable): array {
+
+        // Target instances scope is per-analysable (it can't be lower as calculations run once per
+        // analysable, not time splitting method nor time range).
+        $target = call_user_func(array($this->analyser->get_target(), 'instance'));
+
+        // We need to check that the analysable is valid for the target even if we don't include targets
+        // as we still need to discard invalid analysables for the target.
+        $isvalidresult = $target->is_valid_analysable($analysable, $this->includetarget);
+        if ($isvalidresult !== true) {
+            $a = new \stdClass();
+            $a->analysableid = $analysable->get_name();
+            $a->result = $isvalidresult;
+            $this->analyser->add_log(get_string('analysablenotvalidfortarget', 'analytics', $a));
+            return array();
+        }
+
+        // Process all provided time splitting methods.
+        $results = array();
+        foreach ($this->analyser->get_timesplittings() as $timesplitting) {
+
+            $cachedresult = $this->result->retrieve_cached_result($timesplitting, $analysable);
+            if ($cachedresult) {
+                $result = new \stdClass();
+                $result->result = $cachedresult;
+                $results[$timesplitting->get_id()] = $result;
+                continue;
+            }
+
+            $results[$timesplitting->get_id()] = $this->process_time_splitting($timesplitting, $analysable, $target);
+        }
+
+        return $results;
+    }
+
+    /**
+     * Processes the analysable samples using the provided time splitting method.
+     *
+     * @param \core_analytics\local\time_splitting\base $timesplitting
+     * @param \core_analytics\analysable $analysable
+     * @param \core_analytics\local\target\base $target
+     * @return \stdClass Results object.
+     */
+    protected function process_time_splitting(\core_analytics\local\time_splitting\base $timesplitting,
+            \core_analytics\analysable $analysable, \core_analytics\local\target\base $target): \stdClass {
+
+        $options = $this->analyser->get_options();
+
+        $result = new \stdClass();
+
+        if (!$timesplitting->is_valid_analysable($analysable)) {
+            $result->status = \core_analytics\model::ANALYSABLE_REJECTED_TIME_SPLITTING_METHOD;
+            $result->message = get_string('invalidanalysablefortimesplitting', 'analytics',
+                $timesplitting->get_name());
+            return $result;
+        }
+        $timesplitting->set_analysable($analysable);
+
+        if (CLI_SCRIPT && !PHPUNIT_TEST) {
+            mtrace('Analysing id "' . $analysable->get_id() . '" with "' . $timesplitting->get_name() .
+                '" time splitting method...');
+        }
+
+        // What is a sample is defined by the analyser, it can be an enrolment, a course, a user, a question
+        // attempt... it is on what we will base indicators calculations.
+        list($sampleids, $samplesdata) = $this->analyser->get_all_samples($analysable);
+
+        if (count($sampleids) === 0) {
+            $result->status = \core_analytics\model::ANALYSABLE_REJECTED_TIME_SPLITTING_METHOD;
+            $result->message = get_string('nodata', 'analytics');
+            return $result;
+        }
+
+        if ($this->includetarget) {
+            // All ranges are used when we are calculating data for training.
+            $ranges = $timesplitting->get_training_ranges();
+        } else {
+            // The latest range that has not yet been used for prediction (it depends on the time range where we are right now).
+            $ranges = $timesplitting->get_most_recent_prediction_range();
+        }
+
+        // There is no need to keep track of the evaluated samples and ranges as we always evaluate the whole dataset.
+        if ($options['evaluation'] === false) {
+
+            if (empty($ranges)) {
+                $result->status = \core_analytics\model::ANALYSABLE_REJECTED_TIME_SPLITTING_METHOD;
+                $result->message = get_string('noranges', 'analytics');
+                return $result;
+            }
+
+            // We skip all samples that are already part of a training dataset, even if they have not been used for prediction.
+            if (!$target::based_on_assumptions()) {
+                // Targets based on assumptions can not be trained.
+                $this->filter_out_train_samples($sampleids, $timesplitting);
+            }
+
+            if (count($sampleids) === 0) {
+                $result->status = \core_analytics\model::ANALYSABLE_REJECTED_TIME_SPLITTING_METHOD;
+                $result->message = get_string('nonewdata', 'analytics');
+                return $result;
+            }
+
+            // Only when processing data for predictions.
+            if (!$this->includetarget) {
+                // We also filter out samples and ranges that have already been used for predictions.
+                $predictsamplesrecord = $this->filter_out_prediction_samples_and_ranges($sampleids, $ranges, $timesplitting);
+            }
+
+            if (count($sampleids) === 0) {
+                $result->status = \core_analytics\model::ANALYSABLE_REJECTED_TIME_SPLITTING_METHOD;
+                $result->message = get_string('nonewdata', 'analytics');
+                return $result;
+            }
+
+            if (count($ranges) === 0) {
+                $result->status = \core_analytics\model::ANALYSABLE_REJECTED_TIME_SPLITTING_METHOD;
+                $result->message = get_string('nonewranges', 'analytics');
+                return $result;
+            }
+        }
+
+        // Flag the model + analysable + timesplitting as being analysed (prevent concurrent executions).
+        if (!$this->init_analysable_analysis($timesplitting->get_id(), $analysable->get_id())) {
+            // If this model + analysable + timesplitting combination is being analysed we skip this process.
+            $result->status = \core_analytics\model::NO_DATASET;
+            $result->message = get_string('analysisinprogress', 'analytics');
+            return $result;
+        }
+
+        // Remove samples the target consider invalid.
+        try {
+            $target->add_sample_data($samplesdata);
+            $target->filter_out_invalid_samples($sampleids, $analysable, $this->includetarget);
+        } catch (\Throwable $e) {
+            $this->finish_analysable_analysis();
+            throw $e;
+        }
+
+        if (!$sampleids) {
+            $result->status = \core_analytics\model::NO_DATASET;
+            $result->message = get_string('novalidsamples', 'analytics');
+            $this->finish_analysable_analysis();
+            return $result;
+        }
+
+        try {
+            $indicators = $this->analyser->get_indicators();
+            foreach ($indicators as $key => $indicator) {
+                // The analyser attaches the main entities the sample depends on and are provided to the
+                // indicator to calculate the sample.
+                $indicators[$key]->add_sample_data($samplesdata);
+            }
+
+            // Here we start the memory intensive process that will last until $data var is
+            // unset (until the method is finished basically).
+            $data = $this->calculate($timesplitting, $sampleids, $ranges, $target);
+        } catch (\Throwable $e) {
+            $this->finish_analysable_analysis();
+            throw $e;
+        }
+
+        if (!$data) {
+            $result->status = \core_analytics\model::ANALYSABLE_REJECTED_TIME_SPLITTING_METHOD;
+            $result->message = get_string('novaliddata', 'analytics');
+            $this->finish_analysable_analysis();
+            return $result;
+        }
+
+        try {
+            // No need to keep track of analysed stuff when evaluating.
+            if ($options['evaluation'] === false) {
+                // Save the samples that have been already analysed so they are not analysed again in future.
+
+                if ($this->includetarget) {
+                    $this->save_train_samples($sampleids, $timesplitting);
+                } else {
+                    // The variable $predictsamplesrecord will always be set as filter_out_prediction_samples_and_ranges
+                    // will always be called before it (no evaluation mode and no includetarget).
+                    $this->save_prediction_samples($sampleids, $ranges, $timesplitting, $predictsamplesrecord);
+                }
+            }
+
+            // We need to pass all the analysis data.
+            $formattedresult = $this->result->format_result($data, $target, $timesplitting, $analysable);
+
+        } catch (\Throwable $e) {
+            $this->finish_analysable_analysis();
+            throw $e;
+        }
+
+        if (!$formattedresult) {
+            $this->finish_analysable_analysis();
+            throw new \moodle_exception('errorcannotwritedataset', 'analytics');
+        }
+
+        $result->status = \core_analytics\model::OK;
+        $result->message = get_string('successfullyanalysed', 'analytics');
+        $result->result = $formattedresult;
+
+        // Flag the model + analysable + timesplitting as analysed.
+        $this->finish_analysable_analysis();
+
+        return $result;
+    }
+
+    /**
+     * Calculates indicators and targets.
+     *
+     * @param \core_analytics\local\time_splitting\base $timesplitting
+     * @param array $sampleids
+     * @param array $ranges
+     * @param \core_analytics\local\target\base $target
+     * @return array|null
+     */
+    public function calculate(\core_analytics\local\time_splitting\base $timesplitting, array &$sampleids,
+            array $ranges, \core_analytics\local\target\base $target): ?array {
+
+        $calculatedtarget = null;
+        if ($this->includetarget) {
+            // We first calculate the target because analysable data may still be invalid or none
+            // of the analysable samples may be valid.
+            $calculatedtarget = $target->calculate($sampleids, $timesplitting->get_analysable());
+
+            // We remove samples we can not calculate their target.
+            $sampleids = array_filter($sampleids, function($sampleid) use ($calculatedtarget) {
+                if (is_null($calculatedtarget[$sampleid])) {
+                    return false;
+                }
+                return true;
+            });
+        }
+
+        // No need to continue calculating if the target couldn't be calculated for any sample.
+        if (empty($sampleids)) {
+            return null;
+        }
+
+        $dataset = $this->calculate_indicators($timesplitting, $sampleids, $ranges);
+
+        if (empty($dataset)) {
+            return null;
+        }
+
+        // Now that we have the indicators in place we can add the time range indicators (and target if provided) to each of them.
+        $this->fill_dataset($timesplitting, $dataset, $calculatedtarget);
+
+        $this->add_context_metadata($timesplitting, $dataset, $target);
+
+        if (!PHPUNIT_TEST && CLI_SCRIPT) {
+            echo PHP_EOL;
+        }
+
+        return $dataset;
+    }
+
+    /**
+     * Calculates indicators.
+     *
+     * @param \core_analytics\local\time_splitting\base $timesplitting
+     * @param array $sampleids
+     * @param array $ranges
+     * @return array
+     */
+    protected function calculate_indicators(\core_analytics\local\time_splitting\base $timesplitting, array $sampleids,
+            array $ranges): array {
+        global $DB;
+
+        $options = $this->analyser->get_options();
+
+        $dataset = array();
+
+        // Faster to run 1 db query per range.
+        $existingcalculations = array();
+        if ($timesplitting->cache_indicator_calculations()) {
+            foreach ($ranges as $rangeindex => $range) {
+                // Load existing calculations.
+                $existingcalculations[$rangeindex] = \core_analytics\manager::get_indicator_calculations(
+                    $timesplitting->get_analysable(), $range['start'], $range['end'], $this->analyser->get_samples_origin());
+            }
+        }
+
+        // Here we store samples which calculations are not all null.
+        $notnulls = array();
+
+        // Fill the dataset samples with indicators data.
+        $newcalculations = array();
+        foreach ($this->analyser->get_indicators() as $indicator) {
+
+            // Hook to allow indicators to store analysable-dependant data.
+            $indicator->fill_per_analysable_caches($timesplitting->get_analysable());
+
+            // Per-range calculations.
+            foreach ($ranges as $rangeindex => $range) {
+
+                // Indicator instances are per-range.
+                $rangeindicator = clone $indicator;
+
+                $prevcalculations = array();
+                if (!empty($existingcalculations[$rangeindex][$rangeindicator->get_id()])) {
+                    $prevcalculations = $existingcalculations[$rangeindex][$rangeindicator->get_id()];
+                }
+
+                // Calculate the indicator for each sample in this time range.
+                list($samplesfeatures, $newindicatorcalculations, $indicatornotnulls) = $rangeindicator->calculate($sampleids,
+                    $this->analyser->get_samples_origin(), $range['start'], $range['end'], $prevcalculations);
+
+                // Copy the features data to the dataset.
+                foreach ($samplesfeatures as $analysersampleid => $features) {
+
+                    $uniquesampleid = $timesplitting->append_rangeindex($analysersampleid, $rangeindex);
+
+                    if (!isset($notnulls[$uniquesampleid]) && !empty($indicatornotnulls[$analysersampleid])) {
+                        $notnulls[$uniquesampleid] = $uniquesampleid;
+                    }
+
+                    // Init the sample if it is still empty.
+                    if (!isset($dataset[$uniquesampleid])) {
+                        $dataset[$uniquesampleid] = array();
+                    }
+
+                    // Append the features indicator features at the end of the sample.
+                    $dataset[$uniquesampleid] = array_merge($dataset[$uniquesampleid], $features);
+                }
+
+                if (!$options['evaluation'] && $timesplitting->cache_indicator_calculations()) {
+                    $timecreated = time();
+                    foreach ($newindicatorcalculations as $sampleid => $calculatedvalue) {
+                        // Prepare the new calculations to be stored into DB.
+
+                        $indcalc = new \stdClass();
+                        $indcalc->contextid = $timesplitting->get_analysable()->get_context()->id;
+                        $indcalc->starttime = $range['start'];
+                        $indcalc->endtime = $range['end'];
+                        $indcalc->sampleid = $sampleid;
+                        $indcalc->sampleorigin = $this->analyser->get_samples_origin();
+                        $indcalc->indicator = $rangeindicator->get_id();
+                        $indcalc->value = $calculatedvalue;
+                        $indcalc->timecreated = $timecreated;
+                        $newcalculations[] = $indcalc;
+                    }
+                }
+            }
+
+            if (!$options['evaluation'] && $timesplitting->cache_indicator_calculations()) {
+                $batchsize = self::get_insert_batch_size();
+                if (count($newcalculations) > $batchsize) {
+                    // We don't want newcalculations array to grow too much as we already keep the
+                    // system memory busy storing $dataset contents.
+
+                    // Insert from the beginning.
+                    $remaining = array_splice($newcalculations, $batchsize);
+
+                    // Sorry mssql and oracle, this will be slow.
+                    $DB->insert_records('analytics_indicator_calc', $newcalculations);
+                    $newcalculations = $remaining;
+                }
+            }
+        }
+
+        if (!$options['evaluation'] && $timesplitting->cache_indicator_calculations() && $newcalculations) {
+            // Insert the remaining records.
+            $DB->insert_records('analytics_indicator_calc', $newcalculations);
+        }
+
+        // Delete rows where all calculations are null.
+        // We still store the indicator calculation and we still store the sample id as
+        // processed so we don't have to process this sample again, but we exclude it
+        // from the dataset because it is not useful.
+        $nulls = array_diff_key($dataset, $notnulls);
+        foreach ($nulls as $uniqueid => $ignoredvalues) {
+            unset($dataset[$uniqueid]);
+        }
+
+        return $dataset;
+    }
+
+    /**
+     * Adds time range indicators and the target to each sample.
+     *
+     * This will identify the sample as belonging to a specific range.
+     *
+     * @param \core_analytics\local\time_splitting\base $timesplitting
+     * @param array $dataset
+     * @param array|null $calculatedtarget
+     * @return null
+     */
+    protected function fill_dataset(\core_analytics\local\time_splitting\base $timesplitting,
+            array &$dataset, ?array $calculatedtarget = null) {
+
+        $nranges = count($timesplitting->get_distinct_ranges());
+
+        foreach ($dataset as $uniquesampleid => $unmodified) {
+
+            list($analysersampleid, $rangeindex) = $timesplitting->infer_sample_info($uniquesampleid);
+
+            // No need to add range features if this time splitting method only defines one time range.
+            if ($nranges > 1) {
+
+                // 1 column for each range.
+                $timeindicators = array_fill(0, $nranges, 0);
+
+                $timeindicators[$rangeindex] = 1;
+
+                $dataset[$uniquesampleid] = array_merge($timeindicators, $dataset[$uniquesampleid]);
+            }
+
+            if ($calculatedtarget) {
+                // Add this sampleid's calculated target and the end.
+                $dataset[$uniquesampleid][] = $calculatedtarget[$analysersampleid];
+
+            } else {
+                // Add this sampleid, it will be used to identify the prediction that comes back from
+                // the predictions processor.
+                array_unshift($dataset[$uniquesampleid], $uniquesampleid);
+            }
+        }
+    }
+
+    /**
+     * Updates the analysable analysis time.
+     *
+     * @param array $processedanalysables
+     * @param int $analysableid
+     * @return null
+     */
+    protected function update_analysable_analysed_time(array $processedanalysables, int $analysableid) {
+        global $DB;
+
+        if (!empty($processedanalysables[$analysableid])) {
+            $obj = $processedanalysables[$analysableid];
+
+            $obj->id = $obj->primarykey;
+            unset($obj->primarykey);
+
+            $obj->timeanalysed = time();
+            $DB->update_record('analytics_used_analysables', $obj);
+
+        } else {
+
+            $obj = new \stdClass();
+            $obj->modelid = $this->analyser->get_modelid();
+            $obj->action = ($this->includetarget) ? 'training' : 'prediction';
+            $obj->analysableid = $analysableid;
+            $obj->timeanalysed = time();
+
+            $DB->insert_record('analytics_used_analysables', $obj);
+        }
+    }
+
+    /**
+     * Adds dataset context info.
+     *
+     * The final dataset document will look like this:
+     * ----------------------------------------------------
+     * metadata1,metadata2,metadata3,.....
+     * value1, value2, value3,.....
+     *
+     * header1,header2,header3,header4,.....
+     * stud1value1,stud1value2,stud1value3,stud1value4,.....
+     * stud2value1,stud2value2,stud2value3,stud2value4,.....
+     * .....
+     * ----------------------------------------------------
+     *
+     * @param \core_analytics\local\time_splitting\base $timesplitting
+     * @param array $dataset
+     * @param \core_analytics\local\target\base $target
+     * @return null
+     */
+    protected function add_context_metadata(\core_analytics\local\time_splitting\base $timesplitting, array &$dataset,
+            \core_analytics\local\target\base $target) {
+        $headers = $this->get_headers($timesplitting, $target);
+
+        // This will also reset samples' dataset keys.
+        array_unshift($dataset, $headers);
+    }
+
+    /**
+     * Returns the headers for the csv file based on the indicators and the target.
+     *
+     * @param \core_analytics\local\time_splitting\base $timesplitting
+     * @param \core_analytics\local\target\base $target
+     * @return string[]
+     */
+    public function get_headers(\core_analytics\local\time_splitting\base $timesplitting,
+            \core_analytics\local\target\base $target): array {
+        // 3rd column will contain the indicator ids.
+        $headers = array();
+
+        if (!$this->includetarget) {
+            // The first column is the sampleid.
+            $headers[] = 'sampleid';
+        }
+
+        // We always have 1 column for each time splitting method range, it does not depend on how
+        // many ranges we calculated.
+        $ranges = $timesplitting->get_distinct_ranges();
+        if (count($ranges) > 1) {
+            foreach ($ranges as $rangeindex) {
+                $headers[] = 'range/' . $rangeindex;
+            }
+        }
+
+        // Model indicators.
+        foreach ($this->analyser->get_indicators() as $indicator) {
+            $headers = array_merge($headers, $indicator::get_feature_headers());
+        }
+
+        // The target as well.
+        if ($this->includetarget) {
+            $headers[] = $target->get_id();
+        }
+
+        return $headers;
+    }
+
+    /**
+     * Filters out samples that have already been used for training.
+     *
+     * @param int[] $sampleids
+     * @param \core_analytics\local\time_splitting\base $timesplitting
+     * @return  null
+     */
+    protected function filter_out_train_samples(array &$sampleids, \core_analytics\local\time_splitting\base $timesplitting) {
+        global $DB;
+
+        $params = array('modelid' => $this->analyser->get_modelid(), 'analysableid' => $timesplitting->get_analysable()->get_id(),
+            'timesplitting' => $timesplitting->get_id());
+
+        $trainingsamples = $DB->get_records('analytics_train_samples', $params);
+
+        // Skip each file trained samples.
+        foreach ($trainingsamples as $trainingfile) {
+
+            $usedsamples = json_decode($trainingfile->sampleids, true);
+
+            if (!empty($usedsamples)) {
+                // Reset $sampleids to $sampleids minus this file's $usedsamples.
+                $sampleids = array_diff_key($sampleids, $usedsamples);
+            }
+        }
+    }
+
+    /**
+     * Filters out samples that have already been used for prediction.
+     *
+     * @param int[] $sampleids
+     * @param array $ranges
+     * @param \core_analytics\local\time_splitting\base $timesplitting
+     * @return  \stdClass|null The analytics_predict_samples record or null
+     */
+    protected function filter_out_prediction_samples_and_ranges(array &$sampleids, array &$ranges,
+            \core_analytics\local\time_splitting\base $timesplitting) {
+
+        if (count($ranges) > 1) {
+            throw new \coding_exception('$ranges argument should only contain one range');
+        }
+
+        $rangeindex = key($ranges);
+        $predictedrange = $this->get_predict_samples_record($timesplitting, $rangeindex);
+
+        if (!$predictedrange) {
+            // Nothing to filter out.
+            return null;
+        }
+
+        $predictedrange->sampleids = json_decode($predictedrange->sampleids, true);
+        $missingsamples = array_diff_key($sampleids, $predictedrange->sampleids);
+        if (count($missingsamples) === 0) {
+            // All samples already calculated.
+            unset($ranges[$rangeindex]);
+            return null;
+        }
+
+        // Replace the list of samples by the one excluding samples that already got predictions at this range.
+        $sampleids = $missingsamples;
+
+        return $predictedrange;
+    }
+
+    /**
+     * Returns a predict samples record.
+     *
+     * @param  \core_analytics\local\time_splitting\base $timesplitting
+     * @param  int                                       $rangeindex
+     * @return \stdClass|false
+     */
+    private function get_predict_samples_record(\core_analytics\local\time_splitting\base $timesplitting, int $rangeindex) {
+        global $DB;
+
+        $params = array('modelid' => $this->analyser->get_modelid(), 'analysableid' => $timesplitting->get_analysable()->get_id(),
+            'timesplitting' => $timesplitting->get_id(), 'rangeindex' => $rangeindex);
+        $predictedrange = $DB->get_record('analytics_predict_samples', $params);
+
+        return $predictedrange;
+    }
+
+    /**
+     * Saves samples that have just been used for training.
+     *
+     * @param int[] $sampleids
+     * @param \core_analytics\local\time_splitting\base $timesplitting
+     * @return null
+     */
+    protected function save_train_samples(array $sampleids, \core_analytics\local\time_splitting\base $timesplitting) {
+        global $DB;
+
+        $trainingsamples = new \stdClass();
+        $trainingsamples->modelid = $this->analyser->get_modelid();
+        $trainingsamples->analysableid = $timesplitting->get_analysable()->get_id();
+        $trainingsamples->timesplitting = $timesplitting->get_id();
+
+        $trainingsamples->sampleids = json_encode($sampleids);
+        $trainingsamples->timecreated = time();
+
+        $DB->insert_record('analytics_train_samples', $trainingsamples);
+    }
+
+    /**
+     * Saves samples that have just been used for prediction.
+     *
+     * @param int[] $sampleids
+     * @param array $ranges
+     * @param \core_analytics\local\time_splitting\base $timesplitting
+     * @param \stdClass|null $predictsamplesrecord The existing record or null if there is no record yet.
+     * @return null
+     */
+    protected function save_prediction_samples(array $sampleids, array $ranges,
+            \core_analytics\local\time_splitting\base $timesplitting, ?\stdClass $predictsamplesrecord = null) {
+        global $DB;
+
+        if (count($ranges) > 1) {
+            throw new \coding_exception('$ranges argument should only contain one range');
+        }
+
+        $rangeindex = key($ranges);
+
+        if ($predictsamplesrecord) {
+            // Append the new samples used for prediction.
+            $predictsamplesrecord->sampleids = json_encode($predictsamplesrecord->sampleids + $sampleids);
+            $predictsamplesrecord->timemodified = time();
+            $DB->update_record('analytics_predict_samples', $predictsamplesrecord);
+        } else {
+            $predictsamplesrecord = (object)[
+                'modelid' => $this->analyser->get_modelid(),
+                'analysableid' => $timesplitting->get_analysable()->get_id(),
+                'timesplitting' => $timesplitting->get_id(), 'rangeindex' => $rangeindex
+            ];
+            $predictsamplesrecord->sampleids = json_encode($sampleids);
+            $predictsamplesrecord->timecreated = time();
+            $predictsamplesrecord->timemodified = $predictsamplesrecord->timecreated;
+            $DB->insert_record('analytics_predict_samples', $predictsamplesrecord);
+        }
+    }
+
+    /**
+     * Flags the analysable element as in-analysis and stores a lock for it.
+     *
+     * @param  string $timesplittingid
+     * @param  int    $analysableid
+     * @return bool Success or not
+     */
+    private function init_analysable_analysis(string $timesplittingid, int $analysableid) {
+
+        // Do not include $this->includetarget as we don't want the same analysable to be analysed for training
+        // and prediction at the same time.
+        $lockkey = 'modelid:' . $this->analyser->get_modelid() . '-analysableid:' . $analysableid .
+            '-timesplitting:' . self::clean_time_splitting_id($timesplittingid);
+
+        // Large timeout as processes may be quite long.
+        $lockfactory = \core\lock\lock_config::get_lock_factory('core_analytics');
+
+        // If it is not ready in 10 secs skip this model + analysable + timesplittingmethod combination
+        // it will attempt it again during next cron run.
+        if (!$this->lock = $lockfactory->get_lock($lockkey, 10)) {
+            return false;
+        }
+        return true;
+    }
+
+
+    /**
+     * Remove all possibly problematic chars from the time splitting method id (id = its full class name).
+     *
+     * @param string $timesplittingid
+     * @return string
+     */
+    public static function clean_time_splitting_id($timesplittingid) {
+        $timesplittingid = str_replace('\\', '-', $timesplittingid);
+        return clean_param($timesplittingid, PARAM_ALPHANUMEXT);
+    }
+
+    /**
+     * Mark the currently analysed analysable+timesplitting as analysed.
+     *
+     * @return null
+     */
+    private function finish_analysable_analysis() {
+        $this->lock->release();
+    }
+
+    /**
+     * Returns the batch size used for insert_records.
+     *
+     * This method tries to find the best batch size without getting
+     * into dml internals. Maximum 1000 records to save memory.
+     *
+     * @return int
+     */
+    private static function get_insert_batch_size(): int {
+        global $DB;
+
+        $dbconfig = $DB->export_dbconfig();
+
+        // 500 is pgsql default so using 1000 is fine, no other db driver uses a hardcoded value.
+        if (empty($dbconfig) || empty($dbconfig->dboptions) || empty($dbconfig->dboptions['bulkinsertsize'])) {
+            return 1000;
+        }
+
+        $bulkinsert = $dbconfig->dboptions['bulkinsertsize'];
+        if ($bulkinsert < 1000) {
+            return $bulkinsert;
+        }
+
+        while ($bulkinsert > 1000) {
+            $bulkinsert = round($bulkinsert / 2, 0);
+        }
+
+        return (int)$bulkinsert;
+    }
+}
index 919f36b..6e372cf 100644 (file)
@@ -132,12 +132,11 @@ class course implements \core_analytics\analysable {
      * Use self::instance() instead to get cached copies of the course. Instances obtained
      * through this constructor will not be cached.
      *
-     * Lazy load of course data, students and teachers.
-     *
-     * @param int|\stdClass $course Course id
+     * @param int|\stdClass $course Course id or mdl_course record
+     * @param \context|null $context
      * @return void
      */
-    public function __construct($course) {
+    public function __construct($course, ?\context $context = null) {
 
         if (is_scalar($course)) {
             $this->course = new \stdClass();
@@ -145,6 +144,10 @@ class course implements \core_analytics\analysable {
         } else {
             $this->course = $course;
         }
+
+        if (!is_null($context)) {
+            $this->coursecontext = $context;
+        }
     }
 
     /**
@@ -153,9 +156,10 @@ class course implements \core_analytics\analysable {
      * Lazy load of course data, students and teachers.
      *
      * @param int|\stdClass $course Course object or course id
+     * @param \context|null $context
      * @return \core_analytics\course
      */
-    public static function instance($course) {
+    public static function instance($course, ?\context $context = null) {
 
         $courseid = $course;
         if (!is_scalar($courseid)) {
@@ -166,7 +170,7 @@ class course implements \core_analytics\analysable {
             return self::$cachedinstance;
         }
 
-        $cachedinstance = new \core_analytics\course($course);
+        $cachedinstance = new \core_analytics\course($course, $context);
         self::$cachedinstance = $cachedinstance;
         self::$cachedid = (int)$courseid;
         return self::$cachedinstance;
index 00b571a..811bf7c 100644 (file)
@@ -109,31 +109,8 @@ class dataset_manager {
         $this->modelid = $modelid;
         $this->analysableid = $analysableid;
         $this->timesplittingid = $timesplittingid;
-        $this->evaluation = $evaluation;
         $this->filearea = $filearea;
-    }
-
-    /**
-     * Mark the analysable as being analysed.
-     *
-     * @return bool Could we get the lock or not.
-     */
-    public function init_process() {
-
-        // Do not include $this->includetarget as we don't want the same analysable to be analysed for training
-        // and prediction at the same time.
-        $lockkey = 'modelid:' . $this->modelid . '-analysableid:' . $this->analysableid .
-            '-timesplitting:' . self::clean_time_splitting_id($this->timesplittingid);
-
-        // Large timeout as processes may be quite long.
-        $lockfactory = \core\lock\lock_config::get_lock_factory('core_analytics');
-
-        // If it is not ready in 10 secs skip this model + analysable + timesplittingmethod combination
-        // it will attempt it again during next cron run.
-        if (!$this->lock = $lockfactory->get_lock($lockkey, 10)) {
-            return false;
-        }
-        return true;
+        $this->evaluation = $evaluation;
     }
 
     /**
@@ -152,7 +129,8 @@ class dataset_manager {
             'filearea' => $this->filearea,
             'itemid' => $this->modelid,
             'contextid' => \context_system::instance()->id,
-            'filepath' => '/analysable/' . $this->analysableid . '/' . self::clean_time_splitting_id($this->timesplittingid) . '/',
+            'filepath' => '/analysable/' . $this->analysableid . '/' .
+                \core_analytics\analysis::clean_time_splitting_id($this->timesplittingid) . '/',
             'filename' => self::get_filename($this->evaluation)
         ];
 
@@ -167,8 +145,7 @@ class dataset_manager {
         $filepath = make_request_directory() . DIRECTORY_SEPARATOR . $filerecord['filename'];
         $fh = fopen($filepath, 'w+');
         if (!$fh) {
-            $this->close_process();
-            throw new \moodle_exception('errorcannotwritedataset', 'analytics', '', $filepath);
+            return false;
         }
         foreach ($data as $line) {
             fputcsv($fh, $line);
@@ -178,15 +155,6 @@ class dataset_manager {
         return $fs->create_file_from_pathname($filerecord, $filepath);
     }
 
-    /**
-     * Mark as analysed.
-     *
-     * @return void
-     */
-    public function close_process() {
-        $this->lock->release();
-    }
-
     /**
      * Returns the previous evaluation file.
      *
@@ -200,8 +168,9 @@ class dataset_manager {
     public static function get_previous_evaluation_file($modelid, $timesplittingid) {
         $fs = get_file_storage();
         // Evaluation data is always labelled.
+        $filepath = '/timesplitting/' . \core_analytics\analysis::clean_time_splitting_id($timesplittingid) . '/';
         return $fs->get_file(\context_system::instance()->id, 'analytics', self::LABELLED_FILEAREA, $modelid,
-            '/timesplitting/' . self::clean_time_splitting_id($timesplittingid) . '/', self::EVALUATION_FILENAME);
+            $filepath, self::EVALUATION_FILENAME);
     }
 
     /**
@@ -233,7 +202,7 @@ class dataset_manager {
         $filesbytimesplitting = array();
         foreach ($timesplittingids as $timesplittingid) {
 
-            $filepath = '/timesplitting/' . self::clean_time_splitting_id($timesplittingid) . '/';
+            $filepath = '/timesplitting/' . \core_analytics\analysis::clean_time_splitting_id($timesplittingid) . '/';
             $files = $fs->get_directory_files(\context_system::instance()->id, 'analytics', $filearea, $modelid, $filepath);
             foreach ($files as $file) {
 
@@ -291,7 +260,8 @@ class dataset_manager {
         // Always evaluation.csv and labelled as it is an evaluation file.
         $filearea = self::LABELLED_FILEAREA;
         $filename = self::get_filename(true);
-        $filepath = '/analysable/' . $analysableid . '/' . self::clean_time_splitting_id($timesplittingid) . '/';
+        $filepath = '/analysable/' . $analysableid . '/' .
+            \core_analytics\analysis::clean_time_splitting_id($timesplittingid) . '/';
         return $fs->get_file(\context_system::instance()->id, 'analytics', $filearea, $modelid, $filepath, $filename);
     }
 
@@ -372,7 +342,7 @@ class dataset_manager {
             'filearea' => $filearea,
             'itemid' => $modelid,
             'contextid' => \context_system::instance()->id,
-            'filepath' => '/timesplitting/' . self::clean_time_splitting_id($timesplittingid) . '/',
+            'filepath' => '/timesplitting/' . \core_analytics\analysis::clean_time_splitting_id($timesplittingid) . '/',
             'filename' => self::get_filename($evaluation)
         ];
 
@@ -393,7 +363,7 @@ class dataset_manager {
         $fs = get_file_storage();
 
         $contextid = \context_system::instance()->id;
-        $filepath = '/timesplitting/' . self::clean_time_splitting_id($timesplittingid) . '/';
+        $filepath = '/timesplitting/' . \core_analytics\analysis::clean_time_splitting_id($timesplittingid) . '/';
 
         $files = $fs->get_directory_files($contextid, 'analytics', self::LABELLED_FILEAREA, $modelid,
             $filepath, true, false);
@@ -467,17 +437,6 @@ class dataset_manager {
         return $fs->delete_area_files(\context_system::instance()->id, 'analytics', false, $modelid);
     }
 
-    /**
-     * Remove all possibly problematic chars from the time splitting method id (id = its full class name).
-     *
-     * @param string $timesplittingid
-     * @return string
-     */
-    protected static function clean_time_splitting_id($timesplittingid) {
-        $timesplittingid = str_replace('\\', '-', $timesplittingid);
-        return clean_param($timesplittingid, PARAM_ALPHANUMEXT);
-    }
-
     /**
      * Returns the file name to be used.
      *
diff --git a/analytics/classes/insights_generator.php b/analytics/classes/insights_generator.php
new file mode 100644 (file)
index 0000000..24fdfaf
--- /dev/null
@@ -0,0 +1,236 @@
+<?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/>.
+
+/**
+ * Insights generator.
+ *
+ * @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;
+
+defined('MOODLE_INTERNAL') || die();
+
+require_once($CFG->dirroot . '/lib/messagelib.php');
+
+/**
+ * Insights generator.
+ *
+ * @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 insights_generator {
+
+    /**
+     * @var int
+     */
+    private $modelid;
+
+    /**
+     * @var \core_analytics\local\target\base
+     */
+    private $target;
+
+    /**
+     * @var int[]
+     */
+    private $contextcourseids;
+
+    /**
+     * Constructor.
+     *
+     * @param int $modelid
+     * @param \core_analytics\local\target\base $target
+     */
+    public function __construct(int $modelid, \core_analytics\local\target\base $target) {
+        $this->modelid = $modelid;
+        $this->target = $target;
+    }
+
+    /**
+     * Generates insight notifications.
+     *
+     * @param array                         $samplecontexts    The contexts these predictions belong to
+     * @param \core_analytics\prediction[]  $predictions       The prediction records
+     * @return  null
+     */
+    public function generate($samplecontexts, $predictions) {
+        global $OUTPUT;
+
+        $analyserclass = $this->target->get_analyser_class();
+
+        // We will need to restore it later.
+        $actuallanguage = current_language();
+
+        if ($analyserclass::one_sample_per_analysable()) {
+
+            // Iterate through the predictions and the users in each prediction (likely to be just one).
+            foreach ($predictions as $prediction) {
+
+                $context = $samplecontexts[$prediction->get_prediction_data()->contextid];
+
+                $users = $this->target->get_insights_users($context);
+                foreach ($users as $user) {
+
+                    $this->set_notification_language($user);
+                    list($insighturl, $fullmessage, $fullmessagehtml) = $this->prediction_info($prediction);
+                    $this->notification($context, $user, $insighturl, $fullmessage, $fullmessagehtml);
+                }
+            }
+
+        } else {
+
+            // Iterate through the context and the users in each context.
+            foreach ($samplecontexts as $context) {
+
+                $users = $this->target->get_insights_users($context);
+                foreach ($users as $user) {
+
+                    $this->set_notification_language($user);
+
+                    $insighturl = $this->target->get_insight_context_url($this->modelid, $context);
+
+                    $fullmessage = get_string('insightinfomessage', 'analytics', $insighturl->out(false));
+                    $fullmessagehtml = $OUTPUT->render_from_template('core_analytics/insight_info_message',
+                        ['url' => $insighturl->out(false)]
+                    );
+
+                    $this->notification($context, $user, $insighturl, $fullmessage, $fullmessagehtml);
+                }
+            }
+        }
+
+        force_current_language($actuallanguage);
+    }
+
+    /**
+     * Generates a insight notification for the user.
+     *
+     * @param  \context    $context
+     * @param  \stdClass   $user
+     * @param  \moodle_url $insighturl    The insight URL
+     * @param  string      $fullmessage
+     * @param  string      $fullmessagehtml
+     * @return null
+     */
+    private function notification(\context $context, \stdClass $user, \moodle_url $insighturl, string $fullmessage, string $fullmessagehtml) {
+
+        $message = new \core\message\message();
+        $message->component = 'moodle';
+        $message->name = 'insights';
+
+        $message->userfrom = \core_user::get_noreply_user();
+        $message->userto = $user;
+
+        $message->subject = $this->target->get_insight_subject($this->modelid, $context);
+
+        // Same than the subject.
+        $message->contexturlname = $message->subject;
+        $message->courseid = $this->get_context_courseid($context);
+
+        $message->fullmessage = $fullmessage;
+        $message->fullmessageformat = FORMAT_PLAIN;
+        $message->fullmessagehtml = $fullmessagehtml;
+        $message->smallmessage = $fullmessage;
+        $message->contexturl = $insighturl->out(false);
+
+        message_send($message);
+    }
+
+    /**
+     * Returns the course context of the provided context reading an internal cache first.
+     *
+     * @param  \context $context
+     * @return int
+     */
+    private function get_context_courseid(\context $context) {
+
+        if (empty($this->contextcourseids[$context->id])) {
+
+            $coursecontext = $context->get_course_context(false);
+            if (!$coursecontext) {
+                // Default to the frontpage course context.
+                $coursecontext = \context_course::instance(SITEID);
+            }
+            $this->contextcourseids[$context->id] = $coursecontext->instanceid;
+        }
+
+        return $this->contextcourseids[$context->id];
+    }
+
+    /**
+     * Extracts info from the prediction for display purposes.
+     *
+     * @param  \core_analytics\prediction $prediction
+     * @return array Three items array with formats [\moodle_url, string, string]
+     */
+    private function prediction_info(\core_analytics\prediction $prediction) {
+        global $OUTPUT;
+
+        $predictionactions = $this->target->prediction_actions($prediction, true, true);
+
+        // For FORMAT_PLAIN.
+        $fullmessageplaintext  = '';
+
+        // For FORMAT_HTML.
+        $messageactions  = [];
+        $insighturl = null;
+        foreach ($predictionactions as $action) {
+            $actionurl = $action->get_url();
+            if (!$actionurl->get_param('forwardurl')) {
+
+                $actiondoneurl = new \moodle_url('/report/insights/done.php');
+                // Set the forward url to the 'done' script.
+                $actionurl->param('forwardurl', $actiondoneurl->out(false));
+            }
+
+            if (empty($insighturl)) {
+                // We use the primary action url as insight url so we log that the user followed the provided link.
+                $insighturl = $action->get_url();
+            }
+            $actiondata = (object)['url' => $action->get_url()->out(false), 'text' => $action->get_text()];
+            $fullmessageplaintext .= get_string('insightinfomessageaction', 'analytics', $actiondata) . PHP_EOL;
+            $messageactions[] = $actiondata;
+        }
+
+        $fullmessagehtml = $OUTPUT->render_from_template('core_analytics/insight_info_message_prediction',
+            ['actions' => $messageactions]);
+        return [$insighturl, $fullmessageplaintext, $fullmessagehtml];
+    }
+
+    /**
+     * Sets the session language to the language used by the notification receiver.
+     *
+     * @param  \stdClass $user The user who will receive the message
+     * @return null
+     */
+    private function set_notification_language($user) {
+        global $CFG;
+
+        // Copied from current_language().
+        if (!empty($user->lang)) {
+            $lang = $user->lang;
+        } else if (isset($CFG->lang)) {
+            $lang = $CFG->lang;
+        } else {
+            $lang = 'en';
+        }
+        force_current_language($lang);
+    }
+}
index 06b7468..1c65e2c 100644 (file)
@@ -47,13 +47,6 @@ abstract class base {
      */
     protected $target;
 
-    /**
-     * A $this->$target copy loaded with the ongoing analysis analysable.
-     *
-     * @var \core_analytics\local\target\base
-     */
-    protected $analysabletarget;
-
     /**
      * The model indicators.
      *
@@ -118,9 +111,37 @@ abstract class base {
      * \core_analytics\local\analyser\by_course and \core_analytics\local\analyser\sitewide are implementing
      * this method returning site courses (by_course) and the whole system (sitewide) as analysables.
      *
+     * @todo MDL-65284 This will be removed in Moodle 4.1
+     * @deprecated
+     * @see get_analysables_iterator
+     * @throws  \coding_exception
      * @return \core_analytics\analysable[] Array of analysable elements using the analysable id as array key.
      */
-    abstract public function get_analysables();
+    public function get_analysables() {
+        // This function should only be called from get_analysables_iterator and we keep it here until Moodle 4.1
+        // for backwards compatibility.
+        throw new \coding_exception('This method is deprecated in favour of get_analysables_iterator.');
+    }
+
+    /**
+     * Returns the list of analysable elements available on the site.
+     *
+     * A relatively complex SQL query should be set so that we take into account which analysable elements
+     * have already been processed and the order in which they have been processed. Helper methods are available
+     * to ease to implementation of get_analysables_iterator: get_iterator_sql and order_sql.
+     *
+     * @param string|null $action 'prediction', 'training' or null if no specific action needed.
+     * @return \Iterator
+     */
+    public function get_analysables_iterator(?string $action = null) {
+
+        debugging('Please overwrite get_analysables_iterator with your own implementation, we only keep this default
+            implementation for backwards compatibility purposes with get_analysables(). note that $action param will
+            be ignored so the analysable elements will be processed using get_analysables order, regardless of the
+            last time they were processed.');
+
+        return new \ArrayIterator($this->get_analysables());
+    }
 
     /**
      * This function returns this analysable list of samples.
@@ -128,7 +149,7 @@ abstract class base {
      * @param \core_analytics\analysable $analysable
      * @return array array[0] = int[] (sampleids) and array[1] = array (samplesdata)
      */
-    abstract protected function get_all_samples(\core_analytics\analysable $analysable);
+    abstract public function get_all_samples(\core_analytics\analysable $analysable);
 
     /**
      * This function returns the samples data from a list of sample ids.
@@ -174,70 +195,46 @@ abstract class base {
     abstract public function sample_description($sampleid, $contextid, $sampledata);
 
     /**
-     * Main analyser method which processes the site analysables.
-     *
-     * @param bool $includetarget
-     * @return \stored_file[]
+     * Model id getter.
+     * @return int
      */
-    public function get_analysable_data($includetarget) {
-        global $DB;
-
-        // Time limit control.
-        $modeltimelimit = intval(get_config('analytics', 'modeltimelimit'));
-
-        $filesbytimesplitting = array();
-
-        list($analysables, $processedanalysables) = $this->get_sorted_analysables($includetarget);
-
-        $inittime = time();
-      &nbs