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

[#39] Updating web client for teacher (preview iqset in progress) #58

Merged
merged 3 commits into from
Apr 25, 2014
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
260 changes: 187 additions & 73 deletions static/js/smileteacher.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/

var VERSION = '0.1.2';
var VERSION = '0.1.5';

var SMILEROUTES = {
"currentmessage": "/smile/currentmessage",
Expand All @@ -38,6 +38,10 @@ var SMILEROUTES = {
"createsession": "/smile/createsession"
};

var TEMP_IQSETS = [];
var TEMP_POSITION = 0;
//var READY = false;

/* --------
MODELS
-------- */
Expand All @@ -63,8 +67,11 @@ var IQSet = function(position,id,title,teacherName,groupName,date) {
this.groupName = groupName;
this.date = date;
this.questions = [];
//this.size = size;
}



var GlobalViewModel = {
teacher_name: ko.observable(""),
session_name: ko.observable(""),
Expand Down Expand Up @@ -104,6 +111,40 @@ ko.extenders.required = function(target, overrideMessage) {
/* ---------
ACTIONS
--------- */

// Displays directly a "Recovering Session" button if a session is already running
GlobalViewModel.initializePage = function() {

$.ajax({
cache: false,
type: "GET",
dataType: "text",
url: SMILEROUTES["all"],
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');
},
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]');
}
}
});
}

GlobalViewModel.resetSessionValues = function() {

this.teacher_name("");
this.session_name("");
this.group_name("");
//window.location.href = window.location.pathname;
//window.location.reload(true);
return false;
}

GlobalViewModel.createSession = function() {

if (!this.teacher_name() || this.teacher_name() === "") { this.teacher_name('Default Teacher'); }
Expand All @@ -128,6 +169,106 @@ GlobalViewModel.createSession = function() {
return false;
}

GlobalViewModel.usePreparedQuestions = function() {

switchSection('div[data-slug=choose-an-iqset]');



$.ajax({
cache: false,
type: "GET",
dataType: "text",
url: SMILEROUTES["iqsets"],
data: {},

error: function(xhr, text, err) {
smileAlert('#globalstatus', 'Unable to call /smile/iqsets. Reason: ' + xhr.status + ':' + xhr.responseText + '. Please verify your connection or server status.', 'trace');
},

success: function(data) {

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.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,
type: "GET",
dataType: "text",
url: SMILEROUTES["iqset"]+iqsets[i].id,
data: {},
error: function(xhr, text, err) { smileAlert('#globalstatus', 'Unable to ajax in ajax.', 'trace'); },

success: function(data) {
var iqset = JSON.parse(data);


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);

//smileAlert('#globalstatus', 'session name='+TEMP_IQSET.sessionName, 'green', 5000);
}
});

*/
}
}
});

//smileAlert('#globalstatus', 'FINAL='+GlobalViewModel.iqsets.length, 'blue', 15000);

//GlobalViewModel.iqsets = TEMP_IQSETS;

return false;
}

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
}

GlobalViewModel.recoverSession = function() {

smileAlert('#globalstatus', 'Session recovered', 'green', 5000);
Expand Down Expand Up @@ -176,11 +317,7 @@ GlobalViewModel.startMakingQuestionsWithIQSet = function() {
)
);
}

GlobalViewModel.title_iqset = iqset.title;

//smileAlert('#globalstatus', 'title_iqset='+GlobalViewModel.title_iqset, 'blue', 15000);
//smileAlert('#globalstatus', 'questions='+GlobalViewModel.questions()[0].author, 'blue', 5000);
//GlobalViewModel.title_iqset = iqset.title;
}
});

Expand All @@ -189,94 +326,71 @@ GlobalViewModel.startMakingQuestionsWithIQSet = function() {
return false;
}

