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

Added function to calculate cumulative GPA(including unfinished semesters) #140

Merged
merged 6 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
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
25 changes: 14 additions & 11 deletions src/js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function calculate_gpa (courses) {
let sum = 0;
for (var i = 0; i < courses.length; i++) {
if (gradeToGPA(courses[i].grade) !== -1) {
const multiplier = total_add(courses[i].name);
const multiplier = calculate_credit_hours(courses[i].name);
courses_with_grades += multiplier;
sum += multiplier * (gradeToGPA(courses[i].grade) + course_boost(courses[i].name, courses[i].grade));
}
Expand All @@ -113,16 +113,7 @@ function calculate_gpa (courses) {
return '0.00';
}
return (sum / courses_with_grades).toFixed(2);
function total_add (course_name) {
const double_effect_courses = [`English 10/American History`, `English 9/World History`];
if (double_effect_courses.includes(course_name)) {
return 2;
}
if (/^(I Service: |IS: )/.test(course_name)) {
return 0.5;
}
return 1;
}

function course_boost (course_name, grade) {
if (gradeToGPA(grade) < 1.8) {
return 0;
Expand All @@ -134,6 +125,17 @@ function calculate_gpa (courses) {
}
}

function calculate_credit_hours (course_name) {
const double_effect_courses = [`English 10/American History`, `English 9/World History`];
if (double_effect_courses.includes(course_name)) {
return 2;
}
if (/^(I Service: |IS: )/.test(course_name)) {
return 0.5;
}
return 1;
}

/**
* Extract the final percent from the course page html.
* @param {String} html course page html
Expand Down Expand Up @@ -184,4 +186,5 @@ export {
calculate_gpa,
extractFinalPercent,
assignments,
calculate_credit_hours,
};
12 changes: 12 additions & 0 deletions src/js/models/Course.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
import { calculate_credit_hours } from '../helpers';

export default class Course {
#name;
#grade;
#finalPercent;
#link;
#assignments;
#creditHour;

/**
* Create new course instance
Expand All @@ -37,19 +39,25 @@ export default class Course {
* @param {String} [grade] current grade for the course
* @param {Number?} [finalPercent] current final percent of the course
* @param {Assignment[]} [assignments] number of missing assignments
* @param {Number?} [creditHour] number of credit hours this course counts for, optional
*/
constructor (name, link, grade, finalPercent, assignments) {
this.#name = name;
this.#link = link;
this.#grade = grade;
this.#finalPercent = finalPercent;
this.#assignments = assignments;
this.#creditHour = calculate_credit_hours(this.#name);
}

get name () {
return this.#name;
}

get creditHour () {
return this.#creditHour;
gary-kim marked this conversation as resolved.
Show resolved Hide resolved
}

get link () {
return this.#link;
}
Expand All @@ -62,6 +70,10 @@ export default class Course {
this.#grade = g;
}

set creditHour (c) {
this.#creditHour = c;
}

get finalPercent () {
return this.#finalPercent;
}
Expand Down
94 changes: 94 additions & 0 deletions src/js/saspowerschoolff.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
*
* @copyright Copyright (c) 2018-2020 Gary Kim <gary@garykim.dev>
*
* @copyright Copyright (c) 2020 Suhas Hariharan <mail@suhas.net>
*
* @author Gary Kim <gary@garykim.dev>
*
* @license GNU AGPL version 3 only
Expand Down Expand Up @@ -70,6 +72,7 @@ function analytics_message (action_input) {
const href = window.location.href.split("?")[0];
browser.runtime.sendMessage({ action: "analytics_send", args: { url: href, action: action_input } });
}

function main_page () {
const student_name = document.querySelector('#userName').querySelector('span').innerText;
let second_semester = false;
Expand Down Expand Up @@ -128,6 +131,7 @@ function main_page () {
}
}
$("table[border='0'][cellpadding='3'][cellspacing='1'][width='100%']").prepend(`<tr><td align="center">Current Semester GPA (${second_semester ? 'S2' : 'S1'}): ${calculate_gpa(courses)}</td></tr>`);

if (second_semester) {
fetch("https://powerschool.sas.edu.sg/guardian/termgrades.html")
.then((response) => {
Expand All @@ -151,6 +155,11 @@ function main_page () {
}
});
}
$("table[border='0'][cellpadding='3'][cellspacing='1'][width='100%']").prepend(`<td style="background-color: white;" align="center"><button id="calculateCumulative">Calculate Cumulative GPA</button></td>`);
// passing courses in to possibly include current semester GPA if term has not finished yet.
$("#calculateCumulative").click(function () {
show_cumulative_gpa(courses);
});
// Hypo Grade Calculator
const HypoGradesDiv = document.createElement('div');
HypoGradesDiv.classList.add("hypo-grade-div-fixed");
Expand All @@ -162,6 +171,12 @@ function main_page () {
},
}).$mount(".hypo-grade-div-fixed");
}
function show_cumulative_gpa (courses) {
$("#calculateCumulative").hide();
calculate_cumulative_gpa(courses).then(cumulative_gpa => {
$("table[border='0'][cellpadding='3'][cellspacing='1'][width='100%']").prepend(`<tr><td align="center">Cumulative GPA(9-12): ${cumulative_gpa.toFixed(2)}</td></tr>`);
});
}
function class_page () {
// Show final percent
const number = extractFinalPercent($("table.linkDescList").html());
Expand All @@ -188,6 +203,85 @@ function login_page () {
});
}

