some restructuring of formslib - moving moodleform_mod definition to new file course...
[moodle.git] / course / moodleform_mod.php
CommitLineData
e24b7f85 1<?php
2require_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 */
8class 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 /**
39 * Only available on moodleform_mod since defaults for forms cannot
40 * be calculated externally. You can use _customdata in here if necessary.
41 *
42 *
43 * @param array $default_values passed by reference
44 */
45 function defaults_preprocessing(&$default_values){
46 }
47 /**
48 * Load in existing data as form defaults. Usually new entry defaults are stored directly in
49 * form definition (new entry form); this function is used to load in data where values
50 * already exist and data is being edited (edit entry form).
51 *
52 * @param mixed $default_values object or array of default values
53 */
54 function set_defaults($default_values) {
55 if (is_object($default_values)) {
56 $default_values = (array)$default_values;
57 }
58 $this->defaults_preprocessing($default_values);
59 parent::set_defaults($default_values + $this->standard_coursemodule_elements_settings());//never slashed for moodleform_mod
60 }
61 /**
62 * Adds all the standard elements to a form to edit the settings for an activity module.
63 *
64 * @param bool $supportsgroups does this module support groups?
65 */
66 function standard_coursemodule_elements($supportsgroups=true){
67 $mform =& $this->_form;
68 $mform->addElement('header', '', get_string('modstandardels', 'form'));
69 if ($supportsgroups){
70 $mform->addElement('modgroupmode', 'groupmode', get_string('groupmode'));
71 }
72 $mform->addElement('modvisible', 'visible', get_string('visible'));
73
74 $this->standard_hidden_coursemodule_elements();
75 }
76
77 function standard_hidden_coursemodule_elements(){
78 $mform =& $this->_form;
79 $mform->addElement('hidden', 'course', 0);
80 $mform->setType('course', PARAM_INT);
81
82 $mform->addElement('hidden', 'coursemodule', 0);
83 $mform->setType('coursemodule', PARAM_INT);
84
85 $mform->addElement('hidden', 'section', 0);
86 $mform->setType('section', PARAM_INT);
87
88 $mform->addElement('hidden', 'module', 0);
89 $mform->setType('module', PARAM_INT);
90
91 $mform->addElement('hidden', 'modulename', '');
92 $mform->setType('modulename', PARAM_SAFEDIR);
93
94 $mform->addElement('hidden', 'instance', 0);
95 $mform->setType('instance', PARAM_INT);
96
97 $mform->addElement('hidden', 'add', 0);
98 $mform->setType('add', PARAM_ALPHA);
99
100 $mform->addElement('hidden', 'update', 0);
101 $mform->setType('update', PARAM_INT);
102 }
103
104 /**
105 * This function is called by course/modedit.php to setup defaults for standard form
106 * elements.
107 *
108 * @param object $course
109 * @param object $cm
110 * @param integer $section
111 * @return unknown
112 */
113 function standard_coursemodule_elements_settings(){
114 return ($this->modgroupmode_settings() + $this->modvisible_settings());
115 }
116 /**
117 * This is called from modedit.php to load the default for the groupmode element.
118 *
119 * @param object $course
120 * @param object $cm
121 */
122 function modgroupmode_settings(){
123 global $COURSE;
124 return array('groupmode'=>groupmode($COURSE, $this->_cm));
125 }
126 /**
127 * This is called from modedit.php to set the default for modvisible form element.
128 *
129 * @param object $course
130 * @param object $cm
131 * @param integer $section section is a db id when updating a activity config
132 * or the section no when adding a new activity
133 */
134 function modvisible_settings(){
135 global $COURSE;
136 $cm=$this->_cm;
137 $section=$this->_section;
138 if ($cm) {
139 $visible = $cm->visible;
140 } else {
141 $visible = 1;
142 }
143
144 $hiddensection = !get_field('course_sections', 'visible', 'section', $section, 'course', $COURSE->id);
145 if ($hiddensection) {
146 $visible = 0;
147 }
148 return array('visible'=>$visible);
149 }
150
151}
152
153?>