-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlocallib.php
360 lines (306 loc) · 10.5 KB
/
locallib.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
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
/**
* AMOS local library.
*
* @package local_amos
* @copyright 2010 David Mudrak <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/local/amos/mlanglib.php');
define('AMOS_USER_MAINTAINER', 0);
define('AMOS_USER_CONTRIBUTOR', 1);
/**
* Renderable stash
*/
class local_amos_stash implements renderable {
/** @var int identifier in the table of stashes */
public $id;
/** @var string title of the stash */
public $name;
/** @var int timestamp of when the stash was created */
public $timecreated;
/** @var int timestamp of when the stash was modified */
public $timemodified;
/** @var stdClass the owner of the stash */
public $owner;
/** @var array of language names */
public $languages = array();
/** @var array of component names */
public $components = array();
/** @var int number of stashed strings */
public $strings = 0;
/** @var bool is autosave stash */
public $isautosave;
/** @var array of stdClasses representing stash actions */
protected $actions = array();
/**
* Factory method using an instance if {@see mlang_stash} as a data source
*
* @param mlang_stash $stash
* @param stdClass $owner owner user data
* @return local_amos_stash new instance
*/
public static function instance_from_mlang_stash(mlang_stash $stash, stdClass $owner) {
if ($stash->ownerid != $owner->id) {
throw new coding_exception('Stash owner mismatch');
}
$new = new local_amos_stash();
$new->id = $stash->id;
$new->name = $stash->name;
$new->timecreated = $stash->timecreated;
$new->timemodified = $stash->timemodified;
$stage = new mlang_stage();
$stash->apply($stage);
list($new->strings, $new->languages, $new->components) = mlang_stage::analyze($stage);
$stage->clear();
unset($stage);
$new->components = explode('/', trim($new->components, '/'));
$new->languages = explode('/', trim($new->languages, '/'));
$new->owner = $owner;
if ($stash->hash === 'xxxxautosaveuser'.$new->owner->id) {
$new->isautosave = true;
} else {
$new->isautosave = false;
}
return $new;
}
/**
* Factory method using plain database record from amos_stashes table as a source
*
* @param stdClass $record stash record from amos_stashes table
* @param stdClass $owner owner user data
* @return local_amos_stash new instance
*/
public static function instance_from_record(stdClass $record, stdClass $owner) {
if ($record->ownerid != $owner->id) {
throw new coding_exception('Stash owner mismatch');
}
$new = new local_amos_stash();
$new->id = $record->id;
$new->name = $record->name;
$new->timecreated = $record->timecreated;
$new->timemodified = $record->timemodified;
$new->strings = $record->strings;
$new->components = explode('/', trim($record->components, '/'));
$new->languages = explode('/', trim($record->languages, '/'));
$new->owner = $owner;
if ($record->hash === 'xxxxautosaveuser'.$new->owner->id) {
$new->isautosave = true;
} else {
$new->isautosave = false;
}
return $new;
}
/**
* Constructor is not public, use one of factory methods above
*/
protected function __construct() {
}
/**
* Register a new action that can be done with the stash
*
* @param string $id action identifier
* @param moodle_url $url action handler
* @param string $label action name
*/
public function add_action($id, moodle_url $url, $label) {
$action = new stdClass();
$action->id = $id;
$action->url = $url;
$action->label = $label;
$this->actions[] = $action;
}
/**
* Get the list of actions attached to this stash
*
* @return array of stdClasses with $url and $label properties
*/
public function get_actions() {
return $this->actions;
}
}
/**
* Represents renderable contribution infor
*/
class local_amos_contribution implements renderable {
/** Newly submitted contribution. */
const STATE_NEW = 0;
/** Contribution under review. */
const STATE_REVIEW = 10;
/** Rejected contribution. */
const STATE_REJECTED = 20;
/** Accepted submission. */
const STATE_ACCEPTED = 30;
/** @var stdClass */
public $info;
/** @var stdClass */
public $author;
/** @var stdClss */
public $assignee;
/** @var string */
public $language;
/** @var string */
public $components;
/** @var int number of strings */
public $strings;
/** @var int number of strings after rebase */
public $stringsreb;
/**
* Constructor.
*
* @param stdClass $info Contribution data.
* @param stdClass $author Contributor user record, if known.
* @param stdClass $assignee Assignee user record, if known.
*/
public function __construct(stdClass $info, stdClass $author=null, stdClass $assignee=null) {
global $DB;
$this->info = $info;
if (empty($author)) {
$this->author = $DB->get_record('user', array('id' => $info->authorid));
} else {
$this->author = $author;
}
if (empty($assignee) and !empty($info->assignee)) {
$this->assignee = $DB->get_record('user', array('id' => $info->assignee));
} else {
$this->assignee = $assignee;
}
}
}
/**
* Returns the list of app components
*
* @return array (string)frankenstylename
*/
function local_amos_app_plugins() {
global $DB;
static $list = null;
if (is_null($list)) {
$components = $DB->get_fieldset_select('amos_app_strings', 'DISTINCT component', "");
$list = array_combine($components, $components);
$list['local_moodlemobileapp'] = 'local_moodlemobileapp';
}
return $list;
}
/**
* Returns the list of workplace components
*
* @return array (string)componentname
*/
function local_amos_workplace_plugins() {
$components = get_config('local_amos', 'workplacecomponents');
return explode(',', $components);
}
/**
* Returns the list of app components
*
* @return array (string)component/(string)stringid => (string)appid
*/
function local_amos_applist_strings() {
global $DB;
static $applist = null;
if (is_null($applist)) {
// Get the app strings.
$applist = array();
$rs = $DB->get_records('amos_app_strings');
foreach ($rs as $s) {
$applist[$s->component.'/'.$s->stringid] = $s->appid;
}
}
return $applist;
}
/**
* Returns the options used for {@see local_amos_importfile_form}
*
* @return array
*/
function local_amos_importfile_options() {
$options = array();
$options['versions'] = array();
$options['versioncurrent'] = null;
foreach (mlang_version::list_all() as $version) {
if ($version->translatable) {
$options['versions'][$version->code] = $version->label;
}
}
$options['versioncurrent'] = mlang_version::latest_version()->code;
$options['languages'] = array_merge(array('' => get_string('choosedots')), mlang_tools::list_languages(false));
$currentlanguage = current_language();
if ($currentlanguage === 'en') {
$currentlanguage = 'en_fix';
}
$options['languagecurrent'] = $currentlanguage;
return $options;
}
/**
* Returns the options used for {@see local_amos_execute_form}
*
* @return array
*/
function local_amos_execute_options() {
$options = array();
$options['versions'] = array();
$options['versioncurrent'] = null;
$latestversioncode = mlang_version::latest_version()->code;
foreach (mlang_version::list_all() as $version) {
if ($version->translatable) {
$options['versions'][$version->code] = $version->label;
if ($version->code == $latestversioncode) {
$options['versioncurrent'] = $version->code;
}
}
}
return $options;
}
/**
* Returns an array of the changes from $old text to the $new one
*
* This is just slightly customized version of Paul's Simple Diff Algorithm.
* Given two arrays of chunks (words), the function returns an array of the changes
* leading from $old to $new.
*
* @author Paul Butler
* @copyright (C) Paul Butler 2007 <http://www.paulbutler.org/>
* @license May be used and distributed under the zlib/libpng license
* @link https://github.com/paulgb/simplediff
* @param array $old array of words
* @param array $new array of words
* @return array
*/
function local_amos_simplediff(array $old, array $new) {
$maxlen = 0;
foreach ($old as $oindex => $ovalue) {
$nkeys = array_keys($new, $ovalue);
foreach ($nkeys as $nindex) {
$matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
$matrix[$oindex - 1][$nindex - 1] + 1 : 1;
if ($matrix[$oindex][$nindex] > $maxlen) {
$maxlen = $matrix[$oindex][$nindex];
$omax = $oindex + 1 - $maxlen;
$nmax = $nindex + 1 - $maxlen;
}
}
}
if ($maxlen == 0) {
return array(array('d' => $old, 'i' => $new));
}
return array_merge(
local_amos_simplediff(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
array_slice($new, $nmax, $maxlen),
local_amos_simplediff(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen)));
}