2 require_once ($CFG->libdir
.'/formslib.php');
3 require_once($CFG->libdir
.'/completionlib.php');
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
9 abstract class moodleform_mod
extends moodleform
{
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
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).
28 * Course module record of the module that is being updated. Will be null if this is an 'add' form and not an
35 * List of modform features
39 * @var array Custom completion-rule elements, if enabled
41 protected $_customcompletionelements;
43 * @var string name of module
46 /** current context, course or module depends if already exists*/
49 /** a flag indicating whether outcomes are being used*/
50 protected $_outcomesused;
53 * @var bool A flag used to indicate that this module should lock settings
54 * based on admin settings flags in definition_after_data.
56 protected $applyadminlockedflags = false;
58 /** @var object The course format of the current course. */
59 protected $courseformat;
61 function moodleform_mod($current, $section, $cm, $course) {
64 $this->current
= $current;
65 $this->_instance
= $current->instance
;
66 $this->_section
= $section;
69 $this->context
= context_module
::instance($this->_cm
->id
);
71 $this->context
= context_course
::instance($course->id
);
74 // Set the course format.
75 require_once($CFG->dirroot
. '/course/format/lib.php');
76 $this->courseformat
= course_get_format($course);
80 if (!preg_match('/^mod_([^_]+)_mod_form$/', get_class($this), $matches)) {
81 debugging('Use $modname parameter or rename form to mod_xx_mod_form, where xx is name of your module');
82 print_error('unknownmodulename');
84 $this->_modname
= $matches[1];
85 $this->init_features();
86 parent
::moodleform('modedit.php');
89 protected function init_features() {
92 $this->_features
= new stdClass();
93 $this->_features
->groups
= plugin_supports('mod', $this->_modname
, FEATURE_GROUPS
, true);
94 $this->_features
->groupings
= plugin_supports('mod', $this->_modname
, FEATURE_GROUPINGS
, false);
95 $this->_features
->groupmembersonly
= (!empty($CFG->enablegroupmembersonly
) and plugin_supports('mod', $this->_modname
, FEATURE_GROUPMEMBERSONLY
, false));
96 $this->_features
->outcomes
= (!empty($CFG->enableoutcomes
) and plugin_supports('mod', $this->_modname
, FEATURE_GRADE_OUTCOMES
, true));
97 $this->_features
->hasgrades
= plugin_supports('mod', $this->_modname
, FEATURE_GRADE_HAS_GRADE
, false);
98 $this->_features
->idnumber
= plugin_supports('mod', $this->_modname
, FEATURE_IDNUMBER
, true);
99 $this->_features
->introeditor
= plugin_supports('mod', $this->_modname
, FEATURE_MOD_INTRO
, true);
100 $this->_features
->defaultcompletion
= plugin_supports('mod', $this->_modname
, FEATURE_MODEDIT_DEFAULT_COMPLETION
, true);
101 $this->_features
->rating
= plugin_supports('mod', $this->_modname
, FEATURE_RATE
, false);
102 $this->_features
->showdescription
= plugin_supports('mod', $this->_modname
, FEATURE_SHOW_DESCRIPTION
, false);
104 $this->_features
->gradecat
= ($this->_features
->outcomes
or $this->_features
->hasgrades
);
105 $this->_features
->advancedgrading
= plugin_supports('mod', $this->_modname
, FEATURE_ADVANCED_GRADING
, false);
109 * Only available on moodleform_mod.
111 * @param array $default_values passed by reference
113 function data_preprocessing(&$default_values){
114 if (empty($default_values['scale'])) {
115 $default_values['assessed'] = 0;
118 if (empty($default_values['assessed'])){
119 $default_values['ratingtime'] = 0;
121 $default_values['ratingtime']=
122 ($default_values['assesstimestart'] && $default_values['assesstimefinish']) ?
1 : 0;
127 * Each module which defines definition_after_data() must call this method using parent::definition_after_data();
129 function definition_after_data() {
130 global $CFG, $COURSE;
131 $mform =& $this->_form
;
133 if ($id = $mform->getElementValue('update')) {
134 $modulename = $mform->getElementValue('modulename');
135 $instance = $mform->getElementValue('instance');
137 if ($this->_features
->gradecat
) {
139 if (!empty($CFG->enableoutcomes
) and $this->_features
->outcomes
) {
140 $outcomes = grade_outcome
::fetch_all_available($COURSE->id
);
141 if (!empty($outcomes)) {
146 $items = grade_item
::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename,'iteminstance'=>$instance, 'courseid'=>$COURSE->id
));
147 //will be no items if, for example, this activity supports ratings but rating aggregate type == no ratings
148 if (!empty($items)) {
149 foreach ($items as $item) {
150 if (!empty($item->outcomeid
)) {
151 $elname = 'outcome_'.$item->outcomeid
;
152 if ($mform->elementExists($elname)) {
153 $mform->hardFreeze($elname); // prevent removing of existing outcomes
158 foreach ($items as $item) {
159 if (is_bool($gradecat)) {
160 $gradecat = $item->categoryid
;
163 if ($gradecat != $item->categoryid
) {
171 if ($gradecat === false) {
172 // items and outcomes in different categories - remove the option
173 // TODO: add a "Mixed categories" text instead of removing elements with no explanation
174 if ($mform->elementExists('gradecat')) {
175 $mform->removeElement('gradecat');
176 if ($this->_features
->rating
) {
177 //if supports ratings then the max grade dropdown wasnt added so the grade box can be removed entirely
178 $mform->removeElement('modstandardgrade');
185 if ($COURSE->groupmodeforce
) {
186 if ($mform->elementExists('groupmode')) {
187 $mform->hardFreeze('groupmode'); // groupmode can not be changed if forced from course settings
191 // Don't disable/remove groupingid if it is currently set to something,
192 // otherwise you cannot turn it off at same time as turning off other
193 // option (MDL-30764)
194 if (empty($this->_cm
) ||
!$this->_cm
->groupingid
) {
195 if ($mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly') and empty($COURSE->groupmodeforce
)) {
196 $mform->disabledIf('groupingid', 'groupmode', 'eq', NOGROUPS
);
198 } else if (!$mform->elementExists('groupmode') and $mform->elementExists('groupmembersonly')) {
199 $mform->disabledIf('groupingid', 'groupmembersonly', 'notchecked');
201 } else if (!$mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly')) {
202 // groupings have no use without groupmode or groupmembersonly
203 if ($mform->elementExists('groupingid')) {
204 $mform->removeElement('groupingid');
209 // Completion: If necessary, freeze fields
210 $completion = new completion_info($COURSE);
211 if ($completion->is_enabled()) {
212 // If anybody has completed the activity, these options will be 'locked'
213 $completedcount = empty($this->_cm
)
215 : $completion->count_user_data($this->_cm
);
218 if (!$completedcount) {
219 if ($mform->elementExists('unlockcompletion')) {
220 $mform->removeElement('unlockcompletion');
222 // Automatically set to unlocked (note: this is necessary
223 // in order to make it recalculate completion once the option
224 // is changed, maybe someone has completed it now)
225 $mform->getElement('completionunlocked')->setValue(1);
227 // Has the element been unlocked?
228 if ($mform->exportValue('unlockcompletion')) {
229 // Yes, add in warning text and set the hidden variable
230 $mform->insertElementBefore(
231 $mform->createElement('static', 'completedunlocked',
232 get_string('completedunlocked', 'completion'),
233 get_string('completedunlockedtext', 'completion')),
235 $mform->removeElement('unlockcompletion');
236 $mform->getElement('completionunlocked')->setValue(1);
238 // No, add in the warning text with the count (now we know
239 // it) before the unlock button
240 $mform->insertElementBefore(
241 $mform->createElement('static', 'completedwarning',
242 get_string('completedwarning', 'completion'),
243 get_string('completedwarningtext', 'completion', $completedcount)),
250 $mform->freeze('completion');
251 if ($mform->elementExists('completionview')) {
252 $mform->freeze('completionview'); // don't use hardFreeze or checkbox value gets lost
254 if ($mform->elementExists('completionusegrade')) {
255 $mform->freeze('completionusegrade');
257 $mform->freeze($this->_customcompletionelements
);
261 // Availability conditions
262 if (!empty($CFG->enableavailability
) && $this->_cm
) {
263 $ci = new condition_info($this->_cm
);
264 $fullcm=$ci->get_full_course_module();
267 foreach($fullcm->conditionsgrade
as $gradeitemid=>$minmax) {
268 $groupelements=$mform->getElement('conditiongradegroup['.$num.']')->getElements();
269 $groupelements[0]->setValue($gradeitemid);
270 $groupelements[2]->setValue(is_null($minmax->min
) ?
'' :
271 format_float($minmax->min
, 5, true, true));
272 $groupelements[4]->setValue(is_null($minmax->max
) ?
'' :
273 format_float($minmax->max
, 5, true, true));
278 foreach($fullcm->conditionsfield
as $field => $details) {
279 $groupelements = $mform->getElement('conditionfieldgroup['.$num.']')->getElements();
280 $groupelements[0]->setValue($field);
281 $groupelements[1]->setValue(is_null($details->operator
) ?
'' : $details->operator
);
282 $groupelements[2]->setValue(is_null($details->value
) ?
'' : $details->value
);
286 if ($completion->is_enabled()) {
288 foreach($fullcm->conditionscompletion
as $othercmid=>$state) {
289 $groupelements=$mform->getElement('conditioncompletiongroup['.$num.']')->getElements();
290 $groupelements[0]->setValue($othercmid);
291 $groupelements[1]->setValue($state);
297 // Freeze admin defaults if required (and not different from default)
298 $this->apply_admin_locked_flags();
302 function validation($data, $files) {
304 $errors = parent
::validation($data, $files);
306 $mform =& $this->_form
;
310 if ($mform->elementExists('name')) {
311 $name = trim($data['name']);
313 $errors['name'] = get_string('required');
317 $grade_item = grade_item
::fetch(array('itemtype'=>'mod', 'itemmodule'=>$data['modulename'],
318 'iteminstance'=>$data['instance'], 'itemnumber'=>0, 'courseid'=>$COURSE->id
));
319 if ($data['coursemodule']) {
320 $cm = $DB->get_record('course_modules', array('id'=>$data['coursemodule']));
325 if ($mform->elementExists('cmidnumber')) {
326 // verify the idnumber
327 if (!grade_verify_idnumber($data['cmidnumber'], $COURSE->id
, $grade_item, $cm)) {
328 $errors['cmidnumber'] = get_string('idnumbertaken');
332 // Completion: Don't let them choose automatic completion without turning
333 // on some conditions. Ignore this check when completion settings are
334 // locked, as the options are then disabled.
335 if (array_key_exists('completion', $data) &&
336 $data['completion'] == COMPLETION_TRACKING_AUTOMATIC
&&
337 !empty($data['completionunlocked'])) {
338 if (empty($data['completionview']) && empty($data['completionusegrade']) &&
339 !$this->completion_rule_enabled($data)) {
340 $errors['completion'] = get_string('badautocompletion', 'completion');
344 // Conditions: Don't let them set dates which make no sense
345 if (array_key_exists('availablefrom', $data) &&
346 $data['availablefrom'] && $data['availableuntil'] &&
347 $data['availablefrom'] >= $data['availableuntil']) {
348 $errors['availablefrom'] = get_string('badavailabledates', 'condition');
351 // Conditions: Verify that the grade conditions are numbers, and make sense.
352 if (array_key_exists('conditiongradegroup', $data)) {
353 foreach ($data['conditiongradegroup'] as $i => $gradedata) {
354 if ($gradedata['conditiongrademin'] !== '' &&
355 !is_numeric(unformat_float($gradedata['conditiongrademin']))) {
356 $errors["conditiongradegroup[{$i}]"] = get_string('gradesmustbenumeric', 'condition');
359 if ($gradedata['conditiongrademax'] !== '' &&
360 !is_numeric(unformat_float($gradedata['conditiongrademax']))) {
361 $errors["conditiongradegroup[{$i}]"] = get_string('gradesmustbenumeric', 'condition');
364 if ($gradedata['conditiongrademin'] !== '' && $gradedata['conditiongrademax'] !== '' &&
365 unformat_float($gradedata['conditiongrademax']) <= unformat_float($gradedata['conditiongrademin'])) {
366 $errors["conditiongradegroup[{$i}]"] = get_string('badgradelimits', 'condition');
369 if ($gradedata['conditiongrademin'] === '' && $gradedata['conditiongrademax'] === '' &&
370 $gradedata['conditiongradeitemid']) {
371 $errors["conditiongradegroup[{$i}]"] = get_string('gradeitembutnolimits', 'condition');
374 if (($gradedata['conditiongrademin'] !== '' ||
$gradedata['conditiongrademax'] !== '') &&
375 !$gradedata['conditiongradeitemid']) {
376 $errors["conditiongradegroup[{$i}]"] = get_string('gradelimitsbutnoitem', 'condition');
382 // Conditions: Verify that the user profile field has not been declared more than once
383 if (array_key_exists('conditionfieldgroup', $data)) {
384 // Array to store the existing fields
385 $arrcurrentfields = array();
386 // Error message displayed if any condition is declared more than once. We use lang string because
387 // this way we don't actually generate the string unless there is an error.
388 $stralreadydeclaredwarning = new lang_string('fielddeclaredmultipletimes', 'condition');
389 foreach ($data['conditionfieldgroup'] as $i => $fielddata) {
390 if ($fielddata['conditionfield'] == 0) { // Don't need to bother if none is selected
393 if (in_array($fielddata['conditionfield'], $arrcurrentfields)) {
394 $errors["conditionfieldgroup[{$i}]"] = $stralreadydeclaredwarning->out();
396 // Add the field to the array
397 $arrcurrentfields[] = $fielddata['conditionfield'];
405 * Load in existing data as form defaults. Usually new entry defaults are stored directly in
406 * form definition (new entry form); this function is used to load in data where values
407 * already exist and data is being edited (edit entry form).
409 * @param mixed $default_values object or array of default values
411 function set_data($default_values) {
412 if (is_object($default_values)) {
413 $default_values = (array)$default_values;
416 $this->data_preprocessing($default_values);
417 parent
::set_data($default_values);
421 * Adds all the standard elements to a form to edit the settings for an activity module.
423 function standard_coursemodule_elements(){
424 global $COURSE, $CFG, $DB;
425 $mform =& $this->_form
;
427 $this->_outcomesused
= false;
428 if ($this->_features
->outcomes
) {
429 if ($outcomes = grade_outcome
::fetch_all_available($COURSE->id
)) {
430 $this->_outcomesused
= true;
431 $mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades'));
432 foreach($outcomes as $outcome) {
433 $mform->addElement('advcheckbox', 'outcome_'.$outcome->id
, $outcome->get_name());
439 if ($this->_features
->rating
) {
440 require_once($CFG->dirroot
.'/rating/lib.php');
441 $rm = new rating_manager();
443 $mform->addElement('header', 'modstandardratings', get_string('ratings', 'rating'));
445 $permission=CAP_ALLOW
;
446 $rolenamestring = null;
447 if (!empty($this->_cm
)) {
448 $context = context_module
::instance($this->_cm
->id
);
450 $rolenames = get_role_names_with_caps_in_context($context, array('moodle/rating:rate', 'mod/'.$this->_cm
->modname
.':rate'));
451 $rolenamestring = implode(', ', $rolenames);
453 $rolenamestring = get_string('capabilitychecknotavailable','rating');
455 $mform->addElement('static', 'rolewarning', get_string('rolewarning','rating'), $rolenamestring);
456 $mform->addHelpButton('rolewarning', 'rolewarning', 'rating');
458 $mform->addElement('select', 'assessed', get_string('aggregatetype', 'rating') , $rm->get_aggregate_types());
459 $mform->setDefault('assessed', 0);
460 $mform->addHelpButton('assessed', 'aggregatetype', 'rating');
462 $mform->addElement('modgrade', 'scale', get_string('scale'), false);
463 $mform->disabledIf('scale', 'assessed', 'eq', 0);
465 $mform->addElement('checkbox', 'ratingtime', get_string('ratingtime', 'rating'));
466 $mform->disabledIf('ratingtime', 'assessed', 'eq', 0);
468 $mform->addElement('date_time_selector', 'assesstimestart', get_string('from'));
469 $mform->disabledIf('assesstimestart', 'assessed', 'eq', 0);
470 $mform->disabledIf('assesstimestart', 'ratingtime');
472 $mform->addElement('date_time_selector', 'assesstimefinish', get_string('to'));
473 $mform->disabledIf('assesstimefinish', 'assessed', 'eq', 0);
474 $mform->disabledIf('assesstimefinish', 'ratingtime');
477 //doing this here means splitting up the grade related settings on the lesson settings page
478 //$this->standard_grading_coursemodule_elements();
480 $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
482 $mform->addElement('modvisible', 'visible', get_string('visible'));
483 if (!empty($this->_cm
)) {
484 $context = context_module
::instance($this->_cm
->id
);
485 if (!has_capability('moodle/course:activityvisibility', $context)) {
486 $mform->hardFreeze('visible');
490 if ($this->_features
->idnumber
) {
491 $mform->addElement('text', 'cmidnumber', get_string('idnumbermod'));
492 $mform->setType('cmidnumber', PARAM_RAW
);
493 $mform->addHelpButton('cmidnumber', 'idnumbermod');
496 if ($this->_features
->groups
) {
497 $options = array(NOGROUPS
=> get_string('groupsnone'),
498 SEPARATEGROUPS
=> get_string('groupsseparate'),
499 VISIBLEGROUPS
=> get_string('groupsvisible'));
500 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $options, NOGROUPS
);
501 $mform->addHelpButton('groupmode', 'groupmode', 'group');
504 if ($this->_features
->groupings
or $this->_features
->groupmembersonly
) {
505 //groupings selector - used for normal grouping mode or also when restricting access with groupmembersonly
507 $options[0] = get_string('none');
508 if ($groupings = $DB->get_records('groupings', array('courseid'=>$COURSE->id
))) {
509 foreach ($groupings as $grouping) {
510 $options[$grouping->id
] = format_string($grouping->name
);
513 $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options);
514 $mform->addHelpButton('groupingid', 'grouping', 'group');
517 if ($this->_features
->groupmembersonly
) {
518 $mform->addElement('checkbox', 'groupmembersonly', get_string('groupmembersonly', 'group'));
519 $mform->addHelpButton('groupmembersonly', 'groupmembersonly', 'group');
522 if (!empty($CFG->enableavailability
)) {
523 // String used by conditions
524 $strnone = get_string('none','condition');
525 // Conditional availability
527 // Available from/to defaults to midnight because then the display
528 // will be nicer where it tells users when they can access it (it
529 // shows only the date and not time).
530 $date = usergetdate(time());
531 $midnight = make_timestamp($date['year'], $date['mon'], $date['mday']);
533 // From/until controls
534 $mform->addElement('header', 'availabilityconditionsheader',
535 get_string('availabilityconditions', 'condition'));
536 $mform->addElement('date_time_selector', 'availablefrom',
537 get_string('availablefrom', 'condition'),
538 array('optional' => true, 'defaulttime' => $midnight));
539 $mform->addHelpButton('availablefrom', 'availablefrom', 'condition');
540 $mform->addElement('date_time_selector', 'availableuntil',
541 get_string('availableuntil', 'condition'),
542 array('optional' => true, 'defaulttime' => $midnight));
544 // Conditions based on grades
545 $gradeoptions = array();
546 $items = grade_item
::fetch_all(array('courseid'=>$COURSE->id
));
547 $items = $items ?
$items : array();
548 foreach($items as $id=>$item) {
549 // Do not include grades for current item
550 if (!empty($this->_cm
) && $this->_cm
->instance
== $item->iteminstance
551 && $this->_cm
->modname
== $item->itemmodule
552 && $item->itemtype
== 'mod') {
555 $gradeoptions[$id] = $item->get_name();
557 asort($gradeoptions);
558 $gradeoptions = array(0 => $strnone) +
$gradeoptions;
560 $grouparray = array();
561 $grouparray[] =& $mform->createElement('select','conditiongradeitemid','',$gradeoptions);
562 $grouparray[] =& $mform->createElement('static', '', '',' '.get_string('grade_atleast','condition').' ');
563 $grouparray[] =& $mform->createElement('text', 'conditiongrademin','',array('size'=>3));
564 $grouparray[] =& $mform->createElement('static', '', '','% '.get_string('grade_upto','condition').' ');
565 $grouparray[] =& $mform->createElement('text', 'conditiongrademax','',array('size'=>3));
566 $grouparray[] =& $mform->createElement('static', '', '','%');
567 $group = $mform->createElement('group','conditiongradegroup',
568 get_string('gradecondition', 'condition'),$grouparray);
570 // Get version with condition info and store it so we don't ask
572 if(!empty($this->_cm
)) {
573 $ci = new condition_info($this->_cm
, CONDITION_MISSING_EXTRATABLE
);
574 $this->_cm
= $ci->get_full_course_module();
575 $count = count($this->_cm
->conditionsgrade
)+
1;
576 $fieldcount = count($this->_cm
->conditionsfield
) +
1;
582 $this->repeat_elements(array($group), $count, array(
583 'conditiongradegroup[conditiongrademin]' => array('type' => PARAM_RAW
),
584 'conditiongradegroup[conditiongrademax]' => array('type' => PARAM_RAW
)
585 ), 'conditiongraderepeats', 'conditiongradeadds', 2, get_string('addgrades', 'condition'), true);
586 $mform->addHelpButton('conditiongradegroup[0]', 'gradecondition', 'condition');
588 // Conditions based on user fields
589 $operators = condition_info
::get_condition_user_field_operators();
590 $useroptions = condition_info
::get_condition_user_fields(
591 array('context' => $this->context
));
594 $useroptions = array(0 => $strnone) +
$useroptions;
595 $grouparray = array();
596 $grouparray[] =& $mform->createElement('select', 'conditionfield', '', $useroptions);
597 $grouparray[] =& $mform->createElement('select', 'conditionfieldoperator', '', $operators);
598 $grouparray[] =& $mform->createElement('text', 'conditionfieldvalue');
599 $group = $mform->createElement('group', 'conditionfieldgroup', get_string('userfield', 'condition'), $grouparray);
601 $this->repeat_elements(array($group), $fieldcount, array(
602 'conditionfieldgroup[conditionfieldvalue]' => array('type' => PARAM_RAW
)),
603 'conditionfieldrepeats', 'conditionfieldadds', 2, get_string('adduserfields', 'condition'), true);
604 $mform->addHelpButton('conditionfieldgroup[0]', 'userfield', 'condition');
606 // Conditions based on completion
607 $completion = new completion_info($COURSE);
608 if ($completion->is_enabled()) {
609 $completionoptions = array();
610 $modinfo = get_fast_modinfo($COURSE);
611 foreach($modinfo->cms
as $id=>$cm) {
612 // Add each course-module if it:
613 // (a) has completion turned on
614 // (b) is not the same as current course-module
615 if ($cm->completion
&& (empty($this->_cm
) ||
$this->_cm
->id
!= $id)) {
616 $completionoptions[$id]=$cm->name
;
619 asort($completionoptions);
620 $completionoptions = array(0 => $strnone) +
$completionoptions;
622 $completionvalues=array(
623 COMPLETION_COMPLETE
=>get_string('completion_complete','condition'),
624 COMPLETION_INCOMPLETE
=>get_string('completion_incomplete','condition'),
625 COMPLETION_COMPLETE_PASS
=>get_string('completion_pass','condition'),
626 COMPLETION_COMPLETE_FAIL
=>get_string('completion_fail','condition'));
628 $grouparray = array();
629 $grouparray[] =& $mform->createElement('select','conditionsourcecmid','',$completionoptions);
630 $grouparray[] =& $mform->createElement('select','conditionrequiredcompletion','',$completionvalues);
631 $group = $mform->createElement('group','conditioncompletiongroup',
632 get_string('completioncondition', 'condition'),$grouparray);
634 $count = empty($this->_cm
) ?
1 : count($this->_cm
->conditionscompletion
)+
1;
635 $this->repeat_elements(array($group),$count,array(),
636 'conditioncompletionrepeats','conditioncompletionadds',2,
637 get_string('addcompletions','condition'),true);
638 $mform->addHelpButton('conditioncompletiongroup[0]', 'completioncondition', 'condition');
641 // Do we display availability info to students?
642 $mform->addElement('select', 'showavailability', get_string('showavailability', 'condition'),
643 array(CONDITION_STUDENTVIEW_SHOW
=>get_string('showavailability_show', 'condition'),
644 CONDITION_STUDENTVIEW_HIDE
=>get_string('showavailability_hide', 'condition')));
645 $mform->setDefault('showavailability', CONDITION_STUDENTVIEW_SHOW
);
648 // Conditional activities: completion tracking section
649 if(!isset($completion)) {
650 $completion = new completion_info($COURSE);
652 if ($completion->is_enabled()) {
653 $mform->addElement('header', 'activitycompletionheader', get_string('activitycompletion', 'completion'));
655 // Unlock button for if people have completed it (will
656 // be removed in definition_after_data if they haven't)
657 $mform->addElement('submit', 'unlockcompletion', get_string('unlockcompletion', 'completion'));
658 $mform->registerNoSubmitButton('unlockcompletion');
659 $mform->addElement('hidden', 'completionunlocked', 0);
660 $mform->setType('completionunlocked', PARAM_INT
);
662 $trackingdefault = COMPLETION_TRACKING_NONE
;
663 // If system and activity default is on, set it.
664 if ($CFG->completiondefault
&& $this->_features
->defaultcompletion
) {
665 $trackingdefault = COMPLETION_TRACKING_MANUAL
;
668 $mform->addElement('select', 'completion', get_string('completion', 'completion'),
669 array(COMPLETION_TRACKING_NONE
=>get_string('completion_none', 'completion'),
670 COMPLETION_TRACKING_MANUAL
=>get_string('completion_manual', 'completion')));
671 $mform->setDefault('completion', $trackingdefault);
672 $mform->addHelpButton('completion', 'completion', 'completion');
674 // Automatic completion once you view it
675 $gotcompletionoptions = false;
676 if (plugin_supports('mod', $this->_modname
, FEATURE_COMPLETION_TRACKS_VIEWS
, false)) {
677 $mform->addElement('checkbox', 'completionview', get_string('completionview', 'completion'),
678 get_string('completionview_desc', 'completion'));
679 $mform->disabledIf('completionview', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC
);
680 $gotcompletionoptions = true;
683 // Automatic completion once it's graded
684 if (plugin_supports('mod', $this->_modname
, FEATURE_GRADE_HAS_GRADE
, false)) {
685 $mform->addElement('checkbox', 'completionusegrade', get_string('completionusegrade', 'completion'),
686 get_string('completionusegrade_desc', 'completion'));
687 $mform->disabledIf('completionusegrade', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC
);
688 $mform->addHelpButton('completionusegrade', 'completionusegrade', 'completion');
689 $gotcompletionoptions = true;
692 // Automatic completion according to module-specific rules
693 $this->_customcompletionelements
= $this->add_completion_rules();
694 foreach ($this->_customcompletionelements
as $element) {
695 $mform->disabledIf($element, 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC
);
698 $gotcompletionoptions = $gotcompletionoptions ||
699 count($this->_customcompletionelements
)>0;
701 // Automatic option only appears if possible
702 if ($gotcompletionoptions) {
703 $mform->getElement('completion')->addOption(
704 get_string('completion_automatic', 'completion'),
705 COMPLETION_TRACKING_AUTOMATIC
);
708 // Completion expected at particular date? (For progress tracking)
709 $mform->addElement('date_selector', 'completionexpected', get_string('completionexpected', 'completion'), array('optional'=>true));
710 $mform->addHelpButton('completionexpected', 'completionexpected', 'completion');
711 $mform->disabledIf('completionexpected', 'completion', 'eq', COMPLETION_TRACKING_NONE
);
714 $this->standard_hidden_coursemodule_elements();
718 * Can be overridden to add custom completion rules if the module wishes
719 * them. If overriding this, you should also override completion_rule_enabled.
721 * Just add elements to the form as needed and return the list of IDs. The
722 * system will call disabledIf and handle other behaviour for each returned
724 * @return array Array of string IDs of added items, empty array if none
726 function add_completion_rules() {
731 * Called during validation. Override to indicate, based on the data, whether
732 * a custom completion rule is enabled (selected).
734 * @param array $data Input data (not yet validated)
735 * @return bool True if one or more rules is enabled, false if none are;
736 * default returns false
738 function completion_rule_enabled($data) {
742 function standard_hidden_coursemodule_elements(){
743 $mform =& $this->_form
;
744 $mform->addElement('hidden', 'course', 0);
745 $mform->setType('course', PARAM_INT
);
747 $mform->addElement('hidden', 'coursemodule', 0);
748 $mform->setType('coursemodule', PARAM_INT
);
750 $mform->addElement('hidden', 'section', 0);
751 $mform->setType('section', PARAM_INT
);
753 $mform->addElement('hidden', 'module', 0);
754 $mform->setType('module', PARAM_INT
);
756 $mform->addElement('hidden', 'modulename', '');
757 $mform->setType('modulename', PARAM_PLUGIN
);
759 $mform->addElement('hidden', 'instance', 0);
760 $mform->setType('instance', PARAM_INT
);
762 $mform->addElement('hidden', 'add', 0);
763 $mform->setType('add', PARAM_ALPHA
);
765 $mform->addElement('hidden', 'update', 0);
766 $mform->setType('update', PARAM_INT
);
768 $mform->addElement('hidden', 'return', 0);
769 $mform->setType('return', PARAM_BOOL
);
771 $mform->addElement('hidden', 'sr', 0);
772 $mform->setType('sr', PARAM_INT
);
775 public function standard_grading_coursemodule_elements() {
776 global $COURSE, $CFG;
777 $mform =& $this->_form
;
779 if ($this->_features
->hasgrades
) {
781 if (!$this->_features
->rating ||
$this->_features
->gradecat
) {
782 $mform->addElement('header', 'modstandardgrade', get_string('grade'));
785 //if supports grades and grades arent being handled via ratings
786 if (!$this->_features
->rating
) {
787 $mform->addElement('modgrade', 'grade', get_string('grade'));
788 $mform->setDefault('grade', 100);
791 if ($this->_features
->advancedgrading
792 and !empty($this->current
->_advancedgradingdata
['methods'])
793 and !empty($this->current
->_advancedgradingdata
['areas'])) {
795 if (count($this->current
->_advancedgradingdata
['areas']) == 1) {
796 // if there is just one gradable area (most cases), display just the selector
797 // without its name to make UI simplier
798 $areadata = reset($this->current
->_advancedgradingdata
['areas']);
799 $areaname = key($this->current
->_advancedgradingdata
['areas']);
800 $mform->addElement('select', 'advancedgradingmethod_'.$areaname,
801 get_string('gradingmethod', 'core_grading'), $this->current
->_advancedgradingdata
['methods']);
802 $mform->addHelpButton('advancedgradingmethod_'.$areaname, 'gradingmethod', 'core_grading');
805 // the module defines multiple gradable areas, display a selector
806 // for each of them together with a name of the area
807 $areasgroup = array();
808 foreach ($this->current
->_advancedgradingdata
['areas'] as $areaname => $areadata) {
809 $areasgroup[] = $mform->createElement('select', 'advancedgradingmethod_'.$areaname,
810 $areadata['title'], $this->current
->_advancedgradingdata
['methods']);
811 $areasgroup[] = $mform->createElement('static', 'advancedgradingareaname_'.$areaname, '', $areadata['title']);
813 $mform->addGroup($areasgroup, 'advancedgradingmethodsgroup', get_string('gradingmethods', 'core_grading'),
814 array(' ', '<br />'), false);
818 if ($this->_features
->gradecat
) {
819 $mform->addElement('select', 'gradecat',
820 get_string('gradecategoryonmodform', 'grades'),
821 grade_get_categories_menu($COURSE->id
, $this->_outcomesused
));
822 $mform->addHelpButton('gradecat', 'gradecategoryonmodform', 'grades');
827 function add_intro_editor($required=false, $customlabel=null) {
828 if (!$this->_features
->introeditor
) {
829 // intro editor not supported in this module
833 $mform = $this->_form
;
834 $label = is_null($customlabel) ?
get_string('moduleintro') : $customlabel;
836 $mform->addElement('editor', 'introeditor', $label, array('rows' => 10), array('maxfiles' => EDITOR_UNLIMITED_FILES
,
837 'noclean' => true, 'context' => $this->context
, 'subdirs' => true));
838 $mform->setType('introeditor', PARAM_RAW
); // no XSS prevention here, users must be trusted
840 $mform->addRule('introeditor', get_string('required'), 'required', null, 'client');
843 // If the 'show description' feature is enabled, this checkbox appears below the intro.
844 // We want to hide that when using the singleactivity course format because it is confusing.
845 if ($this->_features
->showdescription
&& $this->courseformat
->has_view_page()) {
846 $mform->addElement('checkbox', 'showdescription', get_string('showdescription'));
847 $mform->addHelpButton('showdescription', 'showdescription');
852 * Overriding formslib's add_action_buttons() method, to add an extra submit "save changes and return" button.
854 * @param bool $cancel show cancel button
855 * @param string $submitlabel null means default, false means none, string is label text
856 * @param string $submit2label null means default, false means none, string is label text
859 function add_action_buttons($cancel=true, $submitlabel=null, $submit2label=null) {
860 if (is_null($submitlabel)) {
861 $submitlabel = get_string('savechangesanddisplay');
864 if (is_null($submit2label)) {
865 $submit2label = get_string('savechangesandreturntocourse');
868 $mform = $this->_form
;
870 // elements in a row need a group
871 $buttonarray = array();
873 // Label for the submit button to return to the course.
874 // Ignore this button in single activity format because it is confusing.
875 if ($submit2label !== false && $this->courseformat
->has_view_page()) {
876 $buttonarray[] = &$mform->createElement('submit', 'submitbutton2', $submit2label);
879 if ($submitlabel !== false) {
880 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
884 $buttonarray[] = &$mform->createElement('cancel');
887 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
888 $mform->setType('buttonar', PARAM_RAW
);
889 $mform->closeHeaderBefore('buttonar');
893 * Get the list of admin settings for this module and apply any locked settings.
894 * This cannot happen in apply_admin_defaults because we do not the current values of the settings
895 * in that function because set_data has not been called yet.
899 protected function apply_admin_locked_flags() {
902 if (!$this->applyadminlockedflags
) {
906 $settings = get_config($this->_modname
);
907 $mform = $this->_form
;
908 $lockedicon = html_writer
::tag('span',
909 $OUTPUT->pix_icon('t/locked', get_string('locked', 'admin')),
910 array('class' => 'action-icon'));
911 $isupdate = !empty($this->_cm
);
913 foreach ($settings as $name => $value) {
914 if (strpos('_', $name) !== false) {
917 if ($mform->elementExists($name)) {
918 $element = $mform->getElement($name);
919 $lockedsetting = $name . '_locked';
920 if (!empty($settings->$lockedsetting)) {
921 // Always lock locked settings for new modules,
922 // for updates, only lock them if the current value is the same as the default (or there is no current value).
923 $value = $settings->$name;
924 if ($isupdate && isset($this->current
->$name)) {
925 $value = $this->current
->$name;
927 if ($value == $settings->$name) {
928 $mform->setConstant($name, $settings->$name);
929 $element->setLabel($element->getLabel() . $lockedicon);
930 // Do not use hardfreeze because we need the hidden input to check dependencies.
939 * Get the list of admin settings for this module and apply any defaults/advanced/locked settings.
941 * @param $datetimeoffsets array - If passed, this is an array of fieldnames => times that the
942 * default date/time value should be relative to. If not passed, all
943 * date/time fields are set relative to the users current midnight.
946 public function apply_admin_defaults($datetimeoffsets = array()) {
947 // This flag triggers the settings to be locked in apply_admin_locked_flags().
948 $this->applyadminlockedflags
= true;
950 $settings = get_config($this->_modname
);
951 $mform = $this->_form
;
952 $usermidnight = usergetmidnight(time());
953 $isupdate = !empty($this->_cm
);
955 foreach ($settings as $name => $value) {
956 if (strpos('_', $name) !== false) {
959 if ($mform->elementExists($name)) {
960 $element = $mform->getElement($name);
962 if ($element->getType() == 'date_time_selector') {
963 $enabledsetting = $name . '_enabled';
964 if (empty($settings->$enabledsetting)) {
965 $mform->setDefault($name, 0);
967 $relativetime = $usermidnight;
968 if (isset($datetimeoffsets[$name])) {
969 $relativetime = $datetimeoffsets[$name];
971 $mform->setDefault($name, $relativetime +
$settings->$name);
974 $mform->setDefault($name, $settings->$name);
977 $advancedsetting = $name . '_adv';
978 if (!empty($settings->$advancedsetting)) {
979 $mform->setAdvanced($name);