MDL-10092:
[moodle-linuxchix.git] / course / moodleform_mod.php
blob5ec1951186d012a122b9795f446210655359f8d4
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 deprecared mod/{modname}/mod.html
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.
14 * @var mixed
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).
21 * @var mixed
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.
28 * @var mixed
30 var $_cm;
32 function moodleform_mod($instance, $section, $cm) {
33 $this->_instance = $instance;
34 $this->_section = $section;
35 $this->_cm = $cm;
36 parent::moodleform('modedit.php');
39 /**
40 * Only available on moodleform_mod.
42 * @param array $default_values passed by reference
44 function data_preprocessing(&$default_values){
47 function definition_after_data() {
48 global $COURSE;
49 $mform =& $this->_form;
51 if ($id = $mform->getElementValue('update')) {
52 $modulename = $mform->getElementValue('modulename');
53 $instance = $mform->getElementValue('instance');
55 if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename,
56 'iteminstance'=>$instance, 'courseid'=>$COURSE->id))) {
57 foreach ($items as $item) {
58 if (!empty($item->outcomeid)) {
59 $elname = 'outcome_'.$item->outcomeid;
60 if ($mform->elementExists($elname)) {
61 $mform->hardFreeze($elname); // prevent removing of existing outcomes
68 if ($mform->elementExists('groupmode')) {
69 if ($COURSE->groupmodeforce) {
70 $mform->hardFreeze('groupmode'); // groupmode can not be changed if forced from course settings
74 // groupings have no use without groupmode or groupmembersonly
75 if (!$mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly')) {
76 if ($mform->elementExists('groupingid')) {
77 $mform->removeElement('groupingid');
82 // form verification
83 function validation($data) {
84 global $COURSE;
86 $mform =& $this->_form;
88 $errors = array();
90 if ($mform->elementExists('name')) {
91 $name = trim($data['name']);
92 if ($name == '') {
93 $errors['name'] = get_string('required');
97 $grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$data['modulename'],
98 'iteminstance'=>$data['instance'], 'itemnumber'=>0, 'courseid'=>$COURSE->id));
99 if ($data['coursemodule']) {
100 $cm = get_record('course_modules', 'id', $data['coursemodule']);
101 } else {
102 $cm = null;
105 if ($mform->elementExists('cmidnumber')) {
106 // verify the idnumber
107 if (!grade_verify_idnumber($data['cmidnumber'], $grade_item, $cm)) {
108 $errors['cmidnumber'] = get_string('idnumbertaken');
112 if (count($errors) == 0) {
113 return true;
114 } else {
115 return $errors;
120 * Load in existing data as form defaults. Usually new entry defaults are stored directly in
121 * form definition (new entry form); this function is used to load in data where values
122 * already exist and data is being edited (edit entry form).
124 * @param mixed $default_values object or array of default values
126 function set_data($default_values) {
127 if (is_object($default_values)) {
128 $default_values = (array)$default_values;
130 $this->data_preprocessing($default_values);
131 parent::set_data($default_values); //never slashed for moodleform_mod
135 * Adds all the standard elements to a form to edit the settings for an activity module.
137 * @param mixed array or object describing supported features - groups, groupings, groupmembersonly
139 function standard_coursemodule_elements($features=null){
140 global $COURSE, $CFG;
141 $mform =& $this->_form;
143 // deal with legacy $supportgroups param
144 if ($features === true or $features === false) {
145 $groupmode = $features;
146 $features = new object();
147 $features->groups = $groupmode;
149 } else if (is_array($features)) {
150 $features = (object)$features;
152 } else if (empty($features)) {
153 $features = new object();
156 if (!isset($features->groups)) {
157 $features->groups = true;
160 if (!isset($features->groupings)) {
161 $features->groupings = false;
164 if (!isset($features->groupmembersonly)) {
165 $features->groupmembersonly = false;
168 if (!empty($CFG->enableoutcomes)) {
169 if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
170 $mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades'));
171 foreach($outcomes as $outcome) {
172 $mform->addElement('advcheckbox', 'outcome_'.$outcome->id, $outcome->get_name());
177 $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
178 if ($features->groups){
179 $mform->addElement('modgroupmode', 'groupmode', get_string('groupmode'));
182 if (!empty($CFG->enablegroupings)) {
183 if ($features->groupings or $features->groupmembersonly) {
184 //groupings selector - used for normal grouping mode or also when restricting access with groupmembersonly
185 $options = array();
186 $options[0] = get_string('none');
187 if ($groupings = get_records('groupings', 'courseid', $COURSE->id)) {
188 foreach ($groupings as $grouping) {
189 $options[$grouping->id] = format_string($grouping->name);
192 $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options);
193 $mform->setAdvanced('groupingid');
196 if ($features->groupmembersonly) {
197 $mform->addElement('advcheckbox', 'groupmembersonly', get_string('groupmembersonly', 'group'));
198 $mform->setAdvanced('groupmembersonly');
202 $mform->addElement('modvisible', 'visible', get_string('visible'));
203 $mform->addElement('text', 'cmidnumber', get_string('idnumber'));
205 $this->standard_hidden_coursemodule_elements();
208 function standard_hidden_coursemodule_elements(){
209 $mform =& $this->_form;
210 $mform->addElement('hidden', 'course', 0);
211 $mform->setType('course', PARAM_INT);
213 $mform->addElement('hidden', 'coursemodule', 0);
214 $mform->setType('coursemodule', PARAM_INT);
216 $mform->addElement('hidden', 'section', 0);
217 $mform->setType('section', PARAM_INT);
219 $mform->addElement('hidden', 'module', 0);
220 $mform->setType('module', PARAM_INT);
222 $mform->addElement('hidden', 'modulename', '');
223 $mform->setType('modulename', PARAM_SAFEDIR);
225 $mform->addElement('hidden', 'instance', 0);
226 $mform->setType('instance', PARAM_INT);
228 $mform->addElement('hidden', 'add', 0);
229 $mform->setType('add', PARAM_ALPHA);
231 $mform->addElement('hidden', 'update', 0);
232 $mform->setType('update', PARAM_INT);
234 $mform->addElement('hidden', 'return', 0);
235 $mform->setType('return', PARAM_BOOL);