From 3ad3d084764b99388475081e7ba15721a4cf9d17 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Mon, 10 Nov 2014 16:52:50 -0500 Subject: [PATCH] opportunity: Added count and score to Meetup display page Signed-off-by: Lucas Bajolet --- contrib/opportunity/src/opportunity_model.nit | 27 +++++++ contrib/opportunity/src/templates/meetup.nit | 97 +++++++++++++++++++++++-- 2 files changed, 119 insertions(+), 5 deletions(-) diff --git a/contrib/opportunity/src/opportunity_model.nit b/contrib/opportunity/src/opportunity_model.nit index 901077a..bb56e02 100644 --- a/contrib/opportunity/src/opportunity_model.nit +++ b/contrib/opportunity/src/opportunity_model.nit @@ -300,6 +300,33 @@ class Answer abort end + # Counts the number of positive or maybe answers + fun count(db: OpportunityDB): Int do + if id == -1 then return -1 + var count = 0 + var res = db.select("part_answers.value FROM part_answers WHERE part_answers.id_ans={id};") + if meetup == null then meetup = load_meetup(db) + for i in res do + if meetup.answer_mode == 0 then + count += i[0].to_i + else + if i[0].to_i == 2 then count += 1 + end + end + return count + end + + # Counts the score for this particular answer + fun score(db: OpportunityDB): Int do + if id == -1 then return -1 + var score = 0 + var res = db.select("part_answers.value FROM part_answers WHERE part_answers.id_ans={id};") + for i in res do + score += i[0].to_i + end + return score + end + redef fun commit(db) do var m = meetup if m == null then return false diff --git a/contrib/opportunity/src/templates/meetup.nit b/contrib/opportunity/src/templates/meetup.nit index 8d0c45a..e5d1f56 100644 --- a/contrib/opportunity/src/templates/meetup.nit +++ b/contrib/opportunity/src/templates/meetup.nit @@ -40,17 +40,78 @@ class OpportunityMeetupPage init do header.page_js = "mode = {mode};\n" header.page_js += """ + function update_scores(){ + var anss = $('.answer'); + var count = {}; + var scores = {}; + var answers = []; + var maxscore = 0; + for(i=0; i < anss.length; i++){ + var incscore = 0; + var inccount = 0; + var idparts = anss[i].id.split("_"); + var ansid = idparts[1]; + var html = anss[i].innerHTML; + if(html === "
✔
"){ + inccount = 1; + incscore = 2; + }else if(html === "
❓
"){ + incscore = 1; + } + var intansid = parseInt(ansid) + if(answers.indexOf(intansid) == -1){ + answers.push(intansid); + } + if(ansid in count){ + count[ansid] += inccount; + }else{ + count[ansid] = inccount; + } + if(ansid in scores){ + scores[ansid] += incscore; + }else{ + scores[ansid] = incscore; + } + if(scores[ansid] > maxscore){ + maxscore = scores[ansid]; + } + } + for(i=0; i < answers.length; i++){ + var ansid = answers[i].toString(); + var el = $('#total'+ansid)[0]; + var ins = "
"+count[ansid]; + if(scores[ansid] >= maxscore){ + ins += "
★"; + } + ins += "
"; + el.innerHTML = ins; + } + } function change_answer(ele, id){ // modify only the currently selected entry if (in_modification_id != id) return; var e = document.getElementById(ele.id); var i = e.innerHTML; - var ans = true; + var ans = true;""" + if mode == 0 then + header.page_js += """ + if(i === "
✔
"){ + ans = 0; + e.innerHTML = "
✘
" + e.style.color = "red"; + }else{ + ans = 1; + e.innerHTML = "
✔
"; + e.style.color = "green"; + }""" + + else + header.page_js += """ if(i === "
✔
"){ ans = 1; e.innerHTML = "
❓
" - e.style.color = "orange"; + e.style.color = "#B8860B"; }else if(i === "
❓
"){ ans = 0; e.innerHTML = "
✘
" @@ -59,10 +120,13 @@ class OpportunityMeetupPage ans = 2; e.innerHTML = "
✔
"; e.style.color = "green"; - } + }""" + end + header.page_js += """ var a = ele.id.split('_') var pid = a[1] var aid = a[2] + update_scores(); $.ajax({ type: "POST", url: "./rest/answer", @@ -90,7 +154,7 @@ class OpportunityMeetupPage header.page_js += """ if(i === "
✔
"){ e.innerHTML = "
❓
"; - e.style.color = "orange"; + e.style.color = "#B8860B"; }else if(i === "
❓
"){ e.innerHTML = "
✘
" e.style.color = "red"; @@ -101,6 +165,7 @@ class OpportunityMeetupPage """ end header.page_js += """ + update_scores(); } function add_part(ele){ var e = document.getElementById(ele.id); @@ -151,6 +216,7 @@ class OpportunityMeetupPage var arr = ele.id.split("_") var pid = arr[1] $('#' + ele.id).parent().parent().parent().remove(); + update_scores(); $.ajax({ type: "POST", url: "./rest/people", @@ -160,7 +226,6 @@ class OpportunityMeetupPage } }); } - // ID of line currently open for modification var in_modification_id = null; function modify_people(ele, id){ @@ -282,6 +347,28 @@ redef class Meetup t.add """
""" t.add "" + # Compute score for each answer + var scores = new HashMap[Int, Int] + var maxsc = 0 + for i in answers(db) do + scores[i.id] = i.score(db) + if scores[i.id] > maxsc then maxsc = scores[i.id] + end + t.add """ + + Total + """ + for i in answers(db) do + t.add """
{{{i.count(db)}}}""" + if scores.has_key(i.id) and scores[i.id] >= maxsc then + t.add """
★""" + end + t.add "
" + end + t.add "" + t.add """ + +""" t.add "" t.add "" return t -- 1.7.9.5