From 37c10507af8c4eb847a52bbc9abf2de184f4dd9d Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Mon, 10 Nov 2014 17:13:35 -0500 Subject: [PATCH] opportunity: Added maybe option Signed-off-by: Lucas Bajolet --- contrib/opportunity/src/opportunity_controller.nit | 18 ++-- contrib/opportunity/src/opportunity_model.nit | 39 ++++---- contrib/opportunity/src/templates/meetup.nit | 95 ++++++++++++++++---- .../opportunity/src/templates/meetup_creation.nit | 6 ++ 4 files changed, 114 insertions(+), 44 deletions(-) diff --git a/contrib/opportunity/src/opportunity_controller.nit b/contrib/opportunity/src/opportunity_controller.nit index 831d90d..f52f07d 100644 --- a/contrib/opportunity/src/opportunity_controller.nit +++ b/contrib/opportunity/src/opportunity_controller.nit @@ -60,13 +60,18 @@ class OpportunityWelcome var mname = request.string_arg("meetup_name") var mdate = request.string_arg("meetup_date") var mplace = request.string_arg("meetup_place") + var mmodestr = request.string_arg("meetup_mode") + var mmode = 0 if mdate == null then mdate = "" if mplace == null then mplace = "" + if mmodestr != null then + mmode = 1 + end if mname == null then mname = "" var rsp = new HttpResponse(200) var meetpage = new MeetupCreationPage - var meet = new Meetup(mname, mdate, mplace) + var meet = new Meetup(mname, mdate, mplace, mmode) meetpage.ans = ansset meetpage.meet = meet meetpage.error = "'Meetup name' is a mandatory fields." @@ -75,7 +80,7 @@ class OpportunityWelcome end var db = new OpportunityDB.open(db_path) - var meet = new Meetup(mname, mdate, mplace) + var meet = new Meetup(mname, mdate, mplace, mmode) if ansset.is_empty then db.close var rsp = new HttpResponse(200) @@ -176,7 +181,7 @@ class OpportunityAnswerREST redef fun answer(request, uri) do var persid = request.int_arg("pers_id") var ansid = request.int_arg("answer_id") - var ans = request.bool_arg("answer") + var ans = request.int_arg("answer") if persid == null or ansid == null or ans == null then return bad_req var db = new OpportunityDB.open(db_path) db.change_answer(ansid, persid, ans) @@ -197,12 +202,11 @@ class OpportunityMeetupREST var ans = request.string_arg("answers").split("&") if name == null or m_id == null then return bad_req print ans - var ansmap = new HashMap[Int, Bool] + var ansmap = new HashMap[Int, Int] for i in ans do var mp = i.split("=") - var b = false - if mp.last == "true" then b = true var id = mp.first.split("_").last + var b = mp.last.to_i if not id.is_numeric then continue ansmap[id.to_i] = b end @@ -220,7 +224,7 @@ class OpportunityMeetupREST var p = new People(rname, rsurname) for i in m.answers(db) do if not ansmap.has_key(i.id) then - p.answers[i] = false + p.answers[i] = 0 else p.answers[i] = ansmap[i.id] end diff --git a/contrib/opportunity/src/opportunity_model.nit b/contrib/opportunity/src/opportunity_model.nit index aa9adec..325ac7d 100644 --- a/contrib/opportunity/src/opportunity_model.nit +++ b/contrib/opportunity/src/opportunity_model.nit @@ -31,7 +31,7 @@ class OpportunityDB # Creates the tables and triggers for Opportunity (SQLite3 DB) fun create_db do - assert create_table("IF NOT EXISTS meetups (id CHAR(40) PRIMARY KEY, name TEXT, date TEXT, place TEXT);") else + assert create_table("IF NOT EXISTS meetups (id CHAR(40) PRIMARY KEY, name TEXT, date TEXT, place TEXT, answer_mode INTEGER DEFAULT 0);") else print error or else "?" end assert create_table("IF NOT EXISTS people(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, surname TEXT);") else @@ -68,7 +68,7 @@ class OpportunityDB fun find_meetup_by_id(id: String): nullable Meetup do var req = select("* FROM meetups where id={id.to_sql_string};") for i in req do - return new Meetup.from_db(i[0].to_s, i[1].to_s, i[2].to_s, i[3].to_s) + return new Meetup.from_db(i[0].to_s, i[1].to_s, i[2].to_s, i[3].to_s, i[4].to_i) end return null end @@ -76,10 +76,8 @@ class OpportunityDB # Change an Answer `ansid` for someone with an id `pid` to `resp` # # Returns `true` if the request was sucessful, false otherwise - fun change_answer(pid: Int, ansid: Int, resp: Bool): Bool do - var rsp = 0 - if resp then rsp = 1 - var rq = execute("INSERT OR REPLACE INTO part_answers(id_part, id_ans, value) VALUES({pid},{ansid},{rsp});") + fun change_answer(pid: Int, ansid: Int, resp: Int): Bool do + var rq = execute("INSERT OR REPLACE INTO part_answers(id_part, id_ans, value) VALUES({pid},{ansid},{resp});") if not rq then print "Error while updating answer {ansid}:{pid}" print error or else "Unknown error" @@ -120,7 +118,10 @@ class People # Surname of the participant var surname: String # Map of the answers of a Meetup and the answers of the participant - var answers: Map[Answer, Bool] = new HashMap[Answer, Bool] + # 0 = No + # 1 = Maybe + # 2 = Yes + var answers: Map[Answer, Int] = new HashMap[Answer, Int] # To be used internally when fetching the `People` in Database private init from_db(id: Int, name, surname: String) do @@ -129,7 +130,7 @@ class People end # Changes an answer `ans` (or adds it) - fun answer=(ans: Answer, resp: Bool) do + fun answer=(ans: Answer, resp: Int) do answers[ans] = resp end @@ -137,12 +138,11 @@ class People # # NOTE: If `self` does not exist in the Database, no answers will be fetched fun load_answers(db: OpportunityDB, meetup: Meetup) do - self.answers = new HashMap[Answer, Bool] + self.answers = new HashMap[Answer, Int] var req = db.select("answers.id, answers.name, part_answers.value FROM part_answers, answers WHERE part_answers.id_part={id} AND answers.id=part_answers.id_ans AND answers.meetup_id={meetup.id.to_sql_string} GROUP BY answers.id;") for i in req do var ans = new Answer.from_db(i[0].to_i, i[1].to_s) - answers[ans] = false - if i[2].to_i == 1 then answers[ans] = true + answers[ans] = i[2].to_i end end @@ -165,8 +165,7 @@ class People end for i,j in answers do if i.id == -1 then i.commit(db) - var val = 0 - if j then val = 1 + var val = j if not db.execute("INSERT OR REPLACE INTO part_answers(id_part, id_ans, value) VALUES ({id},{i.id},{val});") then print("Error while adding/replacing part_answers {id}|{i.id}|{j}") print db.error or else "Unknown error" @@ -189,13 +188,13 @@ class Meetup var date: String # Place of the meetup var place: String + # Mode of answering to the meetup (atm supports with or without Maybe) + var answer_mode: Int # Builds the object with all the informations found in the database - private init from_db(id, name, date, place: String) do + private init from_db(id, name, date, place: String, mode: Int) do self.id = id - self.name = name - self.date = date - self.place = place + init(name, date, place, mode) end # Gets the answers bound to the current `Meetup` @@ -225,7 +224,7 @@ class Meetup if id == "" then var time = get_time var tmpid = (name + date + place + time.to_s).sha1_to_s - if not db.execute("INSERT INTO meetups (id, name, date, place) VALUES({tmpid.to_sql_string}, {name.to_sql_string}, {date.to_sql_string}, {place.to_sql_string});") then + if not db.execute("INSERT INTO meetups (id, name, date, place, answer_mode) VALUES({tmpid.to_sql_string}, {name.to_sql_string}, {date.to_sql_string}, {place.to_sql_string}, {answer_mode});") then print "Error recording entry Meetup {self}" print db.error or else "Null error" return false @@ -233,7 +232,7 @@ class Meetup id = tmpid return true else - return db.execute("UPDATE meetups (name, date, place) VALUES({name.to_sql_string}, {date.to_sql_string}, {place.to_sql_string}) WHERE ID={id.to_sql_string};") + return db.execute("UPDATE meetups (name, date, place, answer_mode) VALUES({name.to_sql_string}, {date.to_sql_string}, {place.to_sql_string}), answer_mode={answer_mode} WHERE ID={id.to_sql_string};") end end @@ -266,7 +265,7 @@ class Answer assert id != -1 var res = db.select("meetups.* FROM meetups, answers WHERE answers.id={id} AND answers.meetup_id=meetups.id;") for i in res do - return new Meetup.from_db(i[0].to_s, i[1].to_s, i[2].to_s, i[3].to_s) + return new Meetup.from_db(i[0].to_s, i[1].to_s, i[2].to_s, i[3].to_s, i[4].to_i) end # If no Meetup could be loaded, the contract was not respected abort diff --git a/contrib/opportunity/src/templates/meetup.nit b/contrib/opportunity/src/templates/meetup.nit index 72503cf..8d0c45a 100644 --- a/contrib/opportunity/src/templates/meetup.nit +++ b/contrib/opportunity/src/templates/meetup.nit @@ -26,15 +26,20 @@ class OpportunityMeetupPage # Meetup the page is supposed to show var meetup: nullable Meetup = null + # Answer mode for the meetup + var mode = 0 init from_id(id: String) do var db = new OpportunityDB.open("opportunity") meetup = db.find_meetup_by_id(id) db.close + if meetup != null then mode = meetup.answer_mode + init end init do - header.page_js = """ + header.page_js = "mode = {mode};\n" + header.page_js += """ function change_answer(ele, id){ // modify only the currently selected entry if (in_modification_id != id) return; @@ -43,10 +48,15 @@ class OpportunityMeetupPage var i = e.innerHTML; var ans = true; if(i === "
✔
"){ - ans = false; + ans = 1; + e.innerHTML = "
❓
" + e.style.color = "orange"; + }else if(i === "
❓
"){ + ans = 0; e.innerHTML = "
✘
" e.style.color = "red"; }else{ + ans = 2; e.innerHTML = "
✔
"; e.style.color = "green"; } @@ -65,16 +75,32 @@ class OpportunityMeetupPage } function change_temp_answer(ele){ var e = document.getElementById(ele.id); - var i = e.innerHTML; - var ans = true; + var i = e.innerHTML;""" + if mode == 0 then + header.page_js += """ + if(i === "
✔
"){ + e.innerHTML = "
✘
" + e.style.color = "red"; + }else{ + e.innerHTML = "
✔
"; + e.style.color = "green"; + } + """ + else + header.page_js += """ if(i === "
✔
"){ - ans = false; - e.innerHTML = "
✘
"; + e.innerHTML = "
❓
"; + e.style.color = "orange"; + }else if(i === "
❓
"){ + e.innerHTML = "
✘
" e.style.color = "red"; }else{ e.innerHTML = "
✔
"; e.style.color = "green"; } + """ + end + header.page_js += """ } function add_part(ele){ var e = document.getElementById(ele.id); @@ -85,11 +111,25 @@ class OpportunityMeetupPage ansmap = {}; for(i=0;i✔"){ - ansmap[curr.attr('id')] = true + ansmap[curr.attr('id')] = 1 }else{ - ansmap[curr.attr('id')] = false - } + ansmap[curr.attr('id')] = 0 + }""" + else + header.page_js += """ + if(curr[0].innerHTML === "
✔
"){ + ansmap[curr.attr('id')] = 2 + }else if(curr[0].innerHTML === "
❓
"){ + ansmap[curr.attr('id')] = 1 + }else{ + ansmap[curr.attr('id')] = 0 + }""" + end + header.page_js += """ } $.ajax({ type: "POST", @@ -192,18 +232,39 @@ redef class Meetup t.add "" t.add i.to_s t.add "" - for j,k in i.answers do + for j, k in i.answers do var color - if k then - color = "green" - else color = "red" - + if answer_mode == 0 then + if k == 1 then + color = "green" + else + color = "red" + end + else + if k == 2 then + color = "green" + else if k == 1 then + color = "#B8860B" + else + color = "red" + end + end t.add """""" t.add "
" - if k then - t.add "✔" + if answer_mode == 0 then + if k == 1 then + t.add "✔" + else + t.add "✘" + end else - t.add "✘" + if k == 2 then + t.add "✔" + else if k == 1 then + t.add "❓" + else + t.add "✘" + end end t.add "
" end diff --git a/contrib/opportunity/src/templates/meetup_creation.nit b/contrib/opportunity/src/templates/meetup_creation.nit index 6f42561..5b1a048 100644 --- a/contrib/opportunity/src/templates/meetup_creation.nit +++ b/contrib/opportunity/src/templates/meetup_creation.nit @@ -93,6 +93,12 @@ function new_answer(sender){ +
+ +
+ +
+

Opportunities

""" -- 1.7.9.5