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