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