MDL-15931 Using $CFG->unittestprefix now for filelib unit tests. MUCH slower but...
[moodle.git] / course / moodleform_mod.php
CommitLineData
61fceb86 1<?php //$Id$
e24b7f85 2require_once ($CFG->libdir.'/formslib.php');
42f103be 3if (!empty($CFG->enablecompletion) or !empty($CFG->enableavailability)) {
82bd6a5e 4 require_once($CFG->libdir.'/completionlib.php');
5}
61fceb86 6
e24b7f85 7/**
8 * This class adds extra methods to form wrapper specific to be used for module
7cac0c4b 9 * add / update forms (mod/{modname}.mod_form.php replaces deprecated mod/{modname}/mod_form.php
e24b7f85 10 *
11 */
12class 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 */
42f103be 20 protected $_instance;
e24b7f85 21 /**
22 * Section of course that module instance will be put in or is in.
24f41672 23 * This is always the section number itself (column 'section' from 'course_sections' table).
e24b7f85 24 *
25 * @var mixed
26 */
42f103be 27 protected $_section;
e24b7f85 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 */
42f103be 34 protected $_cm;
4b86bb08 35 /**
36 * List of modform features
37 */
42f103be 38 protected $_features;
4e781c7b 39 /**
40 * @var array Custom completion-rule elements, if enabled
41 */
42f103be 42 protected $_customcompletionelements;
43 /**
44 * @var string name of module
45 */
46 protected $_modname;
e24b7f85 47
48 function moodleform_mod($instance, $section, $cm) {
49 $this->_instance = $instance;
50 $this->_section = $section;
51 $this->_cm = $cm;
42f103be 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();
e24b7f85 60 parent::moodleform('modedit.php');
61 }
71ee4471 62
42f103be 63 protected function init_features() {
64 global $CFG;
65
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);
75
76 $this->_features->gradecat = ($this->_features->outcomes or $this->_features->hasgrades);
77 }
78
e24b7f85 79 /**
a7f7e52f 80 * Only available on moodleform_mod.
e24b7f85 81 *
82 * @param array $default_values passed by reference
83 */
a7f7e52f 84 function data_preprocessing(&$default_values){
e24b7f85 85 }
71ee4471 86
4b86bb08 87 /**
88 * Each module which defines definition_after_data() must call this method using parent::definition_after_data();
89 */
71ee4471 90 function definition_after_data() {
4b86bb08 91 global $CFG, $COURSE;
71ee4471 92 $mform =& $this->_form;
93
94 if ($id = $mform->getElementValue('update')) {
95 $modulename = $mform->getElementValue('modulename');
96 $instance = $mform->getElementValue('instance');
97
4b86bb08 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;
71ee4471 119 }
4b86bb08 120 if ($gradecat != $item->categoryid) {
121 //mixed categories
122 $gradecat = false;
123 break;
124 }
125 }
126 }
127
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');
71ee4471 133 }
134 }
135 }
136 }
24f41672 137
a78890d5 138 if ($COURSE->groupmodeforce) {
139 if ($mform->elementExists('groupmode')) {
24f41672 140 $mform->hardFreeze('groupmode'); // groupmode can not be changed if forced from course settings
141 }
142 }
a104debf 143
f2fdc8eb 144 if ($mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly') and empty($COURSE->groupmodeforce)) {
145 $mform->disabledIf('groupingid', 'groupmode', 'eq', NOGROUPS);
146
147 } else if (!$mform->elementExists('groupmode') and $mform->elementExists('groupmembersonly')) {
148 $mform->disabledIf('groupingid', 'groupmembersonly', 'notchecked');
4b86bb08 149
f2fdc8eb 150 } else if (!$mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly')) {
151 // groupings have no use without groupmode or groupmembersonly
a104debf 152 if ($mform->elementExists('groupingid')) {
153 $mform->removeElement('groupingid');
154 }
155 }
4e781c7b 156
157 // Completion: If necessary, freeze fields
61fceb86 158 $completion = new completion_info($COURSE);
159 if ($completion->is_enabled()) {
4e781c7b 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);
164
61fceb86 165 $freeze = false;
166 if (!$completedcount) {
167 if ($mform->elementExists('unlockcompletion')) {
4e781c7b 168 $mform->removeElement('unlockcompletion');
169 }
170 } else {
171 // Has the element been unlocked?
61fceb86 172 if ($mform->exportValue('unlockcompletion')) {
4e781c7b 173 // Yes, add in warning text and set the hidden variable
174 $mform->insertElementBefore(
61fceb86 175 $mform->createElement('static', 'completedunlocked',
176 get_string('completedunlocked', 'completion'),
177 get_string('completedunlockedtext', 'completion')),
4e781c7b 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(
61fceb86 185 $mform->createElement('static', 'completedwarning',
186 get_string('completedwarning', 'completion'),
187 get_string('completedwarningtext', 'completion', $completedcount)),
4e781c7b 188 'unlockcompletion');
189 $mform->setHelpButton('completedwarning', array('completionlocked', get_string('help_completionlocked', 'completion'), 'completion'));
190
61fceb86 191 $freeze = true;
4e781c7b 192 }
193 }
194
61fceb86 195 if ($freeze) {
4e781c7b 196 $mform->freeze('completion');
61fceb86 197 if ($mform->elementExists('completionview')) {
4e781c7b 198 $mform->freeze('completionview'); // don't use hardFreeze or checkbox value gets lost
199 }
61fceb86 200 if ($mform->elementExists('completionusegrade')) {
4e781c7b 201 $mform->freeze('completionusegrade');
202 }
203 $mform->freeze($this->_customcompletionelements);
204 }
205 }
82bd6a5e 206
207 // Availability conditions
208 if (!empty($CFG->enableavailability) && $this->_cm) {
209 $ci = new condition_info($this->_cm);
210 $fullcm=$ci->get_full_course_module();
211
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 }
222
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 }
71ee4471 233 }
234
60243313 235 // form verification
a78890d5 236 function validation($data, $files) {
32648682 237 global $COURSE, $DB;
a78890d5 238 $errors = parent::validation($data, $files);
60243313 239
273eb2f5 240 $mform =& $this->_form;
241
60243313 242 $errors = array();
243
273eb2f5 244 if ($mform->elementExists('name')) {
245 $name = trim($data['name']);
246 if ($name == '') {
247 $errors['name'] = get_string('required');
248 }
e04ff2d5 249 }
250
60243313 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']) {
32648682 254 $cm = $DB->get_record('course_modules', array('id'=>$data['coursemodule']));
60243313 255 } else {
256 $cm = null;
257 }
258
273eb2f5 259 if ($mform->elementExists('cmidnumber')) {
260 // verify the idnumber
204175c5 261 if (!grade_verify_idnumber($data['cmidnumber'], $COURSE->id, $grade_item, $cm)) {
273eb2f5 262 $errors['cmidnumber'] = get_string('idnumbertaken');
263 }
60243313 264 }
4e781c7b 265
266 // Completion: Don't let them choose automatic completion without turning
267 // on some conditions
61fceb86 268 if (array_key_exists('completion', $data) && $data['completion']==COMPLETION_TRACKING_AUTOMATIC) {
269 if (empty($data['completionview']) && empty($data['completionusegrade']) &&
4e781c7b 270 !$this->completion_rule_enabled($data)) {
61fceb86 271 $errors['completion'] = get_string('badautocompletion', 'completion');
4e781c7b 272 }
273 }
60243313 274
a561d96e 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
a78890d5 282 return $errors;
60243313 283 }
284
e24b7f85 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 */
32db0d42 292 function set_data($default_values) {
e24b7f85 293 if (is_object($default_values)) {
294 $default_values = (array)$default_values;
295 }
82bd6a5e 296
297 $this->data_preprocessing($default_values);
298 parent::set_data($default_values);
e24b7f85 299 }
71ee4471 300
e24b7f85 301 /**
302 * Adds all the standard elements to a form to edit the settings for an activity module.
e24b7f85 303 */
42f103be 304 function standard_coursemodule_elements(){
c18269c7 305 global $COURSE, $CFG, $DB;
e24b7f85 306 $mform =& $this->_form;
f3b783f4 307
b5ab55dd 308 $outcomesused = false;
42f103be 309 if ($this->_features->outcomes) {
f3b783f4 310 if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
b5ab55dd 311 $outcomesused = true;
f3b783f4 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 }
318
24e25bc1 319 $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
4b86bb08 320 if ($this->_features->groups) {
a78890d5 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')));
e24b7f85 326 }
24f41672 327
328 if (!empty($CFG->enablegroupings)) {
4b86bb08 329 if ($this->_features->groupings or $this->_features->groupmembersonly) {
e04ff2d5 330 //groupings selector - used for normal grouping mode or also when restricting access with groupmembersonly
331 $options = array();
332 $options[0] = get_string('none');
c18269c7 333 if ($groupings = $DB->get_records('groupings', array('courseid'=>$COURSE->id))) {
e04ff2d5 334 foreach ($groupings as $grouping) {
335 $options[$grouping->id] = format_string($grouping->name);
336 }
24f41672 337 }
e04ff2d5 338 $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options);
0143840c 339 $mform->setHelpButton('groupingid', array('grouping', get_string('grouping', 'group')));
e04ff2d5 340 $mform->setAdvanced('groupingid');
24f41672 341 }
e04ff2d5 342
4b86bb08 343 if ($this->_features->groupmembersonly) {
f2fdc8eb 344 $mform->addElement('checkbox', 'groupmembersonly', get_string('groupmembersonly', 'group'));
0143840c 345 $mform->setHelpButton('groupmembersonly', array('groupmembersonly', get_string('groupmembersonly', 'group')));
a104debf 346 $mform->setAdvanced('groupmembersonly');
347 }
24f41672 348 }
349
e24b7f85 350 $mform->addElement('modvisible', 'visible', get_string('visible'));
b5ab55dd 351
352 if ($this->_features->idnumber) {
353 $mform->addElement('text', 'cmidnumber', get_string('idnumbermod'));
354 $mform->setHelpButton('cmidnumber', array('cmidnumber', get_string('idnumbermod')), true);
355 }
71ee4471 356
4b86bb08 357 if ($this->_features->gradecat) {
57956368 358 $categories = grade_get_categories_menu($COURSE->id, $outcomesused);
4b86bb08 359 $mform->addElement('select', 'gradecat', get_string('gradecategory', 'grades'), $categories);
360 }
361
82bd6a5e 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'));
369
370 // Conditions based on grades
42f103be 371 $gradeoptions = array();
372 $items = grade_item::fetch_all(array('courseid'=>$COURSE->id));
373 $items = $items ? $items : array();
82bd6a5e 374 foreach($items as $id=>$item) {
d030fa7b 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 }
42f103be 381 $gradeoptions[$id] = $item->get_name();
82bd6a5e 382 }
383 asort($gradeoptions);
42f103be 384 $gradeoptions = array(0=>get_string('none','condition'))+$gradeoptions;
82bd6a5e 385
42f103be 386 $grouparray = array();
82bd6a5e 387 $grouparray[] =& $mform->createElement('select','conditiongradeitemid','',$gradeoptions);
9271e424 388 $grouparray[] =& $mform->createElement('static', '', '',' '.get_string('grade_atleast','condition').' ');
82bd6a5e 389 $grouparray[] =& $mform->createElement('text', 'conditiongrademin','',array('size'=>3));
75564228 390 $grouparray[] =& $mform->createElement('static', '', '','% '.get_string('grade_upto','condition').' ');
82bd6a5e 391 $grouparray[] =& $mform->createElement('text', 'conditiongrademax','',array('size'=>3));
75564228 392 $grouparray[] =& $mform->createElement('static', '', '','%');
82bd6a5e 393 $mform->setType('conditiongrademin',PARAM_FLOAT);
394 $mform->setType('conditiongrademax',PARAM_FLOAT);
395 $group = $mform->createElement('group','conditiongradegroup',
396 get_string('gradecondition', 'condition'),$grouparray);
397
398 // Get version with condition info and store it so we don't ask
399 // twice
400 if(!empty($this->_cm)) {
42f103be 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;
82bd6a5e 404 } else {
42f103be 405 $count = 1;
82bd6a5e 406 }
407
42f103be 408 $this->repeat_elements(array($group), $count, array(), 'conditiongraderepeats', 'conditiongradeadds', 2,
409 get_string('addgrades', 'condition'), true);
82bd6a5e 410 $mform->setHelpButton('conditiongradegroup[0]', array('gradecondition', get_string('help_gradecondition', 'condition'), 'condition'));
411
412 // Conditions based on completion
413 $completion = new completion_info($COURSE);
414 if ($completion->is_enabled()) {
42f103be 415 $completionoptions = array();
416 $modinfo = get_fast_modinfo($COURSE);
82bd6a5e 417 foreach($modinfo->cms as $id=>$cm) {
d030fa7b 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)) {
fe44022d 422 $completionoptions[$id]=$cm->name;
423 }
82bd6a5e 424 }
425 asort($completionoptions);
42f103be 426 $completionoptions = array(0=>get_string('none','condition'))+$completionoptions;
82bd6a5e 427
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'));
433
42f103be 434 $grouparray = array();
82bd6a5e 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);
439
42f103be 440 $count = empty($this->_cm) ? 1 : count($this->_cm->conditionscompletion)+1;
82bd6a5e 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 }
446
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 }
454
4e781c7b 455 // Conditional activities: completion tracking section
82bd6a5e 456 if(!isset($completion)) {
457 $completion = new completion_info($COURSE);
458 }
61fceb86 459 if ($completion->is_enabled()) {
4e781c7b 460 $mform->addElement('header', '', get_string('activitycompletion', 'completion'));
461
462 // Unlock button for if people have completed it (will
463 // be removed in definition_after_data if they haven't)
61fceb86 464 $mform->addElement('submit', 'unlockcompletion', get_string('unlockcompletion', 'completion'));
4e781c7b 465 $mform->registerNoSubmitButton('unlockcompletion');
61fceb86 466 $mform->addElement('hidden', 'completionunlocked', 0);
82bd6a5e 467
61fceb86 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')));
4e781c7b 471 $mform->setHelpButton('completion', array('completion', get_string('help_completion', 'completion'), 'completion'));
61fceb86 472 $mform->setDefault('completion', $this->_features->defaultcompletion
4e781c7b 473 ? COMPLETION_TRACKING_MANUAL
474 : COMPLETION_TRACKING_NONE);
475
476 // Automatic completion once you view it
61fceb86 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'));
4e781c7b 481 $mform->setHelpButton('completionview', array('completionview', get_string('help_completionview', 'completion'), 'completion'));
61fceb86 482 $mform->disabledIf('completionview', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
483 $gotcompletionoptions = true;
4e781c7b 484 }
485
486 // Automatic completion once it's graded
61fceb86 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'));
4e781c7b 490 $mform->setHelpButton('completionusegrade', array('completionusegrade', get_string('help_completionusegrade', 'completion'), 'completion'));
61fceb86 491 $mform->disabledIf('completionusegrade', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
492 $gotcompletionoptions = true;
4e781c7b 493 }
494
495 // Automatic completion according to module-specific rules
496 $this->_customcompletionelements = $this->add_completion_rules();
61fceb86 497 foreach ($this->_customcompletionelements as $element) {
498 $mform->disabledIf($element, 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
4e781c7b 499 }
500
501 $gotcompletionoptions = $gotcompletionoptions ||
502 count($this->_customcompletionelements)>0;
503
504 // Automatic option only appears if possible
61fceb86 505 if ($gotcompletionoptions) {
4e781c7b 506 $mform->getElement('completion')->addOption(
61fceb86 507 get_string('completion_automatic', 'completion'),
4e781c7b 508 COMPLETION_TRACKING_AUTOMATIC);
509 }
510
511 // Completion expected at particular date? (For progress tracking)
61fceb86 512 $mform->addElement('date_selector', 'completionexpected', get_string('completionexpected', 'completion'), array('optional'=>true));
4e781c7b 513 $mform->setHelpButton('completionexpected', array('completionexpected', get_string('help_completionexpected', 'completion'), 'completion'));
61fceb86 514 $mform->disabledIf('completionexpected', 'completion', 'eq', COMPLETION_TRACKING_NONE);
4e781c7b 515 }
516
e24b7f85 517 $this->standard_hidden_coursemodule_elements();
518 }
4e781c7b 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 }
532
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 }
e24b7f85 544
545 function standard_hidden_coursemodule_elements(){
546 $mform =& $this->_form;
547 $mform->addElement('hidden', 'course', 0);
548 $mform->setType('course', PARAM_INT);
549
550 $mform->addElement('hidden', 'coursemodule', 0);
551 $mform->setType('coursemodule', PARAM_INT);
552
553 $mform->addElement('hidden', 'section', 0);
554 $mform->setType('section', PARAM_INT);
555
556 $mform->addElement('hidden', 'module', 0);
557 $mform->setType('module', PARAM_INT);
558
559 $mform->addElement('hidden', 'modulename', '');
560 $mform->setType('modulename', PARAM_SAFEDIR);
561
562 $mform->addElement('hidden', 'instance', 0);
563 $mform->setType('instance', PARAM_INT);
564
565 $mform->addElement('hidden', 'add', 0);
566 $mform->setType('add', PARAM_ALPHA);
567
568 $mform->addElement('hidden', 'update', 0);
569 $mform->setType('update', PARAM_INT);
19110c57 570
571 $mform->addElement('hidden', 'return', 0);
572 $mform->setType('return', PARAM_BOOL);
e24b7f85 573 }
574
6b467109 575 /**
576 * Overriding formslib's add_action_buttons() method, to add an extra submit "save changes and return" button.
b5ab55dd 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
6b467109 581 * @return void
b5ab55dd 582 */
6b467109 583 function add_action_buttons($cancel=true, $submitlabel=null, $submit2label=null) {
584 if (is_null($submitlabel)) {
585 $submitlabel = get_string('savechangesanddisplay');
586 }
b5ab55dd 587
6b467109 588 if (is_null($submit2label)) {
589 $submit2label = get_string('savechangesandreturntocourse');
590 }
b5ab55dd 591
6b467109 592 $mform =& $this->_form;
b5ab55dd 593
594 // elements in a row need a group
595 $buttonarray = array();
596
597 if ($submit2label !== false) {
598 $buttonarray[] = &$mform->createElement('submit', 'submitbutton2', $submit2label);
599 }
600
601 if ($submitlabel !== false) {
602 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
603 }
604
6b467109 605 if ($cancel) {
606 $buttonarray[] = &$mform->createElement('cancel');
607 }
608
609 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
de6f158f 610 $mform->setType('buttonar', PARAM_RAW);
6b467109 611 $mform->closeHeaderBefore('buttonar');
612 }
e24b7f85 613}
614
f3b783f4 615?>