PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/moodle/course/moodleform_mod.php

https://bitbucket.org/geek745/moodle-db2
PHP | 347 lines | 228 code | 55 blank | 64 comment | 54 complexity | fb3c3c00eacda3fde88a1bf7c06a3fbc MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, LGPL-2.0
  1. <?php
  2. require_once ($CFG->libdir.'/formslib.php');
  3. /**
  4. * This class adds extra methods to form wrapper specific to be used for module
  5. * add / update forms (mod/{modname}.mod_form.php replaces deprecated mod/{modname}/mod.html
  6. *
  7. */
  8. class moodleform_mod extends moodleform {
  9. /**
  10. * Instance of the module that is being updated. This is the id of the {prefix}{modulename}
  11. * record. Can be used in form definition. Will be "" if this is an 'add' form and not an
  12. * update one.
  13. *
  14. * @var mixed
  15. */
  16. var $_instance;
  17. /**
  18. * Section of course that module instance will be put in or is in.
  19. * This is always the section number itself (column 'section' from 'course_sections' table).
  20. *
  21. * @var mixed
  22. */
  23. var $_section;
  24. /**
  25. * Coursemodle record of the module that is being updated. Will be null if this is an 'add' form and not an
  26. * update one.
  27. *
  28. * @var mixed
  29. */
  30. var $_cm;
  31. /**
  32. * List of modform features
  33. */
  34. var $_features;
  35. function moodleform_mod($instance, $section, $cm) {
  36. $this->_instance = $instance;
  37. $this->_section = $section;
  38. $this->_cm = $cm;
  39. parent::moodleform('modedit.php');
  40. }
  41. /**
  42. * Only available on moodleform_mod.
  43. *
  44. * @param array $default_values passed by reference
  45. */
  46. function data_preprocessing(&$default_values){
  47. }
  48. /**
  49. * Each module which defines definition_after_data() must call this method using parent::definition_after_data();
  50. */
  51. function definition_after_data() {
  52. global $CFG, $COURSE;
  53. $mform =& $this->_form;
  54. if ($id = $mform->getElementValue('update')) {
  55. $modulename = $mform->getElementValue('modulename');
  56. $instance = $mform->getElementValue('instance');
  57. if ($this->_features->gradecat) {
  58. $gradecat = false;
  59. if (!empty($CFG->enableoutcomes) and $this->_features->outcomes) {
  60. if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
  61. $gradecat = true;
  62. }
  63. }
  64. if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename,
  65. 'iteminstance'=>$instance, 'courseid'=>$COURSE->id))) {
  66. foreach ($items as $item) {
  67. if (!empty($item->outcomeid)) {
  68. $elname = 'outcome_'.$item->outcomeid;
  69. if ($mform->elementExists($elname)) {
  70. $mform->hardFreeze($elname); // prevent removing of existing outcomes
  71. }
  72. }
  73. }
  74. foreach ($items as $item) {
  75. if (is_bool($gradecat)) {
  76. $gradecat = $item->categoryid;
  77. continue;
  78. }
  79. if ($gradecat != $item->categoryid) {
  80. //mixed categories
  81. $gradecat = false;
  82. break;
  83. }
  84. }
  85. }
  86. if ($gradecat === false) {
  87. // items and outcomes in different categories - remove the option
  88. // TODO: it might be better to add a "Mixed categories" text instead
  89. if ($mform->elementExists('gradecat')) {
  90. $mform->removeElement('gradecat');
  91. }
  92. }
  93. }
  94. }
  95. if ($COURSE->groupmodeforce) {
  96. if ($mform->elementExists('groupmode')) {
  97. $mform->hardFreeze('groupmode'); // groupmode can not be changed if forced from course settings
  98. }
  99. }
  100. if ($mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly') and empty($COURSE->groupmodeforce)) {
  101. $mform->disabledIf('groupingid', 'groupmode', 'eq', NOGROUPS);
  102. } else if (!$mform->elementExists('groupmode') and $mform->elementExists('groupmembersonly')) {
  103. $mform->disabledIf('groupingid', 'groupmembersonly', 'notchecked');
  104. } else if (!$mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly')) {
  105. // groupings have no use without groupmode or groupmembersonly
  106. if ($mform->elementExists('groupingid')) {
  107. $mform->removeElement('groupingid');
  108. }
  109. }
  110. }
  111. // form verification
  112. function validation($data, $files) {
  113. global $COURSE;
  114. $errors = parent::validation($data, $files);
  115. $mform =& $this->_form;
  116. $errors = array();
  117. if ($mform->elementExists('name')) {
  118. $name = trim($data['name']);
  119. if ($name == '') {
  120. $errors['name'] = get_string('required');
  121. }
  122. }
  123. $grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$data['modulename'],
  124. 'iteminstance'=>$data['instance'], 'itemnumber'=>0, 'courseid'=>$COURSE->id));
  125. if ($data['coursemodule']) {
  126. $cm = get_record('course_modules', 'id', $data['coursemodule']);
  127. } else {
  128. $cm = null;
  129. }
  130. if ($mform->elementExists('cmidnumber')) {
  131. // verify the idnumber
  132. if (!grade_verify_idnumber($data['cmidnumber'], $COURSE->id, $grade_item, $cm)) {
  133. $errors['cmidnumber'] = get_string('idnumbertaken');
  134. }
  135. }
  136. return $errors;
  137. }
  138. /**
  139. * Load in existing data as form defaults. Usually new entry defaults are stored directly in
  140. * form definition (new entry form); this function is used to load in data where values
  141. * already exist and data is being edited (edit entry form).
  142. *
  143. * @param mixed $default_values object or array of default values
  144. */
  145. function set_data($default_values) {
  146. if (is_object($default_values)) {
  147. $default_values = (array)$default_values;
  148. }
  149. $this->data_preprocessing($default_values);
  150. parent::set_data($default_values); //never slashed for moodleform_mod
  151. }
  152. /**
  153. * Adds all the standard elements to a form to edit the settings for an activity module.
  154. *
  155. * @param mixed array or object describing supported features - groups, groupings, groupmembersonly, etc.
  156. */
  157. function standard_coursemodule_elements($features=null){
  158. global $COURSE, $CFG;
  159. $mform =& $this->_form;
  160. // deal with legacy $supportgroups param
  161. if ($features === true or $features === false) {
  162. $groupmode = $features;
  163. $this->_features = new object();
  164. $this->_features->groups = $groupmode;
  165. } else if (is_array($features)) {
  166. $this->_features = (object)$features;
  167. } else if (empty($features)) {
  168. $this->_features = new object();
  169. } else {
  170. $this->_features = $features;
  171. }
  172. if (!isset($this->_features->groups)) {
  173. $this->_features->groups = true;
  174. }
  175. if (!isset($this->_features->groupings)) {
  176. $this->_features->groupings = false;
  177. }
  178. if (!isset($this->_features->groupmembersonly)) {
  179. $this->_features->groupmembersonly = false;
  180. }
  181. if (!isset($this->_features->outcomes)) {
  182. $this->_features->outcomes = true;
  183. }
  184. if (!isset($this->_features->gradecat)) {
  185. $this->_features->gradecat = true;
  186. }
  187. if (!isset($this->_features->idnumber)) {
  188. $this->_features->idnumber = true;
  189. }
  190. $outcomesused = false;
  191. if (!empty($CFG->enableoutcomes) and $this->_features->outcomes) {
  192. if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
  193. $outcomesused = true;
  194. $mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades'));
  195. foreach($outcomes as $outcome) {
  196. $mform->addElement('advcheckbox', 'outcome_'.$outcome->id, $outcome->get_name());
  197. }
  198. }
  199. }
  200. $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
  201. if ($this->_features->groups) {
  202. $options = array(NOGROUPS => get_string('groupsnone'),
  203. SEPARATEGROUPS => get_string('groupsseparate'),
  204. VISIBLEGROUPS => get_string('groupsvisible'));
  205. $mform->addElement('select', 'groupmode', get_string('groupmode'), $options, NOGROUPS);
  206. $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')));
  207. }
  208. if (!empty($CFG->enablegroupings)) {
  209. if ($this->_features->groupings or $this->_features->groupmembersonly) {
  210. //groupings selector - used for normal grouping mode or also when restricting access with groupmembersonly
  211. $options = array();
  212. $options[0] = get_string('none');
  213. if ($groupings = get_records('groupings', 'courseid', $COURSE->id)) {
  214. foreach ($groupings as $grouping) {
  215. $options[$grouping->id] = format_string($grouping->name);
  216. }
  217. }
  218. $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options);
  219. $mform->setHelpButton('groupingid', array('grouping', get_string('grouping', 'group')));
  220. $mform->setAdvanced('groupingid');
  221. }
  222. if ($this->_features->groupmembersonly) {
  223. $mform->addElement('checkbox', 'groupmembersonly', get_string('groupmembersonly', 'group'));
  224. $mform->setHelpButton('groupmembersonly', array('groupmembersonly', get_string('groupmembersonly', 'group')));
  225. $mform->setAdvanced('groupmembersonly');
  226. }
  227. }
  228. $mform->addElement('modvisible', 'visible', get_string('visible'));
  229. if ($this->_features->idnumber) {
  230. $mform->addElement('text', 'cmidnumber', get_string('idnumbermod'));
  231. $mform->setHelpButton('cmidnumber', array('cmidnumber', get_string('idnumbermod')), true);
  232. }
  233. if ($this->_features->gradecat) {
  234. $categories = grade_get_categories_menu($COURSE->id, $outcomesused);
  235. $mform->addElement('select', 'gradecat', get_string('gradecategory', 'grades'), $categories);
  236. }
  237. $this->standard_hidden_coursemodule_elements();
  238. }
  239. function standard_hidden_coursemodule_elements(){
  240. $mform =& $this->_form;
  241. $mform->addElement('hidden', 'course', 0);
  242. $mform->setType('course', PARAM_INT);
  243. $mform->addElement('hidden', 'coursemodule', 0);
  244. $mform->setType('coursemodule', PARAM_INT);
  245. $mform->addElement('hidden', 'section', 0);
  246. $mform->setType('section', PARAM_INT);
  247. $mform->addElement('hidden', 'module', 0);
  248. $mform->setType('module', PARAM_INT);
  249. $mform->addElement('hidden', 'modulename', '');
  250. $mform->setType('modulename', PARAM_SAFEDIR);
  251. $mform->addElement('hidden', 'instance', 0);
  252. $mform->setType('instance', PARAM_INT);
  253. $mform->addElement('hidden', 'add', 0);
  254. $mform->setType('add', PARAM_ALPHA);
  255. $mform->addElement('hidden', 'update', 0);
  256. $mform->setType('update', PARAM_INT);
  257. $mform->addElement('hidden', 'return', 0);
  258. $mform->setType('return', PARAM_BOOL);
  259. }
  260. /**
  261. * Overriding formslib's add_action_buttons() method, to add an extra submit "save changes and return" button.
  262. *
  263. * @param bool $cancel show cancel button
  264. * @param string $submitlabel null means default, false means none, string is label text
  265. * @param string $submit2label null means default, false means none, string is label text
  266. * @return void
  267. */
  268. function add_action_buttons($cancel=true, $submitlabel=null, $submit2label=null) {
  269. if (is_null($submitlabel)) {
  270. $submitlabel = get_string('savechangesanddisplay');
  271. }
  272. if (is_null($submit2label)) {
  273. $submit2label = get_string('savechangesandreturntocourse');
  274. }
  275. $mform =& $this->_form;
  276. // elements in a row need a group
  277. $buttonarray = array();
  278. if ($submit2label !== false) {
  279. $buttonarray[] = &$mform->createElement('submit', 'submitbutton2', $submit2label);
  280. }
  281. if ($submitlabel !== false) {
  282. $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
  283. }
  284. if ($cancel) {
  285. $buttonarray[] = &$mform->createElement('cancel');
  286. }
  287. $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
  288. $mform->setType('buttonar', PARAM_RAW);
  289. $mform->closeHeaderBefore('buttonar');
  290. }
  291. }
  292. ?>