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 //
// //
2008-01-04 06:45:55 +00:00
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
2002-12-20 14:44:14 +00:00
// //
// 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
2008-08-27 10:24:48 +00:00
* @ 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 " );
2007-02-02 06:40:22 +00:00
require_once ( " $CFG->libdir /ajax/ajaxlib.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
2007-12-14 21:22:38 +00:00
/**
* Javascript related defines
*/
define ( 'REQUIREJS_BEFOREHEADER' , 0 );
define ( 'REQUIREJS_INHEADER' , 1 );
define ( 'REQUIREJS_AFTERHEADER' , 2 );
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
*/
2007-04-24 17:09:12 +00:00
global $ALLOWED_TAGS ;
2003-10-16 14:25:36 +00:00
$ALLOWED_TAGS =
2008-07-26 11:29:00 +00:00
'<p><br><b><i><u><font><table><tbody><thead><tfoot><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>' ;
2005-06-05 05:40:24 +00:00
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' ,
2008-08-07 22:38:46 +00:00
'color' , 'callto' , 'cursor' , 'text-align' , 'font-size' , 'font-weight' , 'font-style' , 'font-family' ,
2008-12-03 16:31:49 +00:00
'border' , 'margin' , 'padding' , 'background' , 'background-color' , 'text-decoration' ); // 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
*/
2007-02-23 20:10:11 +00:00
function s ( $var , $strip = false ) {
2006-04-12 17:39:23 +00:00
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 ) {
2008-09-23 21:09:46 +00:00
return preg_replace ( " /&(# \ d+);/i " , " & $ 1; " , htmlspecialchars ( $var ));
2007-02-23 20:10:11 +00:00
} else {
return preg_replace ( " /&(# \ d+);/i " , " & $ 1; " , htmlspecialchars ( $var ));
2006-04-12 17:39:23 +00:00
}
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
}
2006-12-30 22:45:40 +00:00
/**
* Does proper javascript quoting .
2007-01-04 10:23:06 +00:00
* Do not use addslashes anymore , because it does not work when magic_quotes_sybase is enabled .
*
2007-02-23 06:03:09 +00:00
* @ since 1.8 - 22 / 02 / 2007
2006-12-30 22:45:40 +00:00
* @ param mixed value
* @ return mixed quoted result
*/
function addslashes_js ( $var ) {
if ( is_string ( $var )) {
$var = str_replace ( '\\' , '\\\\' , $var );
$var = str_replace ( array ( '\'' , '"' , " \n " , " \r " , " \0 " ), array ( '\\\'' , '\\"' , '\\n' , '\\r' , '\\0' ), $var );
2007-01-01 13:26:20 +00:00
$var = str_replace ( '</' , '<\/' , $var ); // XHTML compliance
2006-12-30 22:45:40 +00:00
} else if ( is_array ( $var )) {
$var = array_map ( 'addslashes_js' , $var );
} else if ( is_object ( $var )) {
$a = get_object_vars ( $var );
foreach ( $a as $key => $value ) {
$a [ $key ] = addslashes_js ( $value );
}
$var = ( object ) $a ;
}
return $var ;
}
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
/**
2007-03-05 05:47:27 +00:00
* Returns the URL of the HTTP_REFERER , less the querystring portion if required
2008-01-16 08:41:30 +00:00
* @ param boolean $stripquery if true , also removes the query part of the url .
2004-09-23 02:48:41 +00:00
* @ return string
*/
2007-03-05 05:47:27 +00:00
function get_referer ( $stripquery = true ) {
2006-12-30 21:25:13 +00:00
if ( isset ( $_SERVER [ 'HTTP_REFERER' ])) {
2007-03-05 05:47:27 +00:00
if ( $stripquery ) {
return strip_querystring ( $_SERVER [ 'HTTP_REFERER' ]);
} else {
return $_SERVER [ 'HTTP_REFERER' ];
}
2006-12-30 21:25:13 +00:00
} else {
2007-01-04 10:23:06 +00:00
return '' ;
2006-12-30 21:25:13 +00:00
}
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' ];
}
}
2007-10-17 20:21:16 +00:00
// TODO, this does not work in the situation described in MDL-11061, but
// I don't know how to fix it. Possibly believe $CFG->wwwroot ahead of what
// the server reports.
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
}
2007-05-03 04:34:53 +00:00
/**
* Class for creating and manipulating urls .
2007-06-19 14:44:02 +00:00
*
2007-05-20 14:05:49 +00:00
* See short write up here http :// docs . moodle . org / en / Development : lib / weblib . php_moodle_url
2007-05-03 04:34:53 +00:00
*/
class moodle_url {
2007-06-19 14:44:02 +00:00
var $scheme = '' ; // e.g. http
var $host = '' ;
2007-05-03 04:34:53 +00:00
var $port = '' ;
var $user = '' ;
2007-06-19 14:44:02 +00:00
var $pass = '' ;
2007-05-03 04:34:53 +00:00
var $path = '' ;
var $fragment = '' ;
var $params = array (); //associative array of query string params
2007-06-19 14:44:02 +00:00
2007-05-03 04:34:53 +00:00
/**
* Pass no arguments to create a url that refers to this page . Use empty string to create empty url .
2007-06-19 14:44:02 +00:00
*
2007-05-04 05:45:33 +00:00
* @ param string $url url default null means use this page url with no query string
* empty string means empty url .
* if you pass any other type of url it will be parsed into it ' s bits , including query string
* @ param array $params these params override anything in the query string where params have the same name .
2007-05-03 04:34:53 +00:00
*/
2007-05-04 05:45:33 +00:00
function moodle_url ( $url = null , $params = array ()){
2007-05-03 04:34:53 +00:00
global $FULLME ;
if ( $url !== '' ){
if ( $url === null ){
$url = strip_querystring ( $FULLME );
}
$parts = parse_url ( $url );
if ( $parts === FALSE ){
2008-04-04 02:54:20 +00:00
print_error ( 'invalidurl' );
2007-05-03 04:34:53 +00:00
}
if ( isset ( $parts [ 'query' ])){
2007-05-29 06:14:09 +00:00
parse_str ( str_replace ( '&' , '&' , $parts [ 'query' ]), $this -> params );
2007-05-03 04:34:53 +00:00
}
unset ( $parts [ 'query' ]);
foreach ( $parts as $key => $value ){
$this -> $key = $value ;
}
2007-06-19 14:44:02 +00:00
$this -> params ( $params );
2007-05-03 04:34:53 +00:00
}
2007-06-19 14:44:02 +00:00
}
2007-05-03 04:34:53 +00:00
/**
2007-06-19 14:44:02 +00:00
* Add an array of params to the params for this page . The added params override existing ones if they
2007-05-03 04:34:53 +00:00
* have the same name .
*
2008-07-24 15:22:31 +00:00
* @ param array $params Defaults to null . If null then return value of
* param 'name' .
* @ return array params for url .
2007-05-03 04:34:53 +00:00
*/
2008-07-24 15:22:31 +00:00
function params ( $params = null ){
if ( ! is_null ( $params )){
return $this -> params = $params + $this -> params ;
} else {
return $this -> params ;
}
2007-05-03 04:34:53 +00:00
}
2007-06-19 14:44:02 +00:00
2007-05-03 04:34:53 +00:00
/**
2007-06-19 14:44:02 +00:00
* Remove all params if no arguments passed . Or else remove param $arg1 , $arg2 , etc .
2007-05-03 04:34:53 +00:00
*
* @ param string $arg1
* @ param string $arg2
* @ param string $arg3
*/
function remove_params (){
if ( $thisargs = func_get_args ()){
foreach ( $thisargs as $arg ){
2008-05-26 11:50:51 +00:00
if ( isset ( $this -> params [ $arg ])){
unset ( $this -> params [ $arg ]);
2007-05-03 04:34:53 +00:00
}
}
} else { // no args
$this -> params = array ();
}
}
/**
2007-06-19 14:44:02 +00:00
* Add a param to the params for this page . The added param overrides existing one if they
2007-05-03 04:34:53 +00:00
* have the same name .
*
* @ param string $paramname name
2008-07-24 15:22:31 +00:00
* @ param string $param value . Defaults to null . If null then return value
* of param 'name'
2007-05-03 04:34:53 +00:00
*/
2008-07-24 15:22:31 +00:00
function param ( $paramname , $param = null ){
if ( ! is_null ( $param )){
$this -> params = array ( $paramname => $param ) + $this -> params ;
} else {
2008-07-25 16:20:46 +00:00
return $this -> params [ $paramname ];
2008-07-24 15:22:31 +00:00
}
2007-05-03 04:34:53 +00:00
}
2007-06-19 14:44:02 +00:00
2007-05-29 06:27:38 +00:00
function get_query_string ( $overrideparams = array ()){
2007-05-03 04:34:53 +00:00
$arr = array ();
$params = $overrideparams + $this -> params ;
foreach ( $params as $key => $val ){
$arr [] = urlencode ( $key ) . " = " . urlencode ( $val );
}
2007-05-29 06:27:38 +00:00
return implode ( $arr , " & " );
2007-05-03 04:34:53 +00:00
}
/**
* Outputs params as hidden form elements .
2007-05-03 10:03:59 +00:00
*
2007-05-20 14:05:49 +00:00
* @ param array $exclude params to ignore
* @ param integer $indent indentation
2008-05-22 13:29:19 +00:00
* @ param array $overrideparams params to add to the output params , these
* override existing ones with the same name .
2007-05-03 04:34:53 +00:00
* @ return string html for form elements .
*/
2008-05-22 13:29:19 +00:00
function hidden_params_out ( $exclude = array (), $indent = 0 , $overrideparams = array ()){
2007-05-03 04:34:53 +00:00
$tabindent = str_repeat ( " \t " , $indent );
$str = '' ;
2008-05-22 13:29:19 +00:00
$params = $overrideparams + $this -> params ;
foreach ( $params as $key => $val ){
2007-05-03 10:03:59 +00:00
if ( FALSE === array_search ( $key , $exclude )) {
2007-05-20 17:05:27 +00:00
$val = s ( $val );
2007-05-03 10:03:59 +00:00
$str .= " $tabindent <input type= \" hidden \" name= \" $key\ " value = \ " $val\ " /> \n " ;
}
2007-05-03 04:34:53 +00:00
}
return $str ;
}
/**
* Output url
2007-06-19 14:44:02 +00:00
*
2007-05-03 04:34:53 +00:00
* @ param boolean $noquerystring whether to output page params as a query string in the url .
* @ param array $overrideparams params to add to the output url , these override existing ones with the same name .
* @ return string url
*/
2007-06-19 14:44:02 +00:00
function out ( $noquerystring = false , $overrideparams = array ()) {
2007-05-03 04:34:53 +00:00
$uri = $this -> scheme ? $this -> scheme . ':' . (( strtolower ( $this -> scheme ) == 'mailto' ) ? '' : '//' ) : '' ;
$uri .= $this -> user ? $this -> user . ( $this -> pass ? ':' . $this -> pass : '' ) . '@' : '' ;
$uri .= $this -> host ? $this -> host : '' ;
$uri .= $this -> port ? ':' . $this -> port : '' ;
$uri .= $this -> path ? $this -> path : '' ;
if ( ! $noquerystring ){
2007-05-29 06:27:38 +00:00
$uri .= ( count ( $this -> params ) || count ( $overrideparams )) ? '?' . $this -> get_query_string ( $overrideparams ) : '' ;
2007-05-03 04:34:53 +00:00
}
$uri .= $this -> fragment ? '#' . $this -> fragment : '' ;
2007-06-19 14:44:02 +00:00
return $uri ;
2007-05-03 04:34:53 +00:00
}
/**
* Output action url with sesskey
2007-06-19 14:44:02 +00:00
*
2007-05-03 04:34:53 +00:00
* @ param boolean $noquerystring whether to output page params as a query string in the url .
* @ return string url
*/
2007-06-19 14:44:02 +00:00
function out_action ( $overrideparams = array ()) {
2007-05-03 04:34:53 +00:00
$overrideparams = array ( 'sesskey' => sesskey ()) + $overrideparams ;
2007-05-29 06:27:38 +00:00
return $this -> out ( false , $overrideparams );
2007-05-03 04:34:53 +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
*
2006-12-04 09:13:51 +00:00
* Checks that submitted POST data exists and returns it as object .
2004-09-23 04:36:43 +00:00
*
2006-12-04 09:13:51 +00:00
* @ return mixed false or object
2004-09-23 02:48:41 +00:00
*/
2008-06-09 16:53:30 +00:00
function data_submitted () {
2004-09-23 04:36:43 +00:00
2003-01-05 06:45:20 +00:00
if ( empty ( $_POST )) {
2003-01-02 14:49:23 +00:00
return false ;
} else {
2008-06-09 16:53:30 +00:00
return ( object ) $_POST ;
2003-01-02 14:49:23 +00:00
}
}
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-11-11 17:23:20 +00:00
$length = $textlib -> strlen ( $string );
2004-03-10 02:19:17 +00:00
$wordlength = 0 ;
for ( $i = 0 ; $i < $length ; $i ++ ) {
2006-11-11 17:23:20 +00:00
$char = $textlib -> substr ( $string , $i , 1 );
2006-01-04 08:23:42 +00:00
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
/**
2007-11-29 05:54:35 +00:00
* This function will print a button / link / etc . form element
* that will work on both Javascript and non - javascript browsers .
2004-09-23 02:48:41 +00:00
* Relies on the Javascript function openpopup in javascript . php
2004-09-23 04:36:43 +00:00
*
2007-11-29 05:54:35 +00:00
* All parameters default to null , only $type and $url are mandatory .
*
2004-09-23 02:48:41 +00:00
* $url must be relative to home page eg / mod / survey / stuff . php
2008-11-20 09:57:20 +00:00
* @ param string $url Web link . Either relative to $CFG -> wwwroot , or a full URL .
2008-05-21 14:59:33 +00:00
* @ param string $name Name to be assigned to the popup window ( this is used by
2008-03-20 07:05:06 +00:00
* client - side scripts to " talk " to the popup window )
2004-09-23 02:48:41 +00:00
* @ 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
2007-11-29 05:54:35 +00:00
* @ param string $return If true , return as a string , otherwise print
* @ param string $id id added to the element
* @ param string $class class added to the element
2004-09-23 02:48:41 +00:00
* @ return string
* @ uses $CFG
*/
2007-11-29 05:54:35 +00:00
function element_to_popup_window ( $type = null , $url = null , $name = null , $linkname = null ,
2008-01-13 08:30:39 +00:00
$height = 400 , $width = 500 , $title = null ,
2007-11-29 05:54:35 +00:00
$options = null , $return = false , $id = null , $class = null ) {
2001-11-22 06:23:56 +00:00
2008-01-13 08:30:39 +00:00
if ( is_null ( $url )) {
2008-04-29 04:10:08 +00:00
debugging ( 'You must give the url to display in the popup. URL is missing - can\'t create popup window.' , DEBUG_DEVELOPER );
2007-11-30 03:09:59 +00:00
}
2002-10-14 16:00:55 +00:00
2007-11-29 05:54:35 +00:00
global $CFG ;
2001-11-22 06:23:56 +00:00
2007-11-30 03:09:59 +00:00
if ( $options == 'none' ) { // 'none' is legacy, should be removed in v2.0
2008-01-13 08:30:39 +00:00
$options = null ;
2007-11-29 05:54:35 +00:00
}
// add some sane default options for popup windows
2008-01-13 08:30:39 +00:00
if ( ! $options ) {
$options = 'menubar=0,location=0,scrollbars,resizable' ;
2007-11-30 03:09:59 +00:00
}
2008-01-13 08:30:39 +00:00
if ( $width ) {
$options .= ',width=' . $width ;
2007-11-30 03:09:59 +00:00
}
2008-01-13 08:30:39 +00:00
if ( $height ) {
$options .= ',height=' . $height ;
2007-11-30 03:09:59 +00:00
}
2008-01-13 08:30:39 +00:00
if ( $id ) {
$id = ' id="' . $id . '" ' ;
2007-11-30 03:09:59 +00:00
}
2008-01-13 08:30:39 +00:00
if ( $class ) {
$class = ' class="' . $class . '" ' ;
2007-11-30 03:09:59 +00:00
}
2008-04-29 04:10:08 +00:00
if ( $name ) {
2008-04-29 05:10:34 +00:00
$_name = $name ;
if (( $name = preg_replace ( " / \ s/ " , '_' , $name )) != $_name ) {
debugging ( 'The $name of a popup window shouldn\'t contain spaces - string modified. ' . $_name . ' changed to ' . $name , DEBUG_DEVELOPER );
}
2008-04-29 04:10:08 +00:00
} else {
2008-03-20 07:05:06 +00:00
$name = 'popup' ;
2007-11-30 03:09:59 +00:00
}
2008-05-21 14:59:33 +00:00
2008-03-20 07:05:06 +00:00
// get some default string, using the localized version of legacy defaults
2008-05-02 16:11:30 +00:00
if ( is_null ( $linkname ) || $linkname === '' ) {
2008-01-18 05:18:11 +00:00
$linkname = get_string ( 'clickhere' );
2007-11-30 03:09:59 +00:00
}
2008-01-13 08:30:39 +00:00
if ( ! $title ) {
2008-01-18 05:18:11 +00:00
$title = get_string ( 'popupwindowname' );
2007-11-30 03:09:59 +00:00
}
2007-11-29 05:54:35 +00:00
2008-01-13 08:30:39 +00:00
$fullscreen = 0 ; // must be passed to openpopup
2007-11-29 05:54:35 +00:00
$element = '' ;
switch ( $type ) {
2008-11-20 09:57:20 +00:00
case 'button' :
2007-11-29 05:54:35 +00:00
$element = '<input type="button" name="' . $name . '" title="' . $title . '" value="' . $linkname . '" ' . $id . $class .
" onclick= \" return openpopup(' $url ', ' $name ', ' $options ', $fullscreen ); \" /> \n " ;
break ;
2008-11-20 09:57:20 +00:00
case 'link' :
// Add wwwroot only if the URL does not already start with http:// or https://
if ( ! preg_match ( '|https?://|' , $url )) {
$url = $CFG -> wwwroot . $url ;
2007-11-29 05:54:35 +00:00
}
2008-11-20 09:57:20 +00:00
$element = '<a title="' . s ( strip_tags ( $title )) . '" href="' . $url . '" ' .
2007-11-29 05:54:35 +00:00
" onclick= \" this.target=' $name '; return openpopup(' $url ', ' $name ', ' $options ', $fullscreen ); \" > $linkname </a> " ;
break ;
default :
2008-05-15 03:07:21 +00:00
print_error ( 'cannotcreatepopupwin' );
2007-11-29 05:54:35 +00:00
break ;
2004-09-17 00:10:53 +00:00
}
2004-08-02 19:09:06 +00:00
if ( $return ) {
2007-11-29 05:54:35 +00:00
return $element ;
2004-08-02 19:09:06 +00:00
} else {
2007-11-29 05:54:35 +00:00
echo $element ;
2004-08-02 19:09:06 +00:00
}
2001-11-22 06:23:56 +00:00
}
2004-09-23 02:48:41 +00:00
/**
2007-11-29 05:54:35 +00:00
* Creates and displays ( or returns ) a link to a popup window , using element_to_popup_window function .
2004-09-23 04:36:43 +00:00
*
2007-11-29 05:54:35 +00:00
* @ return string html code to display a link to a popup window .
* @ see element_to_popup_window ()
2004-09-23 02:48:41 +00:00
*/
2007-11-29 05:54:35 +00:00
function link_to_popup_window ( $url , $name = null , $linkname = null ,
$height = 400 , $width = 500 , $title = null ,
$options = null , $return = false ) {
2004-02-15 08:04:33 +00:00
2007-11-29 05:54:35 +00:00
return element_to_popup_window ( 'link' , $url , $name , $linkname , $height , $width , $title , $options , $return , null , null );
}
2005-04-15 13:41:57 +00:00
2007-11-29 05:54:35 +00:00
/**
* Creates and displays ( or returns ) a buttons to a popup window , using element_to_popup_window function .
*
* @ return string html code to display a button to a popup window .
* @ see element_to_popup_window ()
*/
function button_to_popup_window ( $url , $name = null , $linkname = null ,
$height = 400 , $width = 500 , $title = null , $options = null , $return = false ,
$id = null , $class = null ) {
2004-02-15 08:04:33 +00:00
2007-11-29 05:54:35 +00:00
return element_to_popup_window ( 'button' , $url , $name , $linkname , $height , $width , $title , $options , $return , $id , $class );
2004-02-15 08:04:33 +00:00
}
2004-09-23 02:48:41 +00:00
/**
* Prints a simple button to close a window
2008-01-16 08:41:30 +00:00
* @ param string $name name of the window to close
2008-09-18 07:34:45 +00:00
* @ param boolean $return whether this function should return a string or output it .
* @ param boolean $reloadopener if true , clicking the button will also reload
* the page that opend this popup window .
2008-01-16 08:41:30 +00:00
* @ return string if $return is true , nothing otherwise
2004-09-23 02:48:41 +00:00
*/
2008-09-18 07:34:45 +00:00
function close_window_button ( $name = 'closewindow' , $return = false , $reloadopener = false ) {
2007-03-05 05:36:43 +00:00
global $CFG ;
2008-09-18 07:34:45 +00:00
$js = 'self.close();' ;
if ( $reloadopener ) {
$js = 'window.opener.location.reload(1);' . $js ;
}
2006-08-10 04:33:55 +00:00
$output = '' ;
2007-01-05 06:23:35 +00:00
$output .= '<div class="closewindow">' . " \n " ;
2008-05-08 02:38:38 +00:00
$output .= '<form action="#"><div>' ;
2008-09-18 07:34:45 +00:00
$output .= '<input type="button" onclick="' . $js . '" value="' . get_string ( $name ) . '" />' ;
2007-03-05 05:36:43 +00:00
$output .= '</div></form>' ;
2007-01-05 06:23:35 +00:00
$output .= '</div>' . " \n " ;
2006-08-10 04:33:55 +00:00
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2001-11-22 06:23:56 +00:00
}
2005-03-22 17:31:04 +00:00
/*
2008-12-10 08:57:50 +00:00
* Try and close the current window using JavaScript , either immediately , or after a delay .
* @ param integer $delay a delay in seconds before closing the window . Default 0.
* @ param boolean $reloadopener if true , we will see if this window was a pop - up , and try
* to reload the parent window before this one closes .
2005-03-22 17:31:04 +00:00
*/
2008-12-10 08:57:50 +00:00
function close_window ( $delay = 0 , $reloadopener = false ) {
global $THEME ;
2005-03-22 17:31:04 +00:00
2008-12-10 08:57:50 +00:00
if ( ! defined ( 'HEADER_PRINTED' )) {
print_header ( get_string ( 'closewindow' ));
} else {
print_container_end_all ( false , $THEME -> open_header_containers );
}
if ( $reloadopener ) {
$function = 'close_window_reloading_opener' ;
} else {
$function = 'close_window' ;
}
echo '<p class="centerpara">' . get_string ( 'windowclosing' ) . '</p>' ;
print_delayed_js_call ( $delay , $function );
print_footer ( 'empty' );
exit ;
}
2005-03-22 17:31:04 +00:00
2004-09-23 04:36:43 +00:00
/**
2007-10-02 18:49:04 +00:00
* Given an array of values , output the HTML for a select element with those options .
* Normally , you only need to use the first few parameters .
2004-09-23 04:36:43 +00:00
*
2007-10-02 18:49:04 +00:00
* @ param array $options The options to offer . An array of the form
* $options [{ value }] = { text displayed for that option };
* @ param string $name the name of this form control , as in & lt ; select name = " ... " ...
* @ param string $selected the option to select initially , default none .
* @ param string $nothing The label for the 'nothing is selected' option . Defaults to get_string ( 'choose' ) .
* Set this to '' if you don 't want a ' nothing is selected ' option .
* @ param string $script in not '' , then this is added to the & lt ; select > element as an onchange handler .
* @ param string $nothingvalue The value corresponding to the $nothing option . Defaults to 0.
* @ param boolean $return if false ( the default ) the the output is printed directly , If true , the
* generated HTML is returned as a string .
* @ param boolean $disabled if true , the select is generated in a disabled state . Default , false .
* @ param int $tabindex if give , sets the tabindex attribute on the & lt ; select > element . Default none .
* @ param string $id value to use for the id attribute of the & lt ; select > element . If none is given ,
* then a suitable one is constructed .
2008-09-08 07:01:41 +00:00
* @ param mixed $listbox if false , display as a dropdown menu . If true , display as a list box .
* By default , the list box will have a number of rows equal to min ( 10 , count ( $options )), but if
* $listbox is an integer , that number is used for size instead .
2008-11-20 13:25:04 +00:00
* @ param boolean $multiple if true , enable multiple selections , else only 1 item can be selected . Used
* when $listbox display is enabled
* @ param string $class value to use for the class attribute of the & lt ; select > element . If none is given ,
* then a suitable one is constructed .
2004-09-23 04:36:43 +00:00
*/
2006-03-04 16:04:48 +00:00
function choose_from_menu ( $options , $name , $selected = '' , $nothing = 'choose' , $script = '' ,
2008-09-08 07:01:41 +00:00
$nothingvalue = '0' , $return = false , $disabled = false , $tabindex = 0 ,
2008-11-20 13:25:04 +00:00
$id = '' , $listbox = false , $multiple = false , $class = '' ) {
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-12-12 22:06:37 +00:00
if ( $id === '' ) {
2007-01-07 21:54:39 +00:00
$id = 'menu' . $name ;
// name may contaion [], which would make an invalid id. e.g. numeric question type editing form, assignment quickgrading
$id = str_replace ( '[' , '' , $id );
$id = str_replace ( ']' , '' , $id );
2006-12-12 22:06:37 +00:00
}
2008-11-20 13:25:04 +00:00
if ( $class === '' ) {
2008-11-20 19:04:23 +00:00
$class = 'menu' . $name ;
2008-11-20 13:25:04 +00:00
// name may contaion [], which would make an invalid class. e.g. numeric question type editing form, assignment quickgrading
$class = str_replace ( '[' , '' , $class );
$class = str_replace ( ']' , '' , $class );
}
2008-11-20 19:04:23 +00:00
$class = 'select ' . $class ; /// Add 'select' selector always
2008-11-20 13:25:04 +00:00
2008-09-08 07:01:41 +00:00
if ( $listbox ) {
if ( is_integer ( $listbox )) {
$size = $listbox ;
} else {
$numchoices = count ( $options );
if ( $nothing ) {
$numchoices += 1 ;
}
$size = min ( 10 , $numchoices );
}
$attributes .= ' size="' . $size . '"' ;
if ( $multiple ) {
$attributes .= ' multiple="multiple"' ;
}
}
2008-11-20 13:25:04 +00:00
$output = '<select id="' . $id . '" class="' . $class . '" name="' . $name . '" ' . $attributes . '>' . " \n " ;
2002-06-05 05:37:55 +00:00
if ( $nothing ) {
2006-12-12 22:06:37 +00:00
$output .= ' <option value="' . s ( $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
}
2008-09-08 07:01:41 +00:00
2003-01-05 06:45:20 +00:00
if ( ! empty ( $options )) {
foreach ( $options as $value => $label ) {
2006-12-12 22:06:37 +00:00
$output .= ' <option value="' . s ( $value ) . '"' ;
2008-09-08 07:01:41 +00:00
if (( string ) $value == ( string ) $selected ||
( is_array ( $selected ) && in_array ( $value , $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 .
2008-01-16 08:41:30 +00:00
* @ param string $name
2008-05-21 14:59:33 +00:00
* @ param string $selected
2008-01-16 08:41:30 +00:00
* @ param string $string ( defaults to '' )
* @ param boolean $return whether this function should return a string or output it ( defaults to false )
* @ param boolean $disabled ( defaults to false )
* @ param int $tabindex
2006-08-31 16:55:48 +00:00
*/
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 ) {
2007-06-19 14:44:02 +00:00
2007-03-06 02:59:25 +00:00
$output .= ' <optgroup label="' . s ( format_string ( $section )) . '">' . " \n " ;
2006-01-11 02:22:16 +00:00
foreach ( $values as $value => $label ) {
2007-03-06 02:59:25 +00:00
$output .= ' <option value="' . format_string ( $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' ;
}
2006-12-12 07:24:32 +00:00
if ( $alt ) {
$alt = strip_tags ( $alt );
} else {
2005-06-28 16:55:01 +00:00
$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 . " \" > " ;
2007-02-06 08:24: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
/**
2008-11-20 06:59:11 +00:00
* Implements a complete little form with a dropdown menu . When JavaScript is on
* selecting an option from the dropdown automatically submits the form ( while
* avoiding the usual acessibility problems with this appoach ) . With JavaScript
* off , a 'Go' button is printed .
2004-09-23 04:36:43 +00:00
*
2008-11-20 06:59:11 +00:00
* @ param string $baseurl The target URL up to the point of the variable that changes
* @ param array $options A list of value - label pairs for the popup list
* @ param string $formid id for the control . Must be unique on the page . Used in the HTML .
* @ param string $selected The option that is initially selected
2004-09-25 05:29:21 +00:00
* @ 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
2008-11-20 06:59:11 +00:00
* @ param boolean $return Indicates whether the function should return the HTML
2004-09-25 05:29:21 +00:00
* 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 .
2007-09-25 11:33:30 +00:00
* @ param string $selectlabel Text to place in a [ label ] element - preferred for accessibility .
2008-11-20 06:59:11 +00:00
* @ param array $optionsextra an array with the same keys as $options . The values are added within the corresponding < option ...> tag .
* @ param string $submitvalue Optional label for the 'Go' button . Defaults to get_string ( 'go' ) .
* @ param boolean $disabled If true , the menu will be displayed disabled .
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 >
*/
2008-11-20 06:59:11 +00:00
function popup_form ( $baseurl , $options , $formid , $selected = '' , $nothing = 'choose' , $help = '' , $helptext = '' , $return = false ,
$targetwindow = 'self' , $selectlabel = '' , $optionsextra = NULL , $submitvalue = '' , $disabled = false ) {
2004-09-25 05:29:21 +00:00
2008-06-22 16:51:55 +00:00
global $CFG , $SESSION ;
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
2008-11-20 06:59:11 +00:00
if ( empty ( $submitvalue )){
if ( ! isset ( $go )) {
$go = get_string ( 'go' );
$submitvalue = $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
}
2008-11-20 06:59:11 +00:00
if ( $disabled ) {
$disabled = 'disabled="disabled"' ;
} else {
$disabled = '' ;
}
2002-08-12 07:40:53 +00:00
2007-01-02 05:04:11 +00:00
// changed reference to document.getElementById('id_abc') instead of document.abc
// MDL-7861
2007-01-04 12:44:58 +00:00
$output = '<form action="' . $CFG -> wwwroot . '/course/jumpto.php"' .
2007-01-04 18:23:55 +00:00
' method="get" ' .
$CFG -> frametarget .
2007-01-03 18:53:03 +00:00
' id="' . $formid . '"' .
2005-05-10 04:00:56 +00:00
' class="popupform">' ;
2007-01-04 12:44:58 +00:00
if ( $help ) {
$button = helpbutton ( $help , $helptext , 'moodle' , true , false , '' , true );
} else {
$button = '' ;
}
2004-09-29 14:00:35 +00:00
2007-01-05 14:13:26 +00:00
if ( $selectlabel ) {
$selectlabel = '<label for="' . $formid . '_jump">' . $selectlabel . '</label>' ;
}
2008-11-20 06:59:11 +00:00
//IE and Opera fire the onchange when ever you move into a dropdown list with the keyboard.
2008-05-02 02:45:18 +00:00
//onfocus will call a function inside dropdown.js. It fixes this IE/Opera behavior.
2008-07-14 02:32:59 +00:00
//Note: There is a bug on Opera+Linux with the javascript code (first mouse selection is inactive),
2008-06-18 05:58:06 +00:00
//so we do not fix the Opera behavior on Linux
if ( check_browser_version ( 'MSIE' ) || ( check_browser_version ( 'Opera' ) && ! check_browser_operating_system ( " Linux " ))) {
2008-11-20 06:59:11 +00:00
$output .= '<div>' . $selectlabel . $button . '<select id="' . $formid . '_jump" onfocus="initSelect(\'' . $formid . '\',' . $targetwindow . ')" name="jump" ' . $disabled . '>' . " \n " ;
2008-05-02 02:45:18 +00:00
}
//Other browser
else {
2008-11-20 06:59:11 +00:00
$output .= '<div>' . $selectlabel . $button . '<select id="' . $formid . '_jump" name="jump" onchange="' . $targetwindow . '.location=document.getElementById(\'' . $formid . '\').jump.options[document.getElementById(\'' . $formid . '\').jump.selectedIndex].value;" ' . $disabled . '>' . " \n " ;
2008-05-02 02:45:18 +00:00
}
2008-05-21 14:59:33 +00:00
2004-09-22 14:39:15 +00:00
if ( $nothing != '' ) {
2008-08-18 06:40:41 +00:00
$selectlabeloption = '' ;
if ( $selected == '' ) {
$selectlabeloption = ' selected="selected"' ;
}
foreach ( $options as $value => $label ) { //if one of the options is the empty value, don't make this the default
if ( $value == '' ) {
$selected = '' ;
}
}
$output .= " <option value= \" javascript:void(0) \" $selectlabeloption > $nothing </option> \n " ;
2001-11-22 06:23:56 +00:00
}
2004-09-28 09:47:14 +00:00
$inoptgroup = false ;
2007-01-02 09:33:07 +00:00
2001-11-22 06:23:56 +00:00
foreach ( $options as $value => $label ) {
2005-01-26 15:57:00 +00:00
2007-01-02 09:33:07 +00:00
if ( $label == '--' ) { /// we are ending previous optgroup
/// 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 );
$output .= ' </optgroup>' ;
}
$optgr = array ();
$inoptgroup = false ;
continue ;
} else 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 ();
2007-03-06 02:59:25 +00:00
$optgr [] = ' <optgroup label="' . s ( format_string ( 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 ()]))
{
2008-11-20 06:59:11 +00:00
$url = $SESSION -> sid_process_url ( $baseurl . $value );
2005-12-05 02:24:45 +00:00
} else
{
2008-11-20 06:59:11 +00:00
$url = $baseurl . $value ;
2005-12-05 02:24:45 +00:00
}
$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
2007-05-16 08:03:37 +00:00
if ( ! empty ( $optionsextra [ $value ])) {
$optstr .= ' ' . $optionsextra [ $value ];
}
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 () . '" />' ;
2007-01-03 18:53:03 +00:00
$output .= '<div id="noscript' . $formid . '" style="display: inline;">' ;
2008-11-20 06:59:11 +00:00
$output .= '<input type="submit" value="' . $submitvalue . '" ' . $disabled . ' /></div>' ;
2004-09-29 14:00:35 +00:00
$output .= '<script type="text/javascript">' .
2006-12-22 04:19:52 +00:00
" \n //<![CDATA[ \n " .
2007-01-03 18:53:03 +00:00
'document.getElementById("noscript' . $formid . '").style.display = "none";' .
2006-12-22 04:19:52 +00:00
" \n //]]> \n " . '</script>' ;
2007-03-05 21:42:49 +00:00
$output .= '</div>' ;
2007-01-15 03:39:48 +00:00
$output .= '</form>' ;
2002-11-10 07:37:15 +00:00
if ( $return ) {
2007-01-04 12:44:58 +00:00
return $output ;
2002-11-10 07:37:15 +00:00
} else {
2007-01-04 12:44:58 +00:00
echo $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 )) {
2007-01-04 23:27:19 +00:00
echo '<span class="error">' . $error . '</span>' ;
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
/**
* 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 ])) {
2008-06-09 16:53:30 +00:00
return $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
*
2008-01-13 08:30:39 +00:00
* This function should mainly be used for long strings like posts ,
2007-12-15 18:42:49 +00:00
* answers , glossary items etc . For short strings @ see format_string () .
*
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
2008-05-15 21:40:00 +00:00
global $CFG , $COURSE , $DB ;
2004-01-16 10:49:07 +00:00
2008-02-03 10:57:07 +00:00
static $croncache = array ();
2008-12-19 02:16:00 +00:00
$hashstr = '' ;
2008-02-03 10:57: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 )) {
2007-01-27 19:56:08 +00:00
$courseid = $COURSE -> id ;
2004-01-16 10:49:07 +00:00
}
2003-08-26 18:44:09 +00:00
2008-12-19 02:16:00 +00:00
// if filter plugin is OOP format, add it to filter list and append hash
// value to $hashstr
if ( ! empty ( $CFG -> textfilters )) {
require_once ( $CFG -> libdir . '/filterlib.php' );
$textfilters = explode ( ',' , $CFG -> textfilters );
foreach ( $textfilters as $textfilter ) {
if ( is_readable ( $CFG -> dirroot . '/' . $textfilter . '/filter.php' )) {
include_once ( $CFG -> dirroot . '/' . $textfilter . '/filter.php' );
$text_filter = basename ( $textfilter ) . '_filter' ;
if ( class_exists ( $text_filter )) {
$obj = new $text_filter ( $courseid , $format , $options );
filter_base :: addfilter ( $text_filter , $obj );
$hashstr .= $obj -> hash ();
}
}
}
}
2006-09-22 18:08:53 +00:00
if ( ! empty ( $CFG -> cachetext ) and empty ( $options -> nocache )) {
2008-12-19 02:16:00 +00:00
$hashstr .= $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 ;
// for debug filtering system
// $hashstr .= time();
2008-02-03 10:57:07 +00:00
2008-12-19 02:16:00 +00:00
$time = time () - $CFG -> cachetext ;
$md5key = md5 ( $hashstr );
2008-02-03 12:53:33 +00:00
if ( defined ( 'FULLME' ) and FULLME == 'cron' ) {
2008-02-03 10:57:07 +00:00
if ( isset ( $croncache [ $md5key ])) {
return $croncache [ $md5key ];
}
}
2008-05-15 21:40:00 +00:00
if ( $oldcacheitem = $DB -> get_record ( 'cache_text' , array ( 'md5key' => $md5key ), '*' , true )) {
2006-03-20 02:33:41 +00:00
if ( $oldcacheitem -> timemodified >= $time ) {
2008-02-03 12:53:33 +00:00
if ( defined ( 'FULLME' ) and FULLME == 'cron' ) {
2008-02-03 10:57:07 +00:00
if ( count ( $croncache ) > 150 ) {
2008-02-25 20:22:59 +00:00
reset ( $croncache );
$key = key ( $croncache );
unset ( $croncache [ $key ]);
2008-02-03 10:57:07 +00:00
}
$croncache [ $md5key ] = $oldcacheitem -> formattedtext ;
}
2006-03-20 02:33:41 +00:00
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 ) {
2007-01-20 13:36:03 +00:00
$text = clean_text ( $text , FORMAT_HTML );
2004-08-09 14:54:39 +00:00
}
2006-08-26 13:00:07 +00:00
if ( $options -> filter ) {
2008-12-19 02:16:00 +00:00
$text = filter_base :: do_filter ( $text , $courseid );
2005-10-29 21:32:09 +00:00
}
2003-02-05 12:41:59 +00:00
break ;
2003-04-27 10:46:16 +00:00
case FORMAT_PLAIN :
2007-01-20 13:36:03 +00:00
$text = s ( $text ); // cleans dangerous JS
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 ) {
2007-01-20 13:36:03 +00:00
$text = clean_text ( $text , FORMAT_HTML );
2004-08-09 14:54:39 +00:00
}
2006-03-04 16:04:48 +00:00
2006-08-26 13:00:07 +00:00
if ( $options -> filter ) {
2008-12-19 02:16:00 +00:00
$text = filter_base :: do_filter ( $text , $courseid );
2005-10-29 21:32:09 +00:00
}
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 ) {
2007-01-20 13:36:03 +00:00
$text = clean_text ( $text , FORMAT_HTML );
2004-08-09 14:54:39 +00:00
}
2006-03-04 16:04:48 +00:00
2006-08-26 13:00:07 +00:00
if ( $options -> filter ) {
2008-12-19 02:16:00 +00:00
$text = filter_base :: do_filter ( $text , $courseid );
2005-10-29 21:32:09 +00:00
}
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 ) {
2008-02-03 12:53:33 +00:00
if ( defined ( 'FULLME' ) and FULLME == 'cron' ) {
2008-02-03 10:57:07 +00:00
// special static cron cache - no need to store it in db if its not already there
if ( count ( $croncache ) > 150 ) {
2008-02-25 20:22:59 +00:00
reset ( $croncache );
$key = key ( $croncache );
unset ( $croncache [ $key ]);
2008-02-03 10:57:07 +00:00
}
$croncache [ $md5key ] = $text ;
return $text ;
}
2006-10-04 21:22:57 +00:00
$newcacheitem = new object ();
2006-03-20 02:33:41 +00:00
$newcacheitem -> md5key = $md5key ;
2008-05-15 21:40:00 +00:00
$newcacheitem -> formattedtext = $text ;
2006-03-20 02:33:41 +00:00
$newcacheitem -> timemodified = time ();
if ( $oldcacheitem ) { // See bug 4677 for discussion
$newcacheitem -> id = $oldcacheitem -> id ;
2008-05-15 21:40:00 +00:00
@ $DB -> update_record ( 'cache_text' , $newcacheitem ); // Update existing record in the cache table
2007-01-04 10:23:06 +00:00
// It's unlikely that the cron cache cleaner could have
2006-03-20 02:33:41 +00:00
// deleted this entry in the meantime, as it allows
// some extra time to cover these cases.
} else {
2008-05-15 21:40:00 +00:00
@ $DB -> insert_record ( 'cache_text' , $newcacheitem ); // Insert a new record in the cache table
2006-03-20 02:33:41 +00:00
// Again, it's possible that another user has caused this
2007-01-04 10:23:06 +00:00
// record to be created already in the time that it took
// to traverse this function. That's OK too, as the
2006-03-20 02:33:41 +00:00
// 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 ;
}
2007-12-25 10:03:59 +00:00
/**
* Resets all data related to filters , called during upgrade or when filter settings change .
* @ return void
*/
function reset_text_filters_cache () {
2008-06-02 21:52:27 +00:00
global $CFG , $DB ;
2007-12-25 10:03:59 +00:00
2008-06-02 21:52:27 +00:00
$DB -> delete_records ( 'cache_text' );
2007-12-25 10:03:59 +00:00
$purifdir = $CFG -> dataroot . '/cache/htmlpurifier' ;
remove_dir ( $purifdir , true );
}
2007-02-23 20:10:11 +00:00
2005-03-25 16:24:43 +00:00
/** Given a simple string , this function returns the string
2007-12-15 18:42:49 +00:00
* processed by enabled string filters if $CFG -> filterall is enabled
*
2008-01-13 08:30:39 +00:00
* This function should be used to print short strings ( non html ) that
* need filter processing e . g . activity titles , post subjects ,
2007-12-15 18:42:49 +00:00
* glossary concepts .
2005-03-25 16:24:43 +00:00
*
2005-03-26 01:07:23 +00:00
* @ param string $string The string to be filtered .
2007-03-16 03:24:36 +00:00
* @ param boolean $striplinks To strip any link in the result text ( Moodle 1.8 default changed from false to true ! MDL - 8713 )
2005-03-26 01:07:23 +00:00
* @ param int $courseid Current course as filters can , potentially , use it
2005-03-25 16:24:43 +00:00
* @ return string
*/
2007-03-16 03:24:36 +00:00
function format_string ( $string , $striplinks = true , $courseid = NULL ) {
2005-03-25 16:24:43 +00:00
2006-09-23 12:46:53 +00:00
global $CFG , $COURSE ;
2007-03-04 22:52:33 +00:00
2005-03-27 20:57:50 +00:00
//We'll use a in-memory cache here to speed up repeated strings
2007-02-23 20:10:11 +00:00
static $strcache = false ;
2007-03-04 22:52:33 +00:00
if ( $strcache === false or count ( $strcache ) > 2000 ) { // this number might need some tuning to limit memory usage in cron
2007-02-23 20:10:11 +00:00
$strcache = array ();
}
2007-06-19 14:44:02 +00:00
2007-03-04 22:52:33 +00:00
//init course id
2007-03-04 22:58:29 +00:00
if ( empty ( $courseid )) {
2007-03-04 22:52:33 +00:00
$courseid = $COURSE -> id ;
}
2005-03-27 20:57:50 +00:00
//Calculate md5
2007-03-04 22:52:33 +00:00
$md5 = md5 ( $string . '<+>' . $striplinks . '<+>' . $courseid . '<+>' . current_language ());
2005-03-27 20:57:50 +00:00
//Fetch from cache if possible
2007-03-04 22:52:33 +00:00
if ( isset ( $strcache [ $md5 ])) {
2005-03-27 20:57:50 +00:00
return $strcache [ $md5 ];
}
2007-03-06 02:59:25 +00:00
// First replace all ampersands not followed by html entity code
2007-03-22 16:37:55 +00:00
$string = preg_replace ( " / \ &(?![a-zA-Z0-9#] { 1,8};)/ " , " & " , $string );
2007-03-19 04:26:45 +00:00
2005-03-26 13:38:21 +00:00
if ( ! empty ( $CFG -> filterall )) {
2007-03-16 02:52:30 +00:00
$string = filter_string ( $string , $courseid );
2005-03-25 16:24:43 +00:00
}
2007-06-19 14:44:02 +00:00
2007-03-16 05:36:24 +00:00
// If the site requires it, strip ALL tags from this string
if ( ! empty ( $CFG -> formatstringstriptags )) {
$string = strip_tags ( $string );
2008-07-25 22:44:17 +00:00
} else {
// Otherwise strip just links if that is required (default)
if ( $striplinks ) { //strip links in string
$string = preg_replace ( '/(<a\s[^>]+?>)(.+?)(<\/a>)/is' , '$2' , $string );
}
$string = clean_text ( $string );
2005-03-26 01:07:23 +00:00
}
2005-03-27 20:57:50 +00:00
//Store to cache
$strcache [ $md5 ] = $string ;
2007-06-19 14:44:02 +00:00
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 ) {
2007-01-27 19:56:08 +00:00
global $CFG , $COURSE ;
if ( empty ( $courseid )) {
$courseid = $COURSE -> id ; // (copied from format_text)
}
2003-10-13 14:11:17 +00:00
2004-02-10 05:05:28 +00:00
if ( ! empty ( $CFG -> textfilters )) {
2007-03-15 06:08:46 +00:00
require_once ( $CFG -> libdir . '/filterlib.php' );
2004-02-10 05:05:28 +00:00
$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 ;
}
2007-03-15 06:08:46 +00:00
/**
* Given a string ( short text ) in HTML format , this function will pass it
* through any filters that have been defined in $CFG -> stringfilters
* 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
*
* @ param string $string The text to be passed through format filters
* @ param int $courseid The id of a course
* @ return string
*/
function filter_string ( $string , $courseid = NULL ) {
global $CFG , $COURSE ;
if ( empty ( $CFG -> textfilters )) { // All filters are disabled anyway so quit
return $string ;
}
if ( empty ( $courseid )) {
2007-06-19 14:44:02 +00:00
$courseid = $COURSE -> id ;
2007-03-15 06:08:46 +00:00
}
require_once ( $CFG -> libdir . '/filterlib.php' );
if ( isset ( $CFG -> stringfilters )) { // We have a predefined list to use, great!
if ( empty ( $CFG -> stringfilters )) { // but it's blank, so finish now
return $string ;
}
$stringfilters = explode ( ',' , $CFG -> stringfilters ); // ..use the list we have
} else { // Otherwise try to derive a list from textfilters
2007-03-16 05:36:24 +00:00
if ( strpos ( $CFG -> textfilters , 'filter/multilang' ) !== false ) { // Multilang is here
2007-03-15 06:08:46 +00:00
$stringfilters = array ( 'filter/multilang' ); // Let's use just that
$CFG -> stringfilters = 'filter/multilang' ; // Save it for next time through
2007-03-16 05:36:24 +00:00
} else {
$CFG -> stringfilters = '' ; // Save the result and return
return $string ;
2007-03-15 06:08:46 +00:00
}
}
2007-03-16 05:36:24 +00:00
2007-03-15 06:08:46 +00:00
foreach ( $stringfilters as $stringfilter ) {
if ( is_readable ( $CFG -> dirroot . '/' . $stringfilter . '/filter.php' )) {
include_once ( $CFG -> dirroot . '/' . $stringfilter . '/filter.php' );
$functionname = basename ( $stringfilter ) . '_filter' ;
if ( function_exists ( $functionname )) {
$string = $functionname ( $courseid , $string );
}
}
}
/// <nolink> tags removed for XHTML compatibility
$string = str_replace ( '<nolink>' , '' , $string );
$string = str_replace ( '</nolink>' , '' , $string );
return $string ;
}
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 !!!
2007-01-04 10:23:06 +00:00
* @ param string $text text that may contain TRUSTTEXT marker
2006-08-26 13:00:07 +00:00
* @ return text without any TRUSTTEXT marker
*/
function trusttext_strip ( $text ) {
global $CFG ;
while ( true ) { //removing nested TRUSTTEXT
2007-01-04 10:23:06 +00:00
$orig = $text ;
2006-08-26 13:00:07 +00:00
$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
2007-04-19 12:18:44 +00:00
global $ALLOWED_TAGS , $CFG ;
2007-04-18 21:52:03 +00:00
if ( empty ( $text ) or is_numeric ( $text )) {
2007-06-19 14:44:02 +00:00
return ( string ) $text ;
2007-04-18 21:52:03 +00:00
}
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 :
2007-04-15 21:54:34 +00:00
case FORMAT_MARKDOWN :
2004-07-30 04:02:58 +00:00
return $text ;
default :
2007-04-18 21:52:03 +00:00
if ( ! empty ( $CFG -> enablehtmlpurifier )) {
$text = purify_html ( $text );
} else {
/// Fix non standard entity notations
$text = preg_replace ( '/(&#[0-9]+)(;?)/' , " \\ 1; " , $text );
$text = preg_replace ( '/(&#x[0-9a-fA-F]+)(;?)/' , " \\ 1; " , $text );
2007-06-19 14:44:02 +00:00
2007-04-18 21:52:03 +00:00
/// Remove tags that are not allowed
$text = strip_tags ( $text , $ALLOWED_TAGS );
2007-06-19 14:44:02 +00:00
2007-04-18 21:52:03 +00:00
/// Clean up embedded scripts and , using kses
$text = cleanAttributes ( $text );
2007-06-27 11:58:33 +00:00
/// Again remove tags that are not allowed
$text = strip_tags ( $text , $ALLOWED_TAGS );
2007-04-18 21:52:03 +00:00
}
2005-01-29 13:02:28 +00:00
2007-04-18 21:52:03 +00:00
/// Remove potential script events - some extra protection for undiscovered bugs in our code
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
2007-04-18 21:52:03 +00:00
/**
* KSES replacement cleaning function - uses HTML Purifier .
*/
function purify_html ( $text ) {
global $CFG ;
2007-12-25 10:03:59 +00:00
// this can not be done only once because we sometimes need to reset the cache
2008-06-16 16:39:09 +00:00
$cachedir = $CFG -> dataroot . '/cache/htmlpurifier' ;
2007-12-25 10:03:59 +00:00
$status = check_dir_exists ( $cachedir , true , true );
2007-04-18 21:52:03 +00:00
static $purifier = false ;
2008-06-16 16:39:09 +00:00
static $config ;
2007-12-25 10:03:59 +00:00
if ( $purifier === false ) {
2008-06-16 16:39:09 +00:00
require_once $CFG -> libdir . '/htmlpurifier/HTMLPurifier.safe-includes.php' ;
2007-04-18 21:52:03 +00:00
$config = HTMLPurifier_Config :: createDefault ();
2008-06-16 16:39:09 +00:00
$config -> set ( 'Core' , 'ConvertDocumentToFragment' , true );
2007-08-21 22:06:47 +00:00
$config -> set ( 'Core' , 'Encoding' , 'UTF-8' );
$config -> set ( 'HTML' , 'Doctype' , 'XHTML 1.0 Transitional' );
2007-12-25 10:03:59 +00:00
$config -> set ( 'Cache' , 'SerializerPath' , $cachedir );
2007-04-18 21:52:03 +00:00
$config -> set ( 'URI' , 'AllowedSchemes' , array ( 'http' => 1 , 'https' => 1 , 'ftp' => 1 , 'irc' => 1 , 'nntp' => 1 , 'news' => 1 , 'rtsp' => 1 , 'teamspeak' => 1 , 'gopher' => 1 , 'mms' => 1 ));
2008-09-24 21:39:47 +00:00
$config -> set ( 'Attr' , 'AllowedFrameTargets' , array ( '_blank' ));
2007-04-18 21:52:03 +00:00
$purifier = new HTMLPurifier ( $config );
}
return $purifier -> purify ( $text );
}
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' ]);
2008-02-28 21:17:52 +00:00
$arreach [ 'value' ] = preg_replace ( " /b \ s*i \ s*n \ s*d \ s*i \ s*n \ s*g/i " , " Xbinding " , $arreach [ 'value' ]);
2007-01-03 23:35:03 +00:00
} else if ( $arreach [ 'name' ] == 'href' ) {
2007-01-06 15:22:23 +00:00
//Adobe Acrobat Reader XSS protection
2008-12-29 21:18:02 +00:00
$arreach [ 'value' ] = preg_replace ( '/(\.(pdf|fdf|xfdf|xdp|xfd)[^#]*)#.*$/i' , '$1' , $arreach [ 'value' ]);
2006-03-02 21:38:20 +00:00
}
2006-12-11 22:07:53 +00:00
$attStr .= ' ' . $arreach [ 'name' ] . '="' . $arreach [ 'value' ] . '"' ;
2004-08-21 04:34:06 +00:00
}
2005-01-31 20:09:17 +00:00
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 ) {
2008-05-11 23:34:29 +00:00
2003-03-12 03:42:44 +00:00
global $CFG ;
2008-05-11 23:34:29 +00:00
if ( empty ( $CFG -> emoticons )) { /// No emoticons defined, nothing to process here
return ;
}
2007-03-20 20:47:57 +00:00
$lang = current_language ();
2007-12-03 06:27:21 +00:00
$emoticonstring = $CFG -> emoticons ;
2003-03-12 21:37:05 +00:00
static $e = array ();
static $img = array ();
2007-12-03 06:27:21 +00:00
static $emoticons = null ;
if ( is_null ( $emoticons )) {
$emoticons = array ();
if ( $emoticonstring ) {
$items = explode ( '{;}' , $CFG -> emoticons );
foreach ( $items as $item ) {
$item = explode ( '{:}' , $item );
2008-01-13 08:30:39 +00:00
$emoticons [ $item [ 0 ]] = $item [ 1 ];
2007-12-03 06:27:21 +00:00
}
}
}
2007-03-20 20:47:57 +00:00
if ( empty ( $img [ $lang ])) { /// After the first time this is not run again
$e [ $lang ] = array ();
$img [ $lang ] = array ();
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' );
2008-11-24 01:05:55 +00:00
$alttext = preg_replace ( '/^\[\[(.*)\]\]$/' , '$1' , $alttext ); /// Clean alttext in case there isn't lang string for it.
2007-03-20 20:47:57 +00:00
$e [ $lang ][] = $emoticon ;
$img [ $lang ][] = '<img alt="' . $alttext . '" width="15" height="15" src="' . $CFG -> pixpath . '/s/' . $image . '.gif" />' ;
2003-03-12 20:36:55 +00:00
}
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
2007-03-20 20:47:57 +00:00
$text = str_replace ( $e [ $lang ], $img [ $lang ], $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
2008-09-25 10:07:11 +00:00
/**
* This code is called from help . php to inject a list of smilies into the
* emoticons help file .
*
* @ return string HTML for a list of smilies .
*/
function get_emoticons_list_for_help_file (){
global $CFG , $SESSION ;
if ( empty ( $CFG -> emoticons )) {
return '' ;
}
require_js ( array ( 'yui_yahoo' , 'yui_event' ));
$items = explode ( '{;}' , $CFG -> emoticons );
$output = '<ul id="emoticonlist">' ;
foreach ( $items as $item ) {
$item = explode ( '{:}' , $item );
$output .= '<li><img src="' . $CFG -> pixpath . '/s/' . $item [ 1 ] . '.gif" alt="' .
$item [ 0 ] . '" /><code>' . $item [ 0 ] . '</code></li>' ;
}
$output .= '</ul>' ;
if ( ! empty ( $SESSION -> inserttextform )) {
$formname = $SESSION -> inserttextform ;
$fieldname = $SESSION -> inserttextfield ;
} else {
$formname = 'theform' ;
$fieldname = 'message' ;
}
2008-11-20 06:59:11 +00:00
2008-09-25 10:07:11 +00:00
$output .= print_js_call ( 'emoticons_help.init' , array ( $formname , $fieldname , 'emoticonlist' ), true );
return $output ;
}
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
2008-12-17 02:55:33 +00:00
$h2t = new html2text ( $html );
$result = $h2t -> get_text ();
2008-07-14 02:32:59 +00:00
2008-12-17 02:55:33 +00:00
// html2text does not fix HTML entities so handle those here.
$result = html_entity_decode ( $result , ENT_NOQUOTES , 'UTF-8' );
2008-07-14 02:32:59 +00:00
2008-06-16 14:07:47 +00:00
return $result ;
2003-11-07 03:27:21 +00:00
}
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 .
*
2008-12-11 10:33:57 +00:00
* @ param string $needle The search string . Syntax like " word1 +word2 -word3 " is dealt with correctly .
* @ param string $haystack The string ( HTML ) within which to highlight the search terms .
* @ param boolean $matchcase whether to do case - sensitive . Default case - insensitive .
* @ param string $prefix the string to put before each search term found .
* @ param string $suffix the string to put after each search term found .
* @ return string The highlighted HTML .
2004-09-23 04:36:43 +00:00
*/
2008-12-11 10:33:57 +00:00
function highlight ( $needle , $haystack , $matchcase = false ,
$prefix = '<span class="highlight">' , $suffix = '</span>' ) {
2008-01-01 15:51:54 +00:00
2008-12-11 10:33:57 +00:00
/// Quick bail-out in trivial cases.
2008-01-01 15:51:54 +00:00
if ( empty ( $needle ) or empty ( $haystack )) {
2003-12-13 06:27:25 +00:00
return $haystack ;
}
2008-12-11 10:33:57 +00:00
/// Break up the search term into words, discard any -words and build a regexp.
$words = preg_split ( '/ +/' , trim ( $needle ));
foreach ( $words as $index => $word ) {
if ( strpos ( $word , '-' ) === 0 ) {
unset ( $words [ $index ]);
} else if ( strpos ( $word , '+' ) === 0 ) {
$words [ $index ] = '\b' . preg_quote ( ltrim ( $word , '+' ), '/' ) . '\b' ; // Match only as a complete word.
} else {
$words [ $index ] = preg_quote ( $word , '/' );
2003-08-09 03:57:46 +00:00
}
}
2008-12-11 10:33:57 +00:00
$regexp = '/(' . implode ( '|' , $words ) . ')/u' ; // u is do UTF-8 matching.
if ( ! $matchcase ) {
$regexp .= 'i' ;
2003-08-09 03:57:46 +00:00
}
2008-12-11 10:33:57 +00:00
/// Another chance to bail-out if $search was only -words
if ( empty ( $words )) {
return $haystack ;
2003-08-09 03:57:46 +00:00
}
2008-12-11 10:33:57 +00:00
/// Find all the HTML tags in the input, and store them in a placeholders array.
$placeholders = array ();
$matches = array ();
preg_match_all ( '/<[^>]*>/' , $haystack , $matches );
foreach ( array_unique ( $matches [ 0 ]) as $key => $htmltag ) {
$placeholders [ '<|' . $key . '|>' ] = $htmltag ;
}
2004-12-31 15:23:50 +00:00
2008-12-11 10:33:57 +00:00
/// In $hastack, replace each HTML tag with the corresponding placeholder.
$haystack = str_replace ( $placeholders , array_keys ( $placeholders ), $haystack );
2004-12-31 15:23:50 +00:00
2008-12-11 10:33:57 +00:00
/// In the resulting string, Do the highlighting.
$haystack = preg_replace ( $regexp , $prefix . '$1' . $suffix , $haystack );
2004-12-31 15:23:50 +00:00
2008-12-11 10:33:57 +00:00
/// Turn the placeholders back into HTML tags.
$haystack = str_replace ( array_keys ( $placeholders ), $placeholders , $haystack );
2003-08-09 03:57:46 +00:00
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
2008-01-01 15:51:54 +00:00
if ( empty ( $needle ) or empty ( $haystack )) {
return $haystack ;
}
2006-10-07 20:47:54 +00:00
$parts = explode ( moodle_strtolower ( $needle ), moodle_strtolower ( $haystack ));
2002-06-11 07:01:51 +00:00
2008-01-01 15:51:54 +00:00
if ( count ( $parts ) === 1 ) {
return $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
2008-01-01 15:51:54 +00:00
return str_replace ( '<span class="highlight"></span>' , '' , join ( '' , $parts ));
2002-06-11 07:01:51 +00:00
}
2007-02-21 10:42:50 +00:00
/**
* Return a string containing 'lang' , xml : lang and optionally 'dir' HTML attributes .
* Internationalisation , for print_header and backup / restorelib .
* @ param $dir Default false .
* @ return string Attributes .
*/
function get_html_lang ( $dir = false ) {
$direction = '' ;
if ( $dir ) {
if ( get_string ( 'thisdirection' ) == 'rtl' ) {
$direction = ' dir="rtl"' ;
} else {
$direction = ' dir="ltr"' ;
}
}
//Accessibility: added the 'lang' attribute to $direction, used in theme <html> tag.
$language = str_replace ( '_' , '-' , str_replace ( '_utf8' , '' , current_language ()));
2007-03-05 15:38:10 +00:00
@ header ( 'Content-Language: ' . $language );
2007-06-19 14:44:02 +00:00
return ( $direction . ' lang="' . $language . '" xml:lang="' . $language . '"' );
2007-02-21 10:42:50 +00:00
}
2007-10-22 06:43:00 +00:00
/**
* Return the markup for the destination of the 'Skip to main content' links .
2007-10-22 09:21:11 +00:00
* Accessibility improvement for keyboard - only users .
* Used in course formats , / index . php and / course / index . php
* @ return string HTML element .
2007-10-22 06:43:00 +00:00
*/
function skip_main_destination () {
return '<span id="maincontent"></span>' ;
}
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
2007-08-17 07:25:47 +00:00
* @ param string $title Appears at the top of the window
* @ param string $heading Appears at the top of the page
* @ param array $navigation Array of $navlinks arrays ( keys : name , link , type ) 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
2004-09-25 05:29:21 +00:00
* @ param boolean $cache Should this page be cacheable ?
2007-08-17 07:25:47 +00:00
* @ param string $button HTML code for a button ( usually for module editing )
* @ param string $menu HTML code for a popup menu
2004-09-25 05:29:21 +00:00
* @ param boolean $usexml use XML for this page
2007-08-17 07:25:47 +00:00
* @ param string $bodytags This text will be included verbatim in the < body > tag ( useful for onload () etc )
* @ 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
2007-01-27 19:56:08 +00:00
global $USER , $CFG , $THEME , $SESSION , $ME , $SITE , $COURSE ;
2007-06-19 14:44:02 +00:00
2007-08-27 09:40:14 +00:00
if ( gettype ( $navigation ) == 'string' && strlen ( $navigation ) != 0 && $navigation != 'home' ) {
2007-08-24 04:22:30 +00:00
debugging ( " print_header() was sent a string as 3rd ( $navigation ) parameter. "
2007-12-23 13:55:10 +00:00
. " This is deprecated in favour of an array built by build_navigation(). Please upgrade your code. " , DEBUG_DEVELOPER );
2007-08-24 04:22:30 +00:00
}
2007-02-28 06:25:22 +00:00
$heading = format_string ( $heading ); // Fix for MDL-8582
2007-06-19 14:44:02 +00:00
2008-01-07 01:54:28 +00:00
if ( defined ( 'CLI_UPGRADE' ) && CLI_UPGRADE ) {
$output = $heading ;
if ( $return ) {
return $output ;
} else {
2008-01-09 23:15:44 +00:00
console_write ( STDOUT , $output . " \n " , '' , false );
2008-01-07 01:54:28 +00:00
return ;
}
}
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' )) {
2008-04-04 02:54:20 +00:00
debugging ( 'print_header() was called more than once - this should not happen. Please check the code for this page closely. Note: print_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
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
2007-06-19 14:44:02 +00:00
2007-01-24 09:20:32 +00:00
/// Add the meta page from the themes if any were requested
$metapage = '' ;
if ( ! isset ( $THEME -> standardmetainclude ) || $THEME -> standardmetainclude ) {
ob_start ();
include_once ( $CFG -> dirroot . '/theme/standard/meta.php' );
$metapage .= ob_get_contents ();
ob_end_clean ();
}
if ( $THEME -> parent && ( ! isset ( $THEME -> parentmetainclude ) || $THEME -> parentmetainclude )) {
2007-01-25 04:43:26 +00:00
if ( file_exists ( $CFG -> dirroot . '/theme/' . $THEME -> parent . '/meta.php' )) {
ob_start ();
include_once ( $CFG -> dirroot . '/theme/' . $THEME -> parent . '/meta.php' );
$metapage .= ob_get_contents ();
ob_end_clean ();
}
2007-01-24 09:20:32 +00:00
}
if ( ! isset ( $THEME -> metainclude ) || $THEME -> metainclude ) {
2007-01-25 04:43:26 +00:00
if ( file_exists ( $CFG -> dirroot . '/theme/' . current_theme () . '/meta.php' )) {
ob_start ();
include_once ( $CFG -> dirroot . '/theme/' . current_theme () . '/meta.php' );
$metapage .= ob_get_contents ();
ob_end_clean ();
}
2007-01-24 09:20:32 +00:00
}
$meta = $meta . " \n " . $metapage ;
2007-11-20 18:04:03 +00:00
$meta .= " \n " . require_js ( '' , 1 );
2007-08-27 04:36:09 +00:00
2008-09-25 03:11:33 +00:00
$meta .= standard_js_config ();
2007-08-27 04:36:09 +00:00
/// Set up some navigation variables
2007-02-12 06:01:49 +00:00
2007-08-27 04:36:09 +00:00
if ( is_newnav ( $navigation )){
2002-12-29 04:17:32 +00:00
$home = false ;
2007-04-16 20:44:32 +00:00
} else {
if ( $navigation == 'home' ) {
$home = true ;
$navigation = '' ;
} 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
}
2008-07-23 03:18:02 +00:00
if ( file_exists ( $CFG -> dataroot . '/' . SITEID . '/maintenance.html' )) {
2009-01-01 14:25:29 +00:00
$button = '<a href="' . $CFG -> wwwroot . '/' . $CFG -> admin . '/maintenance.php">' . get_string ( 'maintenancemode' , 'admin' ) . '</a> ' . $button ;
2008-07-23 03:18:02 +00:00
if ( ! empty ( $title )) {
$title .= ' - ' ;
}
$title .= get_string ( 'maintenancemode' , 'admin' );
}
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
}
2007-01-19 10:29:03 +00:00
$menu = user_login_string ( $COURSE );
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 );
}
2008-11-29 14:22:10 +00:00
if ( has_capability ( 'coursereport/log:view' , get_context_instance ( CONTEXT_SYSTEM ))) {
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
2007-11-13 14:46:44 +00:00
$meta = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' .
2007-09-26 17:48:27 +00:00
" \n " . $meta . " \n " ;
2004-07-21 12:16:36 +00:00
if ( ! $usexml ) {
2007-11-13 14:46:44 +00:00
@ header ( 'Content-Type: text/html; charset=utf-8' );
2004-07-21 12:16:36 +00:00
}
2007-11-13 14:46:44 +00:00
@ header ( 'Content-Script-Type: text/javascript' );
@ header ( 'Content-Style-Type: text/css' );
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.
2007-02-21 10:42:50 +00:00
$direction = get_html_lang ( $dir = true );
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
2007-05-09 06:49:23 +00:00
$currentlanguage = current_language ();
2007-01-04 10:23:06 +00:00
if ( empty ( $usexml )) {
2006-12-12 07:24:32 +00:00
$direction = ' xmlns="http://www.w3.org/1999/xhtml"' . $direction ; // See debug_header
} else {
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
2007-03-19 04:26:45 +00:00
$title = format_string ( $title ); // fix for MDL-8582
2003-10-24 06:27:15 +00:00
$title = str_replace ( '"' , '"' , $title );
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
2007-01-19 10:29:03 +00:00
$pageclass .= ' course-' . $COURSE -> id ;
2005-01-29 10:49:48 +00:00
2007-01-10 14:10:45 +00:00
if ( ! isloggedin ()) {
$pageclass .= ' notloggedin' ;
}
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' ;
}
2007-07-29 20:46:05 +00:00
$pageclass .= ' dir-' . get_string ( 'thisdirection' );
2007-06-19 14:44:02 +00:00
2007-05-09 06:49:23 +00:00
$pageclass .= ' lang-' . $currentlanguage ;
2005-01-25 11:05:23 +00:00
$bodytags .= ' class="' . $pageclass . '" id="' . $pageid . '"' ;
2005-01-26 15:57:00 +00:00
2008-06-05 09:36:55 +00:00
require_once ( $CFG -> libdir . '/editor/htmlEditor.class.php' );
$htmlEditorObject = new htmlEditor ();
2008-07-03 05:32:34 +00:00
$htmlEditor = $htmlEditorObject -> configure ( NULL , $COURSE -> id );
2008-06-05 09:36:55 +00:00
2008-07-04 02:42:49 +00:00
ob_start ();
2006-09-29 05:43:48 +00:00
include ( $CFG -> header );
2008-07-04 02:42:49 +00:00
$output = ob_get_contents ();
ob_end_clean ();
2008-06-05 09:36:55 +00:00
2007-12-14 21:22:38 +00:00
// container debugging info
$THEME -> open_header_containers = open_containers ();
2007-10-22 09:21:11 +00:00
// Skip to main content, see skip_main_destination().
if ( $pageid == 'course-view' or $pageid == 'site-index' or $pageid == 'course-index' ) {
2007-10-22 06:43:00 +00:00
$skiplink = '<a class="skip" href="#maincontent">' . get_string ( 'tocontent' , 'access' ) . '</a>' ;
2008-02-19 13:34:31 +00:00
if ( ! preg_match ( '/(.*<div[^>]+id="page"[^>]*>)(.*)/s' , $output , $matches )) {
2007-10-22 06:43:00 +00:00
preg_match ( '/(.*<body.*?>)(.*)/s' , $output , $matches );
}
$output = $matches [ 1 ] . " \n " . $skiplink . $matches [ 2 ];
2007-10-22 09:21:11 +00:00
}
2007-10-22 06:43:00 +00:00
2007-01-03 01:51:02 +00:00
$output = force_strict_header ( $output );
2006-12-12 07:24:32 +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 ();
}
2007-11-20 18:04:03 +00:00
// Add in any extra JavaScript libraries that occurred during the header
$output .= require_js ( '' , 2 );
2008-10-31 08:45:35 +00:00
$output .= print_js_call ( 'moodle_initialise_body' , array (), true );
2007-11-20 18:04:03 +00:00
2006-08-10 05:01:57 +00:00
if ( $return ) {
return $output ;
} else {
echo $output ;
2005-01-31 07:11:42 +00:00
}
2002-12-20 14:44:14 +00:00
}
2007-11-20 18:04:03 +00:00
/**
* Used to include JavaScript libraries .
*
* When the $lib parameter is given , the function will ensure that the
2008-09-25 06:29:28 +00:00
* named library or libraries is loaded onto the page - either in the
* HTML < head > , just after the header , or at an arbitrary later point in
* the page , depending on where this function is called .
2007-11-20 18:04:03 +00:00
*
* Libraries will not be included more than once , so this works like
* require_once in PHP .
*
2008-09-25 06:29:28 +00:00
* There are two special - case calls to this function from print_header which are
2008-11-20 06:59:11 +00:00
* internal to weblib and use the second $extracthtml parameter :
2007-11-20 18:04:03 +00:00
* $extracthtml = 1 : this is used before printing the header .
2008-09-25 06:29:28 +00:00
* It returns the script tag code that should go inside the < head >.
2007-11-20 18:04:03 +00:00
* $extracthtml = 2 : this is used after printing the header and handles any
2008-09-25 06:29:28 +00:00
* require_js calls that occurred within the header itself .
2007-11-20 18:04:03 +00:00
*
2008-09-25 06:29:28 +00:00
* @ param mixed $lib The library or libraries to load ( a string or array of strings )
* There are three way to specify the library :
* 1. a shorname like 'yui_yahoo' . The list of recognised values is in lib / ajax / ajaxlib . php
* 2. the path to the library relative to wwwroot , for example 'lib/javascript-static.js'
* 3. ( legacy ) a full URL like $CFG -> wwwroot . '/lib/javascript-static.js' .
* @ param int $extracthtml Private . For internal weblib use only .
* @ return mixed No return value ( except when doing the internal $extracthtml
* calls , when it returns html code ) .
2007-11-20 18:04:03 +00:00
*/
2008-09-25 06:29:28 +00:00
function require_js ( $lib , $extracthtml = 0 ) {
2007-11-20 18:04:03 +00:00
global $CFG ;
static $loadlibs = array ();
static $state = REQUIREJS_BEFOREHEADER ;
static $latecode = '' ;
if ( ! empty ( $lib )) {
// Add the lib to the list of libs to be loaded, if it isn't already
// in the list.
if ( is_array ( $lib )) {
foreach ( $lib as $singlelib ) {
require_js ( $singlelib );
}
} else {
$libpath = ajax_get_lib ( $lib );
if ( array_search ( $libpath , $loadlibs ) === false ) {
$loadlibs [] = $libpath ;
// For state other than 0 we need to take action as well as just
// adding it to loadlibs
if ( $state != REQUIREJS_BEFOREHEADER ) {
// Get the script statement for this library
$scriptstatement = get_require_js_code ( array ( $libpath ));
if ( $state == REQUIREJS_AFTERHEADER ) {
// After the header, print it immediately
print $scriptstatement ;
} else {
// Haven't finished the header yet. Add it after the
// header
$latecode .= $scriptstatement ;
}
}
}
}
} else if ( $extracthtml == 1 ) {
if ( $state !== REQUIREJS_BEFOREHEADER ) {
debugging ( 'Incorrect state in require_js (expected BEFOREHEADER): be careful not to call with empty $lib (except in print_header)' );
} else {
$state = REQUIREJS_INHEADER ;
}
return get_require_js_code ( $loadlibs );
} else if ( $extracthtml == 2 ) {
if ( $state !== REQUIREJS_INHEADER ) {
debugging ( 'Incorrect state in require_js (expected INHEADER): be careful not to call with empty $lib (except in print_header)' );
return '' ;
} else {
$state = REQUIREJS_AFTERHEADER ;
return $latecode ;
}
} else {
debugging ( 'Unexpected value for $extracthtml' );
}
}
/**
* Should not be called directly - use require_js . This function obtains the code
* ( script tags ) needed to include JavaScript libraries .
* @ param array $loadlibs Array of library files to include
* @ return string HTML code to include them
*/
function get_require_js_code ( $loadlibs ) {
global $CFG ;
// Return the html needed to load the JavaScript files defined in
// our list of libs to be loaded.
$output = '' ;
foreach ( $loadlibs as $loadlib ) {
$output .= '<script type="text/javascript" ' ;
$output .= " src= \" $loadlib\ " ></ script > \n " ;
if ( $loadlib == $CFG -> wwwroot . '/lib/yui/logger/logger-min.js' ) {
// Special case, we need the CSS too.
$output .= '<link type="text/css" rel="stylesheet" ' ;
$output .= " href= \" { $CFG -> wwwroot } /lib/yui/logger/assets/logger.css \" /> \n " ;
}
}
return $output ;
}
2008-09-19 04:51:47 +00:00
/**
* Generate the HTML for calling a javascript funtion . You often need to do this
* if you have your javascript in an external file , and need to call one function
* to initialise it .
*
2008-09-28 08:21:58 +00:00
* You can pass in an optional list of arguments , which are properly escaped for
* you using the json_encode function .
2008-09-19 04:51:47 +00:00
*
* @ param string $function the name of the JavaScript function to call .
* @ param array $args an optional list of arguments to the function call .
* @ param boolean $return if true , return the HTML code , otherwise output it .
2008-09-19 13:37:33 +00:00
* @ return mixed string if $return is true , otherwise nothing .
2008-09-19 04:51:47 +00:00
*/
function print_js_call ( $function , $args = array (), $return = false ) {
$quotedargs = array ();
foreach ( $args as $arg ) {
2008-09-23 09:59:18 +00:00
$quotedargs [] = json_encode ( $arg );
2008-09-19 04:51:47 +00:00
}
$html = '' ;
$html .= '<script type="text/javascript">//<![CDATA[' . " \n " ;
$html .= $function . '(' . implode ( ', ' , $quotedargs ) . " ); \n " ;
$html .= " //]]></script> \n " ;
if ( $return ) {
return $html ;
} else {
echo $html ;
}
}
2007-11-20 18:04:03 +00:00
2008-09-26 10:49:31 +00:00
/**
* Generate the HTML for calling a javascript funtion after a time delay .
* In other respects , this function is the same as print_js_call .
*
* @ param integer $delay the desired delay in seconds .
* @ param string $function the name of the JavaScript function to call .
* @ param array $args an optional list of arguments to the function call .
* @ param boolean $return if true , return the HTML code , otherwise output it .
* @ return mixed string if $return is true , otherwise nothing .
*/
function print_delayed_js_call ( $delay , $function , $args = array (), $return = false ) {
$quotedargs = array ();
foreach ( $args as $arg ) {
$quotedargs [] = json_encode ( $arg );
}
$html = '' ;
$html .= '<script type="text/javascript">//<![CDATA[' . " \n " ;
$html .= 'setTimeout(function() {' . $function . '(' .
implode ( ', ' , $quotedargs ) . ');}, ' . ( $delay * 1000 ) . " ); \n " ;
$html .= " //]]></script> \n " ;
if ( $return ) {
return $html ;
} else {
echo $html ;
}
}
2008-09-19 13:37:33 +00:00
/**
* Sometimes you need access to some values in your JavaScript that you can only
* get from PHP code . You can handle this by generating your JS in PHP , but a
* better idea is to write static javascrip code that reads some configuration
* variable , and then just output the configuration variables from PHP using
* this function .
*
2008-11-20 06:59:11 +00:00
* For example , look at the code in question_init_qenginejs_script () in
2008-09-19 13:37:33 +00:00
* lib / questionlib . php . It writes out a bunch of $settings like
* 'pixpath' => $CFG -> pixpath , with $prefix = 'qengine_config' . This gets output
* in print_header , then the code in question / qengine . js can access these variables
* as qengine_config . pixpath , and so on .
*
* This method will also work without a prefix , but it is better to avoid that
* we don ' t want to add more things than necessary to the global JavaScript scope .
*
* This method automatically wrapps the values in quotes , and addslashes_js them .
*
* @ param array $settings the values you want to write out , as variablename => value .
* @ param string $prefix a namespace prefix to use in the JavaScript .
* @ param boolean $return if true , return the HTML code , otherwise output it .
* @ return mixed string if $return is true , otherwise nothing .
*/
function print_js_config ( $settings = array (), $prefix = '' , $return = false ) {
$html = '' ;
$html .= '<script type="text/javascript">//<![CDATA[' . " \n " ;
// Have to treat the prefix and no prefix cases separately.
if ( $prefix ) {
// Recommended way, only one thing in global scope.
2008-09-25 10:07:11 +00:00
$html .= " var $prefix = " . json_encode ( $settings ) . " \n " ;
2008-09-19 13:37:33 +00:00
} else {
// Old fashioned way.
foreach ( $settings as $name => $value ) {
2008-09-25 10:07:11 +00:00
$html .= " var $name = ' " . addslashes_js ( $value ) . " ' \n " ;
2008-09-19 13:37:33 +00:00
}
}
// Finish off and return/output.
$html .= " //]]></script> \n " ;
if ( $return ) {
return $html ;
} else {
echo $html ;
}
}
2008-09-25 03:11:33 +00:00
/**
* This function generates the code that defines the standard moodle_cfg object .
* This object has a number of fields that are values that various pieces of
* JavaScript code need access too . For example $CFG -> wwwroot and $CFG -> pixpath .
*
* @ return string a < script > tag that defines the moodle_cfg object .
*/
function standard_js_config () {
global $CFG ;
2008-10-29 08:17:18 +00:00
$config = array (
2008-09-25 03:11:33 +00:00
'wwwroot' => $CFG -> httpswwwroot , // Yes, really.
'pixpath' => $CFG -> pixpath ,
'modpixpath' => $CFG -> modpixpath ,
2008-10-31 08:25:19 +00:00
'sesskey' => sesskey (),
2008-10-29 08:17:18 +00:00
);
if ( debugging ( '' , DEBUG_DEVELOPER )) {
$config [ 'developerdebug' ] = true ;
}
return print_js_config ( $config , 'moodle_cfg' , true );
2008-09-25 03:11:33 +00:00
}
2006-12-12 07:24:32 +00:00
/**
* Debugging aid : serve page as 'application/xhtml+xml' where possible ,
* and substitute the XHTML strict document type .
* Note , requires the 'xmlns' fix in function print_header above .
* See : http :// tracker . moodle . org / browse / MDL - 7883
* TODO :
*/
2007-01-03 01:51:02 +00:00
function force_strict_header ( $output ) {
2006-12-12 07:24:32 +00:00
global $CFG ;
$strict = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' ;
$xsl = '/lib/xhtml.xsl' ;
2007-09-21 17:28:16 +00:00
if ( ! headers_sent () && ! empty ( $CFG -> xmlstrictheaders )) { // With xml strict headers, the browser will barf
2006-12-12 07:24:32 +00:00
$ctype = 'Content-Type: ' ;
$prolog = " <?xml version='1.0' encoding='utf-8'?> \n " ;
2007-01-04 10:23:06 +00:00
if ( isset ( $_SERVER [ 'HTTP_ACCEPT' ])
2006-12-12 07:24:32 +00:00
&& false !== strpos ( $_SERVER [ 'HTTP_ACCEPT' ], 'application/xhtml+xml' )) {
//|| false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') //Safari "Entity 'copy' not defined".
// Firefox et al.
$ctype .= 'application/xhtml+xml' ;
$prolog .= " <!-- \n DEBUG: $ctype \n --> \n " ;
} else if ( file_exists ( $CFG -> dirroot . $xsl )
&& preg_match ( '/MSIE.*Windows NT/' , $_SERVER [ 'HTTP_USER_AGENT' ])) {
// XSL hack for IE 5+ on Windows.
//$www_xsl = preg_replace('/(http:\/\/.+?\/).*/', '', $CFG->wwwroot) .$xsl;
$www_xsl = $CFG -> wwwroot . $xsl ;
$ctype .= 'application/xml' ;
$prolog .= " <?xml-stylesheet type='text/xsl' href=' $www_xsl '?> \n " ;
$prolog .= " <!-- \n DEBUG: $ctype \n --> \n " ;
} else {
//ELSE: Mac/IE, old/non-XML browsers.
$ctype .= 'text/html' ;
$prolog = '' ;
}
@ header ( $ctype . '; charset=utf-8' );
$output = $prolog . $output ;
// Test parser error-handling.
if ( isset ( $_GET [ 'error' ])) {
$output .= " __ TEST: XML well-formed error < __ \n " ;
}
}
2007-01-03 01:51:02 +00:00
2007-01-03 02:03:22 +00:00
$output = preg_replace ( '/(<!DOCTYPE.+?>)/s' , $strict , $output ); // Always change the DOCTYPE to Strict 1.0
2007-01-03 01:51:02 +00:00
2006-12-12 07:24:32 +00:00
return $output ;
}
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
2007-01-04 10:23:06 +00:00
global $COURSE , $CFG ;
2004-08-21 20:20:58 +00:00
2007-12-20 15:52:33 +00:00
// if we have no navigation specified, build it
if ( empty ( $navigation ) ){
$navigation = build_navigation ( '' );
}
2007-06-19 14:44:02 +00:00
// If old style nav prepend course short name otherwise leave $navigation object alone
2007-04-16 20:44:32 +00:00
if ( ! is_newnav ( $navigation )) {
2007-10-11 15:25:27 +00:00
if ( $COURSE -> id != SITEID ) {
$shortname = '<a href="' . $CFG -> wwwroot . '/course/view.php?id=' . $COURSE -> id . '">' . $COURSE -> shortname . '</a> ->' ;
$navigation = $shortname . ' ' . $navigation ;
}
2007-04-16 20:44:32 +00:00
}
$output = print_header ( $COURSE -> shortname . ': ' . $title , $COURSE -> fullname . ' ' . $heading , $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 $USER
2007-12-14 21:22:38 +00:00
* @ param mixed $course course object , used for course link button or
* 'none' means no user link , only docs link
* 'empty' means nothing printed in footer
* 'home' special frontpage footer
* @ param object $usercourse course used in user link
* @ param boolean $return output as string
* @ return mixed string or void
2004-09-23 04:36:43 +00:00
*/
2006-08-10 05:10:00 +00:00
function print_footer ( $course = NULL , $usercourse = NULL , $return = false ) {
2008-11-04 23:07:14 +00:00
global $USER , $CFG , $THEME , $COURSE , $SITE ;
2002-12-20 14:44:14 +00:00
2007-04-30 17:08:34 +00:00
if ( defined ( 'ADMIN_EXT_HEADER_PRINTED' ) and ! defined ( 'ADMIN_EXT_FOOTER_PRINTED' )) {
2007-04-30 17:13:37 +00:00
admin_externalpage_print_footer ();
return ;
2007-04-30 17:08:34 +00:00
}
2007-12-14 21:22:38 +00:00
/// Course links or special footer
2002-12-20 14:44:14 +00:00
if ( $course ) {
2007-12-14 21:22:38 +00:00
if ( $course === 'empty' ) {
// special hack - sometimes we do not want even the docs link in footer
$output = '' ;
if ( ! empty ( $THEME -> open_header_containers )) {
for ( $i = 0 ; $i < $THEME -> open_header_containers ; $i ++ ) {
$output .= print_container_end_all (); // containers opened from header
}
} else {
//1.8 theme compatibility
$output .= " \n </div> " ; // content div
}
$output .= " \n </div> \n </body> \n </html> " ; // close page div started in header
if ( $return ) {
return $output ;
} else {
echo $output ;
return ;
}
} else if ( $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 ;
2007-12-14 21:22:38 +00:00
} else if ( $course === 'home' ) { // special case for site home page - please do not remove
2008-11-04 23:07:14 +00:00
$course = $SITE ;
2005-03-27 17:07:09 +00:00
$homelink = '<div class="sitelink">' .
2008-03-05 05:36:49 +00:00
'<a title="Moodle ' . $CFG -> release . '" href="http://moodle.org/">' .
2007-07-25 14:50:37 +00:00
'<img style="width:100px;height:30px" src="pix/moodlelogo.gif" alt="moodlelogo" /></a></div>' ;
2005-03-26 06:51:33 +00:00
$home = true ;
2007-12-14 21:22:38 +00:00
2002-12-20 14:44:14 +00:00
} else {
2007-01-04 18:23:55 +00:00
$homelink = '<div class="homelink"><a ' . $CFG -> frametarget . ' href="' . $CFG -> wwwroot .
2007-02-28 06:25:22 +00:00
'/course/view.php?id=' . $course -> id . '">' . format_string ( $course -> shortname ) . '</a></div>' ;
2005-03-26 06:51:33 +00:00
$home = false ;
2002-12-20 14:44:14 +00:00
}
2007-12-14 21:22:38 +00:00
2002-12-20 14:44:14 +00:00
} else {
2008-11-04 23:07:14 +00:00
$course = $SITE ; // Set course as site course by default
2007-01-04 18:23:55 +00:00
$homelink = '<div class="homelink"><a ' . $CFG -> frametarget . ' 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
}
2007-12-14 21:22:38 +00:00
/// there should be exactly the same number of open containers as after the header
if ( $THEME -> open_header_containers != open_containers ()) {
debugging ( 'Unexpected number of open containers: ' . open_containers () . ', expecting ' . $THEME -> open_header_containers , DEBUG_DEVELOPER );
2007-11-23 20:26:16 +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 = '' ;
2007-01-21 21:46:55 +00:00
if ( defined ( 'MDL_PERF' ) || ( ! empty ( $CFG -> perfdebug ) and $CFG -> perfdebug > 7 )) {
2005-04-06 07:34:05 +00:00
$perf = get_performance_info ();
2007-09-12 02:57:26 +00:00
if ( defined ( 'MDL_PERFTOLOG' ) && ! function_exists ( 'register_shutdown_function' )) {
2005-04-07 00:11:28 +00:00
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-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
2007-05-02 09:35:35 +00:00
* @ uses $USER
* @ uses $SESSION
* @ uses $COURSE
* @ uses $FULLME
2005-01-30 18:23:06 +00:00
* @ return string
*/
function current_theme () {
2007-05-02 09:35:35 +00:00
global $CFG , $USER , $SESSION , $COURSE , $FULLME ;
if ( empty ( $CFG -> themeorder )) {
$themeorder = array ( 'page' , 'course' , 'category' , 'session' , 'user' , 'site' );
} else {
$themeorder = $CFG -> themeorder ;
}
2008-07-24 22:58:03 +00:00
if ( isloggedin () and isset ( $CFG -> mnet_localhost_id ) and $USER -> mnethostid != $CFG -> mnet_localhost_id ) {
2007-10-02 16:14:39 +00:00
require_once ( $CFG -> dirroot . '/mnet/peer.php' );
$mnet_peer = new mnet_peer ();
$mnet_peer -> set_id ( $USER -> mnethostid );
}
2007-05-02 09:35:35 +00:00
$theme = '' ;
foreach ( $themeorder as $themetype ) {
if ( ! empty ( $theme )) continue ;
2007-06-19 14:44:02 +00:00
2007-05-02 09:35:35 +00:00
switch ( $themetype ) {
case 'page' : // Page theme is for special page-only themes set by code
if ( ! empty ( $CFG -> pagetheme )) {
$theme = $CFG -> pagetheme ;
}
break ;
case 'course' :
if ( ! empty ( $CFG -> allowcoursethemes ) and ! empty ( $COURSE -> theme )) {
$theme = $COURSE -> theme ;
}
break ;
case 'category' :
if ( ! empty ( $CFG -> allowcategorythemes )) {
/// Nasty hack to check if we're in a category page
if ( stripos ( $FULLME , 'course/category.php' ) !== false ) {
global $id ;
if ( ! empty ( $id )) {
$theme = current_category_theme ( $id );
}
/// Otherwise check if we're in a course that has a category theme set
} else if ( ! empty ( $COURSE -> category )) {
$theme = current_category_theme ( $COURSE -> category );
}
}
break ;
case 'session' :
if ( ! empty ( $SESSION -> theme )) {
$theme = $SESSION -> theme ;
}
break ;
case 'user' :
if ( ! empty ( $CFG -> allowuserthemes ) and ! empty ( $USER -> theme )) {
2007-10-02 23:08:00 +00:00
if ( isloggedin () and $USER -> mnethostid != $CFG -> mnet_localhost_id && $mnet_peer -> force_theme == 1 && $mnet_peer -> theme != '' ) {
2007-10-02 16:14:39 +00:00
$theme = $mnet_peer -> theme ;
} else {
$theme = $USER -> theme ;
}
2007-05-02 09:35:35 +00:00
}
break ;
case 'site' :
2008-07-24 22:58:03 +00:00
if ( isloggedin () and isset ( $CFG -> mnet_localhost_id ) and $USER -> mnethostid != $CFG -> mnet_localhost_id && $mnet_peer -> force_theme == 1 && $mnet_peer -> theme != '' ) {
2007-10-02 16:14:39 +00:00
$theme = $mnet_peer -> theme ;
} else {
$theme = $CFG -> theme ;
}
2007-05-02 09:35:35 +00:00
break ;
default :
/// do nothing
}
}
2005-01-30 18:23:06 +00:00
2007-05-02 09:35:35 +00:00
/// A final check in case 'site' was not included in $CFG->themeorder
if ( empty ( $theme )) {
$theme = $CFG -> theme ;
}
2005-01-30 18:23:06 +00:00
2007-05-02 09:35:35 +00:00
return $theme ;
}
2005-01-30 18:23:06 +00:00
2007-05-02 09:35:35 +00:00
/**
* Retrieves the category theme if one exists , otherwise checks the parent categories .
* Recursive function .
*
* @ uses $COURSE
* @ param integer $categoryid id of the category to check
* @ return string theme name
*/
function current_category_theme ( $categoryid = 0 ) {
2008-05-15 21:40:00 +00:00
global $COURSE , $DB ;
2007-06-19 14:44:02 +00:00
2007-05-02 09:35:35 +00:00
/// Use the COURSE global if the categoryid not set
if ( empty ( $categoryid )) {
if ( ! empty ( $COURSE -> category )) {
$categoryid = $COURSE -> category ;
} else {
return false ;
}
}
2007-06-19 14:44:02 +00:00
2007-05-02 09:35:35 +00:00
/// Retrieve the current category
2008-05-15 21:40:00 +00:00
if ( $category = $DB -> get_record ( 'course_categories' , array ( 'id' => $categoryid ))) {
2007-06-19 14:44:02 +00:00
2007-05-02 09:35:35 +00:00
/// Return the category theme if it exists
if ( ! empty ( $category -> theme )) {
return $category -> theme ;
2005-01-30 18:23:06 +00:00
2007-05-02 09:35:35 +00:00
/// Otherwise try the parent category if one exists
} else if ( ! empty ( $category -> parent )) {
return current_category_theme ( $category -> parent );
}
2005-01-30 18:23:06 +00:00
2007-05-02 09:35:35 +00:00
/// Return false if we can't find the category record
2005-01-30 18:23:06 +00:00
} else {
2007-05-02 09:35:35 +00:00
return false ;
2005-01-30 18:23:06 +00:00
}
}
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
*/
2007-08-09 10:09:34 +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
2006-12-11 15:47:23 +00:00
if ( ! isset ( $THEME -> courseformatsheets ) || $THEME -> courseformatsheets ) { // Search for styles.php in course formats
if ( $mods = get_list_of_plugins ( 'format' , '' , $CFG -> dirroot . '/course' )) {
foreach ( $mods as $mod ) {
if ( file_exists ( $CFG -> dirroot . '/course/format/' . $mod . '/styles.php' )) {
$files [] = array ( $CFG -> dirroot , '/course/format/' . $mod . '/styles.php' );
}
}
}
}
2007-10-27 22:01:39 +00:00
if ( ! isset ( $THEME -> gradereportsheets ) || $THEME -> gradereportsheets ) { // Search for styles.php in grade reports
if ( $reports = get_list_of_plugins ( 'grade/report' )) {
foreach ( $reports as $report ) {
if ( file_exists ( $CFG -> dirroot . '/grade/report/' . $report . '/styles.php' )) {
$files [] = array ( $CFG -> dirroot , '/grade/report/' . $report . '/styles.php' );
}
}
}
}
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
2007-01-08 18:35:28 +00:00
/// check if csscobstants is set
if ( ! empty ( $THEME -> cssconstants )) {
require_once ( " $CFG->libdir /cssconstants.php " );
/// Actually collect all the files in order.
$css = '' ;
foreach ( $files as $file ) {
$css .= '/***** ' . $file [ 1 ] . ' start *****/' . " \n \n " ;
$css .= file_get_contents ( $file [ 0 ] . '/' . $file [ 1 ]);
$ccs .= '/***** ' . $file [ 1 ] . ' end *****/' . " \n \n " ;
}
/// replace css_constants with their values
echo replace_cssconstants ( $css );
} else {
/// Actually output all the files in order.
2007-06-23 12:21:59 +00:00
if ( empty ( $CFG -> CSSEdit ) && empty ( $THEME -> CSSEdit )) {
foreach ( $files as $file ) {
echo '/***** ' . $file [ 1 ] . ' start *****/' . " \n \n " ;
@ include_once ( $file [ 0 ] . '/' . $file [ 1 ]);
echo '/***** ' . $file [ 1 ] . ' end *****/' . " \n \n " ;
}
} else {
foreach ( $files as $file ) {
echo '/* @group ' . $file [ 1 ] . ' */' . " \n \n " ;
if ( strstr ( $file [ 1 ], '.css' ) !== FALSE ) {
echo '@import url("' . $CFG -> themewww . '/' . $file [ 1 ] . '");' . " \n \n " ;
} else {
@ include_once ( $file [ 0 ] . '/' . $file [ 1 ]);
}
echo '/* @end */' . " \n \n " ;
}
2007-01-08 18:35:28 +00:00
}
2005-03-24 13:41:42 +00:00
}
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
2007-01-28 21:18:08 +00:00
global $CFG , $THEME , $SESSION , $USER , $HTTPSPAGEREQUIRED ;
2005-01-31 05:59:13 +00:00
2007-12-15 16:53:39 +00:00
/// Do not mess with THEME if header already printed - this would break all the extra stuff in global $THEME from print_header()!!
if ( defined ( 'HEADER_PRINTED' )) {
return ;
}
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 ();
}
2007-07-25 14:50:37 +00:00
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 ();
}
2007-07-25 14:50:37 +00:00
2005-04-24 04:13:18 +00:00
/// 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
2007-02-02 12:19:58 +00:00
if ( isset ( $CFG -> smartpix ) && $CFG -> smartpix == 1 ) {
if ( $CFG -> slasharguments ) { // Use this method if possible for better caching
$extra = '' ;
} else {
$extra = '?file=' ;
}
2007-06-19 14:44:02 +00:00
2007-02-02 12:19:58 +00:00
$CFG -> pixpath = $CFG -> wwwroot . '/pix/smartpix.php' . $extra . '/' . $theme ;
$CFG -> modpixpath = $CFG -> wwwroot . '/pix/smartpix.php' . $extra . '/' . $theme . '/mod' ;
} else 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
2007-01-28 21:18:08 +00:00
/// We have to change some URLs in styles if we are in a $HTTPSPAGEREQUIRED page
if ( ! empty ( $HTTPSPAGEREQUIRED )) {
$CFG -> themewww = str_replace ( 'http:' , 'https:' , $CFG -> themewww );
$CFG -> pixpath = str_replace ( 'http:' , 'https:' , $CFG -> pixpath );
$CFG -> modpixpath = str_replace ( 'http:' , 'https:' , $CFG -> modpixpath );
foreach ( $CFG -> stylesheets as $key => $stylesheet ) {
$CFG -> stylesheets [ $key ] = str_replace ( 'http:' , 'https:' , $stylesheet );
}
}
2007-08-09 08:40:02 +00:00
// RTL support - only for RTL languages, add RTL CSS
if ( get_string ( 'thisdirection' ) == 'rtl' ) {
2008-01-13 08:30:39 +00:00
$CFG -> stylesheets [] = $CFG -> themewww . '/standard/rtl.css' . $paramstring ;
$CFG -> stylesheets [] = $CFG -> themewww . '/' . $theme . '/rtl.css' . $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 ) {
2008-05-15 21:40:00 +00:00
global $USER , $CFG , $SITE , $DB ;
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 ;
}
2009-01-02 20:32:05 +00:00
if ( session_is_loggedinas ()) {
$realuser = session_get_realuser ();
2009-01-02 15:15:26 +00:00
$fullname = fullname ( $realuser , true );
$realuserinfo = " [<a $CFG->frametarget
href = \ " $CFG->wwwroot /course/loginas.php?id= $course->id &return=1&sesskey= " . sesskey () . " \" > $fullname </a>] " ;
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 '' ;
2007-03-19 18:54:58 +00:00
} else if ( ! empty ( $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 );
2007-01-04 18:23:55 +00:00
$username = " <a $CFG->frametarget href= \" $CFG->wwwroot /user/view.php?id= $user->id &course= $course->id\ " > $fullname </ a > " ;
2008-05-15 21:40:00 +00:00
if ( is_mnet_remote_user ( $user ) and $idprovider = $DB -> get_record ( 'mnet_host' , array ( 'id' => $user -> mnethostid ))) {
2007-01-04 18:23:55 +00:00
$username .= " from <a $CFG->frametarget href= \" { $idprovider -> wwwroot } \" > { $idprovider -> name } </a> " ;
2007-01-04 02:52:44 +00:00
}
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' ) .
2007-01-04 18:23:55 +00:00
" (<a $CFG->frametarget href= \" $wwwroot /login/index.php \" > " . get_string ( 'login' ) . '</a>)' ;
2007-09-19 07:06:17 +00:00
} else if ( ! empty ( $user -> access [ 'rsw' ][ $context -> path ])) {
2006-09-21 14:42:45 +00:00
$rolename = '' ;
2008-05-15 21:40:00 +00:00
if ( $role = $DB -> get_record ( 'role' , array ( 'id' => $user -> access [ 'rsw' ][ $context -> path ]))) {
2006-09-21 14:42:45 +00:00
$rolename = ': ' . format_string ( $role -> name );
}
$loggedinas = get_string ( 'loggedinas' , 'moodle' , $username ) . $rolename .
2007-01-04 18:23:55 +00:00
" (<a $CFG->frametarget
2006-09-21 14:42:45 +00:00
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 ) . ' ' .
2007-03-02 16:47:38 +00:00
" (<a $CFG->frametarget href= \" $CFG->wwwroot /login/logout.php?sesskey= " . sesskey () . " \" > " . 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' ) .
2007-01-04 18:23:55 +00:00
" (<a $CFG->frametarget 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 ,
2007-01-04 10:23:06 +00:00
* search forum block , etc . Important : these are 'silent' in a screen - reader
2006-09-19 11:36:21 +00:00
* ( 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 ;
2007-01-04 10:23:06 +00:00
2006-09-19 11:36:21 +00:00
if ( ! isset ( $THEME -> rarrow ) and ! isset ( $THEME -> larrow )) {
2006-11-28 15:26:40 +00:00
// Default, looks good in Win XP/IE 6, Win/Firefox 1.5, Win/Netscape 8...
// Also OK in Win 9x/2K/IE 5.x
2006-09-19 11:36:21 +00:00
$THEME -> rarrow = '►' ;
$THEME -> larrow = '◄' ;
2008-07-10 08:50:43 +00:00
if ( empty ( $_SERVER [ 'HTTP_USER_AGENT' ])) {
$uagent = '' ;
} else {
$uagent = $_SERVER [ 'HTTP_USER_AGENT' ];
}
2006-11-28 15:26:40 +00:00
if ( false !== strpos ( $uagent , 'Opera' )
|| false !== strpos ( $uagent , 'Mac' )) {
// Looks good in Win XP/Mac/Opera 8/9, Mac/Firefox 2, Camino, Safari.
// Not broken in Mac/IE 5, Mac/Netscape 7 (?).
2006-09-19 11:36:21 +00:00
$THEME -> rarrow = '▶' ;
$THEME -> larrow = '◀' ;
2006-11-28 15:26:40 +00:00
}
elseif ( false !== strpos ( $uagent , 'Konqueror' )) {
$THEME -> rarrow = '→' ;
$THEME -> larrow = '←' ;
}
elseif ( isset ( $_SERVER [ 'HTTP_ACCEPT_CHARSET' ])
&& false === stripos ( $_SERVER [ 'HTTP_ACCEPT_CHARSET' ], 'utf-8' )) {
// (Win/IE 5 doesn't set ACCEPT_CHARSET, but handles Unicode.)
// To be safe, non-Unicode browsers!
$THEME -> rarrow = '>' ;
$THEME -> larrow = '<' ;
}
2008-01-13 08:30:39 +00:00
2007-08-09 08:40:02 +00:00
/// RTL support - in RTL languages, swap r and l arrows
2008-01-13 08:30:39 +00:00
if ( right_to_left ()) {
$t = $THEME -> rarrow ;
$THEME -> rarrow = $THEME -> larrow ;
$THEME -> larrow = $t ;
}
2006-09-19 11:36:21 +00:00
}
}
2006-08-10 06:41:38 +00:00
2007-08-09 08:40:02 +00:00
2007-05-09 14:15:33 +00:00
/**
2007-05-14 13:28:21 +00:00
* Return the right arrow with text ( 'next' ), and optionally embedded in a link .
* See function above , check_theme_arrows .
2007-05-22 08:59:56 +00:00
* @ param string $text HTML / plain text label ( set to blank only for breadcrumb separator cases ) .
2007-05-14 13:28:21 +00:00
* @ param string $url An optional link to use in a surrounding HTML anchor .
* @ param bool $accesshide True if text should be hidden ( for screen readers only ) .
* @ param string $addclass Additional class names for the link , or the arrow character .
* @ return string HTML string .
2007-05-09 14:15:33 +00:00
*/
2007-05-14 13:28:21 +00:00
function link_arrow_right ( $text , $url = '' , $accesshide = false , $addclass = '' ) {
2007-05-09 14:15:33 +00:00
global $THEME ;
check_theme_arrows ();
2007-05-14 13:28:21 +00:00
$arrowclass = 'arrow ' ;
if ( ! $url ) {
$arrowclass .= $addclass ;
}
$arrow = '<span class="' . $arrowclass . '">' . $THEME -> rarrow . '</span>' ;
$htmltext = '' ;
if ( $text ) {
2008-05-13 12:36:16 +00:00
$htmltext = '<span class="arrow_text">' . $text . '</span> ' ;
2007-05-14 13:28:21 +00:00
if ( $accesshide ) {
2007-09-25 11:33:30 +00:00
$htmltext = get_accesshide ( $htmltext );
2007-05-14 13:28:21 +00:00
}
2007-05-09 14:15:33 +00:00
}
2007-05-14 13:28:21 +00:00
if ( $url ) {
2008-05-13 12:36:16 +00:00
$class = 'arrow_link' ;
2007-05-14 13:28:21 +00:00
if ( $addclass ) {
2008-05-13 12:36:16 +00:00
$class .= ' ' . $addclass ;
2007-05-14 13:28:21 +00:00
}
2008-05-13 12:36:16 +00:00
return '<a class="' . $class . '" href="' . $url . '" title="' . preg_replace ( '/<.*?>/' , '' , $text ) . '">' . $htmltext . $arrow . '</a>' ;
2007-05-14 13:28:21 +00:00
}
return $htmltext . $arrow ;
2007-05-09 14:15:33 +00:00
}
/**
2007-05-14 13:28:21 +00:00
* Return the left arrow with text ( 'previous' ), and optionally embedded in a link .
* See function above , check_theme_arrows .
2007-05-22 08:59:56 +00:00
* @ param string $text HTML / plain text label ( set to blank only for breadcrumb separator cases ) .
2007-05-14 13:28:21 +00:00
* @ param string $url An optional link to use in a surrounding HTML anchor .
* @ param bool $accesshide True if text should be hidden ( for screen readers only ) .
* @ param string $addclass Additional class names for the link , or the arrow character .
* @ return string HTML string .
2007-05-09 14:15:33 +00:00
*/
2007-05-14 13:28:21 +00:00
function link_arrow_left ( $text , $url = '' , $accesshide = false , $addclass = '' ) {
2007-05-09 14:15:33 +00:00
global $THEME ;
check_theme_arrows ();
2007-05-14 13:28:21 +00:00
$arrowclass = 'arrow ' ;
if ( ! $url ) {
$arrowclass .= $addclass ;
}
$arrow = '<span class="' . $arrowclass . '">' . $THEME -> larrow . '</span>' ;
$htmltext = '' ;
if ( $text ) {
2008-05-13 12:36:16 +00:00
$htmltext = ' <span class="arrow_text">' . $text . '</span>' ;
2007-05-14 13:28:21 +00:00
if ( $accesshide ) {
2007-09-25 11:33:30 +00:00
$htmltext = get_accesshide ( $htmltext );
2007-05-14 13:28:21 +00:00
}
2007-05-09 14:15:33 +00:00
}
2007-05-14 13:28:21 +00:00
if ( $url ) {
2008-05-13 12:36:16 +00:00
$class = 'arrow_link' ;
2007-05-14 13:28:21 +00:00
if ( $addclass ) {
2008-05-13 12:36:16 +00:00
$class .= ' ' . $addclass ;
2007-05-14 13:28:21 +00:00
}
2008-05-13 12:36:16 +00:00
return '<a class="' . $class . '" href="' . $url . '" title="' . preg_replace ( '/<.*?>/' , '' , $text ) . '">' . $arrow . $htmltext . '</a>' ;
2007-05-14 13:28:21 +00:00
}
return $arrow . $htmltext ;
}
2007-09-25 11:33:30 +00:00
/**
* Return a HTML element with the class " accesshide " , for accessibility.
* Please use cautiously - where possible , text should be visible !
* @ param string $text Plain text .
* @ param string $elem Lowercase element name , default " span " .
* @ param string $class Additional classes for the element .
* @ param string $attrs Additional attributes string in the form , " name='value' name2='value2' "
* @ return string HTML string .
*/
function get_accesshide ( $text , $elem = 'span' , $class = '' , $attrs = '' ) {
return " < $elem class= \" accesshide $class\ " $attrs > $text </ $elem > " ;
}
2007-05-14 13:28:21 +00:00
/**
* Return the breadcrumb trail navigation separator .
* @ return string HTML string .
*/
function get_separator () {
//Accessibility: the 'hidden' slash is preferred for screen readers.
return ' ' . link_arrow_right ( $text = '/' , $url = '' , $accesshide = true , 'sep' ) . ' ' ;
2007-05-09 14:15:33 +00:00
}
2007-08-24 12:45:16 +00:00
/**
* Prints breadcrumb trail of links , called in theme /-/ header . html
*
* @ uses $CFG
* @ param mixed $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 ) {
2008-11-04 23:07:14 +00:00
global $CFG , $THEME , $SITE ;
2007-08-24 12:45:16 +00:00
$output = '' ;
if ( 0 === $separator ) {
$separator = get_separator ();
}
else {
$separator = '<span class="sep">' . $separator . '</span>' ;
}
if ( $navigation ) {
if ( is_newnav ( $navigation )) {
if ( $return ) {
return ( $navigation [ 'navlinks' ]);
} else {
echo $navigation [ 'navlinks' ];
return ;
}
} else {
debugging ( 'Navigation needs to be updated to use build_navigation()' , DEBUG_DEVELOPER );
}
if ( ! is_array ( $navigation )) {
$ar = explode ( '->' , $navigation );
$navigation = array ();
foreach ( $ar as $a ) {
if ( strpos ( $a , '</a>' ) === false ) {
$navigation [] = array ( 'title' => $a , 'url' => '' );
} else {
if ( preg_match ( '/<a.*href="([^"]*)">(.*)<\/a>/' , $a , $matches )) {
$navigation [] = array ( 'title' => $matches [ 2 ], 'url' => $matches [ 1 ]);
}
}
}
}
2008-11-04 23:07:14 +00:00
if ( ! $SITE ) {
2007-08-24 12:45:16 +00:00
$site = new object ();
$site -> shortname = get_string ( 'home' );
2008-11-04 23:07:14 +00:00
} else {
$site = $SITE ;
2007-08-24 12:45:16 +00:00
}
//Accessibility: breadcrumb links now in a list, » replaced with a 'silent' character.
2007-09-25 11:33:30 +00:00
$output .= get_accesshide ( get_string ( 'youarehere' , 'access' ), 'h2' ) . " <ul> \n " ;
2007-08-24 12:45:16 +00:00
$output .= '<li class="first">' . " \n " . '<a ' . $CFG -> frametarget . ' onclick="this.target=\'' . $CFG -> framename . '\'" href="'
. $CFG -> wwwroot . (( ! has_capability ( 'moodle/site:config' , get_context_instance ( CONTEXT_SYSTEM ))
&& ! empty ( $USER -> id ) && ! empty ( $CFG -> mymoodleredirect ) && ! isguest ())
? '/my' : '' ) . '/">' . format_string ( $site -> shortname ) . " </a> \n </li> \n " ;
foreach ( $navigation as $navitem ) {
$title = trim ( strip_tags ( format_string ( $navitem [ 'title' ], false )));
$url = $navitem [ 'url' ];
if ( empty ( $url )) {
$output .= '<li class="first">' . " $separator $title </li> \n " ;
} else {
$output .= '<li class="first">' . " $separator\n <a " . $CFG -> frametarget . ' onclick="this.target=\'' . $CFG -> framename . '\'" href="'
. $url . '">' . " $title </a> \n </li> \n " ;
}
}
$output .= " </ul> \n " ;
}
if ( $return ) {
return $output ;
} else {
echo $output ;
}
}
2006-09-19 11:36:21 +00:00
/**
2007-08-24 04:22:30 +00:00
* This function will build the navigation string to be used by print_header
2007-10-10 14:00:41 +00:00
* and others .
*
2007-10-12 10:36:11 +00:00
* It automatically generates the site and course level ( if appropriate ) links .
*
* If you pass in a $cm object , the method will also generate the activity ( e . g . 'Forums' )
* and activityinstances ( e . g . 'General Developer Forum' ) navigation levels .
*
* If you want to add any further navigation links after the ones this function generates ,
* the pass an array of extra link arrays like this :
* array (
* array ( 'name' => $linktext1 , 'link' => $url1 , 'type' => $linktype1 ),
* array ( 'name' => $linktext2 , 'link' => $url2 , 'type' => $linktype2 )
* )
* The normal case is to just add one further link , for example 'Editing forum' after
* 'General Developer Forum' , with no link .
* To do that , you need to pass
* array ( array ( 'name' => $linktext , 'link' => '' , 'type' => 'title' ))
* However , becuase this is a very common case , you can use a shortcut syntax , and just
* pass the string 'Editing forum' , instead of an array as $extranavlinks .
2008-01-13 08:30:39 +00:00
*
2007-10-12 10:36:11 +00:00
* At the moment , the link types only have limited significance . Type 'activity' is
2008-01-13 08:30:39 +00:00
* recognised in order to implement the $CFG -> hideactivitytypenavlink feature . Types
2007-10-12 10:36:11 +00:00
* that are known to appear are 'home' , 'course' , 'activity' , 'activityinstance' and 'title' .
* This really needs to be documented better . In the mean time , try to be consistent , it will
* enable people to customise the navigation more in future .
*
* When passing a $cm object , the fields used are $cm -> modname , $cm -> name and $cm -> course .
* If you get the $cm object using the function get_coursemodule_from_instance or
* get_coursemodule_from_id ( as recommended ) then this will be done for you automatically .
* If you don ' t have $cm -> modname or $cm -> name , this fuction will attempt to find them using
* the $cm -> module and $cm -> instance fields , but this takes extra database queries , so a
* warning is printed in developer debug mode .
2008-01-13 08:30:39 +00:00
*
2006-09-19 11:36:21 +00:00
* @ uses $CFG
2007-08-24 04:22:30 +00:00
* @ uses $THEME
2007-10-10 14:00:41 +00:00
*
2008-01-13 08:30:39 +00:00
* @ param mixed $extranavlinks - Normally an array of arrays , keys : name , link , type . If you
2007-10-12 15:11:57 +00:00
* only want one extra item with no link , you can pass a string instead . If you don ' t want
* any extra links , pass an empty string .
2008-01-13 08:30:39 +00:00
* @ param mixed $cm - optionally the $cm object , if you want this function to generate the
2007-10-10 14:00:41 +00:00
* activity and activityinstance levels of navigation too .
*
2007-08-24 04:22:30 +00:00
* @ return $navigation as an object so it can be differentiated from old style
2007-10-12 10:36:11 +00:00
* navigation strings .
2006-09-19 11:36:21 +00:00
*/
2007-10-10 14:00:41 +00:00
function build_navigation ( $extranavlinks , $cm = null ) {
2008-11-04 23:07:14 +00:00
global $CFG , $COURSE , $DB , $SITE ;
2007-01-04 10:23:06 +00:00
2007-10-12 10:36:11 +00:00
if ( is_string ( $extranavlinks )) {
2007-10-12 15:11:57 +00:00
if ( $extranavlinks == '' ) {
$extranavlinks = array ();
} else {
$extranavlinks = array ( array ( 'name' => $extranavlinks , 'link' => '' , 'type' => 'title' ));
}
2007-10-12 10:36:11 +00:00
}
2007-08-24 04:22:30 +00:00
$navlinks = array ();
//Site name
2008-11-04 23:07:14 +00:00
if ( $SITE ) {
2007-10-10 14:00:41 +00:00
$navlinks [] = array (
2008-11-04 23:07:14 +00:00
'name' => format_string ( $SITE -> shortname ),
2007-10-10 14:00:41 +00:00
'link' => " $CFG->wwwroot / " ,
'type' => 'home' );
}
// Course name, if appropriate.
if ( isset ( $COURSE ) && $COURSE -> id != SITEID ) {
$navlinks [] = array (
'name' => format_string ( $COURSE -> shortname ),
'link' => " $CFG->wwwroot /course/view.php?id= $COURSE->id " ,
'type' => 'course' );
}
// Activity type and instance, if appropriate.
if ( is_object ( $cm )) {
if ( ! isset ( $cm -> modname )) {
debugging ( 'The field $cm->modname should be set if you call build_navigation with ' .
2007-12-20 15:52:33 +00:00
'a $cm parameter. If you get $cm using get_coursemodule_from_instance or ' .
2007-10-10 14:00:41 +00:00
'get_coursemodule_from_id, this will be done automatically.' , DEBUG_DEVELOPER );
2008-05-15 21:40:00 +00:00
if ( ! $cm -> modname = $DB -> get_field ( 'modules' , 'name' , array ( 'id' => $cm -> module ))) {
2008-05-15 03:07:21 +00:00
print_error ( 'cannotmoduletype' );
2007-10-10 14:00:41 +00:00
}
}
if ( ! isset ( $cm -> name )) {
debugging ( 'The field $cm->name should be set if you call build_navigation with ' .
2007-12-20 15:52:33 +00:00
'a $cm parameter. If you get $cm using get_coursemodule_from_instance or ' .
2007-10-10 14:00:41 +00:00
'get_coursemodule_from_id, this will be done automatically.' , DEBUG_DEVELOPER );
2008-05-15 21:40:00 +00:00
if ( ! $cm -> name = $DB -> get_field ( $cm -> modname , 'name' , array ( 'id' => $cm -> instance ))) {
2008-05-15 03:07:21 +00:00
print_error ( 'cannotmodulename' );
2007-10-10 14:00:41 +00:00
}
2007-04-16 20:44:32 +00:00
}
2007-10-10 14:00:41 +00:00
$navlinks [] = array (
'name' => get_string ( 'modulenameplural' , $cm -> modname ),
'link' => $CFG -> wwwroot . '/mod/' . $cm -> modname . '/index.php?id=' . $cm -> course ,
'type' => 'activity' );
$navlinks [] = array (
'name' => format_string ( $cm -> name ),
'link' => $CFG -> wwwroot . '/mod/' . $cm -> modname . '/view.php?id=' . $cm -> id ,
'type' => 'activityinstance' );
2007-08-24 04:22:30 +00:00
}
2007-06-19 14:44:02 +00:00
2007-08-24 04:22:30 +00:00
//Merge in extra navigation links
$navlinks = array_merge ( $navlinks , $extranavlinks );
2007-06-19 14:44:02 +00:00
2007-10-11 11:52:34 +00:00
// Work out whether we should be showing the activity (e.g. Forums) link.
// Note: build_navigation() is called from many places --
// install & upgrade for example -- where we cannot count on the
// roles infrastructure to be defined. Hence the $CFG->rolesactive check.
if ( ! isset ( $CFG -> hideactivitytypenavlink )) {
2007-11-17 20:55:44 +00:00
$CFG -> hideactivitytypenavlink = 0 ;
2007-10-11 11:52:34 +00:00
}
if ( $CFG -> hideactivitytypenavlink == 2 ) {
$hideactivitylink = true ;
} else if ( $CFG -> hideactivitytypenavlink == 1 && $CFG -> rolesactive &&
! empty ( $COURSE -> id ) && $COURSE -> id != SITEID ) {
if ( ! isset ( $COURSE -> context )) {
$COURSE -> context = get_context_instance ( CONTEXT_COURSE , $COURSE -> id );
}
$hideactivitylink = ! has_capability ( 'moodle/course:manageactivities' , $COURSE -> context );
} else {
$hideactivitylink = false ;
}
2007-08-24 04:22:30 +00:00
//Construct an unordered list from $navlinks
//Accessibility: heading hidden from visual browsers by default.
2007-10-15 10:04:41 +00:00
$navigation = get_accesshide ( get_string ( 'youarehere' , 'access' ), 'h2' ) . " <ul> \n " ;
2007-10-11 11:52:34 +00:00
$lastindex = count ( $navlinks ) - 1 ;
$i = - 1 ; // Used to count the times, so we know when we get to the last item.
$first = true ;
2007-08-24 04:22:30 +00:00
foreach ( $navlinks as $navlink ) {
2007-10-11 11:52:34 +00:00
$i ++ ;
$last = ( $i == $lastindex );
if ( ! is_array ( $navlink )) {
2007-08-24 04:22:30 +00:00
continue ;
2007-02-28 06:25:22 +00:00
}
2008-10-20 17:56:11 +00:00
if ( ! empty ( $navlink [ 'type' ]) && $navlink [ 'type' ] == 'activity' && ! $last && $hideactivitylink ) {
2007-08-24 04:22:30 +00:00
continue ;
2006-08-10 06:41:38 +00:00
}
2007-08-24 04:22:30 +00:00
$navigation .= '<li class="first">' ;
2007-10-11 11:52:34 +00:00
if ( ! $first ) {
2007-08-24 04:22:30 +00:00
$navigation .= get_separator ();
}
2007-10-11 11:52:34 +00:00
if (( ! empty ( $navlink [ 'link' ])) && ! $last ) {
2007-08-24 04:22:30 +00:00
$navigation .= " <a onclick= \" this.target=' $CFG->framename ' \" href= \" { $navlink [ 'link' ] } \" > " ;
}
$navigation .= " { $navlink [ 'name' ] } " ;
2007-10-11 11:52:34 +00:00
if (( ! empty ( $navlink [ 'link' ])) && ! $last ) {
2007-08-24 04:22:30 +00:00
$navigation .= " </a> " ;
2007-06-19 14:44:02 +00:00
}
2007-08-24 04:22:30 +00:00
$navigation .= " </li> " ;
2007-10-11 11:52:34 +00:00
$first = false ;
2006-08-10 06:41:38 +00:00
}
2007-08-24 04:22:30 +00:00
$navigation .= " </ul> " ;
return ( array ( 'newnav' => true , 'navlinks' => $navigation ));
2002-12-20 14:44:14 +00:00
}
2007-08-24 04:22:30 +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 ) {
2007-01-04 15:16:31 +00:00
$output = print_heading ( $text , '' , $size , true );
2006-08-10 06:41:38 +00:00
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
*/
2008-09-11 11:01:20 +00:00
function print_heading ( $text , $align = '' , $size = 2 , $class = 'main' , $return = false , $id = '' ) {
2008-01-07 01:54:28 +00:00
global $verbose ;
2005-02-09 08:29:58 +00:00
if ( $align ) {
2007-01-10 08:29:41 +00:00
$align = ' style="text-align:' . $align . ';"' ;
2005-02-09 08:29:58 +00:00
}
2005-03-13 12:56:36 +00:00
if ( $class ) {
$class = ' class="' . $class . '"' ;
}
2008-09-11 11:01:20 +00:00
if ( $id ) {
$id = ' id="' . $id . '"' ;
}
2008-01-07 01:54:28 +00:00
if ( ! defined ( 'CLI_UPGRADE' ) || ! CLI_UPGRADE ) {
2008-09-23 21:09:46 +00:00
$output = " <h $size $align $class $id > " . $text . " </h $size > " ;
2008-01-07 01:54:28 +00:00
} else if ( CLI_UPGRADE ) {
2008-09-23 21:09:46 +00:00
$output = $text ;
2008-01-07 01:54:28 +00:00
if ( $size == 1 ) {
$output = '=>' . $output ;
} else if ( $size == 2 ) {
$output = '-->' . $output ;
}
}
2006-08-10 06:41:38 +00:00
if ( $return ) {
return $output ;
} else {
2008-01-07 01:54:28 +00:00
if ( ! defined ( 'CLI_UPGRADE' ) || ! CLI_UPGRADE ) {
2006-08-10 06:41:38 +00:00
echo $output ;
2008-01-07 01:54:28 +00:00
} else if ( CLI_UPGRADE && ( $verbose > CLI_NO ) ) {
console_write ( STDOUT , $output , '' , false );
print_newline ();
}
2006-08-10 06:41:38 +00:00
}
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 ) {
2008-04-08 16:10:49 +00:00
$output = '<div class="heading-with-help">' ;
2008-06-09 16:53:30 +00:00
$output .= '<h2 class="main help">' . $icon . $text . '</h2>' ;
2006-08-10 06:41:38 +00:00
$output .= helpbutton ( $helppage , $text , $module , true , false , '' , true );
2008-04-08 16:10:49 +00:00
$output .= '</div>' ;
2006-08-10 06:41:38 +00:00
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: ??
2008-06-09 16:53:30 +00:00
$output = '<h2 class="headingblock header ' . $class . '">' . $heading . '</h2>' ;
2006-08-10 06:41:38 +00:00
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 = '' ;
2007-01-20 13:00:03 +00:00
if ( $link == '' ) {
if ( ! empty ( $_SERVER [ 'HTTP_REFERER' ])) {
$link = $_SERVER [ 'HTTP_REFERER' ];
2007-06-19 14:44:02 +00:00
$link = str_replace ( '&' , '&' , $link ); // make it valid XHTML
2007-01-20 13:00:03 +00:00
} else {
$link = $CFG -> wwwroot . '/' ;
}
2002-12-20 14:44:14 +00:00
}
2007-11-20 17:31:13 +00:00
2007-11-19 01:45:45 +00:00
$options = array ();
2007-11-20 17:31:13 +00:00
$linkparts = parse_url ( str_replace ( '&' , '&' , $link ));
2007-11-19 01:45:45 +00:00
if ( isset ( $linkparts [ 'query' ])) {
parse_str ( $linkparts [ 'query' ], $options );
}
2007-11-20 17:31:13 +00:00
2006-08-10 06:41:38 +00:00
$output .= '<div class="continuebutton">' ;
2007-01-22 16:03:34 +00:00
2007-11-19 01:45:45 +00:00
$output .= print_single_button ( $link , $options , get_string ( 'continue' ), 'get' , $CFG -> framename , true );
2006-08-10 06:41:38 +00:00
$output .= '</div>' . " \n " ;
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2002-12-20 14:44:14 +00:00
}
2007-01-08 03:06:32 +00:00
2004-09-23 04:36:43 +00:00
/**
* Print a message in a standard themed box .
2007-01-08 03:06:32 +00:00
* Replaces print_simple_box ( see deprecatedlib . php )
2004-09-23 04:36:43 +00:00
*
2007-01-08 03:06:32 +00:00
* @ param string $message , the content of the box
2007-01-08 07:55:45 +00:00
* @ param string $classes , space - separated class names .
2007-12-14 21:22:38 +00:00
* @ param string $idbase
2007-01-08 03:06:32 +00:00
* @ param boolean $return , return as string or just print it
2007-12-14 21:22:38 +00:00
* @ return mixed string or void
2004-09-23 04:36:43 +00:00
*/
2007-01-08 07:55:45 +00:00
function print_box ( $message , $classes = 'generalbox' , $ids = '' , $return = false ) {
2007-01-08 03:06:32 +00:00
2007-01-08 07:55:45 +00:00
$output = print_box_start ( $classes , $ids , true );
2008-06-16 11:39:21 +00:00
$output .= $message ;
2007-01-08 03:06:32 +00:00
$output .= print_box_end ( true );
2006-08-10 06:41:38 +00:00
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2002-12-20 14:44:14 +00:00
}
2007-01-20 13:00:03 +00:00
/**
2007-01-08 03:06:32 +00:00
* Starts a box using divs
* Replaces print_simple_box_start ( see deprecatedlib . php )
2006-04-06 17:21:04 +00:00
*
2007-01-08 07:55:45 +00:00
* @ param string $classes , space - separated class names .
2007-12-14 21:22:38 +00:00
* @ param string $idbase
2007-01-08 03:06:32 +00:00
* @ param boolean $return , return as string or just print it
2007-12-14 21:22:38 +00:00
* @ return mixed string or void
2004-09-23 04:36:43 +00:00
*/
2007-01-08 07:55:45 +00:00
function print_box_start ( $classes = 'generalbox' , $ids = '' , $return = false ) {
2007-11-23 16:49:51 +00:00
global $THEME ;
2006-08-10 06:41:38 +00:00
2008-01-09 15:52:08 +00:00
if ( strpos ( $classes , 'clearfix' ) !== false ) {
$clearfix = true ;
$classes = trim ( str_replace ( 'clearfix' , '' , $classes ));
} else {
$clearfix = false ;
}
2008-01-13 08:30:39 +00:00
2007-11-23 16:49:51 +00:00
if ( ! empty ( $THEME -> customcorners )) {
$classes .= ' ccbox box' ;
2006-08-10 06:41:38 +00:00
} else {
2007-11-23 16:49:51 +00:00
$classes .= ' box' ;
2006-08-10 06:41:38 +00:00
}
2002-12-20 14:44:14 +00:00
2008-01-09 15:52:08 +00:00
return print_container_start ( $clearfix , $classes , $ids , $return );
2007-11-23 16:49:51 +00:00
}
2007-01-08 03:06:32 +00:00
2007-01-20 13:00:03 +00:00
/**
2007-01-08 03:06:32 +00:00
* Simple function to end a box ( see above )
* Replaces print_simple_box_end ( see deprecatedlib . php )
*
* @ param boolean $return , return as string or just print it
2004-09-23 04:36:43 +00:00
*/
2007-01-08 03:06:32 +00:00
function print_box_end ( $return = false ) {
2007-11-23 16:49:51 +00:00
return print_container_end ( $return );
2002-12-20 14:44:14 +00:00
}
2008-11-03 05:27:10 +00:00
/**
* Print ( or return ) a collapisble region , that has a caption that can
* be clicked to expand or collapse the region . If JavaScript is off , then the region
* will always be exanded .
*
* @ param string $contents the contents of the box .
* @ param string $classes class names added to the div that is output .
* @ param string $id id added to the div that is output . Must not be blank .
* @ param string $caption text displayed at the top . Clicking on this will cause the region to expand or contract .
* @ param string $userpref the name of the user preference that stores the user ' s preferred deafault state .
* ( May be blank if you do not wish the state to be persisted .
* @ param boolean $default Inital collapsed state to use if the user_preference it not set .
* @ param boolean $return if true , return the HTML as a string , rather than printing it .
* @ return mixed if $return is false , returns nothing , otherwise returns a string of HTML .
*/
2008-11-03 05:04:23 +00:00
function print_collapsible_region ( $contents , $classes , $id , $caption , $userpref = '' , $default = false , $return = false ) {
$output = print_collapsible_region_start ( $classes , $id , $caption , $userpref , true );
$output .= $contents ;
$output .= print_collapsible_region_end ( true );
if ( $return ) {
return $output ;
} else {
echo $output ;
}
}
2008-11-03 05:27:10 +00:00
/**
* Print ( or return ) the start of a collapisble region , that has a caption that can
* be clicked to expand or collapse the region . If JavaScript is off , then the region
* will always be exanded .
*
* @ param string $classes class names added to the div that is output .
* @ param string $id id added to the div that is output . Must not be blank .
* @ param string $caption text displayed at the top . Clicking on this will cause the region to expand or contract .
* @ param string $userpref the name of the user preference that stores the user ' s preferred deafault state .
* ( May be blank if you do not wish the state to be persisted .
* @ param boolean $default Inital collapsed state to use if the user_preference it not set .
* @ param boolean $return if true , return the HTML as a string , rather than printing it .
* @ return mixed if $return is false , returns nothing , otherwise returns a string of HTML .
*/
2008-11-03 05:04:23 +00:00
function print_collapsible_region_start ( $classes , $id , $caption , $userpref = false , $default = false , $return = false ) {
global $CFG ;
// Include required JavaScript libraries.
require_js ( array ( 'yui_yahoo' , 'yui_dom-event' , 'yui_event' , 'yui_animation' ));
// Work out the initial state.
if ( is_string ( $userpref )) {
user_preference_allow_ajax_update ( $userpref , PARAM_BOOL );
$collapsed = get_user_preferences ( $userpref , $default );
} else {
$collapsed = $default ;
$userpref = false ;
}
2008-11-04 05:12:12 +00:00
if ( $collapsed ) {
$classes .= ' collapsed' ;
}
2008-11-03 05:04:23 +00:00
$output = '' ;
$output .= '<div id="' . $id . '" class="collapsibleregion ' . $classes . '">' ;
2008-11-04 05:12:12 +00:00
$output .= '<div id="' . $id . '_sizer">' ;
2008-11-03 05:04:23 +00:00
$output .= '<div id="' . $id . '_caption" class="collapsibleregioncaption">' ;
$output .= $caption . ' ' ;
2008-11-04 05:12:12 +00:00
$output .= '</div><div id="' . $id . '_inner" class="collapsibleregioninner">' ;
2008-11-03 05:04:23 +00:00
$output .= print_js_call ( 'new collapsible_region' , array ( $id , $userpref , get_string ( 'clicktohideshow' )), true );
if ( $return ) {
return $output ;
} else {
echo $output ;
}
}
2008-11-03 05:27:10 +00:00
/**
* Close a region started with print_collapsible_region_start .
*
* @ param boolean $return if true , return the HTML as a string , rather than printing it .
* @ return mixed if $return is false , returns nothing , otherwise returns a string of HTML .
*/
2008-11-03 05:04:23 +00:00
function print_collapsible_region_end ( $return = false ) {
2008-11-04 05:12:12 +00:00
$output = '</div></div></div>' ;
2008-11-03 05:04:23 +00:00
if ( $return ) {
return $output ;
} else {
echo $output ;
}
}
2007-11-16 13:33:51 +00:00
/**
2007-11-23 16:49:51 +00:00
* Print a message in a standard themed container .
2007-11-16 13:33:51 +00:00
*
2007-11-23 16:49:51 +00:00
* @ param string $message , the content of the container
2007-12-14 21:22:38 +00:00
* @ param boolean $clearfix clear both sides
2007-11-16 13:33:51 +00:00
* @ param string $classes , space - separated class names .
2007-12-14 21:22:38 +00:00
* @ param string $idbase
2007-11-16 13:33:51 +00:00
* @ param boolean $return , return as string or just print it
2007-12-14 21:22:38 +00:00
* @ return string or void
2007-11-16 13:33:51 +00:00
*/
2007-11-23 16:49:51 +00:00
function print_container ( $message , $clearfix = false , $classes = '' , $idbase = '' , $return = false ) {
2007-11-16 13:33:51 +00:00
2007-11-23 16:49:51 +00:00
$output = print_container_start ( $clearfix , $classes , $idbase , true );
2008-11-03 05:04:23 +00:00
$output .= $message ;
2007-11-23 16:49:51 +00:00
$output .= print_container_end ( true );
2007-11-16 13:33:51 +00:00
if ( $return ) {
return $output ;
} else {
echo $output ;
}
}
/**
2007-11-23 16:49:51 +00:00
* Starts a container using divs
2007-11-16 13:33:51 +00:00
*
2007-12-14 21:22:38 +00:00
* @ param boolean $clearfix clear both sides
2007-11-16 13:33:51 +00:00
* @ param string $classes , space - separated class names .
2007-12-14 21:22:38 +00:00
* @ param string $idbase
2007-11-16 13:33:51 +00:00
* @ param boolean $return , return as string or just print it
2007-12-14 21:22:38 +00:00
* @ return mixed string or void
2007-11-16 13:33:51 +00:00
*/
2007-11-23 16:49:51 +00:00
function print_container_start ( $clearfix = false , $classes = '' , $idbase = '' , $return = false ) {
global $THEME ;
2007-11-17 16:37:00 +00:00
2007-11-23 16:49:51 +00:00
if ( ! isset ( $THEME -> open_containers )) {
$THEME -> open_containers = array ();
}
$THEME -> open_containers [] = $idbase ;
2007-11-17 16:37:00 +00:00
2007-11-23 16:49:51 +00:00
if ( ! empty ( $THEME -> customcorners )) {
$output = _print_custom_corners_start ( $clearfix , $classes , $idbase );
} else {
if ( $idbase ) {
$id = ' id="' . $idbase . '"' ;
} else {
$id = '' ;
}
if ( $clearfix ) {
$clearfix = ' clearfix' ;
} else {
$clearfix = '' ;
}
if ( $classes or $clearfix ) {
$class = ' class="' . $classes . $clearfix . '"' ;
} else {
$class = '' ;
}
$output = '<div' . $id . $class . '>' ;
2007-11-16 13:33:51 +00:00
}
if ( $return ) {
return $output ;
} else {
echo $output ;
}
}
/**
2007-11-23 16:49:51 +00:00
* Simple function to end a container ( see above )
2007-11-16 13:33:51 +00:00
* @ param boolean $return , return as string or just print it
2007-12-14 21:22:38 +00:00
* @ return mixed string or void
2007-11-16 13:33:51 +00:00
*/
2007-11-23 16:49:51 +00:00
function print_container_end ( $return = false ) {
global $THEME ;
2007-11-17 16:37:00 +00:00
2007-11-23 16:49:51 +00:00
if ( empty ( $THEME -> open_containers )) {
2007-12-14 21:22:38 +00:00
debugging ( 'Incorrect request to end container - no more open containers.' , DEBUG_DEVELOPER );
2007-11-23 16:49:51 +00:00
$idbase = '' ;
} else {
$idbase = array_pop ( $THEME -> open_containers );
}
2007-11-17 16:37:00 +00:00
2007-11-16 13:33:51 +00:00
if ( ! empty ( $THEME -> customcorners )) {
2007-11-23 16:49:51 +00:00
$output = _print_custom_corners_end ( $idbase );
} else {
$output = '</div>' ;
}
2007-11-17 16:37:00 +00:00
2007-11-23 16:49:51 +00:00
if ( $return ) {
return $output ;
} else {
echo $output ;
2007-11-16 13:33:51 +00:00
}
2007-11-23 16:49:51 +00:00
}
2007-11-16 13:33:51 +00:00
2007-11-23 20:26:16 +00:00
/**
* Returns number of currently open containers
* @ return int number of open containers
*/
function open_containers () {
global $THEME ;
if ( ! isset ( $THEME -> open_containers )) {
$THEME -> open_containers = array ();
}
return count ( $THEME -> open_containers );
}
2007-11-23 16:49:51 +00:00
/**
2007-12-14 21:22:38 +00:00
* Force closing of open containers
2007-11-23 16:49:51 +00:00
* @ param boolean $return , return as string or just print it
2007-12-14 21:22:38 +00:00
* @ param int $keep number of containers to be kept open - usually theme or page containers
* @ return mixed string or void
2007-11-23 16:49:51 +00:00
*/
2007-12-14 21:22:38 +00:00
function print_container_end_all ( $return = false , $keep = 0 ) {
2007-11-23 16:49:51 +00:00
$output = '' ;
2007-12-14 21:22:38 +00:00
while ( open_containers () > $keep ) {
$output .= print_container_end ( $return );
2007-11-23 16:49:51 +00:00
}
2007-11-16 13:33:51 +00:00
if ( $return ) {
return $output ;
} else {
echo $output ;
}
}
2007-11-23 16:49:51 +00:00
/**
* Internal function - do not use directly !
* Starting part of the surrounding divs for custom corners
*
* @ param boolean $clearfix , add CLASS " clearfix " to the inner div against collapsing
2007-12-14 21:22:38 +00:00
* @ param string $classes
* @ param mixed $idbase , optionally , define one idbase to be added to all the elements in the corners
* @ return string
2007-11-23 16:49:51 +00:00
*/
function _print_custom_corners_start ( $clearfix = false , $classes = '' , $idbase = '' ) {
/// Analise if we want ids for the custom corner elements
$id = '' ;
$idbt = '' ;
$idi1 = '' ;
$idi2 = '' ;
$idi3 = '' ;
if ( $idbase ) {
$id = 'id="' . $idbase . '" ' ;
$idbt = 'id="' . $idbase . '-bt" ' ;
$idi1 = 'id="' . $idbase . '-i1" ' ;
$idi2 = 'id="' . $idbase . '-i2" ' ;
$idi3 = 'id="' . $idbase . '-i3" ' ;
}
2007-11-28 18:14:38 +00:00
/// Calculate current level
$level = open_containers ();
2007-11-23 16:49:51 +00:00
/// Output begins
2007-11-28 18:14:38 +00:00
$output = '<div ' . $id . 'class="wrap wraplevel' . $level . ' ' . $classes . '">' . " \n " ;
2007-11-23 16:49:51 +00:00
$output .= '<div ' . $idbt . 'class="bt"><div> </div></div>' ;
$output .= " \n " ;
$output .= '<div ' . $idi1 . 'class="i1"><div ' . $idi2 . 'class="i2">' ;
$output .= ( ! empty ( $clearfix )) ? '<div ' . $idi3 . 'class="i3 clearfix">' : '<div ' . $idi3 . 'class="i3">' ;
return $output ;
}
/**
* Internal function - do not use directly !
* Ending part of the surrounding divs for custom corners
2007-12-14 21:22:38 +00:00
* @ param string $idbase
* @ return string
2007-11-23 16:49:51 +00:00
*/
function _print_custom_corners_end ( $idbase ) {
/// Analise if we want ids for the custom corner elements
$idbb = '' ;
if ( $idbase ) {
$idbb = 'id="' . $idbase . '-bb" ' ;
}
/// Output begins
$output = '</div></div></div>' ;
$output .= " \n " ;
$output .= '<div ' . $idbb . 'class="bb"><div> </div></div>' . " \n " ;
$output .= '</div>' ;
return $output ;
}
2007-11-16 13:33:51 +00:00
2004-09-23 04:36:43 +00:00
/**
* Print a self contained form with a single submit button .
*
2007-12-19 13:31:03 +00:00
* @ param string $link used as the action attribute on the form , so the URL that will be hit if the button is clicked .
* @ param array $options these become hidden form fields , so these options get passed to the script at $link .
* @ param string $label the caption that appears on the button .
* @ param string $method HTTP method used on the request of the button is clicked . 'get' or 'post' .
2008-03-07 11:17:44 +00:00
* @ param string $notusedanymore no longer used .
2008-01-13 08:30:39 +00:00
* @ param boolean $return if false , output the form directly , otherwise return the HTML as a string .
2007-12-19 13:31:03 +00:00
* @ param string $tooltip a tooltip to add to the button as a title attribute .
* @ param boolean $disabled if true , the button will be disabled .
2008-01-12 08:47:29 +00:00
* @ param string $jsconfirmmessage if not empty then display a confirm dialogue with this string as the question .
2007-12-19 13:31:03 +00:00
* @ return string / nothing depending on the $return paramter .
2004-09-23 04:36:43 +00:00
*/
2008-03-07 11:17:44 +00:00
function print_single_button ( $link , $options , $label = 'OK' , $method = 'get' , $notusedanymore = '' ,
$return = false , $tooltip = '' , $disabled = false , $jsconfirmmessage = '' ) {
2006-08-10 06:41:38 +00:00
$output = '' ;
2007-01-22 18:23:14 +00:00
$link = str_replace ( '"' , '"' , $link ); //basic XSS protection
2006-08-10 06:41:38 +00:00
$output .= '<div class="singlebutton">' ;
2007-01-05 08:01:33 +00:00
// taking target out, will need to add later target="'.$target.'"
2007-06-19 14:44:02 +00:00
$output .= '<form action="' . $link . '" method="' . $method . '">' ;
2007-03-02 19:57:24 +00:00
$output .= '<div>' ;
2002-12-20 14:44:14 +00:00
if ( $options ) {
foreach ( $options as $name => $value ) {
2007-01-20 13:00:03 +00:00
$output .= '<input type="hidden" name="' . $name . '" value="' . s ( $value ) . '" />' ;
2002-12-20 14:44:14 +00:00
}
}
2007-07-24 17:17:52 +00:00
if ( $tooltip ) {
$tooltip = 'title="' . s ( $tooltip ) . '"' ;
} else {
$tooltip = '' ;
}
2007-12-19 13:31:03 +00:00
if ( $disabled ) {
$disabled = 'disabled="disabled"' ;
} else {
$disabled = '' ;
}
2008-01-12 08:47:29 +00:00
if ( $jsconfirmmessage ){
2008-01-13 08:30:39 +00:00
$jsconfirmmessage = addslashes_js ( $jsconfirmmessage );
2008-03-31 06:53:35 +00:00
$jsconfirmmessage = 'onclick="return confirm(\'' . $jsconfirmmessage . '\');" ' ;
2008-01-12 08:47:29 +00:00
}
$output .= '<input type="submit" value="' . s ( $label ) . " \" $tooltip $disabled $jsconfirmmessage /></div></form></div> " ;
2006-08-10 06:41:38 +00:00
if ( $return ) {
return $output ;
} else {
echo $output ;
}
2002-12-20 14:44:14 +00:00
}
2007-01-08 03:06:32 +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://' ) {
2007-01-05 12:58:22 +00:00
$output .= '<img style="height:' . $height . 'px;width:' . $width . 'px;" src="' . $path . '" />' ;
2002-12-20 14:44:14 +00:00
} else if ( $courseid ) {
2007-01-05 12:58:22 +00:00
$output .= '<img style="height:' . $height . 'px;width:' . $width . 'px;" src="' ;
2008-07-10 09:55:11 +00:00
require_once ( $CFG -> libdir . '/filelib.php' );
$output .= get_file_url ( " $courseid / $path " );
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 .
*
2007-09-19 07:19:35 +00:00
* If you pass a $user object that has id , picture , imagealt , firstname , lastname
2008-01-13 08:30:39 +00:00
* you save a DB query .
2007-09-19 07:19:35 +00:00
*
2008-01-13 08:30:39 +00:00
* @ param int $user takes a userid , or a userobj
2004-09-25 05:29:21 +00:00
* @ 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 ?
2006-12-30 19:45:24 +00:00
* @ param string $target link target attribute
* @ param boolean $alttext use username or userspecified text in image alt attribute
2004-09-25 05:29:21 +00:00
* return string
2004-09-23 04:36:43 +00:00
* @ todo Finish documenting this function
*/
2007-09-19 07:19:35 +00:00
function print_user_picture ( $user , $courseid , $picture = NULL , $size = 0 , $return = false , $link = true , $target = '' , $alttext = true ) {
2008-10-07 02:26:47 +00:00
global $CFG , $DB ;
2002-12-20 14:44:14 +00:00
2007-09-19 07:19:35 +00:00
$needrec = false ;
// only touch the DB if we are missing data...
if ( is_object ( $user )) {
// Note - both picture and imagealt _can_ be empty
// what we are trying to see here is if they have been fetched
// from the DB. We should use isset() _except_ that some installs
// have those fields as nullable, and isset() will return false
// on null. The only safe thing is to ask array_key_exists()
// which works on objects. property_exists() isn't quite
// what we want here...
if ( ! ( array_key_exists ( 'picture' , $user )
&& ( $alttext && array_key_exists ( 'imagealt' , $user )
|| ( isset ( $user -> firstname ) && isset ( $user -> lastname )))) ) {
$needrec = true ;
$user = $user -> id ;
}
} else {
if ( $alttext ) {
// we need firstname, lastname, imagealt, can't escape...
$needrec = true ;
} else {
2007-09-19 07:55:29 +00:00
$userobj = new StdClass ; // fake it to save DB traffic
2007-09-19 07:19:35 +00:00
$userobj -> id = $user ;
$userobj -> picture = $picture ;
2007-10-05 06:32:53 +00:00
$user = clone ( $userobj );
2007-09-19 07:19:35 +00:00
unset ( $userobj );
}
}
if ( $needrec ) {
2008-05-15 21:40:00 +00:00
$user = $DB -> get_record ( 'user' , array ( 'id' => $user ), 'id,firstname,lastname,imagealt' );
2007-09-19 07:19:35 +00:00
}
2002-12-20 14:44:14 +00:00
if ( $link ) {
2008-05-02 05:20:40 +00:00
$url = '/user/view.php?id=' . $user -> id . '&course=' . $courseid ;
2005-01-01 04:22:19 +00:00
if ( $target ) {
2008-05-02 05:20:40 +00:00
$target = 'onclick="return openpopup(\'' . $url . '\');"' ;
2005-01-01 04:22:19 +00:00
}
2008-05-02 05:20:40 +00:00
$output = '<a ' . $target . ' href="' . $CFG -> wwwroot . $url . '">' ;
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 " ;
2007-09-19 07:19:35 +00:00
if ( is_null ( $picture )) {
$picture = $user -> picture ;
}
2008-01-13 08:30:39 +00:00
2003-07-28 11:42:53 +00:00
if ( $picture ) { // Print custom user picture
2008-07-10 09:55:11 +00:00
require_once ( $CFG -> libdir . '/filelib.php' );
$src = get_file_url ( $user -> id . '/' . $file . '.jpg' , null , 'user' );
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 " ;
2007-01-04 15:42:49 +00:00
$src = " $CFG->pixpath /u/ $file .png " ;
2002-12-20 14:44:14 +00:00
}
2007-01-25 00:04:02 +00:00
$imagealt = '' ;
2007-09-19 07:19:35 +00:00
if ( $alttext ) {
2006-11-27 07:23:12 +00:00
if ( ! empty ( $user -> imagealt )) {
$imagealt = $user -> imagealt ;
} else {
$imagealt = get_string ( 'pictureof' , '' , fullname ( $user ));
2007-01-04 10:23:06 +00:00
}
2006-11-25 18:09:33 +00:00
}
2006-12-30 19:45:24 +00:00
2008-08-27 10:24:48 +00:00
$output .= " <img class= \" $class\ " src = \ " $src\ " height = \ " $size\ " width = \ " $size\ " alt = \ " " . s ( $imagealt ) . '" />' ;
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 );
2007-10-05 06:31:11 +00:00
if ( isset ( $user -> context -> id )) {
2008-12-01 19:19:29 +00:00
$usercontext = $user -> context ;
} else {
$usercontext = get_context_instance ( CONTEXT_USER , $user -> id );
2007-10-05 06:31:11 +00:00
}
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' );
2007-12-29 15:38:14 +00:00
$string -> city = get_string ( 'city' );
2004-09-22 14:39:15 +00:00
$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' );
2007-09-19 07:19:35 +00:00
$datestring -> year = get_string ( 'year' );
$datestring -> years = get_string ( 'years' );
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">' ;
2007-09-19 07:19:35 +00:00
$output .= print_user_picture ( $user , $course -> id , $user -> picture , true , true );
2006-08-10 06:41:38 +00:00
$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">' ;
2008-11-13 08:40:57 +00:00
if ( ! empty ( $user -> role )) {
2006-08-10 06:41:38 +00:00
$output .= $string -> role . ': ' . $user -> role . '<br />' ;
2004-01-10 16:41:29 +00:00
}
2006-11-12 08:55:13 +00:00
if ( $user -> maildisplay == 1 or ( $user -> maildisplay == 2 and ( $course -> id != SITEID ) and ! isguest ()) or
2006-10-01 06:18:18 +00:00
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' ]))) {
2007-12-29 15:38:14 +00:00
$output .= $string -> city . ': ' ;
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
}
2007-07-05 06:53:17 +00:00
//link to notes
2008-08-26 05:45:07 +00:00
if ( ! empty ( $CFG -> enablenotes ) and ( has_capability ( 'moodle/notes:manage' , $context ) || has_capability ( 'moodle/notes:view' , $context ))) {
2007-07-05 06:53:17 +00:00
$output .= '<a href="' . $CFG -> wwwroot . '/notes/index.php?course=' . $course -> id . '&user=' . $user -> id . '">' . get_string ( 'notes' , 'notes' ) . '</a><br />' ;
}
2007-07-12 16:36:38 +00:00
2008-12-01 19:19:29 +00:00
if ( has_capability ( 'moodle/site:viewreports' , $context ) or has_capability ( 'moodle/user:viewuseractivitiesreport' , $usercontext )) {
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
}
2008-10-06 09:29:26 +00:00
if ( has_capability ( 'moodle/role:assign' , $context ) and get_user_roles ( $context , $user -> id , false )) { // I can unassing and user has some role
2006-10-01 06:18:18 +00:00
$output .= '<a href="' . $CFG -> wwwroot . '/course/unenrol.php?id=' . $course -> id . '&user=' . $user -> id . '">' . $string -> unenrol . '</a><br />' ;
}
2009-01-02 20:32:05 +00:00
if ( $USER -> id != $user -> id && ! session_is_loggedinas () && has_capability ( 'moodle/user:loginas' , $context ) &&
2007-02-13 08:31:10 +00:00
! has_capability ( 'moodle/site:doanything' , $context , $user -> id , false )) {
2007-03-19 18:54:58 +00:00
$output .= '<a href="' . $CFG -> wwwroot . '/course/loginas.php?id=' . $course -> id . '&user=' . $user -> id . '&sesskey=' . sesskey () . '">' . $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 .
*
2007-05-09 14:15:33 +00:00
* @ param group $group A single { @ link group } object OR array of groups .
* @ param int $courseid The course ID .
* @ param boolean $large Default small picture , or large .
* @ param boolean $return If false print picture , otherwise return the output as string
* @ param boolean $link Enclose image in a link to view specified course ?
2004-09-25 05:29:21 +00:00
* @ 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
} else {
2004-09-22 14:39:15 +00:00
$file = 'f2' ;
2004-02-15 07:29:55 +00:00
}
if ( $group -> picture ) { // Print custom group picture
2008-07-10 09:55:11 +00:00
require_once ( $CFG -> libdir . '/filelib.php' );
$grouppictureurl = get_file_url ( $group -> id . '/' . $file . '.jpg' , null , 'usergroup' );
$output .= '<img class="grouppicture" src="' . $grouppictureurl . '"' .
2008-09-12 06:56:11 +00:00
' alt="' . s ( get_string ( 'group' ) . ' ' . $group -> name ) . '" title="' . s ( $group -> name ) . '"/>' ;
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 . '"' .
2007-01-05 12:58:22 +00:00
' 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 {
2007-01-05 12:58:22 +00:00
$output .= '<img src="' . $url . '" style="width: ' . $sizex . 'px; height: ' . $sizey . 'px; ' . $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 .
2007-07-06 11:29:25 +00:00
* < ul >
* < li > $table -> head - An array of heading names .
2004-09-25 05:29:21 +00:00
* < 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
2007-07-06 11:29:25 +00:00
* < li > $table -> class - class attribute to put on the table
* < li > $table -> id - id attribute to put on the table .
* < li > $table -> rowclass [] - classes to add to particular rows .
2008-01-31 22:24:45 +00:00
* < li > $table -> summary - Description of the contents for screen readers .
2008-10-30 04:20:03 +00:00
* < li > $table -> headspan can be used to make a heading span multiple columns .
2008-11-18 10:17:27 +00:00
* < li > $table -> rotateheaders - Causes the contents of the heading cells to be rotated 90 %.
2004-09-25 05:29:21 +00:00
* </ 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 ) {
2007-08-09 08:40:02 +00:00
$align [ $key ] = ' text-align:' . fix_align_rtl ( $aa ) . ';' ; // Fix for RTL languages
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 ) {
2007-01-10 08:29:41 +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 ) {
2007-01-10 08:29:41 +00:00
$wrap [ $key ] = ' white-space: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' ;
}
2008-01-16 07:39:03 +00:00
if ( ! isset ( $table -> cellpadding )) {
2004-09-22 14:39:15 +00:00
$table -> cellpadding = '5' ;
2002-12-20 14:44:14 +00:00
}
2008-01-16 07:39:03 +00:00
if ( ! isset ( $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' ;
}
2008-11-18 10:17:27 +00:00
if ( ! empty ( $table -> rotateheaders )) {
$table -> class .= ' rotateheaders' ;
} else {
$table -> rotateheaders = false ; // Makes life easier later.
}
2005-02-07 11:08:37 +00:00
$tableid = empty ( $table -> id ) ? '' : 'id="' . $table -> id . '"' ;
2007-01-07 11:20:09 +00:00
$output .= '<table width="' . $table -> width . '" ' ;
2008-01-31 22:24:45 +00:00
if ( ! empty ( $table -> summary )) {
$output .= " summary= \" $table->summary\ " " ;
}
2007-01-07 11:20:09 +00:00
$output .= " cellpadding= \" $table->cellpadding\ " cellspacing = \ " $table->cellspacing\ " class = \ " $table->class boxalign $table->tablealign\ " $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>' ;
2008-07-22 14:09:32 +00:00
$keys = array_keys ( $table -> head );
$lastkey = end ( $keys );
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
}
2008-10-30 04:20:03 +00:00
if ( isset ( $table -> headspan [ $key ]) && $table -> headspan [ $key ] > 1 ) {
$colspan = ' colspan="' . $table -> headspan [ $key ] . '"' ;
} else {
$colspan = '' ;
}
2008-06-19 18:38:27 +00:00
if ( $key == $lastkey ) {
$extraclass = ' lastcol' ;
} else {
$extraclass = '' ;
}
2008-11-18 10:17:27 +00:00
if ( $table -> rotateheaders ) {
$wrapperstart = '<span>' ;
$wrapperend = '</span>' ;
} else {
$wrapperstart = '' ;
$wrapperend = '' ;
}
2007-06-19 14:44:02 +00:00
2008-11-20 09:57:20 +00:00
$output .= '<th style="' . $align [ $key ] . $size [ $key ] .
2008-11-18 10:17:27 +00:00
';white-space:nowrap;" class="header c' . $key . $extraclass . '" scope="col"' . $colspan . '>' .
$wrapperstart . $heading . $wrapperend . '</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 ;
2008-07-22 14:09:32 +00:00
$keys = array_keys ( $table -> data );
$lastrowkey = end ( $keys );
2005-01-30 05:21:25 +00:00
foreach ( $table -> data as $key => $row ) {
$oddeven = $oddeven ? 0 : 1 ;
2007-07-06 11:29:25 +00:00
if ( ! isset ( $table -> rowclass [ $key ])) {
$table -> rowclass [ $key ] = '' ;
}
2008-06-19 18:38:27 +00:00
if ( $key == $lastrowkey ) {
$table -> rowclass [ $key ] .= ' lastrow' ;
}
2007-07-06 11:29:25 +00:00
$output .= '<tr class="r' . $oddeven . ' ' . $table -> rowclass [ $key ] . '">' . " \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
2008-07-22 14:09:32 +00:00
$keys2 = array_keys ( $row );
$lastkey = end ( $keys2 );
2004-01-26 09:48:00 +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
}
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
}
2008-06-19 18:38:27 +00:00
if ( $key == $lastkey ) {
2008-07-14 02:32:59 +00:00
$extraclass = ' lastcol' ;
2008-06-19 18:38:27 +00:00
} else {
2008-07-14 02:32:59 +00:00
$extraclass = '' ;
2008-06-19 18:38:27 +00:00
}
$output .= '<td style="' . $align [ $key ] . $size [ $key ] . $wrap [ $key ] . '" class="cell c' . $key . $extraclass . '">' . $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
2008-11-18 10:17:27 +00:00
if ( $table -> rotateheaders && can_use_rotated_text ()) {
require_js ( array ( 'yui_yahoo' , 'yui_event' , 'yui_dom' ));
require_js ( 'course/report/progress/textrotate.js' );
}
2006-06-01 05:51:45 +00:00
if ( $return ) {
return $output ;
}
echo $output ;
2002-12-20 14:44:14 +00:00
return true ;
}
2008-01-24 20:33:50 +00:00
function print_recent_activity_note ( $time , $user , $text , $link , $return = false , $viewfullnames = null ) {
static $strftimerecent = null ;
2006-08-10 06:41:38 +00:00
$output = '' ;
2008-01-24 20:33:50 +00:00
if ( is_null ( $viewfullnames )) {
2008-05-01 06:07:24 +00:00
$context = get_context_instance ( CONTEXT_SYSTEM );
2008-01-24 20:33:50 +00:00
$viewfullnames = has_capability ( 'moodle/site:viewfullnames' , $context );
}
2006-09-24 14:35:48 +00:00
2008-01-24 20:33:50 +00:00
if ( is_null ( $strftimerecent )) {
2005-03-10 13:30:57 +00:00
$strftimerecent = get_string ( 'strftimerecent' );
}
2006-08-10 06:41:38 +00:00
$output .= '<div class="head">' ;
2008-01-24 20:33:50 +00:00
$output .= '<div class="date">' . userdate ( $time , $strftimerecent ) . '</div>' ;
$output .= '<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 .
*
2008-07-14 02:32:59 +00:00
* When using this function , you should
2008-06-11 08:37:26 +00:00
*
2004-09-25 05:29:21 +00:00
* @ uses $CFG
2008-06-11 08:37:26 +00:00
* @ param bool $usehtmleditor Enables the use of the htmleditor for this field .
* @ param int $rows Number of rows to display ( minimum of 10 when $height is non - null )
* @ param int $cols Number of columns to display ( minimum of 65 when $width is non - null )
* @ param null $width ( Deprecated ) Width of the element ; if a value is passed , the minimum value for $cols will be 65. Value is otherwise ignored .
* @ param null $height ( Deprecated ) Height of the element ; if a value is passe , the minimum value for $rows will be 10. Value is otherwise ignored .
* @ param string $name Name to use for the textarea element .
* @ param string $value Initial content to display in the textarea .
2008-08-29 09:54:44 +00:00
* @ param int $obsolete deprecated
2008-06-11 08:37:26 +00:00
* @ param bool $return If false , will output string . If true , will return string value .
* @ param string $id CSS ID to add to the textarea element .
2008-07-18 04:24:53 +00:00
* @ param string $editorclass CSS classes to add to the textarea element when using the htmleditor . Use 'form-textarea-simple' to get a basic editor . Defaults to 'form-textarea-advanced' ( complete editor ) . If this is null or invalid , the htmleditor will not show for this field .
2008-06-11 08:37:26 +00:00
*/
2008-08-29 09:54:44 +00:00
function print_textarea ( $usehtmleditor , $rows , $cols , $width , $height , $name , $value = '' , $obsolete = 0 , $return = false , $id = '' , $editorclass = 'form-textarea-advanced' ) {
/// $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
2008-07-15 09:43:14 +00:00
global $CFG , $COURSE , $HTTPSPAGEREQUIRED , $THEME ;
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 = '' ;
2007-01-04 10:23:06 +00:00
2006-12-12 22:06:37 +00:00
if ( $id === '' ) {
$id = 'edit-' . $name ;
}
2007-01-04 10:23:06 +00:00
2008-08-29 09:54:44 +00:00
if ( empty ( $CFG -> editorsrc ) && $usehtmleditor ) { // for backward compatibility.
if ( $height && ( $rows < $minrows )) {
$rows = $minrows ;
2006-03-04 16:04:48 +00:00
}
2008-08-29 09:54:44 +00:00
if ( $width && ( $cols < $mincols )) {
$cols = $mincols ;
2003-10-29 08:06:11 +00:00
}
2002-12-20 14:44:14 +00:00
}
2008-07-15 09:43:14 +00:00
if ( $usehtmleditor ) {
$THEME -> htmleditors [] = $id ;
2008-07-18 04:24:53 +00:00
} else {
$editorclass = '' ;
2008-07-15 09:43:14 +00:00
}
2008-07-18 04:24:53 +00:00
$str .= " \n " . '<textarea class="form-textarea ' . $editorclass . '" id="' . $id . '" name="' . $name . '" rows="' . $rows . '" cols="' . $cols . '">' . " \n " ;
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 " ;
2007-01-04 10:23:06 +00:00
2007-05-08 15:07:25 +00:00
if ( $usehtmleditor ) {
2008-07-31 03:13:49 +00:00
require_once ( " $CFG->dirroot /repository/lib.php " );
2008-07-30 06:26:26 +00:00
$str_toggle = '<span class="helplink"><a href="javascript:mce_toggleEditor(\'' . $id . '\');"><img width="50" height="17" src="' . $CFG -> httpswwwroot . '/lib/editor/tinymce/images/toggle.gif" alt="' . get_string ( 'editortoggle' ) . '" title="' . get_string ( 'editortoggle' ) . '" class="icontoggle" /></a></span>' ;
2007-04-24 11:59:17 +00:00
// Show shortcuts button if HTML editor is in use, but only if JavaScript is enabled (MDL-9556)
2008-08-07 03:33:47 +00:00
if ( empty ( $COURSE -> context )) {
$ctx = get_context_instance ( CONTEXT_SYSTEM );
} else {
$ctx = $COURSE -> context ;
}
2008-12-09 06:50:06 +00:00
$ret = repository_get_client ( $ctx , array ( 'image' , 'video' , 'media' ), '*' );
2008-08-20 02:52:29 +00:00
$str .= $ret [ 'css' ] . $ret [ 'js' ];
2008-08-05 05:12:30 +00:00
$suffix = $ret [ 'suffix' ];
2008-07-15 09:00:23 +00:00
$str .= '<div class="textareaicons">' ;
2007-09-15 19:36:31 +00:00
$str .= ' < script type = " text/javascript " >
//<![CDATA[
2008-08-05 08:12:21 +00:00
id2suffix [ \ '' . $id . '\']=\'' . $suffix . ' \ ' ;
2008-07-30 06:26:26 +00:00
mce_saveOnSubmit ( \ '' . addslashes_js ( $id ) . ' \ ' );
2008-07-15 09:00:23 +00:00
document . write ( \ '' . addslashes_js ( $str_toggle ) . ' \ ' );
2007-09-15 19:36:31 +00:00
document . write ( \ '' . addslashes_js ( editorshortcutshelpbutton ()) . ' \ ' );
//]]>
</ script > ' ;
2008-07-15 09:00:23 +00:00
$str .= '</div>' ;
2007-05-08 15:07:25 +00:00
}
2006-11-30 08:30:46 +00:00
2006-03-15 05:38:02 +00:00
if ( $return ) {
return $str ;
}
echo $str ;
2002-12-20 14:44:14 +00:00
}
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
*
2007-09-19 06:52:52 +00:00
* Note that the caller is responsible for capchecks .
*
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 ) {
global $CFG , $USER ;
2007-09-19 06:52:52 +00:00
if ( ! empty ( $USER -> editing )) {
$string = get_string ( 'turneditingoff' );
$edit = '0' ;
} else {
$string = get_string ( 'turneditingon' );
$edit = '1' ;
2005-08-15 12:53:57 +00:00
}
2007-09-19 06:52:52 +00:00
return '<form ' . $CFG -> frametarget . ' method="get" action="' . $CFG -> wwwroot . '/course/view.php">' .
'<div>' .
'<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 . '" />' .
'</div></form>' ;
2005-08-15 12:53:57 +00:00
}
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 '' ;
}
2007-10-08 05:11:15 +00:00
if ( ! empty ( $USER -> access [ 'rsw' ][ $context -> path ])){ // Just a button to return to normal
2006-09-21 14:04:32 +00:00
$options = array ();
$options [ 'id' ] = $courseid ;
$options [ 'sesskey' ] = sesskey ();
$options [ 'switchrole' ] = 0 ;
2006-09-21 09:16:41 +00:00
2007-01-04 10:23:06 +00:00
return print_single_button ( $CFG -> wwwroot . '/course/view.php' , $options ,
2006-09-21 14:04:32 +00:00
get_string ( 'switchrolereturn' ), 'post' , '_self' , true );
}
2007-03-16 20:21:27 +00:00
if ( has_capability ( 'moodle/role:switchroles' , $context )) {
2008-02-15 16:40:45 +00:00
if ( ! $roles = get_assignable_roles_for_switchrole ( $context )) {
2007-03-16 20:21:27 +00:00
return '' ; // Nothing to show!
}
// unset default user role - it would not work
unset ( $roles [ $CFG -> guestroleid ]);
return popup_form ( $CFG -> wwwroot . '/course/view.php?id=' . $courseid . '&sesskey=' . sesskey () . '&switchrole=' ,
$roles , 'switchrole' , '' , get_string ( 'switchroleto' ), 'switchrole' , get_string ( 'switchroleto' ), true );
}
return '' ;
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
}
2007-01-03 19:24:48 +00:00
2007-01-04 18:23:55 +00:00
return " <form $CFG->frametarget method= \" get \" action= \" $CFG->wwwroot /my/index.php \" > " .
2007-03-01 06:06:16 +00:00
" <div> " .
2007-01-10 08:35:45 +00:00
" <input type= \" hidden \" name= \" edit \" value= \" $edit\ " /> " .
2007-03-01 06:06:16 +00:00
" <input type= \" submit \" value= \" $string\ " /></ div ></ form > " ;
2005-08-16 00:25:39 +00:00
}
2007-08-03 21:41:09 +00:00
/**
* Returns a turn edit on / off button for tag in a self contained form .
*
* @ uses $CFG
* @ uses $USER
* @ return string
*/
function update_tag_button ( $tagid ) {
global $CFG , $USER ;
if ( ! empty ( $USER -> editing )) {
$string = get_string ( 'turneditingoff' );
$edit = '0' ;
} else {
$string = get_string ( 'turneditingon' );
$edit = '1' ;
}
return " <form $CFG->frametarget method= \" get \" action= \" $CFG->wwwroot /tag/index.php \" > " .
" <div> " .
" <input type= \" hidden \" name= \" edit \" value= \" $edit\ " /> " .
" <input type= \" hidden \" name= \" id \" value= \" $tagid\ " /> " .
" <input type= \" submit \" value= \" $string\ " /></ div ></ 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 );
2007-01-03 19:24:48 +00:00
2007-01-07 13:49:31 +00:00
return " <form $CFG->frametarget method= \" get \" action= \" $CFG->wwwroot /course/mod.php \" onsubmit= \" this.target=' { $CFG -> framename } '; return true \" > " .// hack to allow edit on framed resources
2007-03-01 06:06:16 +00:00
" <div> " .
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 () . " \" /> " .
2007-03-01 06:06:16 +00:00
" <input type= \" submit \" value= \" $string\ " /></ div ></ 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
}
}
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
2008-12-09 20:30:02 +00:00
if ( has_capability ( 'moodle/category:manage' , get_context_instance ( CONTEXT_SYSTEM ))) {
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 " ;
}
2007-01-03 19:24:48 +00:00
2007-01-04 18:23:55 +00:00
return " <form $CFG->frametarget method= \" get \" action= \" $CFG->wwwroot /course/search.php \" > " .
2007-03-01 06:06:16 +00:00
'<div>' .
2005-09-01 04:48:22 +00:00
" <input type= \" hidden \" name= \" edit \" value= \" $edit\ " /> " .
2009-01-02 10:51:26 +00:00
" <input type= \" hidden \" name= \" sesskey \" value= \" " . 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\ " /> " .
2007-03-01 06:06:16 +00:00
" <input type= \" submit \" value= \" " . s ( $string ) . " \" /></div></form> " ;
2005-09-01 04:48:22 +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
2008-05-15 21:40:00 +00:00
global $CFG , $THEME , $USER , $DB ;
2005-03-29 14:22:13 +00:00
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
2008-01-24 20:33:50 +00:00
$modinfo = get_fast_modinfo ( $course );
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 ();
2007-05-16 08:03:37 +00:00
$menustyle = array ();
2003-11-25 04:19:58 +00:00
2008-05-15 21:40:00 +00:00
$sections = $DB -> get_records ( 'course_sections' , array ( '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
}
2008-01-24 20:33:50 +00:00
foreach ( $modinfo -> cms as $mod ) {
if ( $mod -> modname == 'label' ) {
2003-09-15 11:59:15 +00:00
continue ;
}
2004-04-27 16:29:21 +00:00
2008-02-01 14:10:49 +00:00
if ( $mod -> sectionnum > $course -> numsections ) { /// Don't show excess hidden sections
2004-11-21 06:28:17 +00:00
break ;
}
2008-01-24 20:33:50 +00:00
if ( ! $mod -> uservisible ) { // do not icnlude empty sections at all
2007-08-21 03:18:19 +00:00
continue ;
}
2004-11-21 06:28:17 +00:00
2008-02-01 14:10:49 +00:00
if ( $mod -> sectionnum > 0 and $section != $mod -> sectionnum ) {
$thissection = $sections [ $mod -> sectionnum ];
2004-07-21 06:06:10 +00:00
2007-01-04 10:23:06 +00:00
if ( $thissection -> visible or ! $course -> hiddensections or
2006-10-02 14:57:00 +00:00
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 )) {
2008-02-01 14:10:49 +00:00
$menu [] = '--' . $strsection . " " . $mod -> sectionnum ;
2004-07-21 06:06:10 +00:00
} else {
2005-03-29 14:22:13 +00:00
if ( strlen ( $thissection -> summary ) < ( $width - 3 )) {
2007-01-02 10:27:59 +00:00
$menu [] = '--' . $thissection -> summary ;
2004-07-21 06:06:10 +00:00
} else {
2007-01-02 10:27:59 +00:00
$menu [] = '--' . substr ( $thissection -> summary , 0 , $width ) . '...' ;
2004-07-21 06:06:10 +00:00
}
}
2008-02-01 14:10:49 +00:00
$section = $mod -> sectionnum ;
2008-01-24 20:33:50 +00:00
} else {
// no activities from this hidden section shown
continue ;
2004-04-27 16:29:21 +00:00
}
2002-12-20 14:44:14 +00:00
}
2004-04-27 16:29:21 +00:00
2008-01-24 20:33:50 +00:00
$url = $mod -> modname . '/view.php?id=' . $mod -> id ;
if ( $flag ) { // the current mod is the "next" mod
$nextmod = $mod ;
$flag = false ;
}
2008-04-23 17:09:03 +00:00
$localname = $mod -> name ;
2008-01-24 20:33:50 +00:00
if ( $cm == $mod -> id ) {
$selected = $url ;
$selectmod = $mod ;
$backmod = $previousmod ;
$flag = true ; // set flag so we know to use next mod for "next"
2008-04-23 17:09:03 +00:00
$localname = $strjumpto ;
2008-01-24 20:33:50 +00:00
$strjumpto = '' ;
} else {
2008-04-23 17:09:03 +00:00
$localname = strip_tags ( format_string ( $localname , true ));
$tl = textlib_get_instance ();
if ( $tl -> strlen ( $localname ) > ( $width + 5 )) {
$localname = $tl -> substr ( $localname , 0 , $width ) . '...' ;
2003-04-23 17:25:20 +00:00
}
2008-01-24 20:33:50 +00:00
if ( ! $mod -> visible ) {
2008-04-23 17:09:03 +00:00
$localname = '(' . $localname . ')' ;
2007-05-16 08:03:37 +00:00
}
2002-12-20 14:44:14 +00:00
}
2008-04-23 17:09:03 +00:00
$menu [ $url ] = $localname ;
2008-01-24 20:33:50 +00:00
if ( empty ( $THEME -> navmenuiconshide )) {
$menustyle [ $url ] = 'style="background-image: url(' . $CFG -> modpixpath . '/' . $mod -> modname . '/icon.gif);"' ; // Unfortunately necessary to do this here
}
$previousmod = $mod ;
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.
2007-01-03 19:24:48 +00:00
2008-11-29 14:22:10 +00:00
if ( $selectmod and has_capability ( 'coursereport/log:view' , $context )) {
2006-09-19 11:36:21 +00:00
$logstext = get_string ( 'alllogs' );
2007-06-28 19:18:11 +00:00
$logslink = '<li>' . " \n " . '<a title="' . $logstext . '" ' .
$CFG -> frametarget . 'onclick="this.target=\'' . $CFG -> framename . '\';"' . ' href="' .
2006-10-02 14:57:00 +00:00
$CFG -> wwwroot . '/course/report/log/index.php?chooselog=1&user=0&date=0&id=' .
2008-01-24 20:33:50 +00:00
$course -> id . '&modid=' . $selectmod -> id . '">' .
2007-03-23 08:01:01 +00:00
'<img class="icon log" src="' . $CFG -> pixpath . '/i/log.gif" alt="' . $logstext . '" /></a>' . " \n " . '</li>' ;
2004-05-21 13:07:11 +00:00
2004-01-15 07:17:47 +00:00
}
2007-01-04 10:23:06 +00:00
if ( $backmod ) {
2006-09-19 11:36:21 +00:00
$backtext = get_string ( 'activityprev' , 'access' );
2008-01-24 20:33:50 +00:00
$backmod = '<li><form action="' . $CFG -> wwwroot . '/mod/' . $backmod -> modname . '/view.php" ' .
2007-06-28 19:18:11 +00:00
'onclick="this.target=\'' . $CFG -> framename . '\';"' . '><fieldset class="invisiblefieldset">' .
2008-01-24 20:33:50 +00:00
'<input type="hidden" name="id" value="' . $backmod -> id . '" />' .
2007-05-14 13:28:21 +00:00
'<button type="submit" title="' . $backtext . '">' . link_arrow_left ( $backtext , $url = '' , $accesshide = true ) .
'</button></fieldset></form></li>' ;
2003-11-25 04:19:58 +00:00
}
2007-01-04 10:23:06 +00:00
if ( $nextmod ) {
2006-09-19 11:36:21 +00:00
$nexttext = get_string ( 'activitynext' , 'access' );
2008-01-24 20:33:50 +00:00
$nextmod = '<li><form action="' . $CFG -> wwwroot . '/mod/' . $nextmod -> modname . '/view.php" ' .
2007-06-28 19:18:11 +00:00
'onclick="this.target=\'' . $CFG -> framename . '\';"' . '><fieldset class="invisiblefieldset">' .
2008-01-24 20:33:50 +00:00
'<input type="hidden" name="id" value="' . $nextmod -> id . '" />' .
2007-05-14 13:28:21 +00:00
'<button type="submit" title="' . $nexttext . '">' . link_arrow_right ( $nexttext , $url = '' , $accesshide = true ) .
'</button></fieldset></form></li>' ;
2003-11-25 04:19:58 +00:00
}
2005-03-29 14:22:13 +00:00
2007-03-23 08:01:01 +00:00
return '<div class="navigation">' . " \n " . '<ul>' . $logslink . $backmod .
2007-01-06 20:18:44 +00:00
'<li>' . popup_form ( $CFG -> wwwroot . '/mod/' , $menu , 'navmenupopup' , $selected , $strjumpto ,
2007-05-16 08:03:37 +00:00
'' , '' , true , $targetwindow , '' , $menustyle ) . '</li>' .
2007-03-23 08:01:01 +00:00
$nextmod . '</ul>' . " \n " . '</div>' ;
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 ;
$url = '' ;
$menu = array ();
2008-01-24 20:33:50 +00:00
$doneheading = false ;
2005-03-29 13:37:08 +00:00
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>' ;
2008-01-24 20:33:50 +00:00
foreach ( $modinfo -> cms as $mod ) {
if ( $mod -> modname == 'label' ) {
2005-03-29 13:37:08 +00:00
continue ;
}
2008-02-01 14:10:49 +00:00
if ( $mod -> sectionnum > $course -> numsections ) { /// Don't show excess hidden sections
2005-03-29 13:37:08 +00:00
break ;
}
2008-01-24 20:33:50 +00:00
if ( ! $mod -> uservisible ) { // do not icnlude empty sections at all
continue ;
}
2008-02-01 14:10:49 +00:00
if ( $mod -> sectionnum >= 0 and $section != $mod -> sectionnum ) {
$thissection = $sections [ $mod -> sectionnum ];
2005-03-29 13:37:08 +00:00
2007-01-04 10:23:06 +00:00
if ( $thissection -> visible or ! $course -> hiddensections or
2006-10-02 14:57:00 +00:00
has_capability ( 'moodle/course:viewhiddensections' , $coursecontext )) {
2005-03-29 13:37:08 +00:00
$thissection -> summary = strip_tags ( format_string ( $thissection -> summary , true ));
2008-01-24 20:33:50 +00:00
if ( ! $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 )) {
2008-02-01 14:10:49 +00:00
$item = $strsection . " " . $mod -> sectionnum ;
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 ;
2008-01-24 20:33:50 +00:00
2008-02-01 14:10:49 +00:00
$section = $mod -> sectionnum ;
2008-01-24 20:33:50 +00:00
} else {
// no activities from this hidden section shown
continue ;
2005-03-29 13:37:08 +00:00
}
}
2008-01-24 20:33:50 +00:00
$url = $mod -> modname . '/view.php?id=' . $mod -> id ;
$mod -> name = strip_tags ( format_string ( urldecode ( $mod -> name ), true ));
if ( strlen ( $mod -> name ) > ( $width + 5 )) {
$mod -> name = substr ( $mod -> name , 0 , $width ) . '...' ;
2005-03-29 13:37:08 +00:00
}
2008-01-24 20:33:50 +00:00
if ( ! $mod -> visible ) {
$mod -> name = '(' . $mod -> name . ')' ;
}
$class = 'activity ' . $mod -> modname ;
$class .= ( $cmid == $mod -> cm ) ? ' selected' : '' ;
$menu [] = '<li class="' . $class . '">' .
'<img src="' . $CFG -> modpixpath . '/' . $mod -> modname . '/icon.gif" alt="" />' .
'<a href="' . $CFG -> wwwroot . '/mod/' . $url . '">' . $mod -> name . '</a></li>' ;
2005-03-29 13:37:08 +00:00
}
2008-01-24 20:33:50 +00:00
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
2007-01-04 10:23:06 +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 ++ ) {
2007-04-12 00:41:31 +00:00
$months [ $i ] = userdate ( gmmktime ( 12 , 0 , 0 , $i , 15 , 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
2007-01-04 10:23:06 +00:00
* @ 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
2007-01-04 10:23:06 +00:00
* @ 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
2007-01-08 20:30:38 +00:00
$linkobject = '<span class="helplink"><img class="iconhelp" 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
2007-01-08 20:30:38 +00:00
$linkobject = '<span class="helplink"><img class="iconhelp" 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
2007-01-08 20:30:38 +00:00
$linkobject = '<span class="helplink"><img class="iconhelp" 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
}
2005-03-18 12:10:00 +00:00
/**
* Print an error page displaying an error message . New method - use this for new code .
*
2006-04-12 09:32:19 +00:00
* @ param string $errorcode The name of the string from error . php to print
2008-07-14 02:32:59 +00:00
* @ param string $module name of module
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
2008-05-19 18:02:33 +00:00
* @ return terminates script , does not return !
2005-03-18 12:10:00 +00:00
*/
2008-09-02 06:03:37 +00:00
function print_error ( $errorcode , $module = 'error' , $link = '' , $a = NULL ) {
2008-06-13 17:51:34 +00:00
global $CFG , $UNITTEST ;
// If unittest running, throw exception instead
if ( ! empty ( $UNITTEST -> running )) {
// Errors in unit test become exceptions, so you can unit test
// code that might call error().
throw new moodle_exception ( $errorcode , $module , $link , $a );
}
2006-04-13 05:42:47 +00:00
if ( empty ( $module ) || $module == 'moodle' || $module == 'core' ) {
$module = 'error' ;
2008-06-13 17:51:34 +00:00
}
2008-08-16 12:16:01 +00:00
if ( ! isset ( $CFG -> theme ) or ! isset ( $CFG -> stylesheets )) {
2008-06-13 17:51:34 +00:00
// error found before setup.php finished
2008-09-02 06:03:37 +00:00
_print_early_error ( $errorcode , $module , $a );
2008-06-13 17:51:34 +00:00
} else {
2008-09-02 06:03:37 +00:00
_print_normal_error ( $errorcode , $module , $a , $link , debug_backtrace ());
2008-06-13 17:51:34 +00:00
}
}
/**
* Internal function - do not use directly !!
*/
2008-09-02 06:03:37 +00:00
function _print_normal_error ( $errorcode , $module , $a , $link , $backtrace , $debuginfo = null , $showerrordebugwarning = false ) {
2008-06-22 23:35:52 +00:00
global $CFG , $SESSION , $THEME , $DB ;
if ( $DB ) {
//if you enable db debugging and exception is thrown, the print footer prints a lot of rubbish
$DB -> set_debug ( 0 );
}
2008-06-13 17:51:34 +00:00
2008-09-01 14:54:57 +00:00
if ( $module === 'error' ) {
2006-04-13 05:42:47 +00:00
$modulelink = 'moodle' ;
} else {
$modulelink = $module ;
}
2008-09-02 06:03:37 +00:00
$message = get_string ( $errorcode , $module , $a );
2008-09-01 14:54:57 +00:00
if ( $module === 'error' and strpos ( $message , '[[' ) === 0 ) {
//search in moodle file if error specified - needed for backwards compatibility
2008-09-02 06:03:37 +00:00
$message = get_string ( $errorcode , 'moodle' , $a );
2008-09-01 14:54:57 +00:00
}
2008-05-19 18:02:33 +00:00
2008-06-13 17:51:34 +00:00
if ( defined ( 'FULLME' ) && FULLME == 'cron' ) {
// Errors in cron should be mtrace'd.
mtrace ( $message );
die ;
2008-05-19 18:02:33 +00:00
}
2008-04-01 06:19:10 +00:00
if ( empty ( $link ) and ! defined ( 'ADMIN_EXT_HEADER_PRINTED' )) {
if ( ! empty ( $SESSION -> fromurl ) ) {
$link = $SESSION -> fromurl ;
unset ( $SESSION -> fromurl );
} else {
$link = $CFG -> wwwroot . '/' ;
}
}
2006-04-13 05:42:47 +00:00
if ( ! empty ( $CFG -> errordocroot )) {
$errordocroot = $CFG -> errordocroot ;
} else if ( ! empty ( $CFG -> docroot )) {
$errordocroot = $CFG -> docroot ;
} else {
$errordocroot = 'http://docs.moodle.org' ;
}
2008-04-01 06:19:10 +00:00
if ( ! defined ( 'HEADER_PRINTED' )) {
//header not yet printed
@ header ( 'HTTP/1.0 404 Not Found' );
print_header ( get_string ( 'error' ));
} else {
print_container_end_all ( false , $THEME -> open_header_containers );
}
echo '<br />' ;
2008-06-13 17:51:34 +00:00
$message = clean_text ( '<p class="errormessage">' . $message . '</p>' .
'<p class="errorcode">' .
'<a href="' . $errordocroot . '/en/error/' . $modulelink . '/' . $errorcode . '">' .
get_string ( 'moreinformation' ) . '</a></p>' );
2008-04-01 06:19:10 +00:00
print_simple_box ( $message , '' , '' , '' , '' , 'errorbox' );
2008-06-13 17:51:34 +00:00
if ( $showerrordebugwarning ) {
debugging ( 'error() is a deprecated function, please call print_error() instead of error()' , DEBUG_DEVELOPER );
} else {
if ( debugging ( '' , DEBUG_DEVELOPER )) {
2008-06-22 16:51:55 +00:00
if ( $debuginfo ) {
debugging ( $debuginfo , DEBUG_DEVELOPER , $backtrace );
} else {
notify ( 'Stack trace:' . print_backtrace ( $backtrace , true ), 'notifytiny' );
}
2008-06-13 17:51:34 +00:00
}
}
2008-04-01 06:19:10 +00:00
// in case we are logging upgrade in admin/index.php stop it
if ( function_exists ( 'upgrade_log_finish' )) {
upgrade_log_finish ();
}
if ( ! empty ( $link )) {
print_continue ( $link );
}
print_footer ();
for ( $i = 0 ; $i < 512 ; $i ++ ) { // Padding to help IE work with 404
echo ' ' ;
}
die ;
2005-03-18 12:10:00 +00:00
}
2008-02-27 02:52:28 +00:00
2008-05-19 18:02:33 +00:00
/**
2008-06-13 17:51:34 +00:00
* Internal function - do not use directly !!
2008-05-19 18:02:33 +00:00
* This function is used if fatal error occures before the themes are fully initialised ( eg . in lib / setup . php )
*/
2008-11-22 01:16:52 +00:00
function _print_early_error ( $errorcode , $module , $a , $backtrace = null , $debuginfo = null ) {
2008-09-02 06:03:37 +00:00
$message = get_string ( $errorcode , $module , $a );
2008-09-01 14:54:57 +00:00
if ( $module === 'error' and strpos ( $message , '[[' ) === 0 ) {
//search in moodle file if error specified - needed for backwards compatibility
2008-09-02 06:03:37 +00:00
$message = get_string ( $errorcode , 'moodle' , $a );
2008-09-01 14:54:57 +00:00
}
$message = clean_text ( $message );
2008-06-13 17:51:34 +00:00
2008-05-19 18:02:33 +00:00
// In the name of protocol correctness, monitoring and performance
// profiling, set the appropriate error headers for machine comsumption
if ( isset ( $_SERVER [ 'SERVER_PROTOCOL' ])) {
// Avoid it with cron.php. Note that we assume it's HTTP/1.x
@ header ( $_SERVER [ 'SERVER_PROTOCOL' ] . ' 503 Service Unavailable' );
}
// better disable any caching
@ header ( 'Content-Type: text/html; charset=utf-8' );
@ header ( 'Cache-Control: no-store, no-cache, must-revalidate' );
@ header ( 'Cache-Control: post-check=0, pre-check=0' , false );
@ header ( 'Pragma: no-cache' );
@ header ( 'Expires: Mon, 20 Aug 1969 09:23:00 GMT' );
@ header ( 'Last-Modified: ' . gmdate ( 'D, d M Y H:i:s' ) . ' GMT' );
echo ' <! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Strict//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml " '.get_html_lang().' >
< head >
< meta http - equiv = " Content-Type " content = " text/html; charset=utf-8 " />
< title > '.get_string(' error ').' </ title >
</ head >< body >
< div style = " margin-top: 6em; margin-left:auto; margin-right:auto; color:#990000; text-align:center; font-size:large; border-width:1px;
border - color : black ; background - color : #ffffee; border-style:solid; border-radius: 20px; border-collapse: collapse;
width : 80 % ; - moz - border - radius : 20 px ; padding : 15 px " >
2008-06-13 17:51:34 +00:00
'.$message.'
2008-11-22 01:16:52 +00:00
</ div > ' ;
if ( debugging ( '' , DEBUG_DEVELOPER )) {
if ( $debuginfo ) {
debugging ( $debuginfo , DEBUG_DEVELOPER , $backtrace );
} else {
notify ( 'Stack trace:' . print_backtrace ( $backtrace , true ), 'notifytiny' );
}
}
echo '</body></html>' ;
2008-05-19 18:02:33 +00:00
die ;
}
2008-02-27 02:52:28 +00:00
/**
* Print an error to STDOUT and exit with a non - zero code . For commandline scripts .
* Default errorcode is 1.
*
* Very useful for perl - like error - handling :
2008-05-21 14:59:33 +00:00
*
2008-02-27 02:52:28 +00:00
* do_somethting () or mdie ( " Something went wrong " );
*
* @ param string $msg Error message
2008-05-21 14:59:33 +00:00
* @ param integer $errorcode Error code to emit
2008-02-27 02:52:28 +00:00
*/
function mdie ( $msg = '' , $errorcode = 1 ) {
trigger_error ( $msg );
exit ( $errorcode );
}
2007-01-05 04:51:46 +00:00
/**
* Returns a string of html with an image of a help icon linked to a help page on a number of help topics .
* Should be used only with htmleditor or textarea .
* @ param mixed $helptopics variable amount of params accepted . Each param may be a string or an array of arguments for
* helpbutton .
* @ return string
*/
2007-01-04 10:23:06 +00:00
function editorhelpbutton (){
global $CFG , $SESSION ;
$items = func_get_args ();
$i = 1 ;
$urlparams = array ();
$titles = array ();
foreach ( $items as $item ){
if ( is_array ( $item )){
$urlparams [] = " keyword $i = " . urlencode ( $item [ 0 ]);
$urlparams [] = " title $i = " . urlencode ( $item [ 1 ]);
if ( isset ( $item [ 2 ])){
$urlparams [] = " module $i = " . urlencode ( $item [ 2 ]);
}
$titles [] = trim ( $item [ 1 ], " . \t " );
2008-09-25 10:07:11 +00:00
} else if ( is_string ( $item )) {
2007-01-04 10:23:06 +00:00
$urlparams [] = " button $i = " . urlencode ( $item );
2008-09-25 10:07:11 +00:00
switch ( $item ) {
2007-01-04 10:23:06 +00:00
case 'reading' :
$titles [] = get_string ( " helpreading " );
break ;
case 'writing' :
$titles [] = get_string ( " helpwriting " );
break ;
case 'questions' :
$titles [] = get_string ( " helpquestions " );
break ;
2008-09-25 10:07:11 +00:00
case 'emoticons2' :
2007-01-04 10:23:06 +00:00
$titles [] = get_string ( " helpemoticons " );
break ;
2008-09-25 10:07:11 +00:00
case 'richtext2' :
2007-01-05 04:51:46 +00:00
$titles [] = get_string ( 'helprichtext' );
break ;
2008-09-25 10:07:11 +00:00
case 'text2' :
2007-01-05 04:51:46 +00:00
$titles [] = get_string ( 'helptext' );
2007-01-04 10:23:06 +00:00
break ;
default :
2008-05-15 03:07:21 +00:00
print_error ( 'unknownhelp' , '' , '' , $item );
2007-01-04 10:23:06 +00:00
}
}
$i ++ ;
}
if ( count ( $titles ) > 1 ){
//join last two items with an 'and'
$a = new object ();
$a -> one = $titles [ count ( $titles ) - 2 ];
$a -> two = $titles [ count ( $titles ) - 1 ];
$titles [ count ( $titles ) - 2 ] = get_string ( 'and' , '' , $a );
unset ( $titles [ count ( $titles ) - 1 ]);
}
$alttag = join ( ', ' , $titles );
2005-03-18 12:10:00 +00:00
2007-01-04 10:23:06 +00:00
$paramstring = join ( '&' , $urlparams );
2007-01-08 20:30:38 +00:00
$linkobject = '<img alt="' . $alttag . '" class="iconhelp" src="' . $CFG -> pixpath . '/help.gif" />' ;
2008-05-13 05:36:25 +00:00
return link_to_popup_window ( s ( '/lib/form/editorhelp.php?' . $paramstring ), 'popup' , $linkobject , 400 , 500 , $alttag , 'none' , true );
2007-01-05 04:51:46 +00:00
}
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
*/
2008-09-25 07:13:46 +00:00
function helpbutton ( $page , $title , $module = 'moodle' , $image = true , $linktext = false , $text = '' , $return = false ,
2005-01-21 05:04:24 +00:00
$imagetext = '' ) {
2007-09-27 08:18:28 +00:00
global $CFG , $COURSE ;
2006-12-07 02:19:13 +00:00
2008-07-02 06:43:21 +00:00
//warning if ever $text parameter is used
2008-06-25 03:26:08 +00:00
//$text option won't work properly because the text needs to be always cleaned and,
// when cleaned... html tags always break, so it's unusable.
if ( isset ( $text ) && $text != '' ) {
2008-09-25 07:13:46 +00:00
debugging ( 'Warning: it\'s not recommended to use $text parameter in helpbutton ($page=' . $page . ', $module=' . $module . ') function' , DEBUG_DEVELOPER );
2008-06-25 03:26:08 +00:00
}
2008-07-14 02:32:59 +00:00
2006-12-07 02:19:13 +00:00
// fix for MDL-7734
2007-09-27 08:18:28 +00:00
if ( ! empty ( $COURSE -> lang )) {
$forcelang = $COURSE -> lang ;
2006-12-07 02:19:13 +00:00
} else {
$forcelang = '' ;
}
2002-12-20 14:44:14 +00:00
2008-09-25 07:13:46 +00:00
// Catch references to the old text.html and emoticons.html help files that
// were renamed in MDL-13233.
if ( in_array ( $page , array ( 'text' , 'emoticons' , 'richtext' ))) {
$oldname = $page ;
$page .= '2' ;
debugging ( " You are referring to the old help file ' $oldname '. " .
" This was renamed to ' $page ' becuase of MDL-13233. " .
" Please update your code. " , DEBUG_DEVELOPER );
}
2004-09-22 14:39:15 +00:00
if ( $module == '' ) {
$module = 'moodle' ;
2002-12-20 14:44:14 +00:00
}
2007-12-11 13:58:39 +00:00
if ( $title == '' && $linktext == '' ) {
debugging ( 'Error in call to helpbutton function: at least one of $title and $linktext is required' );
}
2007-12-03 16:41:36 +00:00
// Warn users about new window for Accessibility
$tooltip = get_string ( 'helpprefix2' , '' , trim ( $title , " . \t " )) . ' (' . get_string ( 'newwindow' ) . ')' ;
2007-01-04 10:23:06 +00:00
2006-06-18 08:10:52 +00:00
$linkobject = '' ;
2002-12-20 14:44:14 +00:00
if ( $image ) {
if ( $linktext ) {
2006-11-30 06:03:02 +00:00
// MDL-7469 If text link is displayed with help icon, change to alt to "help with this".
2005-01-21 05:04:24 +00:00
$linkobject .= $title . ' ' ;
2007-01-04 10:23:06 +00:00
$tooltip = get_string ( 'helpwiththis' );
2002-12-20 14:44:14 +00:00
}
2006-08-11 10:33:29 +00:00
if ( $imagetext ) {
$linkobject .= $imagetext ;
} else {
2007-04-17 20:42:29 +00:00
$linkobject .= '<img class="iconhelp" alt="' . s ( strip_tags ( $tooltip )) . '" src="' .
2007-05-08 15:07:25 +00:00
$CFG -> pixpath . '/help.gif" />' ;
2006-08-11 10:33:29 +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
2006-12-07 02:19:13 +00:00
// fix for MDL-7734
2002-12-20 14:44:14 +00:00
if ( $text ) {
2006-12-07 02:19:13 +00:00
$url = '/help.php?module=' . $module . '&text=' . s ( urlencode ( $text ) . '&forcelang=' . $forcelang );
2002-12-20 14:44:14 +00:00
} else {
2006-12-07 02:19:13 +00:00
$url = '/help.php?module=' . $module . '&file=' . $page . '.html&forcelang=' . $forcelang ;
2002-12-20 14:44:14 +00:00
}
2004-08-02 19:09:06 +00:00
2007-01-04 10:23:06 +00:00
$link = '<span class="helplink">' .
2006-06-18 08:10:52 +00:00
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
*/
2007-01-04 10:23:06 +00:00
function emoticonhelpbutton ( $form , $field , $return = false ) {
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 ;
2007-01-05 12:58:22 +00:00
$imagetext = '<img src="' . $CFG -> pixpath . '/s/smiley.gif" alt="" class="emoticon" style="margin-left:3px; padding-right:1px;width:15px;height:15px;" />' ;
2008-05-07 03:08:44 +00:00
$help = helpbutton ( 'emoticons2' , get_string ( 'helpemoticons' ), 'moodle' , true , true , '' , true , $imagetext );
2007-01-04 10:23:06 +00:00
if ( ! $return ){
echo $help ;
} else {
return $help ;
}
2003-05-08 16:09:30 +00:00
}
2006-11-30 08:30:46 +00:00
/**
* Print a help button .
*
* Prints a special help button for html editors ( htmlarea in this case )
* @ uses $CFG
*/
function editorshortcutshelpbutton () {
global $CFG ;
2008-09-23 14:51:55 +00:00
//TODO: detect current editor and print correct info
/* $imagetext = '<img src="' . $CFG -> httpswwwroot . '/lib/editor/htmlarea/images/kbhelp.gif" alt="' .
2007-05-08 15:07:25 +00:00
get_string ( 'editorshortcutkeys' ) . '" class="iconkbhelp" />' ;
2006-11-30 08:30:46 +00:00
2008-09-23 14:51:55 +00:00
return helpbutton ( 'editorshortcuts' , get_string ( 'editorshortcutkeys' ), 'moodle' , true , false , '' , true , $imagetext ); */
return '' ;
2006-11-30 08:30:46 +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
*/
2007-04-30 17:08:34 +00:00
function notice ( $message , $link = '' , $course = NULL ) {
2007-12-14 21:22:38 +00:00
global $CFG , $SITE , $THEME , $COURSE ;
2002-12-20 14:44:14 +00:00
2007-12-14 21:22:38 +00:00
$message = clean_text ( $message ); // In case nasties are in here
2007-11-23 16:49:51 +00:00
2007-12-14 21:22:38 +00:00
if ( defined ( 'FULLME' ) && FULLME == 'cron' ) {
// notices in cron should be mtrace'd.
mtrace ( $message );
die ;
}
if ( ! defined ( 'HEADER_PRINTED' )) {
//header not yet printed
print_header ( get_string ( 'notice' ));
} else {
print_container_end_all ( false , $THEME -> open_header_containers );
}
2002-12-20 14:44:14 +00:00
2007-01-13 04:25:02 +00:00
print_box ( $message , 'generalbox' , 'notice' );
2005-02-08 17:09:41 +00:00
print_continue ( $link );
2007-01-04 10:23:06 +00:00
2006-09-24 06:21:15 +00:00
if ( empty ( $course )) {
2007-12-14 21:22:38 +00:00
print_footer ( $COURSE );
2006-09-24 06:21:15 +00:00
} 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 "
2007-06-01 08:01:47 +00:00
* TODO Document remaining arguments
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 );
2007-01-13 04:25:02 +00:00
print_box_start ( 'generalbox' , 'notice' );
echo '<p>' . $message . '</p>' ;
echo '<div class="buttons">' ;
2006-09-21 22:34:45 +00:00
print_single_button ( $linkyes , $optionsyes , get_string ( 'yes' ), $methodyes , $CFG -> framename );
2007-01-13 04:25:02 +00:00
print_single_button ( $linkno , $optionsno , get_string ( 'no' ), $methodno , $CFG -> framename );
echo '</div>' ;
print_box_end ();
2002-12-20 14:44:14 +00:00
}
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
*/
2007-04-30 17:08:34 +00:00
function redirect ( $url , $message = '' , $delay =- 1 ) {
2008-06-22 16:51:55 +00:00
global $CFG , $THEME , $SESSION ;
2006-01-05 07:08:10 +00:00
if ( ! empty ( $CFG -> usesid ) && ! isset ( $_COOKIE [ session_name ()])) {
2008-06-22 16:51:55 +00:00
$url = $SESSION -> 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
2007-04-18 21:52:03 +00:00
$encodedurl = preg_replace ( " / \ &(?![a-zA-Z0-9#] { 1,8};)/ " , " & " , $url );
$encodedurl = preg_replace ( '/^.*href="([^"]*)".*$/' , " \\ 1 " , clean_text ( '<a href="' . $encodedurl . '" />' ));
$url = str_replace ( '&' , '&' , $encodedurl );
2004-10-01 02:43:09 +00:00
2007-03-14 15:29:46 +00:00
/// At developer debug level. Don't redirect if errors have been printed on screen.
2007-04-24 18:53:12 +00:00
/// Currenly only works in PHP 5.2+; we do not want strict PHP5 errors
2007-07-22 17:34:41 +00:00
$lasterror = error_get_last ();
$error = defined ( 'DEBUGGING_PRINTED' ) or ( ! empty ( $lasterror ) && ( $lasterror [ 'type' ] & DEBUG_DEVELOPER ));
$errorprinted = debugging ( '' , DEBUG_ALL ) && $CFG -> debugdisplay && $error ;
2007-04-01 22:19:39 +00:00
if ( $errorprinted ) {
2007-03-14 15:29:46 +00:00
$message = " <strong>Error output, so disabling automatic redirect.</strong></p><p> " . $message ;
}
2007-09-12 02:57:08 +00:00
$performanceinfo = '' ;
if ( defined ( 'MDL_PERF' ) || ( ! empty ( $CFG -> perfdebug ) and $CFG -> perfdebug > 7 )) {
2007-09-12 02:57:26 +00:00
if ( defined ( 'MDL_PERFTOLOG' ) && ! function_exists ( 'register_shutdown_function' )) {
2007-09-12 02:57:08 +00:00
$perf = get_performance_info ();
error_log ( " PERF: " . $perf [ 'txt' ]);
}
}
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' )) {
2007-01-04 10:23:06 +00:00
2006-09-21 15:13:27 +00:00
// Technically, HTTP/1.1 requires Location: header to contain
2007-01-04 10:23:06 +00:00
// the absolute path. (In practice browsers accept relative
2006-09-21 15:13:27 +00:00
// paths - but still, might as well do it properly.)
2007-01-04 10:23:06 +00:00
// This code turns relative into absolute.
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 ) {
2006-11-27 16:42:16 +00:00
$newurl = preg_replace ( '|/(?!\.\.)[^/]*/\.\./|' , '/' , $url );
2006-09-24 19:10:13 +00:00
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
2006-12-27 01:49:14 +00:00
@ header ( $_SERVER [ 'SERVER_PROTOCOL' ] . ' 303 See Other' ); //302 might not work for POST requests, 303 is ignored by obsolete clients
2006-08-01 18:02:42 +00:00
@ 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 . '" />' ;
2008-09-26 10:49:31 +00:00
print_js_call ( 'document.location.replace' , array ( $url ));
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 :-(
2007-03-14 15:29:46 +00:00
print_header ( '' , '' , '' , '' , $errorprinted ? '' : ( '<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
2007-11-23 16:49:51 +00:00
} else {
2007-12-14 21:22:38 +00:00
print_container_end_all ( false , $THEME -> open_header_containers );
2006-05-16 20:31:22 +00:00
}
2008-03-20 06:05:44 +00:00
echo '<div id="redirect">' ;
echo '<div id="message">' . $message . '</div>' ;
echo '<div id="continue">( <a href="' . $encodedurl . '">' . get_string ( 'continue' ) . '</a> )</div>' ;
2007-02-06 08:24:37 +00:00
echo '</div>' ;
2007-03-14 15:29:46 +00:00
if ( ! $errorprinted ) {
2008-09-26 10:49:31 +00:00
print_delayed_js_call ( $delay , 'document.location.replace' , array ( $url ));
2007-03-14 15:29:46 +00:00
}
2008-03-19 08:19:31 +00:00
$CFG -> docroot = false ; // to prevent the link to moodle docs from being displayed on redirect page.
2007-04-30 17:28:28 +00:00
print_footer ( 'none' );
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 ) {
2008-05-15 21:40:00 +00:00
global $DB ;
2008-01-07 01:54:28 +00:00
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 );
2008-01-07 01:54:28 +00:00
if ( ! defined ( 'CLI_UPGRADE' ) ||! CLI_UPGRADE ) {
2007-08-27 05:17:11 +00:00
$output = '<div class="' . $style . '" style="text-align:' . $align . '">' . $message . '</div>' . " \n " ;
2008-05-15 21:40:00 +00:00
} else if ( CLI_UPGRADE && $DB -> get_debug ()) {
2008-01-07 01:54:28 +00:00
$output = '++' . $message . '++' ;
return ;
}
2007-01-04 10:23:06 +00:00
2006-10-31 10:47:26 +00:00
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
2007-05-03 10:03:59 +00:00
* @ param mixed $baseurl If this is a string then it is the url which will be appended with $pagevar , an equals sign and the page number .
* If this is a moodle_url object then the pagevar param will be replaced by the page no , for each page .
2005-05-22 00:50:37 +00:00
* @ 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 ;
2007-05-03 10:03:59 +00:00
if ( ! is_a ( $baseurl , 'moodle_url' )){
$output .= ' (<a href="' . $baseurl . $pagevar . '=' . $pagenum . '">' . get_string ( 'previous' ) . '</a>) ' ;
} else {
2007-05-29 06:27:38 +00:00
$output .= ' (<a href="' . $baseurl -> out ( false , array ( $pagevar => $pagenum )) . '">' . get_string ( 'previous' ) . '</a>) ' ;
2007-05-03 10:03:59 +00:00
}
2003-12-30 17:18:06 +00:00
}
2007-08-30 06:01:57 +00:00
if ( $perpage > 0 ) {
$lastpage = ceil ( $totalcount / $perpage );
} else {
$lastpage = 1 ;
}
2003-08-21 13:47:14 +00:00
if ( $page > 15 ) {
$startpage = $page - 10 ;
2007-05-03 10:03:59 +00:00
if ( ! is_a ( $baseurl , 'moodle_url' )){
$output .= ' <a href="' . $baseurl . $pagevar . '=0">1</a> ...' ;
} else {
2007-05-29 06:27:38 +00:00
$output .= ' <a href="' . $baseurl -> out ( false , array ( $pagevar => 0 )) . '">1</a> ...' ;
2007-05-03 10:03:59 +00:00
}
2003-08-21 13:47:14 +00:00
} else {
$startpage = 0 ;
}
$currpage = $startpage ;
2008-02-21 06:14:02 +00:00
$displaycount = $displaypage = 0 ;
2003-08-21 13:47:14 +00:00
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 {
2007-05-03 10:03:59 +00:00
if ( ! is_a ( $baseurl , 'moodle_url' )){
$output .= ' <a href="' . $baseurl . $pagevar . '=' . $currpage . '">' . $displaypage . '</a>' ;
} else {
2007-05-29 06:27:38 +00:00
$output .= ' <a href="' . $baseurl -> out ( false , array ( $pagevar => $currpage )) . '">' . $displaypage . '</a>' ;
2007-05-03 10:03:59 +00:00
}
2007-06-19 14:44:02 +00:00
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 ;
2007-05-03 10:03:59 +00:00
if ( ! is_a ( $baseurl , 'moodle_url' )){
$output .= ' ...<a href="' . $baseurl . $pagevar . '=' . $lastpageactual . '">' . $lastpage . '</a> ' ;
} else {
2007-05-29 06:27:38 +00:00
$output .= ' ...<a href="' . $baseurl -> out ( false , array ( $pagevar => $lastpageactual )) . '">' . $lastpage . '</a> ' ;
2007-05-03 10:03:59 +00:00
}
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 ) {
2007-05-03 10:03:59 +00:00
if ( ! is_a ( $baseurl , 'moodle_url' )){
$output .= ' (<a href="' . $baseurl . $pagevar . '=' . $pagenum . '">' . get_string ( 'next' ) . '</a>)' ;
} else {
2007-05-29 06:27:38 +00:00
$output .= ' (<a href="' . $baseurl -> out ( false , array ( $pagevar => $pagenum )) . '">' . get_string ( 'next' ) . '</a>)' ;
2007-05-03 10:03:59 +00:00
}
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 .
*
2008-04-22 14:14:47 +00:00
* @ param string $heading HTML for the heading . Can include full HTML or just
* plain text - plain text will automatically be enclosed in the appropriate
* heading tags .
2008-07-11 14:17:21 +00:00
* @ param string $content HTML for the content
* @ param array $list an alternative to $content , it you want a list of things with optional icons .
* @ param array $icons optional icons for the things in $list .
* @ param string $footer Extra HTML content that gets output at the end , inside a & lt ; div class = " footer " >
* @ param array $attributes an array of attribute => value pairs that are put on the
* outer div of this block . If there is a class attribute ' sideblock' gets appended to it . If there isn ' t
* already a class , class = 'sideblock' is used .
* @ 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
}
2007-10-11 16:35:20 +00:00
$skip_link = '<a href="#sb-' . $block_id . '" class="skip-block">' . $skip_text . '</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 )) {
echo $skip_link ;
2007-10-11 16:35:20 +00:00
}
//ELSE: a single link on a page "Skip block 4" is too confusing - ignore.
2006-03-04 16:04:48 +00:00
2004-05-28 10:53:54 +00:00
print_side_block_start ( $heading , $attributes );
2008-07-11 14:17:21 +00:00
// The content.
2004-05-28 10:53:54 +00:00
if ( $content ) {
echo $content ;
} 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 ) {
2007-01-09 11:04:13 +00:00
echo '<div class="icon column c0">' . $icons [ $key ] . '</div>' ;
2004-05-28 10:53:54 +00:00
}
2007-01-09 11:04:13 +00:00
echo '<div class="column c1">' . $string . '</div>' ;
2006-02-28 12:29:51 +00:00
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
}
2008-07-11 14:17:21 +00:00
}
2005-04-03 12:15:45 +00:00
2008-07-11 14:17:21 +00:00
// Footer, if any.
if ( $footer ) {
echo '<div class="footer">' . $footer . '</div>' ;
2004-05-28 10:53:54 +00:00
}
2007-12-18 17:18:19 +00:00
print_side_block_end ( $attributes , $title );
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 .
*
2008-04-22 14:14:47 +00:00
* @ param string $heading HTML for the heading . Can include full HTML or just
* plain text - plain text will automatically be enclosed in the appropriate
* heading tags .
2004-09-25 05:29:21 +00:00
* @ 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 ()) {
2007-01-11 17:01:32 +00:00
global $CFG , $THEME ;
2007-06-19 14:44:02 +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 . '>' ;
2007-08-01 14:08:50 +00:00
2007-07-18 21:06:40 +00:00
if ( ! empty ( $THEME -> customcorners )) {
echo '<div class="wrap">' . " \n " ;
}
2005-05-10 00:20:18 +00:00
if ( $heading ) {
2008-04-22 14:14:47 +00:00
// Some callers pass in complete html for the heading, which may include
// complicated things such as the 'hide block' button; some just pass in
// text. If they only pass in plain text i.e. it doesn't include a
// <div>, then we add in standard tags that make it look like a normal
// page block including the h2 for accessibility
if ( strpos ( $heading , '</div>' ) === false ) {
$heading = '<div class="title"><h2>' . $heading . '</h2></div>' ;
2008-05-21 14:59:33 +00:00
}
2007-01-11 17:01:32 +00:00
echo '<div class="header">' ;
2007-07-18 21:06:40 +00:00
if ( ! empty ( $THEME -> customcorners )) {
2007-08-06 05:49:49 +00:00
echo '<div class="bt"><div> </div></div>' ;
2007-07-18 21:06:40 +00:00
echo '<div class="i1"><div class="i2">' ;
echo '<div class="i3">' ;
}
2007-01-11 17:01:32 +00:00
echo $heading ;
2007-05-09 15:03:01 +00:00
if ( ! empty ( $THEME -> customcorners )) {
2007-07-18 21:06:40 +00:00
echo '</div></div></div>' ;
2007-01-11 17:01:32 +00:00
}
echo '</div>' ;
} else {
2007-05-09 15:03:01 +00:00
if ( ! empty ( $THEME -> customcorners )) {
2007-08-06 05:49:49 +00:00
echo '<div class="bt"><div> </div></div>' ;
2007-01-11 17:01:32 +00:00
}
}
2007-05-09 15:03:01 +00:00
if ( ! empty ( $THEME -> customcorners )) {
2007-07-18 21:06:40 +00:00
echo '<div class="i1"><div class="i2">' ;
echo '<div class="i3">' ;
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 .
*/
2007-12-18 17:18:19 +00:00
function print_side_block_end ( $attributes = array (), $title = '' ) {
2007-01-11 17:01:32 +00:00
global $CFG , $THEME ;
2007-06-19 14:44:02 +00:00
2007-01-11 17:01:32 +00:00
echo '</div>' ;
2007-05-09 15:03:01 +00:00
if ( ! empty ( $THEME -> customcorners )) {
2007-11-23 16:49:51 +00:00
echo '</div></div></div><div class="bb"><div> </div></div></div>' ;
2007-01-11 17:01:32 +00:00
}
2007-08-01 14:08:50 +00:00
2007-07-18 21:06:40 +00:00
echo '</div>' ;
2007-08-01 14:08:50 +00:00
2007-12-21 11:12:43 +00:00
$strshow = addslashes_js ( get_string ( 'showblocka' , 'access' , strip_tags ( $title )));
$strhide = addslashes_js ( get_string ( 'hideblocka' , 'access' , strip_tags ( $title )));
2007-12-18 17:18:19 +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' ])) {
2007-12-18 17:18:19 +00:00
echo '<script type="text/javascript">' . " \n //<![CDATA[ \n " . 'elementCookieHide("' . $attributes [ 'id' ] .
'","' . $strshow . '","' . $strhide . " \" ); \n //]]> \n " . '</script>' ;
2005-02-09 16:17:03 +00:00
}
2005-05-10 00:20:18 +00:00
2004-05-28 10:53:54 +00:00
}
2004-09-23 04:36:43 +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
2008-07-18 03:02:43 +00:00
$CFG -> pagepath = " index.php " ;
2005-02-10 05:11:34 +00:00
print_header ( strip_tags ( $SITE -> fullname ), $SITE -> fullname , 'home' );
2008-07-18 03:02:43 +00:00
print_box_start ();
2005-02-10 05:11:34 +00:00
print_heading ( get_string ( 'sitemaintenance' , 'admin' ));
@ include ( $CFG -> dataroot . '/1/maintenance.html' );
2008-07-18 03:02:43 +00:00
print_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
}
}
/**
2007-01-26 06:28:43 +00:00
* Returns a string containing a nested list , suitable for formatting into tabs with CSS .
2005-03-15 08:49:14 +00:00
*
* @ param array $tabrows An array of rows where each row is an array of tab objects
2007-01-26 06:28:43 +00:00
* @ param string $selected The id of the selected tab ( whatever row it ' s on )
* @ param array $inactive An array of ids of inactive tabs that are not selectable .
* @ param array $activated An array of ids of other tabs that are currently activated
2005-03-15 08:49:14 +00:00
**/
2007-01-26 06:28:43 +00:00
function print_tabs ( $tabrows , $selected = NULL , $inactive = NULL , $activated = NULL , $return = false ) {
2005-03-15 08:49:14 +00:00
global $CFG ;
/// $inactive must be an array
if ( ! is_array ( $inactive )) {
$inactive = array ();
}
2006-03-04 16:04:48 +00:00
2007-01-26 06:28:43 +00:00
/// $activated must be an array
if ( ! is_array ( $activated )) {
$activated = array ();
2005-05-05 09:32:33 +00:00
}
2005-03-15 08:49:14 +00:00
2007-01-11 06:54:23 +00:00
/// Convert the tab rows into a tree that's easier to process
2007-01-26 06:28:43 +00:00
if ( ! $tree = convert_tabrows_to_tree ( $tabrows , $selected , $inactive , $activated )) {
2007-01-11 06:54:23 +00:00
return false ;
}
2005-04-03 12:15:45 +00:00
2007-01-11 06:54:23 +00:00
/// Print out the current tree of tabs (this function is recursive)
2007-06-19 14:44:02 +00:00
2007-01-11 06:54:23 +00:00
$output = convert_tree_to_html ( $tree );
$output = " \n \n " . '<div class="tabtree">' . $output . '</div><div class="clearer"> </div>' . " \n \n " ;
/// We're done!
if ( $return ) {
return $output ;
}
echo $output ;
}
2005-03-27 15:28:35 +00:00
2005-04-03 12:15:45 +00:00
2007-01-11 06:54:23 +00:00
function convert_tree_to_html ( $tree , $row = 0 ) {
2005-04-03 12:15:45 +00:00
2007-01-11 06:54:23 +00:00
$str = " \n " . '<ul class="tabrow' . $row . '">' . " \n " ;
2005-04-03 12:15:45 +00:00
2007-01-25 04:43:26 +00:00
$first = true ;
$count = count ( $tree );
2007-01-11 06:54:23 +00:00
foreach ( $tree as $tab ) {
2007-01-25 04:43:26 +00:00
$count -- ; // countdown to zero
2007-03-04 17:16:00 +00:00
$liclass = '' ;
2007-01-26 06:23:53 +00:00
if ( $first && ( $count == 0 )) { // Just one in the row
2007-03-04 17:16:00 +00:00
$liclass = 'first last' ;
2007-01-26 06:23:53 +00:00
$first = false ;
} else if ( $first ) {
2007-03-04 17:16:00 +00:00
$liclass = 'first' ;
2007-01-25 04:43:26 +00:00
$first = false ;
} else if ( $count == 0 ) {
2007-03-04 17:16:00 +00:00
$liclass = 'last' ;
2007-01-25 04:43:26 +00:00
}
2005-04-03 12:15:45 +00:00
2007-03-04 17:16:00 +00:00
if (( empty ( $tab -> subtree )) && ( ! empty ( $tab -> selected ))) {
$liclass .= ( empty ( $liclass )) ? 'onerow' : ' onerow' ;
2007-01-11 06:54:23 +00:00
}
2005-03-15 08:49:14 +00:00
2008-02-25 11:58:12 +00:00
if ( $tab -> inactive || $tab -> active || $tab -> selected ) {
2007-06-19 14:44:02 +00:00
if ( $tab -> selected ) {
2007-03-04 17:16:00 +00:00
$liclass .= ( empty ( $liclass )) ? 'here selected' : ' here selected' ;
2007-06-19 14:44:02 +00:00
} else if ( $tab -> active ) {
2007-03-04 17:16:00 +00:00
$liclass .= ( empty ( $liclass )) ? 'here active' : ' here active' ;
}
}
$str .= ( ! empty ( $liclass )) ? '<li class="' . $liclass . '">' : '<li>' ;
if ( $tab -> inactive || $tab -> active || ( $tab -> selected && ! $tab -> linkedwhenselected )) {
2007-10-09 16:56:00 +00:00
// The a tag is used for styling
$str .= '<a class="nolink"><span>' . $tab -> text . '</span></a>' ;
2007-01-11 06:54:23 +00:00
} else {
2007-03-04 17:16:00 +00:00
$str .= '<a href="' . $tab -> link . '" title="' . $tab -> title . '"><span>' . $tab -> text . '</span></a>' ;
2005-03-15 08:49:14 +00:00
}
2005-04-03 12:15:45 +00:00
2007-06-19 14:44:02 +00:00
if ( ! empty ( $tab -> subtree )) {
2007-01-11 06:54:23 +00:00
$str .= convert_tree_to_html ( $tab -> subtree , $row + 1 );
2007-01-25 04:43:26 +00:00
} else if ( $tab -> selected ) {
2007-03-04 17:16:00 +00:00
$str .= '<div class="tabrow' . ( $row + 1 ) . ' empty"> </div>' . " \n " ;
2007-01-11 06:54:23 +00:00
}
2007-07-03 20:17:54 +00:00
$str .= ' </li>' . " \n " ;
2005-03-15 08:49:14 +00:00
}
2007-01-11 06:54:23 +00:00
$str .= '</ul>' . " \n " ;
2005-04-03 12:15:45 +00:00
2007-01-11 06:54:23 +00:00
return $str ;
}
2007-01-26 06:28:43 +00:00
function convert_tabrows_to_tree ( $tabrows , $selected , $inactive , $activated ) {
2007-01-11 06:54:23 +00:00
/// Work backwards through the rows (bottom to top) collecting the tree as we go.
$tabrows = array_reverse ( $tabrows );
$subtree = array ();
foreach ( $tabrows as $row ) {
$tree = array ();
foreach ( $row as $tab ) {
2007-02-01 07:02:28 +00:00
$tab -> inactive = in_array (( string ) $tab -> id , $inactive );
$tab -> active = in_array (( string ) $tab -> id , $activated );
$tab -> selected = ( string ) $tab -> id == $selected ;
2007-01-11 06:54:23 +00:00
if ( $tab -> active || $tab -> selected ) {
if ( $subtree ) {
$tab -> subtree = $subtree ;
}
}
$tree [] = $tab ;
}
$subtree = $tree ;
2005-03-15 09:47:41 +00:00
}
2007-01-11 06:54:23 +00:00
2007-03-14 15:29:46 +00:00
return $subtree ;
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 = '' ) {
2007-07-31 16:19:53 +00:00
global $ME , $COURSE , $CFG ;
2007-10-05 15:06:38 +00:00
if ( empty ( $CFG -> docroot ) or empty ( $CFG -> rolesactive )) {
2007-07-31 16:19:53 +00:00
return '' ;
}
if ( empty ( $COURSE -> id )) {
$context = get_context_instance ( CONTEXT_SYSTEM );
} else {
$context = get_context_instance ( CONTEXT_COURSE , $COURSE -> id );
}
if ( ! has_capability ( 'moodle/site:doclinks' , $context )) {
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-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 '' ;
}
2007-06-19 14:44:02 +00:00
return doc_link ( $path , $text , $iconpath );
}
2008-11-05 08:17:30 +00:00
/**
* @ param string $path the end of the URL .
* @ return The start of a MoodleDocs URL in the user ' s language .
* E . g . http :// docs . moodle . org / en /
*/
function get_docs_url ( $path ) {
global $CFG ;
return $CFG -> docroot . '/' . str_replace ( '_utf8' , '' , current_language ()) . '/' . $path ;
}
2007-06-19 14:44:02 +00:00
/**
* Returns a string containing a link to the user documentation .
* Also contains an icon by default . Shown to teachers and admin only .
*
2007-06-19 15:21:27 +00:00
* @ param string $path The page link after doc root and language , no
* leading slash .
2007-06-19 14:44:02 +00:00
* @ param string $text The text to be displayed for the link
* @ param string $iconpath The path to the icon to be displayed
*/
function doc_link ( $path = '' , $text = '' , $iconpath = '' ) {
global $CFG ;
2008-05-21 14:59:33 +00:00
2007-07-31 16:19:53 +00:00
if ( empty ( $CFG -> docroot )) {
2007-06-19 14:44:02 +00:00
return '' ;
}
2008-01-13 08:30:39 +00:00
2008-11-05 08:17:30 +00:00
$url = get_docs_url ( $path );
2008-01-13 08:30:39 +00:00
2007-06-19 14:44:02 +00:00
$target = '' ;
if ( ! empty ( $CFG -> doctonewwindow )) {
2008-01-13 08:30:39 +00:00
$target = " onclick= \" window.open(' $url '); return false; \" " ;
2007-06-19 14:44:02 +00:00
}
2006-04-16 16:21:50 +00:00
2008-01-04 06:45:55 +00:00
$str = " <a href= \" $url\ " $target > " ;
2006-03-04 16:04:48 +00:00
2008-05-21 14:59:33 +00:00
if ( empty ( $iconpath )) {
2007-01-27 19:56:08 +00:00
$iconpath = $CFG -> httpswwwroot . '/pix/docs.gif' ;
2006-03-01 07:47:14 +00:00
}
2006-03-04 16:04:48 +00:00
2006-11-25 18:09:33 +00:00
// alt left blank intentionally to prevent repetition in screenreaders
2007-01-08 20:30:38 +00:00
$str .= '<img class="iconhelp" src="' . $iconpath . '" alt="" />' . $text . '</a>' ;
2006-03-04 16:04:48 +00:00
2006-03-01 07:47:14 +00:00
return $str ;
}
2007-08-09 08:40:02 +00:00
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-12-21 04:03:21 +00:00
* If passed a parameter it will emit a debugging notice similar to trigger_error () . The
* routing of notices is controlled by $CFG -> debugdisplay
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 );
2006-12-21 04:03:21 +00:00
* 4 ) if ( debugging ()) { perform extra debugging operations ( do not use print or echo ) }
*
* In code blocks controlled by debugging () ( such as example 4 )
* any output should be routed via debugging () itself , or the lower - level
* trigger_error () or error_log () . Using echo or print will break XHTML
* JS and HTTP headers .
*
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
2008-06-22 16:51:55 +00:00
* @ param array $backtrace use different backtrace
2006-09-14 04:25:17 +00:00
* @ return bool
*/
2008-06-22 16:51:55 +00:00
function debugging ( $message = '' , $level = DEBUG_NORMAL , $backtrace = null ) {
2006-09-14 04:25:17 +00:00
global $CFG ;
if ( empty ( $CFG -> debug )) {
return false ;
}
if ( $CFG -> debug >= $level ) {
if ( $message ) {
2008-06-22 16:51:55 +00:00
if ( ! $backtrace ) {
$backtrace = debug_backtrace ();
}
$from = print_backtrace ( $backtrace , true );
2006-12-21 04:03:21 +00:00
if ( ! isset ( $CFG -> debugdisplay )) {
2008-07-07 14:34:40 +00:00
$CFG -> debugdisplay = ini_get_bool ( 'display_errors' );
2006-12-21 04:03:21 +00:00
}
if ( $CFG -> debugdisplay ) {
2007-07-22 17:34:41 +00:00
if ( ! defined ( 'DEBUGGING_PRINTED' )) {
define ( 'DEBUGGING_PRINTED' , 1 ); // indicates we have printed something
}
2007-01-22 01:55:09 +00:00
notify ( $message . $from , 'notifytiny' );
2006-12-21 02:51:20 +00:00
} else {
2007-01-22 01:55:09 +00:00
trigger_error ( $message . $from , E_USER_NOTICE );
2006-12-21 02:51:20 +00:00
}
2006-09-14 04:25:17 +00:00
}
return true ;
}
return false ;
}
2006-03-01 07:47:14 +00:00
2008-06-13 17:51:34 +00:00
/**
* Prints formatted backtrace
* @ param backtrace array
* @ param return return as string or print
* @ return mixed
*/
function print_backtrace ( $callers , $return = false ) {
global $CFG ;
$from = '<ul style="text-align: left">' ;
foreach ( $callers as $caller ) {
if ( ! isset ( $caller [ 'line' ])) {
$caller [ 'line' ] = '?' ; // probably call_user_func()
}
if ( ! isset ( $caller [ 'file' ])) {
$caller [ 'file' ] = $CFG -> dirroot . '/unknownfile' ; // probably call_user_func()
}
$from .= '<li>line ' . $caller [ 'line' ] . ' of ' . substr ( $caller [ 'file' ], strlen ( $CFG -> dirroot ) + 1 );
if ( isset ( $caller [ 'function' ])) {
$from .= ': call to ' ;
if ( isset ( $caller [ 'class' ])) {
$from .= $caller [ 'class' ] . $caller [ 'type' ];
}
$from .= $caller [ 'function' ] . '()' ;
} else if ( isset ( $caller [ 'exception' ])) {
$from .= ': ' . $caller [ 'exception' ] . ' thrown' ;
}
$from .= '</li>' ;
}
$from .= '</ul>' ;
if ( $return ) {
return $from ;
} else {
echo $from ;
}
}
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 ;-)
}
2007-01-04 04:57:50 +00:00
2007-01-05 04:51:46 +00:00
/**
2007-01-04 04:57:50 +00:00
* Returns string to add a frame attribute , if required
*/
function frametarget () {
global $CFG ;
if ( empty ( $CFG -> framename ) or ( $CFG -> framename == '_top' )) {
return '' ;
} else {
return ' target="' . $CFG -> framename . '" ' ;
}
}
2007-03-23 08:01:01 +00:00
/**
* Outputs a HTML comment to the browser . This is used for those hard - to - debug
* pages that use bits from many different files in very confusing ways ( e . g . blocks ) .
* @ usage print_location_comment ( __FILE__ , __LINE__ );
* @ param string $file
* @ param integer $line
* @ param boolean $return Whether to return or print the comment
* @ return mixed Void unless true given as third parameter
*/
function print_location_comment ( $file , $line , $return = false )
{
if ( $return ) {
return " <!-- $file at line $line --> \n " ;
} else {
echo " <!-- $file at line $line --> \n " ;
}
}
2007-05-09 15:03:01 +00:00
2007-07-04 10:01:20 +00:00
2007-07-12 16:36:38 +00:00
/**
2007-07-04 10:01:20 +00:00
* Returns an image of an up or down arrow , used for column sorting . To avoid unnecessary DB accesses , please
* provide this function with the language strings for sortasc and sortdesc .
* If no sort string is associated with the direction , an arrow with no alt text will be printed / returned .
* @ param string $direction 'up' or 'down'
* @ param string $strsort The language string used for the alt attribute of this image
* @ param bool $return Whether to print directly or return the html string
* @ return string HTML for the image
2007-07-12 16:36:38 +00:00
*
2007-07-04 10:01:20 +00:00
* TODO See if this isn ' t already defined somewhere . If not , move this to weblib
*/
function print_arrow ( $direction = 'up' , $strsort = null , $return = false ) {
global $CFG ;
2007-07-12 16:36:38 +00:00
2007-08-01 14:08:50 +00:00
if ( ! in_array ( $direction , array ( 'up' , 'down' , 'right' , 'left' , 'move' ))) {
2007-07-04 10:01:20 +00:00
return null ;
}
2007-07-12 16:36:38 +00:00
$return = null ;
2007-07-04 10:01:20 +00:00
switch ( $direction ) {
case 'up' :
$sortdir = 'asc' ;
break ;
case 'down' :
$sortdir = 'desc' ;
break ;
2007-08-01 14:08:50 +00:00
case 'move' :
$sortdir = 'asc' ;
break ;
2007-07-04 10:01:20 +00:00
default :
$sortdir = null ;
break ;
}
// Prepare language string
$strsort = '' ;
if ( empty ( $strsort ) && ! empty ( $sortdir )) {
$strsort = get_string ( 'sort' . $sortdir , 'grades' );
}
$return = ' <img src="' . $CFG -> pixpath . '/t/' . $direction . '.gif" alt="' . $strsort . '" /> ' ;
if ( $return ) {
return $return ;
} else {
echo $return ;
}
}
2007-08-17 07:25:47 +00:00
/**
2007-08-09 08:40:02 +00:00
* Returns boolean true if the current language is right - to - left ( Hebrew , Arabic etc )
*
*/
function right_to_left () {
static $result ;
if ( isset ( $result )) {
return $result ;
}
2008-01-13 08:30:39 +00:00
return $result = ( get_string ( 'thisdirection' ) == 'rtl' );
2007-08-09 08:40:02 +00:00
}
/**
* Returns swapped left <=> right if in RTL environment .
* part of RTL support
*
* @ param string $align align to check
* @ return string
*/
function fix_align_rtl ( $align ) {
2008-01-13 08:30:39 +00:00
if ( ! right_to_left ()) {
2007-08-17 07:25:47 +00:00
return $align ;
2007-08-09 08:40:02 +00:00
}
2008-01-13 08:30:39 +00:00
if ( $align == 'left' ) { return 'right' ; }
if ( $align == 'right' ) { return 'left' ; }
return $align ;
2007-08-09 08:40:02 +00:00
}
2007-11-10 14:53:54 +00:00
/**
* Returns true if the page is displayed in a popup window .
* Gets the information from the URL parameter inpopup .
*
* @ return boolean
*
2008-01-13 08:30:39 +00:00
* TODO Use a central function to create the popup calls allover Moodle and
2007-11-10 14:53:54 +00:00
* TODO In the moment only works with resources and probably questions .
*/
2007-11-12 11:17:24 +00:00
function is_in_popup () {
2007-11-10 14:53:54 +00:00
$inpopup = optional_param ( 'inpopup' , '' , PARAM_BOOL );
2008-01-13 08:30:39 +00:00
2007-11-10 14:53:54 +00:00
return ( $inpopup );
}
2008-07-04 10:15:00 +00:00
class progress_bar {
private $html_id ;
private $percent ;
private $width ;
private $clr ;
private $lastcall ;
private $time_start ;
private $minimum_time = 2 ; //min time between updates.
2008-07-31 22:15:30 +00:00
function __construct ( $html_id = 'pid' , $width = 500 , $autostart = false ){
2008-07-04 10:15:00 +00:00
$this -> html_id = $html_id ;
$this -> clr = new stdClass ;
$this -> clr -> done = 'green' ;
$this -> clr -> process = '#FFCC66' ;
$this -> width = $width ;
$this -> restart ();
if ( $autostart ){
$this -> create ();
}
}
/**
* set progress bar color , call before $this -> create
* Usage :
* $clr -> done = 'red' ;
* $clr -> process = 'blue' ;
* $pb -> setclr ( $clr );
* $pb -> create ();
* ......
2008-07-14 02:32:59 +00:00
*
* @ param $clr object
2008-07-04 10:15:00 +00:00
*/
function setclr ( $clr ){
foreach ( $clr as $n => $v ) {
$this -> clr -> $n = $v ;
}
}
/**
* Create a new progress bar , this function will output
* html .
2008-07-14 02:32:59 +00:00
*
2008-07-04 10:15:00 +00:00
*/
function create (){
flush ();
$this -> lastcall -> pt = 0 ;
$this -> lastcall -> time = microtime ( true );
$htmlcode = <<< EOT
< script type = " text/javascript " >
2008-07-14 02:32:59 +00:00
Number . prototype . fixed = function ( n ){
2008-07-04 10:15:00 +00:00
with ( Math )
2008-07-14 02:32:59 +00:00
return round ( Number ( this ) * pow ( 10 , n )) / pow ( 10 , n );
}
2008-07-04 10:15:00 +00:00
function up_ { $this -> html_id } ( id , width , pt , msg , es ){
percent = pt * 100 ;
document . getElementById ( " status_ " + id ) . innerHTML = msg ;
document . getElementById ( " pt_ " + id ) . innerHTML =
percent . fixed ( 2 ) + '%' ;
if ( percent == 100 ) {
document . getElementById ( " progress_ " + id ) . style . background
= " { $this -> clr -> done } " ;
document . getElementById ( " time_ " + id ) . style . display
= " none " ;
} else {
document . getElementById ( " progress_ " + id ) . style . background
= " { $this -> clr -> process } " ;
if ( es == Infinity ){
document . getElementById ( " time_ " + id ) . innerHTML =
" Initializing... " ;
} else {
document . getElementById ( " time_ " + id ) . innerHTML =
es . fixed ( 2 ) + " sec " ;
document . getElementById ( " time_ " + id ) . style . display
= " block " ;
}
}
document . getElementById ( " progress_ " + id ) . style . width
= width + " px " ;
}
2007-11-10 14:53:54 +00:00
2008-07-04 10:15:00 +00:00
</ script >
< div style = " text-align:center;width: { $this -> width } px;clear:both;padding:0;margin:0 auto; " >
< h2 id = " status_ { $this -> html_id } " style = " text-align: center;margin:0 auto " ></ h2 >
< p id = " time_ { $this -> html_id } " ></ p >
< div id = " bar_ { $this -> html_id } " style = " border-style:solid;border-width:1px;width:500px;height:50px; " >
< div id = " progress_ { $this -> html_id } "
style = " text-align:center;background: { $this -> clr -> process } ;width:4px;border:1px
solid gray ; height : 38 px ; padding - top : 10 px ; " > <span id= " pt_ { $this -> html_id } " ></span>
</ div >
</ div >
</ div >
EOT ;
echo $htmlcode ;
flush ();
}
function _update ( $percent , $msg , $es ){
if ( empty ( $this -> time_start )){
$this -> time_start = microtime ( true );
}
$this -> percent = $percent ;
$this -> lastcall -> time = microtime ( true );
$this -> lastcall -> pt = $percent ;
$w = $this -> percent * $this -> width ;
if ( $es === null ){
$es = " Infinity " ;
}
echo " <script type= \" text/javascript \" >up_ " . $this -> html_id . " (' $this->html_id ', ' $w ', ' $this->percent ', ' $msg ', $es );</script> " ;
flush ();
}
/**
* estimate time
2008-07-14 02:32:59 +00:00
*
2008-07-04 10:15:00 +00:00
* @ param $curtime int the time call this function
* @ param $percent int
*/
function estimate ( $curtime , $pt ){
$consume = $curtime - $this -> time_start ;
$one = $curtime - $this -> lastcall -> time ;
$this -> percent = $pt ;
2008-07-14 02:32:59 +00:00
$percent = $pt - $this -> lastcall -> pt ;
2008-07-04 10:15:00 +00:00
if ( $percent != 0 )
$left = ( $one / $percent ) - $consume ;
else
return null ;
if ( $left < 0 )
return 0 ;
else
return $left ;
}
/**
* Update progress bar according percent
2008-07-14 02:32:59 +00:00
*
2008-07-04 10:15:00 +00:00
* @ param $percent int from 1 - 100
* @ param $msg string the message needed to be shown
*/
function update_full ( $percent , $msg ){
$percent = max ( min ( $percent , 100 ), 0 );
if ( $percent != 100 && ( $this -> lastcall -> time + $this -> minimum_time ) > microtime ( true )){
return ;
}
$this -> _update ( $percent / 100 , $msg );
}
/**
* Update progress bar according the nubmer of tasks
2008-07-14 02:32:59 +00:00
*
* @ param $cur int current task number
* @ param $total int total task number
2008-07-04 10:15:00 +00:00
* @ param $msg string message
*/
function update ( $cur , $total , $msg ){
$cur = max ( $cur , 0 );
if ( $cur >= $total ){
2008-07-14 02:32:59 +00:00
$percent = 1 ;
2008-07-04 10:15:00 +00:00
} else {
$percent = $cur / $total ;
}
2008-09-17 07:02:08 +00:00
/**
2008-07-04 10:15:00 +00:00
if ( $percent != 1 && ( $this -> lastcall -> time + $this -> minimum_time ) > microtime ( true )){
return ;
}
2008-09-17 07:02:08 +00:00
*/
2008-07-04 10:15:00 +00:00
$es = $this -> estimate ( microtime ( true ), $percent );
$this -> _update ( $percent , $msg , $es );
}
/**
* Restart the progress bar .
*/
function restart (){
$this -> percent = 0 ;
$this -> lastcall = new stdClass ;
$this -> lastcall -> pt = 0 ;
$this -> lastcall -> time = microtime ( true );
$this -> time_start = 0 ;
}
}
2007-11-10 14:53:54 +00:00
2003-03-14 07:55:22 +00:00
// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
2007-12-15 16:53:39 +00:00
?>