Skip to content

Commit

Permalink
chore: new degrees config format (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelemusiani authored Jun 7, 2024
1 parent 9ad2867 commit a0451c0
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/config
1 change: 1 addition & 0 deletions src/lib/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export const EDIT_URLS = (path: string) => {
github_dev: `${GH_DEV_BASE_URL}/${repo}/blob/main/${filePath}`
};
};
export const MAX_YEARS_FOR_DEGREE = 3;
32 changes: 20 additions & 12 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,21 +43,28 @@ export async function isTeachingActive(fetch: typeof window.fetch, teaching: Tea
return isTeachingActiveFromName(fetch, teaching.url);
}

export function yearToFlatTeachings(y: Year): string[] {
export function teachingsFilter(d: Degree, year?: number, 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);

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

return res;
}

export async function getActiveTeachings(
fetch: typeof window.fetch,
degree: Degree
): Promise<string[]> {
const years = degree.years;
const years = degree.teachings;
if (!years) return [];
const allTeachings = years.flatMap(yearToFlatTeachings);
const allTeachings = teachingsFilter(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 != null}
<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
27 changes: 9 additions & 18 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 { teachingsFilter, type Degree } from '$lib/teachings';
import { EDIT_URLS } from '$lib/const';
import { doneFiles, anyFileDone } from '$lib/todo-file';
Expand Down Expand Up @@ -70,25 +70,16 @@
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));
if (degree.teachings != null) return false;
return teachingsFilter(degree, undefined, !elective).includes(teachingName);
}
// Skims through degrees looking for a given teaching
function skimDegrees(teachingName: string, electives: boolean): string | undefined {
const degree = data.degrees.find((d) => isInDegree(teachingName, d, electives));
return degree ? degree.id : undefined;
return degree != null ? degree.id : undefined;
}
// Picks a containing degree for this teaching
Expand All @@ -100,10 +91,10 @@
if (teaching?.degree) return teaching.degree;
// Plan C: any degree featuring this teaching as mandatory
const mandatoryDegree = skimDegrees(teachingName, false);
if (mandatoryDegree) return mandatoryDegree;
if (mandatoryDegree != null) return mandatoryDegree;
// Plan D: any degree featuring this teaching as an elective
const electiveDegree = skimDegrees(teachingName, true);
if (electiveDegree) return electiveDegree;
if (electiveDegree != null) return electiveDegree;
// Plan E: give up
return null;
}
Expand Down Expand Up @@ -150,7 +141,7 @@
<span class="text-xl icon-[akar-icons--home-alt1]"></span>
</a>
</li>
{#if degree}
{#if degree != null}
<li>
<a class="flex items-center" href={'/dash/' + degree}>
<span class="text-xl icon-[ic--round-school]"></span>
Expand Down Expand Up @@ -207,7 +198,7 @@
</div>

<div class="grid gap-5 grid-cols-dir md:grid-cols-dir-full mx-4 text-lg">
{#if data.manifest.directories}
{#if data.manifest.directories != null}
{@const directories = data.manifest.directories.sort((a, b) => a.name.localeCompare(b.name))}
{#if !reverseMode}
{#each directories.reverse() as dir}
Expand All @@ -219,7 +210,7 @@
{/each}
{/if}
{/if}
{#if data.manifest.files}
{#if data.manifest.files != null}
{@const files = data.manifest.files.sort((a, b) => a.name.localeCompare(b.name))}
{#if !reverseMode}
{#each files.reverse() as file}
Expand Down
13 changes: 7 additions & 6 deletions src/routes/build/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import type { PageData } from './$types';
import { yearToFlatTeachings } from '$lib/teachings';
import { teachingsFilter } from '$lib/teachings';
import { MAX_YEARS_FOR_DEGREE } from '$lib/const';
const WORKFLOW_NAMES = ['filenames', 'build-and-deploy'];
const WORKFLOW_URL = (project: string, workflow: string) =>
Expand All @@ -26,18 +27,18 @@
<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 != null}
{#each [...Array(MAX_YEARS_FOR_DEGREE).keys()].map((i) => i + 1) 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 teachingsFilter(degree, year) as teaching}
<div>
<h4 class="font-bold">{teaching}</h4>
{#if data.activeTeachings[i].includes(teaching)}
<div class="flex gap-2">
{#each WORKFLOW_NAMES as workflow}
{@const url = data.teachings.get(teaching)?.url}
{#if url}
{#if url != null}
{@const href = WORKFLOW_URL(url, workflow)}
{@const src = `${href}/badge.svg`}
<a {href}>
Expand Down
19 changes: 10 additions & 9 deletions src/routes/dash/[course]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import type { PageData } from './$types';
import { page } from '$app/stores';
import { onMount } from 'svelte';
import type { Degree, Teaching } from '$lib/teachings';
import { teachingsFilter, type Degree, type Teaching } from '$lib/teachings';
import { getLoginUrl, getWhoAmI } from '$lib/upld';
import ListTeaching from './ListTeaching.svelte';
import type { TeachingsBatch } from './ListTeaching.svelte';
import { RISORSE_BASE_URL } from '$lib/const';
import { MAX_YEARS_FOR_DEGREE, RISORSE_BASE_URL } from '$lib/const';
export let data: PageData;
let activeYears: string[] = [];
Expand All @@ -25,16 +25,17 @@
}
function reorganizeTeachings(degree: Degree) {
if (!degree.years) return { mandatory: [], electives: [] };
if (degree.teachings != null && degree.teachings.length == 0)
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 <= MAX_YEARS_FOR_DEGREE; i++) {
const m = teachingsFilter(degree, i, true);
const e = teachingsFilter(degree, i, false);
if (m) mandatory.push({ year: year.year, teachings: namesToTeachings(m) });
if (e) electives.push({ year: year.year, teachings: namesToTeachings(e) });
if (m != null && m.length != 0) mandatory.push({ year: i, teachings: namesToTeachings(m) });
if (e != null && e.length != 0) electives.push({ year: i, teachings: namesToTeachings(e) });
}
return { mandatory, electives };
Expand All @@ -54,7 +55,7 @@
<div class="max-w-5xl p-4 mx-auto">
<nav class="navbar flex bg-base-200 text-neutral-content rounded-box shadow-sm px-5 mb-5">
<div class="navbar-start">
{#if login}
{#if login != null}
{#await login then login}
{#if 'error' in login}
<a class="btn btn-square btn-ghost" href={getLoginUrl($page.url)}> Login </a>
Expand Down
3 changes: 2 additions & 1 deletion src/routes/dash/[course]/ListTeaching.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
<ul class="menu p-2">
{#each years as year}
{#if year.teachings.length > 0}
{@const type = year.year != 0 ? year.year.toString() + ' anno' : 'Generali'}
<li class="menu-title">
<span class="text-2xl mt-5 italic">{year.year} anno {title}</span>
<span class="text-2xl mt-5 italic">{type} {title}</span>
</li>
<div class="divider mt-0"></div>
<div class="flex flex-row flex-wrap">
Expand Down

0 comments on commit a0451c0

Please sign in to comment.