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