0454436d39a8f1388ab36a57a4956efd4293b8b5
[moodle.git] / admin / tool / behat / renderer.php
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/>.
17 /**
18  * Behat tool renderer
19  *
20  * @package    tool_behat
21  * @copyright  2012 David Monllaó
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->libdir . '/behat/classes/behat_selectors.php');
30 /**
31  * Renderer for behat tool web features
32  *
33  * @package    tool_behat
34  * @copyright  2012 David Monllaó
35  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
37 class tool_behat_renderer extends plugin_renderer_base {
39     /**
40      * Renders the list of available steps according to the submitted filters.
41      *
42      * @param mixed $stepsdefinitions Available steps array.
43      * @param moodleform $form
44      * @return string HTML code
45      */
46     public function render_stepsdefinitions($stepsdefinitions, $form) {
48         $html = $this->generic_info();
50         // Form.
51         ob_start();
52         $form->display();
53         $html .= ob_get_contents();
54         ob_end_clean();
56         if (empty($stepsdefinitions)) {
57             $stepsdefinitions = get_string('nostepsdefinitions', 'tool_behat');
58         } else {
60             $stepsdefinitions = implode('', $stepsdefinitions);
62             // Replace text selector type arguments with a user-friendly select.
63             $stepsdefinitions = preg_replace_callback('/(TEXT_SELECTOR\d?_STRING)/',
64                 function ($matches) {
65                     return html_writer::select(behat_selectors::get_allowed_text_selectors(), uniqid());
66                 },
67                 $stepsdefinitions
68             );
70             // Replace selector type arguments with a user-friendly select.
71             $stepsdefinitions = preg_replace_callback('/(SELECTOR\d?_STRING)/',
72                 function ($matches) {
73                     return html_writer::select(behat_selectors::get_allowed_selectors(), uniqid());
74                 },
75                 $stepsdefinitions
76             );
78             // Replace simple OR options.
79             $regex = '#\(\?P<[^>]+>([^\)|]+\|[^\)]+)\)#';
80             $stepsdefinitions = preg_replace_callback($regex,
81                 function($matches){
82                     return html_writer::select(explode('|', $matches[1]), uniqid());
83                 },
84                 $stepsdefinitions
85             );
87             $stepsdefinitions = preg_replace_callback('/(FIELD_VALUE_STRING)/',
88                 function ($matches) {
89                     global $CFG;
91                     // Creating a link to a popup with the help.
92                     $url = new moodle_url(
93                         '/help.php',
94                         array(
95                             'component' => 'tool_behat',
96                             'identifier' => 'fieldvalueargument',
97                             'lang' => current_language()
98                         )
99                     );
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'));
111                 },
112                 $stepsdefinitions
113             );
114         }
116         // Steps definitions.
117         $html .= html_writer::tag('div', $stepsdefinitions, array('class' => 'steps-definitions'));
119         $html .= $this->output->footer();
121         return $html;
122     }
124     /**
125      * Renders an error message adding the generic info about the tool purpose and setup.
126      *
127      * @param string $msg The error message
128      * @return string HTML
129      */
130     public function render_error($msg) {
132         $html = $this->generic_info();
134         $a = new stdClass();
135         $a->errormsg = $msg;
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();
149         return $html;
150     }
152     /**
153      * Generic info about the tool.
154      *
155      * @return string
156      */
157     protected function generic_info() {
159         $title = get_string('pluginname', 'tool_behat');
161         // Header.
162         $html = $this->output->header();
163         $html .= $this->output->heading($title);
165         // Info.
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'));
172         $infos = array(
173             get_string('installinfo', 'tool_behat', $installlink),
174             get_string('newtestsinfo', 'tool_behat', $writetestslink),
175             get_string('newstepsinfo', 'tool_behat', $writestepslink)
176         );
178         // List of steps.
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();
191         return $html;
192     }