PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/mediafilter/hotpot/class.php

https://github.com/KieranRBriggs/moodle-mod_hotpot
PHP | 150 lines | 69 code | 21 blank | 60 comment | 21 complexity | 220a54dae6f69dca17f097362d9bdbab MD5 | raw file
  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. * mod/hotpot/mediafilter/hotpot/class.php
  18. *
  19. * @package mod-hotpot
  20. * @copyright 2010 Gordon Bateson <gordon.bateson@gmail.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. // get the parent class (=hotpot_mediafilter)
  25. require_once($CFG->dirroot.'/mod/hotpot/mediafilter/class.php');
  26. /**
  27. * hotpot_mediafilter_hotpot
  28. *
  29. * @copyright 2010 Gordon Bateson
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. * @since Moodle 2.0
  32. */
  33. class hotpot_mediafilter_hotpot extends hotpot_mediafilter {
  34. /**
  35. * mediaplugin_filter
  36. *
  37. * @param xxx $hotpot
  38. * @param xxx $text
  39. * @param xxx $options (optional, default=array)
  40. * @return xxx
  41. */
  42. function mediaplugin_filter($hotpot, $text, $options=array()) {
  43. global $CFG, $PAGE;
  44. // Keep track of the id of the current quiz
  45. // so that eolas_fix.js is only included once in each quiz
  46. // Note: the cron script calls this method for multiple quizzes
  47. static $eolas_fix_applied = 0;
  48. if (! is_string($text)) {
  49. // non string data can not be filtered anyway
  50. return $text;
  51. }
  52. $newtext = $text; // fullclone is slow and not needed here
  53. foreach (array_keys($this->media_filetypes) as $filetype) {
  54. // set $adminsetting, the name of the $CFG setting, if any, which enables/disables filtering of this file type
  55. $adminsetting = '';
  56. if (preg_match('/^[a-z]+$/', $filetype)) {
  57. $hotpot_enable = 'hotpot_enable'.$filetype;
  58. $filter_mediaplugin_enable = 'filter_mediaplugin_enable_'.$filetype;
  59. if (isset($CFG->$hotpot_enable)) {
  60. $adminsetting = $hotpot_enable;
  61. } else if (isset($CFG->$filter_mediaplugin_enable)) {
  62. $adminsetting = $filter_mediaplugin_enable;
  63. }
  64. }
  65. // set $search and $replace strings
  66. $search = '/<a.*?href="([^"?>]*\.'.$filetype.'[^">]*)"[^>]*>.*?<\/a>/is';
  67. if ($adminsetting=='' || $CFG->$adminsetting) {
  68. // filtering of this file type is allowed
  69. $callback = array($this, 'hotpot_mediaplugin_filter');
  70. $callback = partial($callback, $filetype, $options);
  71. $newtext = preg_replace_callback($search, $callback, $newtext, -1, $count);
  72. } else {
  73. // filtering of this file type is disabled
  74. $replace = '$1<br />'.get_string('error_disabledfilter', 'hotpot', $adminsetting);
  75. $newtext = preg_replace($search, $replace, $newtext, -1, $count);
  76. }
  77. if ($count>0) {
  78. break;
  79. }
  80. }
  81. if (is_null($newtext) || $newtext==$text) {
  82. // error or not filtered
  83. return $text;
  84. }
  85. if ($eolas_fix_applied==$hotpot->id) {
  86. // do nothing - the external javascripts have already been included for this quiz
  87. } else {
  88. $PAGE->requires->js('/mod/hotpot/mediafilter/ufo.js', true);
  89. $PAGE->requires->js('/mod/hotpot/mediafilter/eolas_fix.js');
  90. //$newtext .= "\n".'<script type="text/javascript" src="'.$CFG->wwwroot.'/mod/hotpot/mediafilter/ufo.js"></script>';
  91. //$newtext .= "\n".'<script type="text/javascript" src="'.$CFG->wwwroot.'/mod/hotpot/mediafilter/eolas_fix.js" defer="defer"></script>';
  92. $eolas_fix_applied = $hotpot->id;
  93. }
  94. return $newtext;
  95. }
  96. /**
  97. * hotpot_mediaplugin_filter
  98. *
  99. * @param xxx $match
  100. * @param xxx $filetype
  101. * @param xxx $options
  102. * @return xxx
  103. */
  104. function hotpot_mediaplugin_filter($filetype, $options, $match) {
  105. $link = $match[0];
  106. $mediaurl = $match[1];
  107. // get a valid $player name
  108. if (isset($options['player'])) {
  109. $player = $options['player'];
  110. } else {
  111. $player = '';
  112. }
  113. if ($player=='') {
  114. $player = $this->defaultplayer;
  115. } else if (! array_key_exists($player, $this->players)) {
  116. debugging('Invalid media player requested: '.$player);
  117. $player = $this->defaultplayer;
  118. }
  119. // merge player options
  120. if ($player==$this->defaultplayer) {
  121. $options = array_merge($this->players[$player]->options, $options);
  122. } else {
  123. $options = array_merge($this->players[$this->defaultplayer]->options, $this->players[$player]->options, $options);
  124. }
  125. // generate content for required player
  126. $content = $this->players[$player]->generate($filetype, $link, $mediaurl, $options);
  127. return $content;
  128. }
  129. }