2004-10-08 16:52:20 +00:00
< ? php // $Id$
2001-11-22 06:23:56 +00:00
2002-12-20 14:44:14 +00:00
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com //
// //
// 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 //
// //
///////////////////////////////////////////////////////////////////////////
2001-11-22 06:23:56 +00:00
2004-09-23 02:48:41 +00:00
/**
* Library of functions for web output
*
* Library of all general - purpose Moodle PHP functions and constants
* that produce HTML output
*
* Other main libraries :
* - datalib . php - functions that access the database .
* - moodlelib . php - general - purpose Moodle functions .
* @ author Martin Dougiamas
* @ version $Id $
2004-09-25 05:29:21 +00:00
* @ license http :// www . gnu . org / copyleft / gpl . html GNU Public License
2004-09-23 02:48:41 +00:00
* @ package moodlecore
*/
2005-01-26 15:57:00 +00:00
2006-01-04 08:23:42 +00:00
/// We are going to uses filterlib functions here
require_once ( " $CFG->libdir /filterlib.php " );
2002-10-10 07:26:10 +00:00
/// Constants
2002-10-19 04:58:14 +00:00
/// Define text formatting types ... eventually we can add Wiki, BBcode etc
2004-09-23 02:48:41 +00:00
/**
* Does all sorts of transformations and filtering
*/
2004-09-22 14:39:15 +00:00
define ( 'FORMAT_MOODLE' , '0' ); // Does all sorts of transformations and filtering
2004-09-23 02:48:41 +00:00
/**
* Plain HTML ( with some tags stripped )
*/
2004-09-22 14:39:15 +00:00
define ( 'FORMAT_HTML' , '1' ); // Plain HTML (with some tags stripped)
2004-09-23 02:48:41 +00:00
/**
* Plain text ( even tags are printed in full )
*/
2004-09-22 14:39:15 +00:00
define ( 'FORMAT_PLAIN' , '2' ); // Plain text (even tags are printed in full)
2004-09-23 02:48:41 +00:00
/**
* Wiki - formatted text
2005-04-11 13:46:06 +00:00
* Deprecated : left here just to note that '3' is not used ( at the moment )
* and to catch any latent wiki - like text ( which generates an error )
2004-09-23 02:48:41 +00:00
*/
2004-09-22 14:39:15 +00:00
define ( 'FORMAT_WIKI' , '3' ); // Wiki-formatted text
2004-09-23 02:48:41 +00:00
/**
* Markdown - formatted text http :// daringfireball . net / projects / markdown /
*/
2004-09-22 14:39:15 +00:00
define ( 'FORMAT_MARKDOWN' , '4' ); // Markdown-formatted text http://daringfireball.net/projects/markdown/
2002-10-10 07:26:10 +00:00
2006-08-26 13:00:07 +00:00
/**
* TRUSTTEXT marker - if present in text , text cleaning should be bypassed
*/
define ( 'TRUSTTEXT' , '#####TRUSTTEXT#####' );
2004-09-23 02:48:41 +00:00
/**
* Allowed tags - string of html tags that can be tested against for safe html tags
* @ global string $ALLOWED_TAGS
*/
2003-10-16 14:25:36 +00:00
$ALLOWED_TAGS =
2005-06-05 05:40:24 +00:00
'<p><br><b><i><u><font><table><tbody><span><div><tr><td><th><ol><ul><dl><li><dt><dd><h1><h2><h3><h4><h5><h6><hr><img><a><strong><emphasis><em><sup><sub><address><cite><blockquote><pre><strike><param><acronym><nolink><lang><tex><algebra><math><mi><mn><mo><mtext><mspace><ms><mrow><mfrac><msqrt><mroot><mstyle><merror><mpadded><mphantom><mfenced><msub><msup><msubsup><munder><mover><munderover><mmultiscripts><mtable><mtr><mtd><maligngroup><malignmark><maction><cn><ci><apply><reln><fn><interval><inverse><sep><condition><declare><lambda><compose><ident><quotient><exp><factorial><divide><max><min><minus><plus><power><rem><times><root><gcd><and><or><xor><not><implies><forall><exists><abs><conjugate><eq><neq><gt><lt><geq><leq><ln><log><int><diff><partialdiff><lowlimit><uplimit><bvar><degree><set><list><union><intersect><in><notin><subset><prsubset><notsubset><notprsubset><setdiff><sum><product><limit><tendsto><mean><sdev><variance><median><mode><moment><vector><matrix><matrixrow><determinant><transpose><selector><annotation><semantics><annotation-xml><tt><code>' ;
2004-09-29 14:00:35 +00:00
/**
* Allowed protocols - array of protocols that are safe to use in links and so on
* @ global string $ALLOWED_PROTOCOLS
*/
2005-05-28 13:28:20 +00:00
$ALLOWED_PROTOCOLS = array ( 'http' , 'https' , 'ftp' , 'news' , 'mailto' , 'rtsp' , 'teamspeak' , 'gopher' , 'mms' ,
2006-03-04 16:04:48 +00:00
'color' , 'callto' , 'cursor' , 'text-align' , 'font-size' , 'font-weight' , 'font-style' ,
2006-08-09 19:31:09 +00:00
'border' , 'margin' , 'padding' , 'background' ); // CSS as well to get through kses
2004-09-29 14:00:35 +00:00
2002-10-10 07:26:10 +00:00
/// Functions
2004-09-23 02:48:41 +00:00
/**
* Add quotes to HTML characters
*
* Returns $var with HTML characters ( like " < " , " > " , etc . ) properly quoted .
* This function is very similar to { @ link p ()}
*
* @ param string $var the string potentially containing HTML characters
2006-04-12 17:39:23 +00:00
* @ param boolean $strip to decide if we want to strip slashes or no . Default to false .
* true should be used to print data from forms and false for data from DB .
2004-09-23 02:48:41 +00:00
* @ return string
*/
2006-04-12 17:39:23 +00:00
function s ( $var , $strip = false ) {
2005-07-09 07:20:52 +00:00
if ( $var == '0' ) { // for integer 0, boolean false, string '0'
return '0' ;
2003-05-26 14:43:19 +00:00
}
2006-04-12 17:39:23 +00:00
if ( $strip ) {
return preg_replace ( " /&(# \ d+);/i " , " & $ 1; " , htmlspecialchars ( stripslashes_safe ( $var )));
} else {
return preg_replace ( " /&(# \ d+);/i " , " & $ 1; " , htmlspecialchars ( $var ));
}
2001-11-22 06:23:56 +00:00
}
2004-09-23 02:48:41 +00:00
/**
* Add quotes to HTML characters
*
2004-09-23 04:36:43 +00:00
* Prints $var with HTML characters ( like " < " , " > " , etc . ) properly quoted .
2004-09-23 02:48:41 +00:00
* This function is very similar to { @ link s ()}
*
* @ param string $var the string potentially containing HTML characters
2006-04-12 17:39:23 +00:00
* @ param boolean $strip to decide if we want to strip slashes or no . Default to false .
* true should be used to print data from forms and false for data from DB .
2004-09-23 02:48:41 +00:00
* @ return string
*/
2006-04-12 17:39:23 +00:00
function p ( $var , $strip = false ) {
echo s ( $var , $strip );
2001-11-22 06:23:56 +00:00
}
2004-09-23 02:48:41 +00:00
/**
* Ensure that a variable is set
*
2005-01-26 15:57:00 +00:00
* Return $var if it is defined , otherwise return $default ,
2004-09-23 02:48:41 +00:00
* This function is very similar to { @ link optional_variable ()}
*
* @ param mixed $var the variable which may be unset
* @ param mixed $default the value to return if $var is unset
* @ return mixed
*/
2004-09-22 14:39:15 +00:00
function nvl ( & $var , $default = '' ) {
2006-05-18 22:12:06 +00:00
global $CFG ;
2002-07-11 05:30:57 +00:00
2006-05-18 22:12:06 +00:00
if ( ! empty ( $CFG -> disableglobalshack )) {
error ( " The nvl() function is deprecated ( $var , $default ). " );
}
2002-07-11 05:30:57 +00:00
return isset ( $var ) ? $var : $default ;
}
2001-11-22 06:23:56 +00:00
2004-09-23 02:48:41 +00:00
/**
* Remove query string from url
*
* Takes in a URL and returns it without the querystring portion
*
* @ param string $url the url which may have a query string attached
* @ return string
*/
function strip_querystring ( $url ) {
2001-11-22 06:23:56 +00:00
2002-12-30 03:39:42 +00:00
if ( $commapos = strpos ( $url , '?' )) {
return substr ( $url , 0 , $commapos );
} else {
return $url ;
}
2001-11-22 06:23:56 +00:00
}
2004-09-23 02:48:41 +00:00
/**
* Returns the URL of the HTTP_REFERER , less the querystring portion
* @ return string
*/
2001-11-22 06:23:56 +00:00
function get_referer () {
2004-09-22 14:39:15 +00:00
return strip_querystring ( nvl ( $_SERVER [ 'HTTP_REFERER' ]));
2001-11-22 06:23:56 +00:00
}
2002-10-19 04:58:14 +00:00
2004-09-23 02:48:41 +00:00
/**
* Returns the name of the current script , WITH the querystring portion .
* this function is necessary because PHP_SELF and REQUEST_URI and SCRIPT_NAME
* return different things depending on a lot of things like your OS , Web
* server , and the way PHP is compiled ( ie . as a CGI , module , ISAPI , etc . )
2004-09-23 04:36:43 +00:00
* < b > NOTE :</ b > This function returns false if the global variables needed are not set .
*
2004-09-23 02:48:41 +00:00
* @ return string
*/
function me () {
2001-11-22 06:23:56 +00:00
2004-09-22 14:39:15 +00:00
if ( ! empty ( $_SERVER [ 'REQUEST_URI' ])) {
return $_SERVER [ 'REQUEST_URI' ];
2002-10-19 04:58:14 +00:00
2004-09-22 14:39:15 +00:00
} else if ( ! empty ( $_SERVER [ 'PHP_SELF' ])) {
if ( ! empty ( $_SERVER [ 'QUERY_STRING' ])) {
return $_SERVER [ 'PHP_SELF' ] . '?' . $_SERVER [ 'QUERY_STRING' ];
2003-01-30 15:52:38 +00:00
}
2004-09-22 14:39:15 +00:00
return $_SERVER [ 'PHP_SELF' ];
2002-10-19 04:58:14 +00:00
2004-09-22 14:39:15 +00:00
} else if ( ! empty ( $_SERVER [ 'SCRIPT_NAME' ])) {
if ( ! empty ( $_SERVER [ 'QUERY_STRING' ])) {
return $_SERVER [ 'SCRIPT_NAME' ] . '?' . $_SERVER [ 'QUERY_STRING' ];
2003-01-30 15:52:38 +00:00
}
2004-09-22 14:39:15 +00:00
return $_SERVER [ 'SCRIPT_NAME' ];
2003-01-30 15:52:38 +00:00
2004-09-22 14:39:15 +00:00
} else if ( ! empty ( $_SERVER [ 'URL' ])) { // May help IIS (not well tested)
if ( ! empty ( $_SERVER [ 'QUERY_STRING' ])) {
return $_SERVER [ 'URL' ] . '?' . $_SERVER [ 'QUERY_STRING' ];
2004-07-06 12:34:33 +00:00
}
2004-09-22 14:39:15 +00:00
return $_SERVER [ 'URL' ];
2004-07-06 12:34:33 +00:00
2002-12-30 03:39:42 +00:00
} else {
2004-09-22 14:39:15 +00:00
notify ( 'Warning: Could not find any of these web server variables: $REQUEST_URI, $PHP_SELF, $SCRIPT_NAME or $URL' );
2003-02-05 10:52:55 +00:00
return false ;
2002-10-02 02:05:29 +00:00
}
2001-11-22 06:23:56 +00:00
}
2004-09-23 02:48:41 +00:00
/**
2004-09-23 04:36:43 +00:00
* Like { @ link me ()} but returns a full URL
2004-09-23 02:48:41 +00:00
* @ see me ()
* @ return string
*/
2001-11-22 06:23:56 +00:00
function qualified_me () {
2005-02-28 09:04:37 +00:00
global $CFG ;
2005-03-01 00:11:35 +00:00
if ( ! empty ( $CFG -> wwwroot )) {
$url = parse_url ( $CFG -> wwwroot );
}
2005-02-28 09:04:37 +00:00
if ( ! empty ( $url [ 'host' ])) {
$hostname = $url [ 'host' ];
} else if ( ! empty ( $_SERVER [ 'SERVER_NAME' ])) {
2004-09-22 14:39:15 +00:00
$hostname = $_SERVER [ 'SERVER_NAME' ];
} else if ( ! empty ( $_ENV [ 'SERVER_NAME' ])) {
$hostname = $_ENV [ 'SERVER_NAME' ];
} else if ( ! empty ( $_SERVER [ 'HTTP_HOST' ])) {
$hostname = $_SERVER [ 'HTTP_HOST' ];
} else if ( ! empty ( $_ENV [ 'HTTP_HOST' ])) {
$hostname = $_ENV [ 'HTTP_HOST' ];
2003-01-10 05:40:35 +00:00
} else {
2004-09-22 14:39:15 +00:00
notify ( 'Warning: could not find the name of this server!' );
2003-02-05 10:52:55 +00:00
return false ;
2002-10-19 04:58:14 +00:00
}
2005-04-14 13:56:29 +00:00
if ( ! empty ( $url [ 'port' ])) {
$hostname .= ':' . $url [ 'port' ];
} else if ( ! empty ( $_SERVER [ 'SERVER_PORT' ])) {
if ( $_SERVER [ 'SERVER_PORT' ] != 80 && $_SERVER [ 'SERVER_PORT' ] != 443 ) {
$hostname .= ':' . $_SERVER [ 'SERVER_PORT' ];
}
}
2004-09-14 08:41:17 +00:00
if ( isset ( $_SERVER [ 'HTTPS' ])) {
$protocol = ( $_SERVER [ 'HTTPS' ] == 'on' ) ? 'https://' : 'http://' ;
} else if ( isset ( $_SERVER [ 'SERVER_PORT' ])) { # Apache2 does not export $_SERVER['HTTPS']
$protocol = ( $_SERVER [ 'SERVER_PORT' ] == '443' ) ? 'https://' : 'http://' ;
} else {
$protocol = 'http://' ;
}
2001-11-22 06:23:56 +00:00
2003-01-10 05:40:35 +00:00
$url_prefix = $protocol . $hostname ;
2002-12-30 03:39:42 +00:00
return $url_prefix . me ();
2001-11-22 06:23:56 +00:00
}
2004-09-23 02:48:41 +00:00
/**
* Determine if a web referer is valid
*
* Returns true if the referer is the same as the goodreferer . If
* the referer to test is not specified , use { @ link qualified_me ()} .
* If the admin has not set secure forms ( $CFG -> secureforms ) then
* this function returns true regardless of a match .
2004-09-23 04:36:43 +00:00
*
2004-09-23 02:48:41 +00:00
* @ uses $CFG
* @ param string $goodreferer the url to compare to referer
* @ return boolean
*/
2004-09-22 14:39:15 +00:00
function match_referer ( $goodreferer = '' ) {
2002-11-17 10:45:34 +00:00
global $CFG ;
2003-05-15 15:59:52 +00:00
if ( empty ( $CFG -> secureforms )) { // Don't bother checking referer
2002-11-17 10:45:34 +00:00
return true ;
}
2001-11-22 06:23:56 +00:00
2004-09-22 14:39:15 +00:00
if ( $goodreferer == 'nomatch' ) { // Don't bother checking referer
2003-05-12 09:22:00 +00:00
return true ;
}
2004-05-21 13:07:11 +00:00
if ( empty ( $goodreferer )) {
$goodreferer = qualified_me ();
2006-05-14 21:21:35 +00:00
// try to remove everything after ? because POST url may contain GET parameters (SID rewrite, etc.)
2006-05-17 16:47:44 +00:00
$pos = strpos ( $goodreferer , '?' );
2006-05-14 21:21:35 +00:00
if ( $pos !== FALSE ) {
$goodreferer = substr ( $goodreferer , 0 , $pos );
}
2002-10-19 04:58:14 +00:00
}
2004-09-09 10:26:11 +00:00
2004-09-01 11:24:35 +00:00
$referer = get_referer ();
2005-11-10 21:41:10 +00:00
return (( $referer == $goodreferer ) or ( $referer == $CFG -> wwwroot . '/' ) or ( $referer == $CFG -> wwwroot . '/index.php' ));
2001-11-22 06:23:56 +00:00
}
2004-09-23 02:48:41 +00:00
/**
* Determine if there is data waiting to be processed from a form
*
* Used on most forms in Moodle to check for data
* Returns the data as an object , if it ' s found .
* This object can be used in foreach loops without
* casting because it ' s cast to ( array ) automatically
2005-01-26 15:57:00 +00:00
*
2004-09-23 02:48:41 +00:00
* Checks that submitted POST data exists , and also
* checks the referer against the given url ( it uses
2004-09-23 04:36:43 +00:00
* the current page if none was specified .
*
2004-09-23 02:48:41 +00:00
* @ uses $CFG
* @ param string $url the url to compare to referer for secure forms
* @ return boolean
*/
2004-09-22 14:39:15 +00:00
function data_submitted ( $url = '' ) {
2004-09-23 04:36:43 +00:00
2003-01-02 14:49:23 +00:00
2003-01-20 07:13:02 +00:00
global $CFG ;
2003-01-05 06:45:20 +00:00
if ( empty ( $_POST )) {
2003-01-02 14:49:23 +00:00
return false ;
2003-01-05 06:45:20 +00:00
2003-01-02 14:49:23 +00:00
} else {
if ( match_referer ( $url )) {
2003-01-05 06:45:20 +00:00
return ( object ) $_POST ;
2003-01-02 14:49:23 +00:00
} else {
2006-09-13 09:45:07 +00:00
debugging ( 'The form did not come from this page! (referer = ' . get_referer () . ')' );
2003-01-02 14:49:23 +00:00
return false ;
}
}
}
2004-09-23 02:48:41 +00:00
/**
2006-04-13 21:27:01 +00:00
* Moodle replacement for php stripslashes () function ,
* works also for objects and arrays .
2004-09-23 02:48:41 +00:00
*
2005-01-26 15:57:00 +00:00
* The standard php stripslashes () removes ALL backslashes
2004-09-23 02:48:41 +00:00
* even from strings - so C : \temp becomes C : temp - this isn ' t good .
* This function should work as a fairly safe replacement
* to be called on quoted AND unquoted strings ( to be sure )
2004-09-23 04:36:43 +00:00
*
2006-04-13 21:27:01 +00:00
* @ param mixed something to remove unsafe slashes from
* @ return mixed
2004-09-23 02:48:41 +00:00
*/
2006-04-13 21:27:01 +00:00
function stripslashes_safe ( $mixed ) {
// there is no need to remove slashes from int, float and bool types
if ( empty ( $mixed )) {
//nothing to do...
} else if ( is_string ( $mixed )) {
2006-08-27 21:52:22 +00:00
if ( ini_get_bool ( 'magic_quotes_sybase' )) { //only unescape single quotes
$mixed = str_replace ( " '' " , " ' " , $mixed );
} else { //the rest, simple and double quotes and backslashes
$mixed = str_replace ( " \\ ' " , " ' " , $mixed );
$mixed = str_replace ( '\\"' , '"' , $mixed );
$mixed = str_replace ( '\\\\' , '\\' , $mixed );
}
2006-04-13 21:27:01 +00:00
} else if ( is_array ( $mixed )) {
foreach ( $mixed as $key => $value ) {
$mixed [ $key ] = stripslashes_safe ( $value );
}
} else if ( is_object ( $mixed )) {
$vars = get_object_vars ( $mixed );
foreach ( $vars as $key => $value ) {
$mixed -> $key = stripslashes_safe ( $value );
}
}
2003-01-23 13:07:49 +00:00
2006-04-13 21:27:01 +00:00
return $mixed ;
2003-01-23 13:07:49 +00:00
}
2001-11-22 06:23:56 +00:00
2004-10-19 21:04:28 +00:00
/**
* Recursive implementation of stripslashes ()
*
* This function will allow you to strip the slashes from a variable .
* If the variable is an array or object , slashes will be stripped
* from the items ( or properties ) it contains , even if they are arrays
* or objects themselves .
*
* @ param mixed the variable to remove slashes from
* @ return mixed
*/
function stripslashes_recursive ( $var ) {
if ( is_object ( $var )) {
$properties = get_object_vars ( $var );
foreach ( $properties as $property => $value ) {
$var -> $property = stripslashes_recursive ( $value );
}
}
else if ( is_array ( $var )) {
foreach ( $var as $property => $value ) {
$var [ $property ] = stripslashes_recursive ( $value );
}
}
else if ( is_string ( $var )) {
$var = stripslashes ( $var );
}
return $var ;
}
2004-09-23 02:48:41 +00:00
/**
2004-09-23 04:36:43 +00:00
* Given some normal text this function will break up any
* long words to a given size by inserting the given character
*
2006-01-04 08:23:42 +00:00
* It 's multibyte savvy and doesn' t change anything inside html tags .
*
2004-09-23 02:48:41 +00:00
* @ param string $string the string to be modified
2004-09-25 05:29:21 +00:00
* @ param int $maxsize maximum length of the string to be returned
2004-09-23 02:48:41 +00:00
* @ param string $cutchar the string used to represent word breaks
* @ return string
*/
2004-03-10 02:19:17 +00:00
function break_up_long_words ( $string , $maxsize = 20 , $cutchar = ' ' ) {
2005-04-03 12:15:45 +00:00
2006-01-04 08:23:42 +00:00
/// Loading the textlib singleton instance. We are going to need it.
$textlib = textlib_get_instance ();
2005-03-10 13:30:57 +00:00
2006-01-04 08:23:42 +00:00
/// First of all, save all the tags inside the text to skip them
$tags = array ();
filter_save_tags ( $string , $tags );
2004-07-07 17:45:42 +00:00
2006-01-04 08:23:42 +00:00
/// Process the string adding the cut when necessary
2004-03-10 02:19:17 +00:00
$output = '' ;
2006-01-04 08:23:42 +00:00
$length = $textlib -> strlen ( $string , current_charset ());
2004-03-10 02:19:17 +00:00
$wordlength = 0 ;
for ( $i = 0 ; $i < $length ; $i ++ ) {
2006-01-04 08:23:42 +00:00
$char = $textlib -> substr ( $string , $i , 1 , current_charset ());
if ( $char == ' ' or $char == " \t " or $char == " \n " or $char == " \r " or $char == " < " or $char == " > " ) {
2004-03-10 02:19:17 +00:00
$wordlength = 0 ;
} else {
$wordlength ++ ;
if ( $wordlength > $maxsize ) {
$output .= $cutchar ;
$wordlength = 0 ;
}
}
$output .= $char ;
}
2006-01-04 08:23:42 +00:00
/// Finally load the tags back again
if ( ! empty ( $tags )) {
$output = str_replace ( array_keys ( $tags ), $tags , $output );
}
2004-03-10 02:19:17 +00:00
return $output ;
}
2004-09-23 02:48:41 +00:00
/**
* This does a search and replace , ignoring case
* This function is only used for versions of PHP older than version 5
* which do not have a native version of this function .
* Taken from the PHP manual , by bradhuizenga @ softhome . net
2004-09-23 04:36:43 +00:00
*
2004-09-23 02:48:41 +00:00
* @ param string $find the string to search for
* @ param string $replace the string to replace $find with
* @ param string $string the string to search through
* return string
*/
2004-01-20 08:51:43 +00:00
if ( ! function_exists ( 'str_ireplace' )) { /// Only exists in PHP 5
2003-10-16 09:28:27 +00:00
function str_ireplace ( $find , $replace , $string ) {
if ( ! is_array ( $find )) {
$find = array ( $find );
}
if ( ! is_array ( $replace )) {
if ( ! is_array ( $find )) {
$replace = array ( $replace );
} else {
// this will duplicate the string into an array the size of $find
$c = count ( $find );
$rString = $replace ;
unset ( $replace );
for ( $i = 0 ; $i < $c ; $i ++ ) {
$replace [ $i ] = $rString ;
}
}
}
foreach ( $find as $fKey => $fItem ) {
$between = explode ( strtolower ( $fItem ), strtolower ( $string ));
$pos = 0 ;
foreach ( $between as $bKey => $bItem ) {
$between [ $bKey ] = substr ( $string , $pos , strlen ( $bItem ));
$pos += strlen ( $bItem ) + strlen ( $fItem );
}
$string = implode ( $replace [ $fKey ], $between );
2003-06-06 09:46:27 +00:00
}
2003-10-16 09:28:27 +00:00
return ( $string );
2002-10-18 09:09:19 +00:00
}
}
2004-09-23 02:48:41 +00:00
/**
* Locate the position of a string in another string
*
* This function is only used for versions of PHP older than version 5
* which do not have a native version of this function .
* Taken from the PHP manual , by dmarsh @ spscc . ctc . edu
2004-09-23 04:36:43 +00:00
*
2004-09-23 02:48:41 +00:00
* @ param string $haystack The string to be searched
* @ param string $needle The string to search for
2004-09-25 05:29:21 +00:00
* @ param int $offset The position in $haystack where the search should begin .
2004-09-23 02:48:41 +00:00
*/
2004-01-20 08:51:43 +00:00
if ( ! function_exists ( 'stripos' )) { /// Only exists in PHP 5
function stripos ( $haystack , $needle , $offset = 0 ) {
2004-09-23 04:36:43 +00:00
2004-01-20 08:51:43 +00:00
return strpos ( strtoupper ( $haystack ), strtoupper ( $needle ), $offset );
}
}
2004-09-23 02:48:41 +00:00
/**
* Load a template from file
*
* Returns a ( big ) string containing the contents of a template file with all
* the variables interpolated . all the variables must be in the $var [] array or
* object ( whatever you decide to use ) .
*
* < b > WARNING : do not use this on big files !!</ b >
2004-09-23 04:36:43 +00:00
*
2004-09-23 02:48:41 +00:00
* @ param string $filename Location on the server ' s filesystem where template can be found .
* @ param mixed $var Passed in by reference . An array or object which will be loaded with data from the template file .
*/
2001-11-22 06:23:56 +00:00
function read_template ( $filename , & $var ) {
2004-09-22 14:39:15 +00:00
$temp = str_replace ( " \\ " , " \\ \\ " , implode ( file ( $filename ), '' ));
2002-12-30 03:39:42 +00:00
$temp = str_replace ( '"' , '\"' , $temp );
eval ( " \$ template = \" $temp\ " ; " );
return $template ;
2001-11-22 06:23:56 +00:00
}
2004-09-23 02:48:41 +00:00
/**
* Set a variable ' s value depending on whether or not it already has a value .
*
2005-01-26 15:57:00 +00:00
* If variable is set , set it to the set_value otherwise set it to the
2004-09-23 02:48:41 +00:00
* unset_value . used to handle checkboxes when you are expecting them from
* a form
2004-09-23 04:36:43 +00:00
*
2004-09-23 02:48:41 +00:00
* @ param mixed $var Passed in by reference . The variable to check .
* @ param mixed $set_value The value to set $var to if $var already has a value .
* @ param mixed $unset_value The value to set $var to if $var does not already have a value .
*/
2001-11-22 06:23:56 +00:00
function checked ( & $var , $set_value = 1 , $unset_value = 0 ) {
2002-12-30 03:39:42 +00:00
if ( empty ( $var )) {
$var = $unset_value ;
} else {
$var = $set_value ;
}
2001-11-22 06:23:56 +00:00
}
2004-09-23 02:48:41 +00:00
/**
* Prints the word " checked " if a variable is true , otherwise prints nothing ,
2004-09-23 04:36:43 +00:00
* used for printing the word " checked " in a checkbox form element .
*
2004-09-23 02:48:41 +00:00
* @ param boolean $var Variable to be checked for true value
* @ param string $true_value Value to be printed if $var is true
* @ param string $false_value Value to be printed if $var is false
*/
2004-09-22 14:39:15 +00:00
function frmchecked ( & $var , $true_value = 'checked' , $false_value = '' ) {
2001-11-22 06:23:56 +00:00
2002-12-30 03:39:42 +00:00
if ( $var ) {
echo $true_value ;
} else {
echo $false_value ;
}
2001-11-22 06:23:56 +00:00
}
2004-09-23 02:48:41 +00:00
/**
* This function will create a HTML link that will work on both
* Javascript and non - javascript browsers .
* Relies on the Javascript function openpopup in javascript . php
2004-09-23 04:36:43 +00:00
*
2004-09-23 02:48:41 +00:00
* $url must be relative to home page eg / mod / survey / stuff . php
* @ param string $url Web link relative to home page
* @ param string $name Name to be assigned to the popup window
* @ param string $linkname Text to be displayed as web link
2004-09-25 05:29:21 +00:00
* @ param int $height Height to assign to popup window
* @ param int $width Height to assign to popup window
2004-09-23 02:48:41 +00:00
* @ param string $title Text to be displayed as popup page title
* @ param string $options List of additional options for popup window
* @ todo Add code examples and list of some options that might be used .
* @ param boolean $return Should the link to the popup window be returned as a string ( true ) or printed immediately ( false ) ?
* @ return string
* @ uses $CFG
*/
2004-09-22 14:39:15 +00:00
function link_to_popup_window ( $url , $name = 'popup' , $linkname = 'click here' ,
2006-08-10 06:41:38 +00:00
$height = 400 , $width = 500 , $title = 'Popup window' ,
$options = 'none' , $return = false ) {
2001-11-22 06:23:56 +00:00
2002-10-14 16:00:55 +00:00
global $CFG ;
2004-09-22 14:39:15 +00:00
if ( $options == 'none' ) {
$options = 'menubar=0,location=0,scrollbars,resizable,width=' . $width . ',height=' . $height ;
2003-09-03 12:13:08 +00:00
}
2003-07-20 13:53:31 +00:00
$fullscreen = 0 ;
2001-11-22 06:23:56 +00:00
2004-09-17 00:10:53 +00:00
if ( ! ( strpos ( $url , $CFG -> wwwroot ) === false )) { // some log url entries contain _SERVER[HTTP_REFERRER] in which case wwwroot is already there.
2006-04-05 02:45:13 +00:00
$url = substr ( $url , strlen ( $CFG -> wwwroot ));
2004-09-17 00:10:53 +00:00
}
2004-09-22 14:39:15 +00:00
$link = '<a target="' . $name . '" title="' . $title . '" href="' . $CFG -> wwwroot . $url . '" ' .
2005-01-05 17:40:38 +00:00
" onclick= \" return openpopup(' $url ', ' $name ', ' $options ', $fullscreen ); \" > $linkname </a> " ;
2004-08-02 19:09:06 +00:00
if ( $return ) {
return $link ;
} else {
echo $link ;
}
2001-11-22 06:23:56 +00:00
}
2004-09-23 02:48:41 +00:00
/**
* This function will print a button submit form element
* that will work on both Javascript and non - javascript browsers .
* Relies on the Javascript function openpopup in javascript . php
2004-09-23 04:36:43 +00:00
*
2004-09-23 02:48:41 +00:00
* $url must be relative to home page eg / mod / survey / stuff . php
* @ param string $url Web link relative to home page
* @ param string $name Name to be assigned to the popup window
* @ param string $linkname Text to be displayed as web link
2004-09-25 05:29:21 +00:00
* @ param int $height Height to assign to popup window
* @ param int $width Height to assign to popup window
2004-09-23 02:48:41 +00:00
* @ param string $title Text to be displayed as popup page title
* @ param string $options List of additional options for popup window
2005-04-14 13:56:29 +00:00
* @ param string $return If true , return as a string , otherwise print
2004-09-23 02:48:41 +00:00
* @ return string
* @ uses $CFG
*/
2004-09-22 14:39:15 +00:00
function button_to_popup_window ( $url , $name = 'popup' , $linkname = 'click here' ,
2005-04-20 19:12:28 +00:00
$height = 400 , $width = 500 , $title = 'Popup window' , $options = 'none' , $return = false ,
2005-04-15 13:41:57 +00:00
$id = '' , $class = '' ) {
2004-02-15 08:04:33 +00:00
global $CFG ;
2004-09-22 14:39:15 +00:00
if ( $options == 'none' ) {
$options = 'menubar=0,location=0,scrollbars,resizable,width=' . $width . ',height=' . $height ;
2004-02-15 08:04:33 +00:00
}
2005-04-15 13:41:57 +00:00
if ( $id ) {
$id = ' id="' . $id . '" ' ;
}
if ( $class ) {
$class = ' class="' . $class . '" ' ;
}
2004-02-15 08:04:33 +00:00
$fullscreen = 0 ;
2005-04-15 13:41:57 +00:00
$button = '<input type="button" name="' . $name . '" title="' . $title . '" value="' . $linkname . ' ..." ' . $id . $class .
2005-04-14 13:56:29 +00:00
" onclick= \" return openpopup(' $url ', ' $name ', ' $options ', $fullscreen ); \" /> \n " ;
if ( $return ) {
return $button ;
} else {
echo $button ;
}
2004-02-15 08:04:33 +00:00
}
2004-09-23 02:48:41 +00:00
/**
* Prints a simple button to close a window
*/
2006-08-10 04:33:55 +00:00
function close_window_button ( $name = 'closewindow' , $return = false ) {
$output = '' ;
$output .= '<center>' . " \n " ;
$output .= '<script type="text/javascript">' . " \n " ;
$output .= '<!--' . " \n " ;
$output .= " document.write('<form>'); \n " ;
$output .= " document.write('<input type= \" button \" onclick= \" self.close(); \" value= \" " . get_string ( " closewindow " ) . " \" />'); \n " ;
$output .= " document.write('< \ /form>'); \n " ;
$output .= '-->' . " \n " ;
$output .= '</script>' . " \n " ;
$output .= '<noscript>' . " \n " ;
$output .= get_string ( $name );
$output .= '</noscript>' . " \n " ;
$output .= '</center>' . " \n " ;
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2001-11-22 06:23:56 +00:00
}
2005-03-22 17:31:04 +00:00
/*
* Try and close the current window immediately using Javascript
*/
function close_window ( $delay = 0 ) {
2006-10-31 11:59:54 +00:00
?>
< script type = " text/javascript " >
<!--
function close_this_window () {
self . close ();
}
setTimeout ( " close_this_window() " , < ? php echo $delay * 1000 ?> );
-->
</ script >
< noscript >< center >
< ? php print_string ( 'pleaseclose' ) ?>
</ center ></ noscript >
< ? php
die ;
2005-03-22 17:31:04 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Given an array of value , creates a popup menu to be part of a form
* $options [ " value " ][ " label " ]
*
* @ param type description
* @ todo Finish documenting this function
*/
2006-03-04 16:04:48 +00:00
function choose_from_menu ( $options , $name , $selected = '' , $nothing = 'choose' , $script = '' ,
2005-11-03 05:12:12 +00:00
$nothingvalue = '0' , $return = false , $disabled = false , $tabindex = 0 ) {
2004-05-21 13:07:11 +00:00
2004-09-22 14:39:15 +00:00
if ( $nothing == 'choose' ) {
$nothing = get_string ( 'choose' ) . '...' ;
2002-08-12 07:40:53 +00:00
}
2005-02-12 21:41:22 +00:00
$attributes = ( $script ) ? 'onchange="' . $script . '"' : '' ;
if ( $disabled ) {
$attributes .= ' disabled="disabled"' ;
2001-11-22 06:23:56 +00:00
}
2002-12-29 17:32:32 +00:00
2005-11-03 05:12:12 +00:00
if ( $tabindex ) {
$attributes .= ' tabindex="' . $tabindex . '"' ;
}
2006-08-10 18:58:26 +00:00
$id = str_replace ( '[]' , '' , $name ); // name may end in [], which would make an invalid id. e.g. numeric question type editing form.
$output = '<select id="menu' . $id . '" name="' . $name . '" ' . $attributes . '>' . " \n " ;
2002-06-05 05:37:55 +00:00
if ( $nothing ) {
2004-09-22 14:39:15 +00:00
$output .= ' <option value="' . $nothingvalue . '"' . " \n " ;
2004-04-27 13:57:46 +00:00
if ( $nothingvalue === $selected ) {
2004-09-22 14:39:15 +00:00
$output .= ' selected="selected"' ;
2002-06-05 05:37:55 +00:00
}
2004-09-22 14:39:15 +00:00
$output .= '>' . $nothing . '</option>' . " \n " ;
2002-06-05 03:15:30 +00:00
}
2003-01-05 06:45:20 +00:00
if ( ! empty ( $options )) {
foreach ( $options as $value => $label ) {
2004-09-22 14:39:15 +00:00
$output .= ' <option value="' . $value . '"' ;
2006-01-16 09:05:23 +00:00
if (( string ) $value == ( string ) $selected ) {
2004-09-22 14:39:15 +00:00
$output .= ' selected="selected"' ;
2003-01-05 06:45:20 +00:00
}
2004-09-22 14:39:15 +00:00
if ( $label === '' ) {
$output .= '>' . $value . '</option>' . " \n " ;
2003-08-25 02:32:20 +00:00
} else {
2004-09-22 14:39:15 +00:00
$output .= '>' . $label . '</option>' . " \n " ;
2003-01-05 06:45:20 +00:00
}
2001-11-22 06:23:56 +00:00
}
}
2004-09-22 14:39:15 +00:00
$output .= '</select>' . " \n " ;
2002-09-19 14:51:49 +00:00
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2004-05-21 13:07:11 +00:00
}
2001-11-22 06:23:56 +00:00
2006-08-31 16:55:48 +00:00
/**
* Choose value 0 or 1 from a menu with options 'No' and 'Yes' .
* Other options like choose_from_menu .
*/
function choose_from_menu_yesno ( $name , $selected , $script = '' ,
$return = false , $disabled = false , $tabindex = 0 ) {
return choose_from_menu ( array ( get_string ( 'no' ), get_string ( 'yes' )), $name ,
$selected , '' , $script , '0' , $return , $disabled , $tabindex );
}
2006-01-11 02:22:16 +00:00
/**
* Just like choose_from_menu , but takes a nested array ( 2 levels ) and makes a dropdown menu
* including option headings with the first level .
*/
function choose_from_menu_nested ( $options , $name , $selected = '' , $nothing = 'choose' , $script = '' ,
$nothingvalue = 0 , $return = false , $disabled = false , $tabindex = 0 ) {
if ( $nothing == 'choose' ) {
$nothing = get_string ( 'choose' ) . '...' ;
}
$attributes = ( $script ) ? 'onchange="' . $script . '"' : '' ;
if ( $disabled ) {
$attributes .= ' disabled="disabled"' ;
}
if ( $tabindex ) {
$attributes .= ' tabindex="' . $tabindex . '"' ;
}
$output = '<select id="menu' . $name . '" name="' . $name . '" ' . $attributes . '>' . " \n " ;
if ( $nothing ) {
$output .= ' <option value="' . $nothingvalue . '"' . " \n " ;
if ( $nothingvalue === $selected ) {
$output .= ' selected="selected"' ;
}
$output .= '>' . $nothing . '</option>' . " \n " ;
}
if ( ! empty ( $options )) {
foreach ( $options as $section => $values ) {
$output .= ' <optgroup label="' . $section . '">' . " \n " ;
foreach ( $values as $value => $label ) {
$output .= ' <option value="' . $value . '"' ;
2006-01-16 21:50:57 +00:00
if (( string ) $value == ( string ) $selected ) {
2006-01-11 02:22:16 +00:00
$output .= ' selected="selected"' ;
}
if ( $label === '' ) {
$output .= '>' . $value . '</option>' . " \n " ;
} else {
$output .= '>' . $label . '</option>' . " \n " ;
}
}
$output .= ' </optgroup>' . " \n " ;
}
}
$output .= '</select>' . " \n " ;
if ( $return ) {
return $output ;
} else {
echo $output ;
}
}
2005-06-27 00:17:39 +00:00
/**
* Given an array of values , creates a group of radio buttons to be part of a form
2006-03-04 16:04:48 +00:00
*
2005-06-27 00:17:39 +00:00
* @ param array $options An array of value - label pairs for the radio group ( values as keys )
* @ param string $name Name of the radiogroup ( unique in the form )
* @ param string $checked The value that is already checked
*/
2006-08-10 04:33:55 +00:00
function choose_from_radio ( $options , $name , $checked = '' , $return = false ) {
2005-06-27 00:17:39 +00:00
2005-07-01 03:57:03 +00:00
static $idcounter = 0 ;
2005-06-27 00:17:39 +00:00
if ( ! $name ) {
$name = 'unnamed' ;
}
$output = '<span class="radiogroup ' . $name . " \" > \n " ;
if ( ! empty ( $options )) {
$currentradio = 0 ;
foreach ( $options as $value => $label ) {
2005-07-01 03:57:03 +00:00
$htmlid = 'auto-rb' . sprintf ( '%04d' , ++ $idcounter );
$output .= ' <span class="radioelement ' . $name . ' rb' . $currentradio . " \" > " ;
$output .= '<input name="' . $name . '" id="' . $htmlid . '" type="radio" value="' . $value . '"' ;
2005-06-27 00:17:39 +00:00
if ( $value == $checked ) {
$output .= ' checked="checked"' ;
}
if ( $label === '' ) {
2005-07-01 03:57:03 +00:00
$output .= ' /> <label for="' . $htmlid . '">' . $value . '</label></span>' . " \n " ;
2005-06-27 00:17:39 +00:00
} else {
2005-07-01 03:57:03 +00:00
$output .= ' /> <label for="' . $htmlid . '">' . $label . '</label></span>' . " \n " ;
2005-06-27 00:17:39 +00:00
}
$currentradio = ( $currentradio + 1 ) % 2 ;
}
}
$output .= '</span>' . " \n " ;
2006-08-10 04:33:55 +00:00
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2005-06-27 00:17:39 +00:00
}
2005-06-28 16:55:01 +00:00
/** Display an standard html checkbox with an optional label
*
* @ param string $name The name of the checkbox
* @ param string $value The valus that the checkbox will pass when checked
* @ param boolean $checked The flag to tell the checkbox initial state
* @ param string $label The label to be showed near the checkbox
* @ param string $alt The info to be inserted in the alt tag
*/
2006-01-11 02:14:37 +00:00
function print_checkbox ( $name , $value , $checked = true , $label = '' , $alt = '' , $script = '' , $return = false ) {
2005-06-28 16:55:01 +00:00
2005-07-01 03:57:03 +00:00
static $idcounter = 0 ;
2005-06-28 16:55:01 +00:00
if ( ! $name ) {
$name = 'unnamed' ;
}
if ( ! $alt ) {
$alt = 'checkbox' ;
}
if ( $checked ) {
$strchecked = ' checked="checked"' ;
2006-03-15 20:50:05 +00:00
} else {
$strchecked = '' ;
2005-06-28 16:55:01 +00:00
}
2005-07-01 03:57:03 +00:00
$htmlid = 'auto-cb' . sprintf ( '%04d' , ++ $idcounter );
2005-06-28 16:55:01 +00:00
$output = '<span class="checkbox ' . $name . " \" > " ;
2006-01-11 02:14:37 +00:00
$output .= '<input name="' . $name . '" id="' . $htmlid . '" type="checkbox" value="' . $value . '" alt="' . $alt . '"' . $strchecked . ' ' . (( ! empty ( $script )) ? ' onClick="' . $script . '" ' : '' ) . ' />' ;
2005-07-01 03:57:03 +00:00
if ( ! empty ( $label )) {
$output .= ' <label for="' . $htmlid . '">' . $label . '</label>' ;
}
2005-06-28 16:55:01 +00:00
$output .= '</span>' . " \n " ;
2006-01-11 02:14:37 +00:00
if ( empty ( $return )) {
echo $output ;
} else {
return $output ;
}
2005-06-28 16:55:01 +00:00
}
2006-01-11 02:14:37 +00:00
/** Display an standard html text field with an optional label
*
* @ param string $name The name of the text field
* @ param string $value The value of the text field
* @ param string $label The label to be showed near the text field
* @ param string $alt The info to be inserted in the alt tag
*/
2006-08-10 04:33:55 +00:00
function print_textfield ( $name , $value , $alt = '' , $size = 50 , $maxlength = 0 , $return = false ) {
2006-01-11 02:14:37 +00:00
static $idcounter = 0 ;
if ( empty ( $name )) {
$name = 'unnamed' ;
}
if ( empty ( $alt )) {
$alt = 'textfield' ;
}
if ( ! empty ( $maxlength )) {
$maxlength = ' maxlength="' . $maxlength . '" ' ;
}
2006-01-15 02:32:04 +00:00
$htmlid = 'auto-tf' . sprintf ( '%04d' , ++ $idcounter );
2006-01-11 02:14:37 +00:00
$output = '<span class="textfield ' . $name . " \" > " ;
$output .= '<input name="' . $name . '" id="' . $htmlid . '" type="text" value="' . $value . '" size="' . $size . '" ' . $maxlength . ' alt="' . $alt . '" />' ;
2006-03-04 16:04:48 +00:00
2006-01-11 02:14:37 +00:00
$output .= '</span>' . " \n " ;
if ( empty ( $return )) {
echo $output ;
} else {
return $output ;
}
}
2004-09-23 04:36:43 +00:00
/**
* Implements a complete little popup form
*
2004-09-25 05:29:21 +00:00
* @ uses $CFG
* @ param string $common The URL up to the point of the variable that changes
* @ param array $options Alist of value - label pairs for the popup list
* @ param string $formname Name must be unique on the page
* @ param string $selected The option that is already selected
* @ param string $nothing The label for the " no choice " option
* @ param string $help The name of a help page if help is required
* @ param string $helptext The name of the label for the help button
* @ param boolean $return Indicates whether the function should return the text
* as a string or echo it directly to the page being rendered
2005-01-26 15:57:00 +00:00
* @ param string $targetwindow The name of the target page to open the linked page in .
2004-09-25 05:29:21 +00:00
* @ return string If $return is true then the entire form is returned as a string .
* @ todo Finish documenting this function < br >
*/
function popup_form ( $common , $options , $formname , $selected = '' , $nothing = 'choose' , $help = '' , $helptext = '' , $return = false , $targetwindow = 'self' ) {
2005-01-26 15:57:00 +00:00
global $CFG ;
2004-09-30 10:52:48 +00:00
static $go , $choose ; /// Locally cached, in case there's lots on a page
2004-06-22 18:35:59 +00:00
2004-06-09 08:36:28 +00:00
if ( empty ( $options )) {
return '' ;
}
2003-03-30 13:25:49 +00:00
2004-09-30 10:52:48 +00:00
if ( ! isset ( $go )) {
$go = get_string ( 'go' );
2004-09-29 14:00:35 +00:00
}
2004-09-22 14:39:15 +00:00
if ( $nothing == 'choose' ) {
2004-09-29 14:00:35 +00:00
if ( ! isset ( $choose )) {
$choose = get_string ( 'choose' );
}
$nothing = $choose . '...' ;
2002-08-12 07:40:53 +00:00
}
2004-09-29 14:00:35 +00:00
$startoutput = '<form action="' . $CFG -> wwwroot . '/course/jumpto.php"' .
' method="get"' .
' target="' . $CFG -> framename . '"' .
' name="' . $formname . '"' .
2005-05-10 04:00:56 +00:00
' class="popupform">' ;
2004-09-29 14:00:35 +00:00
$output = '<select name="jump" onchange="' . $targetwindow . '.location=document.' . $formname .
2004-09-30 07:56:27 +00:00
'.jump.options[document.' . $formname . '.jump.selectedIndex].value;">' . " \n " ;
2001-11-22 06:23:56 +00:00
2004-09-22 14:39:15 +00:00
if ( $nothing != '' ) {
2003-05-04 09:43:03 +00:00
$output .= " <option value= \" javascript:void(0) \" > $nothing </option> \n " ;
2001-11-22 06:23:56 +00:00
}
2004-09-28 09:47:14 +00:00
$inoptgroup = false ;
2001-11-22 06:23:56 +00:00
foreach ( $options as $value => $label ) {
2005-01-26 15:57:00 +00:00
2004-10-26 07:42:08 +00:00
if ( substr ( $label , 0 , 2 ) == '--' ) { /// we are starting a new optgroup
2005-01-26 15:57:00 +00:00
2004-10-26 07:42:08 +00:00
/// Check to see if we already have a valid open optgroup
/// XHTML demands that there be at least 1 option within an optgroup
if ( $inoptgroup and ( count ( $optgr ) > 1 ) ) {
$output .= implode ( '' , $optgr );
2004-09-28 09:47:14 +00:00
$output .= ' </optgroup>' ;
}
2004-10-26 07:42:08 +00:00
unset ( $optgr );
$optgr = array ();
$optgr [] = ' <optgroup label="' . substr ( $label , 2 ) . '">' ; // Plain labels
2005-01-26 15:57:00 +00:00
2004-10-26 07:42:08 +00:00
$inoptgroup = true ; /// everything following will be in an optgroup
2004-07-21 05:41:58 +00:00
continue ;
2005-01-26 15:57:00 +00:00
2002-11-10 07:37:15 +00:00
} else {
2005-12-05 02:24:45 +00:00
if ( ! empty ( $CFG -> usesid ) && ! isset ( $_COOKIE [ session_name ()]))
{
$url = sid_process_url ( $common . $value );
} else
{
$url = $common . $value ;
}
$optstr = ' <option value="' . $url . '"' ;
2005-01-26 15:57:00 +00:00
2002-11-10 07:37:15 +00:00
if ( $value == $selected ) {
2004-10-26 07:42:08 +00:00
$optstr .= ' selected="selected"' ;
}
2005-01-26 15:57:00 +00:00
2004-10-26 07:42:08 +00:00
if ( $label ) {
$optstr .= '>' . $label . '</option>' . " \n " ;
} else {
$optstr .= '>' . $value . '</option>' . " \n " ;
}
2005-01-26 15:57:00 +00:00
2004-10-26 07:42:08 +00:00
if ( $inoptgroup ) {
$optgr [] = $optstr ;
} else {
$output .= $optstr ;
2002-11-10 07:37:15 +00:00
}
2001-11-22 06:23:56 +00:00
}
2005-01-26 15:57:00 +00:00
2001-11-22 06:23:56 +00:00
}
2004-10-26 07:42:08 +00:00
/// catch the final group if not closed
if ( $inoptgroup and count ( $optgr ) > 1 ) {
$output .= implode ( '' , $optgr );
2004-09-28 09:47:14 +00:00
$output .= ' </optgroup>' ;
}
2004-10-26 07:42:08 +00:00
2004-09-22 14:39:15 +00:00
$output .= '</select>' ;
2006-09-11 06:47:38 +00:00
$output .= '<input type="hidden" name="sesskey" value="' . sesskey () . '" />' ;
2006-10-24 03:26:55 +00:00
$output .= '<div id="noscript' . $formname . '" style="display: inline;">' ;
$output .= '<input type="submit" value="' . $go . '" /></div>' ;
2004-09-29 14:00:35 +00:00
$output .= '<script type="text/javascript">' .
" \n <!-- \n " .
'document.getElementById("noscript' . $formname . '").style.display = "none";' .
" \n --> \n " . '</script>' ;
2004-09-22 14:39:15 +00:00
$output .= '</form>' . " \n " ;
2002-11-10 07:37:15 +00:00
2004-08-02 19:09:06 +00:00
if ( $help ) {
$button = helpbutton ( $help , $helptext , 'moodle' , true , false , '' , true );
} else {
$button = '' ;
}
2002-11-10 07:37:15 +00:00
if ( $return ) {
2004-08-02 19:09:06 +00:00
return $startoutput . $button . $output ;
2002-11-10 07:37:15 +00:00
} else {
2004-08-02 19:09:06 +00:00
echo $startoutput . $button . $output ;
2002-11-10 07:37:15 +00:00
}
2001-11-22 06:23:56 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Prints some red text
*
* @ param string $error The text to be displayed in red
*/
2001-11-22 06:23:56 +00:00
function formerr ( $error ) {
2004-09-23 04:36:43 +00:00
2001-11-22 06:23:56 +00:00
if ( ! empty ( $error )) {
2004-09-22 14:39:15 +00:00
echo '<font color="#ff0000">' . $error . '</font>' ;
2001-11-22 06:23:56 +00:00
}
}
2004-09-23 04:36:43 +00:00
/**
* Validates an email to make sure it makes sense .
*
* @ param string $address The email address to validate .
* @ return boolean
*/
2004-09-25 05:29:21 +00:00
function validate_email ( $address ) {
2004-09-23 04:36:43 +00:00
2006-04-20 22:32:00 +00:00
return ( ereg ( '^[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+' .
'(\.[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+)*' .
2001-11-22 06:23:56 +00:00
'@' .
'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.' .
'[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$' ,
$address ));
}
2004-11-30 19:44:08 +00:00
/**
* Extracts file argument either from file parameter or PATH_INFO
*
* @ param string $scriptname name of the calling script
* @ return string file path ( only safe characters )
*/
function get_file_argument ( $scriptname ) {
global $_SERVER ;
$relativepath = FALSE ;
// first try normal parameter (compatible method == no relative links!)
$relativepath = optional_param ( 'file' , FALSE , PARAM_PATH );
2005-02-26 20:43:30 +00:00
if ( $relativepath === '/testslasharguments' ) {
2006-07-05 07:44:20 +00:00
echo 'test -1 : Incorrect use - try "file.php/testslasharguments" instead' ; //indicate fopen/fread works for health center
2005-02-26 20:43:30 +00:00
die ;
}
2004-11-30 19:44:08 +00:00
// then try extract file from PATH_INFO (slasharguments method)
if ( ! $relativepath and ! empty ( $_SERVER [ 'PATH_INFO' ])) {
$path_info = $_SERVER [ 'PATH_INFO' ];
// check that PATH_INFO works == must not contain the script name
if ( ! strpos ( $path_info , $scriptname )) {
$relativepath = clean_param ( rawurldecode ( $path_info ), PARAM_PATH );
2005-02-26 20:43:30 +00:00
if ( $relativepath === '/testslasharguments' ) {
2006-07-05 07:44:20 +00:00
echo 'test 1 : Slasharguments test passed. Server confguration is compatible with file.php/1/pic.jpg slashargument setting.' ; //indicate ok for health center
2004-11-30 19:44:08 +00:00
die ;
}
}
}
// now if both fail try the old way
// (for compatibility with misconfigured or older buggy php implementations)
if ( ! $relativepath ) {
$arr = explode ( $scriptname , me ());
if ( ! empty ( $arr [ 1 ])) {
$path_info = strip_querystring ( $arr [ 1 ]);
$relativepath = clean_param ( rawurldecode ( $path_info ), PARAM_PATH );
2005-02-26 20:43:30 +00:00
if ( $relativepath === '/testslasharguments' ) {
2006-07-05 07:44:20 +00:00
echo 'test 2 : Slasharguments test passed (compatibility hack). Server confguration may be compatible with file.php/1/pic.jpg slashargument setting' ; //indicate ok for health center
2004-11-30 19:44:08 +00:00
die ;
}
}
}
return $relativepath ;
}
2004-09-23 04:36:43 +00:00
/**
* Check for bad characters ?
*
* @ param string $string ?
2004-09-25 05:29:21 +00:00
* @ param int $allowdots ?
* @ todo Finish documenting this function - more detail needed in description as well as details on arguments
2004-09-23 04:36:43 +00:00
*/
2004-08-18 05:52:01 +00:00
function detect_munged_arguments ( $string , $allowdots = 1 ) {
if ( substr_count ( $string , '..' ) > $allowdots ) { // Sometimes we allow dots in references
2003-05-09 02:05:16 +00:00
return true ;
}
2003-05-12 06:35:32 +00:00
if ( ereg ( '[\|\`]' , $string )) { // check for other bad characters
2003-05-09 02:05:16 +00:00
return true ;
}
2004-03-16 07:11:14 +00:00
if ( empty ( $string ) or $string == '/' ) {
return true ;
}
2003-05-09 02:05:16 +00:00
return false ;
}
2004-09-23 04:36:43 +00:00
/**
* Searches the current environment variables for some slash arguments
*
* @ param string $file ?
* @ todo Finish documenting this function
*/
2004-09-22 14:39:15 +00:00
function get_slash_arguments ( $file = 'file.php' ) {
2001-11-22 06:23:56 +00:00
2003-02-05 10:48:13 +00:00
if ( ! $string = me ()) {
2001-11-22 06:23:56 +00:00
return false ;
}
2003-02-05 10:48:13 +00:00
2003-01-12 06:53:25 +00:00
$pathinfo = explode ( $file , $string );
2004-05-21 13:07:11 +00:00
2003-02-05 10:52:55 +00:00
if ( ! empty ( $pathinfo [ 1 ])) {
2004-11-04 07:00:12 +00:00
return addslashes ( $pathinfo [ 1 ]);
2003-01-12 06:53:25 +00:00
} else {
return false ;
}
}
2004-09-23 04:36:43 +00:00
/**
* Extracts arguments from " /foo/bar/something "
* eg http :// mysite . com / script . php / foo / bar / something
*
2004-09-25 05:29:21 +00:00
* @ param string $string ?
* @ param int $i ?
* @ return array | string
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2003-01-12 06:53:25 +00:00
function parse_slash_arguments ( $string , $i = 0 ) {
2001-11-22 06:23:56 +00:00
2003-05-09 02:05:16 +00:00
if ( detect_munged_arguments ( $string )) {
2002-11-21 02:51:50 +00:00
return false ;
}
2004-09-22 14:39:15 +00:00
$args = explode ( '/' , $string );
2001-11-22 06:23:56 +00:00
if ( $i ) { // return just the required argument
return $args [ $i ];
} else { // return the whole array
array_shift ( $args ); // get rid of the empty first one
return $args ;
}
}
2004-09-23 04:36:43 +00:00
/**
2004-09-25 05:29:21 +00:00
* Just returns an array of text formats suitable for a popup menu
2004-09-23 04:36:43 +00:00
*
2004-09-25 05:29:21 +00:00
* @ uses FORMAT_MOODLE
* @ uses FORMAT_HTML
* @ uses FORMAT_PLAIN
* @ uses FORMAT_MARKDOWN
* @ return array
2004-09-23 04:36:43 +00:00
*/
2002-10-10 07:26:10 +00:00
function format_text_menu () {
2004-09-23 04:36:43 +00:00
2004-09-22 14:39:15 +00:00
return array ( FORMAT_MOODLE => get_string ( 'formattext' ),
FORMAT_HTML => get_string ( 'formathtml' ),
FORMAT_PLAIN => get_string ( 'formatplain' ),
FORMAT_MARKDOWN => get_string ( 'formatmarkdown' ));
2002-10-10 07:26:10 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Given text in a variety of format codings , this function returns
2005-01-26 15:57:00 +00:00
* the text as safe HTML .
2004-09-23 04:36:43 +00:00
*
* @ uses $CFG
2004-09-25 05:29:21 +00:00
* @ uses FORMAT_MOODLE
* @ uses FORMAT_HTML
* @ uses FORMAT_PLAIN
* @ uses FORMAT_WIKI
* @ uses FORMAT_MARKDOWN
* @ param string $text The text to be formatted . This is raw text originally from user input .
2005-01-26 15:57:00 +00:00
* @ param int $format Identifier of the text format to be used
2004-09-25 05:29:21 +00:00
* ( FORMAT_MOODLE , FORMAT_HTML , FORMAT_PLAIN , FORMAT_WIKI , FORMAT_MARKDOWN )
* @ param array $options ?
* @ param int $courseid ?
* @ return string
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2006-08-26 13:00:07 +00:00
function format_text ( $text , $format = FORMAT_MOODLE , $options = NULL , $courseid = NULL ) {
2002-10-10 07:26:10 +00:00
2004-05-21 13:07:11 +00:00
global $CFG , $course ;
2004-01-16 10:49:07 +00:00
2006-10-24 22:19:41 +00:00
if ( $text === '' ) {
return '' ; // no need to do any filters and cleaning
}
2006-08-26 13:00:07 +00:00
if ( ! isset ( $options -> trusttext )) {
$options -> trusttext = false ;
}
2005-10-29 21:32:09 +00:00
if ( ! isset ( $options -> noclean )) {
$options -> noclean = false ;
}
2006-09-22 18:08:53 +00:00
if ( ! isset ( $options -> nocache )) {
$options -> nocache = false ;
}
2005-10-29 21:32:09 +00:00
if ( ! isset ( $options -> smiley )) {
$options -> smiley = true ;
}
if ( ! isset ( $options -> filter )) {
$options -> filter = true ;
}
if ( ! isset ( $options -> para )) {
$options -> para = true ;
}
if ( ! isset ( $options -> newlines )) {
$options -> newlines = true ;
2004-02-09 12:33:44 +00:00
}
2004-01-16 10:49:07 +00:00
if ( empty ( $courseid )) {
2004-02-10 09:59:46 +00:00
if ( ! empty ( $course -> id )) { // An ugly hack for better compatibility
2004-01-16 10:49:07 +00:00
$courseid = $course -> id ;
}
}
2003-08-26 18:44:09 +00:00
2006-09-22 18:08:53 +00:00
if ( ! empty ( $CFG -> cachetext ) and empty ( $options -> nocache )) {
2005-10-29 21:32:09 +00:00
$time = time () - $CFG -> cachetext ;
2006-10-25 23:18:40 +00:00
$md5key = md5 ( $text . '-' . ( int ) $courseid . '-' . current_language () . '-' . ( int ) $format . ( int ) $options -> trusttext . ( int ) $options -> noclean . ( int ) $options -> smiley . ( int ) $options -> filter . ( int ) $options -> para . ( int ) $options -> newlines );
2006-03-20 02:33:41 +00:00
if ( $oldcacheitem = get_record_sql ( 'SELECT * FROM ' . $CFG -> prefix . 'cache_text WHERE md5key = \'' . $md5key . '\'' , true )) {
if ( $oldcacheitem -> timemodified >= $time ) {
return $oldcacheitem -> formattedtext ;
}
2005-10-29 21:32:09 +00:00
}
}
2006-08-26 13:00:07 +00:00
// trusttext overrides the noclean option!
if ( $options -> trusttext ) {
if ( trusttext_present ( $text )) {
$text = trusttext_strip ( $text );
if ( ! empty ( $CFG -> enabletrusttext )) {
$options -> noclean = true ;
} else {
$options -> noclean = false ;
}
} else {
$options -> noclean = false ;
}
2006-10-24 22:19:41 +00:00
} else if ( ! debugging ( '' , DEBUG_DEVELOPER )) {
// strip any forgotten trusttext in non-developer mode
// do not forget to disable text cache when debugging trusttext!!
$text = trusttext_strip ( $text );
2006-08-26 13:00:07 +00:00
}
2004-02-10 09:59:46 +00:00
$CFG -> currenttextiscacheable = true ; // Default status - can be changed by any filter
2006-03-04 16:04:48 +00:00
2002-10-10 07:26:10 +00:00
switch ( $format ) {
2003-02-05 12:41:59 +00:00
case FORMAT_HTML :
2006-08-26 13:00:07 +00:00
if ( $options -> smiley ) {
2005-10-29 21:32:09 +00:00
replace_smilies ( $text );
}
2006-08-26 13:00:07 +00:00
if ( ! $options -> noclean ) {
2004-08-09 14:54:39 +00:00
$text = clean_text ( $text , $format );
}
2006-08-26 13:00:07 +00:00
if ( $options -> filter ) {
2005-10-29 21:32:09 +00:00
$text = filter_text ( $text , $courseid );
}
2003-02-05 12:41:59 +00:00
break ;
2003-04-27 10:46:16 +00:00
case FORMAT_PLAIN :
2006-03-04 16:04:48 +00:00
$text = s ( $text );
2004-05-11 23:17:25 +00:00
$text = rebuildnolinktag ( $text );
2004-09-22 14:39:15 +00:00
$text = str_replace ( ' ' , ' ' , $text );
2003-04-27 10:46:16 +00:00
$text = nl2br ( $text );
break ;
2003-05-04 04:07:36 +00:00
case FORMAT_WIKI :
2005-04-11 13:46:06 +00:00
// this format is deprecated
2005-04-20 19:12:28 +00:00
$text = ' < p > NOTICE : Wiki - like formatting has been removed from Moodle . You should not be seeing
this message as all texts should have been converted to Markdown format instead .
2005-04-12 03:14:29 +00:00
Please post a bug report to http :// moodle . org / bugs with information about where you
2005-10-29 21:32:09 +00:00
saw this message .</ p > ' . s ( $text );
2003-05-04 04:07:36 +00:00
break ;
2004-07-30 04:02:58 +00:00
case FORMAT_MARKDOWN :
$text = markdown_to_html ( $text );
2006-08-26 13:00:07 +00:00
if ( $options -> smiley ) {
2005-10-29 21:32:09 +00:00
replace_smilies ( $text );
}
2006-08-26 13:00:07 +00:00
if ( ! $options -> noclean ) {
2004-08-09 14:54:39 +00:00
$text = clean_text ( $text , $format );
}
2006-03-04 16:04:48 +00:00
2006-08-26 13:00:07 +00:00
if ( $options -> filter ) {
2005-10-29 21:32:09 +00:00
$text = filter_text ( $text , $courseid );
}
2004-07-30 04:02:58 +00:00
break ;
2003-02-05 12:41:59 +00:00
default : // FORMAT_MOODLE or anything else
2004-03-09 06:44:27 +00:00
$text = text_to_html ( $text , $options -> smiley , $options -> para , $options -> newlines );
2006-08-26 13:00:07 +00:00
if ( ! $options -> noclean ) {
2004-08-09 14:54:39 +00:00
$text = clean_text ( $text , $format );
}
2006-03-04 16:04:48 +00:00
2006-08-26 13:00:07 +00:00
if ( $options -> filter ) {
2005-10-29 21:32:09 +00:00
$text = filter_text ( $text , $courseid );
}
2002-10-10 07:26:10 +00:00
break ;
}
2004-02-09 12:33:44 +00:00
2006-10-04 21:22:57 +00:00
if ( empty ( $options -> nocache ) and ! empty ( $CFG -> cachetext ) and $CFG -> currenttextiscacheable ) {
$newcacheitem = new object ();
2006-03-20 02:33:41 +00:00
$newcacheitem -> md5key = $md5key ;
$newcacheitem -> formattedtext = addslashes ( $text );
$newcacheitem -> timemodified = time ();
if ( $oldcacheitem ) { // See bug 4677 for discussion
$newcacheitem -> id = $oldcacheitem -> id ;
@ update_record ( 'cache_text' , $newcacheitem ); // Update existing record in the cache table
// It's unlikely that the cron cache cleaner could have
// deleted this entry in the meantime, as it allows
// some extra time to cover these cases.
} else {
@ insert_record ( 'cache_text' , $newcacheitem ); // Insert a new record in the cache table
// Again, it's possible that another user has caused this
// record to be created already in the time that it took
// to traverse this function. That's OK too, as the
// call above handles duplicate entries, and eventually
// the cron cleaner will delete them.
}
2004-02-09 12:33:44 +00:00
}
return $text ;
2002-10-10 07:26:10 +00:00
}
2005-05-16 10:08:18 +00:00
/** Converts the text format from the value to the 'internal'
* name or vice versa . $key can either be the value or the name
* and you get the other back .
2006-03-04 16:04:48 +00:00
*
2005-05-16 10:08:18 +00:00
* @ param mixed int 0 - 4 or string one of 'moodle' , 'html' , 'plain' , 'markdown'
* @ return mixed as above but the other way around !
*/
function text_format_name ( $key ) {
$lookup = array ();
$lookup [ FORMAT_MOODLE ] = 'moodle' ;
$lookup [ FORMAT_HTML ] = 'html' ;
$lookup [ FORMAT_PLAIN ] = 'plain' ;
$lookup [ FORMAT_MARKDOWN ] = 'markdown' ;
$value = " error " ;
if ( ! is_numeric ( $key )) {
$key = strtolower ( $key );
$value = array_search ( $key , $lookup );
}
else {
if ( isset ( $lookup [ $key ] )) {
$value = $lookup [ $key ];
}
}
return $value ;
}
2005-03-25 16:24:43 +00:00
/** Given a simple string , this function returns the string
* processed by enabled filters if $CFG -> filterall is enabled
*
2005-03-26 01:07:23 +00:00
* @ param string $string The string to be filtered .
* @ param boolean $striplinks To strip any link in the result text .
* @ param int $courseid Current course as filters can , potentially , use it
2005-03-25 16:24:43 +00:00
* @ return string
*/
2005-03-26 01:07:23 +00:00
function format_string ( $string , $striplinks = false , $courseid = NULL ) {
2005-03-25 16:24:43 +00:00
2006-09-23 12:46:53 +00:00
global $CFG , $COURSE ;
2005-03-25 16:24:43 +00:00
2005-03-27 20:57:50 +00:00
//We'll use a in-memory cache here to speed up repeated strings
static $strcache ;
//Calculate md5
2005-06-27 00:24:04 +00:00
$md5 = md5 ( $string . '<+>' . $striplinks );
2005-03-27 20:57:50 +00:00
//Fetch from cache if possible
if ( isset ( $strcache [ $md5 ])) {
return $strcache [ $md5 ];
}
2005-03-25 16:24:43 +00:00
if ( empty ( $courseid )) {
2006-09-23 12:46:53 +00:00
if ( ! empty ( $COURSE -> id )) { // An ugly hack for better compatibility
$courseid = $COURSE -> id ; // (copied from format_text)
2005-03-25 16:24:43 +00:00
}
}
2005-03-26 13:38:21 +00:00
if ( ! empty ( $CFG -> filterall )) {
2005-03-25 16:24:43 +00:00
$string = filter_text ( $string , $courseid );
}
2005-03-26 01:07:23 +00:00
if ( $striplinks ) { //strip links in string
$string = preg_replace ( '/(<a[^>]+?>)(.+?)(<\/a>)/is' , '$2' , $string );
}
2005-03-27 20:57:50 +00:00
//Store to cache
$strcache [ $md5 ] = $string ;
2005-03-25 16:24:43 +00:00
return $string ;
}
2004-09-23 04:36:43 +00:00
/**
* Given text in a variety of format codings , this function returns
* the text as plain text suitable for plain email .
*
2004-09-25 05:29:21 +00:00
* @ uses FORMAT_MOODLE
* @ uses FORMAT_HTML
* @ uses FORMAT_PLAIN
* @ uses FORMAT_WIKI
* @ uses FORMAT_MARKDOWN
* @ param string $text The text to be formatted . This is raw text originally from user input .
2005-01-26 15:57:00 +00:00
* @ param int $format Identifier of the text format to be used
2004-09-25 05:29:21 +00:00
* ( FORMAT_MOODLE , FORMAT_HTML , FORMAT_PLAIN , FORMAT_WIKI , FORMAT_MARKDOWN )
* @ return string
2004-09-23 04:36:43 +00:00
*/
2003-05-04 04:07:36 +00:00
function format_text_email ( $text , $format ) {
switch ( $format ) {
case FORMAT_PLAIN :
return $text ;
break ;
case FORMAT_WIKI :
$text = wiki_to_html ( $text );
2003-08-26 13:18:10 +00:00
/// This expression turns links into something nice in a text format. (Russell Jungwirth)
/// From: http://php.net/manual/en/function.eregi-replace.php and simplified
2004-08-17 08:09:13 +00:00
$text = eregi_replace ( '(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)' , '\\3 [ \\2 ]' , $text );
2003-07-15 02:06:51 +00:00
return strtr ( strip_tags ( $text ), array_flip ( get_html_translation_table ( HTML_ENTITIES )));
2003-05-04 04:07:36 +00:00
break ;
2003-11-07 03:27:21 +00:00
case FORMAT_HTML :
return html_to_text ( $text );
break ;
2004-07-30 04:02:58 +00:00
case FORMAT_MOODLE :
case FORMAT_MARKDOWN :
2004-08-21 14:21:09 +00:00
default :
2004-08-17 08:09:13 +00:00
$text = eregi_replace ( '(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)' , '\\3 [ \\2 ]' , $text );
2003-07-15 02:06:51 +00:00
return strtr ( strip_tags ( $text ), array_flip ( get_html_translation_table ( HTML_ENTITIES )));
2003-05-04 04:07:36 +00:00
break ;
}
}
2002-10-10 07:26:10 +00:00
2004-09-23 04:36:43 +00:00
/**
* Given some text in HTML format , this function will pass it
* through any filters that have been defined in $CFG -> textfilterx
* The variable defines a filepath to a file containing the
* filter function . The file must contain a variable called
* $textfilter_function which contains the name of the function
* with $courseid and $text parameters
*
2004-09-25 05:29:21 +00:00
* @ param string $text The text to be passed through format filters
* @ param int $courseid ?
2004-09-23 04:36:43 +00:00
* @ return string
* @ todo Finish documenting this function
*/
2004-01-16 10:49:07 +00:00
function filter_text ( $text , $courseid = NULL ) {
global $CFG ;
2003-10-13 14:11:17 +00:00
2005-03-05 04:08:29 +00:00
require_once ( $CFG -> libdir . '/filterlib.php' );
2004-02-10 05:05:28 +00:00
if ( ! empty ( $CFG -> textfilters )) {
$textfilters = explode ( ',' , $CFG -> textfilters );
foreach ( $textfilters as $textfilter ) {
2004-09-22 14:39:15 +00:00
if ( is_readable ( $CFG -> dirroot . '/' . $textfilter . '/filter.php' )) {
include_once ( $CFG -> dirroot . '/' . $textfilter . '/filter.php' );
2004-08-08 06:57:44 +00:00
$functionname = basename ( $textfilter ) . '_filter' ;
if ( function_exists ( $functionname )) {
$text = $functionname ( $courseid , $text );
}
2004-02-10 05:05:28 +00:00
}
2003-10-13 14:11:17 +00:00
}
}
2004-02-10 05:05:28 +00:00
2005-01-18 06:57:59 +00:00
/// <nolink> tags removed for XHTML compatibility
$text = str_replace ( '<nolink>' , '' , $text );
$text = str_replace ( '</nolink>' , '' , $text );
2003-10-13 14:11:17 +00:00
return $text ;
}
2006-08-26 13:00:07 +00:00
/**
* Is the text marked as trusted ?
*
* @ param string $text text to be searched for TRUSTTEXT marker
* @ return boolean
*/
function trusttext_present ( $text ) {
if ( strpos ( $text , TRUSTTEXT ) !== FALSE ) {
return true ;
} else {
return false ;
}
}
/**
* This funtion MUST be called before the cleaning or any other
* function that modifies the data ! We do not know the origin of trusttext
* in database , if it gets there in tweaked form we must not convert it
* to supported form !!!
*
* Please be carefull not to use stripslashes on data from database
* or twice stripslashes when processing data recieved from user .
*
* @ param string $text text that may contain TRUSTTEXT marker
* @ return text without any TRUSTTEXT marker
*/
function trusttext_strip ( $text ) {
global $CFG ;
while ( true ) { //removing nested TRUSTTEXT
$orig = $text ;
$text = str_replace ( TRUSTTEXT , '' , $text );
if ( strcmp ( $orig , $text ) === 0 ) {
return $text ;
}
}
}
/**
* Mark text as trusted , such text may contain any HTML tags because the
* normal text cleaning will be bypassed .
* Please make sure that the text comes from trusted user before storing
* it into database !
*/
function trusttext_mark ( $text ) {
global $CFG ;
if ( ! empty ( $CFG -> enabletrusttext ) and ( strpos ( $text , TRUSTTEXT ) === FALSE )) {
return TRUSTTEXT . $text ;
} else {
return $text ;
}
}
function trusttext_after_edit ( & $text , $context ) {
if ( has_capability ( 'moodle/site:trustcontent' , $context )) {
2006-08-26 17:39:31 +00:00
$text = trusttext_strip ( $text );
2006-08-26 13:00:07 +00:00
$text = trusttext_mark ( $text );
} else {
$text = trusttext_strip ( $text );
}
}
function trusttext_prepare_edit ( & $text , & $format , $usehtmleditor , $context ) {
global $CFG ;
$options = new object ();
$options -> smiley = false ;
$options -> filter = false ;
if ( ! empty ( $CFG -> enabletrusttext )
and has_capability ( 'moodle/site:trustcontent' , $context )
and trusttext_present ( $text )) {
$options -> noclean = true ;
} else {
$options -> noclean = false ;
}
$text = trusttext_strip ( $text );
if ( $usehtmleditor ) {
$text = format_text ( $text , $format , $options );
$format = FORMAT_HTML ;
} else if ( ! $options -> noclean ){
$text = clean_text ( $text , $format );
}
}
2004-09-23 04:36:43 +00:00
/**
* Given raw text ( eg typed in by a user ), this function cleans it up
* and removes any nasty tags that could mess up Moodle pages .
*
2004-09-25 05:29:21 +00:00
* @ uses FORMAT_MOODLE
* @ uses FORMAT_PLAIN
* @ uses ALLOWED_TAGS
* @ param string $text The text to be cleaned
2005-01-26 15:57:00 +00:00
* @ param int $format Identifier of the text format to be used
2004-09-25 05:29:21 +00:00
* ( FORMAT_MOODLE , FORMAT_HTML , FORMAT_PLAIN , FORMAT_WIKI , FORMAT_MARKDOWN )
* @ return string The cleaned up text
2004-09-23 04:36:43 +00:00
*/
2004-01-15 07:17:47 +00:00
function clean_text ( $text , $format = FORMAT_MOODLE ) {
2002-03-26 12:58:54 +00:00
2003-06-09 03:00:23 +00:00
global $ALLOWED_TAGS ;
2002-10-18 09:09:19 +00:00
2004-05-21 13:07:11 +00:00
switch ( $format ) {
2004-07-30 04:02:58 +00:00
case FORMAT_PLAIN :
return $text ;
default :
2006-03-02 21:38:20 +00:00
/// Fix non standard entity notations
$text = preg_replace ( '/(&#[0-9]+)(;?)/' , " \\ 1; " , $text );
$text = preg_replace ( '/(&#x[0-9a-fA-F]+)(;?)/' , " \\ 1; " , $text );
2004-03-28 06:31:27 +00:00
/// Remove tags that are not allowed
2002-10-18 09:09:19 +00:00
$text = strip_tags ( $text , $ALLOWED_TAGS );
2004-07-30 04:02:58 +00:00
2005-01-29 13:02:28 +00:00
/// Clean up embedded scripts and , using kses
$text = cleanAttributes ( $text );
2003-08-26 13:18:10 +00:00
/// Remove script events
2004-05-21 13:07:11 +00:00
$text = eregi_replace ( " ([^a-z])language([[:space:]]*)= " , " \\ 1Xlanguage= " , $text );
$text = eregi_replace ( " ([^a-z])on([a-z]+)([[:space:]]*)= " , " \\ 1Xon \\ 2= " , $text );
2003-04-27 10:46:16 +00:00
return $text ;
2002-10-10 07:26:10 +00:00
}
2002-03-26 12:58:54 +00:00
}
2001-11-22 06:23:56 +00:00
2004-09-23 04:36:43 +00:00
/**
2004-09-25 05:29:21 +00:00
* This function takes a string and examines it for HTML tags .
2004-09-23 04:36:43 +00:00
* If tags are detected it passes the string to a helper function { @ link cleanAttributes2 ()}
* which checks for attributes and filters them for malicious content
* 17 / 08 / 2004 :: Eamon DOT Costello AT dcu DOT ie
*
* @ param string $str The string to be examined for html tags
* @ return string
*/
2004-08-21 04:34:06 +00:00
function cleanAttributes ( $str ){
2005-05-04 23:09:43 +00:00
$result = preg_replace_callback (
'%(<[^>]*(>|$)|>)%m' , #search for html tags
" cleanAttributes2 " ,
2004-08-21 04:34:06 +00:00
$str
2004-08-21 14:21:09 +00:00
);
2004-08-21 04:34:06 +00:00
return $result ;
2004-08-21 14:21:09 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* This function takes a string with an html tag and strips out any unallowed
* protocols e . g . javascript :
* It calls ancillary functions in kses which are prefixed by kses
* 17 / 08 / 2004 :: Eamon DOT Costello AT dcu DOT ie
*
2005-05-04 23:09:43 +00:00
* @ param array $htmlArray An array from { @ link cleanAttributes ()}, containing in its 1 st
* element the html to be cleared
2004-09-23 04:36:43 +00:00
* @ return string
*/
2005-05-04 23:09:43 +00:00
function cleanAttributes2 ( $htmlArray ){
2004-08-21 04:34:06 +00:00
2004-09-29 14:00:35 +00:00
global $CFG , $ALLOWED_PROTOCOLS ;
2004-09-22 14:39:15 +00:00
require_once ( $CFG -> libdir . '/kses.php' );
2004-08-21 04:34:06 +00:00
2005-05-04 23:09:43 +00:00
$htmlTag = $htmlArray [ 1 ];
2004-09-29 14:00:35 +00:00
if ( substr ( $htmlTag , 0 , 1 ) != '<' ) {
2004-08-21 04:34:06 +00:00
return '>' ; //a single character ">" detected
}
2004-09-29 14:00:35 +00:00
if ( ! preg_match ( '%^<\s*(/\s*)?([a-zA-Z0-9]+)([^>]*)>?$%' , $htmlTag , $matches )) {
2004-08-21 14:21:09 +00:00
return '' ; // It's seriously malformed
}
2004-08-21 04:34:06 +00:00
$slash = trim ( $matches [ 1 ]); //trailing xhtml slash
2004-08-21 14:21:09 +00:00
$elem = $matches [ 2 ]; //the element name
2004-08-21 04:34:06 +00:00
$attrlist = $matches [ 3 ]; // the list of attributes as a string
2004-09-29 14:00:35 +00:00
$attrArray = kses_hair ( $attrlist , $ALLOWED_PROTOCOLS );
2004-08-21 04:34:06 +00:00
2004-08-21 14:21:09 +00:00
$attStr = '' ;
2004-09-29 14:00:35 +00:00
foreach ( $attrArray as $arreach ) {
2006-03-02 21:38:20 +00:00
$arreach [ 'name' ] = strtolower ( $arreach [ 'name' ]);
if ( $arreach [ 'name' ] == 'style' ) {
$value = $arreach [ 'value' ];
while ( true ) {
$prevvalue = $value ;
$value = kses_no_null ( $value );
$value = preg_replace ( " / \ / \ *.* \ * \ //Us " , '' , $value );
$value = kses_decode_entities ( $value );
$value = preg_replace ( '/(&#[0-9]+)(;?)/' , " \\ 1; " , $value );
$value = preg_replace ( '/(&#x[0-9a-fA-F]+)(;?)/' , " \\ 1; " , $value );
if ( $value === $prevvalue ) {
$arreach [ 'value' ] = $value ;
break ;
}
}
$arreach [ 'value' ] = preg_replace ( " /j \ s*a \ s*v \ s*a \ s*s \ s*c \ s*r \ s*i \ s*p \ s*t/i " , " Xjavascript " , $arreach [ 'value' ]);
$arreach [ 'value' ] = preg_replace ( " /e \ s*x \ s*p \ s*r \ s*e \ s*s \ s*s \ s*i \ s*o \ s*n/i " , " Xexpression " , $arreach [ 'value' ]);
}
$attStr .= ' ' . $arreach [ 'name' ] . '="' . $arreach [ 'value' ] . '" ' ;
2004-08-21 04:34:06 +00:00
}
2005-01-31 20:09:17 +00:00
// Remove last space from attribute list
$attStr = rtrim ( $attStr );
2004-08-21 04:34:06 +00:00
$xhtml_slash = '' ;
2004-09-29 14:00:35 +00:00
if ( preg_match ( '%/\s*$%' , $attrlist )) {
2004-08-21 14:21:09 +00:00
$xhtml_slash = ' /' ;
2004-08-21 04:34:06 +00:00
}
2004-09-22 14:39:15 +00:00
return '<' . $slash . $elem . $attStr . $xhtml_slash . '>' ;
2004-08-21 04:34:06 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Replaces all known smileys in the text with image equivalents
*
* @ uses $CFG
* @ param string $text Passed by reference . The string to search for smily strings .
* @ return string
*/
2003-05-05 16:40:46 +00:00
function replace_smilies ( & $text ) {
2005-01-26 15:57:00 +00:00
///
2003-03-12 03:42:44 +00:00
global $CFG ;
2002-10-19 04:58:14 +00:00
2003-08-26 13:18:10 +00:00
/// this builds the mapping array only once
2003-03-12 20:36:55 +00:00
static $runonce = false ;
2003-03-12 21:37:05 +00:00
static $e = array ();
static $img = array ();
2003-03-12 20:36:55 +00:00
static $emoticons = array (
2003-11-07 14:55:44 +00:00
':-)' => 'smiley' ,
':)' => 'smiley' ,
':-D' => 'biggrin' ,
';-)' => 'wink' ,
':-/' => 'mixed' ,
'V-.' => 'thoughtful' ,
':-P' => 'tongueout' ,
'B-)' => 'cool' ,
2006-09-22 02:11:00 +00:00
'B)' => 'cool' ,
2003-11-07 14:55:44 +00:00
'^-)' => 'approve' ,
'8-)' => 'wideeyes' ,
':o)' => 'clown' ,
':-(' => 'sad' ,
':(' => 'sad' ,
'8-.' => 'shy' ,
':-I' => 'blush' ,
':-X' => 'kiss' ,
'8-o' => 'surprise' ,
'P-|' => 'blackeye' ,
'8-[' => 'angry' ,
'xx-P' => 'dead' ,
'|-.' => 'sleepy' ,
'}-]' => 'evil' ,
2006-09-22 02:11:00 +00:00
'(h)' => 'heart' ,
'(heart)' => 'heart' ,
'(y)' => 'yes' ,
'(n)' => 'no' ,
2006-09-22 02:28:13 +00:00
'(martin)' => 'martin' ,
2006-09-22 03:49:49 +00:00
'( )' => 'egg'
2003-03-12 03:42:44 +00:00
);
2003-11-07 14:55:44 +00:00
if ( $runonce == false ) { /// After the first time this is not run again
2003-03-12 20:36:55 +00:00
foreach ( $emoticons as $emoticon => $image ){
2003-11-07 14:55:44 +00:00
$alttext = get_string ( $image , 'pix' );
2003-03-12 21:37:05 +00:00
$e [] = $emoticon ;
2004-09-23 04:36:43 +00:00
$img [] = '<img alt="' . $alttext . '" width="15" height="15" src="' . $CFG -> pixpath . '/s/' . $image . '.gif" />' ;
2003-03-12 20:36:55 +00:00
}
$runonce = true ;
2003-03-12 21:40:47 +00:00
}
2002-03-26 12:58:54 +00:00
2004-03-25 06:31:44 +00:00
// Exclude from transformations all the code inside <script> tags
// Needed to solve Bug 1185. Thanks to jouse 2001 detecting it. :-)
// Based on code from glossary fiter by Williams Castillo.
// - Eloy
// Detect all the <script> zones to take out
$excludes = array ();
preg_match_all ( '/<script language(.+?)<\/script>/is' , $text , $list_of_excludes );
// Take out all the <script> zones from text
foreach ( array_unique ( $list_of_excludes [ 0 ]) as $key => $value ) {
$excludes [ '<+' . $key . '+>' ] = $value ;
}
if ( $excludes ) {
$text = str_replace ( $excludes , array_keys ( $excludes ), $text );
}
2003-11-07 14:55:44 +00:00
/// this is the meat of the code - this is run every time
2003-05-05 16:40:46 +00:00
$text = str_replace ( $e , $img , $text );
2004-03-25 06:31:44 +00:00
// Recover all the <script> zones to text
if ( $excludes ) {
$text = str_replace ( array_keys ( $excludes ), $excludes , $text );
}
2002-10-12 05:09:49 +00:00
}
2002-10-10 07:26:10 +00:00
2004-09-25 05:29:21 +00:00
/**
* Given plain text , makes it into HTML as nicely as possible .
* May contain HTML tags already
*
* @ uses $CFG
* @ param string $text The string to convert .
* @ param boolean $smiley Convert any smiley characters to smiley images ?
* @ param boolean $para If true then the returned string will be wrapped in paragraph tags
* @ param boolean $newlines If true then lines newline breaks will be converted to HTML newline breaks .
* @ return string
*/
2004-03-09 06:44:27 +00:00
function text_to_html ( $text , $smiley = true , $para = true , $newlines = true ) {
2005-01-26 15:57:00 +00:00
///
2001-11-22 06:23:56 +00:00
2003-08-27 06:08:34 +00:00
global $CFG ;
2002-10-19 04:58:14 +00:00
/// Remove any whitespace that may be between HTML tags
2002-08-22 08:31:29 +00:00
$text = eregi_replace ( " >([[:space:]]+)< " , " >< " , $text );
2002-10-19 04:58:14 +00:00
/// Remove any returns that precede or follow HTML tags
2002-09-02 07:41:39 +00:00
$text = eregi_replace ( " ([ \n \r ])< " , " < " , $text );
$text = eregi_replace ( " >([ \n \r ]) " , " > " , $text );
2002-08-22 08:31:29 +00:00
2003-05-05 16:40:46 +00:00
convert_urls_into_links ( $text );
2001-11-22 06:23:56 +00:00
2002-10-19 04:58:14 +00:00
/// Make returns into HTML newlines.
2004-03-09 06:44:27 +00:00
if ( $newlines ) {
$text = nl2br ( $text );
}
2001-11-22 06:23:56 +00:00
2002-10-19 04:58:14 +00:00
/// Turn smileys into images.
2002-05-18 05:22:48 +00:00
if ( $smiley ) {
2003-05-05 16:40:46 +00:00
replace_smilies ( $text );
2002-05-18 05:22:48 +00:00
}
2001-11-22 06:23:56 +00:00
2002-10-19 04:58:14 +00:00
/// Wrap the whole thing in a paragraph tag if required
2002-08-12 13:45:41 +00:00
if ( $para ) {
2004-09-22 14:39:15 +00:00
return '<p>' . $text . '</p>' ;
2002-08-12 13:45:41 +00:00
} else {
return $text ;
}
2001-11-22 06:23:56 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Given Markdown formatted text , make it into XHTML using external function
*
2004-09-25 05:29:21 +00:00
* @ uses $CFG
* @ param string $text The markdown formatted text to be converted .
* @ return string Converted text
2004-09-23 04:36:43 +00:00
*/
2004-07-30 04:02:58 +00:00
function markdown_to_html ( $text ) {
global $CFG ;
2004-09-22 14:39:15 +00:00
require_once ( $CFG -> libdir . '/markdown.php' );
2004-07-30 04:02:58 +00:00
return Markdown ( $text );
}
2004-09-23 04:36:43 +00:00
/**
2004-09-25 05:29:21 +00:00
* Given HTML text , make it into plain text using external function
2004-09-23 04:36:43 +00:00
*
* @ uses $CFG
* @ param string $html The text to be converted .
* @ return string
*/
2003-11-07 03:27:21 +00:00
function html_to_text ( $html ) {
2004-09-25 05:29:21 +00:00
2003-11-07 04:14:38 +00:00
global $CFG ;
2003-11-07 03:27:21 +00:00
2004-09-22 14:39:15 +00:00
require_once ( $CFG -> libdir . '/html2text.php' );
2003-11-07 03:27:21 +00:00
return html2text ( $html );
}
2004-09-23 04:36:43 +00:00
/**
* Given some text this function converts any URLs it finds into HTML links
*
* @ param string $text Passed in by reference . The string to be searched for urls .
*/
2003-05-05 16:40:46 +00:00
function convert_urls_into_links ( & $text ) {
/// Make lone URLs into links. eg http://moodle.com/
2003-07-02 11:59:20 +00:00
$text = eregi_replace ( " ([[:space:]]|^| \ (| \ [)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=]) " ,
2005-02-25 13:11:18 +00:00
" \\ 1<a href= \" \\ 2:// \\ 3 \\ 4 \" target= \" _blank \" > \\ 2:// \\ 3 \\ 4</a> " , $text );
2003-05-05 16:40:46 +00:00
/// eg www.moodle.com
2004-05-21 13:07:11 +00:00
$text = eregi_replace ( " ([[:space:]]|^| \ (| \ [)www \ .([^[:space:]]*)([[:alnum:]#?/&=]) " ,
2005-02-25 13:11:18 +00:00
" \\ 1<a href= \" http://www. \\ 2 \\ 3 \" target= \" _blank \" >www. \\ 2 \\ 3</a> " , $text );
2003-05-05 16:40:46 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* This function will highlight search words in a given string
* It cares about HTML and will not ruin links . It ' s best to use
* this function after performing any conversions to HTML .
* Function found here : http :// forums . devshed . com / t67822 / scdaa2d1c3d4bacb4671d075ad41f0854 . html
*
2004-09-25 05:29:21 +00:00
* @ param string $needle The string to search for
* @ param string $haystack The string to search for $needle in
* @ param int $case ?
* @ return string
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2004-05-21 13:07:11 +00:00
function highlight ( $needle , $haystack , $case = 0 ,
2004-09-22 14:39:15 +00:00
$left_string = '<span class="highlight">' , $right_string = '</span>' ) {
2003-12-13 06:27:25 +00:00
if ( empty ( $needle )) {
return $haystack ;
}
2005-05-03 16:37:01 +00:00
//$list_of_words = eregi_replace("[^-a-zA-Z0-9&.']", " ", $needle); // bug 3101
$list_of_words = $needle ;
2004-09-22 14:39:15 +00:00
$list_array = explode ( ' ' , $list_of_words );
2003-08-09 03:57:46 +00:00
for ( $i = 0 ; $i < sizeof ( $list_array ); $i ++ ) {
if ( strlen ( $list_array [ $i ]) == 1 ) {
2004-09-22 14:39:15 +00:00
$list_array [ $i ] = '' ;
2003-08-09 03:57:46 +00:00
}
}
2004-09-22 14:39:15 +00:00
$list_of_words = implode ( ' ' , $list_array );
2003-08-09 03:57:46 +00:00
$list_of_words_cp = $list_of_words ;
$final = array ();
preg_match_all ( '/<(.+?)>/is' , $haystack , $list_of_words );
foreach ( array_unique ( $list_of_words [ 0 ]) as $key => $value ) {
$final [ '<|' . $key . '|>' ] = $value ;
}
$haystack = str_replace ( $final , array_keys ( $final ), $haystack );
2004-09-22 14:39:15 +00:00
$list_of_words_cp = eregi_replace ( ' +' , '|' , $list_of_words_cp );
2003-08-09 03:57:46 +00:00
2004-09-22 14:39:15 +00:00
if ( $list_of_words_cp { 0 } == '|' ) {
$list_of_words_cp { 0 } = '' ;
2003-08-09 03:57:46 +00:00
}
2004-09-22 14:39:15 +00:00
if ( $list_of_words_cp { strlen ( $list_of_words_cp ) - 1 } == '|' ) {
$list_of_words_cp { strlen ( $list_of_words_cp ) - 1 } = '' ;
2003-08-09 03:57:46 +00:00
}
2004-12-31 15:23:50 +00:00
$list_of_words_cp = trim ( $list_of_words_cp );
if ( $list_of_words_cp ) {
$list_of_words_cp = " ( " . $list_of_words_cp . " ) " ;
if ( ! $case ){
$haystack = eregi_replace ( " $list_of_words_cp " , " $left_string " . " \\ 1 " . " $right_string " , $haystack );
} else {
$haystack = ereg_replace ( " $list_of_words_cp " , " $left_string " . " \\ 1 " . " $right_string " , $haystack );
}
2003-08-09 03:57:46 +00:00
}
$haystack = str_replace ( array_keys ( $final ), $final , $haystack );
2004-09-23 07:41:37 +00:00
return $haystack ;
2003-08-09 03:57:46 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* This function will highlight instances of $needle in $haystack
* It 's faster that the above function and doesn' t care about
* HTML or anything .
*
* @ param string $needle The string to search for
* @ param string $haystack The string to search for $needle in
* @ return string
*/
2003-08-09 03:57:46 +00:00
function highlightfast ( $needle , $haystack ) {
2002-06-11 07:01:51 +00:00
2006-10-07 20:47:54 +00:00
$parts = explode ( moodle_strtolower ( $needle ), moodle_strtolower ( $haystack ));
2002-06-11 07:01:51 +00:00
$pos = 0 ;
foreach ( $parts as $key => $part ) {
$parts [ $key ] = substr ( $haystack , $pos , strlen ( $part ));
$pos += strlen ( $part );
2004-09-22 14:39:15 +00:00
$parts [ $key ] .= '<span class="highlight">' . substr ( $haystack , $pos , strlen ( $needle )) . '</span>' ;
2002-06-11 07:01:51 +00:00
$pos += strlen ( $needle );
2004-05-21 13:07:11 +00:00
}
2002-06-11 07:01:51 +00:00
return ( join ( '' , $parts ));
}
2001-11-22 06:23:56 +00:00
2002-12-20 14:44:14 +00:00
/// STANDARD WEB PAGE PARTS ///////////////////////////////////////////////////
2004-09-23 04:36:43 +00:00
/**
* Print a standard header
*
2004-09-25 05:29:21 +00:00
* @ uses $USER
* @ uses $CFG
* @ uses $SESSION
* @ param string $title Appears at the top of the window
* @ param string $heading Appears at the top of the page
* @ param string $navigation Premade navigation string ( for use as breadcrumbs links )
* @ param string $focus Indicates form element to get cursor focus on load eg inputform . password
* @ param string $meta Meta tags to be added to the header
* @ param boolean $cache Should this page be cacheable ?
* @ param string $button HTML code for a button ( usually for module editing )
* @ param string $menu HTML code for a popup menu
* @ param boolean $usexml use XML for this page
* @ param string $bodytags This text will be included verbatim in the < body > tag ( useful for onload () etc )
2006-08-10 05:01:57 +00:00
* @ param bool $return If true , return the visible elements of the header instead of echoing them .
2004-09-23 04:36:43 +00:00
*/
2006-08-10 05:01:57 +00:00
function print_header ( $title = '' , $heading = '' , $navigation = '' , $focus = '' ,
$meta = '' , $cache = true , $button = ' ' , $menu = '' ,
$usexml = false , $bodytags = '' , $return = false ) {
2004-07-14 06:54:43 +00:00
2006-07-30 10:39:21 +00:00
global $USER , $CFG , $THEME , $SESSION , $ME , $SITE , $HTTPSPAGEREQUIRED ;
2006-03-04 16:04:48 +00:00
2006-04-10 14:40:39 +00:00
/// This makes sure that the header is never repeated twice on a page
2006-05-16 20:31:22 +00:00
if ( defined ( 'HEADER_PRINTED' )) {
2006-09-13 09:45:07 +00:00
debugging ( 'print_header() was called more than once - this should not happen. Please check the code for this page closely. Note: error() and redirect() are now safe to call after print_header().' );
2006-04-10 14:40:39 +00:00
return ;
}
2006-05-16 20:31:22 +00:00
define ( 'HEADER_PRINTED' , 'true' );
2006-07-30 10:39:21 +00:00
global $course , $COURSE ;
2006-05-29 08:22:52 +00:00
if ( ! empty ( $COURSE -> lang )) {
$CFG -> courselang = $COURSE -> lang ;
2006-07-30 10:39:21 +00:00
moodle_setlocale ();
} else if ( ! empty ( $course -> lang )) { // ugly backwards compatibility hack
$CFG -> courselang = $course -> lang ;
moodle_setlocale ();
2004-02-09 07:31:04 +00:00
}
2006-05-29 08:22:52 +00:00
if ( ! empty ( $COURSE -> theme )) {
2005-02-10 10:28:27 +00:00
if ( ! empty ( $CFG -> allowcoursethemes )) {
2006-05-29 08:22:52 +00:00
$CFG -> coursetheme = $COURSE -> theme ;
2005-02-10 10:28:27 +00:00
theme_setup ();
}
2006-07-30 10:39:21 +00:00
} else if ( ! empty ( $course -> theme )) { // ugly backwards compatibility hack
if ( ! empty ( $CFG -> allowcoursethemes )) {
$CFG -> coursetheme = $course -> theme ;
theme_setup ();
}
2005-02-10 10:28:27 +00:00
}
2004-02-09 07:31:04 +00:00
2005-07-14 22:29:41 +00:00
/// We have to change some URLs in styles if we are in a $HTTPSPAGEREQUIRED page
if ( ! empty ( $HTTPSPAGEREQUIRED )) {
2006-05-15 21:36:46 +00:00
$CFG -> themewww = str_replace ( 'http:' , 'https:' , $CFG -> themewww );
$CFG -> pixpath = str_replace ( 'http:' , 'https:' , $CFG -> pixpath );
$CFG -> modpixpath = str_replace ( 'http:' , 'https:' , $CFG -> modpixpath );
2005-07-14 22:29:41 +00:00
foreach ( $CFG -> stylesheets as $key => $stylesheet ) {
2006-05-15 21:36:46 +00:00
$CFG -> stylesheets [ $key ] = str_replace ( 'http:' , 'https:' , $stylesheet );
2005-07-14 22:29:41 +00:00
}
}
2005-03-26 06:51:33 +00:00
2005-01-30 18:23:06 +00:00
/// Add the required stylesheets
2005-01-31 05:59:13 +00:00
$stylesheetshtml = '' ;
foreach ( $CFG -> stylesheets as $stylesheet ) {
$stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="' . $stylesheet . '" />' . " \n " ;
2005-01-30 18:23:06 +00:00
}
2005-01-31 05:59:13 +00:00
$meta = $stylesheetshtml . $meta ;
2005-01-25 15:53:13 +00:00
2002-12-20 14:44:14 +00:00
2004-09-22 14:39:15 +00:00
if ( $navigation == 'home' ) {
2002-12-20 14:44:14 +00:00
$home = true ;
2004-09-22 14:39:15 +00:00
$navigation = '' ;
2002-12-29 04:17:32 +00:00
} else {
$home = false ;
2002-12-20 14:44:14 +00:00
}
2005-03-26 06:51:33 +00:00
/// This is another ugly hack to make navigation elements available to print_footer later
$THEME -> title = $title ;
$THEME -> heading = $heading ;
$THEME -> navigation = $navigation ;
$THEME -> button = $button ;
$THEME -> menu = $menu ;
2005-03-29 14:22:13 +00:00
$navmenulist = isset ( $THEME -> navmenulist ) ? $THEME -> navmenulist : '' ;
2005-03-26 06:51:33 +00:00
2004-09-22 14:39:15 +00:00
if ( $button == '' ) {
$button = ' ' ;
2002-12-20 14:44:14 +00:00
}
if ( ! $menu and $navigation ) {
2004-06-19 16:13:28 +00:00
if ( empty ( $CFG -> loginhttps )) {
$wwwroot = $CFG -> wwwroot ;
} else {
2006-05-15 21:36:46 +00:00
$wwwroot = str_replace ( 'http:' , 'https:' , $CFG -> wwwroot );
2004-06-19 16:13:28 +00:00
}
2005-03-01 02:49:10 +00:00
if ( isset ( $course -> id )) {
2005-03-02 19:49:04 +00:00
$menu = user_login_string ( $course );
2002-12-20 14:44:14 +00:00
} else {
2005-03-03 04:41:46 +00:00
$menu = user_login_string ( $SITE );
2002-12-20 14:44:14 +00:00
}
}
2004-08-21 14:21:09 +00:00
2004-07-25 13:47:38 +00:00
if ( isset ( $SESSION -> justloggedin )) {
unset ( $SESSION -> justloggedin );
if ( ! empty ( $CFG -> displayloginfailures )) {
2006-09-14 09:39:23 +00:00
if ( ! empty ( $USER -> username ) and $USER -> username != 'guest' ) {
2004-07-25 13:47:38 +00:00
if ( $count = count_login_failures ( $CFG -> displayloginfailures , $USER -> username , $USER -> lastlogin )) {
$menu .= ' <font size="1">' ;
if ( empty ( $count -> accounts )) {
$menu .= get_string ( 'failedloginattempts' , '' , $count );
} else {
$menu .= get_string ( 'failedloginattemptsall' , '' , $count );
}
2006-09-08 08:46:26 +00:00
if ( has_capability ( 'moodle/site:viewreports' , get_context_instance ( CONTEXT_SYSTEM , SITEID ))) {
2006-03-09 09:47:53 +00:00
$menu .= ' (<a href="' . $CFG -> wwwroot . '/course/report/log/index.php' .
2004-09-16 17:13:57 +00:00
'?chooselog=1&id=1&modid=site_errors">' . get_string ( 'logs' ) . '</a>)' ;
2004-07-25 13:47:38 +00:00
}
$menu .= '</font>' ;
}
}
}
}
2002-12-20 14:44:14 +00:00
2003-11-03 15:17:21 +00:00
2006-01-04 08:23:42 +00:00
$encoding = current_charset ();
2004-09-22 14:39:15 +00:00
$meta = '<meta http-equiv="content-type" content="text/html; charset=' . $encoding . '" />' . " \n " . $meta . " \n " ;
2004-07-21 12:16:36 +00:00
if ( ! $usexml ) {
@ header ( 'Content-type: text/html; charset=' . $encoding );
}
2002-12-20 14:44:14 +00:00
2004-09-22 14:39:15 +00:00
if ( get_string ( 'thisdirection' ) == 'rtl' ) {
$direction = ' dir="rtl"' ;
2002-12-20 14:44:14 +00:00
} else {
2004-09-22 14:39:15 +00:00
$direction = ' dir="ltr"' ;
2002-12-20 14:44:14 +00:00
}
2006-03-22 17:48:53 +00:00
//Accessibility: added the 'lang' attribute to $direction, used in theme <html> tag.
$language = str_replace ( '_utf8' , '' , $CFG -> lang );
$direction .= ' lang="' . $language . '" xml:lang="' . $language . '"' ;
2004-05-21 13:07:11 +00:00
2004-12-10 06:20:54 +00:00
if ( $cache ) { // Allow caching on "back" (but not on normal clicks)
@ header ( 'Cache-Control: private, pre-check=0, post-check=0, max-age=0' );
@ header ( 'Pragma: no-cache' );
2005-01-26 15:57:00 +00:00
@ header ( 'Expires: ' );
2004-12-10 06:20:54 +00:00
} else { // Do everything we can to always prevent clients and proxies caching
2004-07-21 12:16:36 +00:00
@ header ( 'Cache-Control: no-store, no-cache, must-revalidate' );
@ header ( 'Cache-Control: post-check=0, pre-check=0' , false );
@ header ( 'Pragma: no-cache' );
2004-12-10 06:20:54 +00:00
@ header ( 'Expires: Mon, 20 Aug 1969 09:23:00 GMT' );
@ header ( 'Last-Modified: ' . gmdate ( 'D, d M Y H:i:s' ) . ' GMT' );
2004-07-21 12:16:36 +00:00
2004-12-10 06:20:54 +00:00
$meta .= " \n <meta http-equiv= \" pragma \" content= \" no-cache \" /> " ;
$meta .= " \n <meta http-equiv= \" expires \" content= \" 0 \" /> " ;
2003-09-21 15:45:24 +00:00
}
2004-12-10 06:20:54 +00:00
@ header ( 'Accept-Ranges: none' );
2003-09-21 15:45:24 +00:00
if ( $usexml ) { // Added by Gustav Delius / Mad Alex for MathML output
2004-09-21 11:41:58 +00:00
// Modified by Julian Sedding
2003-09-21 15:45:24 +00:00
$currentlanguage = current_language ();
2004-09-21 11:41:58 +00:00
$mathplayer = preg_match ( " /MathPlayer/i " , $_SERVER [ 'HTTP_USER_AGENT' ]);
if ( ! $mathplayer ) {
header ( 'Content-Type: application/xhtml+xml' );
}
2004-09-22 14:39:15 +00:00
echo '<?xml version="1.0" ?>' . " \n " ;
2003-09-21 15:45:24 +00:00
if ( ! empty ( $CFG -> xml_stylesheets )) {
2004-09-22 14:39:15 +00:00
$stylesheets = explode ( ';' , $CFG -> xml_stylesheets );
2003-09-21 15:45:24 +00:00
foreach ( $stylesheets as $stylesheet ) {
2004-09-22 14:39:15 +00:00
echo '<?xml-stylesheet type="text/xsl" href="' . $CFG -> wwwroot . '/' . $stylesheet . '" ?>' . " \n " ;
2003-09-21 15:45:24 +00:00
}
}
2004-09-22 14:39:15 +00:00
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1' ;
2003-09-21 16:35:49 +00:00
if ( ! empty ( $CFG -> xml_doctype_extra )) {
2004-09-22 14:39:15 +00:00
echo ' plus ' . $CFG -> xml_doctype_extra ;
2003-09-21 16:35:49 +00:00
}
2004-09-22 14:39:15 +00:00
echo '//' . strtoupper ( $currentlanguage ) . '" "' . $CFG -> xml_dtd . '">' . " \n " ;
2004-09-21 11:41:58 +00:00
$direction = " xmlns= \" http://www.w3.org/1999/xhtml \"
xmlns : math = \ " http://www.w3.org/1998/Math/MathML \"
xmlns : xlink = \ " http://www.w3.org/1999/xlink \"
$direction " ;
if ( $mathplayer ) {
$meta .= '<object id="mathplayer" classid="clsid:32F66A20-7614-11D4-BD11-00104BD3F987">' . " \n " ;
2004-09-22 14:39:15 +00:00
$meta .= '<!--comment required to prevent this becoming an empty tag-->' . " \n " ;
$meta .= '</object>' . " \n " ;
2004-09-21 11:41:58 +00:00
$meta .= '<?import namespace="math" implementation="#mathplayer" ?>' . " \n " ;
}
2002-12-20 14:44:14 +00:00
}
2005-01-25 11:05:23 +00:00
// Clean up the title
2003-10-24 06:27:15 +00:00
$title = str_replace ( '"' , '"' , $title );
2004-02-19 17:56:05 +00:00
$title = strip_tags ( $title );
2003-10-24 06:27:15 +00:00
2005-01-25 11:05:23 +00:00
// Create class and id for this page
2005-02-02 02:25:50 +00:00
page_id_and_class ( $pageid , $pageclass );
2005-01-25 11:05:23 +00:00
2005-01-29 10:49:48 +00:00
if ( isset ( $course -> id )) {
$pageclass .= ' course-' . $course -> id ;
2005-01-29 12:11:45 +00:00
} else {
$pageclass .= ' course-' . SITEID ;
2005-01-29 10:49:48 +00:00
}
2005-03-21 08:26:14 +00:00
if ( ! empty ( $USER -> editing )) {
$pageclass .= ' editing' ;
}
2006-10-30 09:41:55 +00:00
if ( ! empty ( $CFG -> blocksdrag )) {
$pageclass .= ' drag' ;
}
2005-01-25 11:05:23 +00:00
$bodytags .= ' class="' . $pageclass . '" id="' . $pageid . '"' ;
2005-01-26 15:57:00 +00:00
2006-08-10 05:01:57 +00:00
ob_start ();
2006-09-29 05:43:48 +00:00
include ( $CFG -> header );
2006-08-10 05:01:57 +00:00
$output = ob_get_contents ();
ob_end_clean ();
2004-12-30 12:54:22 +00:00
2005-01-31 07:11:42 +00:00
if ( ! empty ( $CFG -> messaging )) {
2006-08-10 05:01:57 +00:00
$output .= message_popup_window ();
}
if ( $return ) {
return $output ;
} else {
echo $output ;
2005-01-31 07:11:42 +00:00
}
2002-12-20 14:44:14 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* This version of print_header is simpler because the course name does not have to be
* provided explicitly in the strings . It can be used on the site page as in courses
* Eventually all print_header could be replaced by print_header_simple
*
2004-09-25 05:29:21 +00:00
* @ param string $title Appears at the top of the window
* @ param string $heading Appears at the top of the page
* @ param string $navigation Premade navigation string ( for use as breadcrumbs links )
* @ param string $focus Indicates form element to get cursor focus on load eg inputform . password
* @ param string $meta Meta tags to be added to the header
* @ param boolean $cache Should this page be cacheable ?
* @ param string $button HTML code for a button ( usually for module editing )
* @ param string $menu HTML code for a popup menu
* @ param boolean $usexml use XML for this page
* @ param string $bodytags This text will be included verbatim in the < body > tag ( useful for onload () etc )
2006-08-10 05:01:57 +00:00
* @ param bool $return If true , return the visible elements of the header instead of echoing them .
2004-09-23 04:36:43 +00:00
*/
2004-09-22 14:39:15 +00:00
function print_header_simple ( $title = '' , $heading = '' , $navigation = '' , $focus = '' , $meta = '' ,
2006-08-10 05:01:57 +00:00
$cache = true , $button = ' ' , $menu = '' , $usexml = false , $bodytags = '' , $return = false ) {
2004-08-21 20:20:58 +00:00
2006-09-13 04:11:36 +00:00
global $COURSE , $CFG ;
2004-08-21 20:20:58 +00:00
$shortname = '' ;
2006-09-13 04:11:36 +00:00
if ( $COURSE -> category ) {
$shortname = '<a href="' . $CFG -> wwwroot . '/course/view.php?id=' . $COURSE -> id . '">' . $COURSE -> shortname . '</a> ->' ;
2004-08-21 20:20:58 +00:00
}
2006-09-13 05:33:09 +00:00
$output = print_header ( $COURSE -> shortname . ': ' . $title , $COURSE -> fullname . ' ' . $heading , $shortname . ' ' . $navigation , $focus , $meta ,
2006-08-10 06:41:38 +00:00
$cache , $button , $menu , $usexml , $bodytags , true );
2006-08-10 05:01:57 +00:00
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2004-08-21 20:20:58 +00:00
}
2004-08-23 13:18:39 +00:00
2004-09-23 04:36:43 +00:00
/**
* Can provide a course object to make the footer contain a link to
* to the course home page , otherwise the link will go to the site home
*
* @ uses $CFG
* @ uses $USER
2004-09-25 05:29:21 +00:00
* @ param course $course { @ link $COURSE } object containing course information
* @ param ? $usercourse ?
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2006-08-10 05:10:00 +00:00
function print_footer ( $course = NULL , $usercourse = NULL , $return = false ) {
2005-03-26 06:51:33 +00:00
global $USER , $CFG , $THEME ;
2002-12-20 14:44:14 +00:00
/// Course links
if ( $course ) {
2006-01-24 01:40:06 +00:00
if ( is_string ( $course ) && $course == 'none' ) { // Don't print any links etc
2005-01-11 08:26:35 +00:00
$homelink = '' ;
$loggedinas = '' ;
2005-03-26 06:51:33 +00:00
$home = false ;
2006-01-24 01:40:06 +00:00
} else if ( is_string ( $course ) && $course == 'home' ) { // special case for site home page - please do not remove
2005-01-11 08:26:35 +00:00
$course = get_site ();
2005-03-27 17:07:09 +00:00
$homelink = '<div class="sitelink">' .
'<a title="moodle ' . $CFG -> release . ' (' . $CFG -> version . ')" href="http://moodle.org/" target="_blank">' .
'<br /><img width="100" height="30" src="pix/moodlelogo.gif" border="0" alt="moodlelogo" /></a></div>' ;
2005-03-26 06:51:33 +00:00
$home = true ;
2002-12-20 14:44:14 +00:00
} else {
2005-03-27 17:07:09 +00:00
$homelink = '<div class="homelink"><a target="' . $CFG -> framename . '" href="' . $CFG -> wwwroot .
'/course/view.php?id=' . $course -> id . '">' . $course -> shortname . '</a></div>' ;
2005-03-26 06:51:33 +00:00
$home = false ;
2002-12-20 14:44:14 +00:00
}
} else {
2005-01-11 08:26:35 +00:00
$course = get_site (); // Set course as site course by default
2005-03-27 17:07:09 +00:00
$homelink = '<div class="homelink"><a target="' . $CFG -> framename . '" href="' . $CFG -> wwwroot . '/">' . get_string ( 'home' ) . '</a></div>' ;
2005-03-26 06:51:33 +00:00
$home = false ;
2002-12-20 14:44:14 +00:00
}
2005-03-26 06:51:33 +00:00
/// Set up some other navigation links (passed from print_header by ugly hack)
2005-03-29 14:22:13 +00:00
$menu = isset ( $THEME -> menu ) ? str_replace ( 'navmenu' , 'navmenufooter' , $THEME -> menu ) : '' ;
$title = isset ( $THEME -> title ) ? $THEME -> title : '' ;
$button = isset ( $THEME -> button ) ? $THEME -> button : '' ;
$heading = isset ( $THEME -> heading ) ? $THEME -> heading : '' ;
$navigation = isset ( $THEME -> navigation ) ? $THEME -> navigation : '' ;
$navmenulist = isset ( $THEME -> navmenulist ) ? $THEME -> navmenulist : '' ;
2005-03-26 06:51:33 +00:00
2005-03-26 07:26:27 +00:00
2005-01-11 08:26:35 +00:00
/// Set the user link if necessary
if ( ! $usercourse and is_object ( $course )) {
2004-08-02 19:09:06 +00:00
$usercourse = $course ;
}
2005-01-11 08:26:35 +00:00
if ( ! isset ( $loggedinas )) {
$loggedinas = user_login_string ( $usercourse , $USER );
}
2005-03-26 06:51:33 +00:00
if ( $loggedinas == $menu ) {
2005-03-27 05:23:39 +00:00
$menu = '' ;
2005-03-26 06:51:33 +00:00
}
2005-04-01 10:00:18 +00:00
/// Provide some performance info if required
2005-04-06 07:34:05 +00:00
$performanceinfo = '' ;
2006-05-01 23:16:48 +00:00
if ( defined ( 'MDL_PERF' ) || $CFG -> perfdebug > 7 ) {
2005-04-06 07:34:05 +00:00
$perf = get_performance_info ();
2005-04-07 00:11:28 +00:00
if ( defined ( 'MDL_PERFTOLOG' )) {
error_log ( " PERF: " . $perf [ 'txt' ]);
}
2006-09-13 09:45:07 +00:00
if ( defined ( 'MDL_PERFTOFOOT' ) || debugging () || $CFG -> perfdebug > 7 ) {
2005-04-06 07:34:05 +00:00
$performanceinfo = $perf [ 'html' ];
}
2005-04-20 19:12:28 +00:00
}
2005-04-01 10:00:18 +00:00
2005-03-26 06:51:33 +00:00
2005-01-11 08:26:35 +00:00
/// Include the actual footer file
2003-04-25 06:40:30 +00:00
2006-08-10 05:10:00 +00:00
ob_start ();
2006-09-29 05:43:48 +00:00
include ( $CFG -> footer );
2006-08-10 05:10:00 +00:00
$output = ob_get_contents ();
ob_end_clean ();
2005-04-01 05:55:10 +00:00
2006-08-10 05:10:00 +00:00
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2003-04-25 06:40:30 +00:00
}
2005-01-30 18:23:06 +00:00
/**
* Returns the name of the current theme
*
* @ uses $CFG
* @ param $USER
* @ param $SESSION
* @ return string
*/
function current_theme () {
2005-02-10 10:28:27 +00:00
global $CFG , $USER , $SESSION , $course ;
2005-01-30 18:23:06 +00:00
2005-05-09 12:59:06 +00:00
if ( ! empty ( $CFG -> pagetheme )) { // Page theme is for special page-only themes set by code
return $CFG -> pagetheme ;
2005-01-30 18:23:06 +00:00
2005-05-09 16:29:10 +00:00
} else if ( ! empty ( $CFG -> coursetheme ) and ! empty ( $CFG -> allowcoursethemes )) { // Course themes override others
2005-01-30 18:23:06 +00:00
return $CFG -> coursetheme ;
} else if ( ! empty ( $SESSION -> theme )) { // Session theme can override other settings
return $SESSION -> theme ;
2005-02-10 10:28:27 +00:00
} else if ( ! empty ( $USER -> theme ) and ! empty ( $CFG -> allowuserthemes )) { // User theme can override site theme
2005-01-30 18:23:06 +00:00
return $USER -> theme ;
} else {
return $CFG -> theme ;
}
}
2004-09-23 04:36:43 +00:00
/**
* This function is called by stylesheets to set up the header
* approriately as well as the current path
*
* @ uses $CFG
2004-09-25 05:29:21 +00:00
* @ param int $lastmodified ?
* @ param int $lifetime ?
* @ param string $thename ?
2004-09-23 04:36:43 +00:00
*/
2005-04-24 04:13:18 +00:00
function style_sheet_setup ( $lastmodified = 0 , $lifetime = 300 , $themename = '' , $forceconfig = '' , $lang = '' ) {
2003-08-12 07:52:19 +00:00
2005-02-13 08:34:10 +00:00
global $CFG , $THEME ;
2004-05-21 13:07:11 +00:00
2006-04-07 10:18:03 +00:00
// Fix for IE6 caching - we don't want the filemtime('styles.php'), instead use now.
$lastmodified = time ();
2004-09-22 14:39:15 +00:00
header ( 'Last-Modified: ' . gmdate ( " D, d M Y H:i:s " , $lastmodified ) . ' GMT' );
header ( 'Expires: ' . gmdate ( " D, d M Y H:i:s " , time () + $lifetime ) . ' GMT' );
2006-04-07 10:18:03 +00:00
header ( 'Cache-Control: max-age=' . $lifetime );
2004-09-22 14:39:15 +00:00
header ( 'Pragma: ' );
header ( 'Content-type: text/css' ); // Correct MIME type
2003-08-12 07:52:19 +00:00
2006-09-28 10:32:23 +00:00
$DEFAULT_SHEET_LIST = array ( 'styles_layout' , 'styles_fonts' , 'styles_color' );
2005-02-13 08:34:10 +00:00
if ( empty ( $themename )) {
$themename = current_theme (); // So we have something. Normally not needed.
2005-07-05 07:51:11 +00:00
} else {
$themename = clean_param ( $themename , PARAM_SAFEDIR );
2005-02-13 08:34:10 +00:00
}
if ( ! empty ( $forceconfig )) { // Page wants to use the config from this theme instead
unset ( $THEME );
2006-10-09 10:12:41 +00:00
include ( $CFG -> themedir . '/' . $forceconfig . '/' . 'config.php' );
2005-02-13 08:34:10 +00:00
}
/// If this is the standard theme calling us, then find out what sheets we need
if ( $themename == 'standard' ) {
if ( ! isset ( $THEME -> standardsheets ) or $THEME -> standardsheets === true ) { // Use all the sheets we have
$THEME -> sheets = $DEFAULT_SHEET_LIST ;
} else if ( empty ( $THEME -> standardsheets )) { // We can stop right now!
echo " /***** Nothing required from this stylesheet by main theme *****/ \n \n " ;
exit ;
} else { // Use the provided subset only
$THEME -> sheets = $THEME -> standardsheets ;
}
/// If we are a parent theme, then check for parent definitions
} else if ( ! empty ( $THEME -> parent ) && $themename == $THEME -> parent ) {
if ( ! isset ( $THEME -> parentsheets ) or $THEME -> parentsheets === true ) { // Use all the sheets we have
$THEME -> sheets = $DEFAULT_SHEET_LIST ;
} else if ( empty ( $THEME -> parentsheets )) { // We can stop right now!
echo " /***** Nothing required from this stylesheet by main theme *****/ \n \n " ;
exit ;
} else { // Use the provided subset only
$THEME -> sheets = $THEME -> parentsheets ;
}
}
/// Work out the last modified date for this theme
foreach ( $THEME -> sheets as $sheet ) {
2006-10-09 10:12:41 +00:00
if ( file_exists ( $CFG -> themedir . '/' . $themename . '/' . $sheet . '.css' )) {
$sheetmodified = filemtime ( $CFG -> themedir . '/' . $themename . '/' . $sheet . '.css' );
2005-02-13 08:34:10 +00:00
if ( $sheetmodified > $lastmodified ) {
$lastmodified = $sheetmodified ;
}
}
2003-08-12 07:52:19 +00:00
}
2005-03-24 13:41:42 +00:00
/// Get a list of all the files we want to include
$files = array ();
foreach ( $THEME -> sheets as $sheet ) {
$files [] = array ( $CFG -> themedir , $themename . '/' . $sheet . '.css' );
}
if ( $themename == 'standard' ) { // Add any standard styles included in any modules
if ( ! empty ( $THEME -> modsheets )) { // Search for styles.php within activity modules
if ( $mods = get_list_of_plugins ( 'mod' )) {
foreach ( $mods as $mod ) {
if ( file_exists ( $CFG -> dirroot . '/mod/' . $mod . '/styles.php' )) {
$files [] = array ( $CFG -> dirroot , '/mod/' . $mod . '/styles.php' );
}
2005-03-22 17:31:04 +00:00
}
}
}
2005-04-03 12:15:45 +00:00
2005-03-24 13:41:42 +00:00
if ( ! empty ( $THEME -> blocksheets )) { // Search for styles.php within block modules
if ( $mods = get_list_of_plugins ( 'blocks' )) {
foreach ( $mods as $mod ) {
if ( file_exists ( $CFG -> dirroot . '/blocks/' . $mod . '/styles.php' )) {
$files [] = array ( $CFG -> dirroot , '/blocks/' . $mod . '/styles.php' );
}
2005-03-22 17:31:04 +00:00
}
}
}
2005-04-03 12:15:45 +00:00
2005-03-24 13:41:42 +00:00
if ( ! empty ( $THEME -> langsheets )) { // Search for styles.php within the current language
if ( file_exists ( $CFG -> dirroot . '/lang/' . $lang . '/styles.php' )) {
$files [] = array ( $CFG -> dirroot , '/lang/' . $lang . '/styles.php' );
}
}
2005-03-22 17:31:04 +00:00
}
2005-03-24 13:41:42 +00:00
if ( $files ) {
/// Produce a list of all the files first
echo '/**************************************' . " \n " ;
echo ' * THEME NAME: ' . $themename . " \n * \n " ;
echo ' * Files included in this sheet:' . " \n * \n " ;
foreach ( $files as $file ) {
echo ' * ' . $file [ 1 ] . " \n " ;
2005-03-22 17:31:04 +00:00
}
2005-03-24 13:41:42 +00:00
echo ' **************************************/' . " \n \n " ;
2005-03-22 17:31:04 +00:00
2005-03-24 13:41:42 +00:00
/// Actually output all the files in order.
foreach ( $files as $file ) {
echo '/***** ' . $file [ 1 ] . ' start *****/' . " \n \n " ;
2006-10-09 10:12:41 +00:00
@ include_once ( $file [ 0 ] . '/' . $file [ 1 ]);
2005-03-24 13:41:42 +00:00
echo '/***** ' . $file [ 1 ] . ' end *****/' . " \n \n " ;
}
2005-02-13 08:34:10 +00:00
}
2006-10-09 10:12:41 +00:00
return $CFG -> themewww . '/' . $themename ; // Only to help old themes (1.4 and earlier)
2003-08-12 07:52:19 +00:00
}
2005-02-13 08:34:10 +00:00
function theme_setup ( $theme = '' , $params = NULL ) {
2005-01-31 05:59:13 +00:00
/// Sets up global variables related to themes
2005-02-13 09:02:43 +00:00
global $CFG , $THEME , $SESSION , $USER ;
2005-01-31 05:59:13 +00:00
if ( empty ( $theme )) {
$theme = current_theme ();
}
2005-02-13 08:34:10 +00:00
2006-09-14 05:15:06 +00:00
/// If the theme doesn't exist for some reason then revert to standardwhite
2006-10-09 10:12:41 +00:00
if ( ! file_exists ( $CFG -> themedir . '/' . $theme . '/config.php' )) {
2006-09-14 05:15:06 +00:00
$CFG -> theme = $theme = 'standardwhite' ;
}
2005-04-24 04:13:18 +00:00
/// Load up the theme config
$THEME = NULL ; // Just to be sure
2006-10-09 10:12:41 +00:00
include ( $CFG -> themedir . '/' . $theme . '/config.php' ); // Main config for current theme
2005-04-24 04:13:18 +00:00
2005-02-13 08:34:10 +00:00
/// Put together the parameters
if ( ! $params ) {
$params = array ();
}
2005-02-13 09:02:43 +00:00
if ( $theme != $CFG -> theme ) {
$params [] = 'forceconfig=' . $theme ;
2005-02-13 08:34:10 +00:00
}
2005-04-24 04:13:18 +00:00
/// Force language too if required
if ( ! empty ( $THEME -> langsheets )) {
$params [] = 'lang=' . current_language ();
}
/// Convert params to string
2005-02-13 08:34:10 +00:00
if ( $params ) {
2005-04-24 04:13:18 +00:00
$paramstring = '?' . implode ( '&' , $params );
2005-01-31 05:59:13 +00:00
} else {
2005-02-13 08:34:10 +00:00
$paramstring = '' ;
2005-01-31 05:59:13 +00:00
}
2005-02-13 08:34:10 +00:00
/// Set up image paths
2005-05-22 15:34:07 +00:00
if ( empty ( $THEME -> custompix )) { // Could be set in the above file
2005-01-31 05:59:13 +00:00
$CFG -> pixpath = $CFG -> wwwroot . '/pix' ;
$CFG -> modpixpath = $CFG -> wwwroot . '/mod' ;
} else {
2006-10-09 10:12:41 +00:00
$CFG -> pixpath = $CFG -> themewww . '/' . $theme . '/pix' ;
$CFG -> modpixpath = $CFG -> themewww . '/' . $theme . '/pix/mod' ;
2005-01-31 05:59:13 +00:00
}
2005-02-13 08:34:10 +00:00
/// Header and footer paths
2006-10-09 10:12:41 +00:00
$CFG -> header = $CFG -> themedir . '/' . $theme . '/header.html' ;
$CFG -> footer = $CFG -> themedir . '/' . $theme . '/footer.html' ;
2005-01-31 05:59:13 +00:00
2005-02-13 08:34:10 +00:00
/// Define stylesheet loading order
2005-01-31 05:59:13 +00:00
$CFG -> stylesheets = array ();
if ( $theme != 'standard' ) { /// The standard sheet is always loaded first
2006-10-09 10:12:41 +00:00
$CFG -> stylesheets [] = $CFG -> themewww . '/standard/styles.php' . $paramstring ;
2005-01-31 05:59:13 +00:00
}
if ( ! empty ( $THEME -> parent )) { /// Parent stylesheets are loaded next
2006-10-09 10:12:41 +00:00
$CFG -> stylesheets [] = $CFG -> themewww . '/' . $THEME -> parent . '/styles.php' . $paramstring ;
2005-01-31 05:59:13 +00:00
}
2006-10-09 10:12:41 +00:00
$CFG -> stylesheets [] = $CFG -> themewww . '/' . $theme . '/styles.php' . $paramstring ;
2005-01-31 05:59:13 +00:00
}
2005-01-30 18:23:06 +00:00
2004-09-23 04:36:43 +00:00
/**
* Returns text to be displayed to the user which reflects their login status
*
* @ uses $CFG
* @ uses $USER
2004-09-25 05:29:21 +00:00
* @ param course $course { @ link $COURSE } object containing course information
* @ param user $user { @ link $USER } object containing user information
* @ return string
2004-09-23 04:36:43 +00:00
*/
2005-03-03 04:41:46 +00:00
function user_login_string ( $course = NULL , $user = NULL ) {
global $USER , $CFG , $SITE ;
2003-04-25 06:40:30 +00:00
2006-09-02 11:44:04 +00:00
if ( empty ( $user ) and ! empty ( $USER -> id )) {
2003-04-25 06:40:30 +00:00
$user = $USER ;
}
2005-03-03 04:41:46 +00:00
if ( empty ( $course )) {
$course = $SITE ;
}
2003-04-25 06:40:30 +00:00
if ( isset ( $user -> realuser )) {
2004-09-22 14:39:15 +00:00
if ( $realuser = get_record ( 'user' , 'id' , $user -> realuser )) {
2003-11-28 11:00:46 +00:00
$fullname = fullname ( $realuser , true );
$realuserinfo = " [<a target= \" { $CFG -> framename } \"
2006-03-03 19:50:33 +00:00
href = \ " $CFG->wwwroot /course/loginas.php?id= $course->id &return=1 \" > $fullname </a>] " ;
2002-12-20 14:44:14 +00:00
}
2002-12-29 04:17:32 +00:00
} else {
2004-09-22 14:39:15 +00:00
$realuserinfo = '' ;
2002-12-20 14:44:14 +00:00
}
2004-06-22 18:35:59 +00:00
if ( empty ( $CFG -> loginhttps )) {
$wwwroot = $CFG -> wwwroot ;
} else {
2006-05-15 21:36:46 +00:00
$wwwroot = str_replace ( 'http:' , 'https:' , $CFG -> wwwroot );
2004-06-22 18:35:59 +00:00
}
2006-09-17 17:53:20 +00:00
if ( empty ( $course -> id )) {
// $course->id is not defined during installation
return '' ;
} else if ( isset ( $user -> id ) and $user -> id ) {
2006-09-21 15:58:59 +00:00
$context = get_context_instance ( CONTEXT_COURSE , $course -> id );
2003-11-28 11:00:46 +00:00
$fullname = fullname ( $user , true );
$username = " <a target= \" { $CFG -> framename } \" href= \" $CFG->wwwroot /user/view.php?id= $user->id &course= $course->id\ " > $fullname </ a > " ;
2006-09-14 09:39:23 +00:00
if ( isset ( $user -> username ) && $user -> username == 'guest' ) {
2005-05-05 04:22:26 +00:00
$loggedinas = $realuserinfo . get_string ( 'loggedinasguest' ) .
2004-09-22 14:39:15 +00:00
" (<a target= \" { $CFG -> framename } \" href= \" $wwwroot /login/index.php \" > " . get_string ( 'login' ) . '</a>)' ;
2006-09-21 15:58:59 +00:00
} else if ( ! empty ( $user -> switchrole [ $context -> id ])) {
2006-09-21 14:42:45 +00:00
$rolename = '' ;
2006-09-21 15:58:59 +00:00
if ( $role = get_record ( 'role' , 'id' , $user -> switchrole [ $context -> id ])) {
2006-09-21 14:42:45 +00:00
$rolename = ': ' . format_string ( $role -> name );
}
$loggedinas = get_string ( 'loggedinas' , 'moodle' , $username ) . $rolename .
" (<a target= \" { $CFG -> framename } \"
href = \ " $CFG->wwwroot /course/view.php?id= $course->id &switchrole=0&sesskey= " . sesskey () . " \" > " . get_string ( 'switchrolereturn' ) . '</a>)' ;
2004-05-30 00:55:48 +00:00
} else {
2006-09-03 08:10:51 +00:00
$loggedinas = $realuserinfo . get_string ( 'loggedinas' , 'moodle' , $username ) . ' ' .
2004-09-22 14:39:15 +00:00
" (<a target= \" { $CFG -> framename } \" href= \" $CFG->wwwroot /login/logout.php \" > " . get_string ( 'logout' ) . '</a>)' ;
2004-05-30 00:55:48 +00:00
}
2002-12-20 14:44:14 +00:00
} else {
2004-09-22 14:39:15 +00:00
$loggedinas = get_string ( 'loggedinnot' , 'moodle' ) .
" (<a target= \" { $CFG -> framename } \" href= \" $wwwroot /login/index.php \" > " . get_string ( 'login' ) . '</a>)' ;
2002-12-20 14:44:14 +00:00
}
2005-02-25 06:20:53 +00:00
return '<div class="logininfo">' . $loggedinas . '</div>' ;
2002-12-20 14:44:14 +00:00
}
2004-09-23 04:36:43 +00:00
/**
2006-09-19 11:36:21 +00:00
* Tests whether $THEME -> rarrow , $THEME -> larrow have been set ( theme /-/ config . php ) .
* If not it applies sensible defaults .
2004-09-23 04:36:43 +00:00
*
2006-09-19 11:36:21 +00:00
* Accessibility : right and left arrow Unicode characters for breadcrumb , calendar ,
* search forum block , etc . Important : these are 'silent' in a screen - reader
* ( unlike & gt ; & raquo ;), and must be accompanied by text .
* @ uses $THEME
2004-09-23 04:36:43 +00:00
*/
2006-09-19 11:36:21 +00:00
function check_theme_arrows () {
global $THEME ;
if ( ! isset ( $THEME -> rarrow ) and ! isset ( $THEME -> larrow )) {
$THEME -> rarrow = '►' ;
$THEME -> larrow = '◄' ;
if ( FALSE !== strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'Opera' )) {
$THEME -> rarrow = '▶' ;
$THEME -> larrow = '◀' ;
}
}
}
2006-08-10 06:41:38 +00:00
2006-09-19 11:36:21 +00:00
/**
* Prints breadcrumb trail of links , called in theme /-/ header . html
*
* @ uses $CFG
* @ param string $navigation The breadcrumb navigation string to be printed
* @ param string $separator The breadcrumb trail separator . The default 0 leads to the use
* of $THEME -> rarrow , themes could use '→' , '/' , or '' for a style - sheet solution .
* @ param boolean $return False to echo the breadcrumb string ( default ), true to return it .
*/
function print_navigation ( $navigation , $separator = 0 , $return = false ) {
global $CFG , $THEME ;
2006-08-10 06:41:38 +00:00
$output = '' ;
2006-09-19 11:36:21 +00:00
check_theme_arrows ();
if ( 0 === $separator ) {
$separator = $THEME -> rarrow ;
}
if ( ! empty ( $separator )) {
$separator = '<span class="sep">' . $separator . '</span>' ;
}
2006-08-10 06:41:38 +00:00
if ( $navigation ) {
2006-09-19 11:36:21 +00:00
//Accessibility: breadcrumb links now in a list, » replaced with a 'silent' character.
2006-08-10 06:41:38 +00:00
$nav_text = get_string ( 'youarehere' , 'access' );
2006-09-19 11:36:21 +00:00
$output .= '<h2 class="accesshide">' . $nav_text . " </h2><ul> \n " ;
2006-08-10 06:41:38 +00:00
if ( ! $site = get_site ()) {
$site -> shortname = get_string ( 'home' );
}
2006-09-19 11:36:21 +00:00
$navigation = " <li> $separator " . str_replace ( '->' , " </li> \n <li> $separator " , $navigation ) . " </li> \n " ;
2006-09-08 08:46:26 +00:00
$output .= '<li class="first"><a target="' . $CFG -> framename . '" href="' . $CFG -> wwwroot . (( ! has_capability ( 'moodle/site:config' , get_context_instance ( CONTEXT_SYSTEM , SITEID )) && ! empty ( $USER -> id ) && ! empty ( $CFG -> mymoodleredirect ) && ! isguest ())
2006-03-08 12:33:21 +00:00
? '/my' : '' ) . '/">' . $site -> shortname . " </a></li> \n " . $navigation ;
2006-08-10 06:41:38 +00:00
$output .= " </ul> \n " ;
}
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2002-12-20 14:44:14 +00:00
}
2004-09-23 04:36:43 +00:00
/**
2005-02-09 08:29:58 +00:00
* Prints a string in a specified size ( retained for backward compatibility )
2004-09-23 04:36:43 +00:00
*
2004-09-25 05:29:21 +00:00
* @ param string $text The text to be displayed
* @ param int $size The size to set the font for text display .
2004-09-23 04:36:43 +00:00
*/
2006-08-10 06:41:38 +00:00
function print_headline ( $text , $size = 2 , $return = false ) {
$output = print_heading ( $text , 'left' , $size , true );
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2003-07-21 04:53:55 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Prints text in a format for use in headings .
*
2004-09-25 05:29:21 +00:00
* @ param string $text The text to be displayed
* @ param string $align The alignment of the printed paragraph of text
* @ param int $size The size to set the font for text display .
2004-09-23 04:36:43 +00:00
*/
2006-08-10 06:41:38 +00:00
function print_heading ( $text , $align = '' , $size = 2 , $class = 'main' , $return = false ) {
2005-02-09 08:29:58 +00:00
if ( $align ) {
$align = ' align="' . $align . '"' ;
}
2005-03-13 12:56:36 +00:00
if ( $class ) {
$class = ' class="' . $class . '"' ;
}
2006-08-10 06:41:38 +00:00
$output = " <h $size $align $class > " . stripslashes_safe ( $text ) . " </h $size > " ;
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2002-12-20 14:44:14 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Centered heading with attached help button ( same title text )
* and optional icon attached
*
2004-09-25 05:29:21 +00:00
* @ param string $text The text to be displayed
* @ param string $helppage The help page to link to
* @ param string $module The module whose help should be linked to
* @ param string $icon Image to display if needed
2004-09-23 04:36:43 +00:00
*/
2006-08-10 06:41:38 +00:00
function print_heading_with_help ( $text , $helppage , $module = 'moodle' , $icon = '' , $return = false ) {
$output = '' ;
$output .= '<h2 class="main help">' . $icon . stripslashes_safe ( $text );
$output .= helpbutton ( $helppage , $text , $module , true , false , '' , true );
$output .= '</h2>' ;
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2002-12-20 14:44:14 +00:00
}
2004-05-21 13:07:11 +00:00
2005-03-13 14:54:24 +00:00
2006-08-10 06:41:38 +00:00
function print_heading_block ( $heading , $class = '' , $return = false ) {
2006-02-28 12:29:51 +00:00
//Accessibility: 'headingblock' is now H1, see theme/standard/styles_*.css: ??
2006-08-10 06:41:38 +00:00
$output = '<h2 class="headingblock header ' . $class . '">' . stripslashes ( $heading ) . '</h2>' ;
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2005-03-13 14:54:24 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Print a link to continue on to another page .
*
* @ uses $CFG
* @ param string $link The url to create a link to .
*/
2006-08-10 06:41:38 +00:00
function print_continue ( $link , $return = false ) {
2002-12-20 14:44:14 +00:00
2004-08-07 13:43:01 +00:00
global $CFG ;
2006-08-17 22:37:34 +00:00
// in case we are logging upgrade in admin/index.php stop it
if ( function_exists ( 'upgrade_log_finish' )) {
upgrade_log_finish ();
}
2006-08-10 06:41:38 +00:00
$output = '' ;
2002-12-20 14:44:14 +00:00
if ( ! $link ) {
2004-09-22 14:39:15 +00:00
$link = $_SERVER [ 'HTTP_REFERER' ];
2002-12-20 14:44:14 +00:00
}
2006-08-10 06:41:38 +00:00
$output .= '<div class="continuebutton">' ;
$output .= print_single_button ( $link , NULL , get_string ( 'continue' ), 'post' , $CFG -> framename , true );
$output .= '</div>' . " \n " ;
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2002-12-20 14:44:14 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Print a message in a standard themed box .
2006-04-06 17:21:04 +00:00
* See , { @ link print_simple_box_start } .
2004-09-23 04:36:43 +00:00
*
2006-04-06 17:21:04 +00:00
* @ param string $align string , alignment of the box , not the text ( default center , left , right ) .
* @ param string $width string , width of the box , including units % , for example '100%' .
* @ param string $color string , background colour of the box , for example '#eee' .
* @ param int $padding integer , padding in pixels , specified without units .
* @ param string $class string , space - separated class names .
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2006-08-10 06:41:38 +00:00
function print_simple_box ( $message , $align = '' , $width = '' , $color = '' , $padding = 5 , $class = 'generalbox' , $id = '' , $return = false ) {
$output = '' ;
$output .= print_simple_box_start ( $align , $width , $color , $padding , $class , $id , true );
$output .= stripslashes_safe ( $message );
$output .= print_simple_box_end ( true );
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2002-12-20 14:44:14 +00:00
}
2004-09-23 04:36:43 +00:00
/**
2006-04-10 01:39:38 +00:00
* Print the top portion of a standard themed box using a TABLE . Yes , we know .
* See bug 4943 for details on some accessibility work regarding this that didn ' t make it into 1.6 .
2006-04-06 17:21:04 +00:00
*
* @ param string $align string , alignment of the box , not the text ( default center , left , right ) .
* @ param string $width string , width of the box , including % units , for example '100%' .
2006-03-16 11:03:02 +00:00
* @ param string $color string , background colour of the box , for example '#eee' .
* @ param int $padding integer , padding in pixels , specified without units .
* @ param string $class string , space - separated class names .
2004-09-23 04:36:43 +00:00
*/
2006-08-10 06:41:38 +00:00
function print_simple_box_start ( $align = '' , $width = '' , $color = '' , $padding = 5 , $class = 'generalbox' , $id = '' , $return = false ) {
$output = '' ;
2002-12-20 14:44:14 +00:00
2005-01-25 13:36:57 +00:00
if ( $color ) {
2006-04-10 01:39:38 +00:00
$color = 'bgcolor="' . $color . '"' ;
2006-03-21 16:29:50 +00:00
}
2006-04-10 01:39:38 +00:00
if ( $align ) {
$align = 'align="' . $align . '"' ;
2006-03-21 16:29:50 +00:00
}
2002-12-20 14:44:14 +00:00
if ( $width ) {
2006-04-10 01:39:38 +00:00
$width = 'width="' . $width . '"' ;
2002-12-20 14:44:14 +00:00
}
2005-02-07 11:08:37 +00:00
if ( $id ) {
$id = 'id="' . $id . '"' ;
2006-04-10 01:39:38 +00:00
}
2006-08-10 06:41:38 +00:00
$output .= " <table $align $width $id class= \" $class\ " border = \ " 0 \" cellpadding= \" $padding\ " cellspacing = \ " 0 \" > " .
2006-04-10 01:39:38 +00:00
" <tr><td $color class= \" $class " . " content \" > " ;
2006-08-10 06:41:38 +00:00
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2002-12-20 14:44:14 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Print the end portion of a standard themed box .
*/
2006-08-10 06:41:38 +00:00
function print_simple_box_end ( $return = false ) {
$output = '</td></tr></table>' ;
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2002-12-20 14:44:14 +00:00
}
2006-04-10 01:39:38 +00:00
2004-09-23 04:36:43 +00:00
/**
* Print a self contained form with a single submit button .
*
2004-09-25 05:29:21 +00:00
* @ param string $link ?
* @ param array $options ?
* @ param string $label ?
* @ param string $method ?
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2006-08-10 06:41:38 +00:00
function print_single_button ( $link , $options , $label = 'OK' , $method = 'get' , $target = '_self' , $return = false ) {
$output = '' ;
$output .= '<div class="singlebutton">' ;
$output .= '<form action="' . $link . '" method="' . $method . '" target="' . $target . '">' ;
2002-12-20 14:44:14 +00:00
if ( $options ) {
foreach ( $options as $name => $value ) {
2006-08-10 06:41:38 +00:00
$output .= '<input type="hidden" name="' . $name . '" value="' . $value . '" />' ;
2002-12-20 14:44:14 +00:00
}
}
2006-08-10 06:41:38 +00:00
$output .= '<input type="submit" value="' . $label . '" /></form></div>' ;
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2002-12-20 14:44:14 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Print a spacer image with the option of including a line break .
*
2004-09-25 05:29:21 +00:00
* @ param int $height ?
* @ param int $width ?
* @ param boolean $br ?
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2006-08-10 06:41:38 +00:00
function print_spacer ( $height = 1 , $width = 1 , $br = true , $return = false ) {
2002-12-20 14:44:14 +00:00
global $CFG ;
2006-08-10 06:41:38 +00:00
$output = '' ;
$output .= '<img class="spacer" height="' . $height . '" width="' . $width . '" src="' . $CFG -> wwwroot . '/pix/spacer.gif" alt="" />' ;
2002-12-20 14:44:14 +00:00
if ( $br ) {
2006-08-10 06:41:38 +00:00
$output .= '<br />' . " \n " ;
}
if ( $return ) {
return $output ;
} else {
echo $output ;
2002-12-20 14:44:14 +00:00
}
}
2004-09-23 04:36:43 +00:00
/**
* Given the path to a picture file in a course , or a URL ,
* this function includes the picture in the page .
*
2004-09-25 05:29:21 +00:00
* @ param string $path ?
* @ param int $courseid ?
* @ param int $height ?
* @ param int $width ?
* @ param string $link ?
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2006-08-10 06:41:38 +00:00
function print_file_picture ( $path , $courseid = 0 , $height = '' , $width = '' , $link = '' , $return = false ) {
2002-12-20 14:44:14 +00:00
global $CFG ;
2006-08-10 06:41:38 +00:00
$output = '' ;
2002-12-20 14:44:14 +00:00
if ( $height ) {
2004-09-22 14:39:15 +00:00
$height = 'height="' . $height . '"' ;
2002-12-20 14:44:14 +00:00
}
if ( $width ) {
2004-09-22 14:39:15 +00:00
$width = 'width="' . $width . '"' ;
2002-12-20 14:44:14 +00:00
}
if ( $link ) {
2006-08-10 06:41:38 +00:00
$output .= '<a href="' . $link . '">' ;
2002-12-20 14:44:14 +00:00
}
2004-09-22 14:39:15 +00:00
if ( substr ( strtolower ( $path ), 0 , 7 ) == 'http://' ) {
2006-08-10 06:41:38 +00:00
$output .= '<img border="0" ' . $height . $width . ' src="' . $path . '" />' ;
2002-12-20 14:44:14 +00:00
} else if ( $courseid ) {
2006-08-10 06:41:38 +00:00
$output .= '<img border="0" ' . $height . $width . ' src="' ;
2002-12-20 14:44:14 +00:00
if ( $CFG -> slasharguments ) { // Use this method if possible for better caching
2006-08-10 06:41:38 +00:00
$output .= $CFG -> wwwroot . '/file.php/' . $courseid . '/' . $path ;
2002-12-20 14:44:14 +00:00
} else {
2006-08-10 06:41:38 +00:00
$output .= $CFG -> wwwroot . '/file.php?file=/' . $courseid . '/' . $path ;
2002-12-20 14:44:14 +00:00
}
2006-08-10 06:41:38 +00:00
$output .= '" />' ;
2002-12-20 14:44:14 +00:00
} else {
2006-08-10 06:41:38 +00:00
$output .= 'Error: must pass URL or course' ;
2002-12-20 14:44:14 +00:00
}
if ( $link ) {
2006-08-10 06:41:38 +00:00
$output .= '</a>' ;
}
if ( $return ) {
return $output ;
} else {
echo $output ;
2002-12-20 14:44:14 +00:00
}
}
2004-09-23 04:36:43 +00:00
/**
* Print the specified user ' s avatar .
*
2004-09-25 05:29:21 +00:00
* @ param int $userid ?
* @ param int $courseid ?
* @ param boolean $picture Print the user picture ?
2004-12-23 08:58:15 +00:00
* @ param int $size Size in pixels . Special values are ( true / 1 = 100 px ) and ( false / 0 = 35 px ) for backward compatability
2006-08-10 06:41:38 +00:00
* @ param boolean $return If false print picture to current page , otherwise return the output as string
2004-09-25 05:29:21 +00:00
* @ param boolean $link Enclose printed image in a link to view specified course ?
* return string
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2006-08-10 06:41:38 +00:00
function print_user_picture ( $userid , $courseid , $picture , $size = 0 , $return = false , $link = true , $target = '' ) {
2003-12-30 17:18:06 +00:00
global $CFG ;
2002-12-20 14:44:14 +00:00
if ( $link ) {
2005-01-01 04:22:19 +00:00
if ( $target ) {
$target = ' target="_blank"' ;
}
$output = '<a ' . $target . ' href="' . $CFG -> wwwroot . '/user/view.php?id=' . $userid . '&course=' . $courseid . '">' ;
2002-12-20 14:44:14 +00:00
} else {
2004-09-22 14:39:15 +00:00
$output = '' ;
2002-12-20 14:44:14 +00:00
}
2004-12-23 08:58:15 +00:00
if ( empty ( $size )) {
$file = 'f2' ;
2005-09-27 10:33:29 +00:00
$size = 35 ;
2004-12-23 08:58:15 +00:00
} else if ( $size === true or $size == 1 ) {
2004-09-22 14:39:15 +00:00
$file = 'f1' ;
2002-12-20 14:44:14 +00:00
$size = 100 ;
2004-12-23 08:58:15 +00:00
} else if ( $size >= 50 ) {
$file = 'f1' ;
2002-12-20 14:44:14 +00:00
} else {
2004-09-22 14:39:15 +00:00
$file = 'f2' ;
2002-12-20 14:44:14 +00:00
}
2005-07-22 14:10:07 +00:00
$class = " userpicture " ;
2003-07-28 11:42:53 +00:00
if ( $picture ) { // Print custom user picture
2002-12-20 14:44:14 +00:00
if ( $CFG -> slasharguments ) { // Use this method if possible for better caching
2005-07-22 14:10:07 +00:00
$src = $CFG -> wwwroot . '/user/pix.php/' . $userid . '/' . $file . '.jpg"' ;
2002-12-20 14:44:14 +00:00
} else {
2005-07-22 14:10:07 +00:00
$src = $CFG -> wwwroot . '/user/pix.php?file=/' . $userid . '/' . $file . '.jpg"' ;
2002-12-20 14:44:14 +00:00
}
2003-07-28 11:42:53 +00:00
} else { // Print default user pictures (use theme version if available)
2005-07-22 14:10:07 +00:00
$class .= " defaultuserpic " ;
$src = " $CFG->pixpath /u/ $file .png \" " ;
2002-12-20 14:44:14 +00:00
}
2005-07-22 14:10:07 +00:00
$output .= " <img class= \" $class\ " align = \ " middle \" src= \" $src " .
" border= \" 0 \" width= \" $size\ " height = \ " $size\ " alt = \ " \" /> " ;
2002-12-20 14:44:14 +00:00
if ( $link ) {
2004-09-22 14:39:15 +00:00
$output .= '</a>' ;
2002-12-20 14:44:14 +00:00
}
2006-08-10 06:41:38 +00:00
if ( $return ) {
2002-12-20 14:44:14 +00:00
return $output ;
} else {
echo $output ;
}
}
2004-09-23 04:36:43 +00:00
/**
* Prints a summary of a user in a nice little box .
*
2004-09-25 05:29:21 +00:00
* @ uses $CFG
* @ uses $USER
* @ param user $user A { @ link $USER } object representing a user
* @ param course $course A { @ link $COURSE } object representing a course
2004-09-23 04:36:43 +00:00
*/
2006-08-10 06:41:38 +00:00
function print_user ( $user , $course , $messageselect = false , $return = false ) {
2004-01-10 16:41:29 +00:00
2004-09-25 05:29:21 +00:00
global $CFG , $USER ;
2004-01-10 16:47:14 +00:00
2006-08-10 06:41:38 +00:00
$output = '' ;
2004-01-10 16:41:29 +00:00
static $string ;
static $datestring ;
static $countries ;
2006-09-07 08:57:56 +00:00
$context = get_context_instance ( CONTEXT_COURSE , $course -> id );
2006-10-01 06:18:18 +00:00
2004-01-10 16:41:29 +00:00
if ( empty ( $string )) { // Cache all the strings for the rest of the page
2004-09-22 14:39:15 +00:00
$string -> email = get_string ( 'email' );
$string -> location = get_string ( 'location' );
$string -> lastaccess = get_string ( 'lastaccess' );
$string -> activity = get_string ( 'activity' );
$string -> unenrol = get_string ( 'unenrol' );
$string -> loginas = get_string ( 'loginas' );
$string -> fullprofile = get_string ( 'fullprofile' );
$string -> role = get_string ( 'role' );
$string -> name = get_string ( 'name' );
$string -> never = get_string ( 'never' );
$datestring -> day = get_string ( 'day' );
$datestring -> days = get_string ( 'days' );
$datestring -> hour = get_string ( 'hour' );
$datestring -> hours = get_string ( 'hours' );
$datestring -> min = get_string ( 'min' );
$datestring -> mins = get_string ( 'mins' );
$datestring -> sec = get_string ( 'sec' );
$datestring -> secs = get_string ( 'secs' );
2004-01-10 16:41:29 +00:00
$countries = get_list_of_countries ();
}
2005-11-20 21:29:59 +00:00
/// Get the hidden field list
2006-10-01 06:18:18 +00:00
if ( has_capability ( 'moodle/course:viewhiddenuserfields' , $context )) {
2005-11-20 21:29:59 +00:00
$hiddenfields = array ();
} else {
$hiddenfields = array_flip ( explode ( ',' , $CFG -> hiddenuserfields ));
}
2006-08-10 06:41:38 +00:00
$output .= '<table class="userinfobox">' ;
$output .= '<tr>' ;
$output .= '<td class="left side">' ;
$output .= print_user_picture ( $user -> id , $course -> id , $user -> picture , true , true );
$output .= '</td>' ;
$output .= '<td class="content">' ;
2006-09-07 08:57:56 +00:00
$output .= '<div class="username">' . fullname ( $user , has_capability ( 'moodle/site:viewfullnames' , $context )) . '</div>' ;
2006-08-10 06:41:38 +00:00
$output .= '<div class="info">' ;
2004-01-10 16:41:29 +00:00
if ( ! empty ( $user -> role ) and ( $user -> role <> $course -> teacher )) {
2006-08-10 06:41:38 +00:00
$output .= $string -> role . ': ' . $user -> role . '<br />' ;
2004-01-10 16:41:29 +00:00
}
2006-10-01 06:18:18 +00:00
if ( $user -> maildisplay == 1 or ( $user -> maildisplay == 2 and $course -> category and ! isguest ()) or
has_capability ( 'moodle/course:viewhiddenuserfields' , $context )) {
2006-08-10 06:41:38 +00:00
$output .= $string -> email . ': <a href="mailto:' . $user -> email . '">' . $user -> email . '</a><br />' ;
2004-01-10 16:41:29 +00:00
}
2005-11-20 21:29:59 +00:00
if (( $user -> city or $user -> country ) and ( ! isset ( $hiddenfields [ 'city' ]) or ! isset ( $hiddenfields [ 'country' ]))) {
2006-08-10 06:41:38 +00:00
$output .= $string -> location . ': ' ;
2005-11-20 21:29:59 +00:00
if ( $user -> city && ! isset ( $hiddenfields [ 'city' ])) {
2006-08-10 06:41:38 +00:00
$output .= $user -> city ;
2004-07-29 14:54:25 +00:00
}
2005-11-20 21:29:59 +00:00
if ( ! empty ( $countries [ $user -> country ]) && ! isset ( $hiddenfields [ 'country' ])) {
if ( $user -> city && ! isset ( $hiddenfields [ 'city' ])) {
2006-08-10 06:41:38 +00:00
$output .= ', ' ;
2004-07-29 14:54:25 +00:00
}
2006-08-10 06:41:38 +00:00
$output .= $countries [ $user -> country ];
2004-07-29 14:54:25 +00:00
}
2006-08-10 06:41:38 +00:00
$output .= '<br />' ;
2004-01-10 16:41:29 +00:00
}
2006-03-16 08:16:08 +00:00
2005-11-20 21:29:59 +00:00
if ( ! isset ( $hiddenfields [ 'lastaccess' ])) {
if ( $user -> lastaccess ) {
2006-08-10 06:41:38 +00:00
$output .= $string -> lastaccess . ': ' . userdate ( $user -> lastaccess );
2006-10-23 13:42:23 +00:00
$output .= ' (' . format_time ( time () - $user -> lastaccess , $datestring ) . ')' ;
2005-11-20 21:29:59 +00:00
} else {
2006-08-10 06:41:38 +00:00
$output .= $string -> lastaccess . ': ' . $string -> never ;
2005-11-20 21:29:59 +00:00
}
2004-01-10 16:41:29 +00:00
}
2006-08-10 06:41:38 +00:00
$output .= '</div></td><td class="links">' ;
2006-03-16 08:16:08 +00:00
//link to blogs
2006-06-26 08:32:39 +00:00
if ( $CFG -> bloglevel > 0 ) {
2006-08-10 06:41:38 +00:00
$output .= '<a href="' . $CFG -> wwwroot . '/blog/index.php?userid=' . $user -> id . '">' . get_string ( 'blogs' , 'blog' ) . '</a><br />' ;
2006-06-26 08:32:39 +00:00
}
2006-08-10 06:41:38 +00:00
2006-10-01 06:18:18 +00:00
if ( has_capability ( 'moodle/site:viewreports' , $context )) {
2004-01-10 16:41:29 +00:00
$timemidnight = usergetmidnight ( time ());
2006-08-10 06:41:38 +00:00
$output .= '<a href="' . $CFG -> wwwroot . '/course/user.php?id=' . $course -> id . '&user=' . $user -> id . '">' . $string -> activity . '</a><br />' ;
2006-10-01 06:18:18 +00:00
}
if ( has_capability ( 'moodle/role:assign' , $context , NULL )) { // Includes admins
$output .= '<a href="' . $CFG -> wwwroot . '/course/unenrol.php?id=' . $course -> id . '&user=' . $user -> id . '">' . $string -> unenrol . '</a><br />' ;
}
if ( $USER -> id != $user -> id && has_capability ( 'moodle/user:loginas' , $context )) {
$output .= '<a href="' . $CFG -> wwwroot . '/course/loginas.php?id=' . $course -> id . '&user=' . $user -> id . '">' . $string -> loginas . '</a><br />' ;
2004-05-21 13:07:11 +00:00
}
2006-08-10 06:41:38 +00:00
$output .= '<a href="' . $CFG -> wwwroot . '/user/view.php?id=' . $user -> id . '&course=' . $course -> id . '">' . $string -> fullprofile . '...</a>' ;
2004-01-10 16:41:29 +00:00
2006-10-01 06:18:18 +00:00
if ( ! empty ( $messageselect )) {
$output .= '<br /><input type="checkbox" name="user' . $user -> id . '" /> ' ;
2005-08-22 05:44:13 +00:00
}
2006-08-10 06:41:38 +00:00
$output .= '</td></tr></table>' ;
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2004-01-10 16:41:29 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Print a specified group ' s avatar .
*
2006-05-23 19:18:25 +00:00
* @ param group $group A { @ link group } object representing a group or array of groups
2004-09-25 05:29:21 +00:00
* @ param int $courseid ?
* @ param boolean $large ?
2006-08-10 06:41:38 +00:00
* @ param boolean $return ?
2004-09-25 05:29:21 +00:00
* @ param boolean $link ?
* @ return string
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2006-08-10 06:41:38 +00:00
function print_group_picture ( $group , $courseid , $large = false , $return = false , $link = true ) {
2003-12-30 17:18:06 +00:00
global $CFG ;
2006-05-23 19:18:25 +00:00
if ( is_array ( $group )) {
$output = '' ;
foreach ( $group as $g ) {
$output .= print_group_picture ( $g , $courseid , $large , true , $link );
}
2006-08-10 06:41:38 +00:00
if ( $return ) {
2006-05-23 19:18:25 +00:00
return $output ;
} else {
echo $output ;
return ;
}
}
2006-09-07 08:57:56 +00:00
$context = get_context_instance ( CONTEXT_COURSE , $courseid );
2004-02-15 07:37:48 +00:00
2006-09-07 08:57:56 +00:00
if ( $group -> hidepicture and ! has_capability ( 'moodle/course:managegroups' , $context )) {
2004-02-15 07:29:55 +00:00
return '' ;
}
2004-02-15 07:13:08 +00:00
2006-09-07 08:57:56 +00:00
if ( $link or has_capability ( 'moodle/site:accessallgroups' , $context )) {
2005-05-05 02:15:21 +00:00
$output = '<a href="' . $CFG -> wwwroot . '/user/index.php?id=' . $courseid . '&group=' . $group -> id . '">' ;
2004-02-15 07:29:55 +00:00
} else {
$output = '' ;
}
if ( $large ) {
2004-09-22 14:39:15 +00:00
$file = 'f1' ;
2004-02-15 07:29:55 +00:00
$size = 100 ;
} else {
2004-09-22 14:39:15 +00:00
$file = 'f2' ;
2004-02-15 07:29:55 +00:00
$size = 35 ;
}
if ( $group -> picture ) { // Print custom group picture
if ( $CFG -> slasharguments ) { // Use this method if possible for better caching
2005-02-16 10:34:02 +00:00
$output .= '<img class="grouppicture" align="middle" src="' . $CFG -> wwwroot . '/user/pixgroup.php/' . $group -> id . '/' . $file . '.jpg"' .
' border="0" width="' . $size . '" height="' . $size . '" alt="" title="' . s ( $group -> name ) . '"/>' ;
2003-12-31 13:39:08 +00:00
} else {
2005-02-16 10:34:02 +00:00
$output .= '<img class="grouppicture" align="middle" src="' . $CFG -> wwwroot . '/user/pixgroup.php?file=/' . $group -> id . '/' . $file . '.jpg"' .
' border="0" width="' . $size . '" height="' . $size . '" alt="" title="' . s ( $group -> name ) . '"/>' ;
2003-12-31 13:39:08 +00:00
}
2003-12-30 17:18:06 +00:00
}
2006-09-07 08:57:56 +00:00
if ( $link or has_capability ( 'moodle/site:accessallgroups' , $context )) {
2004-09-22 14:39:15 +00:00
$output .= '</a>' ;
2004-02-15 07:29:55 +00:00
}
2003-12-30 17:18:06 +00:00
2006-08-10 06:41:38 +00:00
if ( $return ) {
2003-12-30 17:18:06 +00:00
return $output ;
} else {
echo $output ;
}
}
2004-09-23 04:36:43 +00:00
/**
* Print a png image .
*
2004-09-25 05:29:21 +00:00
* @ param string $url ?
* @ param int $sizex ?
* @ param int $sizey ?
2006-08-10 06:41:38 +00:00
* @ param boolean $return ?
2004-09-25 05:29:21 +00:00
* @ param string $parameters ?
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2006-08-10 06:41:38 +00:00
function print_png ( $url , $sizex , $sizey , $return , $parameters = 'alt=""' ) {
2004-02-10 14:22:20 +00:00
global $CFG ;
static $recentIE ;
if ( ! isset ( $recentIE )) {
$recentIE = check_browser_version ( 'MSIE' , '5.0' );
}
if ( $recentIE ) { // work around the HORRIBLE bug IE has with alpha transparencies
2004-09-25 05:29:21 +00:00
$output .= '<img src="' . $CFG -> pixpath . '/spacer.gif" width="' . $sizex . '" height="' . $sizey . '"' .
2005-03-07 11:53:00 +00:00
' border="0" class="png" style="width: ' . $sizex . 'px; height: ' . $sizey . 'px; ' .
2004-09-25 05:29:21 +00:00
' filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=' .
" ' $url ', sizingMethod='scale') " .
' ' . $parameters . ' />' ;
2004-02-10 14:22:20 +00:00
} else {
2004-09-25 05:29:21 +00:00
$output .= '<img src="' . $url . '" border="0" width="' . $sizex . '" height="' . $sizey . '" ' .
' ' . $parameters . ' />' ;
2004-02-10 14:22:20 +00:00
}
2006-08-10 06:41:38 +00:00
if ( $return ) {
2004-02-10 14:22:20 +00:00
return $output ;
} else {
echo $output ;
}
}
2004-09-23 04:36:43 +00:00
/**
* Print a nicely formatted table .
*
2004-09-25 05:29:21 +00:00
* @ param array $table is an object with several properties .
* < ul < li > $table -> head - An array of heading names .
* < li > $table -> align - An array of column alignments
* < li > $table -> size - An array of column sizes
* < li > $table -> wrap - An array of " nowrap " s or nothing
* < li > $table -> data [] - An array of arrays containing the data .
* < li > $table -> width - A percentage of the page
2006-03-20 15:33:18 +00:00
* < li > $table -> tablealign - Align the whole table
2004-09-25 05:29:21 +00:00
* < li > $table -> cellpadding - Padding on each cell
* < li > $table -> cellspacing - Spacing between cells
* </ ul >
2006-06-01 05:51:45 +00:00
* @ param bool $return whether to return an output string or echo now
* @ return boolean or $string
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2006-06-01 05:51:45 +00:00
function print_table ( $table , $return = false ) {
$output = '' ;
2002-12-20 14:44:14 +00:00
if ( isset ( $table -> align )) {
foreach ( $table -> align as $key => $aa ) {
if ( $aa ) {
2004-09-22 14:39:15 +00:00
$align [ $key ] = ' align="' . $aa . '"' ;
2002-12-20 14:44:14 +00:00
} else {
2004-09-22 14:39:15 +00:00
$align [ $key ] = '' ;
2002-12-20 14:44:14 +00:00
}
}
}
if ( isset ( $table -> size )) {
foreach ( $table -> size as $key => $ss ) {
if ( $ss ) {
2004-09-22 14:39:15 +00:00
$size [ $key ] = ' width="' . $ss . '"' ;
2002-12-20 14:44:14 +00:00
} else {
2004-09-22 14:39:15 +00:00
$size [ $key ] = '' ;
2002-12-20 14:44:14 +00:00
}
}
}
2003-05-09 17:24:17 +00:00
if ( isset ( $table -> wrap )) {
foreach ( $table -> wrap as $key => $ww ) {
if ( $ww ) {
2004-09-22 14:39:15 +00:00
$wrap [ $key ] = ' nowrap="nowrap" ' ;
2003-05-09 17:24:17 +00:00
} else {
2004-09-22 14:39:15 +00:00
$wrap [ $key ] = '' ;
2003-05-09 17:24:17 +00:00
}
}
}
2002-12-20 14:44:14 +00:00
2002-12-29 04:17:32 +00:00
if ( empty ( $table -> width )) {
2004-09-22 14:39:15 +00:00
$table -> width = '80%' ;
2002-12-20 14:44:14 +00:00
}
2006-03-20 15:33:18 +00:00
if ( empty ( $table -> tablealign )) {
$table -> tablealign = 'center' ;
}
2002-12-29 04:17:32 +00:00
if ( empty ( $table -> cellpadding )) {
2004-09-22 14:39:15 +00:00
$table -> cellpadding = '5' ;
2002-12-20 14:44:14 +00:00
}
2002-12-29 04:17:32 +00:00
if ( empty ( $table -> cellspacing )) {
2004-09-22 14:39:15 +00:00
$table -> cellspacing = '1' ;
2002-12-20 14:44:14 +00:00
}
2005-02-07 11:08:37 +00:00
if ( empty ( $table -> class )) {
$table -> class = 'generaltable' ;
}
$tableid = empty ( $table -> id ) ? '' : 'id="' . $table -> id . '"' ;
2006-06-01 05:51:45 +00:00
$output .= '<table width="' . $table -> width . '" border="0" align="' . $table -> tablealign . '" ' ;
$output .= " cellpadding= \" $table->cellpadding\ " cellspacing = \ " $table->cellspacing\ " class = \ " $table->class\ " $tableid > \n " ;
2002-12-20 14:44:14 +00:00
2004-01-26 09:48:00 +00:00
$countcols = 0 ;
2003-01-01 15:13:21 +00:00
if ( ! empty ( $table -> head )) {
2005-07-09 09:04:50 +00:00
$countcols = count ( $table -> head );
2006-06-01 05:51:45 +00:00
$output .= '<tr>' ;
2002-12-20 14:44:14 +00:00
foreach ( $table -> head as $key => $heading ) {
2004-05-21 13:07:11 +00:00
2002-12-29 04:17:32 +00:00
if ( ! isset ( $size [ $key ])) {
2004-09-22 14:39:15 +00:00
$size [ $key ] = '' ;
2004-05-21 13:07:11 +00:00
}
2002-12-29 04:17:32 +00:00
if ( ! isset ( $align [ $key ])) {
2004-09-22 14:39:15 +00:00
$align [ $key ] = '' ;
2004-05-21 13:07:11 +00:00
}
2006-06-01 05:51:45 +00:00
$output .= '<th valign="top" ' . $align [ $key ] . $size [ $key ] . ' nowrap="nowrap" class="header c' . $key . '">' . $heading . '</th>' ;
2002-12-20 14:44:14 +00:00
}
2006-06-01 05:51:45 +00:00
$output .= '</tr>' . " \n " ;
2002-12-20 14:44:14 +00:00
}
2003-09-23 12:18:27 +00:00
if ( ! empty ( $table -> data )) {
2005-01-30 05:21:25 +00:00
$oddeven = 1 ;
foreach ( $table -> data as $key => $row ) {
$oddeven = $oddeven ? 0 : 1 ;
2006-06-01 05:51:45 +00:00
$output .= '<tr class="r' . $oddeven . '">' . " \n " ;
2004-09-22 14:39:15 +00:00
if ( $row == 'hr' and $countcols ) {
2006-06-01 05:51:45 +00:00
$output .= '<td colspan="' . $countcols . '"><div class="tabledivider"></div></td>' ;
2004-01-26 09:48:00 +00:00
} else { /// it's a normal row of data
foreach ( $row as $key => $item ) {
if ( ! isset ( $size [ $key ])) {
2004-09-22 14:39:15 +00:00
$size [ $key ] = '' ;
2004-05-21 13:07:11 +00:00
}
2004-01-26 09:48:00 +00:00
if ( ! isset ( $align [ $key ])) {
2004-09-22 14:39:15 +00:00
$align [ $key ] = '' ;
2004-05-21 13:07:11 +00:00
}
2004-01-26 09:48:00 +00:00
if ( ! isset ( $wrap [ $key ])) {
2004-09-22 14:39:15 +00:00
$wrap [ $key ] = '' ;
2004-05-21 13:07:11 +00:00
}
2006-06-01 05:51:45 +00:00
$output .= '<td ' . $align [ $key ] . $size [ $key ] . $wrap [ $key ] . ' class="cell c' . $key . '">' . $item . '</td>' ;
2004-01-26 09:48:00 +00:00
}
2003-09-23 12:18:27 +00:00
}
2006-06-01 05:51:45 +00:00
$output .= '</tr>' . " \n " ;
2002-12-20 14:44:14 +00:00
}
}
2006-06-01 05:51:45 +00:00
$output .= '</table>' . " \n " ;
2002-12-20 14:44:14 +00:00
2006-06-01 05:51:45 +00:00
if ( $return ) {
return $output ;
}
echo $output ;
2002-12-20 14:44:14 +00:00
return true ;
}
2004-09-23 04:36:43 +00:00
/**
* Creates a nicely formatted table and returns it .
*
2004-09-25 05:29:21 +00:00
* @ param array $table is an object with several properties .
* < ul < li > $table -> head - An array of heading names .
* < li > $table -> align - An array of column alignments
* < li > $table -> size - An array of column sizes
* < li > $table -> wrap - An array of " nowrap " s or nothing
* < li > $table -> data [] - An array of arrays containing the data .
* < li > $table -> class - A css class name
* < li > $table -> fontsize - Is the size of all the text
* < li > $table -> tablealign - Align the whole table
* < li > $table -> width - A percentage of the page
* < li > $table -> cellpadding - Padding on each cell
* < li > $table -> cellspacing - Spacing between cells
* </ ul >
* @ return string
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2003-08-18 16:40:27 +00:00
function make_table ( $table ) {
if ( isset ( $table -> align )) {
foreach ( $table -> align as $key => $aa ) {
if ( $aa ) {
2004-09-22 14:39:15 +00:00
$align [ $key ] = ' align="' . $aa . '"' ;
2003-08-18 16:40:27 +00:00
} else {
2004-09-22 14:39:15 +00:00
$align [ $key ] = '' ;
2003-08-18 16:40:27 +00:00
}
}
}
if ( isset ( $table -> size )) {
foreach ( $table -> size as $key => $ss ) {
if ( $ss ) {
2004-09-22 14:39:15 +00:00
$size [ $key ] = ' width="' . $ss . '"' ;
2003-08-18 16:40:27 +00:00
} else {
2004-09-22 14:39:15 +00:00
$size [ $key ] = '' ;
2003-08-18 16:40:27 +00:00
}
}
}
if ( isset ( $table -> wrap )) {
foreach ( $table -> wrap as $key => $ww ) {
if ( $ww ) {
2004-09-22 14:39:15 +00:00
$wrap [ $key ] = ' nowrap="nowrap" ' ;
2003-08-18 16:40:27 +00:00
} else {
2004-09-22 14:39:15 +00:00
$wrap [ $key ] = '' ;
2003-08-18 16:40:27 +00:00
}
}
}
if ( empty ( $table -> width )) {
2004-09-22 14:39:15 +00:00
$table -> width = '80%' ;
2003-08-18 16:40:27 +00:00
}
if ( empty ( $table -> tablealign )) {
2004-09-22 14:39:15 +00:00
$table -> tablealign = 'center' ;
2003-08-18 16:40:27 +00:00
}
if ( empty ( $table -> cellpadding )) {
2004-09-22 14:39:15 +00:00
$table -> cellpadding = '5' ;
2003-08-18 16:40:27 +00:00
}
if ( empty ( $table -> cellspacing )) {
2004-09-22 14:39:15 +00:00
$table -> cellspacing = '1' ;
2003-08-18 16:40:27 +00:00
}
if ( empty ( $table -> class )) {
2004-09-22 14:39:15 +00:00
$table -> class = 'generaltable' ;
2003-08-18 16:40:27 +00:00
}
if ( empty ( $table -> fontsize )) {
2004-09-22 14:39:15 +00:00
$fontsize = '' ;
2003-08-18 16:40:27 +00:00
} else {
2004-09-22 14:39:15 +00:00
$fontsize = '<font size="' . $table -> fontsize . '">' ;
2003-08-18 16:40:27 +00:00
}
2005-01-13 07:55:24 +00:00
$output = '<table width="' . $table -> width . '" align="' . $table -> tablealign . '" ' ;
2004-09-22 14:39:15 +00:00
$output .= ' cellpadding="' . $table -> cellpadding . '" cellspacing="' . $table -> cellspacing . '" class="' . $table -> class . '">' . " \n " ;
2003-08-18 16:40:27 +00:00
if ( ! empty ( $table -> head )) {
2005-01-13 07:55:24 +00:00
$output .= '<tr valign="top">' ;
2003-08-18 16:40:27 +00:00
foreach ( $table -> head as $key => $heading ) {
if ( ! isset ( $size [ $key ])) {
2004-09-22 14:39:15 +00:00
$size [ $key ] = '' ;
2004-05-21 13:07:11 +00:00
}
2003-08-18 16:40:27 +00:00
if ( ! isset ( $align [ $key ])) {
2004-09-22 14:39:15 +00:00
$align [ $key ] = '' ;
2004-05-21 13:07:11 +00:00
}
2004-09-22 14:39:15 +00:00
$output .= '<th valign="top" ' . $align [ $key ] . $size [ $key ] . ' nowrap="nowrap" class="' . $table -> class . 'header">' . $fontsize . $heading . '</th>' ;
2003-08-18 16:40:27 +00:00
}
2004-09-22 14:39:15 +00:00
$output .= '</tr>' . " \n " ;
2003-08-18 16:40:27 +00:00
}
foreach ( $table -> data as $row ) {
2004-09-22 14:39:15 +00:00
$output .= '<tr valign="top">' ;
2003-08-18 16:40:27 +00:00
foreach ( $row as $key => $item ) {
if ( ! isset ( $size [ $key ])) {
2004-09-22 14:39:15 +00:00
$size [ $key ] = '' ;
2004-05-21 13:07:11 +00:00
}
2003-08-18 16:40:27 +00:00
if ( ! isset ( $align [ $key ])) {
2004-09-22 14:39:15 +00:00
$align [ $key ] = '' ;
2004-05-21 13:07:11 +00:00
}
2003-08-18 16:40:27 +00:00
if ( ! isset ( $wrap [ $key ])) {
2004-09-22 14:39:15 +00:00
$wrap [ $key ] = '' ;
2004-05-21 13:07:11 +00:00
}
2004-09-22 14:39:15 +00:00
$output .= '<td ' . $align [ $key ] . $size [ $key ] . $wrap [ $key ] . ' class="' . $table -> class . 'cell">' . $fontsize . $item . '</td>' ;
2003-08-18 16:40:27 +00:00
}
2004-09-22 14:39:15 +00:00
$output .= '</tr>' . " \n " ;
2003-08-18 16:40:27 +00:00
}
2004-09-22 14:39:15 +00:00
$output .= '</table>' . " \n " ;
2003-08-18 16:40:27 +00:00
return $output ;
}
2006-09-24 14:35:48 +00:00
function print_recent_activity_note ( $time , $user , $text , $link , $return = false ) {
2005-03-10 13:30:57 +00:00
static $strftimerecent ;
2006-08-10 06:41:38 +00:00
$output = '' ;
2006-09-24 14:35:48 +00:00
$context = get_context_instance ( CONTEXT_SYSTEM , SITEID );
$viewfullnames = has_capability ( 'moodle/site:viewfullnames' , $context );
2005-03-10 13:30:57 +00:00
if ( empty ( $strftimerecent )) {
$strftimerecent = get_string ( 'strftimerecent' );
}
$date = userdate ( $time , $strftimerecent );
2006-09-24 14:35:48 +00:00
$name = fullname ( $user , $viewfullnames );
2005-03-10 13:30:57 +00:00
2006-08-10 06:41:38 +00:00
$output .= '<div class="head">' ;
$output .= '<div class="date">' . $date . '</div> ' .
2006-09-24 14:35:48 +00:00
'<div class="name">' . fullname ( $user , $viewfullnames ) . '</div>' ;
2006-08-10 06:41:38 +00:00
$output .= '</div>' ;
$output .= '<div class="info"><a href="' . $link . '">' . format_string ( $text , true ) . '</a></div>' ;
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2005-03-10 13:30:57 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Prints a basic textarea field .
*
2004-09-25 05:29:21 +00:00
* @ uses $CFG
* @ param boolean $usehtmleditor ?
* @ param int $rows ?
* @ param int $cols ?
2005-02-16 10:34:02 +00:00
* @ param null $width < b > Legacy field no longer used !</ b > Set to zero to get control over mincols
* @ param null $height < b > Legacy field no longer used !</ b > Set to zero to get control over minrows
2004-09-25 05:29:21 +00:00
* @ param string $name ?
* @ param string $value ?
* @ param int $courseid ?
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2006-03-15 05:38:02 +00:00
function print_textarea ( $usehtmleditor , $rows , $cols , $width , $height , $name , $value = '' , $courseid = 0 , $return = false ) {
2005-02-25 13:11:18 +00:00
/// $width and height are legacy fields and no longer used as pixels like they used to be.
/// However, you can set them to zero to override the mincols and minrows values below.
2003-10-29 08:06:11 +00:00
2003-11-03 15:17:21 +00:00
global $CFG , $course ;
2005-04-20 19:12:28 +00:00
static $scriptcount ; // For loading the htmlarea script only once.
2003-10-12 17:12:02 +00:00
2005-02-25 13:11:18 +00:00
$mincols = 65 ;
$minrows = 10 ;
2006-03-16 03:56:19 +00:00
$str = '' ;
2006-03-04 16:04:48 +00:00
if ( empty ( $CFG -> editorsrc ) ) { // for backward compatibility.
if ( empty ( $courseid )) {
if ( ! empty ( $course -> id )) { // search for it in global context
$courseid = $course -> id ;
}
2003-10-12 17:12:02 +00:00
}
2002-12-20 14:44:14 +00:00
2006-03-04 16:04:48 +00:00
if ( empty ( $scriptcount )) {
$scriptcount = 0 ;
}
2005-04-20 19:12:28 +00:00
2006-03-04 16:04:48 +00:00
if ( $usehtmleditor ) {
2006-10-02 19:47:11 +00:00
if ( ! empty ( $courseid ) and has_capability ( 'moodle/course:managefiles' , get_context_instance ( CONTEXT_COURSE , $courseid ))) {
// needed for course file area browsing in image insert plugin
2006-03-16 03:56:19 +00:00
$str .= ( $scriptcount < 1 ) ? '<script type="text/javascript" src="' .
2006-03-04 16:04:48 +00:00
$CFG -> wwwroot . '/lib/editor/htmlarea/htmlarea.php?id=' . $courseid . '"></script>' . " \n " : '' ;
} else {
2006-03-16 03:56:19 +00:00
$str .= ( $scriptcount < 1 ) ? '<script type="text/javascript" src="' .
2006-03-04 16:04:48 +00:00
$CFG -> wwwroot . '/lib/editor/htmlarea/htmlarea.php"></script>' . " \n " : '' ;
}
2006-03-15 05:38:02 +00:00
$str .= ( $scriptcount < 1 ) ? '<script type="text/javascript" src="' .
2006-03-04 16:04:48 +00:00
$CFG -> wwwroot . '/lib/editor/htmlarea/lang/en.php"></script>' . " \n " : '' ;
$scriptcount ++ ;
2003-10-12 17:12:02 +00:00
2006-03-04 16:04:48 +00:00
if ( $height ) { // Usually with legacy calls
if ( $rows < $minrows ) {
$rows = $minrows ;
}
2005-02-16 10:34:02 +00:00
}
2006-03-04 16:04:48 +00:00
if ( $width ) { // Usually with legacy calls
if ( $cols < $mincols ) {
$cols = $mincols ;
}
2005-02-16 10:34:02 +00:00
}
2003-10-29 08:06:11 +00:00
}
2002-12-20 14:44:14 +00:00
}
2006-09-25 08:20:14 +00:00
$str .= '<textarea class="form=textarea" id="edit-' . $name . '" name="' . $name . '" rows="' . $rows . '" cols="' . $cols . '">' ;
2005-10-13 20:12:23 +00:00
if ( $usehtmleditor ) {
2006-10-07 17:20:35 +00:00
$str .= htmlspecialchars ( $value ); // needed for editing of cleaned text!
2005-10-13 20:12:23 +00:00
} else {
2006-03-15 05:38:02 +00:00
$str .= s ( $value );
2005-10-13 20:12:23 +00:00
}
2006-03-15 05:38:02 +00:00
$str .= '</textarea>' . " \n " ;
if ( $return ) {
return $str ;
}
echo $str ;
2002-12-20 14:44:14 +00:00
}
2004-09-23 04:36:43 +00:00
/**
2005-01-26 15:57:00 +00:00
* Legacy function , provided for backward compatability .
2004-09-23 04:36:43 +00:00
* This method now simply calls { @ link use_html_editor ()}
*
2004-09-25 05:29:21 +00:00
* @ deprecated Use { @ link use_html_editor ()} instead .
* @ param string $name Form element to replace with HTMl editor by name
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2004-09-22 14:39:15 +00:00
function print_richedit_javascript ( $form , $name , $source = 'no' ) {
2003-11-03 15:17:21 +00:00
use_html_editor ( $name );
}
2004-09-23 04:36:43 +00:00
/**
* Sets up the HTML editor on textareas in the current page .
* If a field name is provided , then it will only be
* applied to that field - otherwise it will be used
* on every textarea in the page .
*
* In most cases no arguments need to be supplied
*
2004-09-25 05:29:21 +00:00
* @ param string $name Form element to replace with HTMl editor by name
2004-09-23 04:36:43 +00:00
*/
2005-02-16 10:34:02 +00:00
function use_html_editor ( $name = '' , $editorhidebuttons = '' ) {
2006-07-20 06:32:38 +00:00
$editor = 'editor_' . md5 ( $name ); //name might contain illegal characters
2004-09-22 14:39:15 +00:00
echo '<script language="javascript" type="text/javascript" defer="defer">' . " \n " ;
2006-07-20 06:32:38 +00:00
echo " $editor = new HTMLArea('edit- $name '); \n " ;
echo " var config = $editor .config; \n " ;
2006-03-15 08:30:03 +00:00
2006-03-14 05:26:03 +00:00
echo print_editor_config ( $editorhidebuttons );
2006-03-15 08:30:03 +00:00
2003-11-03 15:17:21 +00:00
if ( empty ( $name )) {
2006-07-20 06:32:38 +00:00
echo " \n HTMLArea.replaceAll( $editor .config); \n " ;
2003-11-03 15:17:21 +00:00
} else {
2006-07-20 06:32:38 +00:00
echo " \n $editor .generate(); \n " ;
2003-10-29 08:06:11 +00:00
}
2004-09-22 14:39:15 +00:00
echo '</script>' . " \n " ;
2002-12-20 14:44:14 +00:00
}
2006-03-14 05:26:03 +00:00
function print_editor_config ( $editorhidebuttons = '' , $return = false ) {
global $CFG ;
2006-03-15 03:12:28 +00:00
2006-03-15 08:40:56 +00:00
$str = " config.pageStyle = \" body { " ;
2006-03-14 05:26:03 +00:00
if ( ! ( empty ( $CFG -> editorbackgroundcolor ))) {
$str .= " background-color: $CFG->editorbackgroundcolor ; " ;
}
if ( ! ( empty ( $CFG -> editorfontfamily ))) {
$str .= " font-family: $CFG->editorfontfamily ; " ;
}
if ( ! ( empty ( $CFG -> editorfontsize ))) {
$str .= " font-size: $CFG->editorfontsize ; " ;
}
$str .= " } \" ; \n " ;
$str .= " config.killWordOnPaste = " ;
$str .= ( empty ( $CFG -> editorkillword )) ? " false " : " true " ;
$str .= ';' . " \n " ;
$str .= 'config.fontname = {' . " \n " ;
$fontlist = isset ( $CFG -> editorfontlist ) ? explode ( ';' , $CFG -> editorfontlist ) : array ();
$i = 1 ; // Counter is used to get rid of the last comma.
foreach ( $fontlist as $fontline ) {
if ( ! empty ( $fontline )) {
if ( $i > 1 ) {
$str .= ',' . " \n " ;
}
list ( $fontkey , $fontvalue ) = split ( ':' , $fontline );
$str .= '"' . $fontkey . " \" : \t ' " . $fontvalue . " ' " ;
$i ++ ;
}
}
$str .= '};' ;
if ( ! empty ( $editorhidebuttons )) {
$str .= " \n config.hideSomeButtons( \" " . $editorhidebuttons . " \" ); \n " ;
} else if ( ! empty ( $CFG -> editorhidebuttons )) {
$str .= " \n config.hideSomeButtons( \" " . $CFG -> editorhidebuttons . " \" ); \n " ;
}
if ( ! empty ( $CFG -> editorspelling ) && ! empty ( $CFG -> aspellpath )) {
$str .= print_speller_code ( $usehtmleditor = true , true );
}
if ( $return ) {
return $str ;
}
echo $str ;
}
2004-09-23 04:36:43 +00:00
/**
* Returns a turn edit on / off button for course in a self contained form .
* Used to be an icon , but it ' s now a simple form button
*
2004-09-25 05:29:21 +00:00
* @ uses $CFG
* @ uses $USER
* @ param int $courseid The course to update by id as found in 'course' table
* @ return string
2004-09-23 04:36:43 +00:00
*/
2002-12-20 14:44:14 +00:00
function update_course_icon ( $courseid ) {
2005-01-26 15:57:00 +00:00
2002-12-20 14:44:14 +00:00
global $CFG , $USER ;
2006-09-07 08:57:56 +00:00
if ( has_capability ( 'moodle/course:manageactivities' , get_context_instance ( CONTEXT_COURSE , $courseid ))) {
2002-12-29 17:32:32 +00:00
if ( ! empty ( $USER -> editing )) {
2004-09-22 14:39:15 +00:00
$string = get_string ( 'turneditingoff' );
2006-04-23 20:58:06 +00:00
$edit = '0' ;
2002-12-20 14:44:14 +00:00
} else {
2004-09-22 14:39:15 +00:00
$string = get_string ( 'turneditingon' );
2006-04-23 20:58:06 +00:00
$edit = '1' ;
2002-12-20 14:44:14 +00:00
}
2003-12-14 15:03:23 +00:00
return " <form target= \" $CFG->framename\ " method = \ " get \" action= \" $CFG->wwwroot /course/view.php \" > " .
2005-08-15 12:53:57 +00:00
" <input type= \" hidden \" name= \" id \" value= \" $courseid\ " /> " .
" <input type= \" hidden \" name= \" edit \" value= \" $edit\ " /> " .
" <input type= \" hidden \" name= \" sesskey \" value= \" " . sesskey () . " \" /> " .
" <input type= \" submit \" value= \" $string\ " /></ form > " ;
}
}
2006-09-21 09:16:41 +00:00
/**
* Returns a little popup menu for switching roles
*
* @ uses $CFG
* @ uses $USER
* @ param int $courseid The course to update by id as found in 'course' table
* @ return string
*/
function switchroles_form ( $courseid ) {
global $CFG , $USER ;
if ( ! $context = get_context_instance ( CONTEXT_COURSE , $courseid )) {
return '' ;
}
2006-09-21 14:04:32 +00:00
if ( empty ( $USER -> switchrole )) { // Try to print a menu
if ( has_capability ( 'moodle/role:switchroles' , $context )) {
2006-09-21 09:16:41 +00:00
if ( ! $roles = get_assignable_roles ( $context )) {
return '' ; // Nothing to show!
}
2006-10-06 05:44:05 +00:00
return popup_form ( $CFG -> wwwroot . '/course/view.php?id=' . $courseid . '&sesskey=' . sesskey () . '&switchrole=' ,
2006-09-21 09:16:41 +00:00
$roles , 'switchrole' , '' , get_string ( 'switchroleto' ), 'switchrole' , '' , true );
2006-09-21 14:04:32 +00:00
} else {
return '' ;
2006-09-21 09:16:41 +00:00
}
2006-09-21 14:04:32 +00:00
} else { // Just a button to return to normal
$options = array ();
$options [ 'id' ] = $courseid ;
$options [ 'sesskey' ] = sesskey ();
$options [ 'switchrole' ] = 0 ;
2006-09-21 09:16:41 +00:00
2006-09-21 14:04:32 +00:00
return print_single_button ( $CFG -> wwwroot . '/course/view.php' , $options ,
get_string ( 'switchrolereturn' ), 'post' , '_self' , true );
}
2006-09-21 09:16:41 +00:00
}
2002-12-20 14:44:14 +00:00
2005-08-16 00:25:39 +00:00
/**
* Returns a turn edit on / off button for course in a self contained form .
* Used to be an icon , but it ' s now a simple form button
*
* @ uses $CFG
* @ uses $USER
* @ param int $courseid The course to update by id as found in 'course' table
* @ return string
*/
function update_mymoodle_icon () {
global $CFG , $USER ;
2006-03-04 16:04:48 +00:00
2005-08-16 00:25:39 +00:00
if ( ! empty ( $USER -> editing )) {
$string = get_string ( 'updatemymoodleoff' );
2006-04-23 20:58:06 +00:00
$edit = '0' ;
2005-08-16 00:25:39 +00:00
} else {
$string = get_string ( 'updatemymoodleon' );
2006-04-23 20:58:06 +00:00
$edit = '1' ;
2005-08-16 00:25:39 +00:00
}
return " <form target= \" $CFG->framename\ " method = \ " get \" action= \" $CFG->wwwroot /my/index.php \" > " .
" <input type= \" hidden \" name= \" edit \" value= \" $edit\ " /> " .
" <input type= \" submit \" value= \" $string\ " /></ form > " ;
}
2004-09-23 04:36:43 +00:00
/**
* Prints the editing button on a module " view " page
*
* @ uses $CFG
* @ param type description
* @ todo Finish documenting this function
*/
2002-12-20 14:44:14 +00:00
function update_module_button ( $moduleid , $courseid , $string ) {
2004-10-12 18:47:39 +00:00
global $CFG , $USER ;
2002-12-20 14:44:14 +00:00
2006-09-07 08:57:56 +00:00
if ( has_capability ( 'moodle/course:manageactivities' , get_context_instance ( CONTEXT_MODULE , $moduleid ))) {
2004-09-22 14:39:15 +00:00
$string = get_string ( 'updatethis' , '' , $string );
2003-12-14 15:03:23 +00:00
return " <form target= \" $CFG->framename\ " method = \ " get \" action= \" $CFG->wwwroot /course/mod.php \" > " .
2003-09-21 15:45:24 +00:00
" <input type= \" hidden \" name= \" update \" value= \" $moduleid\ " /> " .
" <input type= \" hidden \" name= \" return \" value= \" true \" /> " .
2005-01-31 18:10:50 +00:00
" <input type= \" hidden \" name= \" sesskey \" value= \" " . sesskey () . " \" /> " .
2003-09-21 15:45:24 +00:00
" <input type= \" submit \" value= \" $string\ " /></ form > " ;
2003-08-16 06:44:03 +00:00
} else {
2004-09-22 14:39:15 +00:00
return '' ;
2002-12-20 14:44:14 +00:00
}
}
2004-09-23 04:36:43 +00:00
/**
* Prints the editing button on a category page
*
* @ uses $CFG
* @ uses $USER
2004-09-25 05:29:21 +00:00
* @ param int $categoryid ?
* @ return string
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2003-07-30 13:02:45 +00:00
function update_category_button ( $categoryid ) {
2003-08-07 16:01:31 +00:00
global $CFG , $USER ;
2003-07-30 13:02:45 +00:00
2006-09-07 08:57:56 +00:00
if ( has_capability ( 'moodle/category:update' , get_context_instance ( CONTEXT_COURSECAT , $categoryid ))) {
2003-12-30 17:18:06 +00:00
if ( ! empty ( $USER -> categoryediting )) {
2004-09-22 14:39:15 +00:00
$string = get_string ( 'turneditingoff' );
$edit = 'off' ;
2003-08-07 16:01:31 +00:00
} else {
2004-09-22 14:39:15 +00:00
$string = get_string ( 'turneditingon' );
$edit = 'on' ;
2004-05-21 13:07:11 +00:00
}
2003-12-14 15:03:23 +00:00
return " <form target= \" $CFG->framename\ " method = \ " get \" action= \" $CFG->wwwroot /course/category.php \" > " .
2003-09-21 15:45:24 +00:00
" <input type= \" hidden \" name= \" id \" value= \" $categoryid\ " /> " .
2006-10-04 04:45:58 +00:00
" <input type= \" hidden \" name= \" categoryedit \" value= \" $edit\ " /> " .
2004-10-08 16:52:20 +00:00
" <input type= \" hidden \" name= \" sesskey \" value= \" $USER->sesskey\ " /> " .
2003-09-21 15:45:24 +00:00
" <input type= \" submit \" value= \" $string\ " /></ form > " ;
2003-08-07 16:01:31 +00:00
}
}
2004-09-23 04:36:43 +00:00
/**
* Prints the editing button on categories listing
*
2004-09-25 05:29:21 +00:00
* @ uses $CFG
* @ uses $USER
* @ return string
2004-09-23 04:36:43 +00:00
*/
2003-08-07 16:01:31 +00:00
function update_categories_button () {
global $CFG , $USER ;
2006-09-07 08:57:56 +00:00
if ( has_capability ( 'moodle/category:update' , get_context_instance ( CONTEXT_SYSTEM , SITEID ))) {
2006-04-12 16:28:11 +00:00
if ( ! empty ( $USER -> categoryediting )) {
2004-09-22 14:39:15 +00:00
$string = get_string ( 'turneditingoff' );
2006-08-18 07:29:19 +00:00
$categoryedit = 'off' ;
2003-08-07 16:01:31 +00:00
} else {
2004-09-22 14:39:15 +00:00
$string = get_string ( 'turneditingon' );
2006-08-18 07:29:19 +00:00
$categoryedit = 'on' ;
2003-08-07 16:01:31 +00:00
}
2003-12-14 15:03:23 +00:00
return " <form target= \" $CFG->framename\ " method = \ " get \" action= \" $CFG->wwwroot /course/index.php \" > " .
2006-08-18 07:29:19 +00:00
'<input type="hidden" name="categoryedit" value="' . $categoryedit . '" />' .
2004-10-09 13:22:59 +00:00
'<input type="hidden" name="sesskey" value="' . $USER -> sesskey . '" />' .
2004-09-25 05:29:21 +00:00
'<input type="submit" value="' . $string . '" /></form>' ;
2003-07-30 13:02:45 +00:00
}
}
2002-12-20 14:44:14 +00:00
2005-09-01 04:48:22 +00:00
/**
* Prints the editing button on search results listing
* For bulk move courses to another category
*/
function update_categories_search_button ( $search , $page , $perpage ) {
global $CFG , $USER ;
2006-09-07 08:57:56 +00:00
// not sure if this capability is the best here
if ( has_capability ( 'moodle/category:update' , get_context_instance ( CONTEXT_SYSTEM , SITEID ))) {
2006-05-20 18:41:59 +00:00
if ( ! empty ( $USER -> categoryediting )) {
2005-09-01 04:48:22 +00:00
$string = get_string ( " turneditingoff " );
$edit = " off " ;
$perpage = 30 ;
} else {
$string = get_string ( " turneditingon " );
$edit = " on " ;
}
return " <form target= \" $CFG->framename\ " method = \ " get \" action= \" $CFG->wwwroot /course/search.php \" > " .
" <input type= \" hidden \" name= \" edit \" value= \" $edit\ " /> " .
" <input type= \" hidden \" name= \" sesskey \" value= \" $USER->sesskey\ " /> " .
2006-04-14 08:02:32 +00:00
" <input type= \" hidden \" name= \" search \" value= \" " . s ( $search , true ) . " \" /> " .
2005-09-01 04:48:22 +00:00
" <input type= \" hidden \" name= \" page \" value= \" $page\ " /> " .
" <input type= \" hidden \" name= \" perpage \" value= \" $perpage\ " /> " .
2006-04-14 08:02:32 +00:00
" <input type= \" submit \" value= \" " . s ( $string ) . " \" /></form> " ;
2005-09-01 04:48:22 +00:00
}
}
2004-09-23 04:36:43 +00:00
/**
* Prints the editing button on group page
*
2004-09-25 05:29:21 +00:00
* @ uses $CFG
* @ uses $USER
* @ param int $courseid The course group is associated with
* @ param int $groupid The group to update
* @ return string
2004-09-23 04:36:43 +00:00
*/
2004-02-09 07:31:04 +00:00
function update_group_button ( $courseid , $groupid ) {
2003-12-30 17:18:06 +00:00
global $CFG , $USER ;
2006-09-07 08:57:56 +00:00
if ( has_capability ( 'moodle/course:managegroups' , get_context_instance ( CONTEXT_GROUP , $groupid ))) {
2004-02-09 07:31:04 +00:00
$string = get_string ( 'editgroupprofile' );
2003-12-30 17:18:06 +00:00
return " <form target= \" $CFG->framename\ " method = \ " get \" action= \" $CFG->wwwroot /course/group.php \" > " .
2004-09-25 05:29:21 +00:00
'<input type="hidden" name="id" value="' . $courseid . '" />' .
'<input type="hidden" name="group" value="' . $groupid . '" />' .
'<input type="hidden" name="edit" value="on" />' .
'<input type="submit" value="' . $string . '" /></form>' ;
2003-12-30 17:18:06 +00:00
}
}
2004-09-23 04:36:43 +00:00
/**
* Prints the editing button on groups page
*
2004-09-25 05:29:21 +00:00
* @ uses $CFG
* @ uses $USER
* @ param int $courseid The id of the course to be edited
* @ return string
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2003-12-30 17:18:06 +00:00
function update_groups_button ( $courseid ) {
global $CFG , $USER ;
2006-09-07 08:57:56 +00:00
if ( has_capability ( 'moodle/course:managegroups' , get_context_instance ( CONTEXT_COURSE , $courseid ))) {
2003-12-30 17:18:06 +00:00
if ( ! empty ( $USER -> groupsediting )) {
2004-09-22 14:39:15 +00:00
$string = get_string ( 'turneditingoff' );
$edit = 'off' ;
2003-12-30 17:18:06 +00:00
} else {
2004-09-22 14:39:15 +00:00
$string = get_string ( 'turneditingon' );
$edit = 'on' ;
2003-12-30 17:18:06 +00:00
}
return " <form target= \" $CFG->framename\ " method = \ " get \" action= \" $CFG->wwwroot /course/groups.php \" > " .
" <input type= \" hidden \" name= \" id \" value= \" $courseid\ " /> " .
" <input type= \" hidden \" name= \" edit \" value= \" $edit\ " /> " .
" <input type= \" submit \" value= \" $string\ " /></ form > " ;
}
}
2004-09-23 04:36:43 +00:00
/**
* Prints an appropriate group selection menu
*
2004-09-25 05:29:21 +00:00
* @ uses VISIBLEGROUPS
* @ param array $groups ?
* @ param int $groupmode ?
* @ param string $currentgroup ?
* @ param string $urlroot ?
2005-11-08 07:19:27 +00:00
* @ param boolean $showall : if set to 0 , it is a student in separate groups , do not display all participants
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2006-08-10 06:41:38 +00:00
function print_group_menu ( $groups , $groupmode , $currentgroup , $urlroot , $showall = 1 , $return = false ) {
$output = '' ;
2004-02-15 07:13:08 +00:00
2004-02-19 17:56:05 +00:00
/// Add an "All groups" to the start of the menu
2005-11-08 07:19:27 +00:00
if ( $showall ){
$groupsmenu [ 0 ] = get_string ( 'allparticipants' );
}
2004-02-19 17:56:05 +00:00
foreach ( $groups as $key => $groupname ) {
$groupsmenu [ $key ] = $groupname ;
}
2006-08-10 06:41:38 +00:00
$output .= '<table><tr><td align="right">' ;
2004-02-15 07:13:08 +00:00
if ( $groupmode == VISIBLEGROUPS ) {
2006-08-10 06:41:38 +00:00
$output .= get_string ( 'groupsvisible' );
2004-02-15 07:13:08 +00:00
} else {
2006-08-10 06:41:38 +00:00
$output .= get_string ( 'groupsseparate' );
}
$output .= ':' ;
$output .= '</td><td nowrap="nowrap" align="left">' ;
$output .= popup_form ( $urlroot . '&group=' , $groupsmenu , 'selectgroup' , $currentgroup , '' , '' , '' , true , 'self' );
$output .= '</td></tr></table>' ;
if ( $return ) {
return $output ;
} else {
echo $output ;
2004-02-15 07:13:08 +00:00
}
}
2004-09-23 04:36:43 +00:00
/**
* Given a course and a ( current ) coursemodule
* This function returns a small popup menu with all the
* course activity modules in it , as a navigation menu
* The data is taken from the serialised array stored in
* the course record
*
2004-09-25 05:29:21 +00:00
* @ param course $course A { @ link $COURSE } object .
* @ param course $cm A { @ link $COURSE } object .
* @ param string $targetwindow ?
* @ return string
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2004-09-22 14:39:15 +00:00
function navmenu ( $course , $cm = NULL , $targetwindow = 'self' ) {
2002-12-20 14:44:14 +00:00
2005-03-29 14:22:13 +00:00
global $CFG , $THEME ;
if ( empty ( $THEME -> navmenuwidth )) {
$width = 50 ;
} else {
$width = $THEME -> navmenuwidth ;
}
2002-12-20 14:44:14 +00:00
if ( $cm ) {
2003-11-25 04:19:58 +00:00
$cm = $cm -> id ;
2002-12-20 14:44:14 +00:00
}
if ( $course -> format == 'weeks' ) {
2004-09-22 14:39:15 +00:00
$strsection = get_string ( 'week' );
2002-12-20 14:44:14 +00:00
} else {
2004-09-22 14:39:15 +00:00
$strsection = get_string ( 'topic' );
2002-12-20 14:44:14 +00:00
}
2005-03-30 03:41:37 +00:00
$strjumpto = get_string ( 'jumpto' );
2002-12-20 14:44:14 +00:00
2006-09-04 21:12:37 +00:00
/// Casting $course->modinfo to string prevents one notice when the field is null
if ( ! $modinfo = unserialize (( string ) $course -> modinfo )) {
2004-09-22 14:39:15 +00:00
return '' ;
2002-12-20 14:44:14 +00:00
}
2006-10-02 14:57:00 +00:00
$context = get_context_instance ( CONTEXT_COURSE , $course -> id );
2002-12-20 14:44:14 +00:00
$section = - 1 ;
2004-09-22 14:39:15 +00:00
$selected = '' ;
$url = '' ;
2003-11-25 04:19:58 +00:00
$previousmod = NULL ;
$backmod = NULL ;
$nextmod = NULL ;
2004-02-16 00:41:52 +00:00
$selectmod = NULL ;
2004-01-15 07:17:47 +00:00
$logslink = NULL ;
2003-11-25 04:19:58 +00:00
$flag = false ;
2004-06-09 08:36:28 +00:00
$menu = array ();
2003-11-25 04:19:58 +00:00
2004-09-22 14:39:15 +00:00
$sections = get_records ( 'course_sections' , 'course' , $course -> id , 'section' , 'section,visible,summary' );
2004-04-27 16:29:21 +00:00
2005-03-29 14:22:13 +00:00
if ( ! empty ( $THEME -> makenavmenulist )) { /// A hack to produce an XHTML navmenu list for use in themes
2006-10-02 14:57:00 +00:00
$THEME -> navmenulist = navmenulist ( $course , $sections , $modinfo , $strsection , $strjumpto , $width , $cm );
2005-03-29 14:22:13 +00:00
}
2002-12-20 14:44:14 +00:00
foreach ( $modinfo as $mod ) {
2004-09-22 14:39:15 +00:00
if ( $mod -> mod == 'label' ) {
2003-09-15 11:59:15 +00:00
continue ;
}
2004-04-27 16:29:21 +00:00
2004-11-21 06:28:17 +00:00
if ( $mod -> section > $course -> numsections ) { /// Don't show excess hidden sections
break ;
}
2002-12-20 14:44:14 +00:00
if ( $mod -> section > 0 and $section <> $mod -> section ) {
2004-07-21 06:06:10 +00:00
$thissection = $sections [ $mod -> section ];
2006-10-02 14:57:00 +00:00
if ( $thissection -> visible or ! $course -> hiddensections or
has_capability ( 'moodle/course:viewhiddensections' , $context )) {
2005-03-26 19:02:56 +00:00
$thissection -> summary = strip_tags ( format_string ( $thissection -> summary , true ));
2004-07-21 06:06:10 +00:00
if ( $course -> format == 'weeks' or empty ( $thissection -> summary )) {
2004-09-22 14:39:15 +00:00
$menu [] = '-------------- ' . $strsection . " " . $mod -> section . ' --------------' ;
2004-07-21 06:06:10 +00:00
} else {
2005-03-29 14:22:13 +00:00
if ( strlen ( $thissection -> summary ) < ( $width - 3 )) {
2004-07-21 06:06:10 +00:00
$menu [] = '-- ' . $thissection -> summary ;
} else {
2005-03-29 14:22:13 +00:00
$menu [] = '-- ' . substr ( $thissection -> summary , 0 , $width ) . '...' ;
2004-07-21 06:06:10 +00:00
}
}
2004-04-27 16:29:21 +00:00
}
2002-12-20 14:44:14 +00:00
}
2004-04-27 16:29:21 +00:00
2002-12-20 14:44:14 +00:00
$section = $mod -> section ;
2004-04-27 16:29:21 +00:00
2003-04-23 16:32:11 +00:00
//Only add visible or teacher mods to jumpmenu
2006-10-01 04:56:43 +00:00
if ( $mod -> visible or has_capability ( 'moodle/course:viewhiddenactivities' ,
get_context_instance ( CONTEXT_MODULE , $mod -> cm ))) {
2004-09-22 14:39:15 +00:00
$url = $mod -> mod . '/view.php?id=' . $mod -> cm ;
2003-11-25 04:19:58 +00:00
if ( $flag ) { // the current mod is the "next" mod
$nextmod = $mod ;
$flag = false ;
}
2003-04-23 16:32:11 +00:00
if ( $cm == $mod -> cm ) {
$selected = $url ;
2004-01-15 07:17:47 +00:00
$selectmod = $mod ;
2003-11-25 04:19:58 +00:00
$backmod = $previousmod ;
$flag = true ; // set flag so we know to use next mod for "next"
2004-06-21 13:07:44 +00:00
$mod -> name = $strjumpto ;
2004-06-09 08:55:44 +00:00
$strjumpto = '' ;
2004-06-09 08:46:43 +00:00
} else {
2005-03-26 19:02:56 +00:00
$mod -> name = strip_tags ( format_string ( urldecode ( $mod -> name ), true ));
2005-03-29 14:22:13 +00:00
if ( strlen ( $mod -> name ) > ( $width + 5 )) {
$mod -> name = substr ( $mod -> name , 0 , $width ) . '...' ;
2004-06-09 08:46:43 +00:00
}
if ( ! $mod -> visible ) {
2004-09-22 14:39:15 +00:00
$mod -> name = '(' . $mod -> name . ')' ;
2004-06-09 08:46:43 +00:00
}
2003-04-23 17:25:20 +00:00
}
2003-11-25 04:19:58 +00:00
$menu [ $url ] = $mod -> name ;
2003-11-29 02:27:04 +00:00
$previousmod = $mod ;
2002-12-20 14:44:14 +00:00
}
2003-11-25 04:19:58 +00:00
}
2006-09-19 11:36:21 +00:00
//Accessibility: added Alt text, replaced > < with 'silent' character and 'accesshide' text.
check_theme_arrows ();
2006-10-02 14:57:00 +00:00
if ( $selectmod and has_capability ( 'moodle/site:viewreports' , $context )) {
2006-09-19 11:36:21 +00:00
$logstext = get_string ( 'alllogs' );
2006-10-02 14:57:00 +00:00
$logslink = '<a title="' . $logstext . '" target="' . $CFG -> framename . '" href="' .
$CFG -> wwwroot . '/course/report/log/index.php?chooselog=1&user=0&date=0&id=' .
$course -> id . '&modid=' . $selectmod -> cm . '">' .
'<img class="icon log" src="' . $CFG -> pixpath . '/i/log.gif" alt="' . $logstext . '" /></a>' ;
2004-05-21 13:07:11 +00:00
2004-01-15 07:17:47 +00:00
}
2003-11-25 04:19:58 +00:00
if ( $backmod ) {
2006-09-19 11:36:21 +00:00
$backtext = get_string ( 'activityprev' , 'access' );
2006-10-02 14:57:00 +00:00
$backmod = '<form action="' . $CFG -> wwwroot . '/mod/' . $backmod -> mod . '/view.php" target="' . $CFG -> framename . '">' .
'<input type="hidden" name="id" value="' . $backmod -> cm . '" />' .
'<button type="submit" title="' . $backtext . '">' . $THEME -> larrow .
'<span class="accesshide">' . $backtext . '</span></button></form>' ;
2003-11-25 04:19:58 +00:00
}
if ( $nextmod ) {
2006-09-19 11:36:21 +00:00
$nexttext = get_string ( 'activitynext' , 'access' );
2006-10-02 14:57:00 +00:00
$nextmod = '<form action="' . $CFG -> wwwroot . '/mod/' . $nextmod -> mod . '/view.php" target="' . $CFG -> framename . '">' .
'<input type="hidden" name="id" value="' . $nextmod -> cm . '" />' .
'<button type="submit" title="' . $nexttext . '">' . $THEME -> rarrow .
'<span class="accesshide">' . $nexttext . '</span></button></form>' ;
2003-11-25 04:19:58 +00:00
}
2005-03-29 14:22:13 +00:00
2006-09-19 11:36:21 +00:00
return " <table><tr> \n <td> " . $logslink . '</td><td>' . $backmod . '</td><td>' .
2004-09-22 14:39:15 +00:00
popup_form ( $CFG -> wwwroot . '/mod/' , $menu , 'navmenu' , $selected , $strjumpto ,
'' , '' , true , $targetwindow ) .
2006-09-19 11:36:21 +00:00
'</td><td>' . $nextmod . " </td> \n </tr></table> " ;
2003-11-25 04:19:58 +00:00
}
2002-12-20 14:44:14 +00:00
2005-03-29 13:37:08 +00:00
/**
2005-04-03 12:15:45 +00:00
* Given a course
2005-03-29 13:37:08 +00:00
* This function returns a small popup menu with all the
* course activity modules in it , as a navigation menu
2005-04-03 12:15:45 +00:00
* outputs a simple list structure in XHTML
2005-03-29 13:37:08 +00:00
* The data is taken from the serialised array stored in
* the course record
*
* @ param course $course A { @ link $COURSE } object .
* @ return string
* @ todo Finish documenting this function
*/
2006-10-02 14:57:00 +00:00
function navmenulist ( $course , $sections , $modinfo , $strsection , $strjumpto , $width = 50 , $cmid = 0 ) {
2005-03-29 13:37:08 +00:00
global $CFG ;
$section = - 1 ;
$selected = '' ;
$url = '' ;
$previousmod = NULL ;
$backmod = NULL ;
$nextmod = NULL ;
$selectmod = NULL ;
$logslink = NULL ;
$flag = false ;
$menu = array ();
2006-10-02 14:57:00 +00:00
$coursecontext = get_context_instance ( CONTEXT_COURSE , $course -> id );
2005-03-30 12:31:00 +00:00
$menu [] = '<ul class="navmenulist"><li class="jumpto section"><span>' . $strjumpto . '</span><ul>' ;
2005-03-29 13:37:08 +00:00
foreach ( $modinfo as $mod ) {
if ( $mod -> mod == 'label' ) {
continue ;
}
if ( $mod -> section > $course -> numsections ) { /// Don't show excess hidden sections
break ;
}
2005-03-29 21:08:17 +00:00
if ( $mod -> section >= 0 and $section <> $mod -> section ) {
2005-03-29 13:37:08 +00:00
$thissection = $sections [ $mod -> section ];
2006-10-02 14:57:00 +00:00
if ( $thissection -> visible or ! $course -> hiddensections or
has_capability ( 'moodle/course:viewhiddensections' , $coursecontext )) {
2005-03-29 13:37:08 +00:00
$thissection -> summary = strip_tags ( format_string ( $thissection -> summary , true ));
if ( ! empty ( $doneheading )) {
2005-03-29 13:46:20 +00:00
$menu [] = '</ul></li>' ;
2005-03-29 13:37:08 +00:00
}
if ( $course -> format == 'weeks' or empty ( $thissection -> summary )) {
2005-03-30 11:54:58 +00:00
$item = $strsection . " " . $mod -> section ;
2005-03-29 13:37:08 +00:00
} else {
if ( strlen ( $thissection -> summary ) < ( $width - 3 )) {
2005-03-30 11:54:58 +00:00
$item = $thissection -> summary ;
2005-03-29 13:37:08 +00:00
} else {
2005-03-30 11:54:58 +00:00
$item = substr ( $thissection -> summary , 0 , $width ) . '...' ;
2005-03-29 13:37:08 +00:00
}
}
2005-03-30 12:31:00 +00:00
$menu [] = '<li class="section"><span>' . $item . '</span>' ;
2005-03-29 13:37:08 +00:00
$menu [] = '<ul>' ;
$doneheading = true ;
}
}
$section = $mod -> section ;
//Only add visible or teacher mods to jumpmenu
2006-09-07 08:57:56 +00:00
if ( $mod -> visible or has_capability ( 'moodle/course:viewhiddenactivities' , get_context_instance ( CONTEXT_MODULE , $mod -> id ))) {
2005-03-29 13:37:08 +00:00
$url = $mod -> mod . '/view.php?id=' . $mod -> cm ;
if ( $flag ) { // the current mod is the "next" mod
$nextmod = $mod ;
$flag = false ;
}
$mod -> name = strip_tags ( format_string ( urldecode ( $mod -> name ), true ));
if ( strlen ( $mod -> name ) > ( $width + 5 )) {
$mod -> name = substr ( $mod -> name , 0 , $width ) . '...' ;
}
if ( ! $mod -> visible ) {
$mod -> name = '(' . $mod -> name . ')' ;
}
2005-03-30 03:41:37 +00:00
$class = 'activity ' . $mod -> mod ;
$class .= ( $cmid == $mod -> cm ) ? ' selected' : '' ;
2005-03-30 12:31:00 +00:00
$menu [] = '<li class="' . $class . '">' .
2005-03-30 11:54:58 +00:00
'<img src="' . $CFG -> modpixpath . '/' . $mod -> mod . '/icon.gif" border="0" />' .
2005-03-30 12:31:00 +00:00
'<a href="' . $CFG -> wwwroot . '/mod/' . $url . '">' . $mod -> name . '</a></li>' ;
2005-03-29 13:37:08 +00:00
$previousmod = $mod ;
}
}
2005-03-29 13:46:20 +00:00
if ( $doneheading ) {
2005-03-29 13:48:25 +00:00
$menu [] = '</ul></li>' ;
2005-03-29 13:46:20 +00:00
}
2005-03-29 21:08:17 +00:00
$menu [] = '</ul></li></ul>' ;
2005-03-29 13:37:08 +00:00
return implode ( " \n " , $menu );
}
2004-09-23 04:36:43 +00:00
/**
* Prints form items with the names $day , $month and $year
*
2006-03-27 03:42:44 +00:00
* @ param string $day fieldname
* @ param string $month fieldname
* @ param string $year fieldname
2004-09-25 05:29:21 +00:00
* @ param int $currenttime A default timestamp in GMT
2006-03-27 03:42:44 +00:00
* @ param boolean $return
2004-09-23 04:36:43 +00:00
*/
2006-03-27 03:42:44 +00:00
function print_date_selector ( $day , $month , $year , $currenttime = 0 , $return = false ) {
2002-12-20 14:44:14 +00:00
if ( ! $currenttime ) {
$currenttime = time ();
}
$currentdate = usergetdate ( $currenttime );
for ( $i = 1 ; $i <= 31 ; $i ++ ) {
2004-09-22 14:39:15 +00:00
$days [ $i ] = $i ;
2002-12-20 14:44:14 +00:00
}
for ( $i = 1 ; $i <= 12 ; $i ++ ) {
2003-01-10 05:40:35 +00:00
$months [ $i ] = userdate ( gmmktime ( 12 , 0 , 0 , $i , 1 , 2000 ), " %B " );
2002-12-20 14:44:14 +00:00
}
2006-03-30 03:55:24 +00:00
for ( $i = 1970 ; $i <= 2020 ; $i ++ ) {
2002-12-20 14:44:14 +00:00
$years [ $i ] = $i ;
}
2006-03-27 03:42:44 +00:00
return choose_from_menu ( $days , $day , $currentdate [ 'mday' ], '' , '' , '0' , $return )
. choose_from_menu ( $months , $month , $currentdate [ 'mon' ], '' , '' , '0' , $return )
. choose_from_menu ( $years , $year , $currentdate [ 'year' ], '' , '' , '0' , $return );
2002-12-20 14:44:14 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Prints form items with the names $hour and $minute
*
2006-03-27 03:42:44 +00:00
* @ param string $hour fieldname
* @ param string ? $minute fieldname
2004-09-23 04:36:43 +00:00
* @ param $currenttime A default timestamp in GMT
2006-03-27 03:42:44 +00:00
* @ param int $step minute spacing
* @ param boolean $return
2004-09-23 04:36:43 +00:00
*/
2006-03-27 03:42:44 +00:00
function print_time_selector ( $hour , $minute , $currenttime = 0 , $step = 5 , $return = false ) {
2002-12-20 14:44:14 +00:00
if ( ! $currenttime ) {
$currenttime = time ();
}
$currentdate = usergetdate ( $currenttime );
2003-12-08 17:20:31 +00:00
if ( $step != 1 ) {
$currentdate [ 'minutes' ] = ceil ( $currentdate [ 'minutes' ] / $step ) * $step ;
}
2002-12-20 14:44:14 +00:00
for ( $i = 0 ; $i <= 23 ; $i ++ ) {
$hours [ $i ] = sprintf ( " %02d " , $i );
}
2003-12-08 17:20:31 +00:00
for ( $i = 0 ; $i <= 59 ; $i += $step ) {
2002-12-20 14:44:14 +00:00
$minutes [ $i ] = sprintf ( " %02d " , $i );
}
2005-09-01 04:14:31 +00:00
2006-03-27 03:42:44 +00:00
return choose_from_menu ( $hours , $hour , $currentdate [ 'hours' ], '' , '' , '0' , $return )
. choose_from_menu ( $minutes , $minute , $currentdate [ 'minutes' ], '' , '' , '0' , $return );
2002-12-20 14:44:14 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Prints time limit value selector
*
* @ uses $CFG
2006-03-27 03:42:44 +00:00
* @ param int $timelimit default
* @ param string $unit
* @ param string $name
* @ param boolean $return
2004-09-23 04:36:43 +00:00
*/
2006-03-27 03:42:44 +00:00
function print_timer_selector ( $timelimit = 0 , $unit = '' , $name = 'timelimit' , $return = false ) {
2004-06-02 18:00:12 +00:00
global $CFG ;
2004-06-21 13:07:44 +00:00
if ( $unit ) {
$unit = ' ' . $unit ;
}
2004-06-02 18:00:12 +00:00
// Max timelimit is sessiontimeout - 10 minutes.
$maxvalue = ( $CFG -> sessiontimeout / 60 ) - 10 ;
2004-06-21 13:07:44 +00:00
for ( $i = 1 ; $i <= $maxvalue ; $i ++ ) {
$minutes [ $i ] = $i . $unit ;
2004-06-02 18:00:12 +00:00
}
2006-03-27 03:42:44 +00:00
return choose_from_menu ( $minutes , $name , $timelimit , get_string ( 'none' ), '' , '' , '0' , $return );
2004-06-02 18:00:12 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Prints a grade menu ( as part of an existing form ) with help
* Showing all possible numerical grades and scales
*
2004-09-25 05:29:21 +00:00
* @ uses $CFG
* @ param int $courseid ?
* @ param string $name ?
* @ param string $current ?
* @ param boolean $includenograde ?
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2006-08-10 06:41:38 +00:00
function print_grade_menu ( $courseid , $name , $current , $includenograde = true , $return = false ) {
2003-08-18 05:28:30 +00:00
2003-09-14 12:25:16 +00:00
global $CFG ;
2003-08-18 05:28:30 +00:00
2006-08-10 06:41:38 +00:00
$output = '' ;
2004-09-22 14:39:15 +00:00
$strscale = get_string ( 'scale' );
$strscales = get_string ( 'scales' );
2003-08-18 05:28:30 +00:00
2003-08-18 11:58:07 +00:00
$scales = get_scales_menu ( $courseid );
2003-08-18 05:28:30 +00:00
foreach ( $scales as $i => $scalename ) {
2004-09-22 14:39:15 +00:00
$grades [ - $i ] = $strscale . ': ' . $scalename ;
2003-08-18 05:28:30 +00:00
}
2003-08-25 12:47:36 +00:00
if ( $includenograde ) {
2004-09-22 14:39:15 +00:00
$grades [ 0 ] = get_string ( 'nograde' );
2003-08-25 12:47:36 +00:00
}
2003-08-18 05:28:30 +00:00
for ( $i = 100 ; $i >= 1 ; $i -- ) {
$grades [ $i ] = $i ;
}
2006-08-10 06:41:38 +00:00
$output .= choose_from_menu ( $grades , $name , $current , '' , '' , 0 , true );
2003-08-18 05:28:30 +00:00
2005-05-10 00:20:18 +00:00
$linkobject = '<span class="helplink"><img height="17" width="17" alt="' . $strscales . '" src="' . $CFG -> pixpath . '/help.gif" /></span>' ;
2006-08-10 06:41:38 +00:00
$output .= link_to_popup_window ( '/course/scales.php?id=' . $courseid . '&list=true' , 'ratingscales' ,
$linkobject , 400 , 500 , $strscales , 'none' , true );
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2003-08-18 05:28:30 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Prints a scale menu ( as part of an existing form ) including help button
2004-09-25 05:29:21 +00:00
* Just like { @ link print_grade_menu ()} but without the numeric grades
2004-09-23 04:36:43 +00:00
*
2004-09-25 05:29:21 +00:00
* @ param int $courseid ?
* @ param string $name ?
* @ param string $current ?
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2006-08-10 06:41:38 +00:00
function print_scale_menu ( $courseid , $name , $current , $return = false ) {
2003-08-15 13:59:24 +00:00
2003-09-14 12:25:16 +00:00
global $CFG ;
2003-08-15 13:59:24 +00:00
2006-08-10 06:41:38 +00:00
$output = '' ;
2004-09-22 14:39:15 +00:00
$strscales = get_string ( 'scales' );
2006-08-10 06:41:38 +00:00
$output .= choose_from_menu ( get_scales_menu ( $courseid ), $name , $current , '' , '' , 0 , true );
2005-05-10 00:20:18 +00:00
$linkobject = '<span class="helplink"><img height="17" width="17" alt="' . $strscales . '" src="' . $CFG -> pixpath . '/help.gif" /></span>' ;
2006-08-10 06:41:38 +00:00
$output .= link_to_popup_window ( '/course/scales.php?id=' . $courseid . '&list=true' , 'ratingscales' ,
$linkobject , 400 , 500 , $strscales , 'none' , true );
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2003-08-15 13:59:24 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Prints a help button about a scale
*
* @ uses $CFG
* @ param id $courseid ?
* @ param object $scale ?
* @ todo Finish documenting this function
*/
2006-08-10 06:41:38 +00:00
function print_scale_menu_helpbutton ( $courseid , $scale , $return = false ) {
2003-08-15 13:59:24 +00:00
2003-09-14 12:25:16 +00:00
global $CFG ;
2003-08-15 13:59:24 +00:00
2006-08-10 06:41:38 +00:00
$output = '' ;
2004-09-22 14:39:15 +00:00
$strscales = get_string ( 'scales' );
2005-05-10 00:20:18 +00:00
$linkobject = '<span class="helplink"><img height="17" width="17" alt="' . $scale -> name . '" src="' . $CFG -> pixpath . '/help.gif" /></span>' ;
2006-08-10 06:41:38 +00:00
$output .= link_to_popup_window ( '/course/scales.php?id=' . $courseid . '&list=true&scaleid=' . $scale -> id , 'ratingscale' ,
$linkobject , 400 , 500 , $scale -> name , 'none' , true );
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2003-08-15 13:59:24 +00:00
}
2004-09-23 04:36:43 +00:00
/**
2005-04-03 12:15:45 +00:00
* Print an error page displaying an error message .
2005-03-18 12:10:00 +00:00
* Old method , don ' t call directly in new code - use print_error instead .
2005-04-03 12:15:45 +00:00
*
2004-09-23 04:36:43 +00:00
*
2004-09-25 05:29:21 +00:00
* @ uses $SESSION
* @ uses $CFG
2004-09-23 04:36:43 +00:00
* @ param string $message The message to display to the user about the error .
* @ param string $link The url where the user will be prompted to continue . If no url is provided the user will be directed to the site index page .
*/
2004-09-22 14:39:15 +00:00
function error ( $message , $link = '' ) {
2006-09-25 02:44:54 +00:00
2002-12-20 14:44:14 +00:00
global $CFG , $SESSION ;
2006-09-25 02:44:54 +00:00
$message = clean_text ( $message ); // In case nasties are in here
if ( defined ( 'FULLME' ) && FULLME == 'cron' ) {
// Errors in cron should be mtrace'd.
mtrace ( $message );
die ;
}
2002-12-20 14:44:14 +00:00
2006-05-16 20:31:22 +00:00
if ( ! defined ( 'HEADER_PRINTED' )) {
2006-05-01 21:31:12 +00:00
//header not yet printed
@ header ( 'HTTP/1.0 404 Not Found' );
print_header ( get_string ( 'error' ));
}
2005-08-30 09:36:14 +00:00
2004-09-22 14:39:15 +00:00
echo '<br />' ;
2005-08-09 09:25:20 +00:00
print_simple_box ( $message , '' , '' , '' , '' , 'errorbox' );
2004-05-21 13:07:11 +00:00
2006-08-17 22:37:34 +00:00
// in case we are logging upgrade in admin/index.php stop it
if ( function_exists ( 'upgrade_log_finish' )) {
upgrade_log_finish ();
}
2002-12-20 14:44:14 +00:00
if ( ! $link ) {
if ( ! empty ( $SESSION -> fromurl ) ) {
2004-09-22 14:39:15 +00:00
$link = $SESSION -> fromurl ;
2002-12-20 14:44:14 +00:00
unset ( $SESSION -> fromurl );
} else {
2004-09-22 14:39:15 +00:00
$link = $CFG -> wwwroot . '/' ;
2002-12-20 14:44:14 +00:00
}
}
print_continue ( $link );
print_footer ();
2005-08-30 09:36:14 +00:00
for ( $i = 0 ; $i < 512 ; $i ++ ) { // Padding to help IE work with 404
echo ' ' ;
}
2006-08-01 07:46:19 +00:00
2002-12-20 14:44:14 +00:00
die ;
}
2005-03-18 12:10:00 +00:00
/**
* Print an error page displaying an error message . New method - use this for new code .
*
* @ uses $SESSION
* @ uses $CFG
2006-04-12 09:32:19 +00:00
* @ param string $errorcode The name of the string from error . php to print
2005-03-18 12:10:00 +00:00
* @ param string $link The url where the user will be prompted to continue . If no url is provided the user will be directed to the site index page .
2006-06-14 03:17:19 +00:00
* @ param object $a Extra words and phrases that might be required in the error string
2005-03-18 12:10:00 +00:00
*/
2006-06-14 03:17:19 +00:00
function print_error ( $errorcode , $module = '' , $link = '' , $a = NULL ) {
2005-03-18 12:10:00 +00:00
2006-04-13 05:42:47 +00:00
global $CFG ;
if ( empty ( $module ) || $module == 'moodle' || $module == 'core' ) {
$module = 'error' ;
$modulelink = 'moodle' ;
} else {
$modulelink = $module ;
}
if ( ! empty ( $CFG -> errordocroot )) {
$errordocroot = $CFG -> errordocroot ;
} else if ( ! empty ( $CFG -> docroot )) {
$errordocroot = $CFG -> docroot ;
} else {
$errordocroot = 'http://docs.moodle.org' ;
}
2006-06-14 03:17:19 +00:00
$message = '<p class="errormessage">' . get_string ( $errorcode , $module , $a ) . '</p>' .
2006-04-13 05:42:47 +00:00
'<p class="errorcode">' .
'<a href="' . $errordocroot . '/en/error/' . $modulelink . '/' . $errorcode . '">' .
get_string ( 'moreinformation' ) . '</a></p>' ;
2006-04-12 09:32:19 +00:00
error ( $message , $link );
2005-03-18 12:10:00 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Print a help button .
*
2004-09-25 05:29:21 +00:00
* @ uses $CFG
* @ param string $page The keyword that defines a help page
* @ param string $title The title of links , rollover tips , alt tags etc
2006-02-28 15:29:11 +00:00
* 'Help with' ( or the language equivalent ) will be prefixed and '...' will be stripped .
2004-09-25 05:29:21 +00:00
* @ param string $module Which module is the page defined in
* @ param mixed $image Use a help image for the link ? ( true / false / " both " )
2006-08-11 10:33:29 +00:00
* @ param boolean $linktext If true , display the title next to the help icon .
2004-09-25 05:29:21 +00:00
* @ param string $text If defined then this text is used in the page , and
* the $page variable is ignored .
* @ param boolean $return If true then the output is returned as a string , if false it is printed to the current page .
2005-01-21 05:04:24 +00:00
* @ param string $imagetext The full text for the helpbutton icon . If empty use default help . gif
2004-09-25 05:29:21 +00:00
* @ return string
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2005-01-21 05:04:24 +00:00
function helpbutton ( $page , $title = '' , $module = 'moodle' , $image = true , $linktext = false , $text = '' , $return = false ,
$imagetext = '' ) {
2005-01-25 13:36:57 +00:00
global $CFG ;
2002-12-20 14:44:14 +00:00
2004-09-22 14:39:15 +00:00
if ( $module == '' ) {
$module = 'moodle' ;
2002-12-20 14:44:14 +00:00
}
2005-01-21 05:04:24 +00:00
2006-03-16 11:13:21 +00:00
//Accessibility: prefix the alt text/title with 'Help with', strip distracting dots '...'
// PLEASE DO NOT CHANGE. ('...' is VERY distracting for non-visual users)
$tooltip = get_string ( 'helpprefix' , '' , trim ( $title , " . \t " ));
2006-03-10 09:16:11 +00:00
2006-06-18 08:10:52 +00:00
$linkobject = '' ;
2002-12-20 14:44:14 +00:00
if ( $image ) {
if ( $linktext ) {
2005-01-21 05:04:24 +00:00
$linkobject .= $title . ' ' ;
2002-12-20 14:44:14 +00:00
}
2005-01-21 05:04:24 +00:00
2006-08-11 10:33:29 +00:00
if ( $imagetext ) {
$linkobject .= $imagetext ;
} else {
$linkobject .= '<img alt="' . $tooltip . '" src="' .
$CFG -> pixpath . '/help.gif" />' ;
}
2005-01-26 15:57:00 +00:00
2002-12-20 14:44:14 +00:00
} else {
2006-03-10 08:57:22 +00:00
$linkobject .= $tooltip ;
2002-12-20 14:44:14 +00:00
}
2005-01-21 05:04:24 +00:00
2002-12-20 14:44:14 +00:00
if ( $text ) {
2006-04-11 16:09:52 +00:00
$url = '/help.php?module=' . $module . '&text=' . s ( urlencode ( $text ));
2002-12-20 14:44:14 +00:00
} else {
2004-09-22 14:39:15 +00:00
$url = '/help.php?module=' . $module . '&file=' . $page . '.html' ;
2002-12-20 14:44:14 +00:00
}
2004-08-02 19:09:06 +00:00
2006-06-18 08:10:52 +00:00
$link = '<span class="helplink">' .
link_to_popup_window ( $url , 'popup' , $linkobject , 400 , 500 , $tooltip , 'none' , true ) .
'</span>' ;
2004-08-02 19:09:06 +00:00
if ( $return ) {
return $link ;
} else {
echo $link ;
}
2002-12-20 14:44:14 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Print a help button .
*
2004-09-25 05:29:21 +00:00
* Prints a special help button that is a link to the " live " emoticon popup
* @ uses $CFG
* @ uses $SESSION
* @ param string $form ?
* @ param string $field ?
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2003-05-08 16:09:30 +00:00
function emoticonhelpbutton ( $form , $field ) {
2004-09-25 05:29:21 +00:00
2003-05-08 16:09:30 +00:00
global $CFG , $SESSION ;
$SESSION -> inserttextform = $form ;
$SESSION -> inserttextfield = $field ;
2005-01-25 11:05:23 +00:00
$imagetext = '<img src="' . $CFG -> pixpath . ' / s / smiley . gif " border= " 0 " align= " middle " width= " 15 " height= " 15 " alt= " "
2005-03-07 11:53:00 +00:00
class = " emoticon " style = " margin-left: 7px " /> ' ;
2005-01-21 05:04:24 +00:00
helpbutton ( 'emoticons' , get_string ( 'helpemoticons' ), 'moodle' , true , true , '' , false , $imagetext );
2003-05-08 16:09:30 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Print a message and exit .
*
2004-09-25 05:29:21 +00:00
* @ uses $CFG
* @ param string $message ?
* @ param string $link ?
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2006-09-24 06:21:15 +00:00
function notice ( $message , $link = '' , $course = NULL ) {
global $CFG , $SITE ;
2002-12-20 14:44:14 +00:00
2004-10-01 02:43:09 +00:00
$message = clean_text ( $message );
$link = clean_text ( $link );
2002-12-20 14:44:14 +00:00
if ( ! $link ) {
2004-09-22 14:39:15 +00:00
if ( ! empty ( $_SERVER [ 'HTTP_REFERER' ])) {
$link = $_SERVER [ 'HTTP_REFERER' ];
2003-01-26 06:04:14 +00:00
} else {
2004-09-22 14:39:15 +00:00
$link = $CFG -> wwwroot . '/' ;
2003-01-26 06:04:14 +00:00
}
2002-12-20 14:44:14 +00:00
}
2004-09-22 14:39:15 +00:00
echo '<br />' ;
2006-04-10 08:32:27 +00:00
print_simple_box ( $message , 'center' , '50%' , '' , '20' , 'generalbox' , 'notice' );
2005-02-08 17:09:41 +00:00
print_continue ( $link );
2006-09-24 06:21:15 +00:00
if ( empty ( $course )) {
print_footer ( $SITE );
} else {
print_footer ( $course );
}
exit ;
2002-12-20 14:44:14 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Print a message along with " Yes " and " No " links for the user to continue .
*
2004-09-25 05:29:21 +00:00
* @ param string $message The text to display
* @ param string $linkyes The link to take the user to if they choose " Yes "
* @ param string $linkno The link to take the user to if they choose " No "
2004-09-23 04:36:43 +00:00
*/
2006-09-21 22:34:45 +00:00
function notice_yesno ( $message , $linkyes , $linkno , $optionsyes = NULL , $optionsno = NULL , $methodyes = 'post' , $methodno = 'post' ) {
2002-12-20 14:44:14 +00:00
2005-02-08 17:09:41 +00:00
global $CFG ;
2004-10-01 02:43:09 +00:00
$message = clean_text ( $message );
$linkyes = clean_text ( $linkyes );
$linkno = clean_text ( $linkno );
2006-04-10 08:32:27 +00:00
print_simple_box_start ( 'center' , '60%' , '' , 5 , 'generalbox' , 'notice' );
2005-02-08 17:09:41 +00:00
echo '<p align="center">' . $message . '</p>' ;
echo '<table align="center" cellpadding="20"><tr><td>' ;
2006-09-21 22:34:45 +00:00
print_single_button ( $linkyes , $optionsyes , get_string ( 'yes' ), $methodyes , $CFG -> framename );
2005-02-08 17:09:41 +00:00
echo '</td><td>' ;
2006-09-21 22:34:45 +00:00
print_single_button ( $linkno , $optionsno , get_string ( 'no' ), $methodno , $CFG -> framename );
2005-02-08 17:09:41 +00:00
echo '</td></tr></table>' ;
2002-12-20 14:44:14 +00:00
print_simple_box_end ();
}
2004-09-23 04:36:43 +00:00
/**
* Redirects the user to another page , after printing a notice
*
2004-09-25 05:29:21 +00:00
* @ param string $url The url to take the user to
* @ param string $message The text message to display to the user about the redirect , if any
* @ param string $delay How long before refreshing to the new page at $url ?
* @ todo '&' needs to be encoded into '&' for XHTML compliance ,
* however , this is not true for javascript . Therefore we
* first decode all entities in $url ( since we cannot rely on )
* the correct input ) and then encode for where it ' s needed
* echo " <script type='text/javascript'>alert('Redirect $url ');</script> " ;
2004-09-23 04:36:43 +00:00
*/
2006-08-31 07:45:58 +00:00
function redirect ( $url , $message = '' , $delay =- 1 ) {
2004-10-01 02:43:09 +00:00
2005-12-05 02:24:45 +00:00
global $CFG ;
2006-01-05 07:08:10 +00:00
if ( ! empty ( $CFG -> usesid ) && ! isset ( $_COOKIE [ session_name ()])) {
$url = sid_process_url ( $url );
2005-12-05 02:24:45 +00:00
}
2004-10-01 02:43:09 +00:00
$message = clean_text ( $message );
2005-01-26 15:57:00 +00:00
2006-08-31 07:45:58 +00:00
$url = html_entity_decode ( $url );
2005-04-20 20:45:43 +00:00
$url = str_replace ( array ( " \n " , " \r " ), '' , $url ); // some more cleaning
2004-09-21 11:41:58 +00:00
$encodedurl = htmlentities ( $url );
2005-11-17 06:45:40 +00:00
$tmpstr = clean_text ( '<a href="' . $encodedurl . '" />' ); //clean encoded URL
$encodedurl = substr ( $tmpstr , 9 , strlen ( $tmpstr ) - 13 );
2006-08-01 18:02:42 +00:00
$url = html_entity_decode ( $encodedurl );
$surl = addslashes ( $url );
2004-10-01 02:43:09 +00:00
2006-05-16 20:31:22 +00:00
/// when no message and header printed yet, try to redirect
if ( empty ( $message ) and ! defined ( 'HEADER_PRINTED' )) {
2006-09-21 15:13:27 +00:00
// Technically, HTTP/1.1 requires Location: header to contain
// the absolute path. (In practice browsers accept relative
// paths - but still, might as well do it properly.)
// This code turns relative into absolute.
2006-09-24 19:10:13 +00:00
if ( ! preg_match ( '|^[a-z]+:|' , $url )) {
2006-09-22 10:48:37 +00:00
// Get host name http://www.wherever.com
2006-09-24 19:10:13 +00:00
$hostpart = preg_replace ( '|^(.*?[^:/])/.*$|' , '$1' , $CFG -> wwwroot );
if ( preg_match ( '|^/|' , $url )) {
// URLs beginning with / are relative to web server root so we just add them in
$url = $hostpart . $url ;
2006-09-22 10:48:37 +00:00
} else {
2006-09-24 19:10:13 +00:00
// URLs not beginning with / are relative to path of current script, so add that on.
2006-09-25 11:23:15 +00:00
$url = $hostpart . preg_replace ( '|\?.*$|' , '' , me ()) . '/../' . $url ;
2006-09-22 10:48:37 +00:00
}
2006-09-21 15:13:27 +00:00
// Replace all ..s
2006-09-24 19:10:13 +00:00
while ( true ) {
$newurl = preg_replace ( '|/[^/]*/\.\./|' , '/' , $url );
if ( $newurl == $url ) {
2006-09-21 15:13:27 +00:00
break ;
}
2006-09-24 19:10:13 +00:00
$url = $newurl ;
2006-09-21 15:13:27 +00:00
}
}
2006-09-24 19:10:13 +00:00
2006-08-31 07:45:58 +00:00
$delay = 0 ;
2006-08-01 18:02:42 +00:00
//try header redirection first
@ header ( 'HTTP/1.x 303 See Other' ); //302 might not work for POST requests, 303 is ignored by obsolete clients
@ header ( 'Location: ' . $url );
//another way for older browsers and already sent headers (eg trailing whitespace in config.php)
2004-09-22 14:39:15 +00:00
echo '<meta http-equiv="refresh" content="' . $delay . '; url=' . $encodedurl . '" />' ;
2006-08-01 18:02:42 +00:00
echo '<script type="text/javascript">' . " \n " . '<!--' . " \n " . " location.replace(' $surl '); " . " \n " . '//-->' . " \n " . '</script>' ; // To cope with Mozilla bug
2006-05-16 20:31:22 +00:00
die ;
}
2005-11-17 06:45:40 +00:00
2006-08-31 07:45:58 +00:00
if ( $delay == - 1 ) {
$delay = 3 ; // if no delay specified wait 3 seconds
2006-05-16 20:31:22 +00:00
}
if ( ! defined ( 'HEADER_PRINTED' )) {
2006-09-07 19:40:57 +00:00
// this type of redirect might not be working in some browsers - such as lynx :-(
2006-05-16 20:31:22 +00:00
print_header ( '' , '' , '' , '' , '<meta http-equiv="refresh" content="' . $delay . '; url=' . $encodedurl . '" />' );
2006-09-07 19:40:57 +00:00
$delay += 3 ; // double redirect prevention, it was sometimes breaking upgrades before 1.7
2006-05-16 20:31:22 +00:00
}
echo '<center>' ;
echo '<p>' . $message . '</p>' ;
echo '<p>( <a href="' . $encodedurl . '">' . get_string ( 'continue' ) . '</a> )</p>' ;
echo '</center>' ;
2006-08-31 07:45:58 +00:00
// it might be better not to set timeout the same for both types of redirect, so that we can be sure which one wins
2005-05-06 09:29:02 +00:00
?>
< script type = " text/javascript " >
<!--
function redirect () {
2006-08-01 18:02:42 +00:00
document . location . replace ( '<?php echo $surl ?>' );
2005-05-06 09:29:02 +00:00
}
2006-09-07 19:40:57 +00:00
setTimeout ( " redirect() " , < ? php echo ( $delay * 1000 ) ?> );
2005-05-06 09:29:02 +00:00
-->
</ script >
< ? php
2004-05-21 13:07:11 +00:00
die ;
2002-12-20 14:44:14 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Print a bold message in an optional color .
*
2004-09-25 05:29:21 +00:00
* @ param string $message The message to print out
2005-02-08 17:09:41 +00:00
* @ param string $style Optional style to display message text in
* @ param string $align Alignment option
2006-10-31 10:47:26 +00:00
* @ param bool $return whether to return an output string or echo now
2004-09-23 04:36:43 +00:00
*/
2006-10-31 10:47:26 +00:00
function notify ( $message , $style = 'notifyproblem' , $align = 'center' , $return = false ) {
2005-02-08 17:09:41 +00:00
if ( $style == 'green' ) {
$style = 'notifysuccess' ; // backward compatible with old color system
}
2004-10-01 02:43:09 +00:00
$message = clean_text ( $message );
2006-10-31 10:47:26 +00:00
$output = '<div class="' . $style . '" align="' . $align . '">' . $message . '</div>' . " <br /> \n " ;
if ( $return ) {
return $output ;
}
echo $output ;
2002-12-20 14:44:14 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Given an email address , this function will return an obfuscated version of it
*
2004-09-25 05:29:21 +00:00
* @ param string $email The email address to obfuscate
* @ return string
2004-09-23 04:36:43 +00:00
*/
function obfuscate_email ( $email ) {
2003-07-11 08:38:39 +00:00
$i = 0 ;
$length = strlen ( $email );
2004-09-22 14:39:15 +00:00
$obfuscated = '' ;
2003-07-11 08:38:39 +00:00
while ( $i < $length ) {
if ( rand ( 0 , 2 )) {
$obfuscated .= '%' . dechex ( ord ( $email { $i }));
} else {
$obfuscated .= $email { $i };
}
$i ++ ;
}
return $obfuscated ;
}
2004-09-23 04:36:43 +00:00
/**
* This function takes some text and replaces about half of the characters
* with HTML entity equivalents . Return string is obviously longer .
*
2004-09-25 05:29:21 +00:00
* @ param string $plaintext The text to be obfuscated
* @ return string
2004-09-23 04:36:43 +00:00
*/
2003-07-11 08:38:39 +00:00
function obfuscate_text ( $plaintext ) {
2005-01-26 15:57:00 +00:00
2003-07-11 08:38:39 +00:00
$i = 0 ;
$length = strlen ( $plaintext );
2004-09-22 14:39:15 +00:00
$obfuscated = '' ;
2003-09-09 02:22:36 +00:00
$prev_obfuscated = false ;
2003-07-11 08:38:39 +00:00
while ( $i < $length ) {
2003-09-09 02:22:36 +00:00
$c = ord ( $plaintext { $i });
$numerical = ( $c >= ord ( '0' )) && ( $c <= ord ( '9' ));
if ( $prev_obfuscated and $numerical ) {
2004-09-23 12:09:19 +00:00
$obfuscated .= '&#' . ord ( $plaintext { $i }) . ';' ;
2003-09-09 02:22:36 +00:00
} else if ( rand ( 0 , 2 )) {
2004-09-23 12:09:19 +00:00
$obfuscated .= '&#' . ord ( $plaintext { $i }) . ';' ;
2003-09-09 02:22:36 +00:00
$prev_obfuscated = true ;
2003-07-11 08:38:39 +00:00
} else {
$obfuscated .= $plaintext { $i };
2003-09-09 02:22:36 +00:00
$prev_obfuscated = false ;
2003-07-11 08:38:39 +00:00
}
2003-09-09 02:22:36 +00:00
$i ++ ;
2003-07-11 08:38:39 +00:00
}
return $obfuscated ;
}
2004-09-23 04:36:43 +00:00
/**
2004-09-25 05:29:21 +00:00
* This function uses the { @ link obfuscate_email ()} and { @ link obfuscate_text ()}
* to generate a fully obfuscated email link , ready to use .
2004-09-23 04:36:43 +00:00
*
2004-09-25 05:29:21 +00:00
* @ param string $email The email address to display
* @ param string $label The text to dispalyed as hyperlink to $email
* @ param boolean $dimmed If true then use css class ' dimmed ' for hyperlink
* @ return string
2004-09-23 04:36:43 +00:00
*/
2004-09-22 14:39:15 +00:00
function obfuscate_mailto ( $email , $label = '' , $dimmed = false ) {
2003-07-11 08:38:39 +00:00
if ( empty ( $label )) {
$label = $email ;
}
2004-02-20 10:27:24 +00:00
if ( $dimmed ) {
$title = get_string ( 'emaildisable' );
$dimmed = ' class="dimmed"' ;
} else {
$title = '' ;
$dimmed = '' ;
}
2004-05-21 13:07:11 +00:00
return sprintf ( " <a href= \" %s:%s \" $dimmed title= \" $title\ " >% s </ a > " ,
2004-02-20 10:27:24 +00:00
obfuscate_text ( 'mailto' ), obfuscate_email ( $email ),
obfuscate_text ( $label ));
2003-07-11 08:38:39 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Prints a single paging bar to provide access to other pages ( usually in a search )
*
2005-05-22 00:50:37 +00:00
* @ param int $totalcount Thetotal number of entries available to be paged through
* @ param int $page The page you are currently viewing
* @ param int $perpage The number of entries that should be shown per page
2006-03-04 16:04:48 +00:00
* @ param string $baseurl The url which will be used to create page numbered links . Each page will consist of the base url appended by the page
2005-05-22 00:50:37 +00:00
var an equal sign , then the page number .
* @ param string $pagevar This is the variable name that you use for the page number in your code ( ie . 'tablepage' , 'blogpage' , etc )
2006-06-01 05:51:45 +00:00
* @ param bool $nocurr do not display the current page as a link
* @ param bool $return whether to return an output string or echo now
* @ return bool or string
2004-09-23 04:36:43 +00:00
*/
2006-06-01 05:51:45 +00:00
function print_paging_bar ( $totalcount , $page , $perpage , $baseurl , $pagevar = 'page' , $nocurr = false , $return = false ) {
2003-08-22 06:07:18 +00:00
$maxdisplay = 18 ;
2006-06-01 05:51:45 +00:00
$output = '' ;
2003-08-21 09:33:07 +00:00
2003-08-19 03:35:53 +00:00
if ( $totalcount > $perpage ) {
2006-06-01 05:51:45 +00:00
$output .= '<div class="paging">' ;
$output .= get_string ( 'page' ) . ':' ;
2003-12-30 17:18:06 +00:00
if ( $page > 0 ) {
2005-05-22 00:50:37 +00:00
$pagenum = $page - 1 ;
2006-06-01 05:51:45 +00:00
$output .= ' (<a href="' . $baseurl . $pagevar . '=' . $pagenum . '">' . get_string ( 'previous' ) . '</a>) ' ;
2003-12-30 17:18:06 +00:00
}
2003-08-21 13:47:14 +00:00
$lastpage = ceil ( $totalcount / $perpage );
if ( $page > 15 ) {
$startpage = $page - 10 ;
2006-06-01 05:51:45 +00:00
$output .= ' <a href="' . $baseurl . $pagevar . '=0">1</a> ...' ;
2003-08-21 13:47:14 +00:00
} else {
$startpage = 0 ;
}
$currpage = $startpage ;
$displaycount = 0 ;
while ( $displaycount < $maxdisplay and $currpage < $lastpage ) {
$displaypage = $currpage + 1 ;
2005-09-01 04:48:22 +00:00
if ( $page == $currpage && empty ( $nocurr )) {
2006-06-01 05:51:45 +00:00
$output .= ' ' . $displaypage ;
2003-08-19 03:35:53 +00:00
} else {
2006-06-01 05:51:45 +00:00
$output .= ' <a href="' . $baseurl . $pagevar . '=' . $currpage . '">' . $displaypage . '</a>' ;
2003-08-19 04:37:32 +00:00
}
2003-08-21 13:47:14 +00:00
$displaycount ++ ;
$currpage ++ ;
2003-08-19 03:35:53 +00:00
}
2003-08-21 14:22:06 +00:00
if ( $currpage < $lastpage ) {
2003-08-22 06:07:18 +00:00
$lastpageactual = $lastpage - 1 ;
2006-06-01 05:51:45 +00:00
$output .= ' ...<a href="' . $baseurl . $pagevar . '=' . $lastpageactual . '">' . $lastpage . '</a> ' ;
2003-08-21 14:22:06 +00:00
}
2003-08-19 03:35:53 +00:00
$pagenum = $page + 1 ;
2003-08-21 13:47:14 +00:00
if ( $pagenum != $displaypage ) {
2006-06-01 05:51:45 +00:00
$output .= ' (<a href="' . $baseurl . $pagevar . '=' . $pagenum . '">' . get_string ( 'next' ) . '</a>)' ;
2003-08-19 03:35:53 +00:00
}
2006-06-01 05:51:45 +00:00
$output .= '</div>' ;
2003-08-19 03:35:53 +00:00
}
2006-06-01 05:51:45 +00:00
if ( $return ) {
return $output ;
}
echo $output ;
return true ;
2003-08-19 03:35:53 +00:00
}
2002-12-20 14:44:14 +00:00
2004-09-23 04:36:43 +00:00
/**
* This function is used to rebuild the < nolink > tag because some formats ( PLAIN and WIKI )
* will transform it to html entities
*
2004-09-25 05:29:21 +00:00
* @ param string $text Text to search for nolink tag in
* @ return string
2004-09-23 04:36:43 +00:00
*/
2004-05-11 23:17:25 +00:00
function rebuildnolinktag ( $text ) {
2004-05-21 13:07:11 +00:00
2004-05-11 23:17:25 +00:00
$text = preg_replace ( '/<(\/*nolink)>/i' , '<$1>' , $text );
return $text ;
}
2004-09-23 04:36:43 +00:00
/**
* Prints a nice side block with an optional header . The content can either
* be a block of HTML or a list of text with optional icons .
*
2006-06-09 14:25:18 +00:00
* @ param string $heading Block $title embedded in HTML tags , for example < h2 >.
2004-09-25 05:29:21 +00:00
* @ param string $content ?
* @ param array $list ?
* @ param array $icons ?
* @ param string $footer ?
* @ param array $attributes ?
2006-06-09 14:25:18 +00:00
* @ param string $title Plain text title , as embedded in the $heading .
2004-09-25 05:29:21 +00:00
* @ todo Finish documenting this function . Show example of various attributes , etc .
2004-09-23 04:36:43 +00:00
*/
2006-06-09 14:25:18 +00:00
function print_side_block ( $heading = '' , $content = '' , $list = NULL , $icons = NULL , $footer = '' , $attributes = array (), $title = '' ) {
2004-05-28 10:53:54 +00:00
2006-06-09 14:25:18 +00:00
//Accessibility: skip block link, with title-text (or $block_id) to differentiate links.
2006-03-03 11:48:02 +00:00
static $block_id = 0 ;
2006-02-28 12:29:51 +00:00
$block_id ++ ;
2006-06-09 14:25:18 +00:00
if ( empty ( $heading )) {
$skip_text = get_string ( 'skipblock' , 'access' ) . ' ' . $block_id ;
}
else {
2006-09-14 07:00:43 +00:00
$skip_text = get_string ( 'skipa' , 'access' , strip_tags ( $title ));
2006-06-09 14:25:18 +00:00
}
$skip_link = '<a href="#sb-' . $block_id . '" class="skip-block" title="' . $skip_text . '"><span class="accesshide">' . $skip_text . '</span></a>' ;
2006-03-06 12:49:07 +00:00
$skip_dest = '<span id="sb-' . $block_id . '" class="skip-block-to"></span>' ;
2006-06-09 14:25:18 +00:00
2006-02-28 12:29:51 +00:00
if ( ! empty ( $heading )) {
2006-06-09 14:25:18 +00:00
$heading = $skip_link . $heading ;
}
/* else { //ELSE: I think a single link on a page, "Skip block 4" is too confusing - don't print.
2006-02-28 12:29:51 +00:00
echo $skip_link ;
2006-06-09 14:25:18 +00:00
} */
2006-03-04 16:04:48 +00:00
2004-05-28 10:53:54 +00:00
print_side_block_start ( $heading , $attributes );
if ( $content ) {
echo $content ;
if ( $footer ) {
2005-02-09 20:13:56 +00:00
echo '<div class="footer">' . $footer . '</div>' ;
2004-05-28 10:53:54 +00:00
}
} else {
if ( $list ) {
2005-03-05 12:22:33 +00:00
$row = 0 ;
2006-02-28 12:29:51 +00:00
//Accessibility: replaced unnecessary table with list, see themes/standard/styles_layout.css
echo " \n <ul class='list'> \n " ;
2004-05-28 10:53:54 +00:00
foreach ( $list as $key => $string ) {
2006-02-28 12:29:51 +00:00
echo '<li class="r' . $row . '">' ;
2004-05-28 10:53:54 +00:00
if ( $icons ) {
2006-03-10 16:46:19 +00:00
echo '<span class="icon c0">' . $icons [ $key ] . '</span>' ;
2004-05-28 10:53:54 +00:00
}
2006-02-28 12:29:51 +00:00
echo '<span class="c1">' . $string . '</span>' ;
echo " </li> \n " ;
2005-03-05 12:22:33 +00:00
$row = $row ? 0 : 1 ;
2004-05-28 10:53:54 +00:00
}
2006-03-04 16:04:48 +00:00
echo " </ul> \n " ;
2004-05-28 10:53:54 +00:00
}
if ( $footer ) {
2005-02-09 20:13:56 +00:00
echo '<div class="footer">' . $footer . '</div>' ;
2004-05-28 10:53:54 +00:00
}
2005-04-03 12:15:45 +00:00
2004-05-28 10:53:54 +00:00
}
2005-02-05 21:37:53 +00:00
print_side_block_end ( $attributes );
2006-02-28 12:29:51 +00:00
echo $skip_dest ;
2004-05-28 10:53:54 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Starts a nice side block with an optional header .
*
2004-09-25 05:29:21 +00:00
* @ param string $heading ?
* @ param array $attributes ?
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2004-05-28 10:53:54 +00:00
function print_side_block_start ( $heading = '' , $attributes = array ()) {
2005-02-05 21:37:53 +00:00
global $CFG ;
2005-02-14 16:42:52 +00:00
2004-05-28 10:53:54 +00:00
// If there are no special attributes, give a default CSS class
2005-02-14 16:42:52 +00:00
if ( empty ( $attributes ) || ! is_array ( $attributes )) {
2004-05-28 10:53:54 +00:00
$attributes = array ( 'class' => 'sideblock' );
2005-02-14 16:42:52 +00:00
} else if ( ! isset ( $attributes [ 'class' ])) {
2004-05-28 10:53:54 +00:00
$attributes [ 'class' ] = 'sideblock' ;
2005-02-14 16:42:52 +00:00
} else if ( ! strpos ( $attributes [ 'class' ], 'sideblock' )) {
2004-05-28 10:53:54 +00:00
$attributes [ 'class' ] .= ' sideblock' ;
}
2005-02-09 01:23:06 +00:00
2004-05-28 10:53:54 +00:00
// OK, the class is surely there and in addition to anything
// else, it's tagged as a sideblock
2005-02-09 01:23:06 +00:00
/*
2005-04-03 12:15:45 +00:00
2005-02-09 01:23:06 +00:00
// IE misery: if I do it this way, blocks which start hidden cannot be "unhidden"
2005-04-03 12:15:45 +00:00
2005-02-09 01:23:06 +00:00
// If there is a cookie to hide this thing, start it hidden
2005-02-14 16:42:52 +00:00
if ( ! empty ( $attributes [ 'id' ]) && isset ( $_COOKIE [ 'hide:' . $attributes [ 'id' ]])) {
2005-02-09 01:23:06 +00:00
$attributes [ 'class' ] = 'hidden ' . $attributes [ 'class' ];
}
*/
2004-05-28 10:53:54 +00:00
$attrtext = '' ;
2005-02-14 16:42:52 +00:00
foreach ( $attributes as $attr => $val ) {
$attrtext .= ' ' . $attr . '="' . $val . '"' ;
2004-05-28 10:53:54 +00:00
}
2005-05-10 00:20:18 +00:00
echo '<div ' . $attrtext . '>' ;
if ( $heading ) {
2006-03-06 12:49:07 +00:00
//Accessibility: replaced <div> with H2; no, H2 more appropriate in moodleblock.class.php: _title_html.
echo '<div class="header">' . $heading . '</div>' ;
2005-05-10 00:20:18 +00:00
}
echo '<div class="content">' ;
2006-03-04 16:04:48 +00:00
2004-05-28 10:53:54 +00:00
}
2004-07-25 13:47:38 +00:00
2004-09-23 04:36:43 +00:00
/**
* Print table ending tags for a side block box .
*/
2005-11-08 07:19:27 +00:00
function print_side_block_end ( $attributes = array ()) {
2005-02-14 16:42:52 +00:00
global $CFG ;
2005-05-10 00:20:18 +00:00
echo '</div></div>' ;
2005-05-10 17:32:01 +00:00
2005-02-09 01:23:06 +00:00
// IE workaround: if I do it THIS way, it works! WTF?
2005-02-14 16:42:52 +00:00
if ( ! empty ( $CFG -> allowuserblockhiding ) && isset ( $attributes [ 'id' ])) {
2005-02-09 16:17:03 +00:00
echo '<script type="text/javascript"><!-- ' . " \n " . 'elementCookieHide("' . $attributes [ 'id' ] . '"); ' . " \n " . '--></script>' ;
}
2005-05-10 00:20:18 +00:00
2004-05-28 10:53:54 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Prints out code needed for spellchecking .
* Original idea by Ludo ( Marc Alier ) .
*
2004-09-25 05:29:21 +00:00
* @ uses $CFG
* @ param boolean $usehtmleditor ?
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2006-03-14 05:26:03 +00:00
function print_speller_code ( $usehtmleditor = false , $return = false ) {
2004-08-21 10:12:09 +00:00
global $CFG ;
2006-03-14 05:26:03 +00:00
$str = '' ;
2004-08-21 10:12:09 +00:00
if ( ! $usehtmleditor ) {
2006-03-14 05:26:03 +00:00
$str .= " \n " . '<script language="javascript" type="text/javascript">' . " \n " ;
$str .= 'function openSpellChecker() {' . " \n " ;
$str .= " \t var speller = new spellChecker(); \n " ;
$str .= " \t speller.popUpUrl = \" " . $CFG -> wwwroot . " /lib/speller/spellchecker.html \" ; \n " ;
$str .= " \t speller.spellCheckScript = \" " . $CFG -> wwwroot . " /lib/speller/server-scripts/spellchecker.php \" ; \n " ;
$str .= " \t speller.spellCheckAll(); \n " ;
$str .= '}' . " \n " ;
$str .= '</script>' . " \n " ;
2004-08-21 10:12:09 +00:00
} else {
2006-03-14 05:26:03 +00:00
$str .= " \n function spellClickHandler(editor, buttonId) { \n " ;
$str .= " \t editor._textArea.value = editor.getHTML(); \n " ;
$str .= " \t var speller = new spellChecker( editor._textArea ); \n " ;
$str .= " \t speller.popUpUrl = \" " . $CFG -> wwwroot . " /lib/speller/spellchecker.html \" ; \n " ;
$str .= " \t speller.spellCheckScript = \" " . $CFG -> wwwroot . " /lib/speller/server-scripts/spellchecker.php \" ; \n " ;
$str .= " \t speller._moogle_edit=1; \n " ;
$str .= " \t speller._editor=editor; \n " ;
$str .= " \t speller.openChecker(); \n " ;
$str .= '}' . " \n " ;
2004-08-21 10:12:09 +00:00
}
2006-03-14 05:26:03 +00:00
if ( $return ) {
return $str ;
}
echo $str ;
2004-08-21 10:12:09 +00:00
}
2004-09-23 04:36:43 +00:00
/**
* Print button for spellchecking when editor is disabled
*/
2004-08-21 10:12:09 +00:00
function print_speller_button () {
2004-09-22 14:39:15 +00:00
echo '<input type="button" value="Check spelling" onclick="openSpellChecker();" />' . " \n " ;
2004-08-21 10:12:09 +00:00
}
2004-10-19 21:04:28 +00:00
2005-02-02 02:25:50 +00:00
function page_id_and_class ( & $getid , & $getclass ) {
// Create class and id for this page
global $CFG , $ME ;
static $class = NULL ;
static $id = NULL ;
2006-03-01 02:38:03 +00:00
if ( empty ( $CFG -> pagepath )) {
$CFG -> pagepath = $ME ;
}
if ( empty ( $class ) || empty ( $id )) {
$path = str_replace ( $CFG -> httpswwwroot . '/' , '' , $CFG -> pagepath ); //Because the page could be HTTPSPAGEREQUIRED
2005-02-02 02:25:50 +00:00
$path = str_replace ( '.php' , '' , $path );
if ( substr ( $path , - 1 ) == '/' ) {
$path .= 'index' ;
}
2005-03-09 21:16:38 +00:00
if ( empty ( $path ) || $path == 'index' ) {
2005-02-02 02:25:50 +00:00
$id = 'site-index' ;
$class = 'course' ;
2005-11-08 07:19:27 +00:00
} else if ( substr ( $path , 0 , 5 ) == 'admin' ) {
$id = str_replace ( '/' , '-' , $path );
$class = 'admin' ;
2005-02-02 02:25:50 +00:00
} else {
$id = str_replace ( '/' , '-' , $path );
$class = explode ( '-' , $id );
array_pop ( $class );
$class = implode ( '-' , $class );
}
}
$getid = $id ;
$getclass = $class ;
}
2005-02-10 05:11:34 +00:00
/**
* Prints a maintenance message from / maintenance . html
*/
function print_maintenance_message () {
global $CFG , $SITE ;
2005-04-03 12:15:45 +00:00
2005-02-10 05:11:34 +00:00
print_header ( strip_tags ( $SITE -> fullname ), $SITE -> fullname , 'home' );
print_simple_box_start ( 'center' );
print_heading ( get_string ( 'sitemaintenance' , 'admin' ));
@ include ( $CFG -> dataroot . '/1/maintenance.html' );
print_simple_box_end ();
2005-03-13 07:28:19 +00:00
print_footer ();
2005-02-10 05:11:34 +00:00
}
2005-06-25 15:42:44 +00:00
/**
* Adjust the list of allowed tags based on $CFG -> allowobjectembed and user roles ( admin )
*/
function adjust_allowed_tags () {
2005-02-10 05:11:34 +00:00
2005-06-25 15:42:44 +00:00
global $CFG , $ALLOWED_TAGS ;
2005-06-29 16:17:11 +00:00
if ( ! empty ( $CFG -> allowobjectembed )) {
2005-06-25 15:42:44 +00:00
$ALLOWED_TAGS .= '<embed><object>' ;
}
}
2005-03-15 08:49:14 +00:00
/// Some code to print tabs
/// A class for tabs
class tabobject {
var $id ;
var $link ;
var $text ;
2006-03-08 04:53:48 +00:00
var $linkedwhenselected ;
2005-03-15 08:49:14 +00:00
/// A constructor just because I like constructors
2006-03-08 04:53:48 +00:00
function tabobject ( $id , $link = '' , $text = '' , $title = '' , $linkedwhenselected = false ) {
2005-03-15 08:49:14 +00:00
$this -> id = $id ;
$this -> link = $link ;
$this -> text = $text ;
2006-02-09 13:38:04 +00:00
$this -> title = $title ? $title : $text ;
2006-03-08 04:53:48 +00:00
$this -> linkedwhenselected = $linkedwhenselected ;
2005-03-15 08:49:14 +00:00
}
2005-04-03 12:15:45 +00:00
2005-03-15 08:49:14 +00:00
/// a method to look after the messy business of setting up a tab cell
/// with all the appropriate classes and things
2005-05-05 09:32:33 +00:00
function createtab ( $selected = false , $inactive = false , $activetwo = false , $last = false ) {
2005-03-15 08:49:14 +00:00
$str = '' ;
$astr = '' ;
$cstr = '' ;
/// The text and anchor for this tab
2006-03-08 04:53:48 +00:00
if ( $inactive || $activetwo || ( $selected && ! $this -> linkedwhenselected )) {
2005-03-15 08:49:14 +00:00
$astr .= $this -> text ;
} else {
2006-02-09 13:38:04 +00:00
$astr .= '<a href="' . $this -> link . '" title="' . $this -> title . '">' . $this -> text . '</a>' ;
2005-03-15 08:49:14 +00:00
}
/// There's an IE bug with background images in <a> tags
/// so we put a div around so that we can add a background image
$astr = '<div class="tablink">' . $astr . '</div>' ;
/// Set the class for inactive cells
if ( $inactive ) {
$cstr .= ' inactive' ;
2006-03-04 16:04:48 +00:00
2005-05-05 09:32:33 +00:00
/// Set the class for active cells in the second row
if ( $activetwo ) {
$cstr .= ' activetwo' ;
}
2005-04-03 12:15:45 +00:00
2005-03-15 08:49:14 +00:00
/// Set the class for the selected cell
} else if ( $selected ) {
$cstr .= ' selected' ;
2005-04-20 19:12:28 +00:00
2005-04-14 21:43:18 +00:00
/// Set the standard class for a cell
} else {
$cstr .= ' active' ;
2005-03-15 08:49:14 +00:00
}
2005-04-03 12:15:45 +00:00
2005-03-15 08:49:14 +00:00
/// Are we on the last tab in this row?
if ( $last ) {
2005-05-12 09:10:58 +00:00
$astr = '<div class="last">' . $astr . '</div>' ;
2005-03-15 08:49:14 +00:00
}
2005-04-03 12:15:45 +00:00
2005-03-15 08:49:14 +00:00
/// Lets set up the tab cell
$str .= '<td' ;
if ( ! empty ( $cstr )) {
$str .= ' class="' . ltrim ( $cstr ) . '"' ;
}
$str .= '>' ;
$str .= $astr ;
$str .= '</td>' ;
return $str ;
}
}
/**
* Returns a string containing a table with tabs inside and formatted with
* CSS styles .
*
* @ param array $tabrows An array of rows where each row is an array of tab objects
* @ param string $selected The id of the selected tab
* @ param array $inactive Ids of inactive tabs
**/
2005-05-05 09:32:33 +00:00
function print_tabs ( $tabrows , $selected = NULL , $inactive = NULL , $activetwo = NULL , $return = false ) {
2005-03-15 08:49:14 +00:00
global $CFG ;
/// Bring the row with the selected tab to the front
if ( ! empty ( $CFG -> tabselectedtofront ) and ( $selected !== NULL ) ) {
$found = false ;
$frontrows = array ();
$rearrows = array ();
foreach ( $tabrows as $row ) {
if ( $found ) {
$rearrows [] = $row ;
} else {
foreach ( $row as $tab ) {
if ( $found ) {
continue ;
}
$found = ( $selected == $tab -> id );
}
$frontrows [] = $row ;
}
}
$tabrows = array_merge ( $rearrows , $frontrows );
}
2005-04-03 12:15:45 +00:00
2005-03-15 08:49:14 +00:00
/// $inactive must be an array
if ( ! is_array ( $inactive )) {
$inactive = array ();
}
2006-03-04 16:04:48 +00:00
2005-05-05 09:32:33 +00:00
/// $activetwo must be an array
if ( ! is_array ( $activetwo )) {
$activetwo = array ();
}
2005-03-15 08:49:14 +00:00
2005-04-03 12:15:45 +00:00
/// A table to encapsulate the tabs
2005-03-15 08:49:14 +00:00
$str = '<table class="tabs" cellspacing="0">' ;
2005-03-15 09:47:41 +00:00
$str .= '<tr><td class="left side"></td><td>' ;
2005-04-03 12:15:45 +00:00
2005-03-27 15:28:35 +00:00
$rowcount = count ( $tabrows );
2005-03-15 08:49:14 +00:00
/// Cycle through the tab rows
foreach ( $tabrows as $row ) {
2005-03-27 15:28:35 +00:00
$rowcount -- ;
2005-04-03 12:15:45 +00:00
2005-03-27 15:28:35 +00:00
$str .= '<table class="tabrow r' . $rowcount . '" cellspacing="0">' ;
2005-03-15 08:49:14 +00:00
$str .= '<tr>' ;
2005-04-03 12:15:45 +00:00
2005-03-15 08:49:14 +00:00
$numberoftabs = count ( $row );
$currenttab = 0 ;
$cstr = '' ;
$astr = '' ;
2005-04-03 12:15:45 +00:00
2005-03-15 08:49:14 +00:00
/// Cycle through the tabs
foreach ( $row as $tab ) {
$currenttab ++ ;
2005-05-05 09:32:33 +00:00
$str .= $tab -> createtab ( ( $selected == $tab -> id ), ( in_array ( $tab -> id , $inactive )), ( in_array ( $tab -> id , $activetwo )), ( $currenttab == $numberoftabs ) );
2005-03-15 08:49:14 +00:00
}
2005-04-03 12:15:45 +00:00
2005-03-15 08:49:14 +00:00
$str .= '</tr>' ;
$str .= '</table>' ;
}
2005-03-15 09:47:41 +00:00
$str .= '</td><td class="right side"></td></tr>' ;
$str .= '</table>' ;
2005-04-03 12:15:45 +00:00
2005-03-15 09:47:41 +00:00
if ( $return ) {
return $str ;
}
echo $str ;
2005-03-15 08:49:14 +00:00
}
2006-03-01 07:47:14 +00:00
/**
* Returns a string containing a link to the user documentation for the current
* page . Also contains an icon by default . Shown to teachers and admin only .
*
* @ param string $text The text to be displayed for the link
* @ param string $iconpath The path to the icon to be displayed
*/
function page_doc_link ( $text = '' , $iconpath = '' ) {
global $ME , $CFG ;
2006-03-04 16:04:48 +00:00
2006-09-03 16:26:55 +00:00
if ( empty ( $CFG -> docroot ) || ! has_capability ( 'moodle/site:doclinks' )) {
2006-03-01 07:47:14 +00:00
return '' ;
}
2006-03-04 16:04:48 +00:00
2006-03-01 07:47:14 +00:00
if ( empty ( $CFG -> pagepath )) {
$CFG -> pagepath = $ME ;
}
2006-03-04 16:04:48 +00:00
2006-06-20 13:53:09 +00:00
$target = '' ;
if ( ! empty ( $CFG -> doctonewwindow )) {
$target = ' target="_blank"' ;
}
2006-03-01 07:47:14 +00:00
$path = str_replace ( $CFG -> httpswwwroot . '/' , '' , $CFG -> pagepath ); // Because the page could be HTTPSPAGEREQUIRED
$path = str_replace ( '.php' , '' , $path );
2006-04-16 16:21:50 +00:00
if ( empty ( $path )) { // Not for home page
return '' ;
}
2006-03-01 07:47:14 +00:00
$lang = str_replace ( '_utf8' , '' , current_language ());
2006-03-04 16:04:48 +00:00
2006-06-20 13:53:09 +00:00
$str = '<a href="' . $CFG -> docroot . '/' . $lang . '/' . $path . '"' . $target . '>' ;
2006-03-04 16:04:48 +00:00
2006-03-01 07:47:14 +00:00
if ( empty ( $iconpath )) {
$iconpath = $CFG -> wwwroot . '/pix/docs.gif' ;
}
2006-03-04 16:04:48 +00:00
2006-03-01 07:47:14 +00:00
$str .= '<img src="' . $iconpath . '" alt="Docs" />' . $text . '</a>' ;
2006-03-04 16:04:48 +00:00
2006-03-01 07:47:14 +00:00
return $str ;
}
2006-09-14 04:25:17 +00:00
/**
2006-09-23 09:38:39 +00:00
* Returns true if the current site debugging settings are equal or above specified level .
2006-09-14 04:25:17 +00:00
* eg use like this :
*
2006-09-23 09:38:39 +00:00
* 1 ) debugging ( 'a normal debug notice' );
* 2 ) debugging ( 'something really picky' , DEBUG_ALL );
* 3 ) debugging ( 'annoying debug message only for develpers' , DEBUG_DEVELOPER );
* 4 ) if ( debugging ()) { echo " a bunch of commands could be here " }
2006-09-14 04:25:17 +00:00
*
2006-09-14 04:31:05 +00:00
* @ param string $message a message to print
2006-09-14 04:25:17 +00:00
* @ param int $level the level at which this debugging statement should show
* @ return bool
*/
2006-09-23 09:38:39 +00:00
function debugging ( $message = '' , $level = DEBUG_NORMAL ) {
2006-09-14 04:25:17 +00:00
global $CFG ;
if ( empty ( $CFG -> debug )) {
return false ;
}
if ( $CFG -> debug >= $level ) {
if ( $message ) {
notify ( $message , 'notifytiny' );
}
return true ;
}
return false ;
}
2006-03-01 07:47:14 +00:00
2006-09-23 09:38:39 +00:00
/**
* Disable debug messages from debugging (), while keeping PHP error reporting level as is .
*/
function disable_debugging () {
global $CFG ;
$CFG -> debug = $CFG -> debug | 0x80000000 ; // switch the sign bit in integer number ;-)
}
2003-03-14 07:55:22 +00:00
// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
2006-09-21 09:16:41 +00:00
?>