From c9d9703a042083d929657adb8bc1876cfe6204da Mon Sep 17 00:00:00 2001 From: PMKuipers Date: Fri, 1 Dec 2023 11:12:21 +0100 Subject: [PATCH] Bufgix where checking page progress on an empty page would result in division by sero --- classes/studyplan.php | 19 +++++++++++++------ classes/studyplanpage.php | 7 ++++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/classes/studyplan.php b/classes/studyplan.php index f2ffe4b..19a77a4 100644 --- a/classes/studyplan.php +++ b/classes/studyplan.php @@ -629,11 +629,15 @@ class studyplan { $progress = 0; $pages = $this->pages(); foreach ($pages as $p) { - $progress += $p->scanuserprogress($userid); + $prg = $p->scanuserprogress($userid); + $progress += $prg; } // Now average it out over the amount of pages - $progress = $progress / count($pages); - return $progress; + if (count($pages) > 0 ) { + return $progress / count($pages); + } else { + return 0; + } } /** @@ -642,7 +646,10 @@ class studyplan { * @return array Webservice data model */ public function user_model($userid) { - + $progress = $this->scanuserprogress($userid); + if (is_nan($progress)) { + $progress = 0; + } $model = [ 'id' => $this->r->id, 'userid' => $userid, @@ -652,7 +659,7 @@ class studyplan { 'descriptionformat' => $this->r->descriptionformat, 'icon' => $this->icon(), 'idnumber' => $this->r->idnumber, - 'progress' => $this->scanuserprogress($userid), + 'progress' => $progress, 'pages' => [], 'aggregation_info' => $this->aggregator->basic_model(), ]; @@ -865,7 +872,7 @@ class studyplan { $sql = "SELECT COUNT(i.id) FROM {local_treestudyplan} INNER JOIN {local_treestudyplan_line} l ON p.id = l.studyplan_id - INNER JOIN {local_treestudyplan_item} i ON l.id = i.line_id + {local_treestudyplan_item} i ON l.id = i.line_id WHERE p.id = :planid AND i.course_id = :courseid"; $count = $DB->get_field_sql($sql, ["courseid" => $courseid, "planid" => $this->id]); diff --git a/classes/studyplanpage.php b/classes/studyplanpage.php index 106b2fd..36a706c 100644 --- a/classes/studyplanpage.php +++ b/classes/studyplanpage.php @@ -405,7 +405,12 @@ class studyplanpage { } } } - return ($completed/$courses); + if ( $courses > 0 ) { + return ($completed/$courses); + } else { + return 0; + } + } /**