From 20b91161854394a12775de2d56a6027437bf0c3a Mon Sep 17 00:00:00 2001 From: chrqls Date: Fri, 25 Apr 2014 23:15:33 +0200 Subject: [PATCH] [#39] Trying to implement the preview IQSet feature but still not working (problem with id) --- static/js/smileteacher.js | 212 ++++++++++++++++++++++++-------------- static/smile-teacher.html | 13 ++- 2 files changed, 146 insertions(+), 79 deletions(-) diff --git a/static/js/smileteacher.js b/static/js/smileteacher.js index 5a22cda..139c202 100644 --- a/static/js/smileteacher.js +++ b/static/js/smileteacher.js @@ -38,6 +38,10 @@ var SMILEROUTES = { "createsession": "/smile/createsession" }; +var TEMP_IQSETS = []; +var TEMP_POSITION = 0; +//var READY = false; + /* -------- MODELS -------- */ @@ -54,7 +58,7 @@ var Question = function(position,urlImage,author,question,answer,options,ip,type this.type = type; } -var IQSet = function(position,id,title,teacherName,groupName,date,size) { +var IQSet = function(position,id,title,teacherName,groupName,date) { this.position = position; this.id = id; @@ -63,10 +67,10 @@ var IQSet = function(position,id,title,teacherName,groupName,date,size) { this.groupName = groupName; this.date = date; this.questions = []; - this.size = size; + //this.size = size; } -var TEMP_IQSET = new IQSet(); + var GlobalViewModel = { teacher_name: ko.observable(""), @@ -107,88 +111,61 @@ ko.extenders.required = function(target, overrideMessage) { /* --------- ACTIONS --------- */ -GlobalViewModel.createSession = function() { - - if (!this.teacher_name() || this.teacher_name() === "") { this.teacher_name('Default Teacher'); } - if (!this.session_name() || this.session_name() === "") { this.session_name('Default Session'); } - if (!this.group_name() || this.group_name() === "") { this.group_name('Default Group'); } - + +// Displays directly a "Recovering Session" button if a session is already running +GlobalViewModel.initializePage = function() { + $.ajax({ cache: false, - type: "POST", + type: "GET", dataType: "text", - url: SMILEROUTES["createsession"], - data: {"teacherName":GlobalViewModel.teacher_name,"groupName":GlobalViewModel.group_name,"sessionName":GlobalViewModel.session_name}, + url: SMILEROUTES["all"], + data: {}, error: function(xhr, text, err) { - smileAlert('#globalstatus', 'Unable to post session values. Reason: ' + xhr.status + ':' + xhr.responseText + '. Please verify your connection or server status.', 'trace'); + smileAlert('#globalstatus', 'Unable to call /smile/all. Reason: ' + xhr.status + ':' + xhr.responseText + '. Please verify your connection or server status.', 'trace'); }, success: function(data) { - switchSection('div[data-slug=choose-activity-flow]'); + + // If a session is already running, we replace the session values fields by a "recovering session" button + if(data.indexOf('SessionID') !== -1 ){ + switchSection('div[data-slug=recover-session]'); + } } }); - - return false; -} - -GlobalViewModel.recoverSession = function() { - - smileAlert('#globalstatus', 'Session recovered', 'green', 5000); - return false; } -GlobalViewModel.startMakingQuestions = function() { +GlobalViewModel.resetSessionValues = function() { - smileAlert('#globalstatus', 'Start making questions', 'green', 5000); + this.teacher_name(""); + this.session_name(""); + this.group_name(""); + //window.location.href = window.location.pathname; + //window.location.reload(true); return false; } -GlobalViewModel.startMakingQuestionsWithIQSet = function() { +GlobalViewModel.createSession = function() { + + if (!this.teacher_name() || this.teacher_name() === "") { this.teacher_name('Default Teacher'); } + if (!this.session_name() || this.session_name() === "") { this.session_name('Default Session'); } + if (!this.group_name() || this.group_name() === "") { this.group_name('Default Group'); } $.ajax({ cache: false, - type: "GET", + type: "POST", dataType: "text", - url: SMILEROUTES["iqset"]+GlobalViewModel.iqsets()[GlobalViewModel.position()].id, - data: {}, + url: SMILEROUTES["createsession"], + data: {"teacherName":GlobalViewModel.teacher_name,"groupName":GlobalViewModel.group_name,"sessionName":GlobalViewModel.session_name}, error: function(xhr, text, err) { - smileAlert('#globalstatus', 'Unable to call /smile/iqset/{key}. Reason: ' + xhr.status + ':' + xhr.responseText + '. Please verify your connection or server status.', 'trace'); + smileAlert('#globalstatus', 'Unable to post session values. Reason: ' + xhr.status + ':' + xhr.responseText + '. Please verify your connection or server status.', 'trace'); }, - success: function(data) { - - var iqset = JSON.parse(data); - var iqdata = iqset.iqdata; - - for (i = 0; i < iqdata.length; i++) { - - var options = []; - options.push(iqdata[i].O1,iqdata[i].O2,iqdata[i].O3,iqdata[i].O4); - - GlobalViewModel.questions.push( - new Question( - i, - iqdata[i].PICURL, - iqdata[i].NAME, - iqdata[i].Q, - iqdata[i].A, - options, - iqdata[i].IP, - iqdata[i].TYPE - ) - ); - } - - GlobalViewModel.title_iqset = iqset.title; - - //smileAlert('#globalstatus', 'title_iqset='+GlobalViewModel.title_iqset, 'blue', 15000); - //smileAlert('#globalstatus', 'questions='+GlobalViewModel.questions()[0].author, 'blue', 5000); + switchSection('div[data-slug=choose-activity-flow]'); } }); - switchSection('div[data-slug=list-questions]'); - return false; } @@ -196,6 +173,8 @@ GlobalViewModel.usePreparedQuestions = function() { switchSection('div[data-slug=choose-an-iqset]'); + + $.ajax({ cache: false, type: "GET", @@ -212,18 +191,33 @@ GlobalViewModel.usePreparedQuestions = function() { var dataObject = JSON.parse(data); var iqsets = dataObject.rows; + TEMP_POSITION = 0; + for (i = 0; i < dataObject.total_rows; i++) { // Getting the values accessible from /smile/iqsets - TEMP_IQSET = new IQSet( + /* + TEMP_IQSET.position = i; + TEMP_IQSET.id = iqsets[i].id; + TEMP_IQSET.sessionName = iqsets[i].value[0]; + TEMP_IQSET.teacherName = iqsets[i].value[1]; + TEMP_IQSET.groupName = iqsets[i].value[2]; + TEMP_IQSET.date = iqsets[i].key.substr(0, 10); + */ + + GlobalViewModel.iqsets.push(new IQSet( i, iqsets[i].id, iqsets[i].value[0], iqsets[i].value[1], iqsets[i].value[2], iqsets[i].key.substr(0, 10) - ); + )); + + //smileAlert('#globalstatus', 'during push='+GlobalViewModel.iqsets.length, 'blue', 15000); + + /* // Getting the values accessible from /smile/iqset/{id} (for now, the size) $.ajax({ cache: false, @@ -235,50 +229,101 @@ GlobalViewModel.usePreparedQuestions = function() { success: function(data) { var iqset = JSON.parse(data); - TEMP_IQSET.size = iqset.iqdata.length; + + + smileAlert('#globalstatus', 'TEMP_POSITION='+TEMP_POSITION, 'green', 15000); + //GlobalViewModel.iqsets[0].size = iqset.iqdata.length; + + TEMP_POSITION++; + + //smileAlert('#globalstatus', '???='+iqset.title, 'green', 15000); + // The IQSet is ready, we can add it to the list - GlobalViewModel.iqsets.push(TEMP_IQSET); + //GlobalViewModel.iqsets.push(TEMP_IQSET); + + //smileAlert('#globalstatus', 'session name='+TEMP_IQSET.sessionName, 'green', 5000); } }); + + */ } } }); + //smileAlert('#globalstatus', 'FINAL='+GlobalViewModel.iqsets.length, 'blue', 15000); + + //GlobalViewModel.iqsets = TEMP_IQSETS; + return false; } -GlobalViewModel.resetSessionValues = function() { +function previewIQSet() { + + // Do something + //alert($(this).parent().find('input[name=id]').attr('value')); + + //smileAlert('#globalstatus', '1st attempt='+$(this).val(), 'blue', 10000); + smileAlert('#globalstatus', '1st attempt='+$(this).attr('name'), 'blue', 10000); // should display the string 'id' + smileAlert('#globalstatus', '2nd attempt='+$(this).attr('value'), 'blue', 10000); // should display a very long id + smileAlert('#globalstatus', '3rd attempt='+$(this).parent().find('input[type=hidden]').attr('name'), 'blue', 10000); // should display a very long id +} - this.teacher_name(""); - this.session_name(""); - this.group_name(""); - //window.location.href = window.location.pathname; - //window.location.reload(true); +GlobalViewModel.recoverSession = function() { + + smileAlert('#globalstatus', 'Session recovered', 'green', 5000); return false; } -// Displays directly a "Recovering Session" button if a session is already running -GlobalViewModel.initializePage = function() { +GlobalViewModel.startMakingQuestions = function() { + smileAlert('#globalstatus', 'Start making questions', 'green', 5000); + return false; +} + +GlobalViewModel.startMakingQuestionsWithIQSet = function() { + $.ajax({ cache: false, type: "GET", dataType: "text", - url: SMILEROUTES["all"], + url: SMILEROUTES["iqset"]+GlobalViewModel.iqsets()[GlobalViewModel.position()].id, data: {}, error: function(xhr, text, err) { - smileAlert('#globalstatus', 'Unable to call /smile/all. Reason: ' + xhr.status + ':' + xhr.responseText + '. Please verify your connection or server status.', 'trace'); + smileAlert('#globalstatus', 'Unable to call /smile/iqset/{key}. Reason: ' + xhr.status + ':' + xhr.responseText + '. Please verify your connection or server status.', 'trace'); }, + success: function(data) { - // If a session is already running, we replace the session values fields by a "recovering session" button - if(data.indexOf('SessionID') !== -1 ){ - switchSection('div[data-slug=recover-session]'); + var iqset = JSON.parse(data); + var iqdata = iqset.iqdata; + + for (i = 0; i < iqdata.length; i++) { + + var options = []; + options.push(iqdata[i].O1,iqdata[i].O2,iqdata[i].O3,iqdata[i].O4); + + GlobalViewModel.questions.push( + new Question( + i, + iqdata[i].PICURL, + iqdata[i].NAME, + iqdata[i].Q, + iqdata[i].A, + options, + iqdata[i].IP, + iqdata[i].TYPE + ) + ); } + //GlobalViewModel.title_iqset = iqset.title; } }); + + switchSection('div[data-slug=list-questions]'); + + return false; } $(document).ready(function() { @@ -287,6 +332,11 @@ $(document).ready(function() { ko.applyBindings(GlobalViewModel); GlobalViewModel.initializePage(); + + $('#more').click(function () { + + alert('test'); + }); }); /* --------- @@ -307,7 +357,15 @@ function sleep(seconds) { GlobalViewModel.foobar = function() { - smileAlert('#globalstatus', 'CACHE='+TEMP_IQSET.size, 'blue',5000); + //smileAlert('#globalstatus', 'TEMP_IQSETS='+TEMP_IQSETS.length, 'blue', 15000); + smileAlert('#globalstatus', 'GlobalViewModel.iqsets.length='+GlobalViewModel.iqsets.total_rows, 'blue', 15000); +} + +GlobalViewModel.addFakeIQSet = function() { + + GlobalViewModel.iqsets.push( + new IQSet(i,'blabla','name','teacher','group','date',0) + ); } /* diff --git a/static/smile-teacher.html b/static/smile-teacher.html index 3110b59..7e71d70 100644 --- a/static/smile-teacher.html +++ b/static/smile-teacher.html @@ -119,7 +119,8 @@

- [] + + more (, , @@ -128,6 +129,10 @@

@@ -139,7 +144,11 @@

- + - + - + - + +