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