2 require_once ($CFG->libdir
.'/formslib.php');
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
8 class moodleform_mod
extends moodleform
{
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
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).
25 * Coursemodle record of the module that is being updated. Will be null if this is an 'add' form and not an
32 function moodleform_mod($instance, $section, $cm) {
33 $this->_instance
= $instance;
34 $this->_section
= $section;
36 parent
::moodleform('modedit.php');
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() {
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 if ($mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly') and empty($COURSE->groupmodeforce
)) {
75 $mform->disabledIf('groupingid', 'groupmode', 'eq', NOGROUPS
);
77 } else if (!$mform->elementExists('groupmode') and $mform->elementExists('groupmembersonly')) {
78 $mform->disabledIf('groupingid', 'groupmembersonly', 'notchecked');
80 } else if (!$mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly')) {
81 // groupings have no use without groupmode or groupmembersonly
82 if ($mform->elementExists('groupingid')) {
83 $mform->removeElement('groupingid');
89 function validation($data) {
92 $mform =& $this->_form
;
96 if ($mform->elementExists('name')) {
97 $name = trim($data['name']);
99 $errors['name'] = get_string('required');
103 $grade_item = grade_item
::fetch(array('itemtype'=>'mod', 'itemmodule'=>$data['modulename'],
104 'iteminstance'=>$data['instance'], 'itemnumber'=>0, 'courseid'=>$COURSE->id
));
105 if ($data['coursemodule']) {
106 $cm = get_record('course_modules', 'id', $data['coursemodule']);
111 if ($mform->elementExists('cmidnumber')) {
112 // verify the idnumber
113 if (!grade_verify_idnumber($data['cmidnumber'], $grade_item, $cm)) {
114 $errors['cmidnumber'] = get_string('idnumbertaken');
118 if (count($errors) == 0) {
126 * Load in existing data as form defaults. Usually new entry defaults are stored directly in
127 * form definition (new entry form); this function is used to load in data where values
128 * already exist and data is being edited (edit entry form).
130 * @param mixed $default_values object or array of default values
132 function set_data($default_values) {
133 if (is_object($default_values)) {
134 $default_values = (array)$default_values;
136 $this->data_preprocessing($default_values);
137 parent
::set_data($default_values); //never slashed for moodleform_mod
141 * Adds all the standard elements to a form to edit the settings for an activity module.
143 * @param mixed array or object describing supported features - groups, groupings, groupmembersonly
145 function standard_coursemodule_elements($features=null){
146 global $COURSE, $CFG;
147 $mform =& $this->_form
;
149 // deal with legacy $supportgroups param
150 if ($features === true or $features === false) {
151 $groupmode = $features;
152 $features = new object();
153 $features->groups
= $groupmode;
155 } else if (is_array($features)) {
156 $features = (object)$features;
158 } else if (empty($features)) {
159 $features = new object();
162 if (!isset($features->groups
)) {
163 $features->groups
= true;
166 if (!isset($features->groupings
)) {
167 $features->groupings
= false;
170 if (!isset($features->groupmembersonly
)) {
171 $features->groupmembersonly
= false;
174 if (!empty($CFG->enableoutcomes
)) {
175 if ($outcomes = grade_outcome
::fetch_all_available($COURSE->id
)) {
176 $mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades'));
177 foreach($outcomes as $outcome) {
178 $mform->addElement('advcheckbox', 'outcome_'.$outcome->id
, $outcome->get_name());
183 $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
184 if ($features->groups
){
185 $mform->addElement('modgroupmode', 'groupmode', get_string('groupmode'));
188 if (!empty($CFG->enablegroupings
)) {
189 if ($features->groupings
or $features->groupmembersonly
) {
190 //groupings selector - used for normal grouping mode or also when restricting access with groupmembersonly
192 $options[0] = get_string('none');
193 if ($groupings = get_records('groupings', 'courseid', $COURSE->id
)) {
194 foreach ($groupings as $grouping) {
195 $options[$grouping->id
] = format_string($grouping->name
);
198 $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options);
199 $mform->setAdvanced('groupingid');
202 if ($features->groupmembersonly
) {
203 $mform->addElement('checkbox', 'groupmembersonly', get_string('groupmembersonly', 'group'));
204 $mform->setAdvanced('groupmembersonly');
208 $mform->addElement('modvisible', 'visible', get_string('visible'));
209 $mform->addElement('text', 'cmidnumber', get_string('idnumber'));
211 $this->standard_hidden_coursemodule_elements();
214 function standard_hidden_coursemodule_elements(){
215 $mform =& $this->_form
;
216 $mform->addElement('hidden', 'course', 0);
217 $mform->setType('course', PARAM_INT
);
219 $mform->addElement('hidden', 'coursemodule', 0);
220 $mform->setType('coursemodule', PARAM_INT
);
222 $mform->addElement('hidden', 'section', 0);
223 $mform->setType('section', PARAM_INT
);
225 $mform->addElement('hidden', 'module', 0);
226 $mform->setType('module', PARAM_INT
);
228 $mform->addElement('hidden', 'modulename', '');
229 $mform->setType('modulename', PARAM_SAFEDIR
);
231 $mform->addElement('hidden', 'instance', 0);
232 $mform->setType('instance', PARAM_INT
);
234 $mform->addElement('hidden', 'add', 0);
235 $mform->setType('add', PARAM_ALPHA
);
237 $mform->addElement('hidden', 'update', 0);
238 $mform->setType('update', PARAM_INT
);
240 $mform->addElement('hidden', 'return', 0);
241 $mform->setType('return', PARAM_BOOL
);