-
Notifications
You must be signed in to change notification settings - Fork 7
/
initiative.js
437 lines (373 loc) · 14.6 KB
/
initiative.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
var ejs = require('./ejs.js');
var lf = require('./lfcli.js');
var texts = require('./texts.json');
var issueFunc = require('./issue.js');
var userFunc = require('./user.js');
var suggFunc = require('./suggestion.js');
// Basic functions on JS objects
var registerFunctions = function() {
/**
* checks if an object is contained in an array
*
* @param obj object to be checked
*/
Array.prototype.contains = function(obj) {
for (var i = 0; i < this.length; i++) {
if (this[i] === obj) {
return true;
}
}
return false;
}
}
/**
* checks if the user supports an initiative
*
* @param support array with support query result
* @param ini id of initiative to be checked
*/
var getMemberSupport = function(support, ini) {
for(var a = 0; a < support.length; a++) {
if(support[a].length > 0 && support[a][0].initiative_id == ini) {
return true;
}
}
return false;
}
/**
* gets the quorum from a policy
*
* @param policy the policy to be used
*/
var getQuorum = function(policy) {
if(policy.issue_quorum_num != null) {
return (policy.issue_quorum_num / policy.issue_quorum_den);
}
else {
return (policy.initiative_quorum_num / policy.initiative_quorum_den);
}
}
module.exports.getQuorum = getQuorum;
/**
* Takes care of retrieving data for and rendering the
* initiative page.
*
* @param state The state object of the current HTTP-Request
*/
exports.show = function(state, render) {
// we need a valid user session...
if(!state.session_key()) {
state.sendToLogin();
return;
}
// we need an initiative id
if(!state.url.query.initiative_id) {
state.fail_invalidResource('Please provide initiative_id parameter');
return;
}
registerFunctions();
var initiative_id = state.url.query.initiative_id;
var builtIni = {};
var initiative;
var issue;
var area;
var unit;
var policy;
var drafts = [];
var authors = [];
var supporters = [];
var alternatives = [];
var support = [];
var suggestions = [];
var opinions = [];
var current_draft;
var iniDone = false;
var draftDone = false;
var supportDone = false;
var alternativesDone = false;
var suggestionsDone = false;
var finish = function() {
if(iniDone && draftDone && supportDone && alternativesDone && suggestionsDone
&& drafts.length == authors.length
&& alternatives.length == support.length
&& opinions.length == suggestions.length) {
builtIni.id = initiative_id;
builtIni.name = initiative.name;
builtIni.discussionurl = initiative.discussion_url;
builtIni.area = {};
builtIni.area.id = area.id;
builtIni.area.name = area.name;
builtIni.issue = {};
builtIni.issue.id = issue.id;
builtIni.policy = policy.name;
if(current_draft) {
builtIni.text = current_draft.content;
}
builtIni.state = issueFunc.getIssueStateText(issue.state);
var date = new Date(initiative.created);
builtIni.createdat = date.getDate() + '.' + ( date.getMonth() + 1 ) + '.' + date.getFullYear() + ' ' + date.getHours() + ':' + date.getMinutes();
// calculate quorum
builtIni.requiredquorum = (getQuorum(policy) * 100) + '%';
builtIni.requiredrightnow = Math.ceil(getQuorum(policy) * (area.member_weight));
if(initiative.admitted) {
builtIni.admitted = texts.yes;
}
else {
builtIni.admitted = texts.no;
}
// get authors
var author_ids = [];
builtIni.authors = [];
for(var i = 0; i < authors.length; i++) {
builtAuthor = {};
if(!author_ids.contains(authors[i].id)) {
author_ids.push(authors[i].id);
builtAuthor = userFunc.getUserBasic(authors[i]);
if(builtAuthor.id == current_draft.author_id) {
builtAuthor.lastauthor = true;
}
builtIni.authors.push(builtAuthor);
}
}
builtIni.drafts = [];
// sort drafts by date
Array.prototype.sort.call(drafts, function(a,b) {
if (a.created > b.created)
return -1;
else if (a.created < b.created)
return 1;
else
return 0;
});
// build drafts
for(var i = 0; i < drafts.length; i++) {
builtDraft = {};
builtDraft.id = drafts[i].id;
var date = new Date(drafts[i].created);
builtDraft.createdat = date.getDate() + '.' + ( date.getMonth() + 1 ) + '.' + date.getFullYear() + ' ' + date.getHours() + ':' + date.getMinutes();
builtDraft.author = {};
// get author
for(var a = 0; a < authors.length; a++) {
if(authors[a].id == drafts[i].author_id) {
builtDraft.author = userFunc.getUserBasic(authors[a]);
}
}
builtIni.drafts.push(builtDraft);
}
builtIni.supporters = [];
supporternumber = 0;
potsupporternumber = 0;
var supporterpages = Math.ceil(supporters.length / 24);
builtIni.supporterspages = supporterpages;
builtIni.supporterspage = 1;
var start_support = 0;
var end_support = 24;
if(state.url.query.supporterpage !== undefined && state.url.query.supporterpage > 1) {
start_support = (state.url.query.supporterpage - 1) * 24;
end_support = state.url.query.supporterpage * 24;
builtIni.supporterspage = state.url.query.supporterpage;
}
// get supporters
for(var i = 0; i < supporters.length; i++) {
builtMember = userFunc.getUserBasic(supporters[i].member);
// limit to 24
if(i >= start_support && i < end_support) {
builtIni.supporters.push(builtMember);
}
if(supporters[i].member.id == state.user_id()) {
builtIni.isupport = true;
}
if(supporters[i].supporter.satisfied) {
supporternumber++;
}
else {
potsupporternumber++;
}
}
builtIni.supporter = supporternumber;
builtIni.potsupporter = potsupporternumber;
builtIni.suggestions = [];
builtIni.suggestionsnumber = suggestions.length;
// calculate suggestion pagination
builtIni.suggestionspages = Math.ceil(suggestions.length / 4);
if(state.url.query.suggestionpage !== undefined && state.url.query.suggestionpage > 1) {
builtIni.suggestionspage = state.url.query.suggestionpage;
start_sugg = (builtIni.suggestionspage - 1) * 4;
end_sugg = builtIni.suggestionspage * 4;
}
else {
builtIni.suggestionspage = 1;
start_sugg = 0;
end_sugg = 4;
}
var total_supporters = initiative.supporter_count;
// get suggestions
for(var i = start_sugg; i < suggestions.length && i < end_sugg; i++) {
builtSugg = {};
builtSugg.id = suggestions[i].id;
builtSugg.name = suggestions[i].name;
builtSugg.mustsupporter = suggestions[i].plus2_unfulfilled_count + suggestions[i].plus2_fulfilled_count;
builtSugg.shouldsupporter = suggestions[i].plus1_unfulfilled_count + suggestions[i].plus1_fulfilled_count;
builtSugg.neutralsupporter = total_supporters
- (suggestions[i].plus2_unfulfilled_count + suggestions[i].plus2_fulfilled_count)
- (suggestions[i].plus1_unfulfilled_count + suggestions[i].plus1_fulfilled_count)
- (suggestions[i].minus2_unfulfilled_count + suggestions[i].minus2_fulfilled_count)
- (suggestions[i].minus1_unfulfilled_count + suggestions[i].minus1_fulfilled_count);
builtSugg.shouldnotsupporter = suggestions[i].minus1_unfulfilled_count + suggestions[i].minus1_fulfilled_count;
builtSugg.mustnotsupporter = suggestions[i].minus2_unfulfilled_count + suggestions[i].minus2_fulfilled_count;
builtSugg.mustsupportwidth = ( builtSugg.mustsupporter / total_supporters * 100) + '%'
builtSugg.shouldsupportwidth = ( builtSugg.shouldsupporter / total_supporters * 100) + '%'
builtSugg.neutralsupportwidth = ( builtSugg.neutralsupporter / total_supporters * 100) + '%'
builtSugg.shouldnotsupportwidth = ( builtSugg.shouldnotsupporter / total_supporters * 100) + '%'
builtSugg.mustnotsupportwidth = ( builtSugg.mustnotsupporter / total_supporters * 100) + '%'
builtSugg.notimplementedmustsupporter = suggestions[i].plus2_unfulfilled_count;
builtSugg.notimplementedshouldsupporter = suggestions[i].plus1_unfulfilled_count;
builtSugg.notimplementedneutralsupporter = builtSugg.neutralsupporter;
builtSugg.notimplementedshouldnotsupporter = suggestions[i].minus1_unfulfilled_count;
builtSugg.notimplementedmustnotsupporter = suggestions[i].minus2_unfulfilled_count;
builtSugg.notimplementedmustsupporterwidth = ( builtSugg.notimplementedmustsupporter / total_supporters * 100) + '%'
builtSugg.notimplementedshouldsupporterwidth = ( builtSugg.notimplementedshouldsupporter / total_supporters * 100) + '%'
builtSugg.notimplementedneutralsupporterwidth = ( builtSugg.notimplementedneutralsupporter / total_supporters * 100) + '%'
builtSugg.notimplementedshouldnotsupportwidth = ( builtSugg.notimplementedshouldnotsupporter / total_supporters * 100) + '%'
builtSugg.notimplementedmustnotsupportwidth = ( builtSugg.notimplementedmustnotsupporter / total_supporters * 100) + '%'
builtSugg.implementedmustsupporter = suggestions[i].plus2_fulfilled_count;
builtSugg.implementedshouldsupporter = suggestions[i].plus1_fulfilled_count;
builtSugg.implementedneutralsupporter = builtSugg.neutralsupporter;
builtSugg.implementedshouldnotsupporter = suggestions[i].minus1_fulfilled_count;
builtSugg.implementedmustnotsupporter = suggestions[i].minus2_fulfilled_count;
builtSugg.implementedmustsupporterwidth = ( builtSugg.implementedmustsupporter / total_supporters * 100) + '%'
builtSugg.implementedshouldsupporterwidth = ( builtSugg.implementedshouldsupporter / total_supporters * 100) + '%'
builtSugg.implementedneutralsupporterwidth = ( builtSugg.implementedneutralsupporter / total_supporters * 100) + '%'
builtSugg.implementedshouldnotsupportwidth = ( builtSugg.implementedshouldnotsupporter / total_supporters * 100) + '%'
builtSugg.implementedmustnotsupportwidth = ( builtSugg.implementedmustnotsupporter / total_supporters * 100) + '%'
// get my opinion
for(var a = 0; a < opinions.length; a++) {
if(opinions[a].length > 0 && opinions[a][0].suggestion_id == builtSugg.id) {
builtSugg.smiley = suggFunc.calculate_smiley(opinions[a][0]);
builtSugg.isayimplemented = opinions[a][0].fulfilled;
builtSugg.my_opinion = opinions[a][0].degree;
}
}
builtIni.suggestions.push(builtSugg);
}
builtIni.alternativeinis = [];
builtIni.alternativeinisnumber = alternatives.length;
// sort alternative inis
if(issue.ranks_available) {
// sort inis by rank
Array.prototype.sort.call(alternatives, function(a,b) {
if (a.rank < b.rank)
return -1;
else if (a.rank > b.rank)
return 1;
else
return 0;
});
}
else {
// sort inis by supporter
Array.prototype.sort.call(alternatives, function(a,b) {
if (a.satisfied_supporter_count > b.satisfied_supporter_count)
return -1;
else if (a.satisfied_supporter_count < b.satisfied_supporter_count)
return 1;
else
return 0;
});
}
// get alternative inis
for(var i = 0; i < alternatives.length; i++) {
alternativeIni = {};
alternativeIni.id = alternatives[i].id;
alternativeIni.name = alternatives[i].name;
alternativeIni.supporter = alternatives[i].satisfied_supporter_count;
alternativeIni.potsupporter = alternatives[i].supporter_count - alternatives[i].satisfied_supporter_count;
alternativeIni.uninterested = ( area.member_weight - alternativeIni.supporter ) - alternativeIni.potsupporter;
if(alternativeIni.uninterested < 0) {
alternativeIni.uninterested = 0;
}
var total = alternativeIni.supporter + alternativeIni.potsupporter + alternativeIni.uninterested;
alternativeIni.supporterwidth = Math.floor(( alternativeIni.supporter / total ) * 100) + '%';
alternativeIni.potsupporterwidth = Math.floor(( alternativeIni.potsupporter / total ) * 100) + '%';
alternativeIni.uninterestedwidth = Math.floor(( alternativeIni.uninterested / total ) * 100) + '%';
alternativeIni.quorumwidth = builtIni.requiredquorum;
// check if member supports ini
if(getMemberSupport(support, alternatives[i].id)) {
alternativeIni.isupport = true;
}
builtIni.alternativeinis.push(alternativeIni);
}
state.context.initiative = builtIni;
state.context.meta.currentpage = "initiative";
ejs.render(state, '/initiative.tpl');
}
}
// get the initiative
lf.query('/initiative', { 'initiative_id': initiative_id, 'include_issues': 1, 'include_areas': 1, 'include_policies': 1, 'include_units': 1 }, state, function(ini_res) {
initiative = ini_res.result[0];
issue = ini_res.issues[initiative.issue_id];
area = ini_res.areas[issue.area_id];
unit = ini_res.units[area.unit_id];
policy = ini_res.policies[issue.policy_id];
// get alternative inis
lf.query('/initiative', { 'issue_id': initiative.issue_id }, state, function(alt_res) {
for(var i = 0; i < alt_res.result.length; i++) {
if(alt_res.result[i].id != initiative_id) {
alternatives.push(alt_res.result[i]);
// get supporters
lf.query('/supporter', { 'initiative_id': alt_res.result[i].id, 'snapshot': 'latest', 'member_id': state.user_id() }, state, function(support_res) {
support.push(support_res.result);
finish();
});
}
}
alternativesDone = true;
finish();
});
iniDone = true;
finish();
});
// get drafts
lf.query('/draft', { 'initiative_id': initiative_id, 'render_content': 'html' }, state, function(draft_res) {
var highest_id = 0;
for(var i = 0; i < draft_res.result.length; i++) {
drafts.push(draft_res.result[i]);
if(draft_res.result[i].id > highest_id) {
highest_id = draft_res.result[i].id;
current_draft = draft_res.result[i];
}
// get authors
lf.query('/member', { 'member_id': draft_res.result[i].author_id }, state, function(member_res) {
authors.push(member_res.result[0]);
finish();
});
}
draftDone = true;
finish();
});
// get supporters
lf.query('/supporter', { 'initiative_id': initiative_id, 'snapshot': 'latest', 'include_members': 1 }, state, function(support_res) {
for(var i = 0; i < support_res.result.length; i++) {
fetchedSupporter = {
"supporter": support_res.result[i],
"member": support_res.members[support_res.result[i].member_id]
};
supporters.push(fetchedSupporter);
}
supportDone = true;
finish();
});
// get suggestions
lf.query('/suggestion', { 'initiative_id': initiative_id, 'render_content': 'html' }, state, function(res) {
for(var i = 0; i < res.result.length; i++) {
suggestions.push(res.result[i]);
// get my opinion
lf.query('/opinion', { 'suggestion_id': res.result[i].id, 'member_id': state.user_id() }, state, function(op_res) {
opinions.push(op_res.result);
finish()
});
}
suggestionsDone = true;
finish();
});
}