GlobalViewModel.usePreparedQuestions = function() {
$(document).ready(function() {

// Init Data Model
ko.applyBindings(GlobalViewModel);

switchSection('div[data-slug=choose-an-iqset]');
GlobalViewModel.initializePage();

$.ajax({
cache: false,
type: "GET",
dataType: "text",
url: SMILEROUTES["iqsets"],
data: {},

error: function(xhr, text, err) {
smileAlert('#globalstatus', 'Unable to call /smile/iqsets. Reason: ' + xhr.status + ':' + xhr.responseText + '. Please verify your connection or server status.', 'trace');
},

success: function(data) {
$('#more').click(function () {

var dataObject = JSON.parse(data);
var iqsets = dataObject.rows;
alert('test');
});
});

for (i = 0; i < dataObject.total_rows; i++) {
/* ---------
UTILITY
--------- */

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)
)
);
}
}
});
function switchSection(newSection) {
$('section.active').removeClass('active');
$(newSection).parent().addClass('active');
}

return false;
function sleep(seconds) {

var now = new Date();
var desiredTime = new Date().setSeconds(now.getSeconds() + seconds);
while (now < desiredTime) { now = new Date(); }
}

GlobalViewModel.resetSessionValues = function() {
GlobalViewModel.foobar = function() {

this.teacher_name("");
this.session_name("");
this.group_name("");
//window.location.href = window.location.pathname;
//window.location.reload(true);
return false;
//smileAlert('#globalstatus', 'TEMP_IQSETS='+TEMP_IQSETS.length, 'blue', 15000);
smileAlert('#globalstatus', 'GlobalViewModel.iqsets.length='+GlobalViewModel.iqsets.total_rows, 'blue', 15000);
}

// Displays directly a "Recovering Session" button if a session is already running
GlobalViewModel.initializePage = function() {
GlobalViewModel.addFakeIQSet = function() {

GlobalViewModel.iqsets.push(
new IQSet(i,'blabla','name','teacher','group','date',0)
);
}

/*
function getJsonIQSet(id) {

var jsonIQSet = '';

$.ajax({
cache: false,
type: "GET",
dataType: "text",
url: SMILEROUTES["all"],
url: SMILEROUTES["iqset"]+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');
},
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]');
}
error: function(xhr, text, err) { smileAlert('#globalstatus', 'Unable to ajax in ajax.', 'trace'); },
success: function(data) {
jsonIQSet = JSON.parse(data);

}
});
}

$(document).ready(function() {

// Init Data Model
ko.applyBindings(GlobalViewModel);

GlobalViewModel.initializePage();
});

/* ---------
UTILITY
--------- */

function switchSection(newSection) {
$('section.active').removeClass('active');
$(newSection).parent().addClass('active');
while(jsonIQSet === '') { var lol = 'lol'; }
smileAlert('#globalstatus', 'A la sortie >> '+jsonIQSet, 'blue',5000);
return jsonIQSet;
}
*/

// alerttype
// - by default, none is required if you don't intend to use lifetime
Expand Down
12 changes: 11 additions & 1 deletion static/smile-teacher.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ <h3>
<div data-bind="foreach: iqsets" >
<div>
<input type="radio" name="optionsGroup" data-bind="attr: {value: position}, checked: $root.position" />
<input type="hidden" name="id" data-bind="attr: {value: id}" />
<a id="more" onclick="previewIQSet()" data-bind="attr: {name: id}" class="tiny round button more">more</a>
<span data-bind="text: sessionName"></span>
(<span data-bind="text: teacherName"></span>,
<span data-bind="text: groupName"></span>,
Expand All @@ -127,6 +129,10 @@ <h3>
</div>
<ul class="button-group even two-up">
<li><a href="#" data-bind='click: startMakingQuestionsWithIQSet' class="button">Load</a></li>
<!--
<li><a href="#" data-bind='click: foobar' class="button">FINAL</a></li>
<li><a href="#" data-bind='click: addFakeIQSet' class="button">ADD</a></li>
-->
</ul>
</div>
</section>
Expand All @@ -138,7 +144,11 @@ <h3>
<div data-bind="foreach: questions" >
<div>
<input type="checkbox" name="questionsToSave" data-bind="attr: {value: position}, checked: $root.position" />
<span data-bind="text: author"></span>
<span data-bind="text: position"></span> -
<span data-bind="text: author"></span> -
<span data-bind="text: ip"></span> -
<span data-bind="text: type"></span>

</div>
</div>

Expand Down