68b005ae3d14feb4bc86ed7a3e73e7e4d5c34d38
[moodle.git] / course / moodleform_mod.php
1 <?php
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 replaced deprecated mod/{modname}/mod.html
10  */
11 abstract class moodleform_mod extends moodleform {
12     /** Current data */
13     protected $current;
14     /**
15      * Instance of the module that is being updated. This is the id of the {prefix}{modulename}
16      * record. Can be used in form definition. Will be "" if this is an 'add' form and not an
17      * update one.
18      *
19      * @var mixed
20      */
21     protected $_instance;
22     /**
23      * Section of course that module instance will be put in or is in.
24      * This is always the section number itself (column 'section' from 'course_sections' table).
25      *
26      * @var mixed
27      */
28     protected $_section;
29     /**
30      * Coursemodle record of the module that is being updated. Will be null if this is an 'add' form and not an
31      * update one.
32       *
33      * @var mixed
34      */
35     protected $_cm;
36     /**
37      * List of modform features
38      */
39     protected $_features;
40     /**
41      * @var array Custom completion-rule elements, if enabled
42      */
43     protected $_customcompletionelements;
44     /**
45      * @var string name of module
46      */
47     protected $_modname;
48     /** current context, course or module depends if already exists*/
49     protected $context;
51     /** a flag indicating whether outcomes are being used*/
52     protected $_outcomesused;
54     function moodleform_mod($current, $section, $cm, $course) {
55         $this->current   = $current;
56         $this->_instance = $current->instance;
57         $this->_section  = $section;
58         $this->_cm       = $cm;
59         if ($this->_cm) {
60             $this->context = get_context_instance(CONTEXT_MODULE, $this->_cm->id);
61         } else {
62             $this->context = get_context_instance(CONTEXT_COURSE, $course->id);
63         }
65         // Guess module name
66         $matches = array();
67         if (!preg_match('/^mod_([^_]+)_mod_form$/', get_class($this), $matches)) {
68             debugging('Use $modname parameter or rename form to mod_xx_mod_form, where xx is name of your module');
69             print_error('unknownmodulename');
70         }
71         $this->_modname = $matches[1];
72         $this->init_features();
73         parent::moodleform('modedit.php');
74     }
76     protected function init_features() {
77         global $CFG;
79         $this->_features = new object();
80         $this->_features->groups            = plugin_supports('mod', $this->_modname, FEATURE_GROUPS, true);
81         $this->_features->groupings         = plugin_supports('mod', $this->_modname, FEATURE_GROUPINGS, false);
82         $this->_features->groupmembersonly  = plugin_supports('mod', $this->_modname, FEATURE_GROUPMEMBERSONLY, false);
83         $this->_features->outcomes          = (!empty($CFG->enableoutcomes) and plugin_supports('mod', $this->_modname, FEATURE_GRADE_OUTCOMES, true));
84         $this->_features->hasgrades         = plugin_supports('mod', $this->_modname, FEATURE_GRADE_HAS_GRADE, false);
85         $this->_features->idnumber          = plugin_supports('mod', $this->_modname, FEATURE_IDNUMBER, true);
86         $this->_features->introeditor       = plugin_supports('mod', $this->_modname, FEATURE_MOD_INTRO, true);
87         $this->_features->defaultcompletion = plugin_supports('mod', $this->_modname, FEATURE_MODEDIT_DEFAULT_COMPLETION, true);
88         $this->_features->rating            = plugin_supports('mod', $this->_modname, FEATURE_RATE, false);
90         $this->_features->gradecat          = ($this->_features->outcomes or $this->_features->hasgrades);
91     }
93     /**
94      * Only available on moodleform_mod.
95      *
96      * @param array $default_values passed by reference
97      */
98     function data_preprocessing(&$default_values){
99         if (empty($default_values['scale'])) {
100             $default_values['assessed'] = 0;
101         }
103         if (empty($default_values['assessed'])){
104             $default_values['ratingtime'] = 0;
105         } else {
106             $default_values['ratingtime']=
107                 ($default_values['assesstimestart'] && $default_values['assesstimefinish']) ? 1 : 0;
108         }
109     }
111     /**
112      * Each module which defines definition_after_data() must call this method using parent::definition_after_data();
113      */
114     function definition_after_data() {
115         global $CFG, $COURSE;
116         $mform =& $this->_form;
118         if ($id = $mform->getElementValue('update')) {
119             $modulename = $mform->getElementValue('modulename');
120             $instance   = $mform->getElementValue('instance');
122             if ($this->_features->gradecat) {
123                 $gradecat = false;
124                 if (!empty($CFG->enableoutcomes) and $this->_features->outcomes) {
125                     $outcomes = grade_outcome::fetch_all_available($COURSE->id);
126                     if (!empty($outcomes)) {
127                         $gradecat = true;
128                     }
129                 }
131                 $items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename,'iteminstance'=>$instance, 'courseid'=>$COURSE->id));
132                 //will be no items if, for example, this activity supports ratings but rating aggregate type == no ratings
133                 if (!empty($items)) {
134                     foreach ($items as $item) {
135                         if (!empty($item->outcomeid)) {
136                             $elname = 'outcome_'.$item->outcomeid;
137                             if ($mform->elementExists($elname)) {
138                                 $mform->hardFreeze($elname); // prevent removing of existing outcomes
139                             }
140                         }
141                     }
143                     foreach ($items as $item) {
144                         if (is_bool($gradecat)) {
145                             $gradecat = $item->categoryid;
146                             continue;
147                         }
148                         if ($gradecat != $item->categoryid) {
149                             //mixed categories
150                             $gradecat = false;
151                             break;
152                         }
153                     }
154                 }
156                 if ($gradecat === false) {
157                     // items and outcomes in different categories - remove the option
158                     // TODO: add a "Mixed categories" text instead of removing elements with no explanation
159                     if ($mform->elementExists('gradecat')) {
160                         $mform->removeElement('gradecat');
161                         if ($this->_features->rating) {
162                             //if supports ratings then the max grade dropdown wasnt added so the grade box can be removed entirely
163                             $mform->removeElement('modstandardgrade');
164                         }
165                     }
166                 }
167             }
168         }
170         if ($COURSE->groupmodeforce) {
171             if ($mform->elementExists('groupmode')) {
172                 $mform->hardFreeze('groupmode'); // groupmode can not be changed if forced from course settings
173             }
174         }
176         if ($mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly') and empty($COURSE->groupmodeforce)) {
177             $mform->disabledIf('groupingid', 'groupmode', 'eq', NOGROUPS);
179         } else if (!$mform->elementExists('groupmode') and $mform->elementExists('groupmembersonly')) {
180             $mform->disabledIf('groupingid', 'groupmembersonly', 'notchecked');
182         } else if (!$mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly')) {
183             // groupings have no use without groupmode or groupmembersonly
184             if ($mform->elementExists('groupingid')) {
185                 $mform->removeElement('groupingid');
186             }
187         }
189         // Completion: If necessary, freeze fields
190         $completion = new completion_info($COURSE);
191         if ($completion->is_enabled()) {
192             // If anybody has completed the activity, these options will be 'locked'
193             $completedcount = empty($this->_cm)
194                 ? 0
195                 : $completion->count_user_data($this->_cm);
197             $freeze = false;
198             if (!$completedcount) {
199                 if ($mform->elementExists('unlockcompletion')) {
200                     $mform->removeElement('unlockcompletion');
201                 }
202             } else {
203                 // Has the element been unlocked?
204                 if ($mform->exportValue('unlockcompletion')) {
205                     // Yes, add in warning text and set the hidden variable
206                     $mform->insertElementBefore(
207                         $mform->createElement('static', 'completedunlocked',
208                             get_string('completedunlocked', 'completion'),
209                             get_string('completedunlockedtext', 'completion')),
210                         'unlockcompletion');
211                     $mform->removeElement('unlockcompletion');
212                     $mform->getElement('completionunlocked')->setValue(1);
213                 } else {
214                     // No, add in the warning text with the count (now we know
215                     // it) before the unlock button
216                     $mform->insertElementBefore(
217                         $mform->createElement('static', 'completedwarning',
218                             get_string('completedwarning', 'completion'),
219                             get_string('completedwarningtext', 'completion', $completedcount)),
220                         'unlockcompletion');
221                     $freeze = true;
222                 }
223             }
225             if ($freeze) {
226                 $mform->freeze('completion');
227                 if ($mform->elementExists('completionview')) {
228                     $mform->freeze('completionview'); // don't use hardFreeze or checkbox value gets lost
229                 }
230                 if ($mform->elementExists('completionusegrade')) {
231                     $mform->freeze('completionusegrade');
232                 }
233                 $mform->freeze($this->_customcompletionelements);
234             }
235         }
237         // Availability conditions
238         if (!empty($CFG->enableavailability) && $this->_cm) {
239             $ci = new condition_info($this->_cm);
240             $fullcm=$ci->get_full_course_module();
242             $num=0;
243             foreach($fullcm->conditionsgrade as $gradeitemid=>$minmax) {
244                 $groupelements=$mform->getElement('conditiongradegroup['.$num.']')->getElements();
245                 $groupelements[0]->setValue($gradeitemid);
246                 // These numbers are always in the format 0.00000 - the rtrims remove any final zeros and,
247                 // if it is a whole number, the decimal place.
248                 $groupelements[2]->setValue(is_null($minmax->min)?'':rtrim(rtrim($minmax->min,'0'),'.'));
249                 $groupelements[4]->setValue(is_null($minmax->max)?'':rtrim(rtrim($minmax->max,'0'),'.'));
250                 $num++;
251             }
253             if ($completion->is_enabled()) {
254                 $num=0;
255                 foreach($fullcm->conditionscompletion as $othercmid=>$state) {
256                     $groupelements=$mform->getElement('conditioncompletiongroup['.$num.']')->getElements();
257                     $groupelements[0]->setValue($othercmid);
258                     $groupelements[1]->setValue($state);
259                     $num++;
260                 }
261             }
262         }
263     }
265     // form verification
266     function validation($data, $files) {
267         global $COURSE, $DB;
268         $errors = parent::validation($data, $files);
270         $mform =& $this->_form;
272         $errors = array();
274         if ($mform->elementExists('name')) {
275             $name = trim($data['name']);
276             if ($name == '') {
277                 $errors['name'] = get_string('required');
278             }
279         }
281         $grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$data['modulename'],
282                      'iteminstance'=>$data['instance'], 'itemnumber'=>0, 'courseid'=>$COURSE->id));
283         if ($data['coursemodule']) {
284             $cm = $DB->get_record('course_modules', array('id'=>$data['coursemodule']));
285         } else {
286             $cm = null;
287         }
289         if ($mform->elementExists('cmidnumber')) {
290             // verify the idnumber
291             if (!grade_verify_idnumber($data['cmidnumber'], $COURSE->id, $grade_item, $cm)) {
292                 $errors['cmidnumber'] = get_string('idnumbertaken');
293             }
294         }
296         // Completion: Don't let them choose automatic completion without turning
297         // on some conditions
298         if (array_key_exists('completion', $data) && $data['completion']==COMPLETION_TRACKING_AUTOMATIC) {
299             if (empty($data['completionview']) && empty($data['completionusegrade']) &&
300                 !$this->completion_rule_enabled($data)) {
301                 $errors['completion'] = get_string('badautocompletion', 'completion');
302             }
303         }
305         // Conditions: Don't let them set dates which make no sense
306         if (array_key_exists('availablefrom', $data) &&
307             $data['availablefrom'] && $data['availableuntil'] &&
308             $data['availablefrom'] > $data['availableuntil']) {
309             $errors['availablefrom'] = get_string('badavailabledates', 'condition');
310         }
312         return $errors;
313     }
315     /**
316      * Load in existing data as form defaults. Usually new entry defaults are stored directly in
317      * form definition (new entry form); this function is used to load in data where values
318      * already exist and data is being edited (edit entry form).
319      *
320      * @param mixed $default_values object or array of default values
321      */
322     function set_data($default_values) {
323         if (is_object($default_values)) {
324             $default_values = (array)$default_values;
325         }
327         $this->data_preprocessing($default_values);
328         parent::set_data($default_values);
329     }
331     /**
332      * Adds all the standard elements to a form to edit the settings for an activity module.
333      */
334     function standard_coursemodule_elements(){
335         global $COURSE, $CFG, $DB;
336         $mform =& $this->_form;
338         $this->_outcomesused = false;
339         if ($this->_features->outcomes) {
340             if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
341                 $this->_outcomesused = true;
342                 $mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades'));
343                 foreach($outcomes as $outcome) {
344                     $mform->addElement('advcheckbox', 'outcome_'.$outcome->id, $outcome->get_name());
345                 }
346             }
347         }
349         
350         if ($this->_features->rating) {
351             require_once($CFG->dirroot.'/rating/lib.php');
352             $rm = new rating_manager();
354             $mform->addElement('header', 'modstandardratings', get_string('ratings', 'rating'));
356             $permission=null;
357             $context = get_context_instance(CONTEXT_MODULE, $this->_cm->id);
359             $roles1 = get_roles_with_capability('moodle/rating:rate', $permission, $context);
360             $roles2 = get_roles_with_capability('mod/'.$this->_cm->modname.':rate', $permission, $context);
362             $rolesthatcanrate = array();
363             foreach($roles1 as $k1=>$v1) {
364                 if (array_key_exists($k1, $roles2)) {
365                     $rolesthatcanrate[] = $v1->name;
366                 }
367             }
369             $mform->addElement('static', 'rolewarning', '', get_string('rolewarning','rating', implode(', ', $rolesthatcanrate)));
371             $mform->addElement('select', 'assessed', get_string('aggregatetype', 'rating') , $rm->get_aggregate_types());
372             $mform->setDefault('assessed', 0);
373             $mform->addHelpButton('assessed', 'aggregatetype', 'forum');
375             $mform->addElement('modgrade', 'scale', get_string('scale'), false);
376             $mform->disabledIf('scale', 'assessed', 'eq', 0);
378             $mform->addElement('checkbox', 'ratingtime', get_string('ratingtime', 'forum'));
379             $mform->disabledIf('ratingtime', 'assessed', 'eq', 0);
381             $mform->addElement('date_time_selector', 'assesstimestart', get_string('from'));
382             $mform->disabledIf('assesstimestart', 'assessed', 'eq', 0);
383             $mform->disabledIf('assesstimestart', 'ratingtime');
385             $mform->addElement('date_time_selector', 'assesstimefinish', get_string('to'));
386             $mform->disabledIf('assesstimefinish', 'assessed', 'eq', 0);
387             $mform->disabledIf('assesstimefinish', 'ratingtime');
388         }
390         //doing this here means splitting up the grade related settings on the lesson settings page
391         //$this->standard_grading_coursemodule_elements();
393         $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
394         if ($this->_features->groups) {
395             $options = array(NOGROUPS       => get_string('groupsnone'),
396                              SEPARATEGROUPS => get_string('groupsseparate'),
397                              VISIBLEGROUPS  => get_string('groupsvisible'));
398             $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $options, NOGROUPS);
399             $mform->addHelpButton('groupmode', 'groupmode', 'group');
400         }
402         if ($this->_features->groupings or $this->_features->groupmembersonly) {
403             //groupings selector - used for normal grouping mode or also when restricting access with groupmembersonly
404             $options = array();
405             $options[0] = get_string('none');
406             if ($groupings = $DB->get_records('groupings', array('courseid'=>$COURSE->id))) {
407                 foreach ($groupings as $grouping) {
408                     $options[$grouping->id] = format_string($grouping->name);
409                 }
410             }
411             $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options);
412             $mform->addHelpButton('groupingid', 'grouping', 'group');
413             $mform->setAdvanced('groupingid');
414         }
416         if ($this->_features->groupmembersonly) {
417             $mform->addElement('checkbox', 'groupmembersonly', get_string('groupmembersonly', 'group'));
418             $mform->addHelpButton('groupmembersonly', 'groupmembersonly', 'group');
419             $mform->setAdvanced('groupmembersonly');
420         }
422         $mform->addElement('modvisible', 'visible', get_string('visible'));
424         if ($this->_features->idnumber) {
425             $mform->addElement('text', 'cmidnumber', get_string('idnumbermod'));
426             $mform->addHelpButton('cmidnumber', 'idnumbermod');
427         }
429         if (!empty($CFG->enableavailability)) {
430             // Conditional availability
431             $mform->addElement('header', '', get_string('availabilityconditions', 'condition'));
432             $mform->addElement('date_selector', 'availablefrom', get_string('availablefrom', 'condition'), array('optional'=>true));
433             $mform->addElement('date_selector', 'availableuntil', get_string('availableuntil', 'condition'), array('optional'=>true));
435             // Conditions based on grades
436             $gradeoptions = array();
437             $items = grade_item::fetch_all(array('courseid'=>$COURSE->id));
438             $items = $items ? $items : array();
439             foreach($items as $id=>$item) {
440                 // Do not include grades for current item
441                 if (!empty($this->_cm) && $this->_cm->instance == $item->iteminstance
442                     && $this->_cm->modname == $item->itemmodule
443                     && $item->itemtype == 'mod') {
444                     continue;
445                 }
446                 $gradeoptions[$id] = $item->get_name();
447             }
448             asort($gradeoptions);
449             $gradeoptions = array(0=>get_string('none','condition'))+$gradeoptions;
451             $grouparray = array();
452             $grouparray[] =& $mform->createElement('select','conditiongradeitemid','',$gradeoptions);
453             $grouparray[] =& $mform->createElement('static', '', '',' '.get_string('grade_atleast','condition').' ');
454             $grouparray[] =& $mform->createElement('text', 'conditiongrademin','',array('size'=>3));
455             $grouparray[] =& $mform->createElement('static', '', '','% '.get_string('grade_upto','condition').' ');
456             $grouparray[] =& $mform->createElement('text', 'conditiongrademax','',array('size'=>3));
457             $grouparray[] =& $mform->createElement('static', '', '','%');
458             $mform->setType('conditiongrademin',PARAM_FLOAT);
459             $mform->setType('conditiongrademax',PARAM_FLOAT);
460             $group = $mform->createElement('group','conditiongradegroup',
461                 get_string('gradecondition', 'condition'),$grouparray);
463             // Get version with condition info and store it so we don't ask
464             // twice
465             if(!empty($this->_cm)) {
466                 $ci = new condition_info($this->_cm, CONDITION_MISSING_EXTRATABLE);
467                 $this->_cm = $ci->get_full_course_module();
468                 $count = count($this->_cm->conditionsgrade)+1;
469             } else {
470                 $count = 1;
471             }
473             $this->repeat_elements(array($group), $count, array(), 'conditiongraderepeats', 'conditiongradeadds', 2,
474                                    get_string('addgrades', 'condition'), true);
475             $mform->addHelpButton('conditiongradegroup[0]', 'gradecondition', 'condition');
477             // Conditions based on completion
478             $completion = new completion_info($COURSE);
479             if ($completion->is_enabled()) {
480                 $completionoptions = array();
481                 $modinfo = get_fast_modinfo($COURSE);
482                 foreach($modinfo->cms as $id=>$cm) {
483                     // Add each course-module if it:
484                     // (a) has completion turned on
485                     // (b) is not the same as current course-module
486                     if ($cm->completion && (empty($this->_cm) || $this->_cm->id != $id)) {
487                         $completionoptions[$id]=$cm->name;
488                     }
489                 }
490                 asort($completionoptions);
491                 $completionoptions = array(0=>get_string('none','condition'))+$completionoptions;
493                 $completionvalues=array(
494                     COMPLETION_COMPLETE=>get_string('completion_complete','condition'),
495                     COMPLETION_INCOMPLETE=>get_string('completion_incomplete','condition'),
496                     COMPLETION_COMPLETE_PASS=>get_string('completion_pass','condition'),
497                     COMPLETION_COMPLETE_FAIL=>get_string('completion_fail','condition'));
499                 $grouparray = array();
500                 $grouparray[] =& $mform->createElement('select','conditionsourcecmid','',$completionoptions);
501                 $grouparray[] =& $mform->createElement('select','conditionrequiredcompletion','',$completionvalues);
502                 $group = $mform->createElement('group','conditioncompletiongroup',
503                     get_string('completioncondition', 'condition'),$grouparray);
505                 $count = empty($this->_cm) ? 1 : count($this->_cm->conditionscompletion)+1;
506                 $this->repeat_elements(array($group),$count,array(),
507                     'conditioncompletionrepeats','conditioncompletionadds',2,
508                     get_string('addcompletions','condition'),true);
509                 $mform->addHelpButton('conditioncompletiongroup[0]', 'completioncondition', 'condition');
510             }
512             // Do we display availability info to students?
513             $mform->addElement('select', 'showavailability', get_string('showavailability', 'condition'),
514                     array(CONDITION_STUDENTVIEW_SHOW=>get_string('showavailability_show', 'condition'),
515                     CONDITION_STUDENTVIEW_HIDE=>get_string('showavailability_hide', 'condition')));
516             $mform->setDefault('showavailability', CONDITION_STUDENTVIEW_SHOW);
517         }
519         // Conditional activities: completion tracking section
520         if(!isset($completion)) {
521             $completion = new completion_info($COURSE);
522         }
523         if ($completion->is_enabled()) {
524             $mform->addElement('header', '', get_string('activitycompletion', 'completion'));
526             // Unlock button for if people have completed it (will
527             // be removed in definition_after_data if they haven't)
528             $mform->addElement('submit', 'unlockcompletion', get_string('unlockcompletion', 'completion'));
529             $mform->registerNoSubmitButton('unlockcompletion');
530             $mform->addElement('hidden', 'completionunlocked', 0);
531             $mform->setType('completionunlocked', PARAM_INT);
533             $mform->addElement('select', 'completion', get_string('completion', 'completion'),
534                 array(COMPLETION_TRACKING_NONE=>get_string('completion_none', 'completion'),
535                 COMPLETION_TRACKING_MANUAL=>get_string('completion_manual', 'completion')));
536             $mform->setDefault('completion', $this->_features->defaultcompletion
537                 ? COMPLETION_TRACKING_MANUAL
538                 : COMPLETION_TRACKING_NONE);
539             $mform->addHelpButton('completion', 'completion', 'completion');
541             // Automatic completion once you view it
542             $gotcompletionoptions = false;
543             if (plugin_supports('mod', $this->_modname, FEATURE_COMPLETION_TRACKS_VIEWS, false)) {
544                 $mform->addElement('checkbox', 'completionview', get_string('completionview', 'completion'),
545                     get_string('completionview_desc', 'completion'));
546                 $mform->disabledIf('completionview', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
547                 $gotcompletionoptions = true;
548             }
550             // Automatic completion once it's graded
551             if (plugin_supports('mod', $this->_modname, FEATURE_GRADE_HAS_GRADE, false)) {
552                 $mform->addElement('checkbox', 'completionusegrade', get_string('completionusegrade', 'completion'),
553                     get_string('completionusegrade_desc', 'completion'));
554                 $mform->disabledIf('completionusegrade', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
555                 $mform->addHelpButton('completionusegrade', 'completionusegrade', 'completion');
556                 $gotcompletionoptions = true;
557             }
559             // Automatic completion according to module-specific rules
560             $this->_customcompletionelements = $this->add_completion_rules();
561             foreach ($this->_customcompletionelements as $element) {
562                 $mform->disabledIf($element, 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
563             }
565             $gotcompletionoptions = $gotcompletionoptions ||
566                 count($this->_customcompletionelements)>0;
568             // Automatic option only appears if possible
569             if ($gotcompletionoptions) {
570                 $mform->getElement('completion')->addOption(
571                     get_string('completion_automatic', 'completion'),
572                     COMPLETION_TRACKING_AUTOMATIC);
573             }
575             // Completion expected at particular date? (For progress tracking)
576             $mform->addElement('date_selector', 'completionexpected', get_string('completionexpected', 'completion'), array('optional'=>true));
577             $mform->addHelpButton('completionexpected', 'completionexpected', 'completion');
578             $mform->disabledIf('completionexpected', 'completion', 'eq', COMPLETION_TRACKING_NONE);
579         }
581         $this->standard_hidden_coursemodule_elements();
582     }
584     /**
585      * Can be overridden to add custom completion rules if the module wishes
586      * them. If overriding this, you should also override completion_rule_enabled.
587      * <p>
588      * Just add elements to the form as needed and return the list of IDs. The
589      * system will call disabledIf and handle other behaviour for each returned
590      * ID.
591      * @return array Array of string IDs of added items, empty array if none
592      */
593     function add_completion_rules() {
594         return array();
595     }
597     /**
598      * Called during validation. Override to indicate, based on the data, whether
599      * a custom completion rule is enabled (selected).
600      *
601      * @param array $data Input data (not yet validated)
602      * @return bool True if one or more rules is enabled, false if none are;
603      *   default returns false
604      */
605     function completion_rule_enabled(&$data) {
606         return false;
607     }
609     function standard_hidden_coursemodule_elements(){
610         $mform =& $this->_form;
611         $mform->addElement('hidden', 'course', 0);
612         $mform->setType('course', PARAM_INT);
614         $mform->addElement('hidden', 'coursemodule', 0);
615         $mform->setType('coursemodule', PARAM_INT);
617         $mform->addElement('hidden', 'section', 0);
618         $mform->setType('section', PARAM_INT);
620         $mform->addElement('hidden', 'module', 0);
621         $mform->setType('module', PARAM_INT);
623         $mform->addElement('hidden', 'modulename', '');
624         $mform->setType('modulename', PARAM_SAFEDIR);
626         $mform->addElement('hidden', 'instance', 0);
627         $mform->setType('instance', PARAM_INT);
629         $mform->addElement('hidden', 'add', 0);
630         $mform->setType('add', PARAM_ALPHA);
632         $mform->addElement('hidden', 'update', 0);
633         $mform->setType('update', PARAM_INT);
635         $mform->addElement('hidden', 'return', 0);
636         $mform->setType('return', PARAM_BOOL);
637     }
639     public function standard_grading_coursemodule_elements() {
640         global $COURSE, $CFG;
641         $mform =& $this->_form;
643         if ($this->_features->hasgrades) {
645             if (!$this->_features->rating || $this->_features->gradecat) {
646                 $mform->addElement('header', 'modstandardgrade', get_string('grade'));
647             }
649             //if supports grades and grades arent being handled via ratings
650             if (!$this->_features->rating) {
651                 $mform->addElement('modgrade', 'grade', get_string('grade'));
652                 $mform->setDefault('grade', 100);
653             }
655             if ($this->_features->gradecat) {
656                 $categories = grade_get_categories_menu($COURSE->id, $this->_outcomesused);
657                 $mform->addElement('select', 'gradecat', get_string('gradecategory', 'grades'), $categories);
658             }
659         }
660     }
662     function add_intro_editor($required=false, $customlabel=null) {
663         if (!$this->_features->introeditor) {
664             // intro editor not supported in this module
665             return;
666         }
668         $mform = $this->_form;
669         $label = is_null($customlabel) ? get_string('moduleintro') : $customlabel;
671         $mform->addElement('editor', 'introeditor', $label, null, array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$this->context));
672         $mform->setType('introeditor', PARAM_RAW); // no XSS prevention here, users must be trusted
673         if ($required) {
674             $mform->addRule('introeditor', get_string('required'), 'required', null, 'client');
675         }
676     }
678     /**
679      * Overriding formslib's add_action_buttons() method, to add an extra submit "save changes and return" button.
680      *
681      * @param bool $cancel show cancel button
682      * @param string $submitlabel null means default, false means none, string is label text
683      * @param string $submit2label  null means default, false means none, string is label text
684      * @return void
685      */
686     function add_action_buttons($cancel=true, $submitlabel=null, $submit2label=null) {
687         if (is_null($submitlabel)) {
688             $submitlabel = get_string('savechangesanddisplay');
689         }
691         if (is_null($submit2label)) {
692             $submit2label = get_string('savechangesandreturntocourse');
693         }
695         $mform = $this->_form;
697         // elements in a row need a group
698         $buttonarray = array();
700         if ($submit2label !== false) {
701             $buttonarray[] = &$mform->createElement('submit', 'submitbutton2', $submit2label);
702         }
704         if ($submitlabel !== false) {
705             $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
706         }
708         if ($cancel) {
709             $buttonarray[] = &$mform->createElement('cancel');
710         }
712         $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
713         $mform->setType('buttonar', PARAM_RAW);
714         $mform->closeHeaderBefore('buttonar');
715     }