Skip to content

Commit

Permalink
[Parent][MBL-14349] Additional course filtering (#780)
Browse files Browse the repository at this point in the history
* [Parent][MBL-14349] Additional course filtering

* fixed a woopsie

* fixed a no-op
  • Loading branch information
TrevorNeedham authored May 5, 2020
1 parent 60736fb commit 14f70ba
Show file tree
Hide file tree
Showing 17 changed files with 1,000 additions and 151 deletions.
58 changes: 56 additions & 2 deletions apps/flutter_parent/lib/models/course.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ library course;
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
import 'package:flutter_parent/models/section.dart';
import 'package:flutter_parent/models/term.dart';

import 'course_grade.dart';
import 'enrollment.dart';
Expand Down Expand Up @@ -63,11 +65,11 @@ abstract class Course implements Built<Course, CourseBuilder> {

@nullable
@BuiltValueField(wireName: 'start_at')
String get startAt;
DateTime get startAt;

@nullable
@BuiltValueField(wireName: 'end_at')
String get endAt;
DateTime get endAt;

@nullable
@BuiltValueField(wireName: 'syllabus_body')
Expand Down Expand Up @@ -115,9 +117,16 @@ abstract class Course implements Built<Course, CourseBuilder> {
@BuiltValueField(wireName: 'default_view')
HomePage get homePage;

@nullable
Term get term;

@nullable
BuiltList<Section> get sections;

static void _initializeBuilder(CourseBuilder b) => b
..id = ''
..enrollments = ListBuilder<Enrollment>()
..sections = ListBuilder<Section>()
..name = ''
..needsGradingCount = 0
..hideFinalGrades = false
Expand Down Expand Up @@ -158,6 +167,51 @@ abstract class Course implements Built<Course, CourseBuilder> {
);

String contextFilterId() => 'course_${this.id}';

/// Verifies that the course should be displayed within the parent app
bool isValidForDate() {
if (accessRestrictedByDate) return false;

if (workflowState == 'completed') return false;

final now = DateTime.now();
final isWithinCourseDates = _isWithinDates(startAt, endAt, now);

if (restrictEnrollmentsToCourseDates) {
return isWithinCourseDates;
} else {
final isWithinTermDates = _isWithinDates(term?.startAt, term?.endAt, now);
var isWithinAnySection;
if (sections == null || sections.isEmpty)
isWithinAnySection = true;
else
isWithinAnySection = sections.any((section) => _isWithinDates(section.startAt, section.endAt, now));

return isWithinCourseDates && isWithinTermDates && isWithinAnySection;
}
}

/// Filters enrollments by those associated with the currently selected user and isValidForParent
bool isValidForCurrentStudent(String currentStudentId) {
final hasValidEnrollments = enrollments?.any((enrollment) => enrollment.userId == currentStudentId) ?? false;
return hasValidEnrollments && isValidForDate();
}

bool _isWithinDates(DateTime startAt, DateTime endAt, DateTime now) {
bool isValidEndAt, isValidStartAt;
// If the dates are null, we have to show it
if (startAt == null)
isValidStartAt = true;
else
isValidStartAt = now.isAfter(startAt);

if (endAt == null)
isValidEndAt = true;
else
isValidEndAt = now.isBefore(endAt);

return isValidEndAt && isValidStartAt;
}
}

@BuiltValueEnum(wireName: 'default_view')
Expand Down
125 changes: 89 additions & 36 deletions apps/flutter_parent/lib/models/course.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions apps/flutter_parent/lib/models/section.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2020 - present Instructure, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';

part 'section.g.dart';

/// To have this built_value be generated, run this command from the project root:
/// flutter pub run build_runner build --delete-conflicting-outputs
abstract class Section implements Built<Section, SectionBuilder> {
@BuiltValueSerializer(serializeNulls: true) // Add this line to get nulls to serialize when we convert to JSON
static Serializer<Section> get serializer => _$sectionSerializer;

String get id;
String get name;

@BuiltValueField(wireName: 'start_at')
@nullable
DateTime get startAt;

@BuiltValueField(wireName: 'end_at')
@nullable
DateTime get endAt;

Section._();
factory Section([void Function(SectionBuilder) updates]) = _$Section;

static void _initializeBuilder(SectionBuilder b) => b
..id = ''
..name = '';
}
Loading

0 comments on commit 14f70ba

Please sign in to comment.