-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch2.php
178 lines (162 loc) · 4.74 KB
/
match2.php
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
<?php
// put full path to Smarty.class.php
require('/usr/local/lib/php/Smarty/libs/Smarty.class.php');
$smarty = new Smarty();
$smarty->setTemplateDir('./smarty/templates');
$smarty->setCompileDir('./smarty/templates_c');
$smarty->setCacheDir('./smarty/cache');
$smarty->setConfigDir('./smarty/configs');
$input_file[0] = './inventura_parties_candidate2012.json';
$input_file[1] = './inventura_votes_candidates.json';
//extract user values
$user = get_user_values();
//read parties and mps
$parties_0 = json_decode(file_get_contents($input_file[0]));
$mps = json_decode(file_get_contents($input_file[1]));
//calculate match
$results_2010 = calc_match($user,$parties_0,'party');
$results_mps = calc_match($user,$mps,'mp');
//create additional link for comparison
$additional_string = create_additional_string($results_2010);
$smarty->assign('query_string', $_SERVER['QUERY_STRING'] . '&' . $additional_string);
$smarty->assign('results_2010', $results_2010);
$smarty->assign('results_mps', $results_mps);
$smarty->display('match2.tpl');
/**
* creates additional string with order of parties
*/
function create_additional_string($results) {
$out = 'order=';
foreach ($results as $row) {
$out .= $row['id'] . '|' . $row['result_percent'] . ',';
}
return rtrim($out,',');
}
/**
* calculates results for one set
*/
function calc_match($user,$set,$what,$extra=2) {
$results = array();
foreach ($set as $s) {
$sum = 0;
$count = 0;
if (count($user['vote']) > 0) {
foreach($user['vote'] as $key => $uv) {
//weight
if (isset($user['weight'][$key])) $w = $extra;
else $w = 1;
//existing divisions only:
if ((property_exists($s,'vote')) and (property_exists($s->vote,$key))) {
$sum = $sum + $w*$s->vote->$key*$uv;
}
$count = $count + $w;
}
}
if ($count == 0) $count = 1; // to allow match = 0/1 = 0;
if ($what == 'party') {
$results[] = array(
'id' => $s->id,
'name' => $s->name,
'short_name' => $s->short_name,
'friendly_name' => $s->friendly_name,
'result' => $sum/$count,
'result_percent' => round(100*$sum/$count),
);
} else {
$c2010 = party2party($s->candidate2010);
$tmp = array(
'last_name' => $s->last_name,
'first_name' => $s->first_name,
'id' => $s->id,
'name_2010' => $c2010['name'],
'short_name_2010' => $c2010['short_name'],
'friendly_name_2010' => $c2010['friendly_name'],
'result' => $sum/$count,
'result_percent' => round(100*$sum/$count),
);
if (property_exists($s,'candidate2012')) {
$c2012 = party2party($s->candidate2012);
$tmp['name_2012'] = $c2012['name'];
$tmp['short_name_2012'] = $c2012['short_name'];
$tmp['friendly_name_2012'] = $c2012['friendly_name'];
} else {
$tmp['name_2012'] = 'Nekandiduje';
$tmp['short_name_2012'] = '-';
$tmp['friendly_name_2012'] = 'n';
}
$results[] = $tmp;
}
}
//sort by result
foreach ($results as $key => $row) {
$result[$key] = $row['result'];
}
array_multisort($result, SORT_DESC, $results);
return $results;
}
/**
* extracts user's answers
*/
function get_user_values() {
$user = array();
if (count($_GET) > 0) {
foreach ($_GET as $key => $param) {
//votes
if (substr($key,0,2) == 'q-')
$user['vote'][substr($key,2)] = $param;
else if (substr($key,0,2) == 'c-')
$user['weight'][substr($key,2)] = true;
}
} else
return false;
return $user;
}
/**
* add party information
*/
function party2party($party) {
$ar = array(
'MOST-HÍD' => array(
'name' => 'Most-Híd',
'short_name' => 'M-H',
'friendly_name' => 'most-hid'
),
'KDH' => array(
'name' => 'Kresťansko-demokratické hnutie',
'short_name' => 'KDH',
'friendly_name' => 'kdh'
),
'SNS' => array(
'name' => 'Slovenská národná strana',
'short_name' => 'SNS',
'friendly_name' => 'sns'
),
'SDKÚ – DS' => array(
'name' => 'Slovenská demokratická a kresťanská únia – Demokratická strana',
'short_name' => 'SDKÚ-DS',
'friendly_name' => 'sdku-ds'
),
'SMER – SD' => array(
'name' => 'Smer – sociálna demokracia',
'short_name' => 'SMER-SD',
'friendly_name' => 'smer-sd'
),
'SaS' => array(
'name' => 'Sľoboda a solidarita',
'short_name' => 'SaS',
'friendly_name' => 'sas'
),
'OĽaNO' => array(
'name' => 'Obyčajní ľudia a nezávislé osobnosti',
'short_name' => 'OĽaNO',
'friendly_name' => 'ol'
),
'NAS'=> array(
'name' => 'Národ a spravodlivosť',
'short_name' => 'NAS',
'friendly_name' => 'nas'
)
);
return $ar[$party];
}
?>