e24b7f85 |
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 |
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. |
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 | function moodleform_mod($instance, $section, $cm) { |
33 | $this->_instance = $instance; |
34 | $this->_section = $section; |
35 | $this->_cm = $cm; |
36 | parent::moodleform('modedit.php'); |
37 | } |
38 | /** |
a7f7e52f |
39 | * Only available on moodleform_mod. |
e24b7f85 |
40 | * |
41 | * @param array $default_values passed by reference |
42 | */ |
a7f7e52f |
43 | function data_preprocessing(&$default_values){ |
e24b7f85 |
44 | } |
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). |
49 | * |
50 | * @param mixed $default_values object or array of default values |
51 | */ |
32db0d42 |
52 | function set_data($default_values) { |
e24b7f85 |
53 | if (is_object($default_values)) { |
54 | $default_values = (array)$default_values; |
55 | } |
ab6803a9 |
56 | $this->data_preprocessing($default_values); |
32db0d42 |
57 | parent::set_data($default_values + $this->standard_coursemodule_elements_settings());//never slashed for moodleform_mod |
e24b7f85 |
58 | } |
59 | /** |
60 | * Adds all the standard elements to a form to edit the settings for an activity module. |
61 | * |
62 | * @param bool $supportsgroups does this module support groups? |
63 | */ |
64 | function standard_coursemodule_elements($supportsgroups=true){ |
65 | $mform =& $this->_form; |
24e25bc1 |
66 | $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form')); |
e24b7f85 |
67 | if ($supportsgroups){ |
08103c93 |
68 | // TODO: we must define this as mod property! |
e24b7f85 |
69 | $mform->addElement('modgroupmode', 'groupmode', get_string('groupmode')); |
70 | } |
71 | $mform->addElement('modvisible', 'visible', get_string('visible')); |
2d11be3b |
72 | $mform->addElement('text', 'cmidnumber', get_string('idnumber')); |
e24b7f85 |
73 | $this->standard_hidden_coursemodule_elements(); |
74 | } |
75 | |
76 | function standard_hidden_coursemodule_elements(){ |
77 | $mform =& $this->_form; |
78 | $mform->addElement('hidden', 'course', 0); |
79 | $mform->setType('course', PARAM_INT); |
80 | |
81 | $mform->addElement('hidden', 'coursemodule', 0); |
82 | $mform->setType('coursemodule', PARAM_INT); |
83 | |
84 | $mform->addElement('hidden', 'section', 0); |
85 | $mform->setType('section', PARAM_INT); |
86 | |
87 | $mform->addElement('hidden', 'module', 0); |
88 | $mform->setType('module', PARAM_INT); |
89 | |
90 | $mform->addElement('hidden', 'modulename', ''); |
91 | $mform->setType('modulename', PARAM_SAFEDIR); |
92 | |
93 | $mform->addElement('hidden', 'instance', 0); |
94 | $mform->setType('instance', PARAM_INT); |
95 | |
96 | $mform->addElement('hidden', 'add', 0); |
97 | $mform->setType('add', PARAM_ALPHA); |
98 | |
99 | $mform->addElement('hidden', 'update', 0); |
100 | $mform->setType('update', PARAM_INT); |
19110c57 |
101 | |
102 | $mform->addElement('hidden', 'return', 0); |
103 | $mform->setType('return', PARAM_BOOL); |
e24b7f85 |
104 | } |
105 | |
106 | /** |
107 | * This function is called by course/modedit.php to setup defaults for standard form |
108 | * elements. |
109 | * |
110 | * @param object $course |
111 | * @param object $cm |
112 | * @param integer $section |
113 | * @return unknown |
114 | */ |
115 | function standard_coursemodule_elements_settings(){ |
2d11be3b |
116 | return ($this->modgroupmode_settings() + $this->modvisible_settings()); |
e24b7f85 |
117 | } |
118 | /** |
119 | * This is called from modedit.php to load the default for the groupmode element. |
120 | * |
121 | * @param object $course |
122 | * @param object $cm |
123 | */ |
124 | function modgroupmode_settings(){ |
125 | global $COURSE; |
126 | return array('groupmode'=>groupmode($COURSE, $this->_cm)); |
127 | } |
128 | /** |
129 | * This is called from modedit.php to set the default for modvisible form element. |
130 | * |
131 | * @param object $course |
132 | * @param object $cm |
133 | * @param integer $section section is a db id when updating a activity config |
134 | * or the section no when adding a new activity |
135 | */ |
136 | function modvisible_settings(){ |
137 | global $COURSE; |
138 | $cm=$this->_cm; |
139 | $section=$this->_section; |
140 | if ($cm) { |
141 | $visible = $cm->visible; |
142 | } else { |
143 | $visible = 1; |
144 | } |
145 | |
146 | $hiddensection = !get_field('course_sections', 'visible', 'section', $section, 'course', $COURSE->id); |
147 | if ($hiddensection) { |
148 | $visible = 0; |
149 | } |
150 | return array('visible'=>$visible); |
151 | } |
152 | |
153 | } |
154 | |
155 | ?> |