forked from lupaelma/jenti
/
JentiWord.php
386 lines (343 loc) · 11.4 KB
/
JentiWord.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
// Copyright 2015 - NINETY-DEGREES
require_once "dbedit/DBEManagerSql.1.1.php";
class JentiWord
extends ManagerSql
{
// aggregate multiple errors for methods
// that execute many sql queries
public $error_array;
// config
private $config;
private $enum;
// database tables
private $table_word = "WORD";
private $table_word_definition = "WORD_DEFINITION";
private $table_word_tag = "WORD_TAG";
private $table_word_list = "WORD_LIST";
private $play_table_prefix = "PLAY";
function __construct($config=null)
{
parent::__construct($config);
$this->config = $config;
}
/**
* Return the next word for a playing user.
*
* @param array $user_info the user data necessary to generate the next word
* @return array $word_info the word user must guess
*/
public function get_next_word($user_info)
{
// identify the table that is used for this user game
$table_name = $this->play_table_prefix
. "_" . strtoupper($user_info["LANGUAGE_CODE"])
. (isset($user_info["TAG"])
? "_" . strtoupper($user_info["TAG"])
: "")
;
// the table contains as many rows as many words used to play
$word_count = $this->get_table_row_count($table_name);
if (!$this->error && $word_count > 0)
{
// next word chosen randomly among the the words in the table
$word_index = rand(1, $word_count);
$word_id = $this->get_next_word_id($table_name, $word_index);
if (!$this->error)
{
return $this->get_word($word_id);
}
}
return null;
}
/**
* Add word and definition to the database.
* Use $error_array to check for errors.
*
* @param array $word_info the word data
*/
public function add_word($word_info)
{
$this->error_array = array();
// insert word
$word_id = $this->add_word_word($word_info);
if ($this->errno == 1062)
{
// word already in the database, get word id
$word_id = $this->get_word_id($word_info);
}
if ($word_id > 0)
{
// insert word definition
$word_info["WORD_ID"] = $word_id;
$this->add_word_definitions($word_info);
// insert new words
$this->add_word_list_words($word_info);
}
}
/**
* Get a word id.
*
* @param array $word_info the word data
*/
public function get_word_id($word_info)
{
$sql = "SELECT ID FROM {$this->table_word} "
. "WHERE WORD = '".$word_info["WORD"]."' "
. "AND TYPE = '".$word_info["TYPE"]."' "
. "AND LANGUAGE_CODE = '".$word_info["LANGUAGE_CODE"]."'"
;
$query_result = $this->query($sql);
if ($query_result)
{
$result_assoc = $this->get_row_assoc($query_result);
return $result_assoc["ID"];
}
return 0;
}
/**
* Get a word id from one of the game tables.
*
* @param string $table_name the name of the game table
* @param string $word_index the word index which is the id in the game table
*/
public function get_next_word_id($table_name, $word_index)
{
$sql = "SELECT WORD_ID FROM {$table_name} "
. "WHERE ID = {$word_index} "
;
$query_result = $this->query($sql);
if ($query_result)
{
$result_assoc = $this->get_row_assoc($query_result);
return $result_assoc["WORD_ID"];
}
return 0;
}
/**
* Get a word data.
*
* @param string $word_id the id of the needed word
*/
public function get_word($word_id)
{
$sql = "SELECT * FROM {$this->table_word} WO, {$this->table_word_definition} WD "
. "WHERE WO.ID = {$word_id} "
. "AND WD.WORD_ID = WO.ID "
. "ORDER BY WD.ID"
;
$query_result = $this->query($sql);
if ($query_result)
{
$result_assoc = $this->get_row_assoc($query_result);
// build more definitions array
$more_definitions = array();
for ($i=1; $i<$this->get_num_rows($query_result); $i++)
{
$row = $this->get_row_assoc($query_result);
$more_definitions[] = array(
"DEFINITION" => $row["DEFINITION"],
"TAGS" => $row["TAGS"],
"SOURCE_NAME" => $row["SOURCE_NAME"]);
}
$result_assoc["MORE_DEFINITIONS"] = $more_definitions;
return $result_assoc;
}
return null;
}
/**
* Get a list of words to be acquired from source.
*
* @param object $request source specific request object
*/
public function get_words_without_definition($request)
{
$sql = "SELECT WORD FROM {$this->table_word_list} WL "
. "WHERE WL.LANGUAGE_CODE = '{$request->language_code}' "
. "AND (WL.AVOID_SOURCES IS NULL OR WL.AVOID_SOURCES NOT LIKE '%({$request->service_name})%') "
. "AND NOT EXISTS "
. "(SELECT WORD FROM WORD WO, WORD_DEFINITION WD "
. " WHERE WD.SOURCE_NAME = '{$request->service_name}' "
. " AND WD.WORD_ID = WO.ID "
. " AND WO.WORD = WL.WORD)"
;
$word_array = array();
$query_result = $this->query($sql);
if ($query_result)
{
for ($i=0; $i<$this->get_num_rows($query_result); $i++)
{
$word_array = $this->get_row_array($query_result);
}
}
return $word_array;
}
/**
* Update word list to indicate sources to avoid for a word.
*
* @param array $word_info the word information
*/
public function update_word_list_word($word_info)
{
$sql = "UPDATE {$this->table_word_list} "
. "SET AVOID_SOURCES = CONCAT_WS('', AVOID_SOURCES, '(".$word_info["SOURCE_NAME"].")') "
. "WHERE WORD = '{$word_info["WORD"]}' "
. "AND LANGUAGE_CODE = '{$word_info["LANGUAGE_CODE"]}' "
. "AND (AVOID_SOURCES IS NULL OR AVOID_SOURCES NOT LIKE '%(".$word_info["SOURCE_NAME"].")%') "
;
return $this->query($sql);
}
/**
* Update likes -> increment word likes and word definition likes by 1
*
* @param array $word_info the word information
*/
public function update_likes($word_info)
{
$this->update_word_likes($word_info);
$this->update_word_definition_likes($word_info);
}
/**
* Add word tags to the database.
*
* @param array $definition_info the word definition data
*/
private function add_word_definition_tags($definition_info, $language_code)
{
$tags_array = $definition_info["TAGS_ARRAY"];
foreach ($tags_array as $tag)
{
$tag_data["TAG"] = $tag;
$tag_data["LANGUAGE_CODE"] = $language_code;
$this->query_insert($this->table_word_tag, $tag_data);
if ($this->error)
{
$this->error_array[] = $this->error;
}
}
}
/**
* Update word definition likes -> increment definition likes by 1
*
* @param array $word_info the word information
*/
private function update_word_definition_likes($word_info)
{
$sql = "UPDATE {$this->table_word_definition} "
. "SET LIKES = LIKES + 1 "
. "WHERE WORD_ID = '{$word_info["WORD_ID"]}' "
. "AND ID = '{$word_info["DEFINITION_ID"]}' "
;
return $this->query($sql);
}
/**
* Update word likes -> increment word likes by 1
*
* @param array $word_info the word information
*/
private function update_word_likes($word_info)
{
$sql = "UPDATE {$this->table_word} "
. "SET LIKES = LIKES + 1 "
. "WHERE ID = '{$word_info["WORD_ID"]}' "
;
return $this->query($sql);
}
/**
* Add new words to word list.
*
* @param array $word_info the word data
*/
private function add_word_list_words($word_info)
{
if (isset($word_info["MORE_WORDS"]))
{
foreach ($word_info["MORE_WORDS"] as $word)
{
$word_list_info["WORD"] = $word;
$word_list_info["LANGUAGE_CODE"] = $word_info["LANGUAGE_CODE"];
$this->query_insert($this->table_word_list, $word_list_info);
if ($this->error)
{
$this->error_array[] = $this->error;
}
}
}
}
/**
* Add word to the database.
*
* @param array $word_info the word data
* @return int the id of the inserted or existing word
*/
private function add_word_word($word_info)
{
$word_id = 0;
if ($this->validate_word_data($word_info))
{
// insert word
$query_result = $this->query_insert($this->table_word, $word_info);
if ($query_result)
{
// need id of word just inserted
$word_id = $this->get_insert_id();
}
else
{
$this->error_array[] = $this->error;
}
}
return $word_id;
}
/**
* Add word definitions to the database.
*
* @param array $word_info the word data
*/
private function add_word_definitions($word_info)
{
if (isset($word_info["WORD_ID"]))
{
$definition_array = $word_info["DEFINITION_ARRAY"];
foreach ($definition_array as $definition_info)
{
$definition_info["WORD_ID"] = $word_info["WORD_ID"];
$this->query_insert($this->table_word_definition, $definition_info);
if ($this->error)
{
$this->error_array[] = $this->error;
}
// tags
$this->add_word_definition_tags($definition_info, $word_info["LANGUAGE_CODE"]);
}
}
else
{
$this->error_array[] = get_class($this)
. " ERROR: WORD_ID is required to add word definitions.";
}
}
/**
* Validate word data.
*
* @param array $word_info the word data
* @return boolean
*/
private function validate_word_data($word_info)
{
// check that values exist in enumerations
// TODO we do not need to check enums for now...
//if (!$this->enum)
//{
// $this->enum = new JentiEnum(array_merge($this->config, $word_info));
//}
if (array_search($word_info["LANGUAGE_CODE"], $this->config["supported_language_codes"]) === FALSE)
{
$this->error_array[] = get_class($this)
. " ERROR : Invalid word data"
. " LANGUAGE_CODE={$word_info["LANGUAGE_CODE"]}";
return false;
}
return true;
}
}