From: jamiesensei Date: Thu, 9 Nov 2006 18:08:34 +0000 (+0000) Subject: fixes for small bugs in datetimeselector and dateselector and added custom elements... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=e2294b982205b375ee8e23ca703f4fc897565621;p=moodle.git fixes for small bugs in datetimeselector and dateselector and added custom elements to formslib library for standard module forms --- diff --git a/lib/form/dateselector.php b/lib/form/dateselector.php index fb185655d2..f9746ab0d1 100644 --- a/lib/form/dateselector.php +++ b/lib/form/dateselector.php @@ -78,13 +78,14 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group $this->_elements[] =& MoodleQuickForm::createElement('select', 'day', null, $days, $this->getAttributes(), true); $this->_elements[] =& MoodleQuickForm::createElement('select', 'month', null, $months, $this->getAttributes(), true); $this->_elements[] =& MoodleQuickForm::createElement('select', 'year', null, $years, $this->getAttributes(), true); + $this->setValue(); } // }}} // {{{ setValue() - function setValue($value) + function setValue($value=0) { if (!($value)) { $value = time(); diff --git a/lib/form/datetimeselector.php b/lib/form/datetimeselector.php index 4d4d78c1fb..c5a885f342 100644 --- a/lib/form/datetimeselector.php +++ b/lib/form/datetimeselector.php @@ -5,9 +5,9 @@ require_once "$CFG->libdir/formslib.php"; /** * Class for a group of elements used to input a date and time. - * + * * Emulates moodle print_date_selector function and also allows you to select a time. - * + * * @author Jamie Pratt * @access public */ @@ -30,12 +30,12 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group{ * @var array */ var $_wrap = array('', ''); - + //var $_seperator=array('', '', 'Time ', ''); /** * Class constructor - * + * * @access public * @param string Element's name * @param mixed Label(s) for an element @@ -89,12 +89,13 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group{ $this->_elements[] =& MoodleQuickForm::createElement('select', 'hour', null, $hours, $this->getAttributes(), true); $this->_elements[] =& MoodleQuickForm::createElement('select','minute', null, $minutes, $this->getAttributes(), true); + $this->setValue(); } // }}} // {{{ setValue() - function setValue($value) + function setValue($value=0) { if (!($value)) { $value = time(); @@ -107,7 +108,7 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group{ 'day' => $currentdate['mday'], 'month' => $currentdate['mon'], 'year' => $currentdate['year']); - + } parent::setValue($value); } diff --git a/lib/form/modgroupmode.php b/lib/form/modgroupmode.php new file mode 100644 index 0000000000..a45d6cfbcc --- /dev/null +++ b/lib/form/modgroupmode.php @@ -0,0 +1,75 @@ +libdir/form/select.php"; + +/** + * HTML class for a editor format drop down element + * + * @author Jamie Pratt + * @access public + */ +class MoodleQuickForm_modgroupmode extends MoodleQuickForm_select{ + + + /** + * Class constructor + * + * @param string Select name attribute + * @param mixed Label(s) for the select + * @param mixed Either a typical HTML attribute string or an associative array + * @param mixed $options ignored + * @access public + * @return void + */ + function MoodleQuickForm_modgroupmode($elementName=null, $elementLabel=null, $attributes=null, $options=null) + { + HTML_QuickForm_element::HTML_QuickForm_element($elementName, $elementLabel, $attributes, null); + $this->_type = 'modgroupmode'; + + /*if ($options['course']->groupmodeforce){ + $this->updateAttributes(array('disabled'=>'disabled')); + }*/ + + } //end constructor + + /** + * Called by HTML_QuickForm whenever form event is made on this element + * + * @param string $event Name of event + * @param mixed $arg event arguments + * @param object $caller calling object + * @since 1.0 + * @access public + * @return mixed + */ + function onQuickFormEvent($event, $arg, &$caller) + { + switch ($event) { + case 'createElement': + $choices=array(); + $choices[NOGROUPS] = get_string('groupsnone'); + $choices[SEPARATEGROUPS] = get_string('groupsseparate'); + $choices[VISIBLEGROUPS] = get_string('groupsvisible'); + $this->load($choices); + break; + /*case 'updateValue' : + $value = $this->_findValue($caller->_constantValues); + if (null === $value) { + $value = $this->_findValue($caller->_submitValues); + if (null === $value && (!$caller->isSubmitted() || !$this->getMultiple())) { + $value = $this->_findValue($caller->_defaultValues); + } + } + if (null !== $value) { + $this->setValue($value); + }else{ + $this->setValue(groupmode($options['course'], $options['cm'])); + } + return true; + break;*/ + } + return parent::onQuickFormEvent($event, $arg, $caller); + } + +} +?> \ No newline at end of file diff --git a/lib/form/modvisible.php b/lib/form/modvisible.php new file mode 100644 index 0000000000..fd89b68bb3 --- /dev/null +++ b/lib/form/modvisible.php @@ -0,0 +1,83 @@ +libdir/form/select.php"; + +/** + * HTML class for a editor format drop down element + * + * @author Jamie Pratt + * @access public + */ +class MoodleQuickForm_modvisible extends MoodleQuickForm_select{ + + + + /** + * Class constructor + * + * @param string $elementName Select name attribute + * @param mixed $elementLabel Label(s) for the select + * @param mixed $attributes Either a typical HTML attribute string or an associative array + * @param array $options ignored + * @access public + * @return void + */ + function MoodleQuickForm_modvisible($elementName=null, $elementLabel=null, $attributes=null, $options=null) + { + HTML_QuickForm_element::HTML_QuickForm_element($elementName, $elementLabel, $attributes, null); + $this->_type = 'modvisible'; + + + } //end constructor + + /** + * Called by HTML_QuickForm whenever form event is made on this element + * + * @param string $event Name of event + * @param mixed $arg event arguments + * @param object $caller calling object + * @access public + * @return mixed + */ + function onQuickFormEvent($event, $arg, &$caller) + { + switch ($event) { + case 'createElement': + $choices=array(); + $choices[1] = get_string('show'); + $choices[0] = get_string('hide'); + $this->load($choices); + break; + /*case 'updateValue' : + $value = $this->_findValue($caller->_constantValues); + if (null === $value) { + $value = $this->_findValue($caller->_submitValues); + if (null === $value && (!$caller->isSubmitted() || !$this->getMultiple())) { + $value = $this->_findValue($caller->_defaultValues); + } + } + if (null !== $value) { + $this->setValue($value); + }else{ + $this->setValue($this->_modVisible($this->_options['hiddensection'], + $this->_options['cm'])); + } + return true; + break;*/ + } + return parent::onQuickFormEvent($event, $arg, $caller); + } +/* function _modVisible($hiddensection, $cm){ + if ($cm) { + $visible = $cm->visible; + } else { + $visible = true; + } + if ($hiddensection) { + $visible = false; + } + return $visible; + }*/ + +} +?> \ No newline at end of file diff --git a/lib/formslib.php b/lib/formslib.php index 1067d42059..9b3b9c0d23 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -280,6 +280,74 @@ class moodleform { return true; } } +/** + * For extra methods for form wrapper specific to be used for module add / update forms. + * + */ +class moodleform_mod extends moodleform { + function standard_coursemodule_elements(){ + $mform=$this->_form; + $mform->addElement('header', '', get_string('modstandardels', 'form')); + $mform->addElement('modgroupmode', 'groupmode', get_string('groupmode')); + $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode'))); + $mform->setType('groupmode', PARAM_INT); + + $mform->addElement('modvisible', 'visible', get_string('visible')); + $mform->setType('visible', PARAM_INT); + + $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); + } + + function standard_coursemodule_elements_setup($course, $cm, $section){ + $this->modgroupmode_setup($course, $cm); + $this->modvisible_setup($course, $cm, $section); + } + function modgroupmode_setup($course, $cm){ + $this->set_defaults(array('groupmode'=>groupmode($course, $cm))); + + } + 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)); + } + +} class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless { var $_types = array(); @@ -611,30 +679,6 @@ function validate_' . $this->_attributes['id'] . '(frm) { return ''; } - function addGroupmodeSetting($course) { - - if (! $course = get_record('course', 'id', $course)) { - error("This course doesn't exist"); - } - - if ($form->coursemodule) { - if (! $cm = get_record('course_modules', 'id', $form->coursemodule)) { - error("This course module doesn't exist"); - } - } else { - $cm = null; - } - $groupmode = groupmode($course, $cm); - if ($course->groupmode or (!$course->groupmodeforce)) { - $choices = array(); - $choices[NOGROUPS] = get_string('groupsnone'); - $choices[SEPARATEGROUPS] = get_string('groupsseparate'); - $choices[VISIBLEGROUPS] = get_string('groupsvisible'); - choose_from_menu($choices, 'groupmode', $groupmode, '', '', 0, false, $course->groupmodeforce); - - } - - } } /** @@ -787,5 +831,7 @@ MoodleQuickForm::registerElementType('htmleditor', "$CFG->libdir/form/htmleditor MoodleQuickForm::registerElementType('format', "$CFG->libdir/form/format.php", 'MoodleQuickForm_format'); MoodleQuickForm::registerElementType('static', "$CFG->libdir/form/static.php", 'MoodleQuickForm_static'); MoodleQuickForm::registerElementType('hidden', "$CFG->libdir/form/hidden.php", 'MoodleQuickForm_hidden'); +MoodleQuickForm::registerElementType('modvisible', "$CFG->libdir/form/modvisible.php", 'MoodleQuickForm_modvisible'); +MoodleQuickForm::registerElementType('modgroupmode', "$CFG->libdir/form/modgroupmode.php", 'MoodleQuickForm_modgroupmode'); ?> \ No newline at end of file