MDL-68549 Drag-drop into text: minor glitches caused by MDL-68113
[moodle.git] / admin / tool / behat / renderer.php
blob864dc53fc3d9dde8794f754b7f554804e6375815
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.
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/>.
17 /**
18 * Behat tool renderer
20 * @package tool_behat
21 * @copyright 2012 David MonllaĆ³
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Renderer for behat tool web features
30 * @package tool_behat
31 * @copyright 2012 David MonllaĆ³
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class tool_behat_renderer extends plugin_renderer_base {
36 /**
37 * Renders the list of available steps according to the submitted filters.
39 * @param mixed $stepsdefinitions Available steps array.
40 * @param moodleform $form
41 * @return string HTML code
43 public function render_stepsdefinitions($stepsdefinitions, $form) {
44 global $CFG;
45 require_once($CFG->libdir . '/behat/classes/behat_selectors.php');
47 $html = $this->output->header();
48 $html .= $this->output->heading(get_string('pluginname', 'tool_behat'));
49 $html .= $form->render();
51 if (empty($stepsdefinitions)) {
52 $stepsdefinitions = get_string('nostepsdefinitions', 'tool_behat');
53 } else {
55 $stepsdefinitions = implode('', $stepsdefinitions);
57 // Replace text selector type arguments with a user-friendly select.
58 $stepsdefinitions = preg_replace_callback('/(TEXT_SELECTOR\d?_STRING)/',
59 function ($matches) {
60 return html_writer::select(behat_selectors::get_allowed_text_selectors(), uniqid());
62 $stepsdefinitions
65 // Replace selector type arguments with a user-friendly select.
66 $stepsdefinitions = preg_replace_callback('/(SELECTOR\d?_STRING)/',
67 function ($matches) {
68 return html_writer::select(behat_selectors::get_allowed_selectors(), uniqid());
70 $stepsdefinitions
73 // Replace simple OR options.
74 $regex = '#\(\?P<[^>]+>([^\)|]+\|[^\)]+)\)#';
75 $stepsdefinitions = preg_replace_callback($regex,
76 function($matches){
77 return html_writer::select(explode('|', $matches[1]), uniqid());
79 $stepsdefinitions
82 $stepsdefinitions = preg_replace_callback('/(FIELD_VALUE_STRING)/',
83 function ($matches) {
84 global $CFG;
86 // Creating a link to a popup with the help.
87 $url = new moodle_url(
88 '/help.php',
89 array(
90 'component' => 'tool_behat',
91 'identifier' => 'fieldvalueargument',
92 'lang' => current_language()
96 // Note: this title is displayed only if JS is disabled,
97 // otherwise the link will have the new ajax tooltip.
98 $title = get_string('fieldvalueargument', 'tool_behat');
99 $title = get_string('helpprefix2', '', trim($title, ". \t"));
101 $attributes = array('href' => $url, 'title' => $title,
102 'aria-haspopup' => 'true', 'target' => '_blank');
104 $output = html_writer::tag('a', 'FIELD_VALUE_STRING', $attributes);
105 return html_writer::tag('span', $output, array('class' => 'helptooltip'));
107 $stepsdefinitions
111 // Steps definitions.
112 $html .= html_writer::tag('div', $stepsdefinitions, array('class' => 'steps-definitions'));
114 $html .= $this->output->footer();
116 return $html;
120 * Renders an error message adding the generic info about the tool purpose and setup.
122 * @param string $msg The error message
123 * @return string HTML
125 public function render_error($msg) {
127 $html = $this->output->header();
128 $html .= $this->output->heading(get_string('pluginname', 'tool_behat'));
129 $html .= $this->generic_info();
131 $a = new stdClass();
132 $a->errormsg = $msg;
133 $a->behatcommand = behat_command::get_behat_command();
134 $a->behatinit = 'php admin' . DIRECTORY_SEPARATOR . 'tool' . DIRECTORY_SEPARATOR .
135 'behat' . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'init.php';
137 $msg = get_string('wrongbehatsetup', 'tool_behat', $a);
139 // Error box including generic error string + specific error msg.
140 $html .= $this->output->box_start('box errorbox alert alert-danger');
141 $html .= html_writer::tag('div', $msg);
142 $html .= $this->output->box_end();
144 $html .= $this->output->footer();
146 return $html;
150 * Generic info about the tool.
152 * @return string
154 public function generic_info() {
156 // Info.
157 $installurl = behat_command::DOCS_URL;
158 $installlink = html_writer::tag('a', $installurl, array('href' => $installurl, 'target' => '_blank'));
159 $writetestsurl = 'https://docs.moodle.org/dev/Writing acceptance tests';
160 $writetestslink = html_writer::tag('a', $writetestsurl, array('href' => $writetestsurl, 'target' => '_blank'));
161 $writestepsurl = 'https://docs.moodle.org/dev/Writing_new_acceptance_test_step_definitions';
162 $writestepslink = html_writer::tag('a', $writestepsurl, array('href' => $writestepsurl, 'target' => '_blank'));
163 $infos = array(
164 get_string('installinfo', 'tool_behat', $installlink),
165 get_string('newtestsinfo', 'tool_behat', $writetestslink),
166 get_string('newstepsinfo', 'tool_behat', $writestepslink)
169 // List of steps.
170 $html = $this->output->box_start();
171 $html .= html_writer::tag('div', get_string('aim', 'tool_behat'));
172 $html .= html_writer::start_tag('div');
173 $html .= html_writer::start_tag('ul');
174 $html .= html_writer::start_tag('li');
175 $html .= implode(html_writer::end_tag('li') . html_writer::start_tag('li'), $infos);
176 $html .= html_writer::end_tag('li');
177 $html .= html_writer::end_tag('ul');
178 $html .= html_writer::end_tag('div');
179 $html .= $this->output->box_end();
181 return $html;