aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Module/Vote.php
blob: 52d6a4bea105c6f0d0e48afcb1914106d2b7f4c1 (plain) (blame)
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
<?php
namespace Zotlabs\Module;

use App;
use Zotlabs\Web\Controller;
use Zotlabs\Lib\Activity;
use Zotlabs\Daemon\Master;
use Zotlabs\Lib\Libsync;

class Vote extends Controller {

	function init() {

		$ret = [ 'success' => false, 'message' => EMPTY_STR ];

		$channel = App::get_channel();

		if (! $channel) {
			$ret['message'] = t('Permission denied.');
			json_return_and_die($ret);
		}


		$fetch = null;
		$id = argv(1);
		$response = $_REQUEST['answer'];
		
		if ($id) {
			$fetch = q("select * from item where id = %d limit 1",
				intval($id)
			);
		}


		if ($fetch && $fetch[0]['obj_type'] === 'Question') {
			$obj = json_decode($fetch[0]['obj'],true);

		}
		else {
			$ret['message'] = t('Poll not found.');
			json_return_and_die($ret);
		}

		$valid = false;
		
		if ($obj['oneOf']) {
			foreach($obj['oneOf'] as $selection) {
				//		logger('selection: ' . $selection);
				//		logger('response: ' . $response);
				if($selection['name'] && $selection['name'] === $response) {
					$valid = true;
				}
			}
		}

		$choices = [];
		if ($obj['anyOf']) {
			foreach ($obj['anyOf'] as $selection) {
				$choices[] = $selection['name'];
			}
			foreach ($response as $res) {
				if (! in_array($res,$choices)) {
					$valid = false;
					break;
				}
				$valid = true;
			}
		}

		if (! $valid) {
			$ret['message'] = t('Invalid response.');
			json_return_and_die($ret);
		}

		if (! is_array($response)) {
			$response = [ $response ];
		}

		foreach ($response as $res) {

			$item = [];


			$item['aid'] = $channel['channel_account_id'];
			$item['uid'] = $channel['channel_id'];
			$item['item_origin'] = true;
			$item['parent'] = $fetch[0]['id'];
			$item['parent_mid'] = $fetch[0]['mid'];
			$item['uuid'] = new_uuid();
			$item['mid'] = z_root() . '/item/' . $item['uuid'];
			$item['verb'] = 'Answer';
			$item['title'] = $res;
			$item['author_xchan'] = $channel['channel_hash'];
			$item['owner_xchan'] = $fetch[0]['author_xchan'];
			
			$item['obj'] = $obj;
			$item['obj_type'] = 'Question';

			$x = item_store($item); 

			retain_item($fetch[0]['id']);

			if($x['success']) {
				$itemid = $x['item_id'];
				Master::Summon( [ 'Notifier', 'like', $itemid ] );
			}
		
			$r = q("select * from item where id = %d",
				intval($itemid)
			);
			if ($r) {
				xchan_query($r);
				$sync_item = fetch_post_tags($r);
				Libsync::build_sync_packet($channel['channel_id'], [ 'item' => [ encode_item($sync_item[0],true) ] ]);
			}
		}
		$ret['success'] = true;
		$ret['message'] = t('Response submitted. Updates may not appear instantly.');
		json_return_and_die($ret);
	}
}