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