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/**
18 * Fixture for testing the functionality of core_media_player_native.
19 *
20 * @package     core
21 * @subpackage  fixtures
22 * @category    test
23 * @copyright   2019 Ruslan Kabalin
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Native media player stub for testing purposes.
31 *
32 * @copyright   2019 Ruslan Kabalin
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35class media_test_native_plugin extends core_media_player_native {
36    /** @var int Player rank */
37    public $rank;
38    /** @var int Arbitrary number */
39    public $num;
40
41    /**
42     * Constructor is used for tuning the fixture.
43     *
44     * @param int $num Number (used in output)
45     * @param int $rank Player rank
46     */
47    public function __construct($num = 1, $rank = 13) {
48        $this->rank = $rank;
49        $this->num = $num;
50    }
51
52    /**
53     * Generates code required to embed the player.
54     *
55     * @param array $urls URLs of media files
56     * @param string $name Display name; '' to use default
57     * @param int $width Optional width; 0 to use default
58     * @param int $height Optional height; 0 to use default
59     * @param array $options Options array
60     * @return string HTML code for embed
61     */
62    public function embed($urls, $name, $width, $height, $options) {
63        $sources = array();
64        foreach ($urls as $url) {
65            $params = ['src' => $url];
66            $sources[] = html_writer::empty_tag('source', $params);
67        }
68
69        $sources = implode("\n", $sources);
70        $title = $this->get_name($name, $urls);
71        // Escape title but prevent double escaping.
72        $title = s(preg_replace(['/&amp;/', '/&gt;/', '/&lt;/'], ['&', '>', '<'], $title));
73
74        return <<<OET
75<video class="mediaplugin mediaplugin_test" title="$title">
76    $sources
77</video>
78OET;
79    }
80
81    /**
82     * Gets the ranking of this player.
83     *
84     * @return int Rank
85     */
86    public function get_rank() {
87        return 10;
88    }
89}