renderer.php 39 KB

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