function replace_file_links($question, $fromcourseid, $tocourseid, $url, $destination) { global $CFG; $updateqrec = false; /// Question image if (!empty($question->image)) { //support for older questions where we have a complete url in image field if (substr(strtolower($question->image), 0, 7) == 'http://') { $questionimage = preg_replace('!^' . question_file_links_base_url($fromcourseid) . preg_quote($url, '!') . '$!i', $destination, $question->image, 1); } else { $questionimage = preg_replace('!^' . preg_quote($url, '!') . '$!i', $destination, $question->image, 1); } if ($questionimage != $question->image) { $question->image = $questionimage; $updateqrec = true; } } /// Questiontext and general feedback. $question->questiontext = question_replace_file_links_in_html($question->questiontext, $fromcourseid, $tocourseid, $url, $destination, $updateqrec); $question->generalfeedback = question_replace_file_links_in_html($question->generalfeedback, $fromcourseid, $tocourseid, $url, $destination, $updateqrec); /// If anything has changed, update it in the database. if ($updateqrec) { if (!update_record('question', addslashes_recursive($question))) { error('Couldn\'t update question ' . $question->name); } } /// Answers, if this question uses them. if (isset($question->options->answers)) { //answers that do not need updating have been unset foreach ($question->options->answers as $answer) { $answerchanged = false; /// URLs in the answers themselves, if appropriate. if ($this->has_html_answers()) { $answer->answer = question_replace_file_links_in_html($answer->answer, $fromcourseid, $tocourseid, $url, $destination, $answerchanged); } /// URLs in the answer feedback. $answer->feedback = question_replace_file_links_in_html($answer->feedback, $fromcourseid, $tocourseid, $url, $destination, $answerchanged); /// If anything has changed, update it in the database. if ($answerchanged) { if (!update_record('question_answers', addslashes_recursive($answer))) { error('Couldn\'t update question (' . $question->name . ') answer ' . $answer->id); } } } } }
function question_replace_file_links_in_html($html, $fromcourseid, $tocourseid, $url, $destination, &$changed) { global $CFG; if ($CFG->slasharguments) { // Use this method if possible for better caching $tourl = "{$CFG->wwwroot}/file.php/{$tocourseid}/{$destination}"; } else { $tourl = "{$CFG->wwwroot}/file.php?file=/{$tocourseid}/{$destination}"; } $fromurl = question_file_links_base_url($fromcourseid) . preg_quote($url, '!'); $searchfor = array('!(<\\s*(a|img)\\s[^>]*(href|src)\\s*=\\s*")' . $fromurl . '(")!i', '!(<\\s*(a|img)\\s[^>]*(href|src)\\s*=\\s*\')' . $fromurl . '(\')!i'); $newhtml = preg_replace($searchfor, '\\1' . $tourl . '\\5', $html); if ($newhtml != $html) { $changed = true; } return $newhtml; }
function question_replace_file_links_in_html($html, $fromcourseid, $tocourseid, $url, $destination, &$changed) { global $CFG; require_once $CFG->libdir . '/filelib.php'; $tourl = get_file_url("{$tocourseid}/{$destination}"); $fromurl = question_file_links_base_url($fromcourseid) . preg_quote($url, '!'); $searchfor = array('!(<\\s*(a|img)\\s[^>]*(href|src)\\s*=\\s*")' . $fromurl . '(")!i', '!(<\\s*(a|img)\\s[^>]*(href|src)\\s*=\\s*\')' . $fromurl . '(\')!i'); $newhtml = preg_replace($searchfor, '\\1' . $tourl . '\\5', $html); if ($newhtml != $html) { $changed = true; } return $newhtml; }
function replace_file_links($question, $fromcourseid, $tocourseid, $url, $destination) { global $CFG; parent::replace_file_links($question, $fromcourseid, $tocourseid, $url, $destination); $optionschanged = false; if (!empty($question->options->backgroundmedia->media)) { //support for older questions where we have a complete url in image field if (substr(strtolower($question->options->backgroundmedia->media), 0, 7) == 'http://') { $questionbackground = preg_replace('!^' . question_file_links_base_url($fromcourseid) . preg_quote($url, '!') . '$!i', $destination, $question->options->backgroundmedia->media, 1); } else { $questionbackground = preg_replace('!^' . preg_quote($url, '!') . '$!i', $destination, $question->options->backgroundmedia->media, 1); } if ($questionbackground != $question->options->backgroundmedia->media) { $question->options->backgroundmedia->media = $questionbackground; $optionschanged = true; } } $question->options->feedbackok = question_replace_file_links_in_html($question->options->feedbackok, $fromcourseid, $tocourseid, $url, $destination, $optionschanged); $question->options->feedbackmissed = question_replace_file_links_in_html($question->options->feedbackmissed, $fromcourseid, $tocourseid, $url, $destination, $optionschanged); if ($optionschanged) { if (!update_record('question_dragdrop', addslashes_recursive($question->options))) { error('Couldn\'t update \'question_dragdrop\' record ' . $question->options->id); } } $mediachanged = false; foreach ($question->options->media as $media) { if (!empty($media->media)) { if (substr(strtolower($media->media), 0, 7) == 'http://') { $medianame = preg_replace('!^' . question_file_links_base_url($fromcourseid) . preg_quote($url, '!') . '$!i', $destination, $media->media, 1); } else { $medianame = preg_replace('!^' . preg_quote($url, '!') . '$!i', $destination, $media->media, 1); } if ($medianame != $media->media) { $media->media = $medianame; $mediachanged = true; } } $media->questiontext = question_replace_file_links_in_html($media->questiontext, $fromcourseid, $tocourseid, $url, $destination, $mediachanged); if ($mediachanged) { if (!update_record('question_dragdrop_media', addslashes_recursive($media))) { error('Couldn\'t update \'question_dragdrop_media\' record ' . $media->id); } } } }