PageRenderTime 65ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/course/moodleform_mod.php

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