MDL-20636 fix notice on qtypes admin page. (Eloy review comment I2)
[moodle.git] / course / moodleform_mod.php
blobe4eb740ac444bad8f2614f8282d56356f429d18a
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 if ($mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly') and empty($COURSE->groupmodeforce)) {
175 $mform->disabledIf('groupingid', 'groupmode', 'eq', NOGROUPS);
177 } else if (!$mform->elementExists('groupmode') and $mform->elementExists('groupmembersonly')) {
178 $mform->disabledIf('groupingid', 'groupmembersonly', 'notchecked');
180 } else if (!$mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly')) {
181 // groupings have no use without groupmode or groupmembersonly
182 if ($mform->elementExists('groupingid')) {
183 $mform->removeElement('groupingid');
187 // Completion: If necessary, freeze fields
188 $completion = new completion_info($COURSE);
189 if ($completion->is_enabled()) {
190 // If anybody has completed the activity, these options will be 'locked'
191 $completedcount = empty($this->_cm)
193 : $completion->count_user_data($this->_cm);
195 $freeze = false;
196 if (!$completedcount) {
197 if ($mform->elementExists('unlockcompletion')) {
198 $mform->removeElement('unlockcompletion');
200 // Automatically set to unlocked (note: this is necessary
201 // in order to make it recalculate completion once the option
202 // is changed, maybe someone has completed it now)
203 $mform->getElement('completionunlocked')->setValue(1);
204 } else {
205 // Has the element been unlocked?
206 if ($mform->exportValue('unlockcompletion')) {
207 // Yes, add in warning text and set the hidden variable
208 $mform->insertElementBefore(
209 $mform->createElement('static', 'completedunlocked',
210 get_string('completedunlocked', 'completion'),
211 get_string('completedunlockedtext', 'completion')),
212 'unlockcompletion');
213 $mform->removeElement('unlockcompletion');
214 $mform->getElement('completionunlocked')->setValue(1);
215 } else {
216 // No, add in the warning text with the count (now we know
217 // it) before the unlock button
218 $mform->insertElementBefore(
219 $mform->createElement('static', 'completedwarning',
220 get_string('completedwarning', 'completion'),
221 get_string('completedwarningtext', 'completion', $completedcount)),
222 'unlockcompletion');
223 $freeze = true;
227 if ($freeze) {
228 $mform->freeze('completion');
229 if ($mform->elementExists('completionview')) {
230 $mform->freeze('completionview'); // don't use hardFreeze or checkbox value gets lost
232 if ($mform->elementExists('completionusegrade')) {
233 $mform->freeze('completionusegrade');
235 $mform->freeze($this->_customcompletionelements);
239 // Availability conditions
240 if (!empty($CFG->enableavailability) && $this->_cm) {
241 $ci = new condition_info($this->_cm);
242 $fullcm=$ci->get_full_course_module();
244 $num=0;
245 foreach($fullcm->conditionsgrade as $gradeitemid=>$minmax) {
246 $groupelements=$mform->getElement('conditiongradegroup['.$num.']')->getElements();
247 $groupelements[0]->setValue($gradeitemid);
248 // These numbers are always in the format 0.00000 - the rtrims remove any final zeros and,
249 // if it is a whole number, the decimal place.
250 $groupelements[2]->setValue(is_null($minmax->min)?'':rtrim(rtrim($minmax->min,'0'),'.'));
251 $groupelements[4]->setValue(is_null($minmax->max)?'':rtrim(rtrim($minmax->max,'0'),'.'));
252 $num++;
255 if ($completion->is_enabled()) {
256 $num=0;
257 foreach($fullcm->conditionscompletion as $othercmid=>$state) {
258 $groupelements=$mform->getElement('conditioncompletiongroup['.$num.']')->getElements();
259 $groupelements[0]->setValue($othercmid);
260 $groupelements[1]->setValue($state);
261 $num++;
267 // form verification
268 function validation($data, $files) {
269 global $COURSE, $DB;
270 $errors = parent::validation($data, $files);
272 $mform =& $this->_form;
274 $errors = array();
276 if ($mform->elementExists('name')) {
277 $name = trim($data['name']);
278 if ($name == '') {
279 $errors['name'] = get_string('required');
283 $grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$data['modulename'],
284 'iteminstance'=>$data['instance'], 'itemnumber'=>0, 'courseid'=>$COURSE->id));
285 if ($data['coursemodule']) {
286 $cm = $DB->get_record('course_modules', array('id'=>$data['coursemodule']));
287 } else {
288 $cm = null;
291 if ($mform->elementExists('cmidnumber')) {
292 // verify the idnumber
293 if (!grade_verify_idnumber($data['cmidnumber'], $COURSE->id, $grade_item, $cm)) {
294 $errors['cmidnumber'] = get_string('idnumbertaken');
298 // Completion: Don't let them choose automatic completion without turning
299 // on some conditions
300 if (array_key_exists('completion', $data) && $data['completion']==COMPLETION_TRACKING_AUTOMATIC) {
301 if (empty($data['completionview']) && empty($data['completionusegrade']) &&
302 !$this->completion_rule_enabled($data)) {
303 $errors['completion'] = get_string('badautocompletion', 'completion');
307 // Conditions: Don't let them set dates which make no sense
308 if (array_key_exists('availablefrom', $data) &&
309 $data['availablefrom'] && $data['availableuntil'] &&
310 $data['availablefrom'] > $data['availableuntil']) {
311 $errors['availablefrom'] = get_string('badavailabledates', 'condition');
314 return $errors;
318 * Load in existing data as form defaults. Usually new entry defaults are stored directly in
319 * form definition (new entry form); this function is used to load in data where values
320 * already exist and data is being edited (edit entry form).
322 * @param mixed $default_values object or array of default values
324 function set_data($default_values) {
325 if (is_object($default_values)) {
326 $default_values = (array)$default_values;
329 $this->data_preprocessing($default_values);
330 parent::set_data($default_values);
334 * Adds all the standard elements to a form to edit the settings for an activity module.
336 function standard_coursemodule_elements(){
337 global $COURSE, $CFG, $DB;
338 $mform =& $this->_form;
340 $this->_outcomesused = false;
341 if ($this->_features->outcomes) {
342 if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
343 $this->_outcomesused = true;
344 $mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades'));
345 foreach($outcomes as $outcome) {
346 $mform->addElement('advcheckbox', 'outcome_'.$outcome->id, $outcome->get_name());
352 if ($this->_features->rating) {
353 require_once($CFG->dirroot.'/rating/lib.php');
354 $rm = new rating_manager();
356 $mform->addElement('header', 'modstandardratings', get_string('ratings', 'rating'));
358 $permission=CAP_ALLOW;
359 $rolenamestring = null;
360 if (!empty($this->_cm)) {
361 $context = get_context_instance(CONTEXT_MODULE, $this->_cm->id);
363 $rolenames = get_role_names_with_caps_in_context($context, array('moodle/rating:rate', 'mod/'.$this->_cm->modname.':rate'));
364 $rolenamestring = implode(', ', $rolenames);
365 } else {
366 $rolenamestring = get_string('capabilitychecknotavailable','rating');
368 $mform->addElement('static', 'rolewarning', get_string('rolewarning','rating'), $rolenamestring);
369 $mform->addHelpButton('rolewarning', 'rolewarning', 'rating');
371 $mform->addElement('select', 'assessed', get_string('aggregatetype', 'rating') , $rm->get_aggregate_types());
372 $mform->setDefault('assessed', 0);
373 $mform->addHelpButton('assessed', 'aggregatetype', 'rating');
375 $mform->addElement('modgrade', 'scale', get_string('scale'), false);
376 $mform->disabledIf('scale', 'assessed', 'eq', 0);
378 $mform->addElement('checkbox', 'ratingtime', get_string('ratingtime', 'rating'));
379 $mform->disabledIf('ratingtime', 'assessed', 'eq', 0);
381 $mform->addElement('date_time_selector', 'assesstimestart', get_string('from'));
382 $mform->disabledIf('assesstimestart', 'assessed', 'eq', 0);
383 $mform->disabledIf('assesstimestart', 'ratingtime');
385 $mform->addElement('date_time_selector', 'assesstimefinish', get_string('to'));
386 $mform->disabledIf('assesstimefinish', 'assessed', 'eq', 0);
387 $mform->disabledIf('assesstimefinish', 'ratingtime');
390 //doing this here means splitting up the grade related settings on the lesson settings page
391 //$this->standard_grading_coursemodule_elements();
393 $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
394 if ($this->_features->groups) {
395 $options = array(NOGROUPS => get_string('groupsnone'),
396 SEPARATEGROUPS => get_string('groupsseparate'),
397 VISIBLEGROUPS => get_string('groupsvisible'));
398 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $options, NOGROUPS);
399 $mform->addHelpButton('groupmode', 'groupmode', 'group');
402 if ($this->_features->groupings or $this->_features->groupmembersonly) {
403 //groupings selector - used for normal grouping mode or also when restricting access with groupmembersonly
404 $options = array();
405 $options[0] = get_string('none');
406 if ($groupings = $DB->get_records('groupings', array('courseid'=>$COURSE->id))) {
407 foreach ($groupings as $grouping) {
408 $options[$grouping->id] = format_string($grouping->name);
411 $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options);
412 $mform->addHelpButton('groupingid', 'grouping', 'group');
413 $mform->setAdvanced('groupingid');
416 if ($this->_features->groupmembersonly) {
417 $mform->addElement('checkbox', 'groupmembersonly', get_string('groupmembersonly', 'group'));
418 $mform->addHelpButton('groupmembersonly', 'groupmembersonly', 'group');
419 $mform->setAdvanced('groupmembersonly');
422 $mform->addElement('modvisible', 'visible', get_string('visible'));
424 if ($this->_features->idnumber) {
425 $mform->addElement('text', 'cmidnumber', get_string('idnumbermod'));
426 $mform->addHelpButton('cmidnumber', 'idnumbermod');
429 if (!empty($CFG->enableavailability)) {
430 // Conditional availability
431 $mform->addElement('header', 'availabilityconditionsheader', get_string('availabilityconditions', 'condition'));
432 $mform->addElement('date_selector', 'availablefrom', get_string('availablefrom', 'condition'), array('optional'=>true));
433 $mform->addHelpButton('availablefrom', 'availablefrom', 'condition');
434 $mform->addElement('date_selector', 'availableuntil', get_string('availableuntil', 'condition'), array('optional'=>true));
436 // Conditions based on grades
437 $gradeoptions = array();
438 $items = grade_item::fetch_all(array('courseid'=>$COURSE->id));
439 $items = $items ? $items : array();
440 foreach($items as $id=>$item) {
441 // Do not include grades for current item
442 if (!empty($this->_cm) && $this->_cm->instance == $item->iteminstance
443 && $this->_cm->modname == $item->itemmodule
444 && $item->itemtype == 'mod') {
445 continue;
447 $gradeoptions[$id] = $item->get_name();
449 asort($gradeoptions);
450 $gradeoptions = array(0=>get_string('none','condition'))+$gradeoptions;
452 $grouparray = array();
453 $grouparray[] =& $mform->createElement('select','conditiongradeitemid','',$gradeoptions);
454 $grouparray[] =& $mform->createElement('static', '', '',' '.get_string('grade_atleast','condition').' ');
455 $grouparray[] =& $mform->createElement('text', 'conditiongrademin','',array('size'=>3));
456 $grouparray[] =& $mform->createElement('static', '', '','% '.get_string('grade_upto','condition').' ');
457 $grouparray[] =& $mform->createElement('text', 'conditiongrademax','',array('size'=>3));
458 $grouparray[] =& $mform->createElement('static', '', '','%');
459 $mform->setType('conditiongrademin',PARAM_FLOAT);
460 $mform->setType('conditiongrademax',PARAM_FLOAT);
461 $group = $mform->createElement('group','conditiongradegroup',
462 get_string('gradecondition', 'condition'),$grouparray);
464 // Get version with condition info and store it so we don't ask
465 // twice
466 if(!empty($this->_cm)) {
467 $ci = new condition_info($this->_cm, CONDITION_MISSING_EXTRATABLE);
468 $this->_cm = $ci->get_full_course_module();
469 $count = count($this->_cm->conditionsgrade)+1;
470 } else {
471 $count = 1;
474 $this->repeat_elements(array($group), $count, array(), 'conditiongraderepeats', 'conditiongradeadds', 2,
475 get_string('addgrades', 'condition'), true);
476 $mform->addHelpButton('conditiongradegroup[0]', 'gradecondition', 'condition');
478 // Conditions based on completion
479 $completion = new completion_info($COURSE);
480 if ($completion->is_enabled()) {
481 $completionoptions = array();
482 $modinfo = get_fast_modinfo($COURSE);
483 foreach($modinfo->cms as $id=>$cm) {
484 // Add each course-module if it:
485 // (a) has completion turned on
486 // (b) is not the same as current course-module
487 if ($cm->completion && (empty($this->_cm) || $this->_cm->id != $id)) {
488 $completionoptions[$id]=$cm->name;
491 asort($completionoptions);
492 $completionoptions = array(0=>get_string('none','condition'))+$completionoptions;
494 $completionvalues=array(
495 COMPLETION_COMPLETE=>get_string('completion_complete','condition'),
496 COMPLETION_INCOMPLETE=>get_string('completion_incomplete','condition'),
497 COMPLETION_COMPLETE_PASS=>get_string('completion_pass','condition'),
498 COMPLETION_COMPLETE_FAIL=>get_string('completion_fail','condition'));
500 $grouparray = array();
501 $grouparray[] =& $mform->createElement('select','conditionsourcecmid','',$completionoptions);
502 $grouparray[] =& $mform->createElement('select','conditionrequiredcompletion','',$completionvalues);
503 $group = $mform->createElement('group','conditioncompletiongroup',
504 get_string('completioncondition', 'condition'),$grouparray);
506 $count = empty($this->_cm) ? 1 : count($this->_cm->conditionscompletion)+1;
507 $this->repeat_elements(array($group),$count,array(),
508 'conditioncompletionrepeats','conditioncompletionadds',2,
509 get_string('addcompletions','condition'),true);
510 $mform->addHelpButton('conditioncompletiongroup[0]', 'completioncondition', 'condition');
513 // Do we display availability info to students?
514 $mform->addElement('select', 'showavailability', get_string('showavailability', 'condition'),
515 array(CONDITION_STUDENTVIEW_SHOW=>get_string('showavailability_show', 'condition'),
516 CONDITION_STUDENTVIEW_HIDE=>get_string('showavailability_hide', 'condition')));
517 $mform->setDefault('showavailability', CONDITION_STUDENTVIEW_SHOW);
520 // Conditional activities: completion tracking section
521 if(!isset($completion)) {
522 $completion = new completion_info($COURSE);
524 if ($completion->is_enabled()) {
525 $mform->addElement('header', 'activitycompletionheader', get_string('activitycompletion', 'completion'));
527 // Unlock button for if people have completed it (will
528 // be removed in definition_after_data if they haven't)
529 $mform->addElement('submit', 'unlockcompletion', get_string('unlockcompletion', 'completion'));
530 $mform->registerNoSubmitButton('unlockcompletion');
531 $mform->addElement('hidden', 'completionunlocked', 0);
532 $mform->setType('completionunlocked', PARAM_INT);
534 $mform->addElement('select', 'completion', get_string('completion', 'completion'),
535 array(COMPLETION_TRACKING_NONE=>get_string('completion_none', 'completion'),
536 COMPLETION_TRACKING_MANUAL=>get_string('completion_manual', 'completion')));
537 $mform->setDefault('completion', $this->_features->defaultcompletion
538 ? COMPLETION_TRACKING_MANUAL
539 : COMPLETION_TRACKING_NONE);
540 $mform->addHelpButton('completion', 'completion', 'completion');
542 // Automatic completion once you view it
543 $gotcompletionoptions = false;
544 if (plugin_supports('mod', $this->_modname, FEATURE_COMPLETION_TRACKS_VIEWS, false)) {
545 $mform->addElement('checkbox', 'completionview', get_string('completionview', 'completion'),
546 get_string('completionview_desc', 'completion'));
547 $mform->disabledIf('completionview', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
548 $gotcompletionoptions = true;
551 // Automatic completion once it's graded
552 if (plugin_supports('mod', $this->_modname, FEATURE_GRADE_HAS_GRADE, false)) {
553 $mform->addElement('checkbox', 'completionusegrade', get_string('completionusegrade', 'completion'),
554 get_string('completionusegrade_desc', 'completion'));
555 $mform->disabledIf('completionusegrade', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
556 $mform->addHelpButton('completionusegrade', 'completionusegrade', 'completion');
557 $gotcompletionoptions = true;
560 // Automatic completion according to module-specific rules
561 $this->_customcompletionelements = $this->add_completion_rules();
562 foreach ($this->_customcompletionelements as $element) {
563 $mform->disabledIf($element, 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
566 $gotcompletionoptions = $gotcompletionoptions ||
567 count($this->_customcompletionelements)>0;
569 // Automatic option only appears if possible
570 if ($gotcompletionoptions) {
571 $mform->getElement('completion')->addOption(
572 get_string('completion_automatic', 'completion'),
573 COMPLETION_TRACKING_AUTOMATIC);
576 // Completion expected at particular date? (For progress tracking)
577 $mform->addElement('date_selector', 'completionexpected', get_string('completionexpected', 'completion'), array('optional'=>true));
578 $mform->addHelpButton('completionexpected', 'completionexpected', 'completion');
579 $mform->disabledIf('completionexpected', 'completion', 'eq', COMPLETION_TRACKING_NONE);
582 $this->standard_hidden_coursemodule_elements();
586 * Can be overridden to add custom completion rules if the module wishes
587 * them. If overriding this, you should also override completion_rule_enabled.
588 * <p>
589 * Just add elements to the form as needed and return the list of IDs. The
590 * system will call disabledIf and handle other behaviour for each returned
591 * ID.
592 * @return array Array of string IDs of added items, empty array if none
594 function add_completion_rules() {
595 return array();
599 * Called during validation. Override to indicate, based on the data, whether
600 * a custom completion rule is enabled (selected).
602 * @param array $data Input data (not yet validated)
603 * @return bool True if one or more rules is enabled, false if none are;
604 * default returns false
606 function completion_rule_enabled(&$data) {
607 return false;
610 function standard_hidden_coursemodule_elements(){
611 $mform =& $this->_form;
612 $mform->addElement('hidden', 'course', 0);
613 $mform->setType('course', PARAM_INT);
615 $mform->addElement('hidden', 'coursemodule', 0);
616 $mform->setType('coursemodule', PARAM_INT);
618 $mform->addElement('hidden', 'section', 0);
619 $mform->setType('section', PARAM_INT);
621 $mform->addElement('hidden', 'module', 0);
622 $mform->setType('module', PARAM_INT);
624 $mform->addElement('hidden', 'modulename', '');
625 $mform->setType('modulename', PARAM_SAFEDIR);
627 $mform->addElement('hidden', 'instance', 0);
628 $mform->setType('instance', PARAM_INT);
630 $mform->addElement('hidden', 'add', 0);
631 $mform->setType('add', PARAM_ALPHA);
633 $mform->addElement('hidden', 'update', 0);
634 $mform->setType('update', PARAM_INT);
636 $mform->addElement('hidden', 'return', 0);
637 $mform->setType('return', PARAM_BOOL);
640 public function standard_grading_coursemodule_elements() {
641 global $COURSE, $CFG;
642 $mform =& $this->_form;
644 if ($this->_features->hasgrades) {
646 if (!$this->_features->rating || $this->_features->gradecat) {
647 $mform->addElement('header', 'modstandardgrade', get_string('grade'));
650 //if supports grades and grades arent being handled via ratings
651 if (!$this->_features->rating) {
652 $mform->addElement('modgrade', 'grade', get_string('grade'));
653 $mform->setDefault('grade', 100);
656 if ($this->_features->gradecat) {
657 $categories = grade_get_categories_menu($COURSE->id, $this->_outcomesused);
658 $mform->addElement('select', 'gradecat', get_string('gradecategory', 'grades'), $categories);
663 function add_intro_editor($required=false, $customlabel=null) {
664 if (!$this->_features->introeditor) {
665 // intro editor not supported in this module
666 return;
669 $mform = $this->_form;
670 $label = is_null($customlabel) ? get_string('moduleintro') : $customlabel;
672 $mform->addElement('editor', 'introeditor', $label, null, array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$this->context));
673 $mform->setType('introeditor', PARAM_RAW); // no XSS prevention here, users must be trusted
674 if ($required) {
675 $mform->addRule('introeditor', get_string('required'), 'required', null, 'client');
680 * Overriding formslib's add_action_buttons() method, to add an extra submit "save changes and return" button.
682 * @param bool $cancel show cancel button
683 * @param string $submitlabel null means default, false means none, string is label text
684 * @param string $submit2label null means default, false means none, string is label text
685 * @return void
687 function add_action_buttons($cancel=true, $submitlabel=null, $submit2label=null) {
688 if (is_null($submitlabel)) {
689 $submitlabel = get_string('savechangesanddisplay');
692 if (is_null($submit2label)) {
693 $submit2label = get_string('savechangesandreturntocourse');
696 $mform = $this->_form;
698 // elements in a row need a group
699 $buttonarray = array();
701 if ($submit2label !== false) {
702 $buttonarray[] = &$mform->createElement('submit', 'submitbutton2', $submit2label);
705 if ($submitlabel !== false) {
706 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
709 if ($cancel) {
710 $buttonarray[] = &$mform->createElement('cancel');
713 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
714 $mform->setType('buttonar', PARAM_RAW);
715 $mform->closeHeaderBefore('buttonar');