Skip to content

Commit

Permalink
MDL-22001 filter_text() and filter_string() now use context parameter…
Browse files Browse the repository at this point in the history
… instead of courseid, PAGE->context is used only as a fallback; moved comment stuff away from format_text() because it does not belong there; filterlib is not using courseid except for legacy filters; fixed coding style in filters;Êimproved php docs; fixed upgrade of filters (should be in plugins, not core)
  • Loading branch information
skodak committed Jul 30, 2010
1 parent 1ef927d commit 35716b8
Show file tree
Hide file tree
Showing 30 changed files with 644 additions and 424 deletions.
3 changes: 2 additions & 1 deletion blog/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.


/**
* Core global functions for Blog.
*
Expand All @@ -25,6 +24,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

/**
* Library of functions and constants for blog
*/
Expand Down
18 changes: 11 additions & 7 deletions blog/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.


/**
* Classes for Blogs.
*
Expand All @@ -25,6 +24,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

/**
* Blog_entry class. Represents an entry in a user's blog. Contains all methods for managing this entry.
Expand Down Expand Up @@ -98,8 +98,9 @@ public function print_html($return=false) {
global $USER, $CFG, $COURSE, $DB, $OUTPUT, $PAGE;

$user = $DB->get_record('user', array('id'=>$this->userid));
$options = new stdclass;
if ($CFG->blogusecomments) {
$cmttext = '';
if (!empty($CFG->usecomments) and $CFG->blogusecomments) {
require_once($CFG->dirroot . '/comment/lib.php');
// Comments
$cmt = new stdClass();
$cmt->context = get_context_instance(CONTEXT_USER, $user->id);
Expand All @@ -108,11 +109,12 @@ public function print_html($return=false) {
$cmt->env = 'blog';
$cmt->itemid = $this->id;
$cmt->showcount = $CFG->blogshowcommentscount;
$options->comments = $cmt;
$comment = new comment($cmt);
$cmttext = $comment->output(true);
}
$this->summary = file_rewrite_pluginfile_urls($this->summary, 'pluginfile.php', SYSCONTEXTID, 'blog', 'post', $this->id);

$template['body'] = format_text($this->summary, $this->summaryformat, $options);
$template['body'] = format_text($this->summary, $this->summaryformat).$cmttext;
$template['title'] = format_string($this->subject);
$template['userid'] = $user->id;
$template['author'] = fullname($user);
Expand Down Expand Up @@ -481,7 +483,9 @@ public function print_attachments($return=false) {

$fs = get_file_storage();

$files = $fs->get_area_files(SYSCONTEXTID, 'blog', 'attachment', $this->id);
$syscontext = get_context_instance(CONTEXT_SYSTEM);

$files = $fs->get_area_files($syscontext->id, 'blog', 'attachment', $this->id);

$imagereturn = "";
$output = "";
Expand Down Expand Up @@ -514,7 +518,7 @@ public function print_attachments($return=false) {
$imagereturn .= "<br />" . $OUTPUT->pix_icon($ffurl, $filename);
} else {
$imagereturn .= html_writer::link($ffurl, $image);
$imagereturn .= filter_text(html_writer::link($ffurl, $filename));
$imagereturn .= format_text(html_writer::link($ffurl, $filename), FORMAT_HTML, array('context'=>$syscontext));
}
}
}
Expand Down
53 changes: 40 additions & 13 deletions filter/activitynames/filter.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,62 @@
<?php
//This function provides automatic linking to
//activities when its name (title) is found inside every Moodle text
//It's based in the glosssary filter by Williams Castillo
//Modifications by stronk7.
class activitynames_filter extends moodle_text_filter {

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* This filter provides automatic linking to
* activities when its name (title) is found inside every Moodle text
*
* @package filter
* @subpackage activitynames
* @copyright 2004 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

/**
* Activity name filtering
*/
class filter_activitynames extends moodle_text_filter {
// Trivial-cache - keyed on $cachedcourseid
static $activitylist = null;
static $cachedcourseid;

function filter($text) {
global $CFG, $COURSE, $DB;

if (empty($this->courseid)) {
$this->courseid = SITEID;
if (!$courseid = get_courseid_from_context($this->context)) {
return $text;
}

// Initialise/invalidate our trivial cache if dealing with a different course
if (!isset($this->cachedcourseid) || $this->cachedcourseid !== (int)$this->courseid) {
if (!isset($this->cachedcourseid) || $this->cachedcourseid !== (int)$courseid) {
$this->activitylist = null;
}
$this->cachedcourseid = (int)$this->courseid;
$this->cachedcourseid = (int)$courseid;

/// It may be cached

if (is_null($this->activitylist)) {
$this->activitylist = array();

if ($COURSE->id == $this->courseid) {
if ($COURSE->id == $courseid) {
$course = $COURSE;
} else {
$course = $DB->get_record("course", array("id"=>$this->courseid));
$course = $DB->get_record("course", array("id"=>$courseid));
}

if (!isset($course->modinfo)) {
Expand All @@ -44,7 +71,7 @@ function filter($text) {
$this->activitylist = array(); /// We will store all the activities here

//Sort modinfo by name length
usort($modinfo, 'comparemodulenamesbylength');
usort($modinfo, 'filter_activitynames_comparemodulenamesbylength');

foreach ($modinfo as $activity) {
//Exclude labels, hidden activities and activities for group members only
Expand Down Expand Up @@ -76,7 +103,7 @@ function filter($text) {


//This function is used to order module names from longer to shorter
function comparemodulenamesbylength($a, $b) {
function filter_activitynames_comparemodulenamesbylength($a, $b) {
if (strlen($a->name) == strlen($b->name)) {
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions filter/algebra/algebradebug.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ function tex2image($texexp, $md5, $return=false) {
if (file_exists($pathname)) {
unlink($pathname);
}
$commandpath = tex_filter_get_executable(true);
$cmd = tex_filter_get_cmd($pathname, $texexp);
$commandpath = filter_tex_get_executable(true);
$cmd = filter_tex_get_cmd($pathname, $texexp);
system($cmd, $status);

if ($return) {
Expand Down
63 changes: 35 additions & 28 deletions filter/algebra/filter.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
<?PHP
/////////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Filter for converting simple calculator-type algebraic //
// expressions to cached gif images //
// //
// Copyright (C) 2004 Zbigniew Fiedorowicz [email protected] //
// Originally based on code provided by Bruno Vernier [email protected]//
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
/////////////////////////////////////////////////////////////////////////////
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Moodle - Filter for converting simple calculator-type algebraic
* expressions to cached gif images
*
* @package filter
* @subpackage algebra
* @copyright 2004 Zbigniew Fiedorowicz [email protected]
* Originally based on code provided by Bruno Vernier [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

//-------------------------------------------------------------------------
// NOTE: This Moodle text filter converts algebraic expressions delimited
// by either @@...@@ or by <algebra...>...</algebra> tags
Expand All @@ -37,7 +44,7 @@
// You will then need to edit your moodle/config.php to invoke mathml_filter.php
//-------------------------------------------------------------------------

function string_file_picture_algebra($imagefile, $tex= "", $height="", $width="", $align="middle") {
function filter_algebra_image($imagefile, $tex= "", $height="", $width="", $align="middle") {
// Given the path to a picture file in a course, or a URL,
// this function includes the picture in the page.
global $CFG, $OUTPUT;
Expand Down Expand Up @@ -77,14 +84,14 @@ function string_file_picture_algebra($imagefile, $tex= "", $height="", $width=""
$action = new popup_action('click', $link, 'popup', array('height'=>300,'width'=>240));
}
$output .= $OUTPUT->action_link($link, $anchorcontents, $action, array('title'=>'TeX'));

} else {
$output .= "Error: must pass URL or course";
}
return $output;
}

class algebra_filter extends moodle_text_filter {
class filter_algebra extends moodle_text_filter {
function filter($text){
global $CFG, $DB;

Expand Down Expand Up @@ -221,12 +228,12 @@ function filter($text){
$texcache->rawtext = $texexp;
$texcache->timemodified = time();
$DB->insert_record("cache_filters", $texcache, false);
$text = str_replace( $matches[0][$i], string_file_picture_algebra($filename, $texexp, '', '', $align), $text);
$text = str_replace( $matches[0][$i], filter_algebra_image($filename, $texexp, '', '', $align), $text);
} else {
$text = str_replace( $matches[0][$i],"<b>Undetermined error:</b> ",$text);
}
} else {
$text = str_replace( $matches[0][$i], string_file_picture_algebra($filename, $texcache->rawtext), $text);
$text = str_replace( $matches[0][$i], filter_algebra_image($filename, $texcache->rawtext), $text);
}
}
return $text;
Expand Down
2 changes: 1 addition & 1 deletion filter/algebra/pix.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
$texexp = str_replace('&gt;','>',$texexp);
$texexp = preg_replace('!\r\n?!',' ',$texexp);
$texexp = '\Large ' . $texexp;
$cmd = tex_filter_get_cmd($pathname, $texexp);
$cmd = filter_tex_get_cmd($pathname, $texexp);
system($cmd, $status);
}
}
Expand Down
36 changes: 33 additions & 3 deletions filter/censor/filter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Censorship filtering
*
* This very simple example of a Text Filter will parse
* printed text, blacking out words perceived to be bad
*
* @package filter
* @subpackage censor
* @copyright 2004 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

//////////////////////////////////////////////////////////////
// Censorship filtering
//
Expand All @@ -9,19 +39,19 @@
//
//////////////////////////////////////////////////////////////

/// This is the filtering class. It accepts the courseid and
/// options to be filtered (In HTML form).
class censor_filter extends moodle_text_filter {
class filter_censor extends moodle_text_filter {
private function _canseecensor() {
return is_siteadmin(); //TODO: add proper access control
}

function hash(){
$cap = "mod/filter:censor";
if (is_siteadmin()) { //TODO: add proper access control
$cap = "mod/filter:seecensor";
}
return $cap;
}

function filter($text){
static $words;
global $CFG;
Expand Down
Loading

0 comments on commit 35716b8

Please sign in to comment.