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