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