From 5bd10de529e23209517f2ca3ba238d8a3fa96483 Mon Sep 17 00:00:00 2001 From: PMKuipers Date: Wed, 22 May 2024 22:16:47 +0200 Subject: [PATCH] Cleanup - and added proper check on available coaching studyplans --- classes/studyplan.php | 5 +---- lib.php | 7 ++++++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/classes/studyplan.php b/classes/studyplan.php index 4ffcd09..08ccbac 100644 --- a/classes/studyplan.php +++ b/classes/studyplan.php @@ -499,17 +499,14 @@ class studyplan { if ($DB->count_records('local_treestudyplan_page', ['studyplan_id' => $this->id]) > 0) { return success::fail('cannot delete studyplan that still has pages'); } else { - // Delete any coach links for this studyplan before deleting the studyplan itself. + // Delete any links to this studyplan before deleting the studyplan itself. $DB->delete_records("local_treestudyplan_coach", ["studyplan_id" => $this->id]); - // TODO: Check if students and cohorts need to be removed at this time too.. $DB->delete_records("local_treestudyplan_cohort", ["studyplan_id" => $this->id]); - // $DB->delete_records("local_treestudyplan_user", ["studyplan_id" => $this->id]); // $DB->delete_records('local_treestudyplan', ['id' => $this->id]); return success::success(); } - } /** diff --git a/lib.php b/lib.php index 94faf87..d137e89 100644 --- a/lib.php +++ b/lib.php @@ -198,10 +198,15 @@ function local_treestudyplan_extend_navigation(global_navigation $navigation) { } else { $hideprimaryhrefs[] = "/local/treestudyplan/edit-plan.php"; } + + $coachsql = "SELECT COUNT('id') FROM {local_treestudyplan_coach} as c + INNER JOIN {local_treestudyplan} AS t ON c.studyplan_id = t.id + WHERE c.user_id = :user_id"; + if ( (premium::enabled() && \get_config("local_treestudyplan","enablecoach")) && (has_capability('local/treestudyplan:coach', context_system::instance()) || webservicehelper::has_capability_in_any_category('local/treestudyplan:coach') - ) && $DB->count_records("local_treestudyplan_coach",["user_id" => $USER->id]) > 0 + ) && $DB->count_records_sql($coachsql,["user_id" => $USER->id]) > 0 ) { $node = navigation_node::create( get_string("link_coach", "local_treestudyplan"),