This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
externallib.php
205 lines (177 loc) · 6.72 KB
/
externallib.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
<?php
// This file is part of Moodle - http://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 <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') || die();
require_once("$CFG->libdir/externallib.php");
/**
* Local Course Translator Web Service
*
* Adds a webservice available via ajax for the Translate Content page.
*
* @package local_coursetranslator
* @copyright 2022 Kaleb Heitzman <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @see https://docs.moodle.org/dev/External_functions_API
*/
class local_coursetranslator_external extends external_api {
/**
* Get field parameters
*
* Adds validation parameters for getting db fields
*
* @return external_function_parameters
*/
public static function get_field_parameters() {
return new external_function_parameters(
array(
'data' => new external_multiple_structure(
new external_single_structure(
array(
'courseid' => new external_value(PARAM_INT, 'course id'),
'id' => new external_value(PARAM_INT, 'id of table record'),
'table' => new external_value(PARAM_RAW, 'table to update text'),
'field' => new external_value(PARAM_RAW, 'table field to update'),
)
)
)
)
);
}
/**
* Get DB Field
*
* Dynamically get db field to allow simultaenous editing
*
* @param object $data
* @return array
*/
public static function get_field($data) {
global $CFG, $DB;
$params = self::validate_parameters(self::get_field_parameters(), array('data' => $data));
$transaction = $DB->start_delegated_transaction();
$response = array();
foreach ($params['data'] as $data) {
// Check for null values and throw errors.
// Security checks.
$context = context_course::instance($data['courseid']);
self::validate_context($context);
require_capability('local/coursetranslator:edittranslations', $context);
// Get the original record.
$record = (array) $DB->get_record($data['table'], array('id' => $data['id']));
$text = $record[$data['field']];
$response[] = array(
'text' => $text,
);
}
// Commit the transaction.
$transaction->allow_commit();
return $response;
}
/**
* Return Field
*
* Returns field data to the user from web service.
*
* @return external_multiple_structure
*/
public static function get_field_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
'text' => new external_value(PARAM_RAW, 'updated text of field')
)
)
);
}
/**
* Update Translation Parameters
*
* Adds validation parameters for translations
*
* @return external_function_parameters
*/
public static function update_translation_parameters() {
return new external_function_parameters(
array(
'data' => new external_multiple_structure(
new external_single_structure(
array(
'courseid' => new external_value(PARAM_INT, 'course id'),
'id' => new external_value(PARAM_INT, 'id of table record'),
'tid' => new external_value(PARAM_INT, 'tid of local_coursetranslator record'),
'table' => new external_value(PARAM_RAW, 'table to update text'),
'field' => new external_value(PARAM_RAW, 'table field to update'),
'text' => new external_value(PARAM_RAW, 'text to be upserted'),
)
)
)
)
);
}
/**
* Update Translation
*
* Dynamically update table and column name for item submitted
*
* @param object $data
* @return array
*/
public static function update_translation($data) {
global $CFG, $DB;
$params = self::validate_parameters(self::update_translation_parameters(), array('data' => $data));
$transaction = $DB->start_delegated_transaction();
$response = array();
foreach ($params['data'] as $data) {
purge_all_caches();
// Check for null values and throw errors.
// Security checks.
$context = context_course::instance($data['courseid']);
self::validate_context($context);
require_capability('local/coursetranslator:edittranslations', $context);
// Update the record.
$dataobject = array();
$dataobject['id'] = $data['id'];
$dataobject[$data['field']] = $data['text'];
$DB->update_record($data['table'], (object) $dataobject);
// Update t_lastmodified.
$timemodified = time();
$DB->update_record('local_coursetranslator', array('id' => $data['tid'], 't_lastmodified' => $timemodified));
$response[] = array(
't_lastmodified' => $timemodified,
'text' => $data['text']
);
}
// Commit the transaction.
$transaction->allow_commit();
return $response;
}
/**
* Return Translation
*
* Returns updated translation to the user from web service.
*
* @return external_multiple_structure
*/
public static function update_translation_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
't_lastmodified' => new external_value(PARAM_INT, 'translation last modified time'),
'text' => new external_value(PARAM_RAW, 'text of field')
)
)
);
}
}