MDL-18910 moving modedit features to modname_supports()
[moodle.git] / course / moodleform_mod.php
1 <?php  //$Id$
2 require_once ($CFG->libdir.'/formslib.php');
3 if (!empty($CFG->enablecompletion) or !empty($CFG->enableavailability)) {
4     require_once($CFG->libdir.'/completionlib.php');
5 }
7 /**
8  * This class adds extra methods to form wrapper specific to be used for module
9  * add / update forms (mod/{modname}.mod_form.php replaces deprecated mod/{modname}/mod_form.php
10  *
11  */
12 class moodleform_mod extends moodleform {
13     /**
14      * Instance of the module that is being updated. This is the id of the {prefix}{modulename}
15      * record. Can be used in form definition. Will be "" if this is an 'add' form and not an
16      * update one.
17      *
18      * @var mixed
19      */
20     protected $_instance;
21     /**
22      * Section of course that module instance will be put in or is in.
23      * This is always the section number itself (column 'section' from 'course_sections' table).
24      *
25      * @var mixed
26      */
27     protected $_section;
28     /**
29      * Coursemodle record of the module that is being updated. Will be null if this is an 'add' form and not an
30      * update one.
31       *
32      * @var mixed
33      */
34     protected $_cm;
35     /**
36      * List of modform features
37      */
38     protected $_features;
39     /**
40      * @var array Custom completion-rule elements, if enabled
41      */
42     protected $_customcompletionelements;
43     /**
44      * @var string name of module
45      */
46     protected $_modname;
48     function moodleform_mod($instance, $section, $cm) {
49         $this->_instance = $instance;
50         $this->_section = $section;
51         $this->_cm = $cm;
52         // Guess module name
53         $matches = array();
54         if (!preg_match('/^mod_([^_]+)_mod_form$/', get_class($this), $matches)) {
55             debugging('Use $modname parameter or rename form to mod_xx_mod_form, where xx is name of your module');
56             print_error('unknownmodulename');
57         }
58         $this->_modname = $matches[1];
59         $this->init_features();
60         parent::moodleform('modedit.php');
61     }
63     protected function init_features() {
64         global $CFG;
66         $this->_features = new object();
67         $this->_features->groups            = plugin_supports('mod', $this->_modname, FEATURE_GROUPS, true);
68         $this->_features->groupings         = plugin_supports('mod', $this->_modname, FEATURE_GROUPINGS, false);
69         $this->_features->groupmembersonly  = plugin_supports('mod', $this->_modname, FEATURE_GROUPMEMBERSONLY, false);
70         $this->_features->outcomes          = (!empty($CFG->enableoutcomes) and plugin_supports('mod', $this->_modname, FEATURE_GRADE_OUTCOMES, true));
71         $this->_features->hasgrades         = plugin_supports('mod', $this->_modname, FEATURE_GRADE_HAS_GRADE, false);
72         $this->_features->idnumber          = plugin_supports('mod', $this->_modname, FEATURE_IDNUMBER, true);
73         $this->_features->introeditor       = plugin_supports('mod', $this->_modname, FEATURE_MODEDIT_INTRO_EDITOR, true);
74         $this->_features->defaultcompletion = plugin_supports('mod', $this->_modname, FEATURE_MODEDIT_DEFAULT_COMPLETION, true);
76         $this->_features->gradecat          = ($this->_features->outcomes or $this->_features->hasgrades);
77     }
79     /**
80      * Only available on moodleform_mod.
81      *
82      * @param array $default_values passed by reference
83      */
84     function data_preprocessing(&$default_values){
85     }
87     /**
88      * Each module which defines definition_after_data() must call this method using parent::definition_after_data();
89      */
90     function definition_after_data() {
91         global $CFG, $COURSE;
92         $mform =& $this->_form;
94         if ($id = $mform->getElementValue('update')) {
95             $modulename = $mform->getElementValue('modulename');
96             $instance   = $mform->getElementValue('instance');
98             if ($this->_features->gradecat) {
99                 $gradecat = false;
100                 if (!empty($CFG->enableoutcomes) and $this->_features->outcomes) {
101                     if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
102                         $gradecat = true;
103                     }
104                 }
105                 if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename,
106                                                    'iteminstance'=>$instance, 'courseid'=>$COURSE->id))) {
107                     foreach ($items as $item) {
108                         if (!empty($item->outcomeid)) {
109                             $elname = 'outcome_'.$item->outcomeid;
110                             if ($mform->elementExists($elname)) {
111                                 $mform->hardFreeze($elname); // prevent removing of existing outcomes
112                             }
113                         }
114                     }
115                     foreach ($items as $item) {
116                         if (is_bool($gradecat)) {
117                             $gradecat = $item->categoryid;
118                             continue;
119                         }
120                         if ($gradecat != $item->categoryid) {
121                             //mixed categories
122                             $gradecat = false;
123                             break;
124                         }
125                     }
126                 }
128                 if ($gradecat === false) {
129                     // items and outcomes in different categories - remove the option
130                     // TODO: it might be better to add a "Mixed categories" text instead
131                     if ($mform->elementExists('gradecat')) {
132                         $mform->removeElement('gradecat');
133                     }
134                 }
135             }
136         }
138         if ($COURSE->groupmodeforce) {
139             if ($mform->elementExists('groupmode')) {
140                 $mform->hardFreeze('groupmode'); // groupmode can not be changed if forced from course settings
141             }
142         }
144         if ($mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly') and empty($COURSE->groupmodeforce)) {
145             $mform->disabledIf('groupingid', 'groupmode', 'eq', NOGROUPS);
147         } else if (!$mform->elementExists('groupmode') and $mform->elementExists('groupmembersonly')) {
148             $mform->disabledIf('groupingid', 'groupmembersonly', 'notchecked');
150         } else if (!$mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly')) {
151             // groupings have no use without groupmode or groupmembersonly
152             if ($mform->elementExists('groupingid')) {
153                 $mform->removeElement('groupingid');
154             }
155         }
157         // Completion: If necessary, freeze fields
158         $completion = new completion_info($COURSE);
159         if ($completion->is_enabled()) {
160             // If anybody has completed the activity, these options will be 'locked'
161             $completedcount = empty($this->_cm)
162                 ? 0
163                 : $completion->count_user_data($this->_cm);
165             $freeze = false;
166             if (!$completedcount) {
167                 if ($mform->elementExists('unlockcompletion')) {
168                     $mform->removeElement('unlockcompletion');
169                 }
170             } else {
171                 // Has the element been unlocked?
172                 if ($mform->exportValue('unlockcompletion')) {
173                     // Yes, add in warning text and set the hidden variable
174                     $mform->insertElementBefore(
175                         $mform->createElement('static', 'completedunlocked',
176                             get_string('completedunlocked', 'completion'),
177                             get_string('completedunlockedtext', 'completion')),
178                         'unlockcompletion');
179                     $mform->removeElement('unlockcompletion');
180                     $mform->getElement('completionunlocked')->setValue(1);
181                 } else {
182                     // No, add in the warning text with the count (now we know
183                     // it) before the unlock button
184                     $mform->insertElementBefore(
185                         $mform->createElement('static', 'completedwarning',
186                             get_string('completedwarning', 'completion'),
187                             get_string('completedwarningtext', 'completion', $completedcount)),
188                         'unlockcompletion');
189                     $mform->setHelpButton('completedwarning', array('completionlocked', get_string('help_completionlocked', 'completion'), 'completion'));
190                             
191                     $freeze = true;
192                 }
193             } 
195             if ($freeze) {
196                 $mform->freeze('completion');
197                 if ($mform->elementExists('completionview')) {
198                     $mform->freeze('completionview'); // don't use hardFreeze or checkbox value gets lost
199                 }
200                 if ($mform->elementExists('completionusegrade')) {
201                     $mform->freeze('completionusegrade');
202                 }
203                 $mform->freeze($this->_customcompletionelements);
204             } 
205         }
207         // Availability conditions
208         if (!empty($CFG->enableavailability) && $this->_cm) {
209             $ci = new condition_info($this->_cm);
210             $fullcm=$ci->get_full_course_module();
212             $num=0;
213             foreach($fullcm->conditionsgrade as $gradeitemid=>$minmax) {
214                 $groupelements=$mform->getElement('conditiongradegroup['.$num.']')->getElements();
215                 $groupelements[0]->setValue($gradeitemid);
216                 // These numbers are always in the format 0.00000 - the rtrims remove any final zeros and,
217                 // if it is a whole number, the decimal place.
218                 $groupelements[2]->setValue(is_null($minmax->min)?'':rtrim(rtrim($minmax->min,'0'),'.'));
219                 $groupelements[4]->setValue(is_null($minmax->max)?'':rtrim(rtrim($minmax->max,'0'),'.'));
220                 $num++;
221             }
223             if ($completion->is_enabled()) {
224                 $num=0;
225                 foreach($fullcm->conditionscompletion as $othercmid=>$state) {
226                     $groupelements=$mform->getElement('conditioncompletiongroup['.$num.']')->getElements();
227                     $groupelements[0]->setValue($othercmid);
228                     $groupelements[1]->setValue($state);
229                     $num++;
230                 }
231             }
232         }
233     }
235     // form verification
236     function validation($data, $files) {
237         global $COURSE, $DB;
238         $errors = parent::validation($data, $files);
240         $mform =& $this->_form;
242         $errors = array();
244         if ($mform->elementExists('name')) {
245             $name = trim($data['name']);
246             if ($name == '') {
247                 $errors['name'] = get_string('required');
248             }
249         }
251         $grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$data['modulename'],
252                      'iteminstance'=>$data['instance'], 'itemnumber'=>0, 'courseid'=>$COURSE->id));
253         if ($data['coursemodule']) {
254             $cm = $DB->get_record('course_modules', array('id'=>$data['coursemodule']));
255         } else {
256             $cm = null;
257         }
259         if ($mform->elementExists('cmidnumber')) {
260             // verify the idnumber
261             if (!grade_verify_idnumber($data['cmidnumber'], $COURSE->id, $grade_item, $cm)) {
262                 $errors['cmidnumber'] = get_string('idnumbertaken');
263             }
264         }
265         
266         // Completion: Don't let them choose automatic completion without turning
267         // on some conditions
268         if (array_key_exists('completion', $data) && $data['completion']==COMPLETION_TRACKING_AUTOMATIC) {
269             if (empty($data['completionview']) && empty($data['completionusegrade']) &&
270                 !$this->completion_rule_enabled($data)) {
271                 $errors['completion'] = get_string('badautocompletion', 'completion');
272             }
273         }
275         // Conditions: Don't let them set dates which make no sense
276         if (array_key_exists('availablefrom', $data) && 
277             $data['availablefrom'] && $data['availableuntil'] && 
278             $data['availablefrom']>=$data['availableuntil']) {
279             $errors['availablefrom'] = get_string('badavailabledates', 'condition');
280         }
281         
282         return $errors;
283     }
285     /**
286      * Load in existing data as form defaults. Usually new entry defaults are stored directly in
287      * form definition (new entry form); this function is used to load in data where values
288      * already exist and data is being edited (edit entry form).
289      *
290      * @param mixed $default_values object or array of default values
291      */
292     function set_data($default_values) {
293         if (is_object($default_values)) {
294             $default_values = (array)$default_values;
295         }
297         $this->data_preprocessing($default_values);        
298         parent::set_data($default_values);
299     }
301     /**
302      * Adds all the standard elements to a form to edit the settings for an activity module.
303      */
304     function standard_coursemodule_elements(){
305         global $COURSE, $CFG, $DB;
306         $mform =& $this->_form;
308         $outcomesused = false;
309         if ($this->_features->outcomes) {
310             if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
311                 $outcomesused = true;
312                 $mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades'));
313                 foreach($outcomes as $outcome) {
314                     $mform->addElement('advcheckbox', 'outcome_'.$outcome->id, $outcome->get_name());
315                 }
316             }
317         }
319         $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
320         if ($this->_features->groups) {
321             $options = array(NOGROUPS       => get_string('groupsnone'),
322                              SEPARATEGROUPS => get_string('groupsseparate'),
323                              VISIBLEGROUPS  => get_string('groupsvisible'));
324             $mform->addElement('select', 'groupmode', get_string('groupmode'), $options, NOGROUPS);
325             $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')));
326         }
328         if (!empty($CFG->enablegroupings)) {
329             if ($this->_features->groupings or $this->_features->groupmembersonly) {
330                 //groupings selector - used for normal grouping mode or also when restricting access with groupmembersonly
331                 $options = array();
332                 $options[0] = get_string('none');
333                 if ($groupings = $DB->get_records('groupings', array('courseid'=>$COURSE->id))) {
334                     foreach ($groupings as $grouping) {
335                         $options[$grouping->id] = format_string($grouping->name);
336                     }
337                 }
338                 $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options);
339                 $mform->setHelpButton('groupingid', array('grouping', get_string('grouping', 'group')));
340                 $mform->setAdvanced('groupingid');
341             }
343             if ($this->_features->groupmembersonly) {
344                 $mform->addElement('checkbox', 'groupmembersonly', get_string('groupmembersonly', 'group'));
345                 $mform->setHelpButton('groupmembersonly', array('groupmembersonly', get_string('groupmembersonly', 'group')));
346                 $mform->setAdvanced('groupmembersonly');
347             }
348         }
350         $mform->addElement('modvisible', 'visible', get_string('visible'));
352         if ($this->_features->idnumber) {
353             $mform->addElement('text', 'cmidnumber', get_string('idnumbermod'));
354             $mform->setHelpButton('cmidnumber', array('cmidnumber', get_string('idnumbermod')), true);
355         }
357         if ($this->_features->gradecat) {
358             $categories = grade_get_categories_menu($COURSE->id, $outcomesused);
359             $mform->addElement('select', 'gradecat', get_string('gradecategory', 'grades'), $categories);
360         }
362         if (!empty($CFG->enableavailability)) {
363             // Conditional availability
364             $mform->addElement('header', '', get_string('availabilityconditions', 'condition'));
365             $mform->addElement('date_selector', 'availablefrom', get_string('availablefrom', 'condition'), array('optional'=>true));
366             $mform->setHelpButton('availablefrom', array('conditiondates', get_string('help_conditiondates', 'condition'), 'condition'));
367             $mform->addElement('date_selector', 'availableuntil', get_string('availableuntil', 'condition'), array('optional'=>true));
368             $mform->setHelpButton('availableuntil', array('conditiondates', get_string('help_conditiondates', 'condition'), 'condition'));
370             // Conditions based on grades
371             $gradeoptions = array();
372             $items = grade_item::fetch_all(array('courseid'=>$COURSE->id));
373             $items = $items ? $items : array();
374             foreach($items as $id=>$item) {
375                 // Do not include grades for current item
376                 if (!empty($this->_cm) && $this->_cm->instance == $item->iteminstance
377                     && $this->_cm->modname == $item->itemmodule
378                     && $item->itemtype == 'mod') {
379                     continue;
380                 }
381                 $gradeoptions[$id] = $item->get_name();
382             }
383             asort($gradeoptions);
384             $gradeoptions = array(0=>get_string('none','condition'))+$gradeoptions;
386             $grouparray = array();
387             $grouparray[] =& $mform->createElement('select','conditiongradeitemid','',$gradeoptions);
388             $grouparray[] =& $mform->createElement('static', '', '',' '.get_string('grade_atleast','condition').' ');
389             $grouparray[] =& $mform->createElement('text', 'conditiongrademin','',array('size'=>3));
390             $grouparray[] =& $mform->createElement('static', '', '','% '.get_string('grade_upto','condition').' ');
391             $grouparray[] =& $mform->createElement('text', 'conditiongrademax','',array('size'=>3));
392             $grouparray[] =& $mform->createElement('static', '', '','%');
393             $mform->setType('conditiongrademin',PARAM_FLOAT);            
394             $mform->setType('conditiongrademax',PARAM_FLOAT);            
395             $group = $mform->createElement('group','conditiongradegroup', 
396                 get_string('gradecondition', 'condition'),$grouparray);
398             // Get version with condition info and store it so we don't ask
399             // twice
400             if(!empty($this->_cm)) {           
401                 $ci = new condition_info($this->_cm, CONDITION_MISSING_EXTRATABLE);
402                 $this->_cm = $ci->get_full_course_module();
403                 $count = count($this->_cm->conditionsgrade)+1;
404             } else {
405                 $count = 1;
406             }
408             $this->repeat_elements(array($group), $count, array(), 'conditiongraderepeats', 'conditiongradeadds', 2,
409                                    get_string('addgrades', 'condition'), true);
410             $mform->setHelpButton('conditiongradegroup[0]', array('gradecondition', get_string('help_gradecondition', 'condition'), 'condition'));
412             // Conditions based on completion
413             $completion = new completion_info($COURSE);
414             if ($completion->is_enabled()) {
415                 $completionoptions = array();
416                 $modinfo = get_fast_modinfo($COURSE);
417                 foreach($modinfo->cms as $id=>$cm) {
418                     // Add each course-module if it:
419                     // (a) has completion turned on
420                     // (b) is not the same as current course-module
421                     if ($cm->completion && (empty($this->_cm) || $this->_cm->id != $id)) {
422                         $completionoptions[$id]=$cm->name;
423                     }
424                 }
425                 asort($completionoptions);
426                 $completionoptions = array(0=>get_string('none','condition'))+$completionoptions;
428                 $completionvalues=array(
429                     COMPLETION_COMPLETE=>get_string('completion_complete','condition'),
430                     COMPLETION_INCOMPLETE=>get_string('completion_incomplete','condition'),
431                     COMPLETION_COMPLETE_PASS=>get_string('completion_pass','condition'),
432                     COMPLETION_COMPLETE_FAIL=>get_string('completion_fail','condition'));
434                 $grouparray = array();        
435                 $grouparray[] =& $mform->createElement('select','conditionsourcecmid','',$completionoptions);
436                 $grouparray[] =& $mform->createElement('select','conditionrequiredcompletion','',$completionvalues);
437                 $group = $mform->createElement('group','conditioncompletiongroup', 
438                     get_string('completioncondition', 'condition'),$grouparray);
440                 $count = empty($this->_cm) ? 1 : count($this->_cm->conditionscompletion)+1;
441                 $this->repeat_elements(array($group),$count,array(),
442                     'conditioncompletionrepeats','conditioncompletionadds',2,
443                     get_string('addcompletions','condition'),true);
444                 $mform->setHelpButton('conditioncompletiongroup[0]', array('completioncondition', get_string('help_completioncondition', 'condition'), 'condition'));
445             }
447             // Do we display availability info to students?        
448             $mform->addElement('select', 'showavailability', get_string('showavailability', 'condition'), 
449                     array(CONDITION_STUDENTVIEW_SHOW=>get_string('showavailability_show', 'condition'), 
450                     CONDITION_STUDENTVIEW_HIDE=>get_string('showavailability_hide', 'condition')));
451             $mform->setDefault('showavailability', CONDITION_STUDENTVIEW_SHOW);                
452             $mform->setHelpButton('showavailability', array('showavailability', get_string('help_showavailability', 'condition'), 'condition'));
453         }
455         // Conditional activities: completion tracking section 
456         if(!isset($completion)) {
457             $completion = new completion_info($COURSE);
458         }
459         if ($completion->is_enabled()) {
460             $mform->addElement('header', '', get_string('activitycompletion', 'completion'));
462             // Unlock button for if people have completed it (will
463             // be removed in definition_after_data if they haven't)
464             $mform->addElement('submit', 'unlockcompletion', get_string('unlockcompletion', 'completion'));
465             $mform->registerNoSubmitButton('unlockcompletion');
466             $mform->addElement('hidden', 'completionunlocked', 0);
468             $mform->addElement('select', 'completion', get_string('completion', 'completion'), 
469                 array(COMPLETION_TRACKING_NONE=>get_string('completion_none', 'completion'), 
470                 COMPLETION_TRACKING_MANUAL=>get_string('completion_manual', 'completion')));
471             $mform->setHelpButton('completion', array('completion', get_string('help_completion', 'completion'), 'completion'));
472             $mform->setDefault('completion', $this->_features->defaultcompletion
473                 ? COMPLETION_TRACKING_MANUAL
474                 : COMPLETION_TRACKING_NONE);
476             // Automatic completion once you view it
477             $gotcompletionoptions = false;
478             if (plugin_supports('mod', $modname, FEATURE_COMPLETION_TRACKS_VIEWS, false)) {
479                 $mform->addElement('checkbox', 'completionview', get_string('completionview', 'completion'),
480                     get_string('completionview_text', 'completion'));
481                 $mform->setHelpButton('completionview', array('completionview', get_string('help_completionview', 'completion'), 'completion'));
482                 $mform->disabledIf('completionview', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
483                 $gotcompletionoptions = true;
484             }
486             // Automatic completion once it's graded
487             if (plugin_supports('mod', $modname, FEATURE_GRADE_HAS_GRADE, false)) {
488                 $mform->addElement('checkbox', 'completionusegrade', get_string('completionusegrade', 'completion'),
489                     get_string('completionusegrade_text', 'completion'));
490                 $mform->setHelpButton('completionusegrade', array('completionusegrade', get_string('help_completionusegrade', 'completion'), 'completion'));
491                 $mform->disabledIf('completionusegrade', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
492                 $gotcompletionoptions = true;
493             }
495             // Automatic completion according to module-specific rules
496             $this->_customcompletionelements = $this->add_completion_rules();
497             foreach ($this->_customcompletionelements as $element) {
498                 $mform->disabledIf($element, 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);                
499             }
501             $gotcompletionoptions = $gotcompletionoptions ||
502                 count($this->_customcompletionelements)>0;
504             // Automatic option only appears if possible
505             if ($gotcompletionoptions) {
506                 $mform->getElement('completion')->addOption(
507                     get_string('completion_automatic', 'completion'),
508                     COMPLETION_TRACKING_AUTOMATIC);
509             } 
511             // Completion expected at particular date? (For progress tracking)
512             $mform->addElement('date_selector', 'completionexpected', get_string('completionexpected', 'completion'), array('optional'=>true));
513             $mform->setHelpButton('completionexpected', array('completionexpected', get_string('help_completionexpected', 'completion'), 'completion'));
514             $mform->disabledIf('completionexpected', 'completion', 'eq', COMPLETION_TRACKING_NONE);    
515         }
517         $this->standard_hidden_coursemodule_elements();
518     }
519     
520     /**
521      * Can be overridden to add custom completion rules if the module wishes
522      * them. If overriding this, you should also override completion_rule_enabled.
523      * <p>
524      * Just add elements to the form as needed and return the list of IDs. The
525      * system will call disabledIf and handle other behaviour for each returned
526      * ID.
527      * @return array Array of string IDs of added items, empty array if none
528      */
529     function add_completion_rules() {
530         return array();
531     }
533     /**
534      * Called during validation. Override to indicate, based on the data, whether
535      * a custom completion rule is enabled (selected).
536      *
537      * @param array $data Input data (not yet validated)
538      * @return bool True if one or more rules is enabled, false if none are;
539      *   default returns false
540      */
541     function completion_rule_enabled(&$data) {
542         return false;
543     }
545     function standard_hidden_coursemodule_elements(){
546         $mform =& $this->_form;
547         $mform->addElement('hidden', 'course', 0);
548         $mform->setType('course', PARAM_INT);
550         $mform->addElement('hidden', 'coursemodule', 0);
551         $mform->setType('coursemodule', PARAM_INT);
553         $mform->addElement('hidden', 'section', 0);
554         $mform->setType('section', PARAM_INT);
556         $mform->addElement('hidden', 'module', 0);
557         $mform->setType('module', PARAM_INT);
559         $mform->addElement('hidden', 'modulename', '');
560         $mform->setType('modulename', PARAM_SAFEDIR);
562         $mform->addElement('hidden', 'instance', 0);
563         $mform->setType('instance', PARAM_INT);
565         $mform->addElement('hidden', 'add', 0);
566         $mform->setType('add', PARAM_ALPHA);
568         $mform->addElement('hidden', 'update', 0);
569         $mform->setType('update', PARAM_INT);
571         $mform->addElement('hidden', 'return', 0);
572         $mform->setType('return', PARAM_BOOL);
573     }
575     /**
576      * Overriding formslib's add_action_buttons() method, to add an extra submit "save changes and return" button.
577      *
578      * @param bool $cancel show cancel button
579      * @param string $submitlabel null means default, false means none, string is label text
580      * @param string $submit2label  null means default, false means none, string is label text
581      * @return void
582      */
583     function add_action_buttons($cancel=true, $submitlabel=null, $submit2label=null) {
584         if (is_null($submitlabel)) {
585             $submitlabel = get_string('savechangesanddisplay');
586         }
588         if (is_null($submit2label)) {
589             $submit2label = get_string('savechangesandreturntocourse');
590         }
592         $mform =& $this->_form;
594         // elements in a row need a group
595         $buttonarray = array();
597         if ($submit2label !== false) {
598             $buttonarray[] = &$mform->createElement('submit', 'submitbutton2', $submit2label);
599         }
601         if ($submitlabel !== false) {
602             $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
603         }
605         if ($cancel) {
606             $buttonarray[] = &$mform->createElement('cancel');
607         }
609         $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
610         $mform->setType('buttonar', PARAM_RAW);
611         $mform->closeHeaderBefore('buttonar');
612     }
615 ?>