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
8//this script may only be included - so its better to die if called directly.
9if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
10	header("location: index.php");
11	exit;
12}
13
14include_once('lib/polls/polllib_shared.php');
15
16/**
17 * PollLib
18 *
19 * @uses PollLibShared
20 */
21class PollLib extends PollLibShared
22{
23
24	/**
25	 * @param $offset
26	 * @param $maxRecords
27	 * @param $sort_mode
28	 * @param $find
29	 * @return array
30	 */
31	function list_polls($offset, $maxRecords, $sort_mode, $find)
32	{
33		if ($find) {
34			$findesc = '%' . $find . '%';
35
36			$mid = " where (`title` like ?)";
37			$bindvars = [$findesc];
38		} else {
39			$mid = "";
40			$bindvars = [];
41		}
42
43		$query = "select * from `tiki_polls` $mid order by " . $this->convertSortMode($sort_mode);
44		$query_cant = "select count(*) from `tiki_polls` $mid";
45		$result = $this->query($query, $bindvars, $maxRecords, $offset);
46		$cant = $this->getOne($query_cant, $bindvars);
47		$ret = [];
48
49		while ($res = $result->fetchRow()) {
50			$query = "select count(*) from `tiki_poll_options` where `pollId`=?";
51
52			$res["options"] = $this->getOne($query, [$res["pollId"]]);
53			$ret[] = $res;
54		}
55
56		$retval = [];
57		$retval["data"] = $ret;
58		$retval["cant"] = $cant;
59		return $retval;
60	}
61
62	/**
63	 * @param $offset
64	 * @param $maxRecords
65	 * @param $sort_mode
66	 * @param $find
67	 * @return array
68	 */
69	function list_active_polls($offset, $maxRecords, $sort_mode, $find)
70	{
71
72		if ($find) {
73			$findesc = '%' . $find . '%';
74			$mid = " where (`active`=? or `active`=? or `active`=?) and `publishDate`<=? and (`title` like ?)";
75			$bindvars = ['a', 'c', 'o', (int) $this->now, $findesc];
76		} else {
77			$mid = " where (`active`=? or `active`=? or `active`=?) and `publishDate`<=? ";
78			$bindvars = ['a', 'c', 'o', (int) $this->now];
79		}
80
81		$query = "select * from `tiki_polls` $mid order by " . $this->convertSortMode($sort_mode);
82		$query_cant = "select count(*) from `tiki_polls` $mid";
83		$result = $this->query($query, $bindvars, $maxRecords, $offset);
84		$cant = $this->getOne($query_cant, $bindvars);
85		$ret = [];
86
87		while ($res = $result->fetchRow()) {
88			$ret[] = $res;
89		}
90
91		$retval = [];
92		$retval["data"] = $ret;
93		$retval["cant"] = $cant;
94		return $retval;
95	}
96
97	/**
98	 * @param $offset
99	 * @param $maxRecords
100	 * @param $sort_mode
101	 * @param $find
102	 * @return array
103	 */
104	function list_all_polls($offset, $maxRecords, $sort_mode, $find)
105	{
106
107		if ($find) {
108			$findesc = '%' . $find . '%';
109			$mid = " where `publishDate`<=? and (`title` like ?)";
110			$bindvars = [(int) $this->now, $findesc];
111		} else {
112			$mid = " where `publishDate`<=? ";
113			$bindvars = [(int) $this->now];
114		}
115
116		$query = "select * from `tiki_polls` $mid order by " . $this->convertSortMode($sort_mode);
117		$query_cant = "select count(*) from `tiki_polls` $mid";
118		$result = $this->query($query, $bindvars, $maxRecords, $offset);
119		$cant = $this->getOne($query_cant, $bindvars);
120		$ret = [];
121
122		while ($res = $result->fetchRow()) {
123			$ret[] = $res;
124		}
125
126		$retval = [];
127		$retval["data"] = $ret;
128		$retval["cant"] = $cant;
129		return $retval;
130	}
131
132	/**
133	 * @return TikiDb_Pdo_Result|TikiDb_Adodb_Result
134	 */
135	function set_last_poll()
136	{
137		$query = "select max(`publishDate`) from `tiki_polls` where `publishDate`<=?";
138		$last = $this->getOne($query, [(int) $this->now]);
139		$query = "update `tiki_polls` set `active`=? where `publishDate`=?";
140		return $this->query($query, ['c', $last]);
141	}
142
143	/**
144	 * @return TikiDb_Pdo_Result|TikiDb_Adodb_Result
145	 */
146	function close_all_polls()
147	{
148		$query = "select max(`publishDate`) from `tiki_polls` where `publishDate`<=?";
149		$last = $this->getOne($query, [(int) $this->now]);
150		$query = "update `tiki_polls` set `active`=? where `publishDate`<?";
151		return $this->query($query, ['x', (int) $last]);
152	}
153
154	/**
155	 * @return TikiDb_Pdo_Result|TikiDb_Adodb_Result
156	 */
157	function active_all_polls()
158	{
159		$query = "update `tiki_polls` set `active`=? where `publishDate`<=?";
160		return $this->query($query, ['a', (int) $this->now]);
161	}
162
163	/**
164	 * @param $optionId
165	 *
166	 * @return TikiDb_Pdo_Result|TikiDb_Adodb_Result
167	 */
168	function remove_poll_option($optionId)
169	{
170		$query = "delete from `tiki_poll_options` where `optionId`=?";
171		return $this->query($query, [$optionId]);
172	}
173
174	/**
175	 * @param $optionId
176	 * @return bool
177	 */
178	function get_poll_option($optionId)
179	{
180		$query = "select * from `tiki_poll_options` where `optionId`=?";
181		$result = $this->query($query, [$optionId]);
182		if (! $result->numRows()) {
183			return false;
184		}
185		$res = $result->fetchRow();
186		return $res;
187	}
188}
189