1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8function wikiplugin_vote_info()
9{
10	return [
11		'name' => tra('Vote'),
12		'documentation' => 'PluginVote',
13		'description' => tra('Create a tracker for voting'),
14		'prefs' => [ 'feature_trackers', 'wikiplugin_vote' ],
15		'body' => tra('Title'),
16		'iconname' => 'thumbs-up',
17		'introduced' => 2,
18		'params' => [
19			'trackerId' => [
20				'required' => true,
21				'name' => tra('Tracker ID'),
22				'description' => tra('Numeric value representing the tracker ID'),
23				'since' => '2.0',
24				'filter' => 'digits',
25				'default' => '',
26				'profile_reference' => 'tracker',
27			],
28			'fields' => [
29				'required' => true,
30				'name' => tra('Fields'),
31				'description' => tra('Colon-separated list of field IDs to be displayed. If not set all the fields that
32					can be used (except IP, user, system, private fields) are used. Example:') . ' <code>2:4:5</code>',
33				'since' => '2.0',
34				'default' => '',
35				'separator' => ':',
36				'profile_reference' => 'tracker_field',
37				'parent' => 'input[name="params[trackerId]"]',
38				'parentkey' => 'tracker_id',
39			],
40			'show_percent' => [
41				'required' => false,
42				'name' => tra('Show Percentage'),
43				'description' => tra('Choose whether to show the percentage of the vote each option received (not
44					shown by default)'),
45				'since' => '2.0',
46				'filter' => 'alpha',
47				'default' => 'n',
48				'options' => [
49					['text' => '', 'value' => ''],
50					['text' => tra('Yes'), 'value' => 'y'],
51					['text' => tra('No'), 'value' => 'n'],
52				],
53			],
54			'show_bar' => [
55				'required' => false,
56				'name' => tra('Show Bar'),
57				'description' => tra('Choose whether to show a bar representing the number of votes each option
58					received (not shown by default)'),
59				'since' => '2.0',
60				'filter' => 'alpha',
61				'default' => 'n',
62				'options' => [
63					['text' => '', 'value' => ''],
64					['text' => tra('Yes'), 'value' => 'y'],
65					['text' => tra('No'), 'value' => 'n'],
66				],
67			],
68			'show_stat' => [
69				'required' => false,
70				'name' => tra('Show Stats'),
71				'description' => tra('Choose whether to show the voting results (shown by default)'),
72				'since' => '2.0',
73				'filter' => 'alpha',
74				'default' => 'y',
75				'options' => [
76					['text' => '', 'value' => ''],
77					['text' => tra('Yes'), 'value' => 'y'],
78					['text' => tra('No'), 'value' => 'n'],
79				],
80			],
81			'show_stat_only_after' => [
82				'required' => false,
83				'name' => tra('Show Stats After'),
84				'description' => tra('Choose whether to show the voting results only after the date given in the
85					tracker configuration (not set by default)'),
86				'since' => '2.0',
87				'filter' => 'alpha',
88				'default' => '',
89				'options' => [
90					['text' => '', 'value' => ''],
91					['text' => tra('Yes'), 'value' => 'y'],
92					['text' => tra('No'), 'value' => 'n'],
93				],
94			],
95			'show_creator' => [
96				'required' => false,
97				'name' => tra('Show Creator'),
98				'description' => tra('Choose whether to display the user name of the creator of the voting tracker (not
99					shown by default)'),
100				'since' => '2.0',
101				'filter' => 'alpha',
102				'default' => '',
103				'options' => [
104					['text' => '', 'value' => ''],
105					['text' => tra('Yes'), 'value' => 'y'],
106					['text' => tra('No'), 'value' => 'n'],
107				],
108			],
109			'status' => [
110				'required' => false,
111				'name' => tra('Status Filter'),
112				'description' => tra('Only show items matching certain status filters'),
113				'since' => '2.0',
114				'filter' => 'alpha',
115				'default' => 'o',
116				'options' => [
117					['text' => '', 'value' => ''],
118					['text' => tra('Open'), 'value' => 'o'],
119					['text' => tra('Pending'), 'value' => 'p'],
120					['text' => tra('Closed'), 'value' => 'c'],
121					['text' => tra('Open & Pending'), 'value' => 'op'],
122					['text' => tra('Open & Closed'), 'value' => 'oc'],
123					['text' => tra('Pending & Closed'), 'value' => 'pc'],
124					['text' => tra('Open, Pending & Closed'), 'value' => 'opc'],
125				],
126			],
127			'float' => [
128				'required' => false,
129				'name' => tra('Float'),
130				'description' => tra('Align the plugin on the page, allowing other elements to wrap around it (not set
131					by default)'),
132				'since' => '2.0',
133				'filter' => 'alpha',
134				'default' => '',
135				'options' => [
136					['text' => '', 'value' => ''],
137					['text' => tra('Left'), 'value' => 'left'],
138					['text' => tra('Right'), 'value' => 'right'],
139					['text' => tra('None'), 'value' => 'none'],
140				],
141			],
142			'show_toggle' => [
143				'required' => false,
144				'name' => tra('Show Toggle'),
145				'description' => tra('Show toggle or not to display the form and the results'),
146				'since' => '10.0',
147				'filter' => 'alpha',
148				'default' => '',
149				'options' => [
150					['text' => '', 'value' => ''],
151					['text' => tra('Yes'), 'value' => 'y'],
152					['text' => tra('No'), 'value' => 'n'],
153				],
154			],
155		],
156	];
157}
158
159function wikiplugin_vote($data, $params)
160{
161	global $user, $prefs, $tiki_p_admin_trackers, $tiki_p_view_trackers;
162	$trklib = TikiLib::lib('trk');
163	$tikilib = TikiLib::lib('tiki');
164	$smarty = TikiLib::lib('smarty');
165	extract($params, EXTR_SKIP);
166
167	if ($prefs['feature_trackers'] != 'y' || ! isset($trackerId) || ! ($tracker = $trklib->get_tracker($trackerId))) {
168		return $smarty->fetch("wiki-plugins/error_tracker.tpl");
169	}
170
171	$smarty->assign_by_ref('tracker', $tracker);
172
173	if (isset($float)) {
174		$smarty->assign('float', $float);
175	} else {
176		$smarty->assign('float', '');
177	}
178	if ($trklib->get_user_item($trackerId, ['oneUserItem' => 'y'])) {
179		$smarty->assign('has_already_voted', 'y');
180	} else {
181		$smarty->assign('has_already_voted', 'n');
182	}
183	if (empty($fields)) {
184		$fields = $trklib->list_tracker_fields($trackerId);
185		$ff = [];
186		foreach ($fields['data'] as $field) {
187			if ($field['type'] != 'u' && $field['type'] != 'I' && $field['type'] != 'g' && $field['isPublic'] == 'y') {
188				$ff[] = $field['fieldId'];
189			}
190		}
191		if (! empty($ff)) {
192			$params['fields'] = $ff;
193		}
194	}
195	if (isset($show_creator) && $show_creator == 'y') {
196		$tracker = $trklib->get_tracker($trackerId);
197		$smarty->assign_by_ref('tracker_creator', $tracker['user']);
198	}
199	$smarty->assign('options', '');
200	if ($tikilib->user_has_perm_on_object($user, $trackerId, 'tracker', 'tiki_p_create_tracker_items')) {
201		$options = $trklib->get_tracker_options($trackerId);
202		if (! empty($options['start']) || ! empty($options['end'])) {
203			$smarty->assign_by_ref('options', $options);
204		}
205		if ((! empty($options['start']) && $tikilib->now < $options['start']) || (! empty($options['end']) && $tikilib->now > $options['end'])) {
206			$smarty->assign('p_create_tracker_items', 'n');
207			$smarty->assign('vote', '');
208		} else {
209			$smarty->assign('p_create_tracker_items', 'y');// to have different vote in the same page
210			include_once('lib/wiki-plugins/wikiplugin_tracker.php');
211			$vote = wikiplugin_tracker($data, $params);
212			$smarty->assign_by_ref('vote', $vote);
213		}
214	} else {
215		$smarty->assign('p_create_tracker_items', 'n');
216	}
217	if (isset($show_toggle) && $show_toggle == 'n') {
218		$smarty->assign('show_toggle', 'n');
219	}
220	if (! isset($show_stat) || $show_stat == 'y') {
221		$show_stat = 'y';
222		if (isset($show_stat_only_after) && $show_stat_only_after == 'y') {
223			if (! isset($options)) {
224				$options = $trklib->get_tracker_options($trackerId);
225				if (! empty($options['start']) || ! empty($options['end'])) {
226					$smarty->assign_by_ref('options', $options);
227				}
228			}
229			if (! empty($options['end']) && $tikilib->now < $options['end']) {
230				$show_stat = 'n';
231			}
232		}
233		if ($show_stat == 'y') {
234			include_once('lib/wiki-plugins/wikiplugin_trackerstat.php');
235			$stat = wikiplugin_trackerstat($data, $params);
236			$smarty->assign_by_ref('stat', $stat);
237		} else {
238			$smarty->assign('stat', '');
239		}
240	} else {
241		$smarty->assign('stat', '');
242	}
243	$smarty->assign('date', $tikilib->now);
244	return $smarty->fetch('wiki-plugins/wikiplugin_vote.tpl');
245}
246