|
| 1 | +import Component from '@glimmer/component'; |
| 2 | +import { cached, tracked } from '@glimmer/tracking'; |
| 3 | +import { array, fn, uniqueId } from '@ember/helper'; |
| 4 | +import { on } from '@ember/modifier'; |
| 5 | +import { action } from '@ember/object'; |
| 6 | +import { LinkTo } from '@ember/routing'; |
| 7 | +import { service } from '@ember/service'; |
| 8 | +import { TrackedAsyncData } from 'ember-async-data'; |
| 9 | +import t from 'ember-intl/helpers/t'; |
| 10 | +import add from 'ember-math-helpers/helpers/add'; |
| 11 | +import eq from 'ember-truth-helpers/helpers/eq'; |
| 12 | +import or from 'ember-truth-helpers/helpers/or'; |
| 13 | +import FaIcon from 'ilios-common/components/fa-icon'; |
| 14 | +import SortableTh from 'ilios-common/components/sortable-th'; |
| 15 | +import sortBy from 'ilios-common/helpers/sort-by'; |
| 16 | +import { mapBy, uniqueValues } from 'ilios-common/utils/array-helpers'; |
| 17 | + |
| 18 | +export default class LearnerGroupCourseAssociationsComponent extends Component { |
| 19 | + @service iliosConfig; |
| 20 | + @service intl; |
| 21 | + @tracked sortBy = 'school'; |
| 22 | + |
| 23 | + crossesBoundaryConfig = new TrackedAsyncData( |
| 24 | + this.iliosConfig.itemFromConfig('academicYearCrossesCalendarYearBoundaries'), |
| 25 | + ); |
| 26 | + |
| 27 | + @cached |
| 28 | + get academicYearCrossesCalendarYearBoundaries() { |
| 29 | + return this.crossesBoundaryConfig.isResolved ? this.crossesBoundaryConfig.value : false; |
| 30 | + } |
| 31 | + |
| 32 | + get sortedAscending() { |
| 33 | + return !this.sortBy.includes(':desc'); |
| 34 | + } |
| 35 | + |
| 36 | + @action |
| 37 | + setSortBy(what) { |
| 38 | + if (this.sortBy === what) { |
| 39 | + what += ':desc'; |
| 40 | + } |
| 41 | + this.sortBy = what; |
| 42 | + } |
| 43 | + |
| 44 | + @action |
| 45 | + sortAssociations(a, b) { |
| 46 | + const locale = this.intl.get('primaryLocale'); |
| 47 | + switch (this.sortBy) { |
| 48 | + case 'school': |
| 49 | + return a.school.title.localeCompare(b.school.title, locale); |
| 50 | + case 'school:desc': |
| 51 | + return b.school.title.localeCompare(a.school.title, locale); |
| 52 | + case 'course': |
| 53 | + return a.course.title.localeCompare(b.course.title, locale); |
| 54 | + case 'course:desc': |
| 55 | + return b.course.title.localeCompare(a.course.title, locale); |
| 56 | + } |
| 57 | + return 0; |
| 58 | + } |
| 59 | + |
| 60 | + @cached |
| 61 | + get associationsData() { |
| 62 | + return new TrackedAsyncData(this.getAssociations(this.args.learnerGroup)); |
| 63 | + } |
| 64 | + |
| 65 | + get associations() { |
| 66 | + return this.associationsData.isResolved ? this.associationsData.value : []; |
| 67 | + } |
| 68 | + |
| 69 | + get hasAssociations() { |
| 70 | + return !!this.associations.length; |
| 71 | + } |
| 72 | + |
| 73 | + get isLoaded() { |
| 74 | + return this.associationsData.isResolved; |
| 75 | + } |
| 76 | + |
| 77 | + async getAssociations(learnerGroup) { |
| 78 | + // get sessions from the offerings and ILMs associated with the given learner group. |
| 79 | + const offerings = await learnerGroup.offerings; |
| 80 | + const ilms = await learnerGroup.ilmSessions; |
| 81 | + const arr = [...offerings, ...ilms]; |
| 82 | + const sessions = await Promise.all(mapBy(arr, 'session')); |
| 83 | + const uniqueSessions = uniqueValues(sessions); |
| 84 | + |
| 85 | + // add owning courses and school to their sessions |
| 86 | + const sessionsWithCourseAndSchool = await Promise.all( |
| 87 | + uniqueSessions.map(async (session) => { |
| 88 | + const course = await session.course; |
| 89 | + const school = await course.school; |
| 90 | + return { session, course, school }; |
| 91 | + }), |
| 92 | + ); |
| 93 | + // group these sessions by their owning courses |
| 94 | + const map = new Map(); |
| 95 | + sessionsWithCourseAndSchool.forEach(({ session, course, school }) => { |
| 96 | + if (!map.has(course.id)) { |
| 97 | + map.set(course.id, { |
| 98 | + course, |
| 99 | + school, |
| 100 | + sessions: [], |
| 101 | + }); |
| 102 | + } |
| 103 | + map.get(course.id).sessions.push(session); |
| 104 | + }); |
| 105 | + // convert map into an array. |
| 106 | + const rhett = [...map.values()]; |
| 107 | + |
| 108 | + // sort sessions by title in each data object, then return the list of objects. |
| 109 | + const locale = this.intl.get('primaryLocale'); |
| 110 | + return rhett.map((obj) => { |
| 111 | + obj.sessions.sort((a, b) => { |
| 112 | + return a.title.localeCompare(b.title, locale); |
| 113 | + }); |
| 114 | + return obj; |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + <template> |
| 119 | + {{#let (uniqueId) as |templateId|}} |
| 120 | + <section |
| 121 | + class="learner-group-course-associations" |
| 122 | + data-test-learner-group-course-associations |
| 123 | + > |
| 124 | + {{#if this.isLoaded}} |
| 125 | + <div class="header" data-test-header> |
| 126 | + <h3 class="title" data-test-title> |
| 127 | + {{#if this.hasAssociations}} |
| 128 | + {{#if @isExpanded}} |
| 129 | + <button |
| 130 | + class="title link-button" |
| 131 | + type="button" |
| 132 | + aria-expanded="true" |
| 133 | + aria-controls="content-{{templateId}}" |
| 134 | + aria-label={{t "general.hideAssociatedCourses"}} |
| 135 | + data-test-toggle |
| 136 | + {{on "click" (fn @setIsExpanded false)}} |
| 137 | + > |
| 138 | + {{t "general.associatedCourses"}} |
| 139 | + ({{this.associations.length}}) |
| 140 | + <FaIcon @icon="caret-down" /> |
| 141 | + </button> |
| 142 | + {{else}} |
| 143 | + <button |
| 144 | + class="title link-button" |
| 145 | + type="button" |
| 146 | + aria-expanded="false" |
| 147 | + aria-controls="content-{{templateId}}" |
| 148 | + aria-label={{t "general.showAssociatedCourses"}} |
| 149 | + data-test-toggle |
| 150 | + {{on "click" (fn @setIsExpanded true)}} |
| 151 | + > |
| 152 | + {{t "general.associatedCourses"}} |
| 153 | + ({{this.associations.length}}) |
| 154 | + <FaIcon @icon="caret-right" /> |
| 155 | + </button> |
| 156 | + {{/if}} |
| 157 | + {{else}} |
| 158 | + {{t "general.associatedCourses"}} |
| 159 | + ({{this.associations.length}}) |
| 160 | + {{/if}} |
| 161 | + </h3> |
| 162 | + </div> |
| 163 | + {{#if this.hasAssociations}} |
| 164 | + <div |
| 165 | + id="content-{{templateId}}" |
| 166 | + class="content{{if @isExpanded '' ' hidden'}}" |
| 167 | + data-test-content |
| 168 | + hidden={{@isExpanded}} |
| 169 | + > |
| 170 | + <table data-test-associations> |
| 171 | + <thead> |
| 172 | + <tr> |
| 173 | + <SortableTh |
| 174 | + @sortedAscending={{this.sortedAscending}} |
| 175 | + @onClick={{fn this.setSortBy "school"}} |
| 176 | + @sortedBy={{or (eq this.sortBy "school") (eq this.sortBy "school:desc")}} |
| 177 | + > |
| 178 | + {{t "general.school"}} |
| 179 | + </SortableTh> |
| 180 | + <SortableTh |
| 181 | + colspan="3" |
| 182 | + @sortedAscending={{this.sortedAscending}} |
| 183 | + @onClick={{fn this.setSortBy "course"}} |
| 184 | + @sortedBy={{or (eq this.sortBy "course") (eq this.sortBy "course:desc")}} |
| 185 | + > |
| 186 | + {{t "general.course"}} |
| 187 | + </SortableTh> |
| 188 | + <th colspan="3">{{t "general.sessions"}}</th> |
| 189 | + </tr> |
| 190 | + </thead> |
| 191 | + <tbody> |
| 192 | + {{#each (sortBy this.sortAssociations this.associations) as |association|}} |
| 193 | + <tr> |
| 194 | + <td>{{association.school.title}}</td> |
| 195 | + <td colspan="3"> |
| 196 | + <LinkTo @route="course" @model={{association.course}}> |
| 197 | + {{association.course.title}} |
| 198 | + {{#if this.academicYearCrossesCalendarYearBoundaries}} |
| 199 | + ({{association.course.year}} |
| 200 | + - |
| 201 | + {{add association.course.year 1}}) |
| 202 | + {{else}} |
| 203 | + ({{association.course.year}}) |
| 204 | + {{/if}} |
| 205 | + </LinkTo> |
| 206 | + </td> |
| 207 | + <td colspan="3"> |
| 208 | + <ul class="sessions-list"> |
| 209 | + {{#each association.sessions as |session|}} |
| 210 | + <li data-test-session> |
| 211 | + <LinkTo @route="session" @models={{array session.course session}}> |
| 212 | + {{session.title}} |
| 213 | + </LinkTo> |
| 214 | + </li> |
| 215 | + {{/each}} |
| 216 | + </ul> |
| 217 | + </td> |
| 218 | + </tr> |
| 219 | + {{/each}} |
| 220 | + </tbody> |
| 221 | + </table> |
| 222 | + </div> |
| 223 | + {{/if}} |
| 224 | + {{/if}} |
| 225 | + </section> |
| 226 | + {{/let}} |
| 227 | + </template> |
| 228 | +} |
0 commit comments