From: jamiesensei Date: Fri, 24 Nov 2006 06:39:15 +0000 (+0000) Subject: some restructuring of formslib - moving moodleform_mod definition to new file course... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=e24b7f851a34b1a5d15970eb4f83876d834f82cf;p=moodle.git some restructuring of formslib - moving moodleform_mod definition to new file course\moodleform_mod.php and some changes to moodleform_mod standard elements setup and moodleform disabledIf API --- diff --git a/course/modedit.php b/course/modedit.php index b0b57b013e..6dc9dd9964 100644 --- a/course/modedit.php +++ b/course/modedit.php @@ -136,7 +136,7 @@ } $mformclassname=$module->name.'_mod_form'; - $mform=& new $mformclassname('modedit.php', array('course' => $course)); + $mform=& new $mformclassname($form->instance, isset($cw->section)?$cw->section:$section, ((isset($cm))?$cm:null)); if ($fromform=$mform->data_submitted()){ if (empty($fromform->coursemodule)) { //add @@ -299,7 +299,6 @@ $icon = ' '; print_heading_with_help($pageheading, "mods", $module->name, $icon); - $mform->standard_coursemodule_elements_setup($course, isset($cm)?$cm:null, $form->section); $mform->set_defaults($form); $mform->display(); print_footer($course); diff --git a/course/moodleform_mod.php b/course/moodleform_mod.php new file mode 100644 index 0000000000..7f88ea5295 --- /dev/null +++ b/course/moodleform_mod.php @@ -0,0 +1,153 @@ +libdir.'/formslib.php'); +/** + * This class adds extra methods to form wrapper specific to be used for module + * add / update forms (mod/{modname}.mod_form.php replaces deprecared mod/{modname}/mod.html + * + */ +class moodleform_mod extends moodleform { + /** + * Instance of the module that is being updated. This is the id of the {prefix}{modulename} + * record. Can be used in form definition. Will be "" if this is an 'add' form and not an + * update one. + * + * @var mixed + */ + var $_instance; + /** + * Section of course that module instance will be put in or is in. + * This is always the section number itself. + * + * @var mixed + */ + var $_section; + /** + * Coursemodle record of the module that is being updated. Will be null if this is an 'add' form and not an + * update one. + * + * @var mixed + */ + var $_cm; + + function moodleform_mod($instance, $section, $cm) { + $this->_instance = $instance; + $this->_section = $section; + $this->_cm = $cm; + parent::moodleform('modedit.php'); + } + /** + * Only available on moodleform_mod since defaults for forms cannot + * be calculated externally. You can use _customdata in here if necessary. + * + * + * @param array $default_values passed by reference + */ + function defaults_preprocessing(&$default_values){ + } + /** + * Load in existing data as form defaults. Usually new entry defaults are stored directly in + * form definition (new entry form); this function is used to load in data where values + * already exist and data is being edited (edit entry form). + * + * @param mixed $default_values object or array of default values + */ + function set_defaults($default_values) { + if (is_object($default_values)) { + $default_values = (array)$default_values; + } + $this->defaults_preprocessing($default_values); + parent::set_defaults($default_values + $this->standard_coursemodule_elements_settings());//never slashed for moodleform_mod + } + /** + * Adds all the standard elements to a form to edit the settings for an activity module. + * + * @param bool $supportsgroups does this module support groups? + */ + function standard_coursemodule_elements($supportsgroups=true){ + $mform =& $this->_form; + $mform->addElement('header', '', get_string('modstandardels', 'form')); + if ($supportsgroups){ + $mform->addElement('modgroupmode', 'groupmode', get_string('groupmode')); + } + $mform->addElement('modvisible', 'visible', get_string('visible')); + + $this->standard_hidden_coursemodule_elements(); + } + + function standard_hidden_coursemodule_elements(){ + $mform =& $this->_form; + $mform->addElement('hidden', 'course', 0); + $mform->setType('course', PARAM_INT); + + $mform->addElement('hidden', 'coursemodule', 0); + $mform->setType('coursemodule', PARAM_INT); + + $mform->addElement('hidden', 'section', 0); + $mform->setType('section', PARAM_INT); + + $mform->addElement('hidden', 'module', 0); + $mform->setType('module', PARAM_INT); + + $mform->addElement('hidden', 'modulename', ''); + $mform->setType('modulename', PARAM_SAFEDIR); + + $mform->addElement('hidden', 'instance', 0); + $mform->setType('instance', PARAM_INT); + + $mform->addElement('hidden', 'add', 0); + $mform->setType('add', PARAM_ALPHA); + + $mform->addElement('hidden', 'update', 0); + $mform->setType('update', PARAM_INT); + } + + /** + * This function is called by course/modedit.php to setup defaults for standard form + * elements. + * + * @param object $course + * @param object $cm + * @param integer $section + * @return unknown + */ + function standard_coursemodule_elements_settings(){ + return ($this->modgroupmode_settings() + $this->modvisible_settings()); + } + /** + * This is called from modedit.php to load the default for the groupmode element. + * + * @param object $course + * @param object $cm + */ + function modgroupmode_settings(){ + global $COURSE; + return array('groupmode'=>groupmode($COURSE, $this->_cm)); + } + /** + * This is called from modedit.php to set the default for modvisible form element. + * + * @param object $course + * @param object $cm + * @param integer $section section is a db id when updating a activity config + * or the section no when adding a new activity + */ + function modvisible_settings(){ + global $COURSE; + $cm=$this->_cm; + $section=$this->_section; + if ($cm) { + $visible = $cm->visible; + } else { + $visible = 1; + } + + $hiddensection = !get_field('course_sections', 'visible', 'section', $section, 'course', $COURSE->id); + if ($hiddensection) { + $visible = 0; + } + return array('visible'=>$visible); + } + +} + +?> \ No newline at end of file diff --git a/lib/form/group.php b/lib/form/group.php index 23ef0db77b..88fbbb7e3e 100644 --- a/lib/form/group.php +++ b/lib/form/group.php @@ -52,4 +52,5 @@ class MoodleQuickForm_group extends HTML_QuickForm_group{ function getElementTemplateType(){ return $this->_elementTemplateType; } -} \ No newline at end of file +} +?> \ No newline at end of file diff --git a/lib/formslib.php b/lib/formslib.php index 454c25e9d5..1a1a0dbba3 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -359,106 +359,6 @@ class moodleform { return true; } } -/** - * This class adds extra methods to form wrapper specific to be used for module - * add / update forms (mod/{modname}.mod_form.php replaces deprecared mod/{modname}/mod.html - * - */ -class moodleform_mod extends moodleform { - - /** - * Adds all the standard elements to a form to edit the settings for an activity module. - * - * @param bool $supportsgroups does this module support groups? - */ - function standard_coursemodule_elements($supportsgroups=true){ - $mform =& $this->_form; - $mform->addElement('header', '', get_string('modstandardels', 'form')); - if ($supportsgroups){ - $mform->addElement('modgroupmode', 'groupmode', get_string('groupmode')); - } - $mform->addElement('modvisible', 'visible', get_string('visible')); - - $this->standard_hidden_coursemodule_elements(); - } - - function standard_hidden_coursemodule_elements(){ - $mform =& $this->_form; - $mform->addElement('hidden', 'course', 0); - $mform->setType('course', PARAM_INT); - - $mform->addElement('hidden', 'coursemodule', 0); - $mform->setType('coursemodule', PARAM_INT); - - $mform->addElement('hidden', 'section', 0); - $mform->setType('section', PARAM_INT); - - $mform->addElement('hidden', 'module', 0); - $mform->setType('module', PARAM_INT); - - $mform->addElement('hidden', 'modulename', ''); - $mform->setType('modulename', PARAM_SAFEDIR); - - $mform->addElement('hidden', 'instance', 0); - $mform->setType('instance', PARAM_INT); - - $mform->addElement('hidden', 'add', 0); - $mform->setType('add', PARAM_ALPHA); - - $mform->addElement('hidden', 'update', 0); - $mform->setType('update', PARAM_INT); - } - - /** - * This function is called by course/modedit.php to setup defaults for standard form - * elements. - * - * @param object $course - * @param object $cm - * @param integer $section - */ - function standard_coursemodule_elements_setup($course, $cm, $section){ - $this->modgroupmode_setup($course, $cm); - $this->modvisible_setup($course, $cm, $section); - } - /** - * This is called from modedit.php to load the default for the groupmode element. - * - * @param object $course - * @param object $cm - */ - function modgroupmode_setup($course, $cm){ - $this->set_defaults(array('groupmode'=>groupmode($course, $cm))); - - } - /** - * This is called from modedit.php to set the default for modvisible form element. - * - * @param object $course - * @param object $cm - * @param integer $section section is a db id when updating a activity config - * or the section no when adding a new activity - */ - function modvisible_setup($course, $cm, $section){ - if ($cm) { - $visible = $cm->visible; - } else { - $visible = 1; - } - - if (!$cm) { // adding activity - //in this case $form->section is the section number, not the id - $hiddensection = !get_field('course_sections', 'visible', 'section', $section, 'course', $course->id); - } else { //updating activity - $hiddensection = !get_field('course_sections', 'visible', 'id', $section); - } - if ($hiddensection) { - $visible = 0; - } - $this->set_defaults(array('visible'=>$visible)); - } - -} /** * You never extend this class directly. The class methods of this class are available from @@ -825,7 +725,8 @@ function validate_' . $this->_attributes['id'] . '(frm) { } $js=rtrim($js, ', '); $js .= "],\n"; - $js .= "condition : '{$element['condition']}'},\n"; + $js .= "condition : '{$element['condition']}',\n"; + $js .= "value : '{$element['value']}'},\n"; }; $js=rtrim($js, ",\n"); @@ -872,17 +773,18 @@ function validate_' . $this->_attributes['id'] . '(frm) { * condition * @param string $condition the condition to check */ - function disabledIf($elementName, $dependentOn, $condition = 'notchecked'){ + function disabledIf($elementName, $dependentOn, $condition = 'notchecked', $value=null){ $dependents = $this->_getElNamesRecursive($elementName); foreach ($dependents as $dependent){ if ($dependent != $dependentOn) { $this->_dependencies[$dependentOn][] = array('dependent'=>$dependent, - 'condition'=>$condition); + 'condition'=>$condition, 'value'=>$value); } } } } + /** * A renderer for MoodleQuickForm that only uses XHTML and CSS and no * table tags, extends PEAR class HTML_QuickForm_Renderer_Tableless diff --git a/lib/javascript-static.js b/lib/javascript-static.js index ee9843819d..d0ede00718 100644 --- a/lib/javascript-static.js +++ b/lib/javascript-static.js @@ -69,8 +69,11 @@ function lockoptionsall(formid) { thislock=!form.elements[master].checked; } else if (items[master].condition=='checked'){ thislock=form.elements[master].checked; + } else if (items[master].condition=='eq'){ + thislock=(form.elements[master].value==items[master].value); } else { - thislock=(form.elements[master].value==items[master].condition); + thislock=(form.elements[master].value!=items[master].value); + } for (var i=0; ilibdir.'/formslib.php'); +require_once ('moodleform_mod.php'); + class chat_mod_form extends moodleform_mod { function definition() { diff --git a/mod/data/mod_form.php b/mod/data/mod_form.php index bd38fc2786..109fd867b4 100644 --- a/mod/data/mod_form.php +++ b/mod/data/mod_form.php @@ -1,5 +1,6 @@ libdir.'/formslib.php'); +require_once ('moodleform_mod.php'); + class data_mod_form extends moodleform_mod { function definition() { diff --git a/mod/forum/mod_form.php b/mod/forum/mod_form.php index 5030592fff..682e150041 100644 --- a/mod/forum/mod_form.php +++ b/mod/forum/mod_form.php @@ -1,5 +1,6 @@ libdir.'/formslib.php'); +require_once ('moodleform_mod.php'); + class forum_mod_form extends moodleform_mod { function definition() { @@ -112,7 +113,7 @@ class forum_mod_form extends moodleform_mod { $mform->setDefault('blockafter', '0'); $mform->addRule('blockafter', null, 'numeric', null, 'client'); $mform->setHelpButton('blockafter', array('manageposts', get_string('blockafter', 'forum'),'forum')); - $mform->disabledIf('blockafter', 'blockperiod', 0); + $mform->disabledIf('blockafter', 'blockperiod', 'eq', 0); $mform->addElement('text', 'warnafter', get_string('warnafter', 'forum')); @@ -120,7 +121,7 @@ class forum_mod_form extends moodleform_mod { $mform->setDefault('warnafter', '0'); $mform->addRule('warnafter', null, 'numeric', null, 'client'); $mform->setHelpButton('warnafter', array('manageposts', get_string('warnafter', 'forum'),'forum')); - $mform->disabledIf('warnafter', 'blockperiod', 0); + $mform->disabledIf('warnafter', 'blockperiod', 'eq', 0); //------------------------------------------------------------------------------- $this->standard_coursemodule_elements(); @@ -150,19 +151,16 @@ class forum_mod_form extends moodleform_mod { } } - function set_defaults($default_values, $slashed=false){ - if (is_object($default_values)) { - $default_values = (array)$default_values; - } + function defaults_preprocessing($default_values){ if (isset($default_values['assessed'])){ $default_values['ratingtime']=($default_values['assessed'] && $default_values['assesstimestart'] && $default_values['assesstimefinish'] )?1:0; } - parent::set_defaults($default_values, $slashed); } + } ?> \ No newline at end of file diff --git a/mod/glossary/mod_form.php b/mod/glossary/mod_form.php index 5f3272a1a0..d34e9c8a88 100644 --- a/mod/glossary/mod_form.php +++ b/mod/glossary/mod_form.php @@ -1,5 +1,6 @@ libdir.'/formslib.php'); +require_once ('moodleform_mod.php'); + class glossary_mod_form extends moodleform_mod { function definition() { @@ -114,7 +115,7 @@ class glossary_mod_form extends moodleform_mod { $choices[50] = '50'; $mform->addElement('select', 'rssarticles', get_string('rssarticles'), $choices); $mform->setHelpButton('rssarticles', array('rssarticles', get_string('rssarticles'), 'glossary')); - $mform->disabledIf('rssarticles', 'rsstype', 0); + $mform->disabledIf('rssarticles', 'rsstype', 'eq', 0); } //------------------------------------------------------------------------------- @@ -170,11 +171,7 @@ class glossary_mod_form extends moodleform_mod { } - - function set_defaults($default_values, $slashed=false){ - if (is_object($default_values)) { - $default_values = (array)$default_values; - } + function defaults_preprocessing($default_values){ if (isset($default_values['assessed'])){ $default_values['userating']=($default_values['assessed'])?true:false; } @@ -183,7 +180,7 @@ class glossary_mod_form extends moodleform_mod { && $default_values['assesstimestart'] && $default_values['assesstimefinish'] )?1:0; } - parent::set_defaults($default_values, $slashed); } + } ?> \ No newline at end of file diff --git a/mod/journal/mod_form.php b/mod/journal/mod_form.php index 7320b5e645..37c50cdb98 100644 --- a/mod/journal/mod_form.php +++ b/mod/journal/mod_form.php @@ -1,5 +1,6 @@ libdir.'/formslib.php'); +require_once ('moodleform_mod.php'); + class journal_mod_form extends moodleform_mod { function definition() { diff --git a/mod/label/mod_form.php b/mod/label/mod_form.php index ac4c519aff..cf62869a0f 100644 --- a/mod/label/mod_form.php +++ b/mod/label/mod_form.php @@ -1,5 +1,6 @@ libdir.'/formslib.php'); +require_once ('moodleform_mod.php'); + class label_mod_form extends moodleform_mod { function definition() { diff --git a/mod/lesson/lib.php b/mod/lesson/lib.php index 6f69d58d07..b6ec3a2179 100644 --- a/mod/lesson/lib.php +++ b/mod/lesson/lib.php @@ -26,7 +26,7 @@ function lesson_add_instance($lesson) { if(!isset($lesson->completed)) { $lesson->completed = 0; } - if(empty($lesson->gradebetterthan) or !is_numeric($lesson->timespent) or $lesson->gradebetterthan < 0) { + if(empty($lesson->gradebetterthan) or !is_numeric($lesson->gradebetterthan) or $lesson->gradebetterthan < 0) { $lesson->gradebetterthan = 0; } else if ($lesson->gradebetterthan > 100) { $lesson->gradebetterthan = 100; @@ -119,7 +119,7 @@ function lesson_update_instance($lesson) { if(!isset($lesson->completed)) { $lesson->completed = 0; } - if(empty($lesson->gradebetterthan) or !is_numeric($lesson->timespent) or $lesson->gradebetterthan < 0) { + if(empty($lesson->gradebetterthan) or !is_numeric($lesson->gradebetterthan) or $lesson->gradebetterthan < 0) { $lesson->gradebetterthan = 0; } else if ($lesson->gradebetterthan > 100) { $lesson->gradebetterthan = 100; diff --git a/mod/lesson/mod_form.php b/mod/lesson/mod_form.php index c23d21e867..debd8fa6bd 100644 --- a/mod/lesson/mod_form.php +++ b/mod/lesson/mod_form.php @@ -7,7 +7,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @package lesson **/ -require_once ($CFG->libdir.'/formslib.php'); +require_once ('moodleform_mod.php'); + require_once('locallib.php'); class lesson_mod_form extends moodleform_mod { @@ -188,7 +189,10 @@ class lesson_mod_form extends moodleform_mod { $options = array(0=>get_string('none')); if ($lessons = get_all_instances_in_course('lesson', $COURSE)) { foreach($lessons as $lesson) { - $options[$lesson->id] = format_string($lesson->name, true); + if ($lesson->id != $this->_instance){ + $options[$lesson->id] = format_string($lesson->name, true); + } + } } $mform->addElement('select', 'dependency', get_string('dependencyon', 'lesson'), $options); @@ -209,25 +213,6 @@ class lesson_mod_form extends moodleform_mod { //------------------------------------------------------------------------------- $mform->addElement('header', '', get_string('mediafile', 'lesson')); - // get the modules - if ($mods = get_course_mods($COURSE->id)) { - $modinstances = array(); - foreach ($mods as $mod) { - - // get the module name and then store it in a new array - if ($module = get_coursemodule_from_instance($mod->modname, $mod->instance, $COURSE->id)) { - $modinstances[$mod->id] = $mod->modname.' - '.$module->name; - } - } - asort($modinstances); // sort by module name - $modinstances=array(0=>get_string('none'))+$modinstances; - - $mform->addElement('select', 'activitylink', get_string('activitylink', 'lesson'), $modinstances); - $mform->setHelpButton('activitylink', array('activitylink', get_string('activitylink', 'lesson'), 'lesson')); - $mform->setDefault('activitylink', 0); - - } - $mform->addElement('choosecoursefile', 'mediafile', get_string('mediafile', 'lesson'), array('courseid'=>$COURSE->id)); $mform->setHelpButton('mediafile', array('mediafile', get_string('mediafile', 'lesson'), 'lesson')); $mform->setDefault('mediafile', ''); @@ -253,6 +238,28 @@ class lesson_mod_form extends moodleform_mod { //------------------------------------------------------------------------------- $mform->addElement('header', '', get_string('other', 'lesson')); + + // get the modules + if ($mods = get_course_mods($COURSE->id)) { + $modinstances = array(); + foreach ($mods as $mod) { + + // get the module name and then store it in a new array + if ($module = get_coursemodule_from_instance($mod->modname, $mod->instance, $COURSE->id)) { + if ($this->_cm->id != $mod->id){ + $modinstances[$mod->id] = $mod->modname.' - '.$module->name; + } + } + } + asort($modinstances); // sort by module name + $modinstances=array(0=>get_string('none'))+$modinstances; + + $mform->addElement('select', 'activitylink', get_string('activitylink', 'lesson'), $modinstances); + $mform->setHelpButton('activitylink', array('activitylink', get_string('activitylink', 'lesson'), 'lesson')); + $mform->setDefault('activitylink', 0); + + } + $mform->addElement('text', 'maxhighscores', get_string('maxhighscores', 'lesson')); $mform->setHelpButton('maxhighscores', array('maxhighscores', get_string('maxhighscores', 'lesson'), 'lesson')); $mform->setDefault('maxhighscores', 10); @@ -275,44 +282,18 @@ class lesson_mod_form extends moodleform_mod { $renderer->addStopFieldsetElements('buttonar'); } - function definition_after_data(){ - $mform =& $this->_form; - $dependencyel =& $mform->getElement('dependency'); - //unset this option from the dependency drop down : - $instance = $mform->getElementValue('instance'); - if ($instance){ - $dependencyel->removeOption($instance); - } - $update = $mform->getElementValue('update'); - if ($update){ - $activitylinkel =& $mform->getElement('activitylink'); - $activitylinkel->removeOption($update); + + function defaults_preprocessing(&$default_values){ + if (isset($default_values['conditions'])) { + $conditions = unserialize($default_values['conditions']); + $default_values['timespent'] = $conditions->timespent; + $default_values['completed'] = $conditions->completed; + $default_values['gradebetterthan'] = $conditions->gradebetterthan; } - } - /** - * Load in existing data as form defaults. Usually new entry defaults are stored directly in - * form definition (new entry form); this function is used to load in data where values - * already exist and data is being edited (edit entry form). - * - * Overriding this to add functionality to unserialize some fields before setting deafaults for - * this module - * - * @param mixed $default_values object or array of default values - * @param bool $slased true if magic quotes applied to data values - */ - function set_defaults($default_values, $slashed=false) { - if (isset($default_values->conditions)) { - $conditions = unserialize($default_values->conditions); - $default_values->timespent = $conditions->timespent; - $default_values->completed = $conditions->completed; - $default_values->gradebetterthan = $conditions->gradebetterthan; - } - if (isset($default_values->password)) { - unset($default_values->password); + if (isset($default_values['password'])) { + unset($default_values['password']); } - parent::set_defaults($default_values, $slashed=false); - } - + } } ?> \ No newline at end of file