MDL-43079 quiz statistics : fix stats graph when all grades zero
[moodle.git] / filter / mediaplugin / filter.php
blob57e58d513dc1d837fc3d3240a407e6c63b71cf3c
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 * Media plugin filtering
20 * This filter will replace any links to a media file with
21 * a media plugin that plays that media inline
23 * @package filter
24 * @subpackage mediaplugin
25 * @copyright 2004 onwards Martin Dougiamas {@link http://moodle.com}
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') || die();
31 /**
32 * Automatic media embedding filter class.
34 * It is highly recommended to configure servers to be compatible with our slasharguments,
35 * otherwise the "?d=600x400" may not work.
37 * @package filter
38 * @subpackage mediaplugin
39 * @copyright 2004 onwards Martin Dougiamas {@link http://moodle.com}
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class filter_mediaplugin extends moodle_text_filter {
43 /** @var bool True if currently filtering trusted text */
44 private $trusted;
45 /** @var core_media_renderer Media renderer */
46 private $mediarenderer;
47 /** @var string Partial regex pattern indicating possible embeddable content */
48 private $embedmarkers;
50 public function filter($text, array $options = array()) {
51 global $CFG, $PAGE;
53 if (!is_string($text) or empty($text)) {
54 // non string data can not be filtered anyway
55 return $text;
58 if (stripos($text, '</a>') === false) {
59 // Performance shortcut - if not </a> tag, nothing can match.
60 return $text;
63 if (!$this->mediarenderer) {
64 $this->mediarenderer = $PAGE->get_renderer('core', 'media');
65 $this->embedmarkers = $this->mediarenderer->get_embeddable_markers();
68 // Check SWF permissions.
69 $this->trusted = !empty($options['noclean']) or !empty($CFG->allowobjectembed);
71 // Handle all links that contain any 'embeddable' marker text (it could
72 // do all links, but the embeddable markers thing should make it faster
73 // by meaning for most links it doesn't drop into PHP code).
74 $newtext = preg_replace_callback($re = '~<a\s[^>]*href="([^"]*(?:' .
75 $this->embedmarkers . ')[^"]*)"[^>]*>([^>]*)</a>~is',
76 array($this, 'callback'), $text);
78 if (empty($newtext) or $newtext === $text) {
79 // error or not filtered
80 return $text;
83 return $newtext;
86 /**
87 * Replace link with embedded content, if supported.
89 * @param array $matches
90 * @return string
92 private function callback(array $matches) {
93 global $CFG, $PAGE;
94 // Check if we ignore it.
95 if (preg_match('/class="[^"]*nomediaplugin/i', $matches[0])) {
96 return $matches[0];
99 // Get name.
100 $name = trim($matches[2]);
101 if (empty($name) or strpos($name, 'http') === 0) {
102 $name = ''; // Use default name.
105 // Split provided URL into alternatives.
106 $urls = core_media::split_alternatives($matches[1], $width, $height);
108 $options = array();
110 // Allow SWF (or not).
111 if ($this->trusted) {
112 $options[core_media::OPTION_TRUSTED] = true;
115 // We could test whether embed is possible using can_embed, but to save
116 // time, let's just embed it with the 'fallback to blank' option which
117 // does most of the same stuff anyhow.
118 $options[core_media::OPTION_FALLBACK_TO_BLANK] = true;
120 // NOTE: Options are not passed through from filter because the 'embed'
121 // code does not recognise filter options (it's a different kind of
122 // option-space) as it can be used in non-filter situations.
123 $result = $this->mediarenderer->embed_alternatives($urls, $name, $width, $height, $options);
125 // If something was embedded, return it, otherwise return original.
126 if ($result !== '') {
127 return $result;
128 } else {
129 return $matches[0];