2006-09-24 17:04:51 +00:00
< ? php
/**
* formslib . php - library of classes for creating forms in Moodle , based on PEAR QuickForms .
* THIS IS NOT YET PART OF THE MOODLE API , IT IS HERE FOR TESTING ONLY
* @ author Jamie Pratt
* @ version $Id $
* @ license http :// www . gnu . org / copyleft / gpl . html GNU Public License
*/
//point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else.
if ( FALSE === strstr ( ini_get ( 'include_path' ), $CFG -> libdir . '/pear' )){
ini_set ( 'include_path' , $CFG -> libdir . '/pear' . PATH_SEPARATOR . ini_get ( 'include_path' ));
}
require_once 'HTML/QuickForm.php' ;
require_once 'HTML/QuickForm/DHTMLRulesTableless.php' ;
require_once 'HTML/QuickForm/Renderer/Tableless.php' ;
2006-09-25 11:08:44 +00:00
if ( $CFG -> debug >= DEBUG_ALL ){
PEAR :: setErrorHandling ( PEAR_ERROR_PRINT );
}
2006-09-24 17:04:51 +00:00
class moodleform extends HTML_QuickForm_DHTMLRulesTableless {
/**
* Class constructor - same parameters as HTML_QuickForm_DHTMLRulesTableless
* @ param string $formName Form ' s name .
* @ param string $method ( optional ) Form 's method defaults to ' POST '
* @ param string $action ( optional ) Form ' s action
* @ param string $target ( optional ) Form ' s target defaults to none
* @ param mixed $attributes ( optional ) Extra attributes for < form > tag
* @ param bool $trackSubmit ( optional ) Whether to track if the form was submitted by adding a special hidden field
* @ access public
*/
function moodleform ( $formName = '' , $method = 'post' , $action = '' , $target = '' , $attributes = null ){
global $CFG ;
//we need to override the constructor, we don't call the parent constructor
//at all because it strips slashes depending on the magic quotes setting
//whereas Moodle already has added slashes whether magic quotes is on or not.
//also added check for sesskey and added sesskey to all forms
//and told class to ask Moodle for the max upload file size
HTML_Common :: HTML_Common ( $attributes );
$method = ( strtoupper ( $method ) == 'GET' ) ? 'get' : 'post' ;
$action = ( $action == '' ) ? $_SERVER [ 'PHP_SELF' ] : $action ;
$target = empty ( $target ) ? array () : array ( 'target' => $target );
//no 'name' atttribute for form in xhtml strict :
$attributes = array ( 'action' => $action , 'method' => $method , 'id' => $formName ) + $target ;
$this -> updateAttributes ( $attributes );
//check for sesskey for this form
//if it is not present then we don't accept any input
if ( isset ( $_REQUEST [ '_qf__' . $formName ])) {
$this -> _submitValues = $this -> _recursiveFilter ( 'stripslashes' , 'get' == $method ? $_GET : $_POST );
foreach ( $_FILES as $keyFirst => $valFirst ) {
foreach ( $valFirst as $keySecond => $valSecond ) {
if ( 'name' == $keySecond ) {
$this -> _submitFiles [ $keyFirst ][ $keySecond ] = $this -> _recursiveFilter ( 'stripslashes' , $valSecond );
} else {
$this -> _submitFiles [ $keyFirst ][ $keySecond ] = $valSecond ;
}
}
}
$this -> _flagSubmitted = count ( $this -> _submitValues ) > 0 || count ( $this -> _submitFiles ) > 0 ;
}
//check sesskey
if ( $this -> _flagSubmitted ){
confirm_sesskey ( $this -> _submitValues [ '_qf__' . $formName ]);
}
unset ( $this -> _submitValues [ '_qf__' . $formName ]);
//add sesskey to all forms
$this -> addElement ( 'hidden' , '_qf__' . $formName , sesskey ());
if ( preg_match ( '/^([0-9]+)([a-zA-Z]*)$/' , get_max_upload_file_size (), $matches )) {
// see http://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
switch ( strtoupper ( $matches [ '2' ])) {
case 'G' :
$this -> _maxFileSize = $matches [ '1' ] * 1073741824 ;
break ;
case 'M' :
$this -> _maxFileSize = $matches [ '1' ] * 1048576 ;
break ;
case 'K' :
$this -> _maxFileSize = $matches [ '1' ] * 1024 ;
break ;
default :
$this -> _maxFileSize = $matches [ '1' ];
}
}
//this is custom stuff for Moodle :
$oldclass = $this -> getAttribute ( 'class' );
if ( ! empty ( $oldclass )){
$this -> updateAttributes ( array ( 'class' => $oldclass . ' mform' ));
} else {
2006-09-26 09:42:42 +00:00
$this -> updateAttributes ( array ( 'class' => 'mform' ));
2006-09-24 17:04:51 +00:00
}
$this -> _helpImageURL = " $CFG->wwwroot /lib/form/req.gif " ;
$this -> _reqHTML =
helpbutton ( 'requiredelement' , get_string ( 'requiredelement' , 'form' ), 'moodle' ,
true , false , '' , true , '<img alt="' . get_string ( 'requiredelement' , 'form' ) . '" src="' .
$this -> _helpImageURL . '" />' );
$this -> setRequiredNote ( get_string ( 'denotesreq' , 'form' , $this -> getReqHTML ()));
}
function getReqHTML (){
return $this -> _reqHTML ;
}
/**
* Class constructor - same parameters as HTML_QuickForm_DHTMLRulesTableless
* @ param array $buttons An associative array representing help button to attach to
* to the form . keys of array correspond to names of elements in form .
*
* @ access public
*/
2006-09-27 16:00:19 +00:00
function setHelpButtons ( $buttons , $suppresscheck = false ){
2006-09-24 17:04:51 +00:00
foreach ( $this -> _elements as $no => $element ){
if ( array_key_exists ( $element -> getName (), $buttons )){
2006-09-26 09:42:42 +00:00
if ( method_exists ( $element , 'setHelpButton' )){
$this -> _elements [ $no ] -> setHelpButton ( $buttons [ $element -> getName ()]);
} else {
$a = new object ();
$a -> name = $element -> getName ();
$a -> classname = get_class ( $element );
print_error ( 'nomethodforaddinghelpbutton' , 'form' , '' , $a );
}
2006-09-24 17:04:51 +00:00
unset ( $buttons [ $element -> getName ()]);
}
}
if ( count ( $buttons ) && ! $suppresscheck ){
print_error ( 'nonexistentformelements' , 'form' , '' , join ( ', ' , array_keys ( $buttons )));
}
}
2006-09-27 16:00:19 +00:00
function acceptGet (){
$names = func_get_args ();
foreach ( $names as $name ){
//if no form data is submitted then the page has just beeen loaded
//so we get the page param value from $_GET
if ( ! $this -> _flagSubmitted && isset ( $_GET [ $name ])){
$this -> _submitValues [ $name ] = $_GET [ $name ];
}
2006-09-24 17:04:51 +00:00
}
}
2006-09-27 16:00:19 +00:00
function required_param ( $element , $paramtype ){
$value = $this -> getSubmitValue ( $element );
if ( null !== $value ) {
if ( false === strpos ( $element , '[' )) {
return $this -> _submitValues [ $element ] = $this -> _clean_param ( $value , $paramtype );
} else {
$idx = " [' " . str_replace ( array ( ']' , '[' ), array ( '' , " '][' " ), $element ) . " '] " ;
eval ( " return \$ this->_submitValues { $idx } = \$ this->_clean_param( \$ value, \$ paramtype); " );
}
} else {
//could print name of param but better not to for security?
print_error ( 'missingrequiredfield' , 'error' );
}
}
function optional_param ( $element , $default , $paramtype ){
$value = $this -> getSubmitValue ( $element );
if ( null === $value ) {
return $default ;
}
if ( false === strpos ( $element , '[' )) {
return $this -> _submitValues [ $element ] = $this -> _clean_param ( $value , $paramtype );
} else {
$idx = " [' " . str_replace ( array ( ']' , '[' ), array ( '' , " '][' " ), $element ) . " '] " ;
eval ( " return \$ this->_submitValues { $idx } = \$ this->_clean_param( \$ value, \$ paramtype); " );
}
}
function clean_param ( $elementList , $paramtype ){
$value = $this -> getSubmitValue ( $element );
if ( false === strpos ( $element , '[' )) {
return $this -> _submitValues [ $element ] = $this -> _clean_param ( $value , $paramtype );
} else {
$idx = " [' " . str_replace ( array ( ']' , '[' ), array ( '' , " '][' " ), $element ) . " '] " ;
eval ( " return \$ this->_submitValues { $idx } = \$ this->_clean_param( \$ value, \$ paramtype); " );
}
}
function _clean_param ( $value , $paramtype ){
//clean_param function in moodlelib expects vars with slashes
$value = $this -> _recursiveFilter ( 'addslashes' , $value );
$value = clean_param ( $value , $paramtype );
return $this -> _recursiveFilter ( 'stripslashes' , $value );
}
2006-09-24 17:04:51 +00:00
function exportValue ( $element , $addslashes = true ){
$unfiltered = parent :: exportValue ( $element );
if ( $addslashes ){
2006-09-27 16:00:19 +00:00
return $this -> _recursiveFilter ( 'addslashes' , $unfiltered );
2006-09-24 17:04:51 +00:00
} else {
return $unfiltered ;
}
}
2006-09-27 16:00:19 +00:00
function exportValues ( $elementList = null , $addslashes = true ){
2006-09-24 17:04:51 +00:00
$unfiltered = parent :: exportValues ( $elementList );
if ( $addslashes ){
2006-09-27 16:00:19 +00:00
return $this -> _recursiveFilter ( 'addslashes' , $unfiltered );
2006-09-24 17:04:51 +00:00
} else {
return $unfiltered ;
}
}
}
/**
* A renderer for moodleform that only uses XHTML and CSS and no
* table tags , extends PEAR class HTML_QuickForm_Renderer_Tableless
*
* Stylesheet is part of standard theme and should be automatically included .
*
* @ author Jamie Pratt < me @ jamiep . org >
* @ license gpl license
*/
class moodleform_renderer extends HTML_QuickForm_Renderer_Tableless {
/**
* Element template array
* @ var array
* @ access private
*/
var $_elementTemplates ;
2006-09-27 16:00:19 +00:00
2006-09-27 19:12:52 +00:00
// uncomment templates below and edit formslib.php for
2006-09-27 16:00:19 +00:00
// ol li containers for form items.
2006-09-27 19:12:52 +00:00
/**
* Template used when opening a hidden fieldset
* ( i . e . a fieldset that is opened when there is no header element )
* @ var string
* @ access private
*/
var $_openHiddenFieldsetTemplate = " \n \t <fieldset class= \" hidden \" > " ;
2006-09-27 16:00:19 +00:00
// var $_openHiddenFieldsetTemplate = "\n\t<fieldset class=\"hidden\">\n\t\t<ol>";
// /**
// * Header Template string
// * @var string
// * @access private
// */
// var $_headerTemplate =
// "\n\t\t<legend>{header}</legend>\n\t\t<ol>";
2006-09-27 19:12:52 +00:00
// var $_headerTemplate =
// "\n\t\t<legend>{header}</legend>\n\t\t<ol>";
/**
* Template used when closing a fieldset
* @ var string
* @ access private
*/
var $_closeFieldsetTemplate = " \n \t \t </fieldset> " ;
2006-09-27 16:00:19 +00:00
// var $_closeFieldsetTemplate = "\n\t\t</ol>\n\t</fieldset>";
2006-09-27 19:12:52 +00:00
/**
* Required Note template string
* @ var string
* @ access private
*/
var $_requiredNoteTemplate =
" \n \t \t <div class= \" fdescription \" > { requiredNote}</div> " ;
2006-09-24 17:04:51 +00:00
var $_htmleditors = array ();
function moodleform_renderer (){
2006-09-27 16:00:19 +00:00
// switch next two lines for ol li containers for form items.
2006-09-27 19:12:52 +00:00
// $this->_elementTemplates=array('default'=>"\n\t\t<li class=\"fitem\"><label>{label}{help}<!-- BEGIN required -->{req}<!-- END required --></label><div class=\"qfelement<!-- BEGIN error --> error<!-- END error --> {type}\"><!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->{element}</div></li>");
2006-09-28 04:19:56 +00:00
$this -> _elementTemplates = array ( 'default' => " \n \t \t <div class= \" fitem \" ><label> { label} { help}<!-- BEGIN required --> { req}<!-- END required --></label><div class= \" felement<!-- BEGIN error --> error<!-- END error --> { type} \" ><!-- BEGIN error --><span class= \" error \" > { error}</span><br /><!-- END error --> { element}</div></div> " ,
'fieldset' => " \n \t \t <div class= \" fitem \" ><label> { label} { help}<!-- BEGIN required --> { req}<!-- END required --></label><fieldset class= \" felement<!-- BEGIN error --> error<!-- END error --> { type} \" ><!-- BEGIN error --><span class= \" error \" > { error}</span><br /><!-- END error --> { element}</fieldset></div> " );
2006-09-24 17:04:51 +00:00
parent :: HTML_QuickForm_Renderer_Tableless ();
}
function startForm ( & $form ){
$this -> _reqHTML = $form -> getReqHTML ();
$this -> _elementTemplates = str_replace ( '{req}' , $this -> _reqHTML , $this -> _elementTemplates );
parent :: startForm ( $form );
}
function startGroup ( & $group , $required , $error ){
if ( method_exists ( $group , 'getElementTemplateType' )){
2006-09-27 16:07:37 +00:00
$html = $this -> _elementTemplates [ $group -> getElementTemplateType ()];
2006-09-24 17:04:51 +00:00
} else {
$html = $this -> _elementTemplates [ 'default' ];
}
if ( method_exists ( $group , 'getHelpButton' )){
$html = str_replace ( '{help}' , $group -> getHelpButton (), $html );
} else {
$html = str_replace ( '{help}' , '' , $html );
}
2006-09-27 19:12:52 +00:00
$html = str_replace ( '{type}' , 'fgroup' , $html );
2006-09-26 09:42:42 +00:00
2006-09-24 17:04:51 +00:00
$this -> _templates [ $group -> getName ()] = $html ;
// Fix for bug in tableless quickforms that didn't allow you to stop a
// fieldset before a group of elements.
// if the element name indicates the end of a fieldset, close the fieldset
if ( in_array ( $group -> getName (), $this -> _stopFieldsetElements )
&& $this -> _fieldsetsOpen > 0
) {
$this -> _html .= $this -> _closeFieldsetTemplate ;
$this -> _fieldsetsOpen -- ;
}
parent :: startGroup ( $group , $required , $error );
}
function renderElement ( & $element , $required , $error ){
if ( method_exists ( $element , 'getElementTemplateType' )){
$html = $this -> _elementTemplates [ $element -> getElementTemplateType ()];
} else {
$html = $this -> _elementTemplates [ 'default' ];
}
2006-09-27 19:12:52 +00:00
$html = str_replace ( '{type}' , 'f' . $element -> getType (), $html );
2006-09-24 17:04:51 +00:00
if ( method_exists ( $element , 'getHelpButton' )){
$html = str_replace ( '{help}' , $element -> getHelpButton (), $html );
} else {
$html = str_replace ( '{help}' , '' , $html );
}
$this -> _templates [ $element -> getName ()] = $html ;
2006-09-28 04:19:56 +00:00
$element -> updateAttributes ( array ( 'id' => 'id_' . $element -> getAttribute ( 'id' )));
2006-09-24 17:04:51 +00:00
parent :: renderElement ( $element , $required , $error );
}
}
2006-09-27 16:00:19 +00:00
/* class moodleform_filter {
2006-09-24 17:04:51 +00:00
var $paramtype ;
var $default ;
2006-09-27 16:00:19 +00:00
function moodleform_filter ( $paramtype , $default = NULL ){
2006-09-24 17:04:51 +00:00
$this -> paramtype = $paramtype ;
$this -> default = $default ;
}
function required_param ( $value ){
if ( isset ( $value )) {
$param = $value ;
} else {
error ( 'A required parameter was missing' );
}
return $this -> clean_param ( $param );
}
function optional_param ( $value ){
if ( ! isset ( $value )) {
return $this -> default ;
}
return $this -> clean_param ( $value );
}
2006-09-27 16:00:19 +00:00
} */
2006-09-24 17:04:51 +00:00
$GLOBALS [ '_HTML_QuickForm_default_renderer' ] =& new moodleform_renderer ();
moodleform :: registerElementType ( 'checkbox' , " $CFG->libdir /form/checkbox.php " , 'moodleform_checkbox' );
moodleform :: registerElementType ( 'file' , " $CFG->libdir /form/file.php " , 'moodleform_file' );
moodleform :: registerElementType ( 'group' , " $CFG->libdir /form/group.php " , 'moodleform_group' );
moodleform :: registerElementType ( 'password' , " $CFG->libdir /form/password.php " , 'moodleform_password' );
moodleform :: registerElementType ( 'radio' , " $CFG->libdir /form/radio.php " , 'moodleform_radio' );
moodleform :: registerElementType ( 'select' , " $CFG->libdir /form/select.php " , 'moodleform_select' );
moodleform :: registerElementType ( 'text' , " $CFG->libdir /form/text.php " , 'moodleform_text' );
moodleform :: registerElementType ( 'textarea' , " $CFG->libdir /form/textarea.php " , 'moodleform_textarea' );
moodleform :: registerElementType ( 'date_selector' , " $CFG->libdir /form/dateselector.php " , 'moodleform_date_selector' );
moodleform :: registerElementType ( 'htmleditor' , " $CFG->libdir /form/htmleditor.php " , 'moodleform_htmleditor' );
2006-09-26 13:55:51 +00:00
moodleform :: registerElementType ( 'static' , " $CFG->libdir /form/static.php " , 'moodleform_static' );
2006-09-27 16:00:19 +00:00
moodleform :: registerElementType ( 'hidden' , " $CFG->libdir /form/hidden.php " , 'moodleform_hidden' );
2006-09-24 17:04:51 +00:00
2006-09-25 11:08:44 +00:00
2006-09-24 17:04:51 +00:00
?>