Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  namespace core_analytics;
  18  
  19  /**
  20   * Unit tests for the dataset manager.
  21   *
  22   * @package   core_analytics
  23   * @copyright 2017 David MonllaĆ³ {@link http://www.davidmonllao.com}
  24   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  class dataset_manager_test extends \advanced_testcase {
  27  
  28      /**
  29       * setUp
  30       *
  31       * @return null
  32       */
  33      public function setUp(): void {
  34          $this->resetAfterTest(true);
  35  
  36          $this->sharedtoprows = array(
  37              array('var1', 'var2'),
  38              array('value1', 'value2'),
  39              array('header1', 'header2')
  40          );
  41      }
  42  
  43      /**
  44       * test_create_dataset
  45       *
  46       * @return null
  47       */
  48      public function test_create_dataset() {
  49  
  50          $dataset1 = new \core_analytics\dataset_manager(1, 1, 'whatever', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
  51          $dataset1data = array_merge($this->sharedtoprows, array(array('yeah', 'yeah', 'yeah')));
  52          $f1 = $dataset1->store($dataset1data);
  53  
  54          $f1contents = $f1->get_content();
  55          $this->assertStringContainsString('yeah', $f1contents);
  56          $this->assertStringContainsString('var1', $f1contents);
  57          $this->assertStringContainsString('value1', $f1contents);
  58          $this->assertStringContainsString('header1', $f1contents);
  59      }
  60  
  61      /**
  62       * test_merge_datasets
  63       *
  64       * @return null
  65       */
  66      public function test_merge_datasets() {
  67  
  68          $dataset1 = new \core_analytics\dataset_manager(1, 1, 'whatever', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
  69          $dataset1data = array_merge($this->sharedtoprows, array(array('yeah', 'yeah', 'yeah')));
  70          $f1 = $dataset1->store($dataset1data);
  71  
  72          $dataset2 = new \core_analytics\dataset_manager(1, 2, 'whatever', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
  73          $dataset2data = array_merge($this->sharedtoprows, array(array('no', 'no', 'no')));
  74          $f2 = $dataset2->store($dataset2data);
  75  
  76          $files = array($f1, $f2);
  77          $merged = \core_analytics\dataset_manager::merge_datasets($files, 1, 'whatever',
  78              \core_analytics\dataset_manager::LABELLED_FILEAREA);
  79  
  80          $mergedfilecontents = $merged->get_content();
  81          $this->assertStringContainsString('yeah', $mergedfilecontents);
  82          $this->assertStringContainsString('no', $mergedfilecontents);
  83          $this->assertStringContainsString('var1', $mergedfilecontents);
  84          $this->assertStringContainsString('value1', $mergedfilecontents);
  85          $this->assertStringContainsString('header1', $mergedfilecontents);
  86      }
  87  
  88      /**
  89       * test_get_pending_files
  90       *
  91       * @return null
  92       */
  93      public function test_get_pending_files() {
  94          global $DB;
  95  
  96          $this->resetAfterTest();
  97  
  98          $fakemodelid = 123;
  99          $timesplittingids = array(
 100              '\core\analytics\time_splitting\quarters',
 101              '\core\analytics\time_splitting\quarters_accum',
 102          );
 103  
 104          // No files.
 105          $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, true, $timesplittingids));
 106          $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids));
 107  
 108          // We will reuse this analysable file to create training and prediction datasets (analysable level files are
 109          // merged into training and prediction files).
 110          $analysabledataset = new \core_analytics\dataset_manager($fakemodelid, 1, 'whatever',
 111              \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
 112          $analysabledatasetdata = array_merge($this->sharedtoprows, array(array('yeah', 'yeah', 'yeah')));
 113          $file = $analysabledataset->store($analysabledatasetdata);
 114  
 115          // Evaluation files ignored.
 116          $evaluationdataset = \core_analytics\dataset_manager::merge_datasets(array($file), $fakemodelid,
 117              '\core\analytics\time_splitting\quarters', \core_analytics\dataset_manager::LABELLED_FILEAREA, true);
 118  
 119          $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, true, $timesplittingids));
 120          $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids));
 121  
 122          // Training and prediction files are not mixed up.
 123          $trainingfile1 = \core_analytics\dataset_manager::merge_datasets(array($file), $fakemodelid,
 124              '\core\analytics\time_splitting\quarters', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
 125          $this->waitForSecond();
 126          $trainingfile2 = \core_analytics\dataset_manager::merge_datasets(array($file), $fakemodelid,
 127              '\core\analytics\time_splitting\quarters', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
 128  
 129          $bytimesplitting = \core_analytics\dataset_manager::get_pending_files($fakemodelid, true, $timesplittingids);
 130          $this->assertFalse(isset($bytimesplitting['\core\analytics\time_splitting\quarters_accum']));
 131          $this->assertCount(2, $bytimesplitting['\core\analytics\time_splitting\quarters']);
 132          $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids));
 133  
 134          $predictionfile = \core_analytics\dataset_manager::merge_datasets(array($file), $fakemodelid,
 135              '\core\analytics\time_splitting\quarters', \core_analytics\dataset_manager::UNLABELLED_FILEAREA, false);
 136          $bytimesplitting = \core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids);
 137          $this->assertFalse(isset($bytimesplitting['\core\analytics\time_splitting\quarters_accum']));
 138          $this->assertCount(1, $bytimesplitting['\core\analytics\time_splitting\quarters']);
 139  
 140          // Already used for training and prediction are discarded.
 141          $usedfile = (object)['modelid' => $fakemodelid, 'fileid' => $trainingfile1->get_id(), 'action' => 'trained',
 142              'time' => time()];
 143          $DB->insert_record('analytics_used_files', $usedfile);
 144          $bytimesplitting = \core_analytics\dataset_manager::get_pending_files($fakemodelid, true, $timesplittingids);
 145          $this->assertCount(1, $bytimesplitting['\core\analytics\time_splitting\quarters']);
 146  
 147          $usedfile->fileid = $predictionfile->get_id();
 148          $usedfile->action = 'predicted';
 149          $DB->insert_record('analytics_used_files', $usedfile);
 150          $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids));
 151      }
 152  }