PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/extsearch/locallib.php

https://github.com/dataview/Moodle2_NZ_Schools
PHP | 306 lines | 163 code | 53 blank | 90 comment | 43 complexity | 22a2cb68f5a389af9b61b7a1008ace7d 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. * Private extsearch module utility functions
  18. *
  19. * @package mod
  20. * @subpackage extsearch
  21. * @copyright 2009 Petr Skoda {@link http://skodak.org}
  22. * @copyright 2011 Aaron Wells {@link http://www.catalyst.net.nz}
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. defined('MOODLE_INTERNAL') || die;
  26. require_once("$CFG->libdir/filelib.php");
  27. require_once("$CFG->libdir/resourcelib.php");
  28. require_once("$CFG->dirroot/mod/extsearch/lib.php");
  29. /**
  30. * Print extsearch header.
  31. * @param object $extsearch
  32. * @param object $cm
  33. * @param object $course
  34. * @return void
  35. */
  36. function extsearch_print_header($extsearch, $cm, $course) {
  37. global $PAGE, $OUTPUT;
  38. $PAGE->set_title($course->shortname.': '.$extsearch->name);
  39. $PAGE->set_heading($course->fullname);
  40. $PAGE->set_activity_record($extsearch);
  41. echo $OUTPUT->header();
  42. }
  43. /**
  44. * Print extsearch heading.
  45. * @param object $extsearch
  46. * @param object $cm
  47. * @param object $course
  48. * @param bool $ignoresettings print even if not specified in modedit
  49. * @return void
  50. */
  51. function extsearch_print_heading($extsearch, $cm, $course, $ignoresettings=false) {
  52. global $OUTPUT;
  53. $options = empty($extsearch->displayoptions) ? array() : unserialize($extsearch->displayoptions);
  54. if ($ignoresettings or !empty($options['printheading'])) {
  55. echo $OUTPUT->heading(format_string($extsearch->name), 2, 'main', 'extsearchheading');
  56. }
  57. }
  58. /**
  59. * Print extsearch introduction.
  60. * @param object $extsearch
  61. * @param object $cm
  62. * @param object $course
  63. * @param bool $ignoresettings print even if not specified in modedit
  64. * @return void
  65. */
  66. function extsearch_print_intro($extsearch, $cm, $course, $ignoresettings=false) {
  67. global $OUTPUT;
  68. $options = empty($extsearch->displayoptions) ? array() : unserialize($extsearch->displayoptions);
  69. if ($ignoresettings or !empty($options['printintro'])) {
  70. if (trim(strip_tags($extsearch->intro))) {
  71. echo $OUTPUT->box_start('mod_introbox', 'extsearchintro');
  72. echo format_module_intro('extsearch', $extsearch, $cm->id);
  73. echo $OUTPUT->box_end();
  74. }
  75. }
  76. }
  77. /**
  78. * Display extsearch frames.
  79. * @param object $extsearch
  80. * @param object $cm
  81. * @param object $course
  82. * @return does not return
  83. */
  84. function extsearch_display_frame($extsearch, $cm, $course) {
  85. global $PAGE, $OUTPUT, $CFG;
  86. $frame = optional_param('frameset', 'main', PARAM_ALPHA);
  87. if ($frame === 'top') {
  88. $PAGE->set_pagelayout('frametop');
  89. extsearch_print_header($extsearch, $cm, $course);
  90. extsearch_print_heading($extsearch, $cm, $course);
  91. extsearch_print_intro($extsearch, $cm, $course);
  92. echo $OUTPUT->footer();
  93. die;
  94. } else {
  95. $config = get_config('extsearch');
  96. $context = get_context_instance(CONTEXT_MODULE, $cm->id);
  97. $exteurl = $extsearch->externalurl;
  98. $navurl = "$CFG->wwwroot/mod/extsearch/view.php?id=$cm->id&amp;frameset=top";
  99. $title = strip_tags(format_string($course->shortname.': '.$extsearch->name));
  100. $framesize = $config->framesize;
  101. $modulename = s(get_string('modulename','extsearch'));
  102. $dir = get_string('thisdirection', 'langconfig');
  103. $extframe = <<<EOF
  104. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
  105. <html dir="$dir">
  106. <head>
  107. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  108. <title>$title</title>
  109. </head>
  110. <frameset rows="$framesize,*">
  111. <frame src="$navurl" title="$modulename"/>
  112. <frame src="$exteurl" title="$modulename"/>
  113. </frameset>
  114. </html>
  115. EOF;
  116. @header('Content-Type: text/html; charset=utf-8');
  117. echo $extframe;
  118. die;
  119. }
  120. }
  121. /**
  122. * Print extsearch info and link.
  123. * @param object $extsearch
  124. * @param object $cm
  125. * @param object $course
  126. * @return does not return
  127. */
  128. function extsearch_print_workaround($extsearch, $cm, $course) {
  129. global $OUTPUT;
  130. extsearch_print_header($extsearch, $cm, $course);
  131. extsearch_print_heading($extsearch, $cm, $course, true);
  132. extsearch_print_intro($extsearch, $cm, $course, true);
  133. $fullurl = $extsearch->externalurl;
  134. $display = extsearch_get_final_display_type($extsearch);
  135. if ($display == RESOURCELIB_DISPLAY_POPUP) {
  136. $options = empty($extsearch->displayoptions) ? array() : unserialize($extsearch->displayoptions);
  137. $width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
  138. $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
  139. $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
  140. $extra = "onclick=\"window.open('$fullurl', '', '$wh'); return false;\"";
  141. } else if ($display == RESOURCELIB_DISPLAY_NEW) {
  142. $extra = "onclick=\"this.target='_blank';\"";
  143. } else {
  144. $extra = '';
  145. }
  146. echo '<div class="urlworkaround">';
  147. print_string('clicktoopen', 'extsearch', "<a href=\"$fullurl\" $extra>$fullurl</a>");
  148. echo '</div>';
  149. echo $OUTPUT->footer();
  150. die;
  151. }
  152. /**
  153. * Display embedded extsearch file.
  154. * @param object $extsearch
  155. * @param object $cm
  156. * @param object $course
  157. * @param stored_file $file main file
  158. * @return does not return
  159. */
  160. function extsearch_display_embed($extsearch, $cm, $course) {
  161. global $CFG, $PAGE, $OUTPUT;
  162. $mimetype = resourcelib_guess_url_mimetype($extsearch->externalurl);
  163. $fullurl = $extsearch->externalurl;
  164. $title = $extsearch->name;
  165. $link = html_writer::tag('a', $fullurl, array('href'=>str_replace('&amp;', '&', $fullurl)));
  166. $clicktoopen = get_string('clicktoopen', 'extsearch', $link);
  167. if (in_array($mimetype, array('image/gif','image/jpeg','image/png'))) { // It's an image
  168. $code = resourcelib_embed_image($fullurl, $title);
  169. } else if ($mimetype == 'audio/mp3') {
  170. // MP3 audio file
  171. $code = resourcelib_embed_mp3($fullurl, $title, $clicktoopen);
  172. } else if ($mimetype == 'video/x-flv') {
  173. // Flash video file
  174. $code = resourcelib_embed_flashvideo($fullurl, $title, $clicktoopen);
  175. } else if ($mimetype == 'application/x-shockwave-flash') {
  176. // Flash file
  177. $code = resourcelib_embed_flash($fullurl, $title, $clicktoopen);
  178. } else if (substr($mimetype, 0, 10) == 'video/x-ms') {
  179. // Windows Media Player file
  180. $code = resourcelib_embed_mediaplayer($fullurl, $title, $clicktoopen);
  181. } else if ($mimetype == 'video/quicktime') {
  182. // Quicktime file
  183. $code = resourcelib_embed_quicktime($fullurl, $title, $clicktoopen);
  184. } else if ($mimetype == 'video/mpeg') {
  185. // Mpeg file
  186. $code = resourcelib_embed_mpeg($fullurl, $title, $clicktoopen);
  187. } else if ($mimetype == 'audio/x-pn-realaudio-plugin') {
  188. // RealMedia file
  189. $code = resourcelib_embed_real($fullurl, $title, $clicktoopen);
  190. } else {
  191. // anything else - just try object tag enlarged as much as possible
  192. $code = resourcelib_embed_general($fullurl, $title, $clicktoopen, $mimetype);
  193. }
  194. extsearch_print_header($extsearch, $cm, $course);
  195. extsearch_print_heading($extsearch, $cm, $course);
  196. echo $code;
  197. extsearch_print_intro($extsearch, $cm, $course);
  198. echo $OUTPUT->footer();
  199. die;
  200. }
  201. /**
  202. * Decide the best diaply format.
  203. * @param object $extsearch
  204. * @return int display type constant
  205. */
  206. function extsearch_get_final_display_type($extsearch) {
  207. global $CFG;
  208. if ($extsearch->display != RESOURCELIB_DISPLAY_AUTO) {
  209. return $extsearch->display;
  210. }
  211. // detect links to local moodle pages
  212. if (strpos($extsearch->externalurl, $CFG->wwwroot) === 0) {
  213. if (strpos($extsearch->externalurl, 'file.php') === false and strpos($extsearch->externalurl, '.php') !== false ) {
  214. // most probably our moodle page with navigation
  215. return RESOURCELIB_DISPLAY_OPEN;
  216. }
  217. }
  218. static $download = array('application/zip', 'application/x-tar', 'application/g-zip', // binary formats
  219. 'application/pdf', 'text/html'); // these are known to cause trouble for external links, sorry
  220. static $embed = array('image/gif', 'image/jpeg', 'image/png', 'image/svg+xml', // images
  221. 'application/x-shockwave-flash', 'video/x-flv', 'video/x-ms-wm', // video formats
  222. 'video/quicktime', 'video/mpeg',
  223. 'audio/mp3', 'audio/x-realaudio-plugin', 'x-realaudio-plugin', // audio formats,
  224. );
  225. $mimetype = mimeinfo('type', $extsearch->externalurl);
  226. if (in_array($mimetype, $download)) {
  227. return RESOURCELIB_DISPLAY_DOWNLOAD;
  228. }
  229. if (in_array($mimetype, $embed)) {
  230. return RESOURCELIB_DISPLAY_EMBED;
  231. }
  232. // let the browser deal with it somehow
  233. return RESOURCELIB_DISPLAY_OPEN;
  234. }
  235. /**
  236. * Optimised mimetype detection from general URL
  237. * @param $fullurl
  238. * @return string mimetype
  239. */
  240. function extsearch_guess_icon($fullurl) {
  241. global $CFG;
  242. require_once("$CFG->libdir/filelib.php");
  243. if (substr_count($fullurl, '/') < 3 or substr($fullurl, -1) === '/') {
  244. // most probably default directory - index.php, index.html, etc.
  245. return 'f/web';
  246. }
  247. $icon = mimeinfo('icon', $fullurl);
  248. $icon = 'f/'.str_replace(array('.gif', '.png'), '', $icon);
  249. if ($icon === 'f/html' or $icon === 'f/unknown') {
  250. $icon = 'f/web';
  251. }
  252. return $icon;
  253. }