Fixes bug MDL-8234, "New groups code & AS keyword"
[moodle-pu.git] / course / moodleform_mod.php
blobdcf6f144150bdc7e1e0ae6b18a622d811e756eb7
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.
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');
38 /**
39 * Only available on moodleform_mod.
41 * @param array $default_values passed by reference
43 function data_preprocessing(&$default_values){
45 /**
46 * Load in existing data as form defaults. Usually new entry defaults are stored directly in
47 * form definition (new entry form); this function is used to load in data where values
48 * already exist and data is being edited (edit entry form).
50 * @param mixed $default_values object or array of default values
52 function set_data($default_values) {
53 if (is_object($default_values)) {
54 $default_values = (array)$default_values;
56 $this->data_preprocessing($default_values);
57 parent::set_data($default_values + $this->standard_coursemodule_elements_settings());//never slashed for moodleform_mod
59 /**
60 * Adds all the standard elements to a form to edit the settings for an activity module.
62 * @param bool $supportsgroups does this module support groups?
64 function standard_coursemodule_elements($supportsgroups=true){
65 $mform =& $this->_form;
66 $mform->addElement('header', '', get_string('modstandardels', 'form'));
67 if ($supportsgroups){
68 $mform->addElement('modgroupmode', 'groupmode', get_string('groupmode'));
70 $mform->addElement('modvisible', 'visible', get_string('visible'));
72 $this->standard_hidden_coursemodule_elements();
75 function standard_hidden_coursemodule_elements(){
76 $mform =& $this->_form;
77 $mform->addElement('hidden', 'course', 0);
78 $mform->setType('course', PARAM_INT);
80 $mform->addElement('hidden', 'coursemodule', 0);
81 $mform->setType('coursemodule', PARAM_INT);
83 $mform->addElement('hidden', 'section', 0);
84 $mform->setType('section', PARAM_INT);
86 $mform->addElement('hidden', 'module', 0);
87 $mform->setType('module', PARAM_INT);
89 $mform->addElement('hidden', 'modulename', '');
90 $mform->setType('modulename', PARAM_SAFEDIR);
92 $mform->addElement('hidden', 'instance', 0);
93 $mform->setType('instance', PARAM_INT);
95 $mform->addElement('hidden', 'add', 0);
96 $mform->setType('add', PARAM_ALPHA);
98 $mform->addElement('hidden', 'update', 0);
99 $mform->setType('update', PARAM_INT);
101 $mform->addElement('hidden', 'return', 0);
102 $mform->setType('return', PARAM_BOOL);
106 * This function is called by course/modedit.php to setup defaults for standard form
107 * elements.
109 * @param object $course
110 * @param object $cm
111 * @param integer $section
112 * @return unknown
114 function standard_coursemodule_elements_settings(){
115 return ($this->modgroupmode_settings() + $this->modvisible_settings());
118 * This is called from modedit.php to load the default for the groupmode element.
120 * @param object $course
121 * @param object $cm
123 function modgroupmode_settings(){
124 global $COURSE;
125 return array('groupmode'=>groupmode($COURSE, $this->_cm));
128 * This is called from modedit.php to set the default for modvisible form element.
130 * @param object $course
131 * @param object $cm
132 * @param integer $section section is a db id when updating a activity config
133 * or the section no when adding a new activity
135 function modvisible_settings(){
136 global $COURSE;
137 $cm=$this->_cm;
138 $section=$this->_section;
139 if ($cm) {
140 $visible = $cm->visible;
141 } else {
142 $visible = 1;
145 $hiddensection = !get_field('course_sections', 'visible', 'section', $section, 'course', $COURSE->id);
146 if ($hiddensection) {
147 $visible = 0;
149 return array('visible'=>$visible);