Commit | Line | Data |
---|---|---|
d4c6ef70 | 1 | <?php |
86dcc6f0 | 2 | |
d4c6ef70 | 3 | /////////////////////////////////////////////////////////////////////////// |
4 | // // | |
5 | // This file is part of Moodle - http://moodle.org/ // | |
6 | // Moodle - Modular Object-Oriented Dynamic Learning Environment // | |
7 | // // | |
8 | // Moodle is free software: you can redistribute it and/or modify // | |
9 | // it under the terms of the GNU General Public License as published by // | |
10 | // the Free Software Foundation, either version 3 of the License, or // | |
11 | // (at your option) any later version. // | |
12 | // // | |
13 | // Moodle is distributed in the hope that it will be useful, // | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // | |
16 | // GNU General Public License for more details. // | |
17 | // // | |
18 | // You should have received a copy of the GNU General Public License // | |
19 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. // | |
20 | // // | |
21 | /////////////////////////////////////////////////////////////////////////// | |
22 | ||
23 | /** | |
24 | * Web service documentation renderer. | |
25 | * @package webservice | |
26 | * @copyright 2009 Moodle Pty Ltd (http://moodle.com) | |
27 | * @author Jerome Mouneyrac | |
28 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
29 | */ | |
c927e35c | 30 | class core_webservice_renderer extends plugin_renderer_base { |
229a7099 | 31 | |
86dcc6f0 | 32 | /** |
33 | * Display the authorised user selector | |
34 | * @param object $options | |
35 | * @return string html | |
36 | */ | |
37 | public function admin_authorised_user_selector(&$options) { | |
de55d469 | 38 | global $CFG; |
86dcc6f0 | 39 | $formcontent = html_writer::empty_tag('input', |
72f68b51 | 40 | array('name' => 'sesskey', 'value' => sesskey(), 'type' => 'hidden')); |
86dcc6f0 | 41 | |
42 | $table = new html_table(); | |
43 | $table->size = array('45%', '10%', '45%'); | |
44 | $table->attributes['class'] = 'roleassigntable generaltable generalbox boxaligncenter'; | |
45 | $table->summary = ''; | |
46 | $table->cellspacing = 0; | |
47 | $table->cellpadding = 0; | |
48 | ||
49 | //create the add and remove button | |
50 | $addinput = html_writer::empty_tag('input', | |
51 | array('name' => 'add', 'id' => 'add', 'type' => 'submit', | |
52 | 'value' => '◀' . ' ' . get_string('add'), | |
53 | 'title' => get_string('add'))); | |
54 | $addbutton = html_writer::tag('div', $addinput, array('id' => 'addcontrols')); | |
55 | $removeinput = html_writer::empty_tag('input', | |
56 | array('name' => 'remove', 'id' => 'remove', 'type' => 'submit', | |
57 | 'value' => '▶' . ' ' . get_string('remove'), | |
58 | 'title' => get_string('remove'))); | |
59 | $removebutton = html_writer::tag('div', $removeinput, array('id' => 'removecontrols')); | |
60 | ||
61 | ||
62 | //create the three cells | |
63 | $label = html_writer::tag('label', get_string('serviceusers', 'webservice'), | |
72f68b51 | 64 | array('for' => 'removeselect')); |
86dcc6f0 | 65 | $label = html_writer::tag('p', $label); |
72f68b51 | 66 | $authoriseduserscell = new html_table_cell($label . |
67 | $options->alloweduserselector->display(true)); | |
86dcc6f0 | 68 | $authoriseduserscell->id = 'existingcell'; |
69 | $buttonscell = new html_table_cell($addbutton . html_writer::empty_tag('br') . $removebutton); | |
70 | $buttonscell->id = 'buttonscell'; | |
71 | $label = html_writer::tag('label', get_string('potusers', 'webservice'), | |
72f68b51 | 72 | array('for' => 'addselect')); |
86dcc6f0 | 73 | $label = html_writer::tag('p', $label); |
72f68b51 | 74 | $otheruserscell = new html_table_cell($label . |
75 | $options->potentialuserselector->display(true)); | |
86dcc6f0 | 76 | $otheruserscell->id = 'potentialcell'; |
77 | ||
78 | $cells = array($authoriseduserscell, $buttonscell, $otheruserscell); | |
79 | $row = new html_table_row($cells); | |
80 | $table->data[] = $row; | |
81 | $formcontent .= html_writer::table($table); | |
82 | ||
83 | $formcontent = html_writer::tag('div', $formcontent); | |
84 | ||
de55d469 | 85 | $actionurl = new moodle_url('/' . $CFG->admin . '/webservice/service_users.php', |
86dcc6f0 | 86 | array('id' => $options->serviceid)); |
87 | $html = html_writer::tag('form', $formcontent, | |
72f68b51 | 88 | array('id' => 'assignform', 'action' => $actionurl, 'method' => 'post')); |
86dcc6f0 | 89 | return $html; |
90 | } | |
91 | ||
92 | /** | |
93 | * Display list of authorised user | |
94 | * @param array $users | |
95 | * @return string $html | |
96 | */ | |
97 | public function admin_authorised_user_list($users, $serviceid) { | |
de55d469 | 98 | global $CFG; |
72f68b51 | 99 | $html = $this->output->box_start('generalbox', 'alloweduserlist'); |
86dcc6f0 | 100 | foreach ($users as $user) { |
de55d469 | 101 | $modifiedauthoriseduserurl = new moodle_url('/' . $CFG->admin . '/webservice/service_user_settings.php', |
86dcc6f0 | 102 | array('userid' => $user->id, 'serviceid' => $serviceid)); |
72f68b51 | 103 | $html .= html_writer::tag('a', $user->firstname . " " |
104 | . $user->lastname . ", " . $user->email, | |
86dcc6f0 | 105 | array('href' => $modifiedauthoriseduserurl)); |
72f68b51 | 106 | //add missing capabilities |
107 | if (!empty($user->missingcapabilities)) { | |
108 | $html .= html_writer::tag('div', | |
109 | get_string('usermissingcaps', 'webservice', $user->missingcapabilities) | |
110 | . ' ' . $this->output->help_icon('missingcaps', 'webservice'), | |
111 | array('class' => 'missingcaps', 'id' => 'usermissingcaps')); | |
112 | $html .= html_writer::empty_tag('br'); | |
113 | } else { | |
114 | $html .= html_writer::empty_tag('br') . html_writer::empty_tag('br'); | |
115 | } | |
116 | } | |
117 | $html .= $this->output->box_end(); | |
118 | return $html; | |
119 | } | |
120 | ||
121 | /** | |
122 | * Display a confirmation page to remove a function from a service | |
123 | * @param object $function | |
124 | * @param object $service | |
125 | * @return string html | |
126 | */ | |
127 | public function admin_remove_service_function_confirmation($function, $service) { | |
128 | $optionsyes = array('id' => $service->id, 'action' => 'delete', | |
129 | 'confirm' => 1, 'sesskey' => sesskey(), 'fid' => $function->id); | |
130 | $optionsno = array('id' => $service->id); | |
131 | $formcontinue = new single_button(new moodle_url('service_functions.php', | |
132 | $optionsyes), get_string('remove')); | |
133 | $formcancel = new single_button(new moodle_url('service_functions.php', | |
134 | $optionsno), get_string('cancel'), 'get'); | |
135 | return $this->output->confirm(get_string('removefunctionconfirm', 'webservice', | |
136 | (object) array('service' => $service->name, 'function' => $function->name)), | |
137 | $formcontinue, $formcancel); | |
138 | } | |
139 | ||
c25662b0 | 140 | /** |
141 | * Display a confirmation page to remove a service | |
142 | * @param object $service | |
143 | * @return string html | |
144 | */ | |
145 | public function admin_remove_service_confirmation($service) { | |
146 | global $CFG; | |
147 | $optionsyes = array('id' => $service->id, 'action' => 'delete', | |
148 | 'confirm' => 1, 'sesskey' => sesskey()); | |
149 | $optionsno = array('section' => 'externalservices'); | |
150 | $formcontinue = new single_button(new moodle_url('service.php', $optionsyes), | |
151 | get_string('delete'), 'post'); | |
152 | $formcancel = new single_button( | |
153 | new moodle_url($CFG->wwwroot . "/" . $CFG->admin . "/settings.php", $optionsno), | |
154 | get_string('cancel'), 'get'); | |
155 | return $this->output->confirm(get_string('deleteserviceconfirm', 'webservice', $service->name), | |
156 | $formcontinue, $formcancel); | |
157 | } | |
158 | ||
9c954e88 | 159 | /** |
160 | * Display a confirmation page to delete a token | |
161 | * @param object $token | |
162 | * @return string html | |
163 | */ | |
164 | public function admin_delete_token_confirmation($token) { | |
de55d469 | 165 | global $CFG; |
9c954e88 | 166 | $optionsyes = array('tokenid' => $token->id, 'action' => 'delete', |
167 | 'confirm' => 1, 'sesskey' => sesskey()); | |
168 | $optionsno = array('section' => 'webservicetokens', 'sesskey' => sesskey()); | |
169 | $formcontinue = new single_button( | |
de55d469 | 170 | new moodle_url('/' . $CFG->admin . '/webservice/tokens.php', $optionsyes), |
9c954e88 | 171 | get_string('delete')); |
172 | $formcancel = new single_button( | |
de55d469 | 173 | new moodle_url('/' . $CFG->admin . '/settings.php', $optionsno), |
9c954e88 | 174 | get_string('cancel'), 'get'); |
175 | return $this->output->confirm(get_string('deletetokenconfirm', 'webservice', | |
176 | (object) array('user' => $token->firstname . " " | |
9ef728d6 | 177 | . $token->lastname, 'service' => $token->name)), |
9c954e88 | 178 | $formcontinue, $formcancel); |
179 | } | |
180 | ||
72f68b51 | 181 | /** |
182 | * Display list of function for a given service | |
183 | * If the service is build-in do not display remove/add operation (read-only) | |
184 | * @param array $functions | |
185 | * @param object $service | |
186 | * @return string the table html + add operation html | |
187 | */ | |
188 | public function admin_service_function_list($functions, $service) { | |
de55d469 | 189 | global $CFG; |
1d2a1936 | 190 | if (!empty($functions)) { |
191 | $table = new html_table(); | |
192 | $table->head = array(get_string('function', 'webservice'), | |
193 | get_string('description'), get_string('requiredcaps', 'webservice')); | |
194 | $table->align = array('left', 'left', 'left'); | |
195 | $table->size = array('15%', '40%', '40%'); | |
196 | $table->width = '100%'; | |
197 | $table->align[] = 'left'; | |
72f68b51 | 198 | |
72f68b51 | 199 | //display remove function operation (except for build-in service) |
200 | if (empty($service->component)) { | |
1d2a1936 | 201 | $table->head[] = get_string('edit'); |
202 | $table->align[] = 'center'; | |
203 | } | |
204 | ||
205 | foreach ($functions as $function) { | |
206 | $function = external_function_info($function); | |
207 | $requiredcaps = html_writer::tag('div', | |
208 | empty($function->capabilities) ? '' : $function->capabilities, | |
209 | array('class' => 'functiondesc')); | |
210 | ; | |
211 | $description = html_writer::tag('div', $function->description, | |
212 | array('class' => 'functiondesc')); | |
213 | //display remove function operation (except for build-in service) | |
214 | if (empty($service->component)) { | |
de55d469 | 215 | $removeurl = new moodle_url('/' . $CFG->admin . '/webservice/service_functions.php', |
1d2a1936 | 216 | array('sesskey' => sesskey(), 'fid' => $function->id, |
217 | 'id' => $service->id, | |
218 | 'action' => 'delete')); | |
219 | $removelink = html_writer::tag('a', | |
220 | get_string('removefunction', 'webservice'), | |
221 | array('href' => $removeurl)); | |
222 | $table->data[] = array($function->name, $description, $requiredcaps, $removelink); | |
223 | } else { | |
224 | $table->data[] = array($function->name, $description, $requiredcaps); | |
225 | } | |
72f68b51 | 226 | } |
72f68b51 | 227 | |
1d2a1936 | 228 | $html = html_writer::table($table); |
229 | } else { | |
230 | $html = get_string('nofunctions', 'webservice') . html_writer::empty_tag('br'); | |
231 | } | |
72f68b51 | 232 | |
233 | //display add function operation (except for build-in service) | |
234 | if (empty($service->component)) { | |
de55d469 | 235 | $addurl = new moodle_url('/' . $CFG->admin . '/webservice/service_functions.php', |
72f68b51 | 236 | array('sesskey' => sesskey(), 'id' => $service->id, 'action' => 'add')); |
1d2a1936 | 237 | $html .= html_writer::tag('a', get_string('addfunctions', 'webservice'), array('href' => $addurl)); |
72f68b51 | 238 | } |
239 | ||
86dcc6f0 | 240 | return $html; |
241 | } | |
242 | ||
229a7099 | 243 | /** |
72f68b51 | 244 | * Display Reset token confirmation box |
229a7099 | 245 | * @param object $token to reset |
246 | * @return string html | |
247 | */ | |
248 | public function user_reset_token_confirmation($token) { | |
72f68b51 | 249 | global $CFG; |
86dcc6f0 | 250 | $managetokenurl = $CFG->wwwroot . "/user/managetoken.php?sesskey=" . sesskey(); |
72f68b51 | 251 | $optionsyes = array('tokenid' => $token->id, 'action' => 'resetwstoken', 'confirm' => 1, |
252 | 'sesskey' => sesskey()); | |
86dcc6f0 | 253 | $optionsno = array('section' => 'webservicetokens', 'sesskey' => sesskey()); |
72f68b51 | 254 | $formcontinue = new single_button(new moodle_url($managetokenurl, $optionsyes), |
255 | get_string('reset')); | |
256 | $formcancel = new single_button(new moodle_url($managetokenurl, $optionsno), | |
257 | get_string('cancel'), 'get'); | |
258 | $html = $this->output->confirm(get_string('resettokenconfirm', 'webservice', | |
259 | (object) array('user' => $token->firstname . " " . | |
9ef728d6 | 260 | $token->lastname, 'service' => $token->name)), |
86dcc6f0 | 261 | $formcontinue, $formcancel); |
229a7099 | 262 | return $html; |
263 | } | |
264 | ||
229a7099 | 265 | /** |
266 | * Display user tokens with buttons to reset them | |
267 | * @param object $tokens | |
268 | * @param int $userid | |
269 | * @return string html code | |
270 | */ | |
9ef728d6 | 271 | public function user_webservice_tokens_box($tokens, $userid, $documentation = false) { |
252708c6 | 272 | global $CFG, $DB; |
229a7099 | 273 | |
274 | // display strings | |
275 | $stroperation = get_string('operation', 'webservice'); | |
276 | $strtoken = get_string('key', 'webservice'); | |
277 | $strservice = get_string('service', 'webservice'); | |
278 | $strcreator = get_string('tokencreator', 'webservice'); | |
279 | $strcontext = get_string('context', 'webservice'); | |
280 | $strvaliduntil = get_string('validuntil', 'webservice'); | |
281 | ||
72f68b51 | 282 | $return = $this->output->heading(get_string('securitykeys', 'webservice'), 3, 'main', true); |
283 | $return .= $this->output->box_start('generalbox webservicestokenui'); | |
229a7099 | 284 | |
285 | $return .= get_string('keyshelp', 'webservice'); | |
286 | ||
287 | $table = new html_table(); | |
86dcc6f0 | 288 | $table->head = array($strtoken, $strservice, $strvaliduntil, $strcreator, $stroperation); |
229a7099 | 289 | $table->align = array('left', 'left', 'left', 'center', 'left', 'center'); |
290 | $table->width = '100%'; | |
86dcc6f0 | 291 | $table->data = array(); |
229a7099 | 292 | |
9ef728d6 | 293 | if ($documentation) { |
294 | $table->head[] = get_string('doc', 'webservice'); | |
295 | $table->align[] = 'center'; | |
296 | } | |
297 | ||
229a7099 | 298 | if (!empty($tokens)) { |
299 | foreach ($tokens as $token) { | |
229a7099 | 300 | |
301 | if ($token->creatorid == $userid) { | |
72f68b51 | 302 | $reset = "<a href=\"" . $CFG->wwwroot . "/user/managetoken.php?sesskey=" |
303 | . sesskey() . "&action=resetwstoken&tokenid=" . $token->id . "\">"; | |
86dcc6f0 | 304 | $reset .= get_string('reset') . "</a>"; |
305 | $creator = $token->firstname . " " . $token->lastname; | |
229a7099 | 306 | } else { |
9ef728d6 | 307 | //retrieve administrator name |
252708c6 | 308 | $admincreator = $DB->get_record('user', array('id'=>$token->creatorid)); |
86dcc6f0 | 309 | $creator = $admincreator->firstname . " " . $admincreator->lastname; |
229a7099 | 310 | $reset = ''; |
311 | } | |
312 | ||
86dcc6f0 | 313 | $userprofilurl = new moodle_url('/user/view.php?id=' . $token->creatorid); |
229a7099 | 314 | $creatoratag = html_writer::start_tag('a', array('href' => $userprofilurl)); |
315 | $creatoratag .= $creator; | |
316 | $creatoratag .= html_writer::end_tag('a'); | |
317 | ||
318 | $validuntil = ''; | |
319 | if (!empty($token->validuntil)) { | |
320 | $validuntil = date("F j, Y"); //TODO: language support (look for moodle function) | |
321 | } | |
322 | ||
9ef728d6 | 323 | $row = array($token->token, $token->name, $validuntil, $creatoratag, $reset); |
324 | ||
325 | if ($documentation) { | |
252708c6 | 326 | $doclink = new moodle_url('/webservice/wsdoc.php', |
9ef728d6 | 327 | array('id' => $token->id, 'sesskey' => sesskey())); |
328 | $row[] = html_writer::tag('a', get_string('doc', 'webservice'), | |
329 | array('href' => $doclink)); | |
330 | } | |
331 | ||
252708c6 | 332 | $table->data[] = $row; |
229a7099 | 333 | } |
334 | $return .= html_writer::table($table); | |
229a7099 | 335 | } else { |
336 | $return .= get_string('notoken', 'webservice'); | |
337 | } | |
338 | ||
72f68b51 | 339 | $return .= $this->output->box_end(); |
229a7099 | 340 | return $return; |
341 | } | |
342 | ||
86dcc6f0 | 343 | /** |
334f9dbb | 344 | * Return documentation for a ws description object |
72f68b51 | 345 | * ws description object can be 'external_multiple_structure', 'external_single_structure' |
346 | * or 'external_value' | |
334f9dbb | 347 | * Example of documentation for moodle_group_create_groups function: |
86dcc6f0 | 348 | list of ( |
349 | object { | |
350 | courseid int //id of course | |
351 | name string //multilang compatible name, course unique | |
352 | description string //group description text | |
353 | enrolmentkey string //group enrol secret phrase | |
354 | } | |
355 | ) | |
d4c6ef70 | 356 | * @param object $params a part of parameter/return description |
357 | * @return string the html to display | |
358 | */ | |
360e9415 | 359 | public function detailed_description_html($params) { |
86dcc6f0 | 360 | /// retrieve the description of the description object |
d4c6ef70 | 361 | $paramdesc = ""; |
362 | if (!empty($params->desc)) { | |
5d0c95a5 | 363 | $paramdesc .= html_writer::start_tag('span', array('style' => "color:#2A33A6")); |
382b9cea | 364 | if ($params->required == VALUE_REQUIRED) { |
365 | $required = ''; | |
366 | } | |
367 | if ($params->required == VALUE_DEFAULT) { | |
4b2023df | 368 | if ($params->default === null) { |
382b9cea | 369 | $params->default = "null"; |
370 | } | |
72f68b51 | 371 | $required = html_writer::start_tag('b', array()) . |
8abfe383 | 372 | get_string('default', 'webservice', print_r($params->default, true)) |
72f68b51 | 373 | . html_writer::end_tag('b'); |
382b9cea | 374 | } |
375 | if ($params->required == VALUE_OPTIONAL) { | |
72f68b51 | 376 | $required = html_writer::start_tag('b', array()) . |
377 | get_string('optional', 'webservice') . html_writer::end_tag('b'); | |
382b9cea | 378 | } |
86dcc6f0 | 379 | $paramdesc .= " " . $required . " "; |
5d0c95a5 | 380 | $paramdesc .= html_writer::start_tag('i', array()); |
382b9cea | 381 | $paramdesc .= "//"; |
382 | ||
383 | $paramdesc .= $params->desc; | |
384 | ||
5d0c95a5 | 385 | $paramdesc .= html_writer::end_tag('i'); |
86dcc6f0 | 386 | |
5d0c95a5 PS |
387 | $paramdesc .= html_writer::end_tag('span'); |
388 | $paramdesc .= html_writer::empty_tag('br', array()); | |
d4c6ef70 | 389 | } |
d4c6ef70 | 390 | |
86dcc6f0 | 391 | /// description object is a list |
334f9dbb | 392 | if ($params instanceof external_multiple_structure) { |
72f68b51 | 393 | return $paramdesc . "list of ( " . html_writer::empty_tag('br', array()) |
394 | . $this->detailed_description_html($params->content) . ")"; | |
d4c6ef70 | 395 | } else if ($params instanceof external_single_structure) { |
86dcc6f0 | 396 | /// description object is an object |
397 | $singlestructuredesc = $paramdesc . "object {" . html_writer::empty_tag('br', array()); | |
d4c6ef70 | 398 | foreach ($params->keys as $attributname => $attribut) { |
5d0c95a5 | 399 | $singlestructuredesc .= html_writer::start_tag('b', array()); |
4597fd4d | 400 | $singlestructuredesc .= $attributname; |
5d0c95a5 | 401 | $singlestructuredesc .= html_writer::end_tag('b'); |
72f68b51 | 402 | $singlestructuredesc .= " " . |
403 | $this->detailed_description_html($params->keys[$attributname]); | |
d4c6ef70 | 404 | } |
4597fd4d | 405 | $singlestructuredesc .= "} "; |
5d0c95a5 | 406 | $singlestructuredesc .= html_writer::empty_tag('br', array()); |
d4c6ef70 | 407 | return $singlestructuredesc; |
408 | } else { | |
86dcc6f0 | 409 | /// description object is a primary type (string, integer) |
410 | switch ($params->type) { | |
d4c6ef70 | 411 | case PARAM_BOOL: // 0 or 1 only for now |
412 | case PARAM_INT: | |
413 | $type = 'int'; | |
414 | break; | |
415 | case PARAM_FLOAT; | |
416 | $type = 'double'; | |
417 | break; | |
418 | default: | |
419 | $type = 'string'; | |
420 | } | |
86dcc6f0 | 421 | return $type . " " . $paramdesc; |
d4c6ef70 | 422 | } |
d4c6ef70 | 423 | } |
424 | ||
425 | /** | |
334f9dbb | 426 | * Return a description object in indented xml format (for REST response) |
d4c6ef70 | 427 | * It is indented in order to be displayed into <pre> tag |
428 | * @param object $returndescription | |
429 | * @param string $indentation composed by space only | |
430 | * @return string the html to diplay | |
431 | */ | |
432 | public function description_in_indented_xml_format($returndescription, $indentation = "") { | |
433 | $indentation = $indentation . " "; | |
434 | $brakeline = <<<EOF | |
435 | ||
436 | ||
437 | EOF; | |
86dcc6f0 | 438 | /// description object is a list |
d4c6ef70 | 439 | if ($returndescription instanceof external_multiple_structure) { |
86dcc6f0 | 440 | $return = $indentation . "<MULTIPLE>" . $brakeline; |
72f68b51 | 441 | $return .= $this->description_in_indented_xml_format($returndescription->content, |
442 | $indentation); | |
86dcc6f0 | 443 | $return .= $indentation . "</MULTIPLE>" . $brakeline; |
d4c6ef70 | 444 | return $return; |
445 | } else if ($returndescription instanceof external_single_structure) { | |
86dcc6f0 | 446 | /// description object is an object |
447 | $singlestructuredesc = $indentation . "<SINGLE>" . $brakeline; | |
448 | $keyindentation = $indentation . " "; | |
d4c6ef70 | 449 | foreach ($returndescription->keys as $attributname => $attribut) { |
72f68b51 | 450 | $singlestructuredesc .= $keyindentation . "<KEY name=\"" . $attributname . "\">" |
451 | . $brakeline . | |
452 | $this->description_in_indented_xml_format( | |
453 | $returndescription->keys[$attributname], $keyindentation) . | |
86dcc6f0 | 454 | $keyindentation . "</KEY>" . $brakeline; |
d4c6ef70 | 455 | } |
86dcc6f0 | 456 | $singlestructuredesc .= $indentation . "</SINGLE>" . $brakeline; |
d4c6ef70 | 457 | return $singlestructuredesc; |
458 | } else { | |
86dcc6f0 | 459 | /// description object is a primary type (string, integer) |
460 | switch ($returndescription->type) { | |
d4c6ef70 | 461 | case PARAM_BOOL: // 0 or 1 only for now |
462 | case PARAM_INT: | |
463 | $type = 'int'; | |
464 | break; | |
465 | case PARAM_FLOAT; | |
466 | $type = 'double'; | |
467 | break; | |
468 | default: | |
469 | $type = 'string'; | |
470 | } | |
86dcc6f0 | 471 | return $indentation . "<VALUE>" . $type . "</VALUE>" . $brakeline; |
d4c6ef70 | 472 | } |
473 | } | |
474 | ||
86dcc6f0 | 475 | /** |
360e9415 | 476 | * Create indented XML-RPC param description |
477 | * @param object $paramdescription | |
478 | * @param string $indentation composed by space only | |
479 | * @return string the html to diplay | |
480 | */ | |
481 | public function xmlrpc_param_description_html($paramdescription, $indentation = "") { | |
482 | $indentation = $indentation . " "; | |
483 | $brakeline = <<<EOF | |
484 | ||
485 | ||
486 | EOF; | |
86dcc6f0 | 487 | /// description object is a list |
360e9415 | 488 | if ($paramdescription instanceof external_multiple_structure) { |
86dcc6f0 | 489 | $return = $brakeline . $indentation . "Array "; |
360e9415 | 490 | $indentation = $indentation . " "; |
86dcc6f0 | 491 | $return .= $brakeline . $indentation . "("; |
492 | $return .= $brakeline . $indentation . "[0] =>"; | |
360e9415 | 493 | $return .= $this->xmlrpc_param_description_html($paramdescription->content, $indentation); |
86dcc6f0 | 494 | $return .= $brakeline . $indentation . ")"; |
360e9415 | 495 | return $return; |
496 | } else if ($paramdescription instanceof external_single_structure) { | |
86dcc6f0 | 497 | /// description object is an object |
498 | $singlestructuredesc = $brakeline . $indentation . "Array "; | |
499 | $keyindentation = $indentation . " "; | |
500 | $singlestructuredesc .= $brakeline . $keyindentation . "("; | |
360e9415 | 501 | foreach ($paramdescription->keys as $attributname => $attribut) { |
86dcc6f0 | 502 | $singlestructuredesc .= $brakeline . $keyindentation . "[" . $attributname . "] =>" . |
72f68b51 | 503 | $this->xmlrpc_param_description_html( |
504 | $paramdescription->keys[$attributname], $keyindentation) . | |
360e9415 | 505 | $keyindentation; |
506 | } | |
86dcc6f0 | 507 | $singlestructuredesc .= $brakeline . $keyindentation . ")"; |
360e9415 | 508 | return $singlestructuredesc; |
509 | } else { | |
86dcc6f0 | 510 | /// description object is a primary type (string, integer) |
511 | switch ($paramdescription->type) { | |
360e9415 | 512 | case PARAM_BOOL: // 0 or 1 only for now |
513 | case PARAM_INT: | |
514 | $type = 'int'; | |
515 | break; | |
516 | case PARAM_FLOAT; | |
517 | $type = 'double'; | |
518 | break; | |
519 | default: | |
520 | $type = 'string'; | |
521 | } | |
86dcc6f0 | 522 | return " " . $type; |
360e9415 | 523 | } |
524 | } | |
525 | ||
334f9dbb | 526 | /** |
527 | * Return the html of a colored box with content | |
528 | * @param string $title - the title of the box | |
529 | * @param string $content - the content to displayed | |
530 | * @param string $rgb - the background color of the box | |
531 | * @return <type> | |
532 | */ | |
4597fd4d | 533 | public function colored_box_with_pre_tag($title, $content, $rgb = 'FEEBE5') { |
72f68b51 | 534 | //TODO: this tag removes xhtml strict error but cause warning |
8abfe383 | 535 | $coloredbox = html_writer::start_tag('div', array()); |
72f68b51 | 536 | $coloredbox .= html_writer::start_tag('div', |
537 | array('style' => "border:solid 1px #DEDEDE;background:#" . $rgb | |
538 | . ";color:#222222;padding:4px;")); | |
5d0c95a5 PS |
539 | $coloredbox .= html_writer::start_tag('pre', array()); |
540 | $coloredbox .= html_writer::start_tag('b', array()); | |
4597fd4d | 541 | $coloredbox .= $title; |
5d0c95a5 PS |
542 | $coloredbox .= html_writer::end_tag('b', array()); |
543 | $coloredbox .= html_writer::empty_tag('br', array()); | |
86dcc6f0 | 544 | $coloredbox .= "\n" . $content . "\n"; |
5d0c95a5 PS |
545 | $coloredbox .= html_writer::end_tag('pre', array()); |
546 | $coloredbox .= html_writer::end_tag('div', array()); | |
8abfe383 | 547 | $coloredbox .= html_writer::end_tag('div', array()); |
4597fd4d | 548 | return $coloredbox; |
d4c6ef70 | 549 | } |
550 | ||
86dcc6f0 | 551 | /** |
334f9dbb | 552 | * Return indented REST param description |
886e3bfb | 553 | * @param object $paramdescription |
554 | * @param string $indentation composed by space only | |
555 | * @return string the html to diplay | |
556 | */ | |
557 | public function rest_param_description_html($paramdescription, $paramstring) { | |
558 | $brakeline = <<<EOF | |
559 | ||
560 | ||
561 | EOF; | |
86dcc6f0 | 562 | /// description object is a list |
886e3bfb | 563 | if ($paramdescription instanceof external_multiple_structure) { |
86dcc6f0 | 564 | $paramstring = $paramstring . '[0]'; |
886e3bfb | 565 | $return = $this->rest_param_description_html($paramdescription->content, $paramstring); |
566 | return $return; | |
567 | } else if ($paramdescription instanceof external_single_structure) { | |
86dcc6f0 | 568 | /// description object is an object |
886e3bfb | 569 | $singlestructuredesc = ""; |
7ef73632 | 570 | $initialparamstring = $paramstring; |
886e3bfb | 571 | foreach ($paramdescription->keys as $attributname => $attribut) { |
86dcc6f0 | 572 | $paramstring = $initialparamstring . '[' . $attributname . ']'; |
72f68b51 | 573 | $singlestructuredesc .= $this->rest_param_description_html( |
574 | $paramdescription->keys[$attributname], $paramstring); | |
886e3bfb | 575 | } |
576 | return $singlestructuredesc; | |
577 | } else { | |
86dcc6f0 | 578 | /// description object is a primary type (string, integer) |
579 | $paramstring = $paramstring . '='; | |
580 | switch ($paramdescription->type) { | |
886e3bfb | 581 | case PARAM_BOOL: // 0 or 1 only for now |
582 | case PARAM_INT: | |
583 | $type = 'int'; | |
584 | break; | |
585 | case PARAM_FLOAT; | |
586 | $type = 'double'; | |
587 | break; | |
588 | default: | |
589 | $type = 'string'; | |
590 | } | |
86dcc6f0 | 591 | return $paramstring . " " . $type . $brakeline; |
886e3bfb | 592 | } |
593 | } | |
594 | ||
d4c6ef70 | 595 | /** |
596 | * This display all the documentation | |
597 | * @param array $functions contains all decription objects | |
9ef728d6 | 598 | * @param array $authparam keys contains 'tokenid' |
cdb0bd6a | 599 | * @param boolean $printableformat true if we want to display the documentation in a printable format |
600 | * @param array $activatedprotocol | |
cde291ed JM |
601 | * @param string $parenturl url of the calling page - needed for the print button url: |
602 | * '/admin/documentation.php' or '/webservice/wsdoc.php' (default) | |
d4c6ef70 | 603 | * @return string the html to diplay |
604 | */ | |
cde291ed JM |
605 | public function documentation_html($functions, $printableformat, $activatedprotocol, |
606 | $authparams, $parenturl = '/webservice/wsdoc.php') { | |
9ef728d6 | 607 | |
cde291ed | 608 | $documentationhtml = $this->output->heading(get_string('wsdocapi', 'webservice')); |
9ef728d6 | 609 | |
85b4c447 | 610 | $br = html_writer::empty_tag('br', array()); |
360e9415 | 611 | $brakeline = <<<EOF |
612 | ||
613 | ||
614 | EOF; | |
86dcc6f0 | 615 | /// Some general information |
390cc887 | 616 | $docinfo = new stdClass(); |
728ebac7 | 617 | $docurl = new moodle_url('http://docs.moodle.org/dev/Creating_a_web_service_client'); |
390cc887 | 618 | $docinfo->doclink = html_writer::tag('a', |
72f68b51 | 619 | get_string('wsclientdoc', 'webservice'), array('href' => $docurl)); |
9ef728d6 | 620 | $documentationhtml .= html_writer::start_tag('table', |
72f68b51 | 621 | array('style' => "margin-left:auto; margin-right:auto;")); |
5d0c95a5 PS |
622 | $documentationhtml .= html_writer::start_tag('tr', array()); |
623 | $documentationhtml .= html_writer::start_tag('td', array()); | |
390cc887 | 624 | $documentationhtml .= get_string('wsdocumentationintro', 'webservice', $docinfo); |
86dcc6f0 | 625 | $documentationhtml .= $br . $br; |
626 | ||
cdb0bd6a | 627 | |
86dcc6f0 | 628 | /// Print button |
85b4c447 | 629 | $authparams['print'] = true; |
630 | //$parameters = array ('token' => $token, 'wsusername' => $username, 'wspassword' => $password, 'print' => true); | |
cde291ed | 631 | $url = new moodle_url($parenturl, $authparams); // Required |
72f68b51 | 632 | $documentationhtml .= $this->output->single_button($url, get_string('print', 'webservice')); |
85b4c447 | 633 | $documentationhtml .= $br; |
86dcc6f0 | 634 | |
635 | ||
72f68b51 | 636 | /// each functions will be displayed into a collapsible region |
637 | //(opened if printableformat = true) | |
d4c6ef70 | 638 | foreach ($functions as $functionname => $description) { |
f03bff3e | 639 | |
640 | if (empty($printableformat)) { | |
641 | $documentationhtml .= print_collapsible_region_start('', | |
86dcc6f0 | 642 | 'aera_' . $functionname, |
72f68b51 | 643 | html_writer::start_tag('strong', array()) |
644 | . $functionname . html_writer::end_tag('strong'), | |
86dcc6f0 | 645 | false, |
646 | !$printableformat, | |
647 | true); | |
f03bff3e | 648 | } else { |
26acc814 | 649 | $documentationhtml .= html_writer::tag('strong', $functionname); |
85b4c447 | 650 | $documentationhtml .= $br; |
f03bff3e | 651 | } |
d4c6ef70 | 652 | |
86dcc6f0 | 653 | /// function global description |
85b4c447 | 654 | $documentationhtml .= $br; |
72f68b51 | 655 | $documentationhtml .= html_writer::start_tag('div', |
656 | array('style' => 'border:solid 1px #DEDEDE;background:#E2E0E0; | |
657 | color:#222222;padding:4px;')); | |
d4c6ef70 | 658 | $documentationhtml .= $description->description; |
5d0c95a5 | 659 | $documentationhtml .= html_writer::end_tag('div'); |
86dcc6f0 | 660 | $documentationhtml .= $br . $br; |
4597fd4d | 661 | |
86dcc6f0 | 662 | /// function arguments documentation |
5d0c95a5 | 663 | $documentationhtml .= html_writer::start_tag('span', array('style' => 'color:#EA33A6')); |
4597fd4d | 664 | $documentationhtml .= get_string('arguments', 'webservice'); |
5d0c95a5 | 665 | $documentationhtml .= html_writer::end_tag('span'); |
85b4c447 | 666 | $documentationhtml .= $br; |
d4c6ef70 | 667 | foreach ($description->parameters_desc->keys as $paramname => $paramdesc) { |
86dcc6f0 | 668 | /// a argument documentation |
5d0c95a5 | 669 | $documentationhtml .= html_writer::start_tag('span', array('style' => 'font-size:80%')); |
86dcc6f0 | 670 | |
382b9cea | 671 | if ($paramdesc->required == VALUE_REQUIRED) { |
86dcc6f0 | 672 | $required = get_string('required', 'webservice'); |
382b9cea | 673 | } |
674 | if ($paramdesc->required == VALUE_DEFAULT) { | |
4b2023df | 675 | if ($paramdesc->default === null) { |
382b9cea | 676 | $default = "null"; |
677 | } else { | |
8abfe383 | 678 | $default = print_r($paramdesc->default, true); |
382b9cea | 679 | } |
680 | $required = get_string('default', 'webservice', $default); | |
681 | } | |
682 | if ($paramdesc->required == VALUE_OPTIONAL) { | |
86dcc6f0 | 683 | $required = get_string('optional', 'webservice'); |
382b9cea | 684 | } |
86dcc6f0 | 685 | |
5d0c95a5 | 686 | $documentationhtml .= html_writer::start_tag('b', array()); |
4597fd4d | 687 | $documentationhtml .= $paramname; |
5d0c95a5 | 688 | $documentationhtml .= html_writer::end_tag('b'); |
86dcc6f0 | 689 | $documentationhtml .= " (" . $required . ")"; // argument is required or optional ? |
85b4c447 | 690 | $documentationhtml .= $br; |
72f68b51 | 691 | $documentationhtml .= " " |
692 | . $paramdesc->desc; // argument description | |
86dcc6f0 | 693 | $documentationhtml .= $br . $br; |
334f9dbb | 694 | ///general structure of the argument |
72f68b51 | 695 | $documentationhtml .= $this->colored_box_with_pre_tag( |
696 | get_string('generalstructure', 'webservice'), | |
86dcc6f0 | 697 | $this->detailed_description_html($paramdesc), |
698 | 'FFF1BC'); | |
334f9dbb | 699 | ///xml-rpc structure of the argument in PHP format |
abf7dc44 | 700 | if (!empty($activatedprotocol['xmlrpc'])) { |
72f68b51 | 701 | $documentationhtml .= $this->colored_box_with_pre_tag( |
702 | get_string('phpparam', 'webservice'), | |
703 | htmlentities('[' . $paramname . '] =>' | |
704 | . $this->xmlrpc_param_description_html($paramdesc)), | |
86dcc6f0 | 705 | 'DFEEE7'); |
abf7dc44 | 706 | } |
334f9dbb | 707 | ///POST format for the REST protocol for the argument |
abf7dc44 | 708 | if (!empty($activatedprotocol['rest'])) { |
72f68b51 | 709 | $documentationhtml .= $this->colored_box_with_pre_tag( |
710 | get_string('restparam', 'webservice'), | |
711 | htmlentities($this->rest_param_description_html( | |
712 | $paramdesc, $paramname)), | |
86dcc6f0 | 713 | 'FEEBE5'); |
abf7dc44 | 714 | } |
5d0c95a5 | 715 | $documentationhtml .= html_writer::end_tag('span'); |
d4c6ef70 | 716 | } |
86dcc6f0 | 717 | $documentationhtml .= $br . $br; |
4597fd4d | 718 | |
334f9dbb | 719 | |
86dcc6f0 | 720 | /// function response documentation |
5d0c95a5 | 721 | $documentationhtml .= html_writer::start_tag('span', array('style' => 'color:#EA33A6')); |
4597fd4d | 722 | $documentationhtml .= get_string('response', 'webservice'); |
5d0c95a5 | 723 | $documentationhtml .= html_writer::end_tag('span'); |
85b4c447 | 724 | $documentationhtml .= $br; |
334f9dbb | 725 | /// function response description |
5d0c95a5 | 726 | $documentationhtml .= html_writer::start_tag('span', array('style' => 'font-size:80%')); |
d4c6ef70 | 727 | if (!empty($description->returns_desc->desc)) { |
4597fd4d | 728 | $documentationhtml .= $description->returns_desc->desc; |
86dcc6f0 | 729 | $documentationhtml .= $br . $br; |
d4c6ef70 | 730 | } |
d4c6ef70 | 731 | if (!empty($description->returns_desc)) { |
334f9dbb | 732 | ///general structure of the response |
72f68b51 | 733 | $documentationhtml .= $this->colored_box_with_pre_tag( |
734 | get_string('generalstructure', 'webservice'), | |
86dcc6f0 | 735 | $this->detailed_description_html($description->returns_desc), |
736 | 'FFF1BC'); | |
334f9dbb | 737 | ///xml-rpc structure of the response in PHP format |
abf7dc44 | 738 | if (!empty($activatedprotocol['xmlrpc'])) { |
72f68b51 | 739 | $documentationhtml .= $this->colored_box_with_pre_tag( |
740 | get_string('phpresponse', 'webservice'), | |
741 | htmlentities($this->xmlrpc_param_description_html( | |
742 | $description->returns_desc)), | |
86dcc6f0 | 743 | 'DFEEE7'); |
abf7dc44 | 744 | } |
334f9dbb | 745 | ///XML response for the REST protocol |
abf7dc44 | 746 | if (!empty($activatedprotocol['rest'])) { |
72f68b51 | 747 | $restresponse = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" |
748 | . $brakeline . "<RESPONSE>" . $brakeline; | |
749 | $restresponse .= $this->description_in_indented_xml_format( | |
750 | $description->returns_desc); | |
86dcc6f0 | 751 | $restresponse .="</RESPONSE>" . $brakeline; |
72f68b51 | 752 | $documentationhtml .= $this->colored_box_with_pre_tag( |
753 | get_string('restcode', 'webservice'), | |
86dcc6f0 | 754 | htmlentities($restresponse), |
755 | 'FEEBE5'); | |
abf7dc44 | 756 | } |
d4c6ef70 | 757 | } |
5d0c95a5 | 758 | $documentationhtml .= html_writer::end_tag('span'); |
86dcc6f0 | 759 | $documentationhtml .= $br . $br; |
d4c6ef70 | 760 | |
86dcc6f0 | 761 | /// function errors documentation for REST protocol |
abf7dc44 | 762 | if (!empty($activatedprotocol['rest'])) { |
5d0c95a5 | 763 | $documentationhtml .= html_writer::start_tag('span', array('style' => 'color:#EA33A6')); |
4597fd4d | 764 | $documentationhtml .= get_string('errorcodes', 'webservice'); |
5d0c95a5 | 765 | $documentationhtml .= html_writer::end_tag('span'); |
86dcc6f0 | 766 | $documentationhtml .= $br . $br; |
5d0c95a5 | 767 | $documentationhtml .= html_writer::start_tag('span', array('style' => 'font-size:80%')); |
abf7dc44 | 768 | $errormessage = get_string('invalidparameter', 'debug'); |
86dcc6f0 | 769 | $restexceptiontext = <<<EOF |
abf7dc44 | 770 | <?xml version="1.0" encoding="UTF-8"?> |
771 | <EXCEPTION class="invalid_parameter_exception"> | |
772 | <MESSAGE>{$errormessage}</MESSAGE> | |
773 | <DEBUGINFO></DEBUGINFO> | |
774 | </EXCEPTION> | |
775 | EOF; | |
72f68b51 | 776 | $documentationhtml .= $this->colored_box_with_pre_tag( |
777 | get_string('restexception', 'webservice'), | |
86dcc6f0 | 778 | htmlentities($restexceptiontext), |
779 | 'FEEBE5'); | |
4597fd4d | 780 | |
86dcc6f0 | 781 | $documentationhtml .= html_writer::end_tag('span'); |
abf7dc44 | 782 | } |
86dcc6f0 | 783 | $documentationhtml .= $br . $br; |
f03bff3e | 784 | if (empty($printableformat)) { |
785 | $documentationhtml .= print_collapsible_region_end(true); | |
786 | } | |
d4c6ef70 | 787 | } |
788 | ||
86dcc6f0 | 789 | /// close the table and return the documentation |
5d0c95a5 PS |
790 | $documentationhtml .= html_writer::end_tag('td'); |
791 | $documentationhtml .= html_writer::end_tag('tr'); | |
792 | $documentationhtml .= html_writer::end_tag('table'); | |
cdb0bd6a | 793 | |
d4c6ef70 | 794 | return $documentationhtml; |
d4c6ef70 | 795 | } |
796 | ||
d4c6ef70 | 797 | } |