You are here

function hotpot_convert_url in Quiz 6.6

Same name and namespace in other branches
  1. 6.5 includes/moodle/question/format/hotpot/format.php \hotpot_convert_url()
1 call to hotpot_convert_url()
hotpot_convert_relative_url in includes/moodle/question/format/hotpot/format.php

File

includes/moodle/question/format/hotpot/format.php, line 745

Code

function hotpot_convert_url($baseurl, $filename, $url, $stripslashes = true) {

  // maintain a cache of converted urls
  static $HOTPOT_RELATIVE_URLS = array();
  if ($stripslashes) {
    $url = stripslashes($url);
  }

  // is this an absolute url? (or javascript pseudo url)
  if (preg_match('%^(http://|/|javascript:)%i', $url)) {

    // do nothing
    // has this relative url already been converted?
  }
  else {
    if (isset($HOTPOT_RELATIVE_URLS[$url])) {
      $url = $HOTPOT_RELATIVE_URLS[$url];
    }
    else {
      $relativeurl = $url;

      // get the subdirectory, $dir, of the quiz $filename
      $dir = dirname($filename);

      // allow for leading "./" and "../"
      while (preg_match('|^(\\.{1,2})/(.*)$|', $url, $matches)) {
        if ($matches[1] == '..') {
          $dir = dirname($dir);
        }
        $url = $matches[2];
      }

      // add subdirectory, $dir, to $baseurl, if necessary
      if ($dir && $dir != '.') {
        $baseurl .= "{$dir}/";
      }

      // prefix $url with $baseurl
      $url = "{$baseurl}{$url}";

      // add url to cache
      $HOTPOT_RELATIVE_URLS[$relativeurl] = $url;
    }
  }
  return $url;
}