Example #1
0
/**
 * callback function from {@link portfolio_rewrite_pluginfile_urls}
 * looks through preg_replace matches and replaces content with whatever the active portfolio export format says
 *
 * @param int $contextid module context id
 * @param string $component module name (eg:mod_assignment)
 * @param string $filearea normal file_area arguments
 * @param int $itemid component item id
 * @param portfolio_format $format exporter format type
 * @param array $options extra options to pass through to the file_output function in the format (optional)
 * @param array $matches internal matching
 * @return object|array|string
 */
function portfolio_rewrite_pluginfile_url_callback($contextid, $component, $filearea, $itemid, $format, $options, $matches)
{
    $matches = $matches[0];
    // no internal matching
    $dom = new DomDocument();
    if (!$dom->loadXML($matches)) {
        return $matches;
    }
    $attributes = array();
    foreach ($dom->documentElement->attributes as $attr => $node) {
        $attributes[$attr] = $node->value;
    }
    // now figure out the file
    $fs = get_file_storage();
    $key = 'href';
    if (!array_key_exists('href', $attributes) && array_key_exists('src', $attributes)) {
        $key = 'src';
    }
    if (!array_key_exists($key, $attributes)) {
        debugging('Couldn\'t find an attribute to use that contains @@PLUGINFILE@@ in portfolio_rewrite_pluginfile');
        return $matches;
    }
    $filename = substr($attributes[$key], strpos($attributes[$key], '@@PLUGINFILE@@') + strlen('@@PLUGINFILE@@'));
    $filepath = '/';
    if (strpos($filename, '/') !== 0) {
        $bits = explode('/', $filename);
        $filename = array_pop($bits);
        $filepath = implode('/', $bits);
    }
    if (!($file = $fs->get_file($contextid, $component, $filearea, $itemid, $filepath, $filename))) {
        debugging("Couldn't find a file from the embedded path info context {$contextid} component {$component} filearea {$filearea} itemid {$itemid} filepath {$filepath} name {$filename}");
        return $matches;
    }
    if (empty($options)) {
        $options = array();
    }
    $options['attributes'] = $attributes;
    return $format->file_output($file, $options);
}
/**
 * callback function from {@link portfolio_rewrite_pluginfile_urls}
 * looks through preg_replace matches and replaces content with whatever the active portfolio export format says
 *
 * @param int $contextid module context id
 * @param string $component module name (eg:mod_assignment)
 * @param string $filearea normal file_area arguments
 * @param int $itemid component item id
 * @param portfolio_format $format exporter format type
 * @param array $options extra options to pass through to the file_output function in the format (optional)
 * @param array $matches internal matching
 * @return object|array|string
 */
function portfolio_rewrite_pluginfile_url_callback($contextid, $component, $filearea, $itemid, $format, $options, $matches)
{
    $matches = $matches[0];
    // No internal matching.
    // Loads the HTML.
    $dom = new DomDocument();
    if (!$dom->loadHTML($matches)) {
        return $matches;
    }
    // Navigates to the node.
    $xpath = new DOMXPath($dom);
    $nodes = $xpath->query('/html/body/child::*');
    if (empty($nodes) || count($nodes) > 1) {
        // Unexpected sequence, none or too many nodes.
        return $matches;
    }
    $dom = $nodes->item(0);
    $attributes = array();
    foreach ($dom->attributes as $attr => $node) {
        $attributes[$attr] = $node->value;
    }
    // now figure out the file
    $fs = get_file_storage();
    $key = 'href';
    if (!array_key_exists('href', $attributes) && array_key_exists('src', $attributes)) {
        $key = 'src';
    }
    if (!array_key_exists($key, $attributes)) {
        debugging('Couldn\'t find an attribute to use that contains @@PLUGINFILE@@ in portfolio_rewrite_pluginfile');
        return $matches;
    }
    $filename = substr($attributes[$key], strpos($attributes[$key], '@@PLUGINFILE@@') + strlen('@@PLUGINFILE@@'));
    $filepath = '/';
    if (strpos($filename, '/') !== 0) {
        $bits = explode('/', $filename);
        $filename = array_pop($bits);
        $filepath = implode('/', $bits);
    }
    if (!($file = $fs->get_file($contextid, $component, $filearea, $itemid, $filepath, urldecode($filename)))) {
        debugging("Couldn't find a file from the embedded path info context {$contextid} component {$component} filearea {$filearea} itemid {$itemid} filepath {$filepath} name {$filename}");
        return $matches;
    }
    if (empty($options)) {
        $options = array();
    }
    $options['attributes'] = $attributes;
    return $format->file_output($file, $options);
}