Commit a8ccc5f2 authored by David Monllaó's avatar David Monllaó
Browse files

MDL-59630 analytics: Move get_analysables to abstract class

parent 23ab0d77
...@@ -112,6 +112,16 @@ abstract class base { ...@@ -112,6 +112,16 @@ abstract class base {
$this->log = array(); $this->log = array();
} }
/**
* Returns the list of analysable elements available on the site.
*
* \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.
*
* @return \core_analytics\analysable[]
*/
abstract public function get_analysables();
/** /**
* This function returns this analysable list of samples. * This function returns this analysable list of samples.
* *
...@@ -141,7 +151,7 @@ abstract class base { ...@@ -141,7 +151,7 @@ abstract class base {
* *
* @return string * @return string
*/ */
abstract protected function get_samples_origin(); abstract public function get_samples_origin();
/** /**
* Returns the context of a sample. * Returns the context of a sample.
...@@ -166,15 +176,29 @@ abstract class base { ...@@ -166,15 +176,29 @@ abstract class base {
/** /**
* Main analyser method which processes the site analysables. * Main analyser method which processes the site analysables.
* *
* \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.
* In most of the cases you should have enough extending from one of these classes so you don't need
* to reimplement this method.
*
* @param bool $includetarget * @param bool $includetarget
* @return \stored_file[] * @return \stored_file[]
*/ */
abstract public function get_analysable_data($includetarget); public function get_analysable_data($includetarget) {
$filesbytimesplitting = array();
$analysables = $this->get_analysables();
foreach ($analysables as $analysable) {
$files = $this->process_analysable($analysable, $includetarget);
// Later we will need to aggregate data by time splitting method.
foreach ($files as $timesplittingid => $file) {
$filesbytimesplitting[$timesplittingid][$analysable->get_id()] = $file;
}
}
// We join the datasets by time splitting method.
$timesplittingfiles = $this->merge_analysable_files($filesbytimesplitting, $includetarget);
return $timesplittingfiles;
}
/** /**
* Samples data this analyser provides. * Samples data this analyser provides.
...@@ -220,6 +244,36 @@ abstract class base { ...@@ -220,6 +244,36 @@ abstract class base {
} }
} }
/**
* Merges analysable dataset files into 1.
*
* @param array $filesbytimesplitting
* @param bool $includetarget
* @return \stored_file[]
*/
protected function merge_analysable_files($filesbytimesplitting, $includetarget) {
$timesplittingfiles = array();
foreach ($filesbytimesplitting as $timesplittingid => $files) {
if ($this->options['evaluation'] === true) {
// Delete the previous copy. Only when evaluating.
\core_analytics\dataset_manager::delete_previous_evaluation_file($this->modelid, $timesplittingid);
}
// Merge all course files into one.
if ($includetarget) {
$filearea = \core_analytics\dataset_manager::LABELLED_FILEAREA;
} else {
$filearea = \core_analytics\dataset_manager::UNLABELLED_FILEAREA;
}
$timesplittingfiles[$timesplittingid] = \core_analytics\dataset_manager::merge_datasets($files,
$this->modelid, $timesplittingid, $filearea, $this->options['evaluation']);
}
return $timesplittingfiles;
}
/** /**
* Checks that this analyser satisfies the provided indicator requirements. * Checks that this analyser satisfies the provided indicator requirements.
* *
......
...@@ -40,14 +40,14 @@ abstract class by_course extends base { ...@@ -40,14 +40,14 @@ abstract class by_course extends base {
* *
* @return \core_analytics\course[] * @return \core_analytics\course[]
*/ */
public function get_courses() { public function get_analysables() {
// Default to all system courses. // Default to all system courses.
if (!empty($this->options['filter'])) { if (!empty($this->options['filter'])) {
$courses = $this->options['filter']; $courses = $this->options['filter'];
} else { } else {
// Iterate through all potentially valid courses. // Iterate through all potentially valid courses.
$courses = get_courses(); $courses = get_courses('all', 'c.sortorder ASC');
} }
unset($courses[SITEID]); unset($courses[SITEID]);
...@@ -55,7 +55,7 @@ abstract class by_course extends base { ...@@ -55,7 +55,7 @@ abstract class by_course extends base {
foreach ($courses as $course) { foreach ($courses as $course) {
// Skip the frontpage course. // Skip the frontpage course.
$analysable = \core_analytics\course::instance($course); $analysable = \core_analytics\course::instance($course);
$analysables[$analysable->get_id()] = $analysable; $analysables[] = $analysable;
} }
if (empty($analysables)) { if (empty($analysables)) {
...@@ -64,62 +64,4 @@ abstract class by_course extends base { ...@@ -64,62 +64,4 @@ abstract class by_course extends base {
return $analysables; return $analysables;
} }
/**
* Returns the analysed data
*
* @param bool $includetarget
* @return \stored_file[]
*/
public function get_analysable_data($includetarget) {
$filesbytimesplitting = array();
// This class and all children will iterate through a list of courses (\core_analytics\course).
$analysables = $this->get_courses('all', 'c.sortorder ASC');
foreach ($analysables as $analysableid => $analysable) {
$files = $this->process_analysable($analysable, $includetarget);
// Later we will need to aggregate data by time splitting method.
foreach ($files as $timesplittingid => $file) {
$filesbytimesplitting[$timesplittingid][$analysableid] = $file;
}
}
// We join the datasets by time splitting method.
$timesplittingfiles = $this->merge_analysable_files($filesbytimesplitting, $includetarget);
return $timesplittingfiles;
}
/**
* Merges analysable dataset files into 1.
*
* @param array $filesbytimesplitting
* @param bool $includetarget
* @return \stored_file[]
*/
protected function merge_analysable_files($filesbytimesplitting, $includetarget) {
$timesplittingfiles = array();
foreach ($filesbytimesplitting as $timesplittingid => $files) {
if ($this->options['evaluation'] === true) {
// Delete the previous copy. Only when evaluating.
\core_analytics\dataset_manager::delete_previous_evaluation_file($this->modelid, $timesplittingid);
}
// Merge all course files into one.
if ($includetarget) {
$filearea = \core_analytics\dataset_manager::LABELLED_FILEAREA;
} else {
$filearea = \core_analytics\dataset_manager::UNLABELLED_FILEAREA;
}
$timesplittingfiles[$timesplittingid] = \core_analytics\dataset_manager::merge_datasets($files,
$this->modelid, $timesplittingid, $filearea, $this->options['evaluation']);
}
return $timesplittingfiles;
}
} }
...@@ -36,36 +36,12 @@ defined('MOODLE_INTERNAL') || die(); ...@@ -36,36 +36,12 @@ defined('MOODLE_INTERNAL') || die();
abstract class sitewide extends base { abstract class sitewide extends base {
/** /**
* Returns the analysable data. * Returns one single analysable element, the site.
* *
* @param bool $includetarget * @return \core_analytics\analysable[]
* @return \stored_file[] One file for each time splitting method.
*/ */
public function get_analysable_data($includetarget) { public function get_analysables() {
// Here there is a single analysable and it is the system.
$analysable = new \core_analytics\site(); $analysable = new \core_analytics\site();
return array($analysable);
$files = $this->process_analysable($analysable, $includetarget);
// Copy to range files as there is just one analysable.
foreach ($files as $timesplittingid => $file) {
if ($this->options['evaluation'] === true) {
// Delete the previous copy. Only when evaluating.
\core_analytics\dataset_manager::delete_previous_evaluation_file($this->modelid, $timesplittingid);
}
// We use merge but it is just a copy.
if ($includetarget) {
$filearea = \core_analytics\dataset_manager::LABELLED_FILEAREA;
} else {
$filearea = \core_analytics\dataset_manager::UNLABELLED_FILEAREA;
}
$files[$timesplittingid] = \core_analytics\dataset_manager::merge_datasets(array($file), $this->modelid,
$timesplittingid, $filearea, $this->options['evaluation']);
}
return $files;
} }
} }
...@@ -247,15 +247,15 @@ class model { ...@@ -247,15 +247,15 @@ class model {
/** /**
* Returns the model analyser (defined by the model target). * Returns the model analyser (defined by the model target).
* *
* @param array $options Default initialisation with no options.
* @return \core_analytics\local\analyser\base * @return \core_analytics\local\analyser\base
*/ */
public function get_analyser() { public function get_analyser($options = array()) {
if ($this->analyser !== null) { if ($this->analyser !== null) {
return $this->analyser; return $this->analyser;
} }
// Default initialisation with no options. $this->init_analyser($options);
$this->init_analyser();
return $this->analyser; return $this->analyser;
} }
...@@ -276,26 +276,29 @@ class model { ...@@ -276,26 +276,29 @@ class model {
throw new \moodle_exception('errornotarget', 'analytics'); throw new \moodle_exception('errornotarget', 'analytics');
} }
if (!empty($options['evaluation'])) { $timesplittings = array();
// The evaluation process will run using all available time splitting methods unless one is specified. if (empty($options['notimesplitting'])) {
if (!empty($options['timesplitting'])) { if (!empty($options['evaluation'])) {
$timesplitting = \core_analytics\manager::get_time_splitting($options['timesplitting']); // The evaluation process will run using all available time splitting methods unless one is specified.
$timesplittings = array($timesplitting->get_id() => $timesplitting); if (!empty($options['timesplitting'])) {
$timesplitting = \core_analytics\manager::get_time_splitting($options['timesplitting']);
$timesplittings = array($timesplitting->get_id() => $timesplitting);
} else {
$timesplittings = \core_analytics\manager::get_enabled_time_splitting_methods();
}
} else { } else {
$timesplittings = \core_analytics\manager::get_enabled_time_splitting_methods();
}
} else {
if (empty($this->model->timesplitting)) { if (empty($this->model->timesplitting)) {
throw new \moodle_exception('invalidtimesplitting', 'analytics', '', $this->model->id); throw new \moodle_exception('invalidtimesplitting', 'analytics', '', $this->model->id);
} }
// Returned as an array as all actions (evaluation, training and prediction) go through the same process. // Returned as an array as all actions (evaluation, training and prediction) go through the same process.
$timesplittings = array($this->model->timesplitting => $this->get_time_splitting()); $timesplittings = array($this->model->timesplitting => $this->get_time_splitting());
} }
if (empty($timesplittings)) { if (empty($timesplittings)) {
throw new \moodle_exception('errornotimesplittings', 'analytics'); throw new \moodle_exception('errornotimesplittings', 'analytics');
}
} }
if (!empty($options['evaluation'])) { if (!empty($options['evaluation'])) {
......
...@@ -49,7 +49,7 @@ class student_enrolments extends \core_analytics\local\analyser\by_course { ...@@ -49,7 +49,7 @@ class student_enrolments extends \core_analytics\local\analyser\by_course {
* *
* @return string * @return string
*/ */
protected function get_samples_origin() { public function get_samples_origin() {
return 'user_enrolments'; return 'user_enrolments';
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment