2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
21 * @copyright 2012 David Monllaó
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->libdir . '/behat/classes/behat_selectors.php');
31 * Renderer for behat tool web features
34 * @copyright 2012 David Monllaó
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class tool_behat_renderer extends plugin_renderer_base {
40 * Renders the list of available steps according to the submitted filters.
42 * @param mixed $stepsdefinitions Available steps array.
43 * @param moodleform $form
44 * @return string HTML code
46 public function render_stepsdefinitions($stepsdefinitions, $form) {
48 $html = $this->generic_info();
53 $html .= ob_get_contents();
56 if (empty($stepsdefinitions)) {
57 $stepsdefinitions = get_string('nostepsdefinitions', 'tool_behat');
60 $stepsdefinitions = implode('', $stepsdefinitions);
62 // Replace text selector type arguments with a user-friendly select.
63 $stepsdefinitions = preg_replace_callback('/(TEXT_SELECTOR\d?_STRING)/',
65 return html_writer::select(behat_selectors::get_allowed_text_selectors(), uniqid());
70 // Replace selector type arguments with a user-friendly select.
71 $stepsdefinitions = preg_replace_callback('/(SELECTOR\d?_STRING)/',
73 return html_writer::select(behat_selectors::get_allowed_selectors(), uniqid());
78 // Replace simple OR options.
79 $regex = '#\(\?P<[^>]+>([^\)|]+\|[^\)]+)\)#';
80 $stepsdefinitions = preg_replace_callback($regex,
82 return html_writer::select(explode('|', $matches[1]), uniqid());
87 $stepsdefinitions = preg_replace_callback('/(FIELD_VALUE_STRING)/',
91 // Creating a link to a popup with the help.
92 $url = new moodle_url(
95 'component' => 'tool_behat',
96 'identifier' => 'fieldvalueargument',
97 'lang' => current_language()
101 // Note: this title is displayed only if JS is disabled,
102 // otherwise the link will have the new ajax tooltip.
103 $title = get_string('fieldvalueargument', 'tool_behat');
104 $title = get_string('helpprefix2', '', trim($title, ". \t"));
106 $attributes = array('href' => $url, 'title' => $title,
107 'aria-haspopup' => 'true', 'target' => '_blank');
109 $output = html_writer::tag('a', 'FIELD_VALUE_STRING', $attributes);
110 return html_writer::tag('span', $output, array('class' => 'helptooltip'));
116 // Steps definitions.
117 $html .= html_writer::tag('div', $stepsdefinitions, array('class' => 'steps-definitions'));
119 $html .= $this->output->footer();
125 * Renders an error message adding the generic info about the tool purpose and setup.
127 * @param string $msg The error message
128 * @return string HTML
130 public function render_error($msg) {
132 $html = $this->generic_info();
136 $a->behatcommand = behat_command::get_behat_command();
137 $a->behatinit = 'php admin' . DIRECTORY_SEPARATOR . 'tool' . DIRECTORY_SEPARATOR .
138 'behat' . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'init.php';
140 $msg = get_string('wrongbehatsetup', 'tool_behat', $a);
142 // Error box including generic error string + specific error msg.
143 $html .= $this->output->box_start('box errorbox');
144 $html .= html_writer::tag('div', $msg);
145 $html .= $this->output->box_end();
147 $html .= $this->output->footer();
153 * Generic info about the tool.
157 protected function generic_info() {
159 $title = get_string('pluginname', 'tool_behat');
162 $html = $this->output->header();
163 $html .= $this->output->heading($title);
166 $installurl = behat_command::DOCS_URL . '#Installation';
167 $installlink = html_writer::tag('a', $installurl, array('href' => $installurl, 'target' => '_blank'));
168 $writetestsurl = behat_command::DOCS_URL . '#Writting_features';
169 $writetestslink = html_writer::tag('a', $writetestsurl, array('href' => $writetestsurl, 'target' => '_blank'));
170 $writestepsurl = behat_command::DOCS_URL . '#Adding_steps_definitions';
171 $writestepslink = html_writer::tag('a', $writestepsurl, array('href' => $writestepsurl, 'target' => '_blank'));
173 get_string('installinfo', 'tool_behat', $installlink),
174 get_string('newtestsinfo', 'tool_behat', $writetestslink),
175 get_string('newstepsinfo', 'tool_behat', $writestepslink)
179 $html .= $this->output->box_start();
180 $html .= html_writer::tag('h1', get_string('infoheading', 'tool_behat'));
181 $html .= html_writer::tag('div', get_string('aim', 'tool_behat'));
182 $html .= html_writer::empty_tag('div');
183 $html .= html_writer::empty_tag('ul');
184 $html .= html_writer::empty_tag('li');
185 $html .= implode(html_writer::end_tag('li') . html_writer::empty_tag('li'), $infos);
186 $html .= html_writer::end_tag('li');
187 $html .= html_writer::end_tag('ul');
188 $html .= html_writer::end_tag('div');
189 $html .= $this->output->box_end();