Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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  /**
  18   * Unit tests for the dataset manager.
  19   *
  20   * @package   core_analytics
  21   * @copyright 2017 David MonllaĆ³ {@link http://www.davidmonllao.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Unit tests for the dataset manager.
  29   *
  30   * @package   core_analytics
  31   * @copyright 2017 David MonllaĆ³ {@link http://www.davidmonllao.com}
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class dataset_manager_testcase extends advanced_testcase {
  35  
  36      /**
  37       * setUp
  38       *
  39       * @return null
  40       */
  41      public function setUp() {
  42          $this->resetAfterTest(true);
  43  
  44          $this->sharedtoprows = array(
  45              array('var1', 'var2'),
  46              array('value1', 'value2'),
  47              array('header1', 'header2')
  48          );
  49      }
  50  
  51      /**
  52       * test_create_dataset
  53       *
  54       * @return null
  55       */
  56      public function test_create_dataset() {
  57  
  58          $dataset1 = new \core_analytics\dataset_manager(1, 1, 'whatever', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
  59          $dataset1data = array_merge($this->sharedtoprows, array(array('yeah', 'yeah', 'yeah')));
  60          $f1 = $dataset1->store($dataset1data);
  61  
  62          $f1contents = $f1->get_content();
  63          $this->assertContains('yeah', $f1contents);
  64          $this->assertContains('var1', $f1contents);
  65          $this->assertContains('value1', $f1contents);
  66          $this->assertContains('header1', $f1contents);
  67      }
  68  
  69      /**
  70       * test_merge_datasets
  71       *
  72       * @return null
  73       */
  74      public function test_merge_datasets() {
  75  
  76          $dataset1 = new \core_analytics\dataset_manager(1, 1, 'whatever', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
  77          $dataset1data = array_merge($this->sharedtoprows, array(array('yeah', 'yeah', 'yeah')));
  78          $f1 = $dataset1->store($dataset1data);
  79  
  80          $dataset2 = new \core_analytics\dataset_manager(1, 2, 'whatever', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
  81          $dataset2data = array_merge($this->sharedtoprows, array(array('no', 'no', 'no')));
  82          $f2 = $dataset2->store($dataset2data);
  83  
  84          $files = array($f1, $f2);
  85          $merged = \core_analytics\dataset_manager::merge_datasets($files, 1, 'whatever',
  86              \core_analytics\dataset_manager::LABELLED_FILEAREA);
  87  
  88          $mergedfilecontents = $merged->get_content();
  89          $this->assertContains('yeah', $mergedfilecontents);
  90          $this->assertContains('no', $mergedfilecontents);
  91          $this->assertContains('var1', $mergedfilecontents);
  92          $this->assertContains('value1', $mergedfilecontents);
  93          $this->assertContains('header1', $mergedfilecontents);
  94      }
  95  
  96      /**
  97       * test_get_pending_files
  98       *
  99       * @return null
 100       */
 101      public function test_get_pending_files() {
 102          global $DB;
 103  
 104          $this->resetAfterTest();
 105  
 106          $fakemodelid = 123;
 107          $timesplittingids = array(
 108              '\core\analytics\time_splitting\quarters',
 109              '\core\analytics\time_splitting\quarters_accum',
 110          );
 111  
 112          // No files.
 113          $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, true, $timesplittingids));
 114          $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids));
 115  
 116          // We will reuse this analysable file to create training and prediction datasets (analysable level files are
 117          // merged into training and prediction files).
 118          $analysabledataset = new \core_analytics\dataset_manager($fakemodelid, 1, 'whatever',
 119              \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
 120          $analysabledatasetdata = array_merge($this->sharedtoprows, array(array('yeah', 'yeah', 'yeah')));
 121          $file = $analysabledataset->store($analysabledatasetdata);
 122  
 123          // Evaluation files ignored.
 124          $evaluationdataset = \core_analytics\dataset_manager::merge_datasets(array($file), $fakemodelid,
 125              '\core\analytics\time_splitting\quarters', \core_analytics\dataset_manager::LABELLED_FILEAREA, true);
 126  
 127          $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, true, $timesplittingids));
 128          $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids));
 129  
 130          // Training and prediction files are not mixed up.
 131          $trainingfile1 = \core_analytics\dataset_manager::merge_datasets(array($file), $fakemodelid,
 132              '\core\analytics\time_splitting\quarters', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
 133          $this->waitForSecond();
 134          $trainingfile2 = \core_analytics\dataset_manager::merge_datasets(array($file), $fakemodelid,
 135              '\core\analytics\time_splitting\quarters', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
 136  
 137          $bytimesplitting = \core_analytics\dataset_manager::get_pending_files($fakemodelid, true, $timesplittingids);
 138          $this->assertFalse(isset($bytimesplitting['\core\analytics\time_splitting\quarters_accum']));
 139          $this->assertCount(2, $bytimesplitting['\core\analytics\time_splitting\quarters']);
 140          $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids));
 141  
 142          $predictionfile = \core_analytics\dataset_manager::merge_datasets(array($file), $fakemodelid,
 143              '\core\analytics\time_splitting\quarters', \core_analytics\dataset_manager::UNLABELLED_FILEAREA, false);
 144          $bytimesplitting = \core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids);
 145          $this->assertFalse(isset($bytimesplitting['\core\analytics\time_splitting\quarters_accum']));
 146          $this->assertCount(1, $bytimesplitting['\core\analytics\time_splitting\quarters']);
 147  
 148          // Already used for training and prediction are discarded.
 149          $usedfile = (object)['modelid' => $fakemodelid, 'fileid' => $trainingfile1->get_id(), 'action' => 'trained',
 150              'time' => time()];
 151          $DB->insert_record('analytics_used_files', $usedfile);
 152          $bytimesplitting = \core_analytics\dataset_manager::get_pending_files($fakemodelid, true, $timesplittingids);
 153          $this->assertCount(1, $bytimesplitting['\core\analytics\time_splitting\quarters']);
 154  
 155          $usedfile->fileid = $predictionfile->get_id();
 156          $usedfile->action = 'predicted';
 157          $DB->insert_record('analytics_used_files', $usedfile);
 158          $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids));
 159      }
 160  }