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 if ($groupings = $DB->get_records('groupings', array('courseid'=>$COURSE->id
))) {
508 foreach ($groupings as $grouping) {
509 $options[$grouping->id
] = format_string($grouping->name
);
512 core_collator
::asort($options);
513 $options = array(0 => get_string('none')) +
$options;
514 $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options);
515 $mform->addHelpButton('groupingid', 'grouping', 'group');
518 if ($this->_features
->groupmembersonly
) {
519 $mform->addElement('checkbox', 'groupmembersonly', get_string('groupmembersonly', 'group'));
520 $mform->addHelpButton('groupmembersonly', 'groupmembersonly', 'group');
523 if (!empty($CFG->enableavailability
)) {
524 // String used by conditions
525 $strnone = get_string('none','condition');
526 // Conditional availability
528 // Available from/to defaults to midnight because then the display
529 // will be nicer where it tells users when they can access it (it
530 // shows only the date and not time).
531 $date = usergetdate(time());
532 $midnight = make_timestamp($date['year'], $date['mon'], $date['mday']);
534 // From/until controls
535 $mform->addElement('header', 'availabilityconditionsheader',
536 get_string('availabilityconditions', 'condition'));
537 $mform->addElement('date_time_selector', 'availablefrom',
538 get_string('availablefrom', 'condition'),
539 array('optional' => true, 'defaulttime' => $midnight));
540 $mform->addHelpButton('availablefrom', 'availablefrom', 'condition');
541 $mform->addElement('date_time_selector', 'availableuntil',
542 get_string('availableuntil', 'condition'),
543 array('optional' => true, 'defaulttime' => $midnight));
545 // Conditions based on grades
546 $gradeoptions = array();
547 $items = grade_item
::fetch_all(array('courseid'=>$COURSE->id
));
548 $items = $items ?
$items : array();
549 foreach($items as $id=>$item) {
550 // Do not include grades for current item
551 if (!empty($this->_cm
) && $this->_cm
->instance
== $item->iteminstance
552 && $this->_cm
->modname
== $item->itemmodule
553 && $item->itemtype
== 'mod') {
556 $gradeoptions[$id] = $item->get_name();
558 asort($gradeoptions);
559 $gradeoptions = array(0 => $strnone) +
$gradeoptions;
561 $grouparray = array();
562 $grouparray[] =& $mform->createElement('select','conditiongradeitemid','',$gradeoptions);
563 $grouparray[] =& $mform->createElement('static', '', '',' '.get_string('grade_atleast','condition').' ');
564 $grouparray[] =& $mform->createElement('text', 'conditiongrademin','',array('size'=>3));
565 $grouparray[] =& $mform->createElement('static', '', '','% '.get_string('grade_upto','condition').' ');
566 $grouparray[] =& $mform->createElement('text', 'conditiongrademax','',array('size'=>3));
567 $grouparray[] =& $mform->createElement('static', '', '','%');
568 $group = $mform->createElement('group','conditiongradegroup',
569 get_string('gradecondition', 'condition'),$grouparray);
571 // Get version with condition info and store it so we don't ask
573 if(!empty($this->_cm
)) {
574 $ci = new condition_info($this->_cm
, CONDITION_MISSING_EXTRATABLE
);
575 $this->_cm
= $ci->get_full_course_module();
576 $count = count($this->_cm
->conditionsgrade
)+
1;
577 $fieldcount = count($this->_cm
->conditionsfield
) +
1;
583 $this->repeat_elements(array($group), $count, array(
584 'conditiongradegroup[conditiongrademin]' => array('type' => PARAM_RAW
),
585 'conditiongradegroup[conditiongrademax]' => array('type' => PARAM_RAW
)
586 ), 'conditiongraderepeats', 'conditiongradeadds', 2, get_string('addgrades', 'condition'), true);
587 $mform->addHelpButton('conditiongradegroup[0]', 'gradecondition', 'condition');
589 // Conditions based on user fields
590 $operators = condition_info
::get_condition_user_field_operators();
591 $useroptions = condition_info
::get_condition_user_fields(
592 array('context' => $this->context
));
595 $useroptions = array(0 => $strnone) +
$useroptions;
596 $grouparray = array();
597 $grouparray[] =& $mform->createElement('select', 'conditionfield', '', $useroptions);
598 $grouparray[] =& $mform->createElement('select', 'conditionfieldoperator', '', $operators);
599 $grouparray[] =& $mform->createElement('text', 'conditionfieldvalue');
600 $group = $mform->createElement('group', 'conditionfieldgroup', get_string('userfield', 'condition'), $grouparray);
602 $this->repeat_elements(array($group), $fieldcount, array(
603 'conditionfieldgroup[conditionfieldvalue]' => array('type' => PARAM_RAW
)),
604 'conditionfieldrepeats', 'conditionfieldadds', 2, get_string('adduserfields', 'condition'), true);
605 $mform->addHelpButton('conditionfieldgroup[0]', 'userfield', 'condition');
607 // Conditions based on completion
608 $completion = new completion_info($COURSE);
609 if ($completion->is_enabled()) {
610 $completionoptions = array();
611 $modinfo = get_fast_modinfo($COURSE);
612 foreach($modinfo->cms
as $id=>$cm) {
613 // Add each course-module if it:
614 // (a) has completion turned on
615 // (b) is not the same as current course-module
616 if ($cm->completion
&& (empty($this->_cm
) ||
$this->_cm
->id
!= $id)) {
617 $completionoptions[$id]=$cm->name
;
620 asort($completionoptions);
621 $completionoptions = array(0 => $strnone) +
$completionoptions;
623 $completionvalues=array(
624 COMPLETION_COMPLETE
=>get_string('completion_complete','condition'),
625 COMPLETION_INCOMPLETE
=>get_string('completion_incomplete','condition'),
626 COMPLETION_COMPLETE_PASS
=>get_string('completion_pass','condition'),
627 COMPLETION_COMPLETE_FAIL
=>get_string('completion_fail','condition'));
629 $grouparray = array();
630 $grouparray[] =& $mform->createElement('select','conditionsourcecmid','',$completionoptions);
631 $grouparray[] =& $mform->createElement('select','conditionrequiredcompletion','',$completionvalues);
632 $group = $mform->createElement('group','conditioncompletiongroup',
633 get_string('completioncondition', 'condition'),$grouparray);
635 $count = empty($this->_cm
) ?
1 : count($this->_cm
->conditionscompletion
)+
1;
636 $this->repeat_elements(array($group),$count,array(),
637 'conditioncompletionrepeats','conditioncompletionadds',2,
638 get_string('addcompletions','condition'),true);
639 $mform->addHelpButton('conditioncompletiongroup[0]', 'completioncondition', 'condition');
642 // Do we display availability info to students?
643 $mform->addElement('select', 'showavailability', get_string('showavailability', 'condition'),
644 array(CONDITION_STUDENTVIEW_SHOW
=>get_string('showavailability_show', 'condition'),
645 CONDITION_STUDENTVIEW_HIDE
=>get_string('showavailability_hide', 'condition')));
646 $mform->setDefault('showavailability', CONDITION_STUDENTVIEW_SHOW
);
649 // Conditional activities: completion tracking section
650 if(!isset($completion)) {
651 $completion = new completion_info($COURSE);
653 if ($completion->is_enabled()) {
654 $mform->addElement('header', 'activitycompletionheader', get_string('activitycompletion', 'completion'));
656 // Unlock button for if people have completed it (will
657 // be removed in definition_after_data if they haven't)
658 $mform->addElement('submit', 'unlockcompletion', get_string('unlockcompletion', 'completion'));
659 $mform->registerNoSubmitButton('unlockcompletion');
660 $mform->addElement('hidden', 'completionunlocked', 0);
661 $mform->setType('completionunlocked', PARAM_INT
);
663 $trackingdefault = COMPLETION_TRACKING_NONE
;
664 // If system and activity default is on, set it.
665 if ($CFG->completiondefault
&& $this->_features
->defaultcompletion
) {
666 $trackingdefault = COMPLETION_TRACKING_MANUAL
;
669 $mform->addElement('select', 'completion', get_string('completion', 'completion'),
670 array(COMPLETION_TRACKING_NONE
=>get_string('completion_none', 'completion'),
671 COMPLETION_TRACKING_MANUAL
=>get_string('completion_manual', 'completion')));
672 $mform->setDefault('completion', $trackingdefault);
673 $mform->addHelpButton('completion', 'completion', 'completion');
675 // Automatic completion once you view it
676 $gotcompletionoptions = false;
677 if (plugin_supports('mod', $this->_modname
, FEATURE_COMPLETION_TRACKS_VIEWS
, false)) {
678 $mform->addElement('checkbox', 'completionview', get_string('completionview', 'completion'),
679 get_string('completionview_desc', 'completion'));
680 $mform->disabledIf('completionview', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC
);
681 $gotcompletionoptions = true;
684 // Automatic completion once it's graded
685 if (plugin_supports('mod', $this->_modname
, FEATURE_GRADE_HAS_GRADE
, false)) {
686 $mform->addElement('checkbox', 'completionusegrade', get_string('completionusegrade', 'completion'),
687 get_string('completionusegrade_desc', 'completion'));
688 $mform->disabledIf('completionusegrade', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC
);
689 $mform->addHelpButton('completionusegrade', 'completionusegrade', 'completion');
690 $gotcompletionoptions = true;
693 // Automatic completion according to module-specific rules
694 $this->_customcompletionelements
= $this->add_completion_rules();
695 foreach ($this->_customcompletionelements
as $element) {
696 $mform->disabledIf($element, 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC
);
699 $gotcompletionoptions = $gotcompletionoptions ||
700 count($this->_customcompletionelements
)>0;
702 // Automatic option only appears if possible
703 if ($gotcompletionoptions) {
704 $mform->getElement('completion')->addOption(
705 get_string('completion_automatic', 'completion'),
706 COMPLETION_TRACKING_AUTOMATIC
);
709 // Completion expected at particular date? (For progress tracking)
710 $mform->addElement('date_selector', 'completionexpected', get_string('completionexpected', 'completion'), array('optional'=>true));
711 $mform->addHelpButton('completionexpected', 'completionexpected', 'completion');
712 $mform->disabledIf('completionexpected', 'completion', 'eq', COMPLETION_TRACKING_NONE
);
715 $this->standard_hidden_coursemodule_elements();
719 * Can be overridden to add custom completion rules if the module wishes
720 * them. If overriding this, you should also override completion_rule_enabled.
722 * Just add elements to the form as needed and return the list of IDs. The
723 * system will call disabledIf and handle other behaviour for each returned
725 * @return array Array of string IDs of added items, empty array if none
727 function add_completion_rules() {
732 * Called during validation. Override to indicate, based on the data, whether
733 * a custom completion rule is enabled (selected).
735 * @param array $data Input data (not yet validated)
736 * @return bool True if one or more rules is enabled, false if none are;
737 * default returns false
739 function completion_rule_enabled($data) {
743 function standard_hidden_coursemodule_elements(){
744 $mform =& $this->_form
;
745 $mform->addElement('hidden', 'course', 0);
746 $mform->setType('course', PARAM_INT
);
748 $mform->addElement('hidden', 'coursemodule', 0);
749 $mform->setType('coursemodule', PARAM_INT
);
751 $mform->addElement('hidden', 'section', 0);
752 $mform->setType('section', PARAM_INT
);
754 $mform->addElement('hidden', 'module', 0);
755 $mform->setType('module', PARAM_INT
);
757 $mform->addElement('hidden', 'modulename', '');
758 $mform->setType('modulename', PARAM_PLUGIN
);
760 $mform->addElement('hidden', 'instance', 0);
761 $mform->setType('instance', PARAM_INT
);
763 $mform->addElement('hidden', 'add', 0);
764 $mform->setType('add', PARAM_ALPHA
);
766 $mform->addElement('hidden', 'update', 0);
767 $mform->setType('update', PARAM_INT
);
769 $mform->addElement('hidden', 'return', 0);
770 $mform->setType('return', PARAM_BOOL
);
772 $mform->addElement('hidden', 'sr', 0);
773 $mform->setType('sr', PARAM_INT
);
776 public function standard_grading_coursemodule_elements() {
777 global $COURSE, $CFG;
778 $mform =& $this->_form
;
780 if ($this->_features
->hasgrades
) {
782 if (!$this->_features
->rating ||
$this->_features
->gradecat
) {
783 $mform->addElement('header', 'modstandardgrade', get_string('grade'));
786 //if supports grades and grades arent being handled via ratings
787 if (!$this->_features
->rating
) {
788 $mform->addElement('modgrade', 'grade', get_string('grade'));
789 $mform->setDefault('grade', 100);
792 if ($this->_features
->advancedgrading
793 and !empty($this->current
->_advancedgradingdata
['methods'])
794 and !empty($this->current
->_advancedgradingdata
['areas'])) {
796 if (count($this->current
->_advancedgradingdata
['areas']) == 1) {
797 // if there is just one gradable area (most cases), display just the selector
798 // without its name to make UI simplier
799 $areadata = reset($this->current
->_advancedgradingdata
['areas']);
800 $areaname = key($this->current
->_advancedgradingdata
['areas']);
801 $mform->addElement('select', 'advancedgradingmethod_'.$areaname,
802 get_string('gradingmethod', 'core_grading'), $this->current
->_advancedgradingdata
['methods']);
803 $mform->addHelpButton('advancedgradingmethod_'.$areaname, 'gradingmethod', 'core_grading');
806 // the module defines multiple gradable areas, display a selector
807 // for each of them together with a name of the area
808 $areasgroup = array();
809 foreach ($this->current
->_advancedgradingdata
['areas'] as $areaname => $areadata) {
810 $areasgroup[] = $mform->createElement('select', 'advancedgradingmethod_'.$areaname,
811 $areadata['title'], $this->current
->_advancedgradingdata
['methods']);
812 $areasgroup[] = $mform->createElement('static', 'advancedgradingareaname_'.$areaname, '', $areadata['title']);
814 $mform->addGroup($areasgroup, 'advancedgradingmethodsgroup', get_string('gradingmethods', 'core_grading'),
815 array(' ', '<br />'), false);
819 if ($this->_features
->gradecat
) {
820 $mform->addElement('select', 'gradecat',
821 get_string('gradecategoryonmodform', 'grades'),
822 grade_get_categories_menu($COURSE->id
, $this->_outcomesused
));
823 $mform->addHelpButton('gradecat', 'gradecategoryonmodform', 'grades');
828 function add_intro_editor($required=false, $customlabel=null) {
829 if (!$this->_features
->introeditor
) {
830 // intro editor not supported in this module
834 $mform = $this->_form
;
835 $label = is_null($customlabel) ?
get_string('moduleintro') : $customlabel;
837 $mform->addElement('editor', 'introeditor', $label, array('rows' => 10), array('maxfiles' => EDITOR_UNLIMITED_FILES
,
838 'noclean' => true, 'context' => $this->context
, 'subdirs' => true));
839 $mform->setType('introeditor', PARAM_RAW
); // no XSS prevention here, users must be trusted
841 $mform->addRule('introeditor', get_string('required'), 'required', null, 'client');
844 // If the 'show description' feature is enabled, this checkbox appears below the intro.
845 // We want to hide that when using the singleactivity course format because it is confusing.
846 if ($this->_features
->showdescription
&& $this->courseformat
->has_view_page()) {
847 $mform->addElement('checkbox', 'showdescription', get_string('showdescription'));
848 $mform->addHelpButton('showdescription', 'showdescription');
853 * Overriding formslib's add_action_buttons() method, to add an extra submit "save changes and return" button.
855 * @param bool $cancel show cancel button
856 * @param string $submitlabel null means default, false means none, string is label text
857 * @param string $submit2label null means default, false means none, string is label text
860 function add_action_buttons($cancel=true, $submitlabel=null, $submit2label=null) {
861 if (is_null($submitlabel)) {
862 $submitlabel = get_string('savechangesanddisplay');
865 if (is_null($submit2label)) {
866 $submit2label = get_string('savechangesandreturntocourse');
869 $mform = $this->_form
;
871 // elements in a row need a group
872 $buttonarray = array();
874 // Label for the submit button to return to the course.
875 // Ignore this button in single activity format because it is confusing.
876 if ($submit2label !== false && $this->courseformat
->has_view_page()) {
877 $buttonarray[] = &$mform->createElement('submit', 'submitbutton2', $submit2label);
880 if ($submitlabel !== false) {
881 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
885 $buttonarray[] = &$mform->createElement('cancel');
888 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
889 $mform->setType('buttonar', PARAM_RAW
);
890 $mform->closeHeaderBefore('buttonar');
894 * Get the list of admin settings for this module and apply any locked settings.
895 * This cannot happen in apply_admin_defaults because we do not the current values of the settings
896 * in that function because set_data has not been called yet.
900 protected function apply_admin_locked_flags() {
903 if (!$this->applyadminlockedflags
) {
907 $settings = get_config($this->_modname
);
908 $mform = $this->_form
;
909 $lockedicon = html_writer
::tag('span',
910 $OUTPUT->pix_icon('t/locked', get_string('locked', 'admin')),
911 array('class' => 'action-icon'));
912 $isupdate = !empty($this->_cm
);
914 foreach ($settings as $name => $value) {
915 if (strpos('_', $name) !== false) {
918 if ($mform->elementExists($name)) {
919 $element = $mform->getElement($name);
920 $lockedsetting = $name . '_locked';
921 if (!empty($settings->$lockedsetting)) {
922 // Always lock locked settings for new modules,
923 // for updates, only lock them if the current value is the same as the default (or there is no current value).
924 $value = $settings->$name;
925 if ($isupdate && isset($this->current
->$name)) {
926 $value = $this->current
->$name;
928 if ($value == $settings->$name) {
929 $mform->setConstant($name, $settings->$name);
930 $element->setLabel($element->getLabel() . $lockedicon);
931 // Do not use hardfreeze because we need the hidden input to check dependencies.
940 * Get the list of admin settings for this module and apply any defaults/advanced/locked settings.
942 * @param $datetimeoffsets array - If passed, this is an array of fieldnames => times that the
943 * default date/time value should be relative to. If not passed, all
944 * date/time fields are set relative to the users current midnight.
947 public function apply_admin_defaults($datetimeoffsets = array()) {
948 // This flag triggers the settings to be locked in apply_admin_locked_flags().
949 $this->applyadminlockedflags
= true;
951 $settings = get_config($this->_modname
);
952 $mform = $this->_form
;
953 $usermidnight = usergetmidnight(time());
954 $isupdate = !empty($this->_cm
);
956 foreach ($settings as $name => $value) {
957 if (strpos('_', $name) !== false) {
960 if ($mform->elementExists($name)) {
961 $element = $mform->getElement($name);
963 if ($element->getType() == 'date_time_selector') {
964 $enabledsetting = $name . '_enabled';
965 if (empty($settings->$enabledsetting)) {
966 $mform->setDefault($name, 0);
968 $relativetime = $usermidnight;
969 if (isset($datetimeoffsets[$name])) {
970 $relativetime = $datetimeoffsets[$name];
972 $mform->setDefault($name, $relativetime +
$settings->$name);
975 $mform->setDefault($name, $settings->$name);
978 $advancedsetting = $name . '_adv';
979 if (!empty($settings->$advancedsetting)) {
980 $mform->setAdvanced($name);