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

firstModule detection fix #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 63 additions & 49 deletions src/components/module_parser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Update the base module type list for other studies than information science.
*
* Sometimes the same module can have a different module type depending on the study (I, WI, ICS, DI etc).
*/
* Update the base module type list for other studies than information science.
*
* Sometimes the same module can have a different module type depending on the study (I, WI, ICS, DI etc).
*/
const updateModuleTypeList = async (oldModuleTypeList, jsonFilePath) => {
let patch = await fetch(Helpers.getExtensionInternalFileUrl(jsonFilePath))
.then(response => response.json());
Expand All @@ -11,22 +11,36 @@ const updateModuleTypeList = async (oldModuleTypeList, jsonFilePath) => {

const ModuleParser = {

/**
* Check if a module is an info module
* Info modules contain INFO in their name
*/
isNotInfoSemester: (hsluModuleName) => {
return !hsluModuleName.includes('INFO')
},
Comment on lines +18 to +20
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a method returns a bool, always think of what it should check, not what it shouldn't. This makes the code more readable.

Suggested change
isNotInfoSemester: (hsluModuleName) => {
return !hsluModuleName.includes('INFO')
},
isInfoModule: (hsluModuleName) => {
return hsluModuleName.includes('INFO')
},


/**
* Check if a module was done in Autumn.
* Modules are marked with 'H' for 'Herbstsemester' (autumn)
* or 'F' for 'Frühlingssemester' (spring).
*/
isAutumnSemester: (hsluModuleName) => {
const includesH = hsluModuleName.split('.')[2].includes('H');
const includesF = hsluModuleName.split('.')[2].includes('F');
return includesH || includesF ? includesH : undefined;
return hsluModuleName.split('.')[2].includes('H') && ModuleParser.isNotInfoSemester(hsluModuleName);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return hsluModuleName.split('.')[2].includes('H') && ModuleParser.isNotInfoSemester(hsluModuleName);
return hsluModuleName.split('.')[2].includes('H') && !ModuleParser.isInfoModule(hsluModuleName);

},

/**
* Check if a module was done in Spring.
* Modules are marked with 'F' for 'Frühlingssemester' (spring).
*/
isSpringSemester: (hsluModuleName) => {
return hsluModuleName.split('.')[2].includes('F') && ModuleParser.isNotInfoSemester(hsluModuleName);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return hsluModuleName.split('.')[2].includes('F') && ModuleParser.isNotInfoSemester(hsluModuleName);
return hsluModuleName.split('.')[2].includes('F') && !ModuleParser.isInfoModule(hsluModuleName);

},

/**
* Calculate the semester.
*
* @param {string} hsluModuleName: is the 'anlassnumber',
* e.g. 'I.BA_BCHAIN.H1901'.
* @param firstModule
*/
calculateSemester: (hsluModuleName, firstModule) => {
let semester = undefined;
Expand Down Expand Up @@ -55,34 +69,32 @@ const ModuleParser = {
if (isStartInAutumn) {
if (isModuleInAutumn) {
semester = yearDifference * 2 + 1;
}
else {
} else {
semester = yearDifference * 2;
}
} else {
if (isModuleInAutumn) {
semester = yearDifference * 2 + 2;
}
else {
} else {
semester = yearDifference * 2 + 1;
}
}
return semester;
},

/**
* The module name includes the module id.
* But there are different formats for the module names.
*
* Modules that are parsed:
* Usually a module name looks like this: I.BA_IPCV.F1901
* There are modules that have a suffix after a second underscore: I.BA_AISO_E.F1901
*
* Modules that are not parsed:
* There are modules without underscores in the name: I.ANRECHINDIVID.F1901
* Only the ANRECHINDIVID module is known to have this format.
* Probably counted as 'Erweiterungsmodul'.
*/
* The module name includes the module id.
* But there are different formats for the module names.
*
* Modules that are parsed:
* Usually a module name looks like this: I.BA_IPCV.F1901
* There are modules that have a suffix after a second underscore: I.BA_AISO_E.F1901
*
* Modules that are not parsed:
* There are modules without underscores in the name: I.ANRECHINDIVID.F1901
* Only the ANRECHINDIVID module is known to have this format.
* Probably counted as 'Erweiterungsmodul'.
*/
getModuleIdFromModuleName: (moduleName) => {

let name = String(moduleName);
Expand All @@ -97,13 +109,11 @@ const ModuleParser = {
name = name.split('.')[0];
}
return name;
}
else if (name.includes('.')) {
} else if (name.includes('.')) {

// for module names like I.ANRECHINDIVID.F1901
return name.split('.')[1];
}
else {
} else {
console.log('module id not parseable: ' + moduleName);
return null;
}
Expand All @@ -124,14 +134,12 @@ const ModuleParser = {
* Only if all of them are matching in the right order test() returns true
* */
const moduleNameRegex = /^(\w+)\.(.+)\.[FH]\d{4}/g;
if (moduleNameRegex.test(moduleName)) {
return true
}
return false
return moduleNameRegex.test(moduleName);

},
/**
* Generates an array of module objects using the API and the module type mapping json file.
*/
* Generates an array of module objects using the API and the module type mapping json file.
*/
generateModuleObjects: async (studyAcronym) => {

const API_URL = "https://mycampus.hslu.ch/de-ch/api/anlasslist/load/?page=1&per_page=69&total_entries=69&datasourceid=5158ceaf-061f-49aa-b270-fc309c1a5f69";
Expand All @@ -154,10 +162,21 @@ const ModuleParser = {
return;
}

firstModule = anlasslistApiResponse.items
let firstSpringModule = anlasslistApiResponse.items
.slice()
.reverse()
.find(modul => ModuleParser.isAutumnSemester(modul.anlassnumber) != undefined);
.find(modul => ModuleParser.isSpringSemester(modul.anlassnumber));

let firstAutumnModule = anlasslistApiResponse.items
.slice()
.reverse()
.find(modul => ModuleParser.isAutumnSemester(modul.anlassnumber));

if (new Date(firstAutumnModule.from).getFullYear() < (new Date(firstSpringModule.from).getFullYear())) {
firstModule = firstAutumnModule
} else {
firstModule = firstSpringModule
}

const passedMessage = await i18n.getMessage("Bestanden");
const ignoreInStatsModules = (await Helpers.getItemFromLocalStorage("ignoreInStatsModules")).ignoreInStatsModules;
Expand All @@ -167,22 +186,19 @@ const ModuleParser = {
let parsedModule = {};
if (ModuleParser.validateModuleName(item.anlassnumber)) {
parsedModule.semester = ModuleParser.calculateSemester(item.anlassnumber, firstModule);
}
else {
} else {
console.log("Not valid modulename: ", item.anlassnumber);
parsedModule.semester = undefined
}

let passed = item.prop1[0].text == 'Erfolgreich teilgenommen';
let passed = item.prop1[0].text === 'Erfolgreich teilgenommen';
parsedModule.passed = passed;

if (item.note != null) {
parsedModule.mark = item.note;
}
else if (passed) {
} else if (passed) {
parsedModule.mark = passedMessage;
}
else {
} else {
parsedModule.mark = 'n/a';
}
parsedModule.grade = item.grade === null ? 'n/a' : item.grade;
Expand All @@ -200,18 +216,16 @@ const ModuleParser = {
}

// sets the UseInStats to true by default
parsedModule.UseInStats = true;
if (ignoreInStatsModules != undefined && ignoreInStatsModules[parsedModule.name]) {
parsedModule.UseInStats = false;
}

parsedModule.UseInStats = !(ignoreInStatsModules !== undefined && ignoreInStatsModules[parsedModule.name]);
myCampusModulesList[parsedModule.name] = {
acronym: parsedModule.name,
}

modules.push(parsedModule);
});
// Save modules from MyCampus to local storage for the popup
await Helpers.saveObjectInLocalStorage({ hsluModules: myCampusModulesList })
await Helpers.saveObjectInLocalStorage({hsluModules: myCampusModulesList})

//add custom modules from local storage
let moduleList = (await Helpers.getItemFromLocalStorage("moduleList")).moduleList
Expand All @@ -220,7 +234,7 @@ const ModuleParser = {
const customModule = moduleList[customModuleName];

let parsedModule = {};
parsedModule.passed = customModule.grade == 'F' ? false : true;
parsedModule.passed = customModule.grade !== 'F';
parsedModule.mark = customModule.mark;
parsedModule.grade = customModule.grade;
parsedModule.type = customModule.type;
Expand Down