-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexicon_lib.php
225 lines (189 loc) · 5.73 KB
/
lexicon_lib.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
<?php
/**
* *************************************************************************
* * OOHOO - TTS - Text To Speech **
* *************************************************************************
* @package block **
* @subpackage TTS **
* @name TTS **
* @copyright oohoo.biz **
* @link http://oohoo.biz **
* @author Ryan Thomas (Original Author) **
* @author Dustin Durand **
* @author Nicolas Bretin **
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later **
* *************************************************************************
* ************************************************************************ */
require_once('../../config.php');
$operation = optional_param('oper', 'get', PARAM_TEXT);
$courseid = optional_param('courseid', 0, PARAM_INT);
if ($courseid == 0)
{
print 0;
exit();
}
require_login($courseid);
//Replace get_context_instance by the class for moodle 2.6+
if(class_exists('context_module'))
{
$context = context_course::instance($courseid);
}
else
{
$context = get_context_instance(CONTEXT_COURSE, $courseid);
}
if (!has_capability('block/tts:lexicon', $context))
{
print 0;
exit();
}
switch ($operation)
{
case 'get':
get_table_records($courseid);
break;
case 'edit':
edit_record();
break;
case 'del':
del_record();
break;
case 'add':
add_record($courseid);
break;
default:break;
}
/**
* Add a record to the lexicon table
* @global moodle_database $DB
* @param int $courseid The course ID to the lexicon
* @return null Return nothing. The result is printed
*/
function add_record($courseid)
{
global $DB;
$expression = optional_param('expression', '', PARAM_TEXT);
$prenounce = optional_param('prenounce', '', PARAM_TEXT);
if ($expression == '' || $prenounce == '')
{
print 0;
return;
}
//Don't want same expression with multiple meanings
$sql = 'SELECT count(id) FROM {block_tts_lexicon} t WHERE t.expression = \'' . $expression . '\'';
if ($DB->count_records_sql($sql))
{
print 0;
return;
}
$record = new stdClass();
$record->expression = $expression;
$record->prenounce = $prenounce;
$record->courseid = $courseid;
$record->lastmodified = time();
if ($id = $DB->insert_record('block_tts_lexicon', $record, true))
{
print $id;
}
else
{
print 0;
}
}
/**
* Delete a record from the lexicon table
* @global moodle_database $DB
* @return null Return nothing. The result is printed
*/
function del_record()
{
global $DB;
$id = optional_param('id', 0, PARAM_INT);
if ($id == 0)
{
print 0;
return;
}
if ($DB->delete_records('block_tts_lexicon', array('id' => $id)))
{
print 1;
}
else
{
print 0;
}
}
/**
* Edit a record in the lexicon table
* @global moodle_database $DB
* @return null Return nothing. The result is printed
*/
function edit_record()
{
global $DB;
$id = optional_param('id', 0, PARAM_INT);
$expression = optional_param('expression', '', PARAM_TEXT);
$prenounce = optional_param('prenounce', '', PARAM_TEXT);
if ($id == 0)
{
print 0;
return;
}
$record = new stdClass();
$record->id = $id;
$record->expression = $expression;
$record->prenounce = $prenounce;
$record->lastmodified = time();
if ($DB->update_record('block_tts_lexicon', $record))
{
print 1;
}
else
{
print 0;
}
}
/**
* Return le list of lexicon records for a course ID. The return is directly printed in json
* @global moodle_database $DB
* @param int $courseid The course ID
*/
function get_table_records($courseid)
{
global $DB;
$page = optional_param('page', 1, PARAM_INT);
$rows = optional_param('rows', 50, PARAM_INT);
$sidx = optional_param('sidx', 'expression', PARAM_TEXT);
$sord = optional_param('sord', 'DESC', PARAM_TEXT);
$count = (int) $DB->count_records('block_tts_lexicon', array('courseid' => $courseid)); //mysql_query("SELECT COUNT(*) AS count FROM invheader");
// calculate the total pages for the query
if ($count > 0 && $rows > 0)
{
$total_pages = ceil($count / $rows);
}
else
{
$total_pages = 0;
}
// if for some reasons the requested page is greater than the total
// set the requested page to total page
if ($page > $total_pages)
$page = $total_pages;
// calculate the starting position of the rows
$start = $rows * $page - $rows;
// if for some reasons start position is negative set it to 0
// typical case is that the user type 0 for the requested page
if ($start < 0)
$start = 0;
// the actual query for the grid data
//$SQL = "SELECT invid, invdate, amount, tax,total, note FROM invheader ORDER BY $sidx $sord LIMIT $start , $limit";
$results = $DB->get_records('block_tts_lexicon', array(), "$sidx $sord", '*');
$table_rows = array();
foreach ($results as $row)
{
$table_rows[] = array('id' => $row->id, 'expression' => $row->expression, 'prenounce' => $row->prenounce);
}
$table = array('page' => $page, 'total' => $total_pages, 'records' => $count, 'rows' => $table_rows);
print json_encode($table);
}
?>