2 ///////////////////////////////////////////////////////////////////////////
4 // This file is part of Moodle - http://moodle.org/ //
5 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // Moodle is free software: you can redistribute it and/or modify //
8 // it under the terms of the GNU General Public License as published by //
9 // the Free Software Foundation, either version 3 of the License, or //
10 // (at your option) any later version. //
12 // Moodle is distributed in the hope that it will be useful, //
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
15 // GNU General Public License for more details. //
17 // You should have received a copy of the GNU General Public License //
18 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. //
20 ///////////////////////////////////////////////////////////////////////////
23 * Web service documentation renderer.
25 * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
26 * @author Jerome Mouneyrac
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 class core_webservice_renderer
extends plugin_renderer_base
{
33 * Display Reset token confirmation box
34 * @param object $token to reset
37 public function user_reset_token_confirmation($token) {
39 $managetokenurl = $CFG->wwwroot
."/user/managetoken.php?sesskey=" . sesskey();
40 $optionsyes = array('tokenid'=>$token->id
, 'action'=>'resetwstoken', 'confirm'=>1, 'sesskey'=>sesskey());
41 $optionsno = array('section'=>'webservicetokens', 'sesskey'=>sesskey());
42 $formcontinue = new single_button(new moodle_url($managetokenurl, $optionsyes), get_string('reset'));
43 $formcancel = new single_button(new moodle_url($managetokenurl, $optionsno), get_string('cancel'), 'get');
44 $html = $OUTPUT->confirm(get_string('resettokenconfirm', 'webservice',
45 (object)array('user'=>$token->firstname
." ".$token->lastname
, 'service'=>$token->name
)),
46 $formcontinue, $formcancel);
53 * Display user tokens with buttons to reset them
54 * @param object $tokens
56 * @return string html code
58 public function user_webservice_tokens_box($tokens, $userid) {
62 $stroperation = get_string('operation', 'webservice');
63 $strtoken = get_string('key', 'webservice');
64 $strservice = get_string('service', 'webservice');
65 $strcreator = get_string('tokencreator', 'webservice');
66 $strcontext = get_string('context', 'webservice');
67 $strvaliduntil = get_string('validuntil', 'webservice');
69 $return = $OUTPUT->heading(get_string('securitykeys', 'webservice'), 3, 'main', true);
70 $return .= $OUTPUT->box_start('generalbox webservicestokenui');
72 $return .= get_string('keyshelp', 'webservice');
74 $table = new html_table();
75 $table->head
= array($strtoken, $strservice, $strvaliduntil, $strcreator, $stroperation);
76 $table->align
= array('left', 'left', 'left', 'center', 'left', 'center');
77 $table->width
= '100%';
78 $table->data
= array();
80 if (!empty($tokens)) {
81 foreach ($tokens as $token) {
82 //TODO: retrieve context
84 if ($token->creatorid
== $userid) {
85 $reset = "<a href=\"".$CFG->wwwroot
."/user/managetoken.php?sesskey=".sesskey().
86 "&action=resetwstoken&tokenid=".$token->id
."\">";
87 $reset .= get_string('reset')."</a>";
88 $creator = $token->firstname
." ".$token->lastname
;
90 //retrive administrator name
91 require_once($CFG->dirroot
.'/user/lib.php');
92 $creators = user_get_users_by_id(array($token->creatorid
));
93 $admincreator = $creators[$token->creatorid
];
94 $creator = $admincreator->firstname
." ".$admincreator->lastname
;
98 $userprofilurl = new moodle_url('/user/view.php?id='.$token->creatorid
);
99 $creatoratag = html_writer
::start_tag('a', array('href' => $userprofilurl));
100 $creatoratag .= $creator;
101 $creatoratag .= html_writer
::end_tag('a');
104 if (!empty($token->validuntil
)) {
105 $validuntil = date("F j, Y"); //TODO: language support (look for moodle function)
108 $table->data
[] = array($token->token
, $token->name
, $validuntil, $creatoratag, $reset);
110 $return .= html_writer
::table($table);
113 $return .= get_string('notoken', 'webservice');
116 $return .= $OUTPUT->box_end();
123 * Return documentation for a ws description object
124 * ws description object can be 'external_multiple_structure', 'external_single_structure' or 'external_value'
125 * Example of documentation for moodle_group_create_groups function:
128 courseid int //id of course
129 name string //multilang compatible name, course unique
130 description string //group description text
131 enrolmentkey string //group enrol secret phrase
134 * @param object $params a part of parameter/return description
135 * @return string the html to display
137 public function detailed_description_html($params) {
138 /// retrieve the description of the description object
140 if (!empty($params->desc
)) {
141 $paramdesc .= html_writer
::start_tag('span', array('style' => "color:#2A33A6"));
142 if ($params->required
== VALUE_REQUIRED
) {
145 if ($params->required
== VALUE_DEFAULT
) {
146 if ($params->default === null) {
147 $params->default = "null";
149 $required = html_writer
::start_tag('b', array()).get_string('default', 'webservice', $params->default).html_writer
::end_tag('b');
151 if ($params->required
== VALUE_OPTIONAL
) {
152 $required = html_writer
::start_tag('b', array()).get_string('optional', 'webservice').html_writer
::end_tag('b');
154 $paramdesc .= " ".$required." ";
155 $paramdesc .= html_writer
::start_tag('i', array());
158 $paramdesc .= $params->desc
;
160 $paramdesc .= html_writer
::end_tag('i');
162 $paramdesc .= html_writer
::end_tag('span');
163 $paramdesc .= html_writer
::empty_tag('br', array());
166 /// description object is a list
167 if ($params instanceof external_multiple_structure
) {
168 return $paramdesc . "list of ( " . html_writer
::empty_tag('br', array()) . $this->detailed_description_html($params->content
) . ")";
169 } else if ($params instanceof external_single_structure
) {
170 /// description object is an object
171 $singlestructuredesc = $paramdesc."object {". html_writer
::empty_tag('br', array());
172 foreach ($params->keys
as $attributname => $attribut) {
173 $singlestructuredesc .= html_writer
::start_tag('b', array());
174 $singlestructuredesc .= $attributname;
175 $singlestructuredesc .= html_writer
::end_tag('b');
176 $singlestructuredesc .= " ".$this->detailed_description_html($params->keys
[$attributname]);
178 $singlestructuredesc .= "} ";
179 $singlestructuredesc .= html_writer
::empty_tag('br', array());
180 return $singlestructuredesc;
182 /// description object is a primary type (string, integer)
183 switch($params->type
) {
184 case PARAM_BOOL
: // 0 or 1 only for now
194 return $type." ".$paramdesc;
199 * Return a description object in indented xml format (for REST response)
200 * It is indented in order to be displayed into <pre> tag
201 * @param object $returndescription
202 * @param string $indentation composed by space only
203 * @return string the html to diplay
205 public function description_in_indented_xml_format($returndescription, $indentation = "") {
206 $indentation = $indentation . " ";
211 /// description object is a list
212 if ($returndescription instanceof external_multiple_structure
) {
213 $return = $indentation."<MULTIPLE>".$brakeline;
214 $return .= $this->description_in_indented_xml_format($returndescription->content
, $indentation);
215 $return .= $indentation."</MULTIPLE>".$brakeline;
217 } else if ($returndescription instanceof external_single_structure
) {
218 /// description object is an object
219 $singlestructuredesc = $indentation."<SINGLE>".$brakeline;
220 $keyindentation = $indentation." ";
221 foreach ($returndescription->keys
as $attributname => $attribut) {
222 $singlestructuredesc .= $keyindentation."<KEY name=\"".$attributname."\">".$brakeline.
223 $this->description_in_indented_xml_format($returndescription->keys
[$attributname], $keyindentation).
224 $keyindentation."</KEY>".$brakeline;
226 $singlestructuredesc .= $indentation."</SINGLE>".$brakeline;
227 return $singlestructuredesc;
229 /// description object is a primary type (string, integer)
230 switch($returndescription->type
) {
231 case PARAM_BOOL
: // 0 or 1 only for now
241 return $indentation."<VALUE>".$type."</VALUE>".$brakeline;
246 * Create indented XML-RPC param description
247 * @param object $paramdescription
248 * @param string $indentation composed by space only
249 * @return string the html to diplay
251 public function xmlrpc_param_description_html($paramdescription, $indentation = "") {
252 $indentation = $indentation . " ";
257 /// description object is a list
258 if ($paramdescription instanceof external_multiple_structure
) {
259 $return = $brakeline.$indentation."Array ";
260 $indentation = $indentation . " ";
261 $return .= $brakeline.$indentation."(";
262 $return .= $brakeline.$indentation."[0] =>";
263 $return .= $this->xmlrpc_param_description_html($paramdescription->content
, $indentation);
264 $return .= $brakeline.$indentation.")";
266 } else if ($paramdescription instanceof external_single_structure
) {
267 /// description object is an object
268 $singlestructuredesc = $brakeline.$indentation."Array ";
269 $keyindentation = $indentation." ";
270 $singlestructuredesc .= $brakeline.$keyindentation."(";
271 foreach ($paramdescription->keys
as $attributname => $attribut) {
272 $singlestructuredesc .= $brakeline.$keyindentation."[".$attributname."] =>".
273 $this->xmlrpc_param_description_html($paramdescription->keys
[$attributname], $keyindentation).
276 $singlestructuredesc .= $brakeline.$keyindentation.")";
277 return $singlestructuredesc;
279 /// description object is a primary type (string, integer)
280 switch($paramdescription->type
) {
281 case PARAM_BOOL
: // 0 or 1 only for now
296 * Return the html of a colored box with content
297 * @param string $title - the title of the box
298 * @param string $content - the content to displayed
299 * @param string $rgb - the background color of the box
302 public function colored_box_with_pre_tag($title, $content, $rgb = 'FEEBE5') {
303 $coloredbox = html_writer
::start_tag('ins', array()); //TODO: this tag removes xhtml strict error but cause warning
304 $coloredbox .= html_writer
::start_tag('div', array('style' => "border:solid 1px #DEDEDE;background:#".$rgb.";color:#222222;padding:4px;"));
305 $coloredbox .= html_writer
::start_tag('pre', array());
306 $coloredbox .= html_writer
::start_tag('b', array());
307 $coloredbox .= $title;
308 $coloredbox .= html_writer
::end_tag('b', array());
309 $coloredbox .= html_writer
::empty_tag('br', array());
310 $coloredbox .= "\n".$content."\n";
311 $coloredbox .= html_writer
::end_tag('pre', array());
312 $coloredbox .= html_writer
::end_tag('div', array());
313 $coloredbox .= html_writer
::end_tag('ins', array());
319 * Return indented REST param description
320 * @param object $paramdescription
321 * @param string $indentation composed by space only
322 * @return string the html to diplay
324 public function rest_param_description_html($paramdescription, $paramstring) {
329 /// description object is a list
330 if ($paramdescription instanceof external_multiple_structure
) {
331 $paramstring = $paramstring.'[0]';
332 $return = $this->rest_param_description_html($paramdescription->content
, $paramstring);
334 } else if ($paramdescription instanceof external_single_structure
) {
335 /// description object is an object
336 $singlestructuredesc = "";
337 $initialparamstring = $paramstring;
338 foreach ($paramdescription->keys
as $attributname => $attribut) {
339 $paramstring = $initialparamstring.'['.$attributname.']';
340 $singlestructuredesc .= $this->rest_param_description_html($paramdescription->keys
[$attributname], $paramstring);
342 return $singlestructuredesc;
344 /// description object is a primary type (string, integer)
345 $paramstring = $paramstring.'=';
346 switch($paramdescription->type
) {
347 case PARAM_BOOL
: // 0 or 1 only for now
357 return $paramstring." ".$type.$brakeline;
363 * This display all the documentation
364 * @param array $functions contains all decription objects
365 * @param array $authparam keys are either 'username'/'password' or 'token'
366 * @param boolean $printableformat true if we want to display the documentation in a printable format
367 * @param array $activatedprotocol
368 * @return string the html to diplay
370 public function documentation_html($functions, $printableformat, $activatedprotocol, $authparams) {
371 global $OUTPUT, $CFG;
372 $br = html_writer
::empty_tag('br', array());
377 /// Some general information
378 $documentationhtml = html_writer
::start_tag('table', array('style' => "margin-left:auto; margin-right:auto;"));
379 $documentationhtml .= html_writer
::start_tag('tr', array());
380 $documentationhtml .= html_writer
::start_tag('td', array());
381 $documentationhtml .= get_string('wsdocumentationintro', 'webservice', $authparams['wsusername']);
382 $documentationhtml .= $br.$br;
386 $authparams['print'] = true;
387 //$parameters = array ('token' => $token, 'wsusername' => $username, 'wspassword' => $password, 'print' => true);
388 $url = new moodle_url('/webservice/wsdoc.php', $authparams); // Required
389 $documentationhtml .= $OUTPUT->single_button($url, get_string('print','webservice'));
390 $documentationhtml .= $br;
393 /// each functions will be displayed into a collapsible region (opened if printableformat = true)
394 foreach ($functions as $functionname => $description) {
396 if (empty($printableformat)) {
397 $documentationhtml .= print_collapsible_region_start('',
398 'aera_'.$functionname,
399 html_writer
::start_tag('strong', array()).$functionname.html_writer
::end_tag('strong'),
404 $documentationhtml .= html_writer
::tag('strong', $functionname);
405 $documentationhtml .= $br;
408 /// function global description
409 $documentationhtml .= $br;
410 $documentationhtml .= html_writer
::start_tag('div', array('style' => 'border:solid 1px #DEDEDE;background:#E2E0E0;color:#222222;padding:4px;'));
411 $documentationhtml .= $description->description
;
412 $documentationhtml .= html_writer
::end_tag('div');
413 $documentationhtml .= $br.$br;
415 /// function arguments documentation
416 $documentationhtml .= html_writer
::start_tag('span', array('style' => 'color:#EA33A6'));
417 $documentationhtml .= get_string('arguments', 'webservice');
418 $documentationhtml .= html_writer
::end_tag('span');
419 $documentationhtml .= $br;
420 foreach ($description->parameters_desc
->keys
as $paramname => $paramdesc) {
421 /// a argument documentation
422 $documentationhtml .= html_writer
::start_tag('span', array('style' => 'font-size:80%'));
424 if ($paramdesc->required
== VALUE_REQUIRED
) {
425 $required = get_string('required', 'webservice');
427 if ($paramdesc->required
== VALUE_DEFAULT
) {
428 if ($paramdesc->default === null) {
431 $default = $paramdesc->default;
433 $required = get_string('default', 'webservice', $default);
435 if ($paramdesc->required
== VALUE_OPTIONAL
) {
436 $required = get_string('optional', 'webservice');
439 $documentationhtml .= html_writer
::start_tag('b', array());
440 $documentationhtml .= $paramname;
441 $documentationhtml .= html_writer
::end_tag('b');
442 $documentationhtml .= " (" .$required. ")"; // argument is required or optional ?
443 $documentationhtml .= $br;
444 $documentationhtml .= " ".$paramdesc->desc
; // argument description
445 $documentationhtml .= $br.$br;
446 ///general structure of the argument
447 $documentationhtml .= $this->colored_box_with_pre_tag(get_string('generalstructure', 'webservice'),
448 $this->detailed_description_html($paramdesc),
450 ///xml-rpc structure of the argument in PHP format
451 if (!empty($activatedprotocol['xmlrpc'])) {
452 $documentationhtml .= $this->colored_box_with_pre_tag(get_string('phpparam', 'webservice'),
453 htmlentities('['.$paramname.'] =>'.$this->xmlrpc_param_description_html($paramdesc)),
456 ///POST format for the REST protocol for the argument
457 if (!empty($activatedprotocol['rest'])) {
458 $documentationhtml .= $this->colored_box_with_pre_tag(get_string('restparam', 'webservice'),
459 htmlentities($this->rest_param_description_html($paramdesc,$paramname)),
462 $documentationhtml .= html_writer
::end_tag('span');
464 $documentationhtml .= $br.$br;
467 /// function response documentation
468 $documentationhtml .= html_writer
::start_tag('span', array('style' => 'color:#EA33A6'));
469 $documentationhtml .= get_string('response', 'webservice');
470 $documentationhtml .= html_writer
::end_tag('span');
471 $documentationhtml .= $br;
472 /// function response description
473 $documentationhtml .= html_writer
::start_tag('span', array('style' => 'font-size:80%'));
474 if (!empty($description->returns_desc
->desc
)) {
475 $documentationhtml .= $description->returns_desc
->desc
;
476 $documentationhtml .= $br.$br;
478 if (!empty($description->returns_desc
)) {
479 ///general structure of the response
480 $documentationhtml .= $this->colored_box_with_pre_tag(get_string('generalstructure', 'webservice'),
481 $this->detailed_description_html($description->returns_desc
),
483 ///xml-rpc structure of the response in PHP format
484 if (!empty($activatedprotocol['xmlrpc'])) {
485 $documentationhtml .= $this->colored_box_with_pre_tag(get_string('phpresponse', 'webservice'),
486 htmlentities($this->xmlrpc_param_description_html($description->returns_desc
)),
489 ///XML response for the REST protocol
490 if (!empty($activatedprotocol['rest'])) {
491 $restresponse = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>".$brakeline."<RESPONSE>".$brakeline;
492 $restresponse .= $this->description_in_indented_xml_format($description->returns_desc
);
493 $restresponse .="</RESPONSE>".$brakeline;
494 $documentationhtml .= $this->colored_box_with_pre_tag(get_string('restcode', 'webservice'),
495 htmlentities($restresponse),
499 $documentationhtml .= html_writer
::end_tag('span');
500 $documentationhtml .= $br.$br;
502 /// function errors documentation for REST protocol
503 if (!empty($activatedprotocol['rest'])) {
504 $documentationhtml .= html_writer
::start_tag('span', array('style' => 'color:#EA33A6'));
505 $documentationhtml .= get_string('errorcodes', 'webservice');
506 $documentationhtml .= html_writer
::end_tag('span');
507 $documentationhtml .= $br.$br;
508 $documentationhtml .= html_writer
::start_tag('span', array('style' => 'font-size:80%'));
509 $errormessage = get_string('invalidparameter', 'debug');
510 $restexceptiontext =<<<EOF
511 <?xml version="1.0" encoding="UTF-8"?>
512 <EXCEPTION class="invalid_parameter_exception">
513 <MESSAGE>{$errormessage}</MESSAGE>
514 <DEBUGINFO></DEBUGINFO>
517 $documentationhtml .= $this->colored_box_with_pre_tag(get_string('restexception', 'webservice'),
518 htmlentities($restexceptiontext),
521 $documentationhtml .= html_writer
::end_tag('span');
523 $documentationhtml .= $br.$br;
524 if (empty($printableformat)) {
525 $documentationhtml .= print_collapsible_region_end(true);
529 /// close the table and return the documentation
530 $documentationhtml .= html_writer
::end_tag('td');
531 $documentationhtml .= html_writer
::end_tag('tr');
532 $documentationhtml .= html_writer
::end_tag('table');
534 return $documentationhtml;
539 * Return the login page html
540 * @param string $errormessage - the error message to display
541 * @return string the html to diplay
543 public function login_page_html($errormessage) {
544 global $CFG, $OUTPUT;
546 $br = html_writer
::empty_tag('br', array());
548 $htmlloginpage = html_writer
::start_tag('table', array('style' => "margin-left:auto; margin-right:auto;"));
549 $htmlloginpage .= html_writer
::start_tag('tr', array());
550 $htmlloginpage .= html_writer
::start_tag('td', array());
552 // /// Display detailed error message when can't login
553 // $htmlloginpage .= get_string('error','webservice',$errormessage);
554 // $htmlloginpage .= html_writer::empty_tag('br', array());
555 // $htmlloginpage .= html_writer::empty_tag('br', array());
557 //login form - we cannot use moodle form as we don't have sessionkey
558 $target = new moodle_url('/webservice/wsdoc.php', array()); // Required
560 $contents = get_string('entertoken', 'webservice');
561 $contents .= $br.$br;
562 $contents .= html_writer
::empty_tag('input', array('type'=>'text', 'name'=>'token', 'style'=>'width: 30em;'));
564 $contents .= $br.$br;
565 $contents .= get_string('wsdocumentationlogin', 'webservice');
566 $contents .= $br.$br;
567 $contents .= html_writer
::empty_tag('input', array('type'=>'text', 'name'=>'wsusername', 'style'=>'width: 30em;', 'value'=>get_string('wsusername', 'webservice')));
568 $contents .= $br.$br;
569 $contents .= html_writer
::empty_tag('input', array('type'=>'text', 'name'=>'wspassword', 'style'=>'width: 30em;', 'value'=>get_string('wspassword', 'webservice')));
570 $contents .= $br.$br;
571 $contents .= html_writer
::empty_tag('input', array('type'=>'submit', 'name'=>'submit', 'value'=>get_string('wsdocumentation', 'webservice')));
573 $htmlloginpage .= html_writer
::tag('form', "<div>$contents</div>", array('method'=>'post', 'target'=>$target));
575 $htmlloginpage .= html_writer
::end_tag('td');
576 $htmlloginpage .= html_writer
::end_tag('tr');
577 $htmlloginpage .= html_writer
::end_tag('table');
579 return $htmlloginpage;