From 935325c1275eec660b1cfb0ba9f093d2306b98b3 Mon Sep 17 00:00:00 2001 From: Johannes Brandenburger <79154528+johannesbrandenburger@users.noreply.github.com> Date: Mon, 15 Apr 2024 15:23:26 +0200 Subject: [PATCH] frontend: moodle course open --- .../lib/components/choose/choose_form.dart | 35 ++++++++++++++----- frontend/lib/models/course.dart | 5 ++- frontend/pubspec.yaml | 1 + 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/frontend/lib/components/choose/choose_form.dart b/frontend/lib/components/choose/choose_form.dart index 60388d32..b8c4dc0e 100644 --- a/frontend/lib/components/choose/choose_form.dart +++ b/frontend/lib/components/choose/choose_form.dart @@ -6,6 +6,7 @@ import 'package:frontend/models/course.dart'; import 'package:frontend/theme/assets.dart'; import 'package:frontend/enums/form_type.dart'; import 'package:rive/rive.dart'; +import 'package:url_launcher/url_launcher.dart'; class ChooseForm extends StatefulWidget { final Course course; @@ -71,6 +72,18 @@ class _ChooseFormState extends State { ); } + void _openMoodle() async { + final moodleCourseId = widget.course.moodleCourseId; + final moodleUrl = + "https://moodle.htwg-konstanz.de/moodle/course/view.php?id=$moodleCourseId"; + final moodleUri = Uri.parse(moodleUrl); + if (await canLaunchUrl(moodleUri)) { + await launchUrl(moodleUri); + } else { + throw 'Could not launch $moodleUrl'; + } + } + @override Widget build(BuildContext context) { final colors = Theme.of(context).colorScheme; @@ -117,15 +130,19 @@ class _ChooseFormState extends State { padding: const EdgeInsets.only(right: 20), child: Align( alignment: Alignment.centerRight, - child: ElevatedButton( - onPressed: () {}, - style: ElevatedButton.styleFrom( - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular(20.0), - ), - ), - child: Image.asset(moodleLogo, width: 80), - ), + child: widget.course.moodleCourseId != '' + ? ElevatedButton( + onPressed: () { + _openMoodle(); + }, + style: ElevatedButton.styleFrom( + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(20.0), + ), + ), + child: Image.asset(moodleLogo, width: 80), + ) + : null, ), ), body: Column( diff --git a/frontend/lib/models/course.dart b/frontend/lib/models/course.dart index f9b3f0da..e325e6da 100644 --- a/frontend/lib/models/course.dart +++ b/frontend/lib/models/course.dart @@ -9,6 +9,7 @@ class Course { final List feedbackForms; final List quizForms; final bool isOwner; + final String moodleCourseId; Course({ required this.id, @@ -17,6 +18,7 @@ class Course { required this.feedbackForms, required this.quizForms, required this.isOwner, + required this.moodleCourseId, }); factory Course.fromJson(Map json) { @@ -25,7 +27,7 @@ class Course { List owners = json['owners'].cast(); isOwner = owners.contains(getSession()!.userId); } - + return Course( id: json['id'], name: json['name'], @@ -37,6 +39,7 @@ class Course { .map((e) => QuizForm.fromJson(e, isOwner: isOwner)) .toList(), isOwner: isOwner, + moodleCourseId: json['moodleCourseId'] ?? '', ); } } diff --git a/frontend/pubspec.yaml b/frontend/pubspec.yaml index 59447220..1b3f6d99 100644 --- a/frontend/pubspec.yaml +++ b/frontend/pubspec.yaml @@ -42,6 +42,7 @@ dependencies: jwt_decoder: ^2.0.1 intl: ^0.19.0 rive: ^0.13.1 + url_launcher: ^6.2.6 # mobile_scanner: ^3.5.7 dev_dependencies: