rating MDL-23908 altered the way the role hints for ratings work
[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 /**
30 * Coursemodle record of the module that is being updated. Will be null if this is an 'add' form and not an
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
79 $this->_features = new object();
80 $this->_features->groups = plugin_supports('mod', $this->_modname, FEATURE_GROUPS, true);
81 $this->_features->groupings = plugin_supports('mod', $this->_modname, FEATURE_GROUPINGS, false);
82 $this->_features->groupmembersonly = plugin_supports('mod', $this->_modname, FEATURE_GROUPMEMBERSONLY, false);
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 }
202 } else {
203 // Has the element been unlocked?
61fceb86 204 if ($mform->exportValue('unlockcompletion')) {
4e781c7b 205 // Yes, add in warning text and set the hidden variable
206 $mform->insertElementBefore(
61fceb86 207 $mform->createElement('static', 'completedunlocked',
208 get_string('completedunlocked', 'completion'),
209 get_string('completedunlockedtext', 'completion')),
4e781c7b 210 'unlockcompletion');
211 $mform->removeElement('unlockcompletion');
212 $mform->getElement('completionunlocked')->setValue(1);
213 } else {
214 // No, add in the warning text with the count (now we know
215 // it) before the unlock button
216 $mform->insertElementBefore(
61fceb86 217 $mform->createElement('static', 'completedwarning',
218 get_string('completedwarning', 'completion'),
219 get_string('completedwarningtext', 'completion', $completedcount)),
4e781c7b 220 'unlockcompletion');
61fceb86 221 $freeze = true;
4e781c7b 222 }
aa6c1ced 223 }
4e781c7b 224
61fceb86 225 if ($freeze) {
4e781c7b 226 $mform->freeze('completion');
61fceb86 227 if ($mform->elementExists('completionview')) {
4e781c7b 228 $mform->freeze('completionview'); // don't use hardFreeze or checkbox value gets lost
229 }
61fceb86 230 if ($mform->elementExists('completionusegrade')) {
4e781c7b 231 $mform->freeze('completionusegrade');
232 }
233 $mform->freeze($this->_customcompletionelements);
aa6c1ced 234 }
4e781c7b 235 }
82bd6a5e 236
237 // Availability conditions
238 if (!empty($CFG->enableavailability) && $this->_cm) {
239 $ci = new condition_info($this->_cm);
240 $fullcm=$ci->get_full_course_module();
241
242 $num=0;
243 foreach($fullcm->conditionsgrade as $gradeitemid=>$minmax) {
244 $groupelements=$mform->getElement('conditiongradegroup['.$num.']')->getElements();
245 $groupelements[0]->setValue($gradeitemid);
246 // These numbers are always in the format 0.00000 - the rtrims remove any final zeros and,
247 // if it is a whole number, the decimal place.
248 $groupelements[2]->setValue(is_null($minmax->min)?'':rtrim(rtrim($minmax->min,'0'),'.'));
249 $groupelements[4]->setValue(is_null($minmax->max)?'':rtrim(rtrim($minmax->max,'0'),'.'));
250 $num++;
251 }
252
253 if ($completion->is_enabled()) {
254 $num=0;
255 foreach($fullcm->conditionscompletion as $othercmid=>$state) {
256 $groupelements=$mform->getElement('conditioncompletiongroup['.$num.']')->getElements();
257 $groupelements[0]->setValue($othercmid);
258 $groupelements[1]->setValue($state);
259 $num++;
260 }
261 }
262 }
71ee4471 263 }
264
60243313 265 // form verification
a78890d5 266 function validation($data, $files) {
32648682 267 global $COURSE, $DB;
a78890d5 268 $errors = parent::validation($data, $files);
60243313 269
273eb2f5 270 $mform =& $this->_form;
271
60243313 272 $errors = array();
273
273eb2f5 274 if ($mform->elementExists('name')) {
275 $name = trim($data['name']);
276 if ($name == '') {
277 $errors['name'] = get_string('required');
278 }
e04ff2d5 279 }
280
60243313 281 $grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$data['modulename'],
282 'iteminstance'=>$data['instance'], 'itemnumber'=>0, 'courseid'=>$COURSE->id));
283 if ($data['coursemodule']) {
32648682 284 $cm = $DB->get_record('course_modules', array('id'=>$data['coursemodule']));
60243313 285 } else {
286 $cm = null;
287 }
288
273eb2f5 289 if ($mform->elementExists('cmidnumber')) {
290 // verify the idnumber
204175c5 291 if (!grade_verify_idnumber($data['cmidnumber'], $COURSE->id, $grade_item, $cm)) {
273eb2f5 292 $errors['cmidnumber'] = get_string('idnumbertaken');
293 }
60243313 294 }
aa6c1ced 295
4e781c7b 296 // Completion: Don't let them choose automatic completion without turning
297 // on some conditions
61fceb86 298 if (array_key_exists('completion', $data) && $data['completion']==COMPLETION_TRACKING_AUTOMATIC) {
299 if (empty($data['completionview']) && empty($data['completionusegrade']) &&
4e781c7b 300 !$this->completion_rule_enabled($data)) {
61fceb86 301 $errors['completion'] = get_string('badautocompletion', 'completion');
4e781c7b 302 }
303 }
60243313 304
a561d96e 305 // Conditions: Don't let them set dates which make no sense
aa6c1ced
PS
306 if (array_key_exists('availablefrom', $data) &&
307 $data['availablefrom'] && $data['availableuntil'] &&
29cb6c4c 308 $data['availablefrom'] > $data['availableuntil']) {
a561d96e 309 $errors['availablefrom'] = get_string('badavailabledates', 'condition');
310 }
aa6c1ced 311
a78890d5 312 return $errors;
60243313 313 }
314
e24b7f85 315 /**
316 * Load in existing data as form defaults. Usually new entry defaults are stored directly in
317 * form definition (new entry form); this function is used to load in data where values
318 * already exist and data is being edited (edit entry form).
319 *
320 * @param mixed $default_values object or array of default values
321 */
32db0d42 322 function set_data($default_values) {
e24b7f85 323 if (is_object($default_values)) {
324 $default_values = (array)$default_values;
325 }
82bd6a5e 326
aa6c1ced 327 $this->data_preprocessing($default_values);
82bd6a5e 328 parent::set_data($default_values);
e24b7f85 329 }
71ee4471 330
e24b7f85 331 /**
332 * Adds all the standard elements to a form to edit the settings for an activity module.
e24b7f85 333 */
42f103be 334 function standard_coursemodule_elements(){
c18269c7 335 global $COURSE, $CFG, $DB;
e24b7f85 336 $mform =& $this->_form;
f3b783f4 337
1d50dd13 338 $this->_outcomesused = false;
42f103be 339 if ($this->_features->outcomes) {
f3b783f4 340 if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
1d50dd13 341 $this->_outcomesused = true;
f3b783f4 342 $mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades'));
343 foreach($outcomes as $outcome) {
344 $mform->addElement('advcheckbox', 'outcome_'.$outcome->id, $outcome->get_name());
345 }
346 }
347 }
348
16306628
AD
349
350 if ($this->_features->rating) {
63e87951
AD
351 require_once($CFG->dirroot.'/rating/lib.php');
352 $rm = new rating_manager();
353
354 $mform->addElement('header', 'modstandardratings', get_string('ratings', 'rating'));
a09aeee4 355
0d54a398
AD
356 $permission=null;
357 $context = get_context_instance(CONTEXT_MODULE, $this->_cm->id);
358
359 $roles1 = get_roles_with_capability('moodle/rating:rate', $permission, $context);
360 $roles2 = get_roles_with_capability('mod/'.$this->_cm->modname.':rate', $permission, $context);
361
362 $rolesthatcanrate = array();
363 foreach($roles1 as $k1=>$v1) {
364 if (array_key_exists($k1, $roles2)) {
365 $rolesthatcanrate[] = $v1->name;
366 }
367 }
368
bbf27733
AD
369 $mform->addElement('static', 'rolewarning', '', get_string('canrate','rating', implode(', ', $rolesthatcanrate)));
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);
530d2d72 374 $mform->addHelpButton('assessed', 'aggregatetype', 'forum');
a09aeee4 375
63e87951 376 $mform->addElement('modgrade', 'scale', get_string('scale'), false);
a09aeee4
AD
377 $mform->disabledIf('scale', 'assessed', 'eq', 0);
378
379 $mform->addElement('checkbox', 'ratingtime', get_string('ratingtime', 'forum'));
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
432 $mform->addElement('header', '', get_string('availabilityconditions', 'condition'));
433 $mform->addElement('date_selector', 'availablefrom', get_string('availablefrom', 'condition'), array('optional'=>true));
82bd6a5e 434 $mform->addElement('date_selector', 'availableuntil', get_string('availableuntil', 'condition'), array('optional'=>true));
82bd6a5e 435
436 // Conditions based on grades
42f103be 437 $gradeoptions = array();
438 $items = grade_item::fetch_all(array('courseid'=>$COURSE->id));
439 $items = $items ? $items : array();
82bd6a5e 440 foreach($items as $id=>$item) {
d030fa7b 441 // Do not include grades for current item
442 if (!empty($this->_cm) && $this->_cm->instance == $item->iteminstance
443 && $this->_cm->modname == $item->itemmodule
444 && $item->itemtype == 'mod') {
445 continue;
446 }
42f103be 447 $gradeoptions[$id] = $item->get_name();
82bd6a5e 448 }
449 asort($gradeoptions);
42f103be 450 $gradeoptions = array(0=>get_string('none','condition'))+$gradeoptions;
82bd6a5e 451
42f103be 452 $grouparray = array();
82bd6a5e 453 $grouparray[] =& $mform->createElement('select','conditiongradeitemid','',$gradeoptions);
9271e424 454 $grouparray[] =& $mform->createElement('static', '', '',' '.get_string('grade_atleast','condition').' ');
82bd6a5e 455 $grouparray[] =& $mform->createElement('text', 'conditiongrademin','',array('size'=>3));
75564228 456 $grouparray[] =& $mform->createElement('static', '', '','% '.get_string('grade_upto','condition').' ');
82bd6a5e 457 $grouparray[] =& $mform->createElement('text', 'conditiongrademax','',array('size'=>3));
75564228 458 $grouparray[] =& $mform->createElement('static', '', '','%');
aa6c1ced
PS
459 $mform->setType('conditiongrademin',PARAM_FLOAT);
460 $mform->setType('conditiongrademax',PARAM_FLOAT);
461 $group = $mform->createElement('group','conditiongradegroup',
82bd6a5e 462 get_string('gradecondition', 'condition'),$grouparray);
463
464 // Get version with condition info and store it so we don't ask
465 // twice
aa6c1ced 466 if(!empty($this->_cm)) {
42f103be 467 $ci = new condition_info($this->_cm, CONDITION_MISSING_EXTRATABLE);
468 $this->_cm = $ci->get_full_course_module();
469 $count = count($this->_cm->conditionsgrade)+1;
82bd6a5e 470 } else {
42f103be 471 $count = 1;
82bd6a5e 472 }
473
42f103be 474 $this->repeat_elements(array($group), $count, array(), 'conditiongraderepeats', 'conditiongradeadds', 2,
475 get_string('addgrades', 'condition'), true);
59f19480 476 $mform->addHelpButton('conditiongradegroup[0]', 'gradecondition', 'condition');
82bd6a5e 477
478 // Conditions based on completion
479 $completion = new completion_info($COURSE);
480 if ($completion->is_enabled()) {
42f103be 481 $completionoptions = array();
482 $modinfo = get_fast_modinfo($COURSE);
82bd6a5e 483 foreach($modinfo->cms as $id=>$cm) {
d030fa7b 484 // Add each course-module if it:
485 // (a) has completion turned on
486 // (b) is not the same as current course-module
487 if ($cm->completion && (empty($this->_cm) || $this->_cm->id != $id)) {
fe44022d 488 $completionoptions[$id]=$cm->name;
489 }
82bd6a5e 490 }
491 asort($completionoptions);
42f103be 492 $completionoptions = array(0=>get_string('none','condition'))+$completionoptions;
82bd6a5e 493
494 $completionvalues=array(
495 COMPLETION_COMPLETE=>get_string('completion_complete','condition'),
496 COMPLETION_INCOMPLETE=>get_string('completion_incomplete','condition'),
497 COMPLETION_COMPLETE_PASS=>get_string('completion_pass','condition'),
498 COMPLETION_COMPLETE_FAIL=>get_string('completion_fail','condition'));
499
aa6c1ced 500 $grouparray = array();
82bd6a5e 501 $grouparray[] =& $mform->createElement('select','conditionsourcecmid','',$completionoptions);
502 $grouparray[] =& $mform->createElement('select','conditionrequiredcompletion','',$completionvalues);
aa6c1ced 503 $group = $mform->createElement('group','conditioncompletiongroup',
82bd6a5e 504 get_string('completioncondition', 'condition'),$grouparray);
505
42f103be 506 $count = empty($this->_cm) ? 1 : count($this->_cm->conditionscompletion)+1;
82bd6a5e 507 $this->repeat_elements(array($group),$count,array(),
508 'conditioncompletionrepeats','conditioncompletionadds',2,
509 get_string('addcompletions','condition'),true);
ddd9f926 510 $mform->addHelpButton('conditioncompletiongroup[0]', 'completioncondition', 'condition');
82bd6a5e 511 }
512
aa6c1ced
PS
513 // Do we display availability info to students?
514 $mform->addElement('select', 'showavailability', get_string('showavailability', 'condition'),
515 array(CONDITION_STUDENTVIEW_SHOW=>get_string('showavailability_show', 'condition'),
82bd6a5e 516 CONDITION_STUDENTVIEW_HIDE=>get_string('showavailability_hide', 'condition')));
aa6c1ced 517 $mform->setDefault('showavailability', CONDITION_STUDENTVIEW_SHOW);
82bd6a5e 518 }
519
aa6c1ced 520 // Conditional activities: completion tracking section
82bd6a5e 521 if(!isset($completion)) {
522 $completion = new completion_info($COURSE);
523 }
61fceb86 524 if ($completion->is_enabled()) {
4e781c7b 525 $mform->addElement('header', '', get_string('activitycompletion', 'completion'));
526
527 // Unlock button for if people have completed it (will
528 // be removed in definition_after_data if they haven't)
61fceb86 529 $mform->addElement('submit', 'unlockcompletion', get_string('unlockcompletion', 'completion'));
4e781c7b 530 $mform->registerNoSubmitButton('unlockcompletion');
61fceb86 531 $mform->addElement('hidden', 'completionunlocked', 0);
d18e0fe6 532 $mform->setType('completionunlocked', PARAM_INT);
82bd6a5e 533
aa6c1ced
PS
534 $mform->addElement('select', 'completion', get_string('completion', 'completion'),
535 array(COMPLETION_TRACKING_NONE=>get_string('completion_none', 'completion'),
61fceb86 536 COMPLETION_TRACKING_MANUAL=>get_string('completion_manual', 'completion')));
61fceb86 537 $mform->setDefault('completion', $this->_features->defaultcompletion
4e781c7b 538 ? COMPLETION_TRACKING_MANUAL
539 : COMPLETION_TRACKING_NONE);
c8dcb793 540 $mform->addHelpButton('completion', 'completion', 'completion');
4e781c7b 541
542 // Automatic completion once you view it
61fceb86 543 $gotcompletionoptions = false;
9593d8b8 544 if (plugin_supports('mod', $this->_modname, FEATURE_COMPLETION_TRACKS_VIEWS, false)) {
61fceb86 545 $mform->addElement('checkbox', 'completionview', get_string('completionview', 'completion'),
ddd9f926 546 get_string('completionview_desc', 'completion'));
61fceb86 547 $mform->disabledIf('completionview', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
548 $gotcompletionoptions = true;
4e781c7b 549 }
550
551 // Automatic completion once it's graded
9593d8b8 552 if (plugin_supports('mod', $this->_modname, FEATURE_GRADE_HAS_GRADE, false)) {
61fceb86 553 $mform->addElement('checkbox', 'completionusegrade', get_string('completionusegrade', 'completion'),
ddd9f926 554 get_string('completionusegrade_desc', 'completion'));
61fceb86 555 $mform->disabledIf('completionusegrade', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
af455aa0 556 $mform->addHelpButton('completionusegrade', 'completionusegrade', 'completion');
61fceb86 557 $gotcompletionoptions = true;
4e781c7b 558 }
559
560 // Automatic completion according to module-specific rules
561 $this->_customcompletionelements = $this->add_completion_rules();
61fceb86 562 foreach ($this->_customcompletionelements as $element) {
aa6c1ced 563 $mform->disabledIf($element, 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
4e781c7b 564 }
565
566 $gotcompletionoptions = $gotcompletionoptions ||
567 count($this->_customcompletionelements)>0;
568
569 // Automatic option only appears if possible
61fceb86 570 if ($gotcompletionoptions) {
4e781c7b 571 $mform->getElement('completion')->addOption(
61fceb86 572 get_string('completion_automatic', 'completion'),
4e781c7b 573 COMPLETION_TRACKING_AUTOMATIC);
aa6c1ced 574 }
4e781c7b 575
576 // Completion expected at particular date? (For progress tracking)
61fceb86 577 $mform->addElement('date_selector', 'completionexpected', get_string('completionexpected', 'completion'), array('optional'=>true));
ddd9f926 578 $mform->addHelpButton('completionexpected', 'completionexpected', 'completion');
aa6c1ced 579 $mform->disabledIf('completionexpected', 'completion', 'eq', COMPLETION_TRACKING_NONE);
4e781c7b 580 }
581
e24b7f85 582 $this->standard_hidden_coursemodule_elements();
583 }
aa6c1ced 584
4e781c7b 585 /**
586 * Can be overridden to add custom completion rules if the module wishes
587 * them. If overriding this, you should also override completion_rule_enabled.
588 * <p>
589 * Just add elements to the form as needed and return the list of IDs. The
590 * system will call disabledIf and handle other behaviour for each returned
591 * ID.
592 * @return array Array of string IDs of added items, empty array if none
593 */
594 function add_completion_rules() {
595 return array();
596 }
597
598 /**
599 * Called during validation. Override to indicate, based on the data, whether
600 * a custom completion rule is enabled (selected).
601 *
602 * @param array $data Input data (not yet validated)
603 * @return bool True if one or more rules is enabled, false if none are;
604 * default returns false
605 */
606 function completion_rule_enabled(&$data) {
607 return false;
608 }
e24b7f85 609
610 function standard_hidden_coursemodule_elements(){
611 $mform =& $this->_form;
612 $mform->addElement('hidden', 'course', 0);
613 $mform->setType('course', PARAM_INT);
614
615 $mform->addElement('hidden', 'coursemodule', 0);
616 $mform->setType('coursemodule', PARAM_INT);
617
618 $mform->addElement('hidden', 'section', 0);
619 $mform->setType('section', PARAM_INT);
620
621 $mform->addElement('hidden', 'module', 0);
622 $mform->setType('module', PARAM_INT);
623
624 $mform->addElement('hidden', 'modulename', '');
625 $mform->setType('modulename', PARAM_SAFEDIR);
626
627 $mform->addElement('hidden', 'instance', 0);
628 $mform->setType('instance', PARAM_INT);
629
630 $mform->addElement('hidden', 'add', 0);
631 $mform->setType('add', PARAM_ALPHA);
632
633 $mform->addElement('hidden', 'update', 0);
634 $mform->setType('update', PARAM_INT);
19110c57 635
636 $mform->addElement('hidden', 'return', 0);
637 $mform->setType('return', PARAM_BOOL);
e24b7f85 638 }
639
60d5a2e4 640 public function standard_grading_coursemodule_elements() {
16306628 641 global $COURSE, $CFG;
1d50dd13
AD
642 $mform =& $this->_form;
643
16306628 644 if ($this->_features->hasgrades) {
1d50dd13 645
16306628
AD
646 if (!$this->_features->rating || $this->_features->gradecat) {
647 $mform->addElement('header', 'modstandardgrade', get_string('grade'));
648 }
649
650 //if supports grades and grades arent being handled via ratings
651 if (!$this->_features->rating) {
652 $mform->addElement('modgrade', 'grade', get_string('grade'));
653 $mform->setDefault('grade', 100);
654 }
1d50dd13
AD
655
656 if ($this->_features->gradecat) {
657 $categories = grade_get_categories_menu($COURSE->id, $this->_outcomesused);
658 $mform->addElement('select', 'gradecat', get_string('gradecategory', 'grades'), $categories);
659 }
660 }
661 }
662
dc5c2bd9 663 function add_intro_editor($required=false, $customlabel=null) {
664 if (!$this->_features->introeditor) {
665 // intro editor not supported in this module
666 return;
667 }
668
669 $mform = $this->_form;
670 $label = is_null($customlabel) ? get_string('moduleintro') : $customlabel;
671
09f8d75e 672 $mform->addElement('editor', 'introeditor', $label, null, array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$this->context));
dc5c2bd9 673 $mform->setType('introeditor', PARAM_RAW); // no XSS prevention here, users must be trusted
674 if ($required) {
675 $mform->addRule('introeditor', get_string('required'), 'required', null, 'client');
676 }
677 }
678
6b467109 679 /**
680 * Overriding formslib's add_action_buttons() method, to add an extra submit "save changes and return" button.
b5ab55dd 681 *
682 * @param bool $cancel show cancel button
683 * @param string $submitlabel null means default, false means none, string is label text
684 * @param string $submit2label null means default, false means none, string is label text
6b467109 685 * @return void
b5ab55dd 686 */
6b467109 687 function add_action_buttons($cancel=true, $submitlabel=null, $submit2label=null) {
688 if (is_null($submitlabel)) {
689 $submitlabel = get_string('savechangesanddisplay');
690 }
b5ab55dd 691
6b467109 692 if (is_null($submit2label)) {
693 $submit2label = get_string('savechangesandreturntocourse');
694 }
b5ab55dd 695
dc5c2bd9 696 $mform = $this->_form;
b5ab55dd 697
698 // elements in a row need a group
699 $buttonarray = array();
700
701 if ($submit2label !== false) {
702 $buttonarray[] = &$mform->createElement('submit', 'submitbutton2', $submit2label);
703 }
704
705 if ($submitlabel !== false) {
706 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
707 }
708
6b467109 709 if ($cancel) {
710 $buttonarray[] = &$mform->createElement('cancel');
711 }
712
713 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
de6f158f 714 $mform->setType('buttonar', PARAM_RAW);
6b467109 715 $mform->closeHeaderBefore('buttonar');
716 }
e24b7f85 717}
718
aa6c1ced 719