MDL-59779 analytics: Reduce community of inquiry indicators db queries
authorDavid Monllao <davidm@moodle.com>
Thu, 10 Aug 2017 09:07:23 +0000 (11:07 +0200)
committerDavid Monllao <davidm@moodle.com>
Thu, 10 Aug 2017 09:07:23 +0000 (11:07 +0200)
analytics/classes/local/indicator/base.php
analytics/classes/local/indicator/community_of_inquiry_activity.php
analytics/classes/local/time_splitting/base.php
mod/book/classes/analytics/indicator/activity_base.php
mod/folder/classes/analytics/indicator/activity_base.php
mod/imscp/classes/analytics/indicator/activity_base.php
mod/label/classes/analytics/indicator/activity_base.php
mod/page/classes/analytics/indicator/activity_base.php
mod/resource/classes/analytics/indicator/activity_base.php
mod/survey/classes/analytics/indicator/activity_base.php
mod/url/classes/analytics/indicator/activity_base.php

index 310dc55..5fc555b 100644 (file)
@@ -121,6 +121,22 @@ abstract class base extends \core_analytics\calculable {
         return self::MIN_VALUE;
     }
 
+    /**
+     * Hook to allow indicators to pre-fill data that is shared accross time range calculations.
+     *
+     * Useful to fill analysable-dependant data that does not depend on the time ranges. Use
+     * instance vars to cache data that can be re-used across samples calculations but changes
+     * between time ranges (indicator instances are reset between time ranges to avoid unexpected
+     * problems).
+     *
+     * You are also responsible of emptying previous analysable caches.
+     *
+     * @param \core_analytics\analysable $analysable
+     * @return void
+     */
+    public function fill_per_analysable_caches(\core_analytics\analysable $analysable) {
+    }
+
     /**
      * Calculates the indicator.
      *
index 60d5e0f..74de894 100644 (file)
@@ -60,6 +60,20 @@ abstract class community_of_inquiry_activity extends linear {
      */
     const INDICATOR_SOCIAL = "social";
 
+    /**
+     * Fetch the course grades of this activity type instances.
+     *
+     * @param \core_analytics\analysable $analysable
+     * @return void
+     */
+    public function fill_per_analysable_caches(\core_analytics\analysable $analysable) {
+
+        // Better to check it, we can not be 100% it will be a \core_analytics\course object.
+        if ($analysable instanceof \core_analytics\course) {
+            $this->fetch_student_grades($analysable);
+        }
+    }
+
     /**
      * Returns the activity type. No point in changing this class in children classes.
      *
@@ -396,8 +410,8 @@ abstract class community_of_inquiry_activity extends linear {
         }
 
         if ($this->grades === null) {
-            $courseactivities = $this->course->get_all_activities($this->get_activity_type());
-            $this->grades = $this->course->get_student_grades($courseactivities);
+            // Even if this is probably already filled during fill_per_analysable_caches.
+            $this->fetch_student_grades($this->course);
         }
 
         if ($cm = $this->retrieve('cm', $sampleid)) {
@@ -645,6 +659,17 @@ abstract class community_of_inquiry_activity extends linear {
         throw new \coding_exception("Indicator type is invalid.");
     }
 
+    /**
+     * Gets the course student grades.
+     *
+     * @param \core_analytics\course $course
+     * @return void
+     */
+    protected function fetch_student_grades(\core_analytics\course $course) {
+        $courseactivities = $course->get_all_activities($this->get_activity_type());
+        $this->grades = $course->get_student_grades($courseactivities);
+    }
+
     /**
      * Defines indicator type.
      *
index 23adfb1..85a7701 100644 (file)
@@ -201,6 +201,9 @@ abstract class base {
         // Fill the dataset samples with indicators data.
         foreach ($indicators as $indicator) {
 
+            // Hook to allow indicators to store analysable-dependant data.
+            $indicator->fill_per_analysable_caches($this->analysable);
+
             // Per-range calculations.
             foreach ($ranges as $rangeindex => $range) {
 
index d5ebbd0..33164e7 100644 (file)
@@ -34,4 +34,13 @@ defined('MOODLE_INTERNAL') || die();
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 abstract class activity_base extends \core_analytics\local\indicator\community_of_inquiry_activity {
+
+    /**
+     * No need to fetch grades for resources.
+     *
+     * @param \core_analytics\course $course
+     * @return void
+     */
+    public function fetch_student_grades(\core_analytics\course $course) {
+    }
 }
index fd86460..96bd395 100644 (file)
@@ -34,4 +34,13 @@ defined('MOODLE_INTERNAL') || die();
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 abstract class activity_base extends \core_analytics\local\indicator\community_of_inquiry_activity {
+
+    /**
+     * No need to fetch grades for resources.
+     *
+     * @param \core_analytics\course $course
+     * @return void
+     */
+    public function fetch_student_grades(\core_analytics\course $course) {
+    }
 }
index c5070ad..23068aa 100644 (file)
@@ -34,4 +34,13 @@ defined('MOODLE_INTERNAL') || die();
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 abstract class activity_base extends \core_analytics\local\indicator\community_of_inquiry_activity {
+
+    /**
+     * No need to fetch grades for resources.
+     *
+     * @param \core_analytics\course $course
+     * @return void
+     */
+    public function fetch_student_grades(\core_analytics\course $course) {
+    }
 }
index b924651..59b5717 100644 (file)
@@ -34,4 +34,13 @@ defined('MOODLE_INTERNAL') || die();
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 abstract class activity_base extends \core_analytics\local\indicator\community_of_inquiry_activity {
+
+    /**
+     * No need to fetch grades for resources.
+     *
+     * @param \core_analytics\course $course
+     * @return void
+     */
+    public function fetch_student_grades(\core_analytics\course $course) {
+    }
 }
index e5585e6..d91b040 100644 (file)
@@ -34,4 +34,13 @@ defined('MOODLE_INTERNAL') || die();
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 abstract class activity_base extends \core_analytics\local\indicator\community_of_inquiry_activity {
+
+    /**
+     * No need to fetch grades for resources.
+     *
+     * @param \core_analytics\course $course
+     * @return void
+     */
+    public function fetch_student_grades(\core_analytics\course $course) {
+    }
 }
index 8e8627a..6acb74b 100644 (file)
@@ -34,4 +34,13 @@ defined('MOODLE_INTERNAL') || die();
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 abstract class activity_base extends \core_analytics\local\indicator\community_of_inquiry_activity {
+
+    /**
+     * No need to fetch grades for resources.
+     *
+     * @param \core_analytics\course $course
+     * @return void
+     */
+    public function fetch_student_grades(\core_analytics\course $course) {
+    }
 }
index d6f39d4..80c8f38 100644 (file)
@@ -34,4 +34,13 @@ defined('MOODLE_INTERNAL') || die();
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 abstract class activity_base extends \core_analytics\local\indicator\community_of_inquiry_activity {
+
+    /**
+     * No need to fetch grades.
+     *
+     * @param \core_analytics\course $course
+     * @return void
+     */
+    public function fetch_student_grades(\core_analytics\course $course) {
+    }
 }
index 82bc6b9..ed5e55e 100644 (file)
@@ -34,4 +34,13 @@ defined('MOODLE_INTERNAL') || die();
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 abstract class activity_base extends \core_analytics\local\indicator\community_of_inquiry_activity {
+
+    /**
+     * No need to fetch grades for resources.
+     *
+     * @param \core_analytics\course $course
+     * @return void
+     */
+    public function fetch_student_grades(\core_analytics\course $course) {
+    }
 }