/**
* AJAX Transcript Loader
*
* @author David Miles
* @created 12/17/2010
*/
/**
* Console fix
*/
if(console === undefined){
var console = {log: function(msg){ alert("Console: " + msg); }};
}
/**
* Page load event
*/
window.onload = function(){
//document.getElementById("game").appendChild(new SuperTapper.Game());
var ajaxRequest = new AjaxRequest();
ajaxRequest.get("xml/transcript.xml", function(){
if(ajaxRequest.valid()){
if(ajaxRequest.xml()){
var transcript = ajaxRequest.xml();
// Get the student
var studentXml = transcript.getElementsByTagName("studentInfo")[0];
var student = {
name: studentXml.firstChild.firstChild.nodeValue,
id: studentXml.firstChild.nextSibling.firstChild.nodeValue,
program: studentXml.firstChild.nextSibling.nextSibling.firstChild.nodeValue,
school: studentXml.lastChild.firstChild.nodeValue
};
// Get the semesters
var semestersXml = transcript.getElementsByTagName("semester");
var semesters = [];
for(var i = 0; i < semestersXml.length; i++){
// Get the courses for this semester
var coursesXml = semestersXml[i].getElementsByTagName("course");
var courses = [];
for(var j = 0; j < coursesXml.length; j++){
var courseXml = coursesXml[j];
var course = {
id: courseXml.firstChild.firstChild.nodeValue,
name: courseXml.firstChild.nextSibling.firstChild.nodeValue,
grade: courseXml.lastChild.firstChild.nodeValue
}
courses.push(course);
}
var semester = {
id: semestersXml[i].getAttribute("id"),
courses: courses
};
semesters.push(semester);
}
// Now, create some tables
var table, tbody, tr, th, ht, td;
// Reset the transcript container's HTML
document.getElementById("transcript").innerHTML = "";
/*
* STUDENT TABLE
*/
table = document.createElement("table");
tbody = document.createElement("tbody");
// Student ID
tr = document.createElement("tr");
th = document.createElement("th");
td = document.createElement("td");
th.appendChild(document.createTextNode("Student ID"));
td.appendChild(document.createTextNode(student.id));
tr.appendChild(th);
tr.appendChild(td);
tbody.appendChild(tr);
// Student name
tr = document.createElement("tr");
th = document.createElement("th");
td = document.createElement("td");
th.appendChild(document.createTextNode("Student name"));
td.appendChild(document.createTextNode(student.name));
tr.appendChild(th);
tr.appendChild(td);
tbody.appendChild(tr);
// Student school
tr = document.createElement("tr");
th = document.createElement("th");
td = document.createElement("td");
th.appendChild(document.createTextNode("School"));
td.appendChild(document.createTextNode(student.school));
tr.appendChild(th);
tr.appendChild(td);
tbody.appendChild(tr);
// Student program
tr = document.createElement("tr");
th = document.createElement("th");
td = document.createElement("td");
th.appendChild(document.createTextNode("Program"));
td.appendChild(document.createTextNode(student.program));
tr.appendChild(th);
tr.appendChild(td);
tbody.appendChild(tr);
// Add the body
table.appendChild(tbody);
// Finally, add the table
document.getElementById("transcript").appendChild(table);
// GPA
var totalGpa = 0.0;
var numCourses = 0;
/*
* SEMESTERS
*/
var semesterContainer = document.createElement("div");
for(var i = 0; i < semesters.length; i++){
var semester = semesters[i];
var h1 = document.createElement("h1");
h1.appendChild(document.createTextNode("Semester #" + semester.id));
var hr = document.createElement("hr");
semesterContainer.appendChild(h1);
semesterContainer.appendChild(hr);
// Course table
table = document.createElement("table");
tbody = document.createElement("tbody");
// Course header
tr = document.createElement("tr");
th = document.createElement("th");
th.appendChild(document.createTextNode("Course ID"));
tr.appendChild(th);
th = document.createElement("th");
th.appendChild(document.createTextNode("Course name"));
tr.appendChild(th);
th = document.createElement("th");
th.appendChild(document.createTextNode("Course grade"));
tr.appendChild(th);
tbody.appendChild(tr);
/*
* COURSES
*/
for(var k = 0; k < semester.courses.length; k++){
var course = semester.courses[k];
// For GPA calculation
numCourses++;
totalGpa += parseFloat(course.grade);
// Course data
tr = document.createElement("tr");
td = document.createElement("td");
td.appendChild(document.createTextNode(course.id));
tr.appendChild(td);
td = document.createElement("td");
td.appendChild(document.createTextNode(course.name));
tr.appendChild(td);
td = document.createElement("td");
td.appendChild(document.createTextNode(course.grade));
tr.appendChild(td);
tbody.appendChild(tr);
}
table.appendChild(tbody);
semesterContainer.appendChild(table);
}
document.getElementById("transcript").appendChild(semesterContainer);
// Calculate and then show the GPA
// Note: I could have stored the student table and went back and added this into the student information,
// or even calculated it while creating the semesters array -- but I didn't. And I don't have enough time
// to go back and do anything.
var overallGpa = totalGpa / numCourses;
var gpaContainer = document.createElement("div");
gpaContainer.setAttribute("id", "gpaContainer");
gpaContainer.appendChild(document.createTextNode("Overall GPA: " + overallGpa));
document.getElementById("transcript").appendChild(gpaContainer);
}else{
document.getElementById("transcript").innerHTML = "There was a problem while loading the transcript.";
}
}
});
};
/**
* Ajax Request class
*/
function AjaxRequest(){
// Request variable
this.req = null;
// Create the request variable
if(window.XMLHttpRequest){
this.req = new XMLHttpRequest();
}else{
if(window.ActiveXObject){
try {
this.req = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
}
/**
* Checks if the request object is valid
*
* @return boolean
*/
AjaxRequest.prototype.valid = function(){
return this.req.readyState == 4 && this.req.status == 200;
};
/**
* Get the XML response
*
* @return xml
*/
AjaxRequest.prototype.xml = function(){
return this.req.responseXML;
}
/**
* Get the text response
*
* @return string
*/
AjaxRequest.prototype.text = function(){
return this.req.responseText;
}
/**
* Send a get request
*
* @param string url URL to send a request to
* @param function handler Function to handle this request
*/
AjaxRequest.prototype.get = function(url, handler){
this.req.onreadystatechange = handler;
this.req.open("GET", url, true);
this.req.send(null);
};
/**
* Send a post request
*
* @param string url URL to send a request to
* @param function handler Function to handle this request
*/
AjaxRequest.prototype.post = function(url, handler){
// TODO: Implement AJAX POST requests
};