modified dirname call to scorm_dirname that works also with URLs
[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 */
32db0d42 54 function set_data($default_values) {
e24b7f85 55 if (is_object($default_values)) {
56 $default_values = (array)$default_values;
57 }
58 $this->defaults_preprocessing($default_values);
32db0d42 59 parent::set_data($default_values + $this->standard_coursemodule_elements_settings());//never slashed for moodleform_mod
e24b7f85 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);
19110c57 102
103 $mform->addElement('hidden', 'return', 0);
104 $mform->setType('return', PARAM_BOOL);
e24b7f85 105 }
106
107 /**
108 * This function is called by course/modedit.php to setup defaults for standard form
109 * elements.
110 *
111 * @param object $course
112 * @param object $cm
113 * @param integer $section
114 * @return unknown
115 */
116 function standard_coursemodule_elements_settings(){
117 return ($this->modgroupmode_settings() + $this->modvisible_settings());
118 }
119 /**
120 * This is called from modedit.php to load the default for the groupmode element.
121 *
122 * @param object $course
123 * @param object $cm
124 */
125 function modgroupmode_settings(){
126 global $COURSE;
127 return array('groupmode'=>groupmode($COURSE, $this->_cm));
128 }
129 /**
130 * This is called from modedit.php to set the default for modvisible form element.
131 *
132 * @param object $course
133 * @param object $cm
134 * @param integer $section section is a db id when updating a activity config
135 * or the section no when adding a new activity
136 */
137 function modvisible_settings(){
138 global $COURSE;
139 $cm=$this->_cm;
140 $section=$this->_section;
141 if ($cm) {
142 $visible = $cm->visible;
143 } else {
144 $visible = 1;
145 }
146
147 $hiddensection = !get_field('course_sections', 'visible', 'section', $section, 'course', $COURSE->id);
148 if ($hiddensection) {
149 $visible = 0;
150 }
151 return array('visible'=>$visible);
152 }
153
154}
155
156?>