function DBService() { this.path = this.getPath(); this.directoryName = this.getDirectoryName(this.path); this.webPath = this.getWebPath(this.directoryName); this.maestroID = 0; this.playerID = 0; this.appName = ""; this.dateTime = null; } DBService.prototype.getPath = function() { return window.location.pathname; } DBService.prototype.getDirectoryName = function(path) { var pathAndFileNames = path.split("/"); return pathAndFileNames[pathAndFileNames.length - 2]; } DBService.prototype.getWebPath = function(directoryName) { if(directoryName === "test") // mouse_typing/test/ return "../src/web/"; else // mousetyping/src/web/client/ return "../"; } DBService.prototype.setMaestroID = function(maestroID) { this.maestroID = maestroID; } DBService.prototype.setPlayerID = function(playerID) { this.playerID = playerID; } DBService.prototype.setAppName = function(appName) { this.appName = appName; } DBService.prototype.setDateTime = function(date, time) { this.dateTime = date + time; } DBService.prototype.resetDateTime = function() { this.dateTime = null; } DBService.prototype.makeXhr = function(filepath, onreadystatechange) { var xhr = new XMLHttpRequest(); xhr.open("POST", this.webPath + filepath, true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = onreadystatechange; return xhr; } // tdd setup DBService.prototype.tddSetup = function(command, onSucceededListener, onFailedListener) { var xhr = this.makeXhr( "php/db/tdd_setup.php", (function() { // onreadystatechange if(xhr.readyState == 4 && xhr.status == 200) { var replyJSON = JSON.parse(xhr.responseText); if(replyJSON != null) onSucceededListener(replyJSON); else onFailedListener(replyJSON); } }).bind(this) ); xhr.send("command=" + command); } DBService.prototype.tddRun = function(command, onSucceededListener, onFailedListener) { var xhr = this.makeXhr( "php/db/tdd_run.php", (function() { // onreadystatechange if(xhr.readyState == 4 && xhr.status == 200) { var replyJSON = JSON.parse(xhr.responseText); if(replyJSON != null) onSucceededListener(replyJSON); else onFailedListener(replyJSON); } }).bind(this) ); xhr.send("command=" + command); } // writing DBService.prototype.requestWritingPlayerList = function(onSucceededListener, onFailedListener) { var xhr = this.makeXhr( "php/writing/player_list.php", (function() { // onreadystatechange if(xhr.readyState == 4 && xhr.status == 200) { var replyJSON = JSON.parse(xhr.responseText); if(replyJSON != null) onSucceededListener(replyJSON); else onFailedListener(replyJSON); } }).bind(this) ); xhr.send("maestroID=" + this.maestroID + "&playerID=" + this.playerID); } DBService.prototype.requestWritingInfo = function(writingID, onSucceededListener, onFailedListener) { var xhr = this.makeXhr( "php/writing/writing_info.php", (function() { // onreadystatechange if(xhr.readyState == 4 && xhr.status == 200) { var replyJSON = JSON.parse(xhr.responseText); if(replyJSON != null) onSucceededListener(replyJSON); else onFailedListener(replyJSON); } }).bind(this) ); xhr.send("writingID=" + writingID); } // typing exam DBService.prototype.addPrevHourTypingExamRecord = function(writingID, record, onSucceededListener, onFailedListener) { var xhr = this.makeXhr( "php/writing/update_result_record.php", (function() { // onreadystatechange if(xhr.readyState == 4 && xhr.status == 200) { var replyJSON = JSON.parse(xhr.responseText); if(replyJSON != null) onSucceededListener(replyJSON); else onFailedListener(replyJSON); } }).bind(this) ); xhr.send( "maestroID=" + this.maestroID + "&playerID=" + this.playerID + "&writingID=" + writingID + "&record=" + record + "&isPrevHourFlag=true" ); } DBService.prototype.updateTypingExamRecord = function(writingID, record, onSucceededListener, onFailedListener) { var xhr = this.makeXhr( "php/writing/update_result_record.php", (function() { // onreadystatechange if(xhr.readyState == 4 && xhr.status == 200) { var replyJSON = JSON.parse(xhr.responseText); if(replyJSON != null) onSucceededListener(replyJSON); else onFailedListener(replyJSON); } }).bind(this) ); xhr.send( "maestroID=" + this.maestroID + "&playerID=" + this.playerID + "&writingID=" + writingID + "&record=" + record ); } DBService.prototype.getTypingExamHighestRecord = function(writingID, onSucceededListener, onFailedListener) { var xhr = this.makeXhr( "php/writing/get_highest_record.php", (function() { // onreadystatechange if(xhr.readyState == 4 && xhr.status == 200) { var replyJSON = JSON.parse(xhr.responseText); if(replyJSON != null) onSucceededListener(replyJSON); else onFailedListener(replyJSON); } }).bind(this) ); xhr.send( "maestroID=" + this.maestroID + "&playerID=" + this.playerID + "&writingID=" + writingID ); }