PageRenderTime 23ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/tool/behat/renderer.php

https://gitlab.com/JrLucena/moodle
PHP | 194 lines | 102 code | 34 blank | 58 comment | 3 complexity | 0d0a9824df4c810bcc4efcefb77d6b46 MD5 | raw file
  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. * Behat tool renderer
  18. *
  19. * @package tool_behat
  20. * @copyright 2012 David MonllaĆ³
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. global $CFG;
  25. require_once($CFG->libdir . '/behat/classes/behat_selectors.php');
  26. /**
  27. * Renderer for behat tool web features
  28. *
  29. * @package tool_behat
  30. * @copyright 2012 David MonllaĆ³
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class tool_behat_renderer extends plugin_renderer_base {
  34. /**
  35. * Renders the list of available steps according to the submitted filters.
  36. *
  37. * @param mixed $stepsdefinitions Available steps array.
  38. * @param moodleform $form
  39. * @return string HTML code
  40. */
  41. public function render_stepsdefinitions($stepsdefinitions, $form) {
  42. $html = $this->generic_info();
  43. // Form.
  44. ob_start();
  45. $form->display();
  46. $html .= ob_get_contents();
  47. ob_end_clean();
  48. if (empty($stepsdefinitions)) {
  49. $stepsdefinitions = get_string('nostepsdefinitions', 'tool_behat');
  50. } else {
  51. $stepsdefinitions = implode('', $stepsdefinitions);
  52. // Replace text selector type arguments with a user-friendly select.
  53. $stepsdefinitions = preg_replace_callback('/(TEXT_SELECTOR\d?_STRING)/',
  54. function ($matches) {
  55. return html_writer::select(behat_selectors::get_allowed_text_selectors(), uniqid());
  56. },
  57. $stepsdefinitions
  58. );
  59. // Replace selector type arguments with a user-friendly select.
  60. $stepsdefinitions = preg_replace_callback('/(SELECTOR\d?_STRING)/',
  61. function ($matches) {
  62. return html_writer::select(behat_selectors::get_allowed_selectors(), uniqid());
  63. },
  64. $stepsdefinitions
  65. );
  66. // Replace simple OR options.
  67. $regex = '#\(\?P<[^>]+>([^\)|]+\|[^\)]+)\)#';
  68. $stepsdefinitions = preg_replace_callback($regex,
  69. function($matches){
  70. return html_writer::select(explode('|', $matches[1]), uniqid());
  71. },
  72. $stepsdefinitions
  73. );
  74. $stepsdefinitions = preg_replace_callback('/(FIELD_VALUE_STRING)/',
  75. function ($matches) {
  76. global $CFG;
  77. // Creating a link to a popup with the help.
  78. $url = new moodle_url(
  79. '/help.php',
  80. array(
  81. 'component' => 'tool_behat',
  82. 'identifier' => 'fieldvalueargument',
  83. 'lang' => current_language()
  84. )
  85. );
  86. // Note: this title is displayed only if JS is disabled,
  87. // otherwise the link will have the new ajax tooltip.
  88. $title = get_string('fieldvalueargument', 'tool_behat');
  89. $title = get_string('helpprefix2', '', trim($title, ". \t"));
  90. $attributes = array('href' => $url, 'title' => $title,
  91. 'aria-haspopup' => 'true', 'target' => '_blank');
  92. $output = html_writer::tag('a', 'FIELD_VALUE_STRING', $attributes);
  93. return html_writer::tag('span', $output, array('class' => 'helptooltip'));
  94. },
  95. $stepsdefinitions
  96. );
  97. }
  98. // Steps definitions.
  99. $html .= html_writer::tag('div', $stepsdefinitions, array('class' => 'steps-definitions'));
  100. $html .= $this->output->footer();
  101. return $html;
  102. }
  103. /**
  104. * Renders an error message adding the generic info about the tool purpose and setup.
  105. *
  106. * @param string $msg The error message
  107. * @return string HTML
  108. */
  109. public function render_error($msg) {
  110. $html = $this->generic_info();
  111. $a = new stdClass();
  112. $a->errormsg = $msg;
  113. $a->behatcommand = behat_command::get_behat_command();
  114. $a->behatinit = 'php admin' . DIRECTORY_SEPARATOR . 'tool' . DIRECTORY_SEPARATOR .
  115. 'behat' . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'init.php';
  116. $msg = get_string('wrongbehatsetup', 'tool_behat', $a);
  117. // Error box including generic error string + specific error msg.
  118. $html .= $this->output->box_start('box errorbox');
  119. $html .= html_writer::tag('div', $msg);
  120. $html .= $this->output->box_end();
  121. $html .= $this->output->footer();
  122. return $html;
  123. }
  124. /**
  125. * Generic info about the tool.
  126. *
  127. * @return string
  128. */
  129. protected function generic_info() {
  130. $title = get_string('pluginname', 'tool_behat');
  131. // Header.
  132. $html = $this->output->header();
  133. $html .= $this->output->heading($title);
  134. // Info.
  135. $installurl = behat_command::DOCS_URL . '#Installation';
  136. $installlink = html_writer::tag('a', $installurl, array('href' => $installurl, 'target' => '_blank'));
  137. $writetestsurl = behat_command::DOCS_URL . '#Writing_features';
  138. $writetestslink = html_writer::tag('a', $writetestsurl, array('href' => $writetestsurl, 'target' => '_blank'));
  139. $writestepsurl = behat_command::DOCS_URL . '#Adding_steps_definitions';
  140. $writestepslink = html_writer::tag('a', $writestepsurl, array('href' => $writestepsurl, 'target' => '_blank'));
  141. $infos = array(
  142. get_string('installinfo', 'tool_behat', $installlink),
  143. get_string('newtestsinfo', 'tool_behat', $writetestslink),
  144. get_string('newstepsinfo', 'tool_behat', $writestepslink)
  145. );
  146. // List of steps.
  147. $html .= $this->output->box_start();
  148. $html .= html_writer::tag('h3', get_string('infoheading', 'tool_behat'));
  149. $html .= html_writer::tag('div', get_string('aim', 'tool_behat'));
  150. $html .= html_writer::start_tag('div');
  151. $html .= html_writer::start_tag('ul');
  152. $html .= html_writer::start_tag('li');
  153. $html .= implode(html_writer::end_tag('li') . html_writer::start_tag('li'), $infos);
  154. $html .= html_writer::end_tag('li');
  155. $html .= html_writer::end_tag('ul');
  156. $html .= html_writer::end_tag('div');
  157. $html .= $this->output->box_end();
  158. return $html;
  159. }
  160. }