Merge branch 'MDL-32249-21' of github.com:srynot4sale/moodle into MOODLE_21_STABLE
[moodle.git] / course / moodleform_mod.php
blob5c5f002b277c59fda457ee37ce3b4d7c709b1a7c
1 <?php
2 require_once ($CFG->libdir.'/formslib.php');
3 require_once($CFG->libdir.'/completionlib.php');
5 /**
6 * This class adds extra methods to form wrapper specific to be used for module
7 * add / update forms mod/{modname}/mod_form.php replaced deprecated mod/{modname}/mod.html
8 */
9 abstract class moodleform_mod extends moodleform {
10 /** Current data */
11 protected $current;
12 /**
13 * Instance of the module that is being updated. This is the id of the {prefix}{modulename}
14 * record. Can be used in form definition. Will be "" if this is an 'add' form and not an
15 * update one.
17 * @var mixed
19 protected $_instance;
20 /**
21 * Section of course that module instance will be put in or is in.
22 * This is always the section number itself (column 'section' from 'course_sections' table).
24 * @var mixed
26 protected $_section;
27 /**
28 * Course module record of the module that is being updated. Will be null if this is an 'add' form and not an
29 * update one.
31 * @var mixed
33 protected $_cm;
34 /**
35 * List of modform features
37 protected $_features;
38 /**
39 * @var array Custom completion-rule elements, if enabled
41 protected $_customcompletionelements;
42 /**
43 * @var string name of module
45 protected $_modname;
46 /** current context, course or module depends if already exists*/
47 protected $context;
49 /** a flag indicating whether outcomes are being used*/
50 protected $_outcomesused;
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;
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);
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');
69 $this->_modname = $matches[1];
70 $this->init_features();
71 parent::moodleform('modedit.php');
74 protected function init_features() {
75 global $CFG;
77 $this->_features = new stdClass();
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 = (!empty($CFG->enablegroupmembersonly) and 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);
84 $this->_features->introeditor = plugin_supports('mod', $this->_modname, FEATURE_MOD_INTRO, true);
85 $this->_features->defaultcompletion = plugin_supports('mod', $this->_modname, FEATURE_MODEDIT_DEFAULT_COMPLETION, true);
86 $this->_features->rating = plugin_supports('mod', $this->_modname, FEATURE_RATE, false);
88 $this->_features->gradecat = ($this->_features->outcomes or $this->_features->hasgrades);
91 /**
92 * Only available on moodleform_mod.
94 * @param array $default_values passed by reference
96 function data_preprocessing(&$default_values){
97 if (empty($default_values['scale'])) {
98 $default_values['assessed'] = 0;
101 if (empty($default_values['assessed'])){
102 $default_values['ratingtime'] = 0;
103 } else {
104 $default_values['ratingtime']=
105 ($default_values['assesstimestart'] && $default_values['assesstimefinish']) ? 1 : 0;
110 * Each module which defines definition_after_data() must call this method using parent::definition_after_data();
112 function definition_after_data() {
113 global $CFG, $COURSE;
114 $mform =& $this->_form;
116 if ($id = $mform->getElementValue('update')) {
117 $modulename = $mform->getElementValue('modulename');
118 $instance = $mform->getElementValue('instance');
120 if ($this->_features->gradecat) {
121 $gradecat = false;
122 if (!empty($CFG->enableoutcomes) and $this->_features->outcomes) {
123 $outcomes = grade_outcome::fetch_all_available($COURSE->id);
124 if (!empty($outcomes)) {
125 $gradecat = true;
129 $items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename,'iteminstance'=>$instance, 'courseid'=>$COURSE->id));
130 //will be no items if, for example, this activity supports ratings but rating aggregate type == no ratings
131 if (!empty($items)) {
132 foreach ($items as $item) {
133 if (!empty($item->outcomeid)) {
134 $elname = 'outcome_'.$item->outcomeid;
135 if ($mform->elementExists($elname)) {
136 $mform->hardFreeze($elname); // prevent removing of existing outcomes
141 foreach ($items as $item) {
142 if (is_bool($gradecat)) {
143 $gradecat = $item->categoryid;
144 continue;
146 if ($gradecat != $item->categoryid) {
147 //mixed categories
148 $gradecat = false;
149 break;
154 if ($gradecat === false) {
155 // items and outcomes in different categories - remove the option
156 // TODO: add a "Mixed categories" text instead of removing elements with no explanation
157 if ($mform->elementExists('gradecat')) {
158 $mform->removeElement('gradecat');
159 if ($this->_features->rating) {
160 //if supports ratings then the max grade dropdown wasnt added so the grade box can be removed entirely
161 $mform->removeElement('modstandardgrade');
168 if ($COURSE->groupmodeforce) {
169 if ($mform->elementExists('groupmode')) {
170 $mform->hardFreeze('groupmode'); // groupmode can not be changed if forced from course settings
174 // Don't disable/remove groupingid if it is currently set to something,
175 // otherwise you cannot turn it off at same time as turning off other
176 // option (MDL-30764)
177 if (empty($this->_cm) || !$this->_cm->groupingid) {
178 if ($mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly') and empty($COURSE->groupmodeforce)) {
179 $mform->disabledIf('groupingid', 'groupmode', 'eq', NOGROUPS);
181 } else if (!$mform->elementExists('groupmode') and $mform->elementExists('groupmembersonly')) {
182 $mform->disabledIf('groupingid', 'groupmembersonly', 'notchecked');
184 } else if (!$mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly')) {
185 // groupings have no use without groupmode or groupmembersonly
186 if ($mform->elementExists('groupingid')) {
187 $mform->removeElement('groupingid');
192 // Completion: If necessary, freeze fields
193 $completion = new completion_info($COURSE);
194 if ($completion->is_enabled()) {
195 // If anybody has completed the activity, these options will be 'locked'
196 $completedcount = empty($this->_cm)
198 : $completion->count_user_data($this->_cm);
200 $freeze = false;
201 if (!$completedcount) {
202 if ($mform->elementExists('unlockcompletion')) {
203 $mform->removeElement('unlockcompletion');
205 // Automatically set to unlocked (note: this is necessary
206 // in order to make it recalculate completion once the option
207 // is changed, maybe someone has completed it now)
208 $mform->getElement('completionunlocked')->setValue(1);
209 } else {
210 // Has the element been unlocked?
211 if ($mform->exportValue('unlockcompletion')) {
212 // Yes, add in warning text and set the hidden variable
213 $mform->insertElementBefore(
214 $mform->createElement('static', 'completedunlocked',
215 get_string('completedunlocked', 'completion'),
216 get_string('completedunlockedtext', 'completion')),
217 'unlockcompletion');
218 $mform->removeElement('unlockcompletion');
219 $mform->getElement('completionunlocked')->setValue(1);
220 } else {
221 // No, add in the warning text with the count (now we know
222 // it) before the unlock button
223 $mform->insertElementBefore(
224 $mform->createElement('static', 'completedwarning',
225 get_string('completedwarning', 'completion'),
226 get_string('completedwarningtext', 'completion', $completedcount)),
227 'unlockcompletion');
228 $freeze = true;
232 if ($freeze) {
233 $mform->freeze('completion');
234 if ($mform->elementExists('completionview')) {
235 $mform->freeze('completionview'); // don't use hardFreeze or checkbox value gets lost
237 if ($mform->elementExists('completionusegrade')) {
238 $mform->freeze('completionusegrade');
240 $mform->freeze($this->_customcompletionelements);
244 // Availability conditions
245 if (!empty($CFG->enableavailability) && $this->_cm) {
246 $ci = new condition_info($this->_cm);
247 $fullcm=$ci->get_full_course_module();
249 $num=0;
250 foreach($fullcm->conditionsgrade as $gradeitemid=>$minmax) {
251 $groupelements=$mform->getElement('conditiongradegroup['.$num.']')->getElements();
252 $groupelements[0]->setValue($gradeitemid);
253 // These numbers are always in the format 0.00000 - the rtrims remove any final zeros and,
254 // if it is a whole number, the decimal place.
255 $groupelements[2]->setValue(is_null($minmax->min)?'':rtrim(rtrim($minmax->min,'0'),'.'));
256 $groupelements[4]->setValue(is_null($minmax->max)?'':rtrim(rtrim($minmax->max,'0'),'.'));
257 $num++;
260 if ($completion->is_enabled()) {
261 $num=0;
262 foreach($fullcm->conditionscompletion as $othercmid=>$state) {
263 $groupelements=$mform->getElement('conditioncompletiongroup['.$num.']')->getElements();
264 $groupelements[0]->setValue($othercmid);
265 $groupelements[1]->setValue($state);
266 $num++;
272 // form verification
273 function validation($data, $files) {
274 global $COURSE, $DB;
275 $errors = parent::validation($data, $files);
277 $mform =& $this->_form;
279 $errors = array();
281 if ($mform->elementExists('name')) {
282 $name = trim($data['name']);
283 if ($name == '') {
284 $errors['name'] = get_string('required');
288 $grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$data['modulename'],
289 'iteminstance'=>$data['instance'], 'itemnumber'=>0, 'courseid'=>$COURSE->id));
290 if ($data['coursemodule']) {
291 $cm = $DB->get_record('course_modules', array('id'=>$data['coursemodule']));
292 } else {
293 $cm = null;
296 if ($mform->elementExists('cmidnumber')) {
297 // verify the idnumber
298 if (!grade_verify_idnumber($data['cmidnumber'], $COURSE->id, $grade_item, $cm)) {
299 $errors['cmidnumber'] = get_string('idnumbertaken');
303 // Completion: Don't let them choose automatic completion without turning
304 // on some conditions
305 if (array_key_exists('completion', $data) && $data['completion']==COMPLETION_TRACKING_AUTOMATIC) {
306 if (empty($data['completionview']) && empty($data['completionusegrade']) &&
307 !$this->completion_rule_enabled($data)) {
308 $errors['completion'] = get_string('badautocompletion', 'completion');
312 // Conditions: Don't let them set dates which make no sense
313 if (array_key_exists('availablefrom', $data) &&
314 $data['availablefrom'] && $data['availableuntil'] &&
315 $data['availablefrom'] > $data['availableuntil']) {
316 $errors['availablefrom'] = get_string('badavailabledates', 'condition');
319 // Conditions: Verify that the grade conditions are numbers, and make sense.
320 if (array_key_exists('conditiongradegroup', $data)) {
321 foreach ($data['conditiongradegroup'] as $i => $gradedata) {
322 if ($gradedata['conditiongrademin'] !== '' && !is_numeric($gradedata['conditiongrademin'])) {
323 $errors["conditiongradegroup[{$i}]"] = get_string('gradesmustbenumeric', 'condition');
324 continue;
326 if ($gradedata['conditiongrademax'] !== '' && !is_numeric($gradedata['conditiongrademax'])) {
327 $errors["conditiongradegroup[{$i}]"] = get_string('gradesmustbenumeric', 'condition');
328 continue;
330 if ($gradedata['conditiongrademin'] !== '' && $gradedata['conditiongrademax'] !== '' &&
331 $gradedata['conditiongrademax'] < $gradedata['conditiongrademin']) {
332 $errors["conditiongradegroup[{$i}]"] = get_string('badgradelimits', 'condition');
333 continue;
335 if ($gradedata['conditiongrademin'] === '' && $gradedata['conditiongrademax'] === '' &&
336 $gradedata['conditiongradeitemid']) {
337 $errors["conditiongradegroup[{$i}]"] = get_string('gradeitembutnolimits', 'condition');
338 continue;
340 if (($gradedata['conditiongrademin'] !== '' || $gradedata['conditiongrademax'] !== '') &&
341 !$gradedata['conditiongradeitemid']) {
342 $errors["conditiongradegroup[{$i}]"] = get_string('gradelimitsbutnoitem', 'condition');
343 continue;
348 return $errors;
352 * Load in existing data as form defaults. Usually new entry defaults are stored directly in
353 * form definition (new entry form); this function is used to load in data where values
354 * already exist and data is being edited (edit entry form).
356 * @param mixed $default_values object or array of default values
358 function set_data($default_values) {
359 if (is_object($default_values)) {
360 $default_values = (array)$default_values;
363 $this->data_preprocessing($default_values);
364 parent::set_data($default_values);
368 * Adds all the standard elements to a form to edit the settings for an activity module.
370 function standard_coursemodule_elements(){
371 global $COURSE, $CFG, $DB;
372 $mform =& $this->_form;
374 $this->_outcomesused = false;
375 if ($this->_features->outcomes) {
376 if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
377 $this->_outcomesused = true;
378 $mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades'));
379 foreach($outcomes as $outcome) {
380 $mform->addElement('advcheckbox', 'outcome_'.$outcome->id, $outcome->get_name());
386 if ($this->_features->rating) {
387 require_once($CFG->dirroot.'/rating/lib.php');
388 $rm = new rating_manager();
390 $mform->addElement('header', 'modstandardratings', get_string('ratings', 'rating'));
392 $permission=CAP_ALLOW;
393 $rolenamestring = null;
394 if (!empty($this->_cm)) {
395 $context = get_context_instance(CONTEXT_MODULE, $this->_cm->id);
397 $rolenames = get_role_names_with_caps_in_context($context, array('moodle/rating:rate', 'mod/'.$this->_cm->modname.':rate'));
398 $rolenamestring = implode(', ', $rolenames);
399 } else {
400 $rolenamestring = get_string('capabilitychecknotavailable','rating');
402 $mform->addElement('static', 'rolewarning', get_string('rolewarning','rating'), $rolenamestring);
403 $mform->addHelpButton('rolewarning', 'rolewarning', 'rating');
405 $mform->addElement('select', 'assessed', get_string('aggregatetype', 'rating') , $rm->get_aggregate_types());
406 $mform->setDefault('assessed', 0);
407 $mform->addHelpButton('assessed', 'aggregatetype', 'rating');
409 $mform->addElement('modgrade', 'scale', get_string('scale'), false);
410 $mform->disabledIf('scale', 'assessed', 'eq', 0);
412 $mform->addElement('checkbox', 'ratingtime', get_string('ratingtime', 'rating'));
413 $mform->disabledIf('ratingtime', 'assessed', 'eq', 0);
415 $mform->addElement('date_time_selector', 'assesstimestart', get_string('from'));
416 $mform->disabledIf('assesstimestart', 'assessed', 'eq', 0);
417 $mform->disabledIf('assesstimestart', 'ratingtime');
419 $mform->addElement('date_time_selector', 'assesstimefinish', get_string('to'));
420 $mform->disabledIf('assesstimefinish', 'assessed', 'eq', 0);
421 $mform->disabledIf('assesstimefinish', 'ratingtime');
424 //doing this here means splitting up the grade related settings on the lesson settings page
425 //$this->standard_grading_coursemodule_elements();
427 $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
428 if ($this->_features->groups) {
429 $options = array(NOGROUPS => get_string('groupsnone'),
430 SEPARATEGROUPS => get_string('groupsseparate'),
431 VISIBLEGROUPS => get_string('groupsvisible'));
432 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $options, NOGROUPS);
433 $mform->addHelpButton('groupmode', 'groupmode', 'group');
436 if ($this->_features->groupings or $this->_features->groupmembersonly) {
437 //groupings selector - used for normal grouping mode or also when restricting access with groupmembersonly
438 $options = array();
439 $options[0] = get_string('none');
440 if ($groupings = $DB->get_records('groupings', array('courseid'=>$COURSE->id))) {
441 foreach ($groupings as $grouping) {
442 $options[$grouping->id] = format_string($grouping->name);
445 $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options);
446 $mform->addHelpButton('groupingid', 'grouping', 'group');
447 $mform->setAdvanced('groupingid');
450 if ($this->_features->groupmembersonly) {
451 $mform->addElement('checkbox', 'groupmembersonly', get_string('groupmembersonly', 'group'));
452 $mform->addHelpButton('groupmembersonly', 'groupmembersonly', 'group');
453 $mform->setAdvanced('groupmembersonly');
456 $mform->addElement('modvisible', 'visible', get_string('visible'));
458 if ($this->_features->idnumber) {
459 $mform->addElement('text', 'cmidnumber', get_string('idnumbermod'));
460 $mform->addHelpButton('cmidnumber', 'idnumbermod');
463 if (!empty($CFG->enableavailability)) {
464 // Conditional availability
465 $mform->addElement('header', 'availabilityconditionsheader', get_string('availabilityconditions', 'condition'));
466 $mform->addElement('date_selector', 'availablefrom', get_string('availablefrom', 'condition'), array('optional'=>true));
467 $mform->addHelpButton('availablefrom', 'availablefrom', 'condition');
468 $mform->addElement('date_selector', 'availableuntil', get_string('availableuntil', 'condition'), array('optional'=>true));
470 // Conditions based on grades
471 $gradeoptions = array();
472 $items = grade_item::fetch_all(array('courseid'=>$COURSE->id));
473 $items = $items ? $items : array();
474 foreach($items as $id=>$item) {
475 // Do not include grades for current item
476 if (!empty($this->_cm) && $this->_cm->instance == $item->iteminstance
477 && $this->_cm->modname == $item->itemmodule
478 && $item->itemtype == 'mod') {
479 continue;
481 $gradeoptions[$id] = $item->get_name();
483 asort($gradeoptions);
484 $gradeoptions = array(0=>get_string('none','condition'))+$gradeoptions;
486 $grouparray = array();
487 $grouparray[] =& $mform->createElement('select','conditiongradeitemid','',$gradeoptions);
488 $grouparray[] =& $mform->createElement('static', '', '',' '.get_string('grade_atleast','condition').' ');
489 $grouparray[] =& $mform->createElement('text', 'conditiongrademin','',array('size'=>3));
490 $grouparray[] =& $mform->createElement('static', '', '','% '.get_string('grade_upto','condition').' ');
491 $grouparray[] =& $mform->createElement('text', 'conditiongrademax','',array('size'=>3));
492 $grouparray[] =& $mform->createElement('static', '', '','%');
493 $group = $mform->createElement('group','conditiongradegroup',
494 get_string('gradecondition', 'condition'),$grouparray);
496 // Get version with condition info and store it so we don't ask
497 // twice
498 if(!empty($this->_cm)) {
499 $ci = new condition_info($this->_cm, CONDITION_MISSING_EXTRATABLE);
500 $this->_cm = $ci->get_full_course_module();
501 $count = count($this->_cm->conditionsgrade)+1;
502 } else {
503 $count = 1;
506 $this->repeat_elements(array($group), $count, array(), 'conditiongraderepeats', 'conditiongradeadds', 2,
507 get_string('addgrades', 'condition'), true);
508 $mform->addHelpButton('conditiongradegroup[0]', 'gradecondition', 'condition');
510 // Conditions based on completion
511 $completion = new completion_info($COURSE);
512 if ($completion->is_enabled()) {
513 $completionoptions = array();
514 $modinfo = get_fast_modinfo($COURSE);
515 foreach($modinfo->cms as $id=>$cm) {
516 // Add each course-module if it:
517 // (a) has completion turned on
518 // (b) is not the same as current course-module
519 if ($cm->completion && (empty($this->_cm) || $this->_cm->id != $id)) {
520 $completionoptions[$id]=$cm->name;
523 asort($completionoptions);
524 $completionoptions = array(0=>get_string('none','condition'))+$completionoptions;
526 $completionvalues=array(
527 COMPLETION_COMPLETE=>get_string('completion_complete','condition'),
528 COMPLETION_INCOMPLETE=>get_string('completion_incomplete','condition'),
529 COMPLETION_COMPLETE_PASS=>get_string('completion_pass','condition'),
530 COMPLETION_COMPLETE_FAIL=>get_string('completion_fail','condition'));
532 $grouparray = array();
533 $grouparray[] =& $mform->createElement('select','conditionsourcecmid','',$completionoptions);
534 $grouparray[] =& $mform->createElement('select','conditionrequiredcompletion','',$completionvalues);
535 $group = $mform->createElement('group','conditioncompletiongroup',
536 get_string('completioncondition', 'condition'),$grouparray);
538 $count = empty($this->_cm) ? 1 : count($this->_cm->conditionscompletion)+1;
539 $this->repeat_elements(array($group),$count,array(),
540 'conditioncompletionrepeats','conditioncompletionadds',2,
541 get_string('addcompletions','condition'),true);
542 $mform->addHelpButton('conditioncompletiongroup[0]', 'completioncondition', 'condition');
545 // Do we display availability info to students?
546 $mform->addElement('select', 'showavailability', get_string('showavailability', 'condition'),
547 array(CONDITION_STUDENTVIEW_SHOW=>get_string('showavailability_show', 'condition'),
548 CONDITION_STUDENTVIEW_HIDE=>get_string('showavailability_hide', 'condition')));
549 $mform->setDefault('showavailability', CONDITION_STUDENTVIEW_SHOW);
552 // Conditional activities: completion tracking section
553 if(!isset($completion)) {
554 $completion = new completion_info($COURSE);
556 if ($completion->is_enabled()) {
557 $mform->addElement('header', 'activitycompletionheader', get_string('activitycompletion', 'completion'));
559 // Unlock button for if people have completed it (will
560 // be removed in definition_after_data if they haven't)
561 $mform->addElement('submit', 'unlockcompletion', get_string('unlockcompletion', 'completion'));
562 $mform->registerNoSubmitButton('unlockcompletion');
563 $mform->addElement('hidden', 'completionunlocked', 0);
564 $mform->setType('completionunlocked', PARAM_INT);
566 $mform->addElement('select', 'completion', get_string('completion', 'completion'),
567 array(COMPLETION_TRACKING_NONE=>get_string('completion_none', 'completion'),
568 COMPLETION_TRACKING_MANUAL=>get_string('completion_manual', 'completion')));
569 $mform->setDefault('completion', $this->_features->defaultcompletion
570 ? COMPLETION_TRACKING_MANUAL
571 : COMPLETION_TRACKING_NONE);
572 $mform->addHelpButton('completion', 'completion', 'completion');
574 // Automatic completion once you view it
575 $gotcompletionoptions = false;
576 if (plugin_supports('mod', $this->_modname, FEATURE_COMPLETION_TRACKS_VIEWS, false)) {
577 $mform->addElement('checkbox', 'completionview', get_string('completionview', 'completion'),
578 get_string('completionview_desc', 'completion'));
579 $mform->disabledIf('completionview', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
580 $gotcompletionoptions = true;
583 // Automatic completion once it's graded
584 if (plugin_supports('mod', $this->_modname, FEATURE_GRADE_HAS_GRADE, false)) {
585 $mform->addElement('checkbox', 'completionusegrade', get_string('completionusegrade', 'completion'),
586 get_string('completionusegrade_desc', 'completion'));
587 $mform->disabledIf('completionusegrade', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
588 $mform->addHelpButton('completionusegrade', 'completionusegrade', 'completion');
589 $gotcompletionoptions = true;
592 // Automatic completion according to module-specific rules
593 $this->_customcompletionelements = $this->add_completion_rules();
594 foreach ($this->_customcompletionelements as $element) {
595 $mform->disabledIf($element, 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
598 $gotcompletionoptions = $gotcompletionoptions ||
599 count($this->_customcompletionelements)>0;
601 // Automatic option only appears if possible
602 if ($gotcompletionoptions) {
603 $mform->getElement('completion')->addOption(
604 get_string('completion_automatic', 'completion'),
605 COMPLETION_TRACKING_AUTOMATIC);
608 // Completion expected at particular date? (For progress tracking)
609 $mform->addElement('date_selector', 'completionexpected', get_string('completionexpected', 'completion'), array('optional'=>true));
610 $mform->addHelpButton('completionexpected', 'completionexpected', 'completion');
611 $mform->disabledIf('completionexpected', 'completion', 'eq', COMPLETION_TRACKING_NONE);
614 $this->standard_hidden_coursemodule_elements();
618 * Can be overridden to add custom completion rules if the module wishes
619 * them. If overriding this, you should also override completion_rule_enabled.
620 * <p>
621 * Just add elements to the form as needed and return the list of IDs. The
622 * system will call disabledIf and handle other behaviour for each returned
623 * ID.
624 * @return array Array of string IDs of added items, empty array if none
626 function add_completion_rules() {
627 return array();
631 * Called during validation. Override to indicate, based on the data, whether
632 * a custom completion rule is enabled (selected).
634 * @param array $data Input data (not yet validated)
635 * @return bool True if one or more rules is enabled, false if none are;
636 * default returns false
638 function completion_rule_enabled(&$data) {
639 return false;
642 function standard_hidden_coursemodule_elements(){
643 $mform =& $this->_form;
644 $mform->addElement('hidden', 'course', 0);
645 $mform->setType('course', PARAM_INT);
647 $mform->addElement('hidden', 'coursemodule', 0);
648 $mform->setType('coursemodule', PARAM_INT);
650 $mform->addElement('hidden', 'section', 0);
651 $mform->setType('section', PARAM_INT);
653 $mform->addElement('hidden', 'module', 0);
654 $mform->setType('module', PARAM_INT);
656 $mform->addElement('hidden', 'modulename', '');
657 $mform->setType('modulename', PARAM_SAFEDIR);
659 $mform->addElement('hidden', 'instance', 0);
660 $mform->setType('instance', PARAM_INT);
662 $mform->addElement('hidden', 'add', 0);
663 $mform->setType('add', PARAM_ALPHA);
665 $mform->addElement('hidden', 'update', 0);
666 $mform->setType('update', PARAM_INT);
668 $mform->addElement('hidden', 'return', 0);
669 $mform->setType('return', PARAM_BOOL);
672 public function standard_grading_coursemodule_elements() {
673 global $COURSE, $CFG;
674 $mform =& $this->_form;
676 if ($this->_features->hasgrades) {
678 if (!$this->_features->rating || $this->_features->gradecat) {
679 $mform->addElement('header', 'modstandardgrade', get_string('grade'));
682 //if supports grades and grades arent being handled via ratings
683 if (!$this->_features->rating) {
684 $mform->addElement('modgrade', 'grade', get_string('grade'));
685 $mform->setDefault('grade', 100);
688 if ($this->_features->gradecat) {
689 $mform->addElement('select', 'gradecat',
690 get_string('gradecategoryonmodform', 'grades'),
691 grade_get_categories_menu($COURSE->id, $this->_outcomesused));
692 $mform->addHelpButton('gradecat', 'gradecategoryonmodform', 'grades');
697 function add_intro_editor($required=false, $customlabel=null) {
698 if (!$this->_features->introeditor) {
699 // intro editor not supported in this module
700 return;
703 $mform = $this->_form;
704 $label = is_null($customlabel) ? get_string('moduleintro') : $customlabel;
706 $mform->addElement('editor', 'introeditor', $label, null, array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$this->context));
707 $mform->setType('introeditor', PARAM_RAW); // no XSS prevention here, users must be trusted
708 if ($required) {
709 $mform->addRule('introeditor', get_string('required'), 'required', null, 'client');
714 * Overriding formslib's add_action_buttons() method, to add an extra submit "save changes and return" button.
716 * @param bool $cancel show cancel button
717 * @param string $submitlabel null means default, false means none, string is label text
718 * @param string $submit2label null means default, false means none, string is label text
719 * @return void
721 function add_action_buttons($cancel=true, $submitlabel=null, $submit2label=null) {
722 if (is_null($submitlabel)) {
723 $submitlabel = get_string('savechangesanddisplay');
726 if (is_null($submit2label)) {
727 $submit2label = get_string('savechangesandreturntocourse');
730 $mform = $this->_form;
732 // elements in a row need a group
733 $buttonarray = array();
735 if ($submit2label !== false) {
736 $buttonarray[] = &$mform->createElement('submit', 'submitbutton2', $submit2label);
739 if ($submitlabel !== false) {
740 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
743 if ($cancel) {
744 $buttonarray[] = &$mform->createElement('cancel');
747 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
748 $mform->setType('buttonar', PARAM_RAW);
749 $mform->closeHeaderBefore('buttonar');