Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new degrees config format #285

Merged
merged 14 commits into from
Jun 7, 2024
2 changes: 1 addition & 1 deletion src/config
81 changes: 70 additions & 11 deletions src/lib/teachings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ export type Teaching = {
degree?: string;
};

export type YearStudyDiagram = {
mandatory?: string[];
electives?: string[];
};

export type Year = {
year: number;
chat?: string;
teachings: YearStudyDiagram;
};

export type DegreeTeaching = {
name: string;
year?: number;
mandatory: boolean;
};

export type Degree = {
id: string;
name: string;
icon: string;
teachings?: DegreeTeaching[];
years?: Year[];
chat?: string;
};
Expand All @@ -42,11 +43,69 @@ export async function isTeachingActive(fetch: typeof window.fetch, teaching: Tea
return isTeachingActiveFromName(fetch, teaching.url);
}

export function yearToFlatTeachings(y: Year): string[] {
export function yearToFlatTeachings(d: Degree, y: number): string[] {
let res: string[] = [];

if (d.teachings) {
d.teachings.forEach((x) => {
if ((x.year && x.year == y) || (!x.year && y == 0)) res = res.concat(x.name);
});
}

return res;
}

export function allMandatoryTeachingsFromYear(d: Degree, y: number): string[] {
let res: string[] = [];

if (d.teachings) {
d.teachings.forEach((x) => {
if ((x.year && x.year == y) || (!x.year && y == 0)) {
if (x.mandatory) {
res = res.concat(x.name);
}
}
});
}

return res;
}

export function allElectivesTeachingsFromYear(d: Degree, y: number): string[] {
let res: string[] = [];

if (d.teachings) {
d.teachings.forEach((x) => {
if ((x.year && x.year == y) || (!x.year && y == 0)) {
if (!x.mandatory) {
res = res.concat(x.name);
}
}
});
}

return res;
}
samuelemusiani marked this conversation as resolved.
Show resolved Hide resolved

export function allTeachingsMandatoryElectives(d: Degree, mandatory: boolean): string[] {
let res: string[] = [];
const studyDiagram = y.teachings;
if (studyDiagram.mandatory) res = res.concat(studyDiagram.mandatory);
if (studyDiagram.electives) res = res.concat(studyDiagram.electives);
if (d.teachings) {
d.teachings.forEach((x) => {
if (x.mandatory == mandatory) {
res = res.concat(x.name);
}
});
}
return res;
}

export function allTeachingsToString(d: Degree): string[] {
let res: string[] = [];

if (d.teachings) {
d.teachings.forEach((x) => (res = res.concat(x.name)));
}
samuelemusiani marked this conversation as resolved.
Show resolved Hide resolved

return res;
}

Expand All @@ -56,7 +115,7 @@ export async function getActiveTeachings(
): Promise<string[]> {
const years = degree.years;
if (!years) return [];
const allTeachings = years.flatMap(yearToFlatTeachings);
const allTeachings = allTeachingsToString(degree);
const activeTeachings = await filterAsync(allTeachings, (t) =>
isTeachingActiveFromName(fetch, t)
);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
</div>
<ul class="menu p-2 text-lg">
{#each data.degrees as degree}
{#if degree.years}
{#if degree.teachings}
samuelemusiani marked this conversation as resolved.
Show resolved Hide resolved
<Line name={degree.name} icon={degree.icon} href="{base}/dash/{degree.id}" />
{:else}
<Line name={degree.name} icon={degree.icon} href="{base}/{degree.id}" />
Expand Down
17 changes: 5 additions & 12 deletions src/routes/[...dir]/[zfile=dir]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { page } from '$app/stores';

import Line from '$lib/components/Line.svelte';
import type { Degree, Year } from '$lib/teachings';
import { allTeachingsMandatoryElectives, type Degree } from '$lib/teachings';
import { EDIT_URLS } from '$lib/const';
import { doneFiles, anyFileDone } from '$lib/todo-file';

Expand Down Expand Up @@ -70,19 +70,12 @@
reverseMode = !reverseMode;
}

// Computes either all mandatory teachings or elective teachings for a year
function getTeachings(y: Year, electives: boolean): string[] | undefined {
if (!y) return undefined;
const studyDiagram = y.teachings;
if (!studyDiagram) return undefined;
return electives ? studyDiagram.electives : studyDiagram.mandatory;
}

// Checks if a teaching is part of a certain degree
function isInDegree(teachingName: string, degree: Degree, elective: boolean): boolean {
const years = degree.years;
if (!years) return false;
return !!years.find((y) => getTeachings(y, elective)?.includes(teachingName));
const t = degree.teachings;
if (!t) return false;
samuelemusiani marked this conversation as resolved.
Show resolved Hide resolved

return allTeachingsMandatoryElectives(degree, !elective).includes(teachingName);
}

// Skims through degrees looking for a given teaching
Expand Down
8 changes: 4 additions & 4 deletions src/routes/build/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
<hr class="my-8 border-primary" />
{/if}
<h2 class="text-center text-2xl">{degree.name}</h2>
{#if degree.years}
{#each degree.years as year}
<h3 class="text-center text-xl font-bold my-4">{year.year}</h3>
{#if degree.teachings}
samuelemusiani marked this conversation as resolved.
Show resolved Hide resolved
{#each [1, 2, 3] as year}
<h3 class="text-center text-xl font-bold my-4">{year}</h3>
<div class="grid grid-cols-4 gap-4">
{#each yearToFlatTeachings(year) as teaching}
{#each yearToFlatTeachings(degree, year) as teaching}
<div>
<h4 class="font-bold">{teaching}</h4>
{#if data.activeTeachings[i].includes(teaching)}
Expand Down
19 changes: 12 additions & 7 deletions src/routes/dash/[course]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
import type { PageData } from './$types';
import { page } from '$app/stores';
import { onMount } from 'svelte';
import type { Degree, Teaching } from '$lib/teachings';
import {
allMandatoryTeachingsFromYear,
allElectivesTeachingsFromYear,
type Degree,
type Teaching
} from '$lib/teachings';
import { getLoginUrl, getWhoAmI } from '$lib/upld';
import ListTeaching from './ListTeaching.svelte';
import type { TeachingsBatch } from './ListTeaching.svelte';
Expand All @@ -25,16 +30,16 @@
}

function reorganizeTeachings(degree: Degree) {
if (!degree.years) return { mandatory: [], electives: [] };
if (!degree.teachings) return { mandatory: [], electives: [] };
const mandatory: TeachingsBatch[] = [];
const electives: TeachingsBatch[] = [];

for (const year of degree.years) {
const m = year.teachings.mandatory;
const e = year.teachings.electives;
for (let i = 0; i <= 3; i++) {
samuelemusiani marked this conversation as resolved.
Show resolved Hide resolved
const m = allMandatoryTeachingsFromYear(degree, i);
const e = allElectivesTeachingsFromYear(degree, i);

if (m) mandatory.push({ year: year.year, teachings: namesToTeachings(m) });
if (e) electives.push({ year: year.year, teachings: namesToTeachings(e) });
if (m) mandatory.push({ year: i, teachings: namesToTeachings(m) });
samuelemusiani marked this conversation as resolved.
Show resolved Hide resolved
if (e) electives.push({ year: i, teachings: namesToTeachings(e) });
samuelemusiani marked this conversation as resolved.
Show resolved Hide resolved
}

return { mandatory, electives };
Expand Down
Loading