bug-827 added points/awarded logic for given assurances
[cacert-devel.git] / includes / wot.inc.php
1 <? /*
2 LibreSSL - CAcert web application
3 Copyright (C) 2004-2011 CAcert Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 function query_init ($query)
20 {
21 return mysql_query($query);
22 }
23
24 function query_getnextrow ($res)
25 {
26 $row1 = mysql_fetch_assoc($res);
27 return $row1;
28 }
29
30 function query_get_number_of_rows ($resultset)
31 {
32 return intval(mysql_num_rows($resultset));
33 }
34
35 function get_number_of_assurances ($userid)
36 {
37 $res = query_init ("SELECT count(*) AS `list` FROM `notary`
38 WHERE `method` = 'Face to Face Meeting' AND `from`='".intval($userid)."' ");
39 $row = query_getnextrow($res);
40
41 return intval($row['list']);
42 }
43
44 function get_number_of_assurees ($userid)
45 {
46 $res = query_init ("SELECT count(*) AS `list` FROM `notary`
47 WHERE `method` = 'Face to Face Meeting' AND `to`='".intval($userid)."' ");
48 $row = query_getnextrow($res);
49
50 return intval($row['list']);
51 }
52
53 function get_top_assurer_position ($no_of_assurances)
54 {
55 $res = query_init ("SELECT count(*) AS `list` FROM `notary`
56 WHERE `method` = 'Face to Face Meeting'
57 GROUP BY `from` HAVING count(*) > '".intval($no_of_assurances)."'");
58 return intval(query_get_number_of_rows($res)+1);
59 }
60
61 function get_top_assuree_position ($no_of_assurees)
62 {
63 $res = query_init ("SELECT count(*) AS `list` FROM `notary`
64 WHERE `method` = 'Face to Face Meeting'
65 GROUP BY `to` HAVING count(*) > '".intval($no_of_assurees)."'");
66 return intval(query_get_number_of_rows($res)+1);
67 }
68
69 function get_given_assurances ($userid)
70 {
71 $res = query_init ("select * from `notary` where `from`='".intval($userid)."' and `from` != `to` order by `id` asc");
72 return $res;
73 }
74
75 function get_received_assurances ($userid)
76 {
77 $res = query_init ("select * from `notary` where `to`='".intval($userid)."' and `from` != `to` order by `id` asc ");
78 return $res;
79 }
80
81 function get_given_assurances_summary ($userid)
82 {
83 $res = query_init ("select count(*) as number,points,awarded,method from notary where `from`='".intval($userid)."' group by points,awarded,method");
84 return $res;
85 }
86
87 function get_received_assurances_summary ($userid)
88 {
89 $res = query_init ("select count(*) as number,points,awarded,method from notary where `to`='".intval($userid)."' group by points,awarded,method");
90 return $res;
91 }
92
93 function get_user ($userid)
94 {
95 $res = query_init ("select * from `users` where `id`='".intval($userid)."'");
96 return mysql_fetch_assoc($res);
97 }
98
99 function get_cats_state ($userid)
100 {
101
102 $res = query_init ("select * from `cats_passed` inner join `cats_variant` on `cats_passed`.`variant_id` = `cats_variant`.`id` and `cats_variant`.`type_id` = 1
103 WHERE `cats_passed`.`user_id` = '".intval($userid)."'");
104 return mysql_num_rows($res);
105 }
106
107 function calc_experience ($row,&$points,&$experience,&$sum_experience)
108 {
109 if ($row['awarded'] < $row['points'])
110 $apoints += $row['points'];
111 else
112 $apoints += $row['awarded'];
113
114 $points += $apoints;
115
116 $experience = "&nbsp;";
117 if ($row['method'] == "Face to Face Meeting")
118 {
119 $sum_experience = $sum_experience +2;
120 $experience = "2";
121 }
122 return $apoints;
123 }
124
125 function calc_assurances ($row,&$points,&$experience,&$sumexperience,&$awarded)
126 {
127 $awarded = calc_points($row);
128
129 if ($awarded > 100)
130 {
131 $experience = $awarded - 100; // needs to be fixed in the future (limit 50 pts and/or no experience if pts > 100)
132 $awarded = 100;
133 }
134 else
135 $experience = 0;
136
137 switch ($row['method'])
138 {
139 case 'Thawte Points Transfer':
140 case 'CT Magazine - Germany':
141 case 'Temporary Increase': // Current usage of 'Temporary Increase' may break audit aspects, needs to be reimplemented
142 $awarded=sprintf("<strong style='color: red'>%s</strong>",_("Revoked"));
143 $experience=0;
144 break;
145 default:
146 $points += $awarded;
147 }
148 $sumexperience = $sumexperience + $experience;
149 }
150
151
152 function show_user_link ($name,$userid)
153 {
154 $name = trim($name);
155 if($name == "")
156 {
157 if ($userid == 0)
158 $name = _("System");
159 else
160 $name = _("Deleted account");
161 }
162 else
163 $name = "<a href='wot.php?id=9&amp;userid=".intval($userid)."'>$name</a>";
164 return $name;
165 }
166
167 function get_assurer_ranking($userid,&$num_of_assurances,&$rank_of_assurer)
168 {
169 $num_of_assurances = get_number_of_assurances (intval($userid));
170 $rank_of_assurer = get_top_assurer_position($num_of_assurances);
171 }
172
173 function get_assuree_ranking($userid,&$num_of_assurees,&$rank_of_assuree)
174 {
175 $num_of_assurees = get_number_of_assurees (intval($userid));
176 $rank_of_assuree = get_top_assuree_position($num_of_assurees);
177 }
178
179
180 // ************* html table definitions ******************
181
182 function output_ranking($userid)
183 {
184 get_assurer_ranking($userid,$num_of_assurances,$rank_of_assurer);
185 get_assuree_ranking($userid,$num_of_assurees,$rank_of_assuree);
186
187 ?>
188 <table align="center" valign="middle" border="0" cellspacing="0" cellpadding="0" class="wrapper">
189 <tr>
190 <td class="title"><?=_("Assurer Ranking")?></td>
191 </tr>
192 <tr>
193 <td class="DataTD"><?=sprintf(_("You have made %s assurances which ranks you as the #%s top assurer."), intval($num_of_assurances), intval($rank_of_assurer) )?></td>
194 </tr>
195 <tr>
196 <td class="DataTD"><?=sprintf(_("You have received %s assurances which ranks you as the #%s top assuree."), intval($num_of_assurees), intval($rank_of_assuree) )?></td>
197 </tr>
198 </table>
199 <br/>
200 <?
201 }
202
203 function output_assurances_header($title)
204 {
205 ?>
206 <table align="center" valign="middle" border="0" cellspacing="0" cellpadding="0" class="wrapper">
207 <tr>
208 <td colspan="7" class="title"><?=$title?></td>
209 </tr>
210 <tr>
211 <td class="DataTD"><strong><?=_("ID")?></strong></td>
212 <td class="DataTD"><strong><?=_("Date")?></strong></td>
213 <td class="DataTD"><strong><?=_("Who")?></strong></td>
214 <td class="DataTD"><strong><?=_("Points")?></strong></td>
215 <td class="DataTD"><strong><?=_("Location")?></strong></td>
216 <td class="DataTD"><strong><?=_("Method")?></strong></td>
217 <td class="DataTD"><strong><?=_("Experience Points")?></strong></td>
218 </tr>
219 <?
220 }
221
222 function output_assurances_footer($points_txt,$points,$experience_txt,$sumexperience)
223 {
224 ?>
225 <tr>
226 <td class="DataTD" colspan="3"><strong><?=$points_txt?>:</strong></td>
227 <td class="DataTD"><?=$points?></td>
228 <td class="DataTD">&nbsp;</td>
229 <td class="DataTD"><strong><?=$experience_txt?>:</strong></td>
230 <td class="DataTD"><?=$sumexperience?></td>
231 </tr>
232 </table>
233 <br/>
234 <?
235 }
236
237 function output_assurances_row($assuranceid,$date,$when,$name,$awarded,$points,$location,$method,$experience)
238 {
239
240 $tdstyle="";
241 $emopen="";
242 $emclose="";
243
244 if ($awarded == $points)
245 {
246 if ($awarded == "0")
247 {
248 if ($when < "2009-09-01")
249 {
250 $tdstyle="style='background-color: #ffff80'";
251 $emopen="<em>";
252 $emclose="</em>";
253 }
254 }
255 }
256
257 ?>
258 <tr>
259 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$assuranceid?><?=$emclose?></td>
260 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$date?><?=$emclose?></td>
261 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$name?><?=$emclose?></td>
262 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$awarded?><?=$emclose?></td>
263 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$location?><?=$emclose?></td>
264 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$method?><?=$emclose?></td>
265 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$experience?><?=$emclose?></td>
266 </tr>
267 <?
268 }
269
270 function output_summary_header()
271 {
272 ?>
273 <table align="center" valign="middle" border="0" cellspacing="0" cellpadding="0" class="wrapper">
274 <tr>
275 <td colspan="4" class="title"><?=_("Summary of your Points")?></td>
276 </tr>
277 <tr>
278 <td class="DataTD"><strong><?=_("Description")?></strong></td>
279 <td class="DataTD"><strong><?=_("Points")?></strong></td>
280 <td class="DataTD"><strong><?=_("Countable Points")?></strong></td>
281 <td class="DataTD"><strong><?=_("Remark")?></strong></td>
282 </tr>
283 <?
284 }
285
286 function output_summary_footer()
287 {
288 ?>
289 </table>
290 <br/>
291 <?
292 }
293
294 function output_summary_row($title,$points,$points_countable,$remark)
295 {
296 ?>
297 <tr>
298 <td class="DataTD"><strong><?=$title?></strong></td>
299 <td class="DataTD"><?=$points?></td>
300 <td class="DataTD"><?=$points_countable?></td>
301 <td class="DataTD"><?=$remark?></td>
302 </tr>
303 <?
304 }
305
306
307 // ************* output given assurances ******************
308
309 function output_given_assurances_content($userid,&$points,&$sum_experience)
310 {
311 $points = 0;
312 $sumexperience = 0;
313 $res = get_given_assurances(intval($userid));
314 while($row = mysql_fetch_assoc($res))
315 {
316 $fromuser = get_user (intval($row['to']));
317 $apoints = calc_experience ($row,$points,$experience,$sum_experience);
318 $name = show_user_link ($fromuser['fname']." ".$fromuser['lname'],intval($row['to']));
319 output_assurances_row (intval($row['id']),$row['date'],$row['when'],$name,$apoints,intval($row['points']),$row['location'],$row['method']==""?"":_(sprintf("%s", $row['method'])),$experience);
320 }
321 }
322
323 // ************* output received assurances ******************
324
325 function output_received_assurances_content($userid,&$points,&$sum_experience)
326 {
327 $points = 0;
328 $sumexperience = 0;
329 $res = get_received_assurances(intval($userid));
330 while($row = mysql_fetch_assoc($res))
331 {
332 $fromuser = get_user (intval($row['from']));
333 calc_assurances ($row,$points,$experience,$sum_experience,$awarded);
334 $name = show_user_link ($fromuser['fname']." ".$fromuser['lname'],intval($row['from']));
335 output_assurances_row (intval($row['id']),$row['date'],$row['when'],$name,$awarded,intval($row['points']),$row['location'],$row['method']==""?"":_(sprintf("%s", $row['method'])),$experience);
336 }
337 }
338
339 // ************* output summary table ******************
340
341 function check_date_limit ($userid,$age)
342 {
343 $dob = date("Y-m-d", mktime(0,0,0,date("m"),date("d"),date("Y")-$age));
344 $res = query_init ("select id from `users` where `id`='".$userid."' and `dob` < '$dob'");
345 return intval(query_get_number_of_rows($res));
346 }
347
348 function calc_points($row)
349 {
350 $awarded = intval($row['awarded']);
351 if ($awarded == "")
352 $awarded = 0;
353 if (intval($row['points']) < $awarded)
354 $points = $awarded; // if 'sum of added points' > 100, awarded shows correct value
355 else
356 $points = intval($row['points']); // on very old assurances, awarded is '0' instead of correct value
357 switch ($row['method'])
358 {
359 case 'Thawte Points Transfer': // revoke all Thawte-points (as per arbitration)
360 case 'CT Magazine - Germany': // revoke c't (only one test-entry)
361 case 'Temporary Increase': // revoke 'temporary increase' (Current usage breaks audit aspects, needs to be reimplemented)
362 $points = 0;
363 break;
364 case 'Administrative Increase': // ignore AI with 2 points or less (historical for experiance points, now other calculation)
365 if ($points <= 2) // maybe limit to 35/50 pts in the future?
366 $points = 0;
367 break;
368 case 'Unknown': // to be revoked in the future? limit to max 50 pts?
369 case 'Trusted Third Parties': // to be revoked in the future? limit to max 35 pts?
370 case '': // to be revoked in the future? limit to max 50 pts?
371 case 'Face to Face Meeting': // normal assurances, limit to 35/50 pts in the future?
372 break;
373 default: // should never happen ... ;-)
374 $points = 0;
375 }
376 if ($points < 0) // ignore negative points (bug needs to be fixed)
377 $points = 0;
378 return $points;
379 }
380
381 function max_points($userid)
382 {
383 return output_summary_content ($userid,0);
384 }
385
386 function output_summary_content($userid,$display_output)
387 {
388 $sum_points = 0;
389 $sum_experience = 0;
390 $sum_experience_other = 0;
391 $max_points = 100;
392 $max_experience = 50;
393
394 $experience_limit_reached_txt = _("Limit reached");
395
396 if (check_date_limit($userid,18) != 1)
397 {
398 $max_experience = 10;
399 $experience_limit_reached_txt = _("Limit given by PoJAM reached");
400 }
401 if (check_date_limit($userid,14) != 1)
402 {
403 $max_experience = 0;
404 $experience_limit_reached_txt = _("Limit given by PoJAM reached");
405 }
406
407 $res = get_received_assurances_summary($userid);
408 while($row = mysql_fetch_assoc($res))
409 {
410 $points = calc_points ($row);
411
412 if ($points > $max_points) // limit to 100 points, above is experience (needs to be fixed)
413 {
414 $sum_experience_other = $sum_experience_other+($points-$max_points)*intval($row['number']);
415 $points = $max_points;
416 }
417 $sum_points += $points*intval($row['number']);
418 }
419
420 $res = get_given_assurances_summary($userid);
421 while($row = mysql_fetch_assoc($res))
422 {
423 switch ($row['method'])
424 {
425 case 'Face to Face Meeting': // count Face to Face only
426 $sum_experience += 2*intval($row['number']);
427 break;
428 }
429
430 }
431
432 if ($sum_points > $max_points)
433 {
434 $sum_points_countable = $max_points;
435 $remark_points = _("Limit reached");
436 }
437 else
438 {
439 $sum_points_countable = $sum_points;
440 $remark_points = "&nbsp;";
441 }
442 if ($sum_experience > $max_experience)
443 {
444 $sum_experience_countable = $max_experience;
445 $remark_experience = $experience_limit_reached_txt;
446 }
447 else
448 {
449 $sum_experience_countable = $sum_experience;
450 $remark_experience = "&nbsp;";
451 }
452
453 if ($sum_experience_countable + $sum_experience_other > $max_experience)
454 {
455 $sum_experience_other_countable = $max_experience-$sum_experience_countable;
456 $remark_experience_other = $experience_limit_reached_txt;
457 }
458 else
459 {
460 $sum_experience_other_countable = $sum_experience_other;
461 $remark_experience_other = "&nbsp;";
462 }
463
464 if ($sum_points_countable < $max_points)
465 {
466 if ($sum_experience_countable != 0)
467 $remark_experience = _("Points on hold due to less assurance points");
468 $sum_experience_countable = 0;
469 if ($sum_experience_other_countable != 0)
470 $remark_experience_other = _("Points on hold due to less assurance points");
471 $sum_experience_other_countable = 0;
472 }
473
474 $issue_points = 0;
475 $cats_test_passed = get_cats_state ($userid);
476 if ($cats_test_passed == 0)
477 {
478 $issue_points_txt = "<strong style='color: red'>"._("You have to pass the CAcert Assurer Challenge (CATS-Test) to be an Assurer")."</strong>";
479 if ($sum_points_countable < $max_points)
480 {
481 $issue_points_txt = "<strong style='color: red'>";
482 $issue_points_txt .= sprintf(_("You need %s assurance points and the passed CATS-Test to be an Assurer"), intval($max_points));
483 $issue_points_txt .= "</strong>";
484 }
485 }
486 else
487 {
488 $experience_total = $sum_experience_countable+$sum_experience_other_countable;
489 $issue_points_txt = "";
490 if ($sum_points_countable == $max_points)
491 $issue_points = 10;
492 if ($experience_total >= 10)
493 $issue_points = 15;
494 if ($experience_total >= 20)
495 $issue_points = 20;
496 if ($experience_total >= 30)
497 $issue_points = 25;
498 if ($experience_total >= 40)
499 $issue_points = 30;
500 if ($experience_total >= 50)
501 $issue_points = 35;
502 if ($issue_points != 0)
503 $issue_points_txt = sprintf(_("You may issue up to %s points"),$issue_points);
504 }
505 if ($display_output)
506 {
507 output_summary_row (_("Assurance Points you received"),$sum_points,$sum_points_countable,$remark_points);
508 output_summary_row (_("Total Experience Points by Assurance"),$sum_experience,$sum_experience_countable,$remark_experience);
509 output_summary_row (_("Total Experience Points (other ways)"),$sum_experience_other,$sum_experience_other_countable,$remark_experience_other);
510 output_summary_row (_("Total Points"),"&nbsp;",$sum_points_countable + $sum_experience_countable + $sum_experience_other_countable,$issue_points_txt);
511 }
512 return $issue_points;
513 }
514
515 function output_given_assurances($userid)
516 {
517 output_assurances_header(_("Assurance Points You Issued"));
518 output_given_assurances_content($userid,$points,$sum_experience);
519 output_assurances_footer(_("Total Points Issued"),$points,_("Total Experience Points"),$sum_experience);
520 }
521
522 function output_received_assurances($userid)
523 {
524 output_assurances_header(_("Your Assurance Points"));
525 output_received_assurances_content($userid,$points,$sum_experience);
526 output_assurances_footer(_("Total Assurance Points"),$points,_("Total Experience Points"),$sum_experience);
527 }
528
529 function output_summary($userid)
530 {
531 output_summary_header();
532 output_summary_content($userid,1);
533 output_summary_footer();
534 }
535
536 function output_end_of_page()
537 {
538 ?>
539 <p>[ <a href='javascript:history.go(-1)'><?=_("Go Back")?></a> ]</p>
540 <?
541 }
542 ?>