weekly release 2.3dev (on-sync version 22_STABLE)
[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);
b11f9da6 90 $this->_features->advancedgrading = plugin_supports('mod', $this->_modname, FEATURE_ADVANCED_GRADING, false);
42f103be 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'] &&
6282381d 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 355 require_once($CFG->dirroot.'/rating/lib.php');
b47dcdf0 356 $rm = new rating_manager();
63e87951
AD
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
6282381d 433
434 // Available from/to defaults to midnight because then the display
435 // will be nicer where it tells users when they can access it (it
436 // shows only the date and not time).
437 $date = usergetdate(time());
438 $midnight = make_timestamp($date['year'], $date['mon'], $date['mday']);
439
440 // From/until controls
441 $mform->addElement('header', 'availabilityconditionsheader',
442 get_string('availabilityconditions', 'condition'));
443 $mform->addElement('date_time_selector', 'availablefrom',
444 get_string('availablefrom', 'condition'),
445 array('optional' => true, 'defaulttime' => $midnight));
f1c5b522 446 $mform->addHelpButton('availablefrom', 'availablefrom', 'condition');
6282381d 447 $mform->addElement('date_time_selector', 'availableuntil',
448 get_string('availableuntil', 'condition'),
449 array('optional' => true, 'defaulttime' => $midnight));
82bd6a5e 450
451 // Conditions based on grades
42f103be 452 $gradeoptions = array();
453 $items = grade_item::fetch_all(array('courseid'=>$COURSE->id));
454 $items = $items ? $items : array();
82bd6a5e 455 foreach($items as $id=>$item) {
d030fa7b 456 // Do not include grades for current item
457 if (!empty($this->_cm) && $this->_cm->instance == $item->iteminstance
458 && $this->_cm->modname == $item->itemmodule
459 && $item->itemtype == 'mod') {
460 continue;
461 }
42f103be 462 $gradeoptions[$id] = $item->get_name();
82bd6a5e 463 }
464 asort($gradeoptions);
42f103be 465 $gradeoptions = array(0=>get_string('none','condition'))+$gradeoptions;
82bd6a5e 466
42f103be 467 $grouparray = array();
82bd6a5e 468 $grouparray[] =& $mform->createElement('select','conditiongradeitemid','',$gradeoptions);
9271e424 469 $grouparray[] =& $mform->createElement('static', '', '',' '.get_string('grade_atleast','condition').' ');
82bd6a5e 470 $grouparray[] =& $mform->createElement('text', 'conditiongrademin','',array('size'=>3));
75564228 471 $grouparray[] =& $mform->createElement('static', '', '','% '.get_string('grade_upto','condition').' ');
82bd6a5e 472 $grouparray[] =& $mform->createElement('text', 'conditiongrademax','',array('size'=>3));
75564228 473 $grouparray[] =& $mform->createElement('static', '', '','%');
aa6c1ced
PS
474 $mform->setType('conditiongrademin',PARAM_FLOAT);
475 $mform->setType('conditiongrademax',PARAM_FLOAT);
476 $group = $mform->createElement('group','conditiongradegroup',
82bd6a5e 477 get_string('gradecondition', 'condition'),$grouparray);
478
479 // Get version with condition info and store it so we don't ask
480 // twice
aa6c1ced 481 if(!empty($this->_cm)) {
42f103be 482 $ci = new condition_info($this->_cm, CONDITION_MISSING_EXTRATABLE);
483 $this->_cm = $ci->get_full_course_module();
484 $count = count($this->_cm->conditionsgrade)+1;
82bd6a5e 485 } else {
42f103be 486 $count = 1;
82bd6a5e 487 }
488
42f103be 489 $this->repeat_elements(array($group), $count, array(), 'conditiongraderepeats', 'conditiongradeadds', 2,
490 get_string('addgrades', 'condition'), true);
59f19480 491 $mform->addHelpButton('conditiongradegroup[0]', 'gradecondition', 'condition');
82bd6a5e 492
493 // Conditions based on completion
494 $completion = new completion_info($COURSE);
495 if ($completion->is_enabled()) {
42f103be 496 $completionoptions = array();
497 $modinfo = get_fast_modinfo($COURSE);
82bd6a5e 498 foreach($modinfo->cms as $id=>$cm) {
d030fa7b 499 // Add each course-module if it:
500 // (a) has completion turned on
501 // (b) is not the same as current course-module
502 if ($cm->completion && (empty($this->_cm) || $this->_cm->id != $id)) {
fe44022d 503 $completionoptions[$id]=$cm->name;
504 }
82bd6a5e 505 }
506 asort($completionoptions);
42f103be 507 $completionoptions = array(0=>get_string('none','condition'))+$completionoptions;
82bd6a5e 508
509 $completionvalues=array(
510 COMPLETION_COMPLETE=>get_string('completion_complete','condition'),
511 COMPLETION_INCOMPLETE=>get_string('completion_incomplete','condition'),
512 COMPLETION_COMPLETE_PASS=>get_string('completion_pass','condition'),
513 COMPLETION_COMPLETE_FAIL=>get_string('completion_fail','condition'));
514
aa6c1ced 515 $grouparray = array();
82bd6a5e 516 $grouparray[] =& $mform->createElement('select','conditionsourcecmid','',$completionoptions);
517 $grouparray[] =& $mform->createElement('select','conditionrequiredcompletion','',$completionvalues);
aa6c1ced 518 $group = $mform->createElement('group','conditioncompletiongroup',
82bd6a5e 519 get_string('completioncondition', 'condition'),$grouparray);
520
42f103be 521 $count = empty($this->_cm) ? 1 : count($this->_cm->conditionscompletion)+1;
82bd6a5e 522 $this->repeat_elements(array($group),$count,array(),
523 'conditioncompletionrepeats','conditioncompletionadds',2,
524 get_string('addcompletions','condition'),true);
ddd9f926 525 $mform->addHelpButton('conditioncompletiongroup[0]', 'completioncondition', 'condition');
82bd6a5e 526 }
527
aa6c1ced
PS
528 // Do we display availability info to students?
529 $mform->addElement('select', 'showavailability', get_string('showavailability', 'condition'),
530 array(CONDITION_STUDENTVIEW_SHOW=>get_string('showavailability_show', 'condition'),
82bd6a5e 531 CONDITION_STUDENTVIEW_HIDE=>get_string('showavailability_hide', 'condition')));
aa6c1ced 532 $mform->setDefault('showavailability', CONDITION_STUDENTVIEW_SHOW);
82bd6a5e 533 }
534
aa6c1ced 535 // Conditional activities: completion tracking section
82bd6a5e 536 if(!isset($completion)) {
537 $completion = new completion_info($COURSE);
538 }
61fceb86 539 if ($completion->is_enabled()) {
3fbe0e8a 540 $mform->addElement('header', 'activitycompletionheader', get_string('activitycompletion', 'completion'));
4e781c7b 541
542 // Unlock button for if people have completed it (will
543 // be removed in definition_after_data if they haven't)
61fceb86 544 $mform->addElement('submit', 'unlockcompletion', get_string('unlockcompletion', 'completion'));
4e781c7b 545 $mform->registerNoSubmitButton('unlockcompletion');
61fceb86 546 $mform->addElement('hidden', 'completionunlocked', 0);
d18e0fe6 547 $mform->setType('completionunlocked', PARAM_INT);
82bd6a5e 548
aa6c1ced
PS
549 $mform->addElement('select', 'completion', get_string('completion', 'completion'),
550 array(COMPLETION_TRACKING_NONE=>get_string('completion_none', 'completion'),
61fceb86 551 COMPLETION_TRACKING_MANUAL=>get_string('completion_manual', 'completion')));
61fceb86 552 $mform->setDefault('completion', $this->_features->defaultcompletion
4e781c7b 553 ? COMPLETION_TRACKING_MANUAL
554 : COMPLETION_TRACKING_NONE);
c8dcb793 555 $mform->addHelpButton('completion', 'completion', 'completion');
4e781c7b 556
557 // Automatic completion once you view it
61fceb86 558 $gotcompletionoptions = false;
9593d8b8 559 if (plugin_supports('mod', $this->_modname, FEATURE_COMPLETION_TRACKS_VIEWS, false)) {
61fceb86 560 $mform->addElement('checkbox', 'completionview', get_string('completionview', 'completion'),
ddd9f926 561 get_string('completionview_desc', 'completion'));
61fceb86 562 $mform->disabledIf('completionview', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
563 $gotcompletionoptions = true;
4e781c7b 564 }
565
566 // Automatic completion once it's graded
9593d8b8 567 if (plugin_supports('mod', $this->_modname, FEATURE_GRADE_HAS_GRADE, false)) {
61fceb86 568 $mform->addElement('checkbox', 'completionusegrade', get_string('completionusegrade', 'completion'),
ddd9f926 569 get_string('completionusegrade_desc', 'completion'));
61fceb86 570 $mform->disabledIf('completionusegrade', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
af455aa0 571 $mform->addHelpButton('completionusegrade', 'completionusegrade', 'completion');
61fceb86 572 $gotcompletionoptions = true;
4e781c7b 573 }
574
575 // Automatic completion according to module-specific rules
576 $this->_customcompletionelements = $this->add_completion_rules();
61fceb86 577 foreach ($this->_customcompletionelements as $element) {
aa6c1ced 578 $mform->disabledIf($element, 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
4e781c7b 579 }
580
581 $gotcompletionoptions = $gotcompletionoptions ||
582 count($this->_customcompletionelements)>0;
583
584 // Automatic option only appears if possible
61fceb86 585 if ($gotcompletionoptions) {
4e781c7b 586 $mform->getElement('completion')->addOption(
61fceb86 587 get_string('completion_automatic', 'completion'),
4e781c7b 588 COMPLETION_TRACKING_AUTOMATIC);
aa6c1ced 589 }
4e781c7b 590
591 // Completion expected at particular date? (For progress tracking)
61fceb86 592 $mform->addElement('date_selector', 'completionexpected', get_string('completionexpected', 'completion'), array('optional'=>true));
ddd9f926 593 $mform->addHelpButton('completionexpected', 'completionexpected', 'completion');
aa6c1ced 594 $mform->disabledIf('completionexpected', 'completion', 'eq', COMPLETION_TRACKING_NONE);
4e781c7b 595 }
596
e24b7f85 597 $this->standard_hidden_coursemodule_elements();
598 }
aa6c1ced 599
4e781c7b 600 /**
601 * Can be overridden to add custom completion rules if the module wishes
602 * them. If overriding this, you should also override completion_rule_enabled.
603 * <p>
604 * Just add elements to the form as needed and return the list of IDs. The
605 * system will call disabledIf and handle other behaviour for each returned
606 * ID.
607 * @return array Array of string IDs of added items, empty array if none
608 */
609 function add_completion_rules() {
610 return array();
611 }
612
613 /**
614 * Called during validation. Override to indicate, based on the data, whether
615 * a custom completion rule is enabled (selected).
616 *
617 * @param array $data Input data (not yet validated)
618 * @return bool True if one or more rules is enabled, false if none are;
619 * default returns false
620 */
621 function completion_rule_enabled(&$data) {
622 return false;
623 }
e24b7f85 624
625 function standard_hidden_coursemodule_elements(){
626 $mform =& $this->_form;
627 $mform->addElement('hidden', 'course', 0);
628 $mform->setType('course', PARAM_INT);
629
630 $mform->addElement('hidden', 'coursemodule', 0);
631 $mform->setType('coursemodule', PARAM_INT);
632
633 $mform->addElement('hidden', 'section', 0);
634 $mform->setType('section', PARAM_INT);
635
636 $mform->addElement('hidden', 'module', 0);
637 $mform->setType('module', PARAM_INT);
638
639 $mform->addElement('hidden', 'modulename', '');
aff24313 640 $mform->setType('modulename', PARAM_PLUGIN);
e24b7f85 641
642 $mform->addElement('hidden', 'instance', 0);
643 $mform->setType('instance', PARAM_INT);
644
645 $mform->addElement('hidden', 'add', 0);
646 $mform->setType('add', PARAM_ALPHA);
647
648 $mform->addElement('hidden', 'update', 0);
649 $mform->setType('update', PARAM_INT);
19110c57 650
651 $mform->addElement('hidden', 'return', 0);
652 $mform->setType('return', PARAM_BOOL);
e24b7f85 653 }
654
60d5a2e4 655 public function standard_grading_coursemodule_elements() {
16306628 656 global $COURSE, $CFG;
1d50dd13
AD
657 $mform =& $this->_form;
658
16306628 659 if ($this->_features->hasgrades) {
1d50dd13 660
16306628
AD
661 if (!$this->_features->rating || $this->_features->gradecat) {
662 $mform->addElement('header', 'modstandardgrade', get_string('grade'));
663 }
664
665 //if supports grades and grades arent being handled via ratings
666 if (!$this->_features->rating) {
667 $mform->addElement('modgrade', 'grade', get_string('grade'));
668 $mform->setDefault('grade', 100);
669 }
1d50dd13 670
b11f9da6
DM
671 if ($this->_features->advancedgrading
672 and !empty($this->current->_advancedgradingdata['methods'])
673 and !empty($this->current->_advancedgradingdata['areas'])) {
674
675 if (count($this->current->_advancedgradingdata['areas']) == 1) {
676 // if there is just one gradable area (most cases), display just the selector
677 // without its name to make UI simplier
678 $areadata = reset($this->current->_advancedgradingdata['areas']);
679 $areaname = key($this->current->_advancedgradingdata['areas']);
680 $mform->addElement('select', 'advancedgradingmethod_'.$areaname,
681 get_string('gradingmethod', 'core_grading'), $this->current->_advancedgradingdata['methods']);
6832a102 682 $mform->addHelpButton('advancedgradingmethod_'.$areaname, 'gradingmethod', 'core_grading');
b11f9da6
DM
683
684 } else {
685 // the module defines multiple gradable areas, display a selector
686 // for each of them together with a name of the area
687 $areasgroup = array();
688 foreach ($this->current->_advancedgradingdata['areas'] as $areaname => $areadata) {
689 $areasgroup[] = $mform->createElement('select', 'advancedgradingmethod_'.$areaname,
690 $areadata['title'], $this->current->_advancedgradingdata['methods']);
691 $areasgroup[] = $mform->createElement('static', 'advancedgradingareaname_'.$areaname, '', $areadata['title']);
692 }
693 $mform->addGroup($areasgroup, 'advancedgradingmethodsgroup', get_string('gradingmethods', 'core_grading'),
694 array(' ', '<br />'), false);
695 }
696 }
697
1d50dd13 698 if ($this->_features->gradecat) {
71c4154a
TH
699 $mform->addElement('select', 'gradecat',
700 get_string('gradecategoryonmodform', 'grades'),
701 grade_get_categories_menu($COURSE->id, $this->_outcomesused));
702 $mform->addHelpButton('gradecat', 'gradecategoryonmodform', 'grades');
1d50dd13
AD
703 }
704 }
705 }
706
dc5c2bd9 707 function add_intro_editor($required=false, $customlabel=null) {
708 if (!$this->_features->introeditor) {
709 // intro editor not supported in this module
710 return;
711 }
712
713 $mform = $this->_form;
714 $label = is_null($customlabel) ? get_string('moduleintro') : $customlabel;
715
09f8d75e 716 $mform->addElement('editor', 'introeditor', $label, null, array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$this->context));
dc5c2bd9 717 $mform->setType('introeditor', PARAM_RAW); // no XSS prevention here, users must be trusted
718 if ($required) {
719 $mform->addRule('introeditor', get_string('required'), 'required', null, 'client');
720 }
8c40662e 721
722 // If the 'show description' feature is enabled, this checkbox appears
723 // below the intro.
724 if ($this->_features->showdescription) {
725 $mform->addElement('checkbox', 'showdescription', get_string('showdescription'));
726 $mform->addHelpButton('showdescription', 'showdescription');
727 }
dc5c2bd9 728 }
729
6b467109 730 /**
731 * Overriding formslib's add_action_buttons() method, to add an extra submit "save changes and return" button.
b5ab55dd 732 *
733 * @param bool $cancel show cancel button
734 * @param string $submitlabel null means default, false means none, string is label text
735 * @param string $submit2label null means default, false means none, string is label text
6b467109 736 * @return void
b5ab55dd 737 */
6b467109 738 function add_action_buttons($cancel=true, $submitlabel=null, $submit2label=null) {
739 if (is_null($submitlabel)) {
740 $submitlabel = get_string('savechangesanddisplay');
741 }
b5ab55dd 742
6b467109 743 if (is_null($submit2label)) {
744 $submit2label = get_string('savechangesandreturntocourse');
745 }
b5ab55dd 746
dc5c2bd9 747 $mform = $this->_form;
b5ab55dd 748
749 // elements in a row need a group
750 $buttonarray = array();
751
752 if ($submit2label !== false) {
753 $buttonarray[] = &$mform->createElement('submit', 'submitbutton2', $submit2label);
754 }
755
756 if ($submitlabel !== false) {
757 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
758 }
759
6b467109 760 if ($cancel) {
761 $buttonarray[] = &$mform->createElement('cancel');
762 }
763
764 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
de6f158f 765 $mform->setType('buttonar', PARAM_RAW);
6b467109 766 $mform->closeHeaderBefore('buttonar');
767 }
e24b7f85 768}
769
aa6c1ced 770