Merge branch 'wip-MDL-32018-m21' of git://github.com/samhemelryk/moodle into MOODLE_2...
[moodle.git] / webservice / renderer.php
blob75668fd6a8aca763e2fabf91af9d280d4eff90b6
1 <?php
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 ///////////////////////////////////////////////////////////////////////////
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
30 class core_webservice_renderer extends plugin_renderer_base {
32 /**
33 * Display the authorised user selector
34 * @param object $options
35 * @return string html
37 public function admin_authorised_user_selector(&$options) {
38 global $CFG;
39 $formcontent = html_writer::empty_tag('input',
40 array('name' => 'sesskey', 'value' => sesskey(), 'type' => 'hidden'));
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;
49 // LTR/RTL support, for drawing button arrows in the right direction
50 if (right_to_left()) {
51 $addarrow = '▶';
52 $removearrow = '◀';
53 } else {
54 $addarrow = '◀';
55 $removearrow = '▶';
58 //create the add and remove button
59 $addinput = html_writer::empty_tag('input',
60 array('name' => 'add', 'id' => 'add', 'type' => 'submit',
61 'value' => $addarrow . ' ' . get_string('add'),
62 'title' => get_string('add')));
63 $addbutton = html_writer::tag('div', $addinput, array('id' => 'addcontrols'));
64 $removeinput = html_writer::empty_tag('input',
65 array('name' => 'remove', 'id' => 'remove', 'type' => 'submit',
66 'value' => $removearrow . ' ' . get_string('remove'),
67 'title' => get_string('remove')));
68 $removebutton = html_writer::tag('div', $removeinput, array('id' => 'removecontrols'));
71 //create the three cells
72 $label = html_writer::tag('label', get_string('serviceusers', 'webservice'),
73 array('for' => 'removeselect'));
74 $label = html_writer::tag('p', $label);
75 $authoriseduserscell = new html_table_cell($label .
76 $options->alloweduserselector->display(true));
77 $authoriseduserscell->id = 'existingcell';
78 $buttonscell = new html_table_cell($addbutton . html_writer::empty_tag('br') . $removebutton);
79 $buttonscell->id = 'buttonscell';
80 $label = html_writer::tag('label', get_string('potusers', 'webservice'),
81 array('for' => 'addselect'));
82 $label = html_writer::tag('p', $label);
83 $otheruserscell = new html_table_cell($label .
84 $options->potentialuserselector->display(true));
85 $otheruserscell->id = 'potentialcell';
87 $cells = array($authoriseduserscell, $buttonscell, $otheruserscell);
88 $row = new html_table_row($cells);
89 $table->data[] = $row;
90 $formcontent .= html_writer::table($table);
92 $formcontent = html_writer::tag('div', $formcontent);
94 $actionurl = new moodle_url('/' . $CFG->admin . '/webservice/service_users.php',
95 array('id' => $options->serviceid));
96 $html = html_writer::tag('form', $formcontent,
97 array('id' => 'assignform', 'action' => $actionurl, 'method' => 'post'));
98 return $html;
102 * Display list of authorised user
103 * @param array $users
104 * @return string $html
106 public function admin_authorised_user_list($users, $serviceid) {
107 global $CFG;
108 $html = $this->output->box_start('generalbox', 'alloweduserlist');
109 foreach ($users as $user) {
110 $modifiedauthoriseduserurl = new moodle_url('/' . $CFG->admin . '/webservice/service_user_settings.php',
111 array('userid' => $user->id, 'serviceid' => $serviceid));
112 $html .= html_writer::tag('a', $user->firstname . " "
113 . $user->lastname . ", " . $user->email,
114 array('href' => $modifiedauthoriseduserurl));
115 //add missing capabilities
116 if (!empty($user->missingcapabilities)) {
117 $html .= html_writer::tag('div',
118 get_string('usermissingcaps', 'webservice', $user->missingcapabilities)
119 . '&nbsp;' . $this->output->help_icon('missingcaps', 'webservice'),
120 array('class' => 'missingcaps', 'id' => 'usermissingcaps'));
121 $html .= html_writer::empty_tag('br');
122 } else {
123 $html .= html_writer::empty_tag('br') . html_writer::empty_tag('br');
126 $html .= $this->output->box_end();
127 return $html;
131 * Display a confirmation page to remove a function from a service
132 * @param object $function
133 * @param object $service
134 * @return string html
136 public function admin_remove_service_function_confirmation($function, $service) {
137 $optionsyes = array('id' => $service->id, 'action' => 'delete',
138 'confirm' => 1, 'sesskey' => sesskey(), 'fid' => $function->id);
139 $optionsno = array('id' => $service->id);
140 $formcontinue = new single_button(new moodle_url('service_functions.php',
141 $optionsyes), get_string('remove'));
142 $formcancel = new single_button(new moodle_url('service_functions.php',
143 $optionsno), get_string('cancel'), 'get');
144 return $this->output->confirm(get_string('removefunctionconfirm', 'webservice',
145 (object) array('service' => $service->name, 'function' => $function->name)),
146 $formcontinue, $formcancel);
150 * Display a confirmation page to remove a service
151 * @param object $service
152 * @return string html
154 public function admin_remove_service_confirmation($service) {
155 global $CFG;
156 $optionsyes = array('id' => $service->id, 'action' => 'delete',
157 'confirm' => 1, 'sesskey' => sesskey());
158 $optionsno = array('section' => 'externalservices');
159 $formcontinue = new single_button(new moodle_url('service.php', $optionsyes),
160 get_string('delete'), 'post');
161 $formcancel = new single_button(
162 new moodle_url($CFG->wwwroot . "/" . $CFG->admin . "/settings.php", $optionsno),
163 get_string('cancel'), 'get');
164 return $this->output->confirm(get_string('deleteserviceconfirm', 'webservice', $service->name),
165 $formcontinue, $formcancel);
169 * Display a confirmation page to delete a token
170 * @param object $token
171 * @return string html
173 public function admin_delete_token_confirmation($token) {
174 global $CFG;
175 $optionsyes = array('tokenid' => $token->id, 'action' => 'delete',
176 'confirm' => 1, 'sesskey' => sesskey());
177 $optionsno = array('section' => 'webservicetokens', 'sesskey' => sesskey());
178 $formcontinue = new single_button(
179 new moodle_url('/' . $CFG->admin . '/webservice/tokens.php', $optionsyes),
180 get_string('delete'));
181 $formcancel = new single_button(
182 new moodle_url('/' . $CFG->admin . '/settings.php', $optionsno),
183 get_string('cancel'), 'get');
184 return $this->output->confirm(get_string('deletetokenconfirm', 'webservice',
185 (object) array('user' => $token->firstname . " "
186 . $token->lastname, 'service' => $token->name)),
187 $formcontinue, $formcancel);
191 * Display list of function for a given service
192 * If the service is build-in do not display remove/add operation (read-only)
193 * @param array $functions
194 * @param object $service
195 * @return string the table html + add operation html
197 public function admin_service_function_list($functions, $service) {
198 global $CFG;
199 if (!empty($functions)) {
200 $table = new html_table();
201 $table->head = array(get_string('function', 'webservice'),
202 get_string('description'), get_string('requiredcaps', 'webservice'));
203 $table->align = array('left', 'left', 'left');
204 $table->size = array('15%', '40%', '40%');
205 $table->width = '100%';
206 $table->align[] = 'left';
208 //display remove function operation (except for build-in service)
209 if (empty($service->component)) {
210 $table->head[] = get_string('edit');
211 $table->align[] = 'center';
214 foreach ($functions as $function) {
215 $function = external_function_info($function);
216 $requiredcaps = html_writer::tag('div',
217 empty($function->capabilities) ? '' : $function->capabilities,
218 array('class' => 'functiondesc'));
220 $description = html_writer::tag('div', $function->description,
221 array('class' => 'functiondesc'));
222 //display remove function operation (except for build-in service)
223 if (empty($service->component)) {
224 $removeurl = new moodle_url('/' . $CFG->admin . '/webservice/service_functions.php',
225 array('sesskey' => sesskey(), 'fid' => $function->id,
226 'id' => $service->id,
227 'action' => 'delete'));
228 $removelink = html_writer::tag('a',
229 get_string('removefunction', 'webservice'),
230 array('href' => $removeurl));
231 $table->data[] = array($function->name, $description, $requiredcaps, $removelink);
232 } else {
233 $table->data[] = array($function->name, $description, $requiredcaps);
237 $html = html_writer::table($table);
238 } else {
239 $html = get_string('nofunctions', 'webservice') . html_writer::empty_tag('br');
242 //display add function operation (except for build-in service)
243 if (empty($service->component)) {
244 $addurl = new moodle_url('/' . $CFG->admin . '/webservice/service_functions.php',
245 array('sesskey' => sesskey(), 'id' => $service->id, 'action' => 'add'));
246 $html .= html_writer::tag('a', get_string('addfunctions', 'webservice'), array('href' => $addurl));
249 return $html;
253 * Display Reset token confirmation box
254 * @param object $token to reset
255 * @return string html
257 public function user_reset_token_confirmation($token) {
258 global $CFG;
259 $managetokenurl = $CFG->wwwroot . "/user/managetoken.php?sesskey=" . sesskey();
260 $optionsyes = array('tokenid' => $token->id, 'action' => 'resetwstoken', 'confirm' => 1,
261 'sesskey' => sesskey());
262 $optionsno = array('section' => 'webservicetokens', 'sesskey' => sesskey());
263 $formcontinue = new single_button(new moodle_url($managetokenurl, $optionsyes),
264 get_string('reset'));
265 $formcancel = new single_button(new moodle_url($managetokenurl, $optionsno),
266 get_string('cancel'), 'get');
267 $html = $this->output->confirm(get_string('resettokenconfirm', 'webservice',
268 (object) array('user' => $token->firstname . " " .
269 $token->lastname, 'service' => $token->name)),
270 $formcontinue, $formcancel);
271 return $html;
275 * Display user tokens with buttons to reset them
276 * @param object $tokens
277 * @param int $userid
278 * @return string html code
280 public function user_webservice_tokens_box($tokens, $userid, $documentation = false) {
281 global $CFG, $DB;
283 // display strings
284 $stroperation = get_string('operation', 'webservice');
285 $strtoken = get_string('key', 'webservice');
286 $strservice = get_string('service', 'webservice');
287 $strcreator = get_string('tokencreator', 'webservice');
288 $strcontext = get_string('context', 'webservice');
289 $strvaliduntil = get_string('validuntil', 'webservice');
291 $return = $this->output->heading(get_string('securitykeys', 'webservice'), 3, 'main', true);
292 $return .= $this->output->box_start('generalbox webservicestokenui');
294 $return .= get_string('keyshelp', 'webservice');
296 $table = new html_table();
297 $table->head = array($strtoken, $strservice, $strvaliduntil, $strcreator, $stroperation);
298 $table->align = array('left', 'left', 'left', 'center', 'left', 'center');
299 $table->width = '100%';
300 $table->data = array();
302 if ($documentation) {
303 $table->head[] = get_string('doc', 'webservice');
304 $table->align[] = 'center';
307 if (!empty($tokens)) {
308 foreach ($tokens as $token) {
310 if ($token->creatorid == $userid) {
311 $reset = "<a href=\"" . $CFG->wwwroot . "/user/managetoken.php?sesskey="
312 . sesskey() . "&amp;action=resetwstoken&amp;tokenid=" . $token->id . "\">";
313 $reset .= get_string('reset') . "</a>";
314 $creator = $token->firstname . " " . $token->lastname;
315 } else {
316 //retrieve administrator name
317 $admincreator = $DB->get_record('user', array('id'=>$token->creatorid));
318 $creator = $admincreator->firstname . " " . $admincreator->lastname;
319 $reset = '';
322 $userprofilurl = new moodle_url('/user/view.php?id=' . $token->creatorid);
323 $creatoratag = html_writer::start_tag('a', array('href' => $userprofilurl));
324 $creatoratag .= $creator;
325 $creatoratag .= html_writer::end_tag('a');
327 $validuntil = '';
328 if (!empty($token->validuntil)) {
329 $validuntil = date("F j, Y"); //TODO: language support (look for moodle function)
332 $tokenname = $token->name;
333 if (!$token->enabled) { //that is the (1 token-1ws) related ws is not enabled.
334 $tokenname = '<span class="dimmed_text">'.$token->name.'</span>';
336 $row = array($token->token, $tokenname, $validuntil, $creatoratag, $reset);
338 if ($documentation) {
339 $doclink = new moodle_url('/webservice/wsdoc.php',
340 array('id' => $token->id, 'sesskey' => sesskey()));
341 $row[] = html_writer::tag('a', get_string('doc', 'webservice'),
342 array('href' => $doclink));
345 $table->data[] = $row;
347 $return .= html_writer::table($table);
348 } else {
349 $return .= get_string('notoken', 'webservice');
352 $return .= $this->output->box_end();
353 return $return;
357 * Return documentation for a ws description object
358 * ws description object can be 'external_multiple_structure', 'external_single_structure'
359 * or 'external_value'
360 * Example of documentation for moodle_group_create_groups function:
361 list of (
362 object {
363 courseid int //id of course
364 name string //multilang compatible name, course unique
365 description string //group description text
366 enrolmentkey string //group enrol secret phrase
369 * @param object $params a part of parameter/return description
370 * @return string the html to display
372 public function detailed_description_html($params) {
373 /// retrieve the description of the description object
374 $paramdesc = "";
375 if (!empty($params->desc)) {
376 $paramdesc .= html_writer::start_tag('span', array('style' => "color:#2A33A6"));
377 if ($params->required == VALUE_REQUIRED) {
378 $required = '';
380 if ($params->required == VALUE_DEFAULT) {
381 if ($params->default === null) {
382 $params->default = "null";
384 $required = html_writer::start_tag('b', array()) .
385 get_string('default', 'webservice', print_r($params->default, true))
386 . html_writer::end_tag('b');
388 if ($params->required == VALUE_OPTIONAL) {
389 $required = html_writer::start_tag('b', array()) .
390 get_string('optional', 'webservice') . html_writer::end_tag('b');
392 $paramdesc .= " " . $required . " ";
393 $paramdesc .= html_writer::start_tag('i', array());
394 $paramdesc .= "//";
396 $paramdesc .= $params->desc;
398 $paramdesc .= html_writer::end_tag('i');
400 $paramdesc .= html_writer::end_tag('span');
401 $paramdesc .= html_writer::empty_tag('br', array());
404 /// description object is a list
405 if ($params instanceof external_multiple_structure) {
406 return $paramdesc . "list of ( " . html_writer::empty_tag('br', array())
407 . $this->detailed_description_html($params->content) . ")";
408 } else if ($params instanceof external_single_structure) {
409 /// description object is an object
410 $singlestructuredesc = $paramdesc . "object {" . html_writer::empty_tag('br', array());
411 foreach ($params->keys as $attributname => $attribut) {
412 $singlestructuredesc .= html_writer::start_tag('b', array());
413 $singlestructuredesc .= $attributname;
414 $singlestructuredesc .= html_writer::end_tag('b');
415 $singlestructuredesc .= " " .
416 $this->detailed_description_html($params->keys[$attributname]);
418 $singlestructuredesc .= "} ";
419 $singlestructuredesc .= html_writer::empty_tag('br', array());
420 return $singlestructuredesc;
421 } else {
422 /// description object is a primary type (string, integer)
423 switch ($params->type) {
424 case PARAM_BOOL: // 0 or 1 only for now
425 case PARAM_INT:
426 $type = 'int';
427 break;
428 case PARAM_FLOAT;
429 $type = 'double';
430 break;
431 default:
432 $type = 'string';
434 return $type . " " . $paramdesc;
439 * Return a description object in indented xml format (for REST response)
440 * It is indented in order to be displayed into <pre> tag
441 * @param object $returndescription
442 * @param string $indentation composed by space only
443 * @return string the html to diplay
445 public function description_in_indented_xml_format($returndescription, $indentation = "") {
446 $indentation = $indentation . " ";
447 $brakeline = <<<EOF
450 EOF;
451 /// description object is a list
452 if ($returndescription instanceof external_multiple_structure) {
453 $return = $indentation . "<MULTIPLE>" . $brakeline;
454 $return .= $this->description_in_indented_xml_format($returndescription->content,
455 $indentation);
456 $return .= $indentation . "</MULTIPLE>" . $brakeline;
457 return $return;
458 } else if ($returndescription instanceof external_single_structure) {
459 /// description object is an object
460 $singlestructuredesc = $indentation . "<SINGLE>" . $brakeline;
461 $keyindentation = $indentation . " ";
462 foreach ($returndescription->keys as $attributname => $attribut) {
463 $singlestructuredesc .= $keyindentation . "<KEY name=\"" . $attributname . "\">"
464 . $brakeline .
465 $this->description_in_indented_xml_format(
466 $returndescription->keys[$attributname], $keyindentation) .
467 $keyindentation . "</KEY>" . $brakeline;
469 $singlestructuredesc .= $indentation . "</SINGLE>" . $brakeline;
470 return $singlestructuredesc;
471 } else {
472 /// description object is a primary type (string, integer)
473 switch ($returndescription->type) {
474 case PARAM_BOOL: // 0 or 1 only for now
475 case PARAM_INT:
476 $type = 'int';
477 break;
478 case PARAM_FLOAT;
479 $type = 'double';
480 break;
481 default:
482 $type = 'string';
484 return $indentation . "<VALUE>" . $type . "</VALUE>" . $brakeline;
489 * Create indented XML-RPC param description
490 * @param object $paramdescription
491 * @param string $indentation composed by space only
492 * @return string the html to diplay
494 public function xmlrpc_param_description_html($paramdescription, $indentation = "") {
495 $indentation = $indentation . " ";
496 $brakeline = <<<EOF
499 EOF;
500 /// description object is a list
501 if ($paramdescription instanceof external_multiple_structure) {
502 $return = $brakeline . $indentation . "Array ";
503 $indentation = $indentation . " ";
504 $return .= $brakeline . $indentation . "(";
505 $return .= $brakeline . $indentation . "[0] =>";
506 $return .= $this->xmlrpc_param_description_html($paramdescription->content, $indentation);
507 $return .= $brakeline . $indentation . ")";
508 return $return;
509 } else if ($paramdescription instanceof external_single_structure) {
510 /// description object is an object
511 $singlestructuredesc = $brakeline . $indentation . "Array ";
512 $keyindentation = $indentation . " ";
513 $singlestructuredesc .= $brakeline . $keyindentation . "(";
514 foreach ($paramdescription->keys as $attributname => $attribut) {
515 $singlestructuredesc .= $brakeline . $keyindentation . "[" . $attributname . "] =>" .
516 $this->xmlrpc_param_description_html(
517 $paramdescription->keys[$attributname], $keyindentation) .
518 $keyindentation;
520 $singlestructuredesc .= $brakeline . $keyindentation . ")";
521 return $singlestructuredesc;
522 } else {
523 /// description object is a primary type (string, integer)
524 switch ($paramdescription->type) {
525 case PARAM_BOOL: // 0 or 1 only for now
526 case PARAM_INT:
527 $type = 'int';
528 break;
529 case PARAM_FLOAT;
530 $type = 'double';
531 break;
532 default:
533 $type = 'string';
535 return " " . $type;
540 * Return the html of a colored box with content
541 * @param string $title - the title of the box
542 * @param string $content - the content to displayed
543 * @param string $rgb - the background color of the box
544 * @return <type>
546 public function colored_box_with_pre_tag($title, $content, $rgb = 'FEEBE5') {
547 //TODO: this tag removes xhtml strict error but cause warning
548 $coloredbox = html_writer::start_tag('div', array());
549 $coloredbox .= html_writer::start_tag('div',
550 array('style' => "border:solid 1px #DEDEDE;background:#" . $rgb
551 . ";color:#222222;padding:4px;"));
552 $coloredbox .= html_writer::start_tag('pre', array());
553 $coloredbox .= html_writer::start_tag('b', array());
554 $coloredbox .= $title;
555 $coloredbox .= html_writer::end_tag('b', array());
556 $coloredbox .= html_writer::empty_tag('br', array());
557 $coloredbox .= "\n" . $content . "\n";
558 $coloredbox .= html_writer::end_tag('pre', array());
559 $coloredbox .= html_writer::end_tag('div', array());
560 $coloredbox .= html_writer::end_tag('div', array());
561 return $coloredbox;
565 * Return indented REST param description
566 * @param object $paramdescription
567 * @param string $indentation composed by space only
568 * @return string the html to diplay
570 public function rest_param_description_html($paramdescription, $paramstring) {
571 $brakeline = <<<EOF
574 EOF;
575 /// description object is a list
576 if ($paramdescription instanceof external_multiple_structure) {
577 $paramstring = $paramstring . '[0]';
578 $return = $this->rest_param_description_html($paramdescription->content, $paramstring);
579 return $return;
580 } else if ($paramdescription instanceof external_single_structure) {
581 /// description object is an object
582 $singlestructuredesc = "";
583 $initialparamstring = $paramstring;
584 foreach ($paramdescription->keys as $attributname => $attribut) {
585 $paramstring = $initialparamstring . '[' . $attributname . ']';
586 $singlestructuredesc .= $this->rest_param_description_html(
587 $paramdescription->keys[$attributname], $paramstring);
589 return $singlestructuredesc;
590 } else {
591 /// description object is a primary type (string, integer)
592 $paramstring = $paramstring . '=';
593 switch ($paramdescription->type) {
594 case PARAM_BOOL: // 0 or 1 only for now
595 case PARAM_INT:
596 $type = 'int';
597 break;
598 case PARAM_FLOAT;
599 $type = 'double';
600 break;
601 default:
602 $type = 'string';
604 return $paramstring . " " . $type . $brakeline;
609 * This display all the documentation
610 * @param array $functions contains all decription objects
611 * @param array $authparam keys contains 'tokenid'
612 * @param boolean $printableformat true if we want to display the documentation in a printable format
613 * @param array $activatedprotocol
614 * @param string $parenturl url of the calling page - needed for the print button url:
615 * '/admin/documentation.php' or '/webservice/wsdoc.php' (default)
616 * @return string the html to diplay
618 public function documentation_html($functions, $printableformat, $activatedprotocol,
619 $authparams, $parenturl = '/webservice/wsdoc.php') {
621 $documentationhtml = $this->output->heading(get_string('wsdocapi', 'webservice'));
623 $br = html_writer::empty_tag('br', array());
624 $brakeline = <<<EOF
627 EOF;
628 /// Some general information
629 $docinfo = new stdClass();
630 $docurl = new moodle_url('http://docs.moodle.org/dev/Creating_a_web_service_client');
631 $docinfo->doclink = html_writer::tag('a',
632 get_string('wsclientdoc', 'webservice'), array('href' => $docurl));
633 $documentationhtml .= html_writer::start_tag('table',
634 array('style' => "margin-left:auto; margin-right:auto;"));
635 $documentationhtml .= html_writer::start_tag('tr', array());
636 $documentationhtml .= html_writer::start_tag('td', array());
637 $documentationhtml .= get_string('wsdocumentationintro', 'webservice', $docinfo);
638 $documentationhtml .= $br . $br;
641 /// Print button
642 $authparams['print'] = true;
643 //$parameters = array ('token' => $token, 'wsusername' => $username, 'wspassword' => $password, 'print' => true);
644 $url = new moodle_url($parenturl, $authparams); // Required
645 $documentationhtml .= $this->output->single_button($url, get_string('print', 'webservice'));
646 $documentationhtml .= $br;
649 /// each functions will be displayed into a collapsible region
650 //(opened if printableformat = true)
651 foreach ($functions as $functionname => $description) {
653 if (empty($printableformat)) {
654 $documentationhtml .= print_collapsible_region_start('',
655 'aera_' . $functionname,
656 html_writer::start_tag('strong', array())
657 . $functionname . html_writer::end_tag('strong'),
658 false,
659 !$printableformat,
660 true);
661 } else {
662 $documentationhtml .= html_writer::tag('strong', $functionname);
663 $documentationhtml .= $br;
666 /// function global description
667 $documentationhtml .= $br;
668 $documentationhtml .= html_writer::start_tag('div',
669 array('style' => 'border:solid 1px #DEDEDE;background:#E2E0E0;
670 color:#222222;padding:4px;'));
671 $documentationhtml .= $description->description;
672 $documentationhtml .= html_writer::end_tag('div');
673 $documentationhtml .= $br . $br;
675 /// function arguments documentation
676 $documentationhtml .= html_writer::start_tag('span', array('style' => 'color:#EA33A6'));
677 $documentationhtml .= get_string('arguments', 'webservice');
678 $documentationhtml .= html_writer::end_tag('span');
679 $documentationhtml .= $br;
680 foreach ($description->parameters_desc->keys as $paramname => $paramdesc) {
681 /// a argument documentation
682 $documentationhtml .= html_writer::start_tag('span', array('style' => 'font-size:80%'));
684 if ($paramdesc->required == VALUE_REQUIRED) {
685 $required = get_string('required', 'webservice');
687 if ($paramdesc->required == VALUE_DEFAULT) {
688 if ($paramdesc->default === null) {
689 $default = "null";
690 } else {
691 $default = print_r($paramdesc->default, true);
693 $required = get_string('default', 'webservice', $default);
695 if ($paramdesc->required == VALUE_OPTIONAL) {
696 $required = get_string('optional', 'webservice');
699 $documentationhtml .= html_writer::start_tag('b', array());
700 $documentationhtml .= $paramname;
701 $documentationhtml .= html_writer::end_tag('b');
702 $documentationhtml .= " (" . $required . ")"; // argument is required or optional ?
703 $documentationhtml .= $br;
704 $documentationhtml .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
705 . $paramdesc->desc; // argument description
706 $documentationhtml .= $br . $br;
707 ///general structure of the argument
708 $documentationhtml .= $this->colored_box_with_pre_tag(
709 get_string('generalstructure', 'webservice'),
710 $this->detailed_description_html($paramdesc),
711 'FFF1BC');
712 ///xml-rpc structure of the argument in PHP format
713 if (!empty($activatedprotocol['xmlrpc'])) {
714 $documentationhtml .= $this->colored_box_with_pre_tag(
715 get_string('phpparam', 'webservice'),
716 htmlentities('[' . $paramname . '] =>'
717 . $this->xmlrpc_param_description_html($paramdesc)),
718 'DFEEE7');
720 ///POST format for the REST protocol for the argument
721 if (!empty($activatedprotocol['rest'])) {
722 $documentationhtml .= $this->colored_box_with_pre_tag(
723 get_string('restparam', 'webservice'),
724 htmlentities($this->rest_param_description_html(
725 $paramdesc, $paramname)),
726 'FEEBE5');
728 $documentationhtml .= html_writer::end_tag('span');
730 $documentationhtml .= $br . $br;
733 /// function response documentation
734 $documentationhtml .= html_writer::start_tag('span', array('style' => 'color:#EA33A6'));
735 $documentationhtml .= get_string('response', 'webservice');
736 $documentationhtml .= html_writer::end_tag('span');
737 $documentationhtml .= $br;
738 /// function response description
739 $documentationhtml .= html_writer::start_tag('span', array('style' => 'font-size:80%'));
740 if (!empty($description->returns_desc->desc)) {
741 $documentationhtml .= $description->returns_desc->desc;
742 $documentationhtml .= $br . $br;
744 if (!empty($description->returns_desc)) {
745 ///general structure of the response
746 $documentationhtml .= $this->colored_box_with_pre_tag(
747 get_string('generalstructure', 'webservice'),
748 $this->detailed_description_html($description->returns_desc),
749 'FFF1BC');
750 ///xml-rpc structure of the response in PHP format
751 if (!empty($activatedprotocol['xmlrpc'])) {
752 $documentationhtml .= $this->colored_box_with_pre_tag(
753 get_string('phpresponse', 'webservice'),
754 htmlentities($this->xmlrpc_param_description_html(
755 $description->returns_desc)),
756 'DFEEE7');
758 ///XML response for the REST protocol
759 if (!empty($activatedprotocol['rest'])) {
760 $restresponse = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
761 . $brakeline . "<RESPONSE>" . $brakeline;
762 $restresponse .= $this->description_in_indented_xml_format(
763 $description->returns_desc);
764 $restresponse .="</RESPONSE>" . $brakeline;
765 $documentationhtml .= $this->colored_box_with_pre_tag(
766 get_string('restcode', 'webservice'),
767 htmlentities($restresponse),
768 'FEEBE5');
771 $documentationhtml .= html_writer::end_tag('span');
772 $documentationhtml .= $br . $br;
774 /// function errors documentation for REST protocol
775 if (!empty($activatedprotocol['rest'])) {
776 $documentationhtml .= html_writer::start_tag('span', array('style' => 'color:#EA33A6'));
777 $documentationhtml .= get_string('errorcodes', 'webservice');
778 $documentationhtml .= html_writer::end_tag('span');
779 $documentationhtml .= $br . $br;
780 $documentationhtml .= html_writer::start_tag('span', array('style' => 'font-size:80%'));
781 $errormessage = get_string('invalidparameter', 'debug');
782 $restexceptiontext = <<<EOF
783 <?xml version="1.0" encoding="UTF-8"?>
784 <EXCEPTION class="invalid_parameter_exception">
785 <MESSAGE>{$errormessage}</MESSAGE>
786 <DEBUGINFO></DEBUGINFO>
787 </EXCEPTION>
788 EOF;
789 $documentationhtml .= $this->colored_box_with_pre_tag(
790 get_string('restexception', 'webservice'),
791 htmlentities($restexceptiontext),
792 'FEEBE5');
794 $documentationhtml .= html_writer::end_tag('span');
796 $documentationhtml .= $br . $br;
797 if (empty($printableformat)) {
798 $documentationhtml .= print_collapsible_region_end(true);
802 /// close the table and return the documentation
803 $documentationhtml .= html_writer::end_tag('td');
804 $documentationhtml .= html_writer::end_tag('tr');
805 $documentationhtml .= html_writer::end_tag('table');
807 return $documentationhtml;