summaryrefslogtreecommitdiffstats
path: root/admin/survey/classes/class.SurveySession.php
blob: 390c0e2d707ffe59c3081019645140133e7c47c3 (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
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
179
180
<?php 
/** Shranjujemo nastavitve ankete. Za vsako anketo unikatno.
 * 
 * @author veselicg
 *
 */

/** Ker za statičen klas ne moremo narediti destructor funkcije,
 *  si pomagamo s lastno destruktor instanco, 
 *  katera pokliče destruct funkcijo našega statičnega razreda
 * 
 * @author veselicg
 *
 */
class SurveySessionDestructor
{
	/** pokličemo destruktor funkcijo
 	 * 
	 */
	public function __destruct()
	{
		SurveySession::destruct();
	}
}

class SurveySession {
	
	private static $destructorInstance;
	private static $anketa = null;
	private static $data = array();
	private static $updated = false;
	
	
	static function sessionStart($anketa = null) {
		if (null === self::$destructorInstance)
			self::$destructorInstance = new SurveySessionDestructor();
		
		if ($anketa == null || (int)$anketa == 0 || !is_numeric($anketa)) 
		{
			throw new Exception('Survey ID is mandatory for SurveySession!');
		}
		self::$anketa = $anketa;
		
		# preberemo vse nastavitve za to anketo
		$sql = sisplet_query("SELECT what,value FROM srv_survey_session WHERE ank_id='".self::$anketa."'");
		while (list($what,$value) = mysqli_fetch_row($sql)) 
		{
			self::$data[$what] = unserialize($value); 
		}
	}

	static function get($what=null) {
		if (self::$anketa == null) 
		{
			throw new Exception('Survey ID is mandatory for SurveySession!');
			return null;
		}
		
		if ($what == null) 
		{
			return self::$data;
		} 
		else if (isset(self::$data[$what])) 
		{
			return self::$data[$what];
		} 
		else
		{
			return null;
		} 
		return null;
	}
	
	static function set($what,$value) {
		if (self::$anketa == null)
		{
			throw new Exception('Survey ID is mandatory for SurveySession!');
			return null;
		}
		if ($what == null) 
		{
			throw new Exception('Variable \'what\' is mandatory for SurveySession!');
			return null;
		}
		if (!is_string($what)) 
		{
			throw new Exception('Variable \'what\' must be string!');
			return null;
		}
		
		# če je vse ok setiramo vrednost
		self::$data[$what] = $value;
		self::$updated = true;
		
		return true;
	}
	
	static public function remove($what)
	{
		if (self::$anketa == null)
		{
			throw new Exception('Survey ID is mandatory for SurveySession!');
			return null;
		}
		if ($what == null) 
		{
			throw new Exception('Variable \'what\' is mandatory for remove()!');
			return null;
		}
		if (!is_string($what)) 
		{
			throw new Exception('Variable \'what\' must be string!');
			return null;
		}
		
		if (isset(self::$data[$what])) {
			unset (self::$data[$what]);
			
			#pobrišemo še iz baze
			$deleteString = "DELETE FROM srv_survey_session WHERE ank_id = '".self::$anketa."' AND what='$what'";
			$query = sisplet_query($deleteString);
			self::$updated = true;
		}
		return null;
	}
	
	/** ob ukinitvi klassa shranimo vse vrednosti
	 * 
	 */
	static public function destruct()
	{
		if (self::$anketa == null)
		{
			throw new Exception('Survey ID is mandatory for SurveySession!');
			return null;
		}
		
		// v destructu več nimamo connect_db resoursa
		global $connect_db;
		if ($connect_db == null) {
			// poizkusimo še 1x
			global $mysql_server, $mysql_username, $mysql_password, $mysql_database_name;
			if (!$connect_db = mysqli_connect($mysql_server, $mysql_username, $mysql_password, $mysql_database_name)) {
				die ('Please try again later [ERR: DB])');
			}
		}		
		
		# pripravimo string za shranjevanje
		if (count(self::$data) > 0 && self::$updated) {
			$insertStringArray = array();
			foreach (self::$data AS $what => $value) {
				$insertStringArray[] = "('".self::$anketa."', '".$what."', '".serialize($value)."')";
			}
			$insertString = "INSERT INTO srv_survey_session (ank_id,what,value) VALUES ".implode(', ',$insertStringArray);
			$insertString .=" ON DUPLICATE KEY UPDATE value = VALUES(value)";
			$query = sisplet_query($insertString, $connect_db);
		}
	}
	
	static function append($where,$what,$value)
	{
		if (self::$anketa == null)
		{
			throw new Exception('Survey ID is mandatory for SurveySession!');
			return null;
		}
		if ($what == null)
		{
			throw new Exception('Variable \'what\' is mandatory for remove()!');
			return null;
		}
		if (!is_string($what))
		{
			throw new Exception('Variable \'what\' must be string!');
			return null;
		}
		self::$updated = true;
		self::$data[$where][$what] = $value; 
	}
}