diff --git a/analytics/classes/dataset_manager.php b/analytics/classes/dataset_manager.php index b4671688cd4..00b571a5725 100644 --- a/analytics/classes/dataset_manager.php +++ b/analytics/classes/dataset_manager.php @@ -157,16 +157,18 @@ class dataset_manager { ]; // Delete previous and old (we already checked that previous copies are not recent) evaluation files for this analysable. - $select = " = {$filerecord['itemid']} AND filepath = :filepath"; - $fs->delete_area_files_select($filerecord['contextid'], $filerecord['component'], $filerecord['filearea'], - $select, array('filepath' => $filerecord['filepath'])); + if ($this->evaluation) { + $select = " = {$filerecord['itemid']} AND filepath = :filepath"; + $fs->delete_area_files_select($filerecord['contextid'], $filerecord['component'], $filerecord['filearea'], + $select, array('filepath' => $filerecord['filepath'])); + } // Write all this stuff to a tmp file. $filepath = make_request_directory() . DIRECTORY_SEPARATOR . $filerecord['filename']; $fh = fopen($filepath, 'w+'); if (!$fh) { $this->close_process(); - throw new \moodle_exception('errorcannotwritedataset', 'analytics', '', $tmpfilepath); + throw new \moodle_exception('errorcannotwritedataset', 'analytics', '', $filepath); } foreach ($data as $line) { fputcsv($fh, $line); diff --git a/analytics/classes/local/analyser/base.php b/analytics/classes/local/analyser/base.php index a8277e5784c..08601e8a38f 100644 --- a/analytics/classes/local/analyser/base.php +++ b/analytics/classes/local/analyser/base.php @@ -514,7 +514,7 @@ abstract class base { } } - if (!empty($target)) { + if (!empty($includetarget)) { $filearea = \core_analytics\dataset_manager::LABELLED_FILEAREA; } else { $filearea = \core_analytics\dataset_manager::UNLABELLED_FILEAREA; diff --git a/analytics/tests/prediction_test.php b/analytics/tests/prediction_test.php index f97db57beeb..269a5c1301d 100644 --- a/analytics/tests/prediction_test.php +++ b/analytics/tests/prediction_test.php @@ -165,6 +165,12 @@ class core_analytics_prediction_testcase extends advanced_testcase { $this->assertCount($ncourses * 2, $samples); $this->assertEquals(1, $DB->count_records('analytics_used_files', array('modelid' => $model->get_id(), 'action' => 'trained'))); + // Check that analysable files for training are stored under labelled filearea. + $fs = get_file_storage(); + $this->assertCount(1, $fs->get_directory_files(\context_system::instance()->id, 'analytics', + \core_analytics\dataset_manager::LABELLED_FILEAREA, $model->get_id(), '/analysable/', true, false)); + $this->assertEmpty($fs->get_directory_files(\context_system::instance()->id, 'analytics', + \core_analytics\dataset_manager::UNLABELLED_FILEAREA, $model->get_id(), '/analysable/', true, false)); $courseparams = $params + array('shortname' => 'aaaaaa', 'fullname' => 'aaaaaa', 'visible' => 0); $course1 = $this->getDataGenerator()->create_course($courseparams); @@ -199,6 +205,12 @@ class core_analytics_prediction_testcase extends advanced_testcase { $this->assertEquals(2, $DB->count_records('analytics_predictions', array('modelid' => $model->get_id()))); + // Check that analysable files to get predictions are stored under unlabelled filearea. + $this->assertCount(1, $fs->get_directory_files(\context_system::instance()->id, 'analytics', + \core_analytics\dataset_manager::LABELLED_FILEAREA, $model->get_id(), '/analysable/', true, false)); + $this->assertCount(1, $fs->get_directory_files(\context_system::instance()->id, 'analytics', + \core_analytics\dataset_manager::UNLABELLED_FILEAREA, $model->get_id(), '/analysable/', true, false)); + // No new generated files nor records as there are no new courses available. $model->predict(); $predictedranges = $DB->get_records('analytics_predict_samples', array('modelid' => $model->get_id())); @@ -234,6 +246,10 @@ class core_analytics_prediction_testcase extends advanced_testcase { array('modelid' => $model->get_id(), 'action' => 'predicted'))); $this->assertEquals(4, $DB->count_records('analytics_predictions', array('modelid' => $model->get_id()))); + $this->assertCount(1, $fs->get_directory_files(\context_system::instance()->id, 'analytics', + \core_analytics\dataset_manager::LABELLED_FILEAREA, $model->get_id(), '/analysable/', true, false)); + $this->assertCount(2, $fs->get_directory_files(\context_system::instance()->id, 'analytics', + \core_analytics\dataset_manager::UNLABELLED_FILEAREA, $model->get_id(), '/analysable/', true, false)); // New visible course (for training). $course5 = $this->getDataGenerator()->create_course(array('shortname' => 'aaa', 'fullname' => 'aa')); @@ -241,18 +257,10 @@ class core_analytics_prediction_testcase extends advanced_testcase { $result = $model->train(); $this->assertEquals(2, $DB->count_records('analytics_used_files', array('modelid' => $model->get_id(), 'action' => 'trained'))); - - // Update one of the courses to not visible, it should be used again for prediction. - $course5->visible = 0; - update_course($course5); - - $model->predict(); - $this->assertEquals(1, $DB->count_records('analytics_predict_samples', - array('modelid' => $model->get_id()))); - $this->assertEquals(2, $DB->count_records('analytics_used_files', - array('modelid' => $model->get_id(), 'action' => 'predicted'))); - $this->assertEquals(4, $DB->count_records('analytics_predictions', - array('modelid' => $model->get_id()))); + $this->assertCount(2, $fs->get_directory_files(\context_system::instance()->id, 'analytics', + \core_analytics\dataset_manager::LABELLED_FILEAREA, $model->get_id(), '/analysable/', true, false)); + $this->assertCount(2, $fs->get_directory_files(\context_system::instance()->id, 'analytics', + \core_analytics\dataset_manager::UNLABELLED_FILEAREA, $model->get_id(), '/analysable/', true, false)); set_config('enabled_stores', '', 'tool_log'); get_log_manager(true); diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index e8b25fc1825..887d7d46d22 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2749,5 +2749,22 @@ function xmldb_main_upgrade($oldversion) { // Main savepoint reached. upgrade_main_savepoint(true, 2017101300.01); } + + if ($oldversion < 2017101900.01) { + + $fs = get_file_storage(); + $models = $DB->get_records('analytics_models'); + foreach ($models as $model) { + $files = $fs->get_directory_files(\context_system::instance()->id, 'analytics', 'unlabelled', $model->id, + '/analysable/', true, true); + foreach ($files as $file) { + $file->delete(); + } + } + + // Main savepoint reached. + upgrade_main_savepoint(true, 2017101900.01); + } + return true; }