3 if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
7 require_once($CFG->libdir
.'/formslib.php');
9 class editsection_form
extends moodleform
{
11 function definition() {
13 $mform = $this->_form
;
14 $course = $this->_customdata
['course'];
15 $mform->addElement('checkbox', 'usedefaultname', get_string('sectionusedefaultname'));
16 $mform->setDefault('usedefaultname', true);
18 $mform->addElement('text', 'name', get_string('sectionname'), array('size'=>'30'));
19 $mform->setType('name', PARAM_TEXT
);
20 $mform->disabledIf('name','usedefaultname','checked');
22 /// Prepare course and the editor
24 $mform->addElement('editor', 'summary_editor', get_string('summary'), null, $this->_customdata
['editoroptions']);
25 $mform->addHelpButton('summary_editor', 'summary');
26 $mform->setType('summary_editor', PARAM_RAW
);
28 $mform->addElement('hidden', 'id');
29 $mform->setType('id', PARAM_INT
);
31 $mform->_registerCancelButton('cancel');
34 public function definition_after_data() {
37 $mform = $this->_form
;
38 $course = $this->_customdata
['course'];
40 if (!empty($CFG->enableavailability
)) {
41 $mform->addElement('header', '', get_string('availabilityconditions', 'condition'));
42 // String used by conditions more than once
43 $strcondnone = get_string('none', 'condition');
44 // Grouping conditions - only if grouping is enabled at site level
45 if (!empty($CFG->enablegroupmembersonly
)) {
47 $options[0] = get_string('none');
48 if ($groupings = $DB->get_records('groupings', array('courseid' => $course->id
))) {
49 foreach ($groupings as $grouping) {
50 $context = context_course
::instance($course->id
);
51 $options[$grouping->id
] = format_string(
52 $grouping->name
, true, array('context' => $context));
55 $mform->addElement('select', 'groupingid', get_string('groupingsection', 'group'), $options);
56 $mform->addHelpButton('groupingid', 'groupingsection', 'group');
59 // Date and time conditions
60 $mform->addElement('date_time_selector', 'availablefrom',
61 get_string('availablefrom', 'condition'), array('optional' => true));
62 $mform->addElement('date_time_selector', 'availableuntil',
63 get_string('availableuntil', 'condition'), array('optional' => true));
65 // Conditions based on grades
66 $gradeoptions = array();
67 $items = grade_item
::fetch_all(array('courseid' => $course->id
));
68 $items = $items ?
$items : array();
69 foreach ($items as $id => $item) {
70 $gradeoptions[$id] = $item->get_name();
73 $gradeoptions = array(0 => $strcondnone) +
$gradeoptions;
75 $grouparray = array();
76 $grouparray[] = $mform->createElement('select', 'conditiongradeitemid', '', $gradeoptions);
77 $grouparray[] = $mform->createElement('static', '', '',
78 ' ' . get_string('grade_atleast', 'condition').' ');
79 $grouparray[] = $mform->createElement('text', 'conditiongrademin', '', array('size' => 3));
80 $grouparray[] = $mform->createElement('static', '', '',
81 '% ' . get_string('grade_upto', 'condition') . ' ');
82 $grouparray[] = $mform->createElement('text', 'conditiongrademax', '', array('size' => 3));
83 $grouparray[] = $mform->createElement('static', '', '', '%');
84 $group = $mform->createElement('group', 'conditiongradegroup',
85 get_string('gradecondition', 'condition'), $grouparray);
87 // Get full version (including condition info) of section object
88 $ci = new condition_info_section($this->_customdata
['cs']);
89 $fullcs = $ci->get_full_section();
90 $count = count($fullcs->conditionsgrade
) +
1;
93 $this->repeat_elements(array($group), $count, array(), 'conditiongraderepeats',
94 'conditiongradeadds', 2, get_string('addgrades', 'condition'), true);
95 $mform->addHelpButton('conditiongradegroup[0]', 'gradecondition', 'condition');
97 // Conditions based on user fields
98 $operators = condition_info
::get_condition_user_field_operators();
99 $useroptions = condition_info
::get_condition_user_fields();
102 $useroptions = array(0 => $strcondnone) +
$useroptions;
103 $grouparray = array();
104 $grouparray[] =& $mform->createElement('select', 'conditionfield', '', $useroptions);
105 $grouparray[] =& $mform->createElement('select', 'conditionfieldoperator', '', $operators);
106 $grouparray[] =& $mform->createElement('text', 'conditionfieldvalue');
107 $mform->setType('conditionfieldvalue', PARAM_RAW
);
108 $group = $mform->createElement('group', 'conditionfieldgroup', get_string('userfield', 'condition'), $grouparray);
110 $fieldcount = count($fullcs->conditionsfield
) +
1;
112 $this->repeat_elements(array($group), $fieldcount, array(), 'conditionfieldrepeats', 'conditionfieldadds', 2,
113 get_string('adduserfields', 'condition'), true);
114 $mform->addHelpButton('conditionfieldgroup[0]', 'userfield', 'condition');
116 // Conditions based on completion
117 $completion = new completion_info($course);
118 if ($completion->is_enabled()) {
119 $completionoptions = array();
120 $modinfo = get_fast_modinfo($course);
121 foreach ($modinfo->cms
as $id => $cm) {
122 // Add each course-module if it:
123 // (a) has completion turned on
124 // (b) does not belong to current course-section
125 if ($cm->completion
&& ($fullcs->id
!= $cm->section
)) {
126 $completionoptions[$id] = $cm->name
;
129 asort($completionoptions);
130 $completionoptions = array(0 => $strcondnone) +
133 $completionvalues = array(
134 COMPLETION_COMPLETE
=> get_string('completion_complete', 'condition'),
135 COMPLETION_INCOMPLETE
=> get_string('completion_incomplete', 'condition'),
136 COMPLETION_COMPLETE_PASS
=> get_string('completion_pass', 'condition'),
137 COMPLETION_COMPLETE_FAIL
=> get_string('completion_fail', 'condition'));
139 $grouparray = array();
140 $grouparray[] = $mform->createElement('select', 'conditionsourcecmid', '',
142 $grouparray[] = $mform->createElement('select', 'conditionrequiredcompletion', '',
144 $group = $mform->createElement('group', 'conditioncompletiongroup',
145 get_string('completioncondition', 'condition'), $grouparray);
147 $count = count($fullcs->conditionscompletion
) +
1;
148 $this->repeat_elements(array($group), $count, array(),
149 'conditioncompletionrepeats', 'conditioncompletionadds', 2,
150 get_string('addcompletions', 'condition'), true);
151 $mform->addHelpButton('conditioncompletiongroup[0]',
152 'completionconditionsection', 'condition');
155 // Availability conditions - set up form values
156 if (!empty($CFG->enableavailability
)) {
158 foreach ($fullcs->conditionsgrade
as $gradeitemid => $minmax) {
159 $groupelements = $mform->getElement(
160 'conditiongradegroup[' . $num . ']')->getElements();
161 $groupelements[0]->setValue($gradeitemid);
162 $groupelements[2]->setValue(is_null($minmax->min
) ?
'' :
163 format_float($minmax->min
, 5, true, true));
164 $groupelements[4]->setValue(is_null($minmax->max
) ?
'' :
165 format_float($minmax->max
, 5, true, true));
170 foreach ($fullcs->conditionsfield
as $fieldid => $data) {
171 $groupelements = $mform->getElement(
172 'conditionfieldgroup[' . $num . ']')->getElements();
173 $groupelements[0]->setValue($fieldid);
174 $groupelements[1]->setValue(is_null($data->operator
) ?
'' :
176 $groupelements[2]->setValue(is_null($data->value
) ?
'' :
181 if ($completion->is_enabled()) {
183 foreach ($fullcs->conditionscompletion
as $othercmid => $state) {
184 $groupelements = $mform->getElement('conditioncompletiongroup[' . $num . ']')->getElements();
185 $groupelements[0]->setValue($othercmid);
186 $groupelements[1]->setValue($state);
192 // Do we display availability info to students?
194 CONDITION_STUDENTVIEW_SHOW
=> get_string('showavailabilitysection_show', 'condition'),
195 CONDITION_STUDENTVIEW_HIDE
=> get_string('showavailabilitysection_hide', 'condition'));
196 $mform->addElement('select', 'showavailability',
197 get_string('showavailabilitysection', 'condition'), $showhide);
199 $mform->setDefault('showavailability', $this->_customdata
['showavailability']);
202 $this->add_action_buttons();
205 public function validation($data, $files) {
206 $errors = parent
::validation($data, $files);
207 // Conditions: Don't let them set dates which make no sense
208 if (array_key_exists('availablefrom', $data) &&
209 $data['availablefrom'] && $data['availableuntil'] &&
210 $data['availablefrom'] > $data['availableuntil']) {
211 $errors['availablefrom'] = get_string('badavailabledates', 'condition');
214 // Conditions: Verify that the user profile field has not been declared more than once
215 if (array_key_exists('conditionfieldgroup', $data)) {
216 // Array to store the existing fields
217 $arrcurrentfields = array();
218 // Error message displayed if any condition is declared more than once. We use lang string because
219 // this way we don't actually generate the string unless there is an error.
220 $stralreadydeclaredwarning = new lang_string('fielddeclaredmultipletimes', 'condition');
221 foreach ($data['conditionfieldgroup'] as $i => $fielddata) {
222 if ($fielddata['conditionfield'] == 0) { // Don't need to bother if none is selected
225 if (in_array($fielddata['conditionfield'], $arrcurrentfields)) {
226 $errors["conditionfieldgroup[{$i}]"] = $stralreadydeclaredwarning->out();
228 // Add the field to the array
229 $arrcurrentfields[] = $fielddata['conditionfield'];