function calculate_cumulative_gpa (current_courses) {
const list_of_gpas = [];
const all_courses = [];
const credit_hour_list = [];
let element_list = [];
const fetches = [];
// Fetches grade history page
const gpas = fetch("https://powerschool.sas.edu.sg/guardian/termgrades.html")
.then(response => response.text())
.then(data => {
const el = document.createElement("html");
el.innerHTML = data;
const tabs = el.getElementsByClassName("tabs")[0].getElementsByTagName("li");
// Iterate until the end of tabs or until no longer at a high school semester.
for (let i = 0; i < tabs.length && /HS$/.test(tabs[i].innerText); i++) {
fetches.push(
fetch(tabs[i].getElementsByTagName("a")[0].href)
.then(res => res.text())
.then(function (data2) {
let courses = [];
const semester = document.createElement("html");
semester.innerHTML = data2;
element_list = semester.getElementsByClassName("box-round")[0].getElementsByTagName("table")[0];
element_list = element_list.getElementsByTagName("tbody")[0].getElementsByTagName("tr");
for (let t = 2; t < element_list.length; t++) {
if (element_list[t].innerText.trim() === ("S2")) {
all_courses.push(courses);
courses = [];
}
if (element_list[t].getElementsByTagName("th").length > 0) {
continue;
} else {
const $prev_course = element_list[t];
// Creates course object with each course from grade history page
const course = new Course($prev_course.getElementsByTagName("td")[0].textContent.trim(), "",
$prev_course.getElementsByTagName("td")[1].textContent.trim(), 0, "");
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$prev_course.getElementsByTagName("td")[1].textContent.trim(), 0, "");
$prev_course.getElementsByTagName("td")[1].textContent.trim());


courses.push(course);
}
}
all_courses.push(courses);
}));
}
// Calculates cumulative GPA based on credit hours per semester and gpa for each semester.
let include_current_semester = false;
if (current_courses.length !== 0) {
for (let i = 0; i < current_courses.length; i++) {
if (current_courses[i].link.includes("begdate")) {
Copy link
Member

@gary-kim gary-kim Mar 17, 2020

Choose a reason for hiding this comment

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

The better way to do this would be something like

Suggested change
if (current_courses[i].link.includes("begdate")) {
if (new URL(current_courses[i].link).searchParams.get("begdate")) {

but we can deal with that during the cleanup if you'd like.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, thanks okay if I merge it now?

Copy link
Member

Choose a reason for hiding this comment

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

If the PR's approved by someone with write access, you don't need to ask :)

include_current_semester = true;
}
}
}
if (include_current_semester) {
all_courses.push(current_courses);
}
const cumulative_gpa = Promise.all(fetches).then(function () {
let total_count = 0;
let total_gpa = 0;
for (let i = 0; i < all_courses.length; i++) {
let count = 0;
list_of_gpas.push(calculate_gpa(all_courses[i]));
for (let t = 0; t < all_courses[i].length; t++) {
count += all_courses[i][t].creditHour;
}
// Adds all credit hours to overall credit hour count
total_count += count;
// Adds each amount of credit hours per semester to list
credit_hour_list.push(count);
}
for (let i = 0; i < all_courses.length; i++) {
total_gpa += ((credit_hour_list[i] / total_count) * list_of_gpas[i]);
}
return (total_gpa);
});
return cumulative_gpa;
});
return gpas;
}

function html2node (html_string) {
return html2nodelist(html_string)[0];
}
Expand Down