PageRenderTime 54ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/course/moodleform_mod.php

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