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 // Grouping conditions - only if grouping is enabled at site level
42 if (!empty($CFG->enablegroupmembersonly
)) {
44 $options[0] = get_string('none');
45 if ($groupings = $DB->get_records('groupings', array('courseid' => $course->id
))) {
46 foreach ($groupings as $grouping) {
47 $context = context_course
::instance($course->id
);
48 $options[$grouping->id
] = format_string(
49 $grouping->name
, true, array('context' => $context));
52 $mform->addElement('header', '', get_string('availabilityconditions', 'condition'));
53 $mform->addElement('select', 'groupingid', get_string('groupingsection', 'group'), $options);
54 $mform->addHelpButton('groupingid', 'groupingsection', 'group');
57 // Date and time conditions
58 $mform->addElement('date_time_selector', 'availablefrom',
59 get_string('availablefrom', 'condition'), array('optional' => true));
60 $mform->addElement('date_time_selector', 'availableuntil',
61 get_string('availableuntil', 'condition'), array('optional' => true));
63 // Conditions based on grades
64 $gradeoptions = array();
65 $items = grade_item
::fetch_all(array('courseid' => $course->id
));
66 $items = $items ?
$items : array();
67 foreach ($items as $id => $item) {
68 $gradeoptions[$id] = $item->get_name();
71 $gradeoptions = array(0 => get_string('none', 'condition')) +
$gradeoptions;
73 $grouparray = array();
74 $grouparray[] = $mform->createElement('select', 'conditiongradeitemid', '', $gradeoptions);
75 $grouparray[] = $mform->createElement('static', '', '',
76 ' ' . get_string('grade_atleast', 'condition').' ');
77 $grouparray[] = $mform->createElement('text', 'conditiongrademin', '', array('size' => 3));
78 $grouparray[] = $mform->createElement('static', '', '',
79 '% ' . get_string('grade_upto', 'condition') . ' ');
80 $grouparray[] = $mform->createElement('text', 'conditiongrademax', '', array('size' => 3));
81 $grouparray[] = $mform->createElement('static', '', '', '%');
82 $group = $mform->createElement('group', 'conditiongradegroup',
83 get_string('gradecondition', 'condition'), $grouparray);
85 // Get full version (including condition info) of section object
86 $ci = new condition_info_section($this->_customdata
['cs']);
87 $fullcs = $ci->get_full_section();
88 $count = count($fullcs->conditionsgrade
) +
1;
91 $this->repeat_elements(array($group), $count, array(), 'conditiongraderepeats',
92 'conditiongradeadds', 2, get_string('addgrades', 'condition'), true);
93 $mform->addHelpButton('conditiongradegroup[0]', 'gradecondition', 'condition');
95 // Conditions based on completion
96 $completion = new completion_info($course);
97 if ($completion->is_enabled()) {
98 $completionoptions = array();
99 $modinfo = get_fast_modinfo($course);
100 foreach ($modinfo->cms
as $id => $cm) {
101 // Add each course-module if it:
102 // (a) has completion turned on
103 // (b) does not belong to current course-section
104 if ($cm->completion
&& ($fullcs->id
!= $cm->section
)) {
105 $completionoptions[$id] = $cm->name
;
108 asort($completionoptions);
109 $completionoptions = array(0 => get_string('none', 'condition')) +
112 $completionvalues = array(
113 COMPLETION_COMPLETE
=> get_string('completion_complete', 'condition'),
114 COMPLETION_INCOMPLETE
=> get_string('completion_incomplete', 'condition'),
115 COMPLETION_COMPLETE_PASS
=> get_string('completion_pass', 'condition'),
116 COMPLETION_COMPLETE_FAIL
=> get_string('completion_fail', 'condition'));
118 $grouparray = array();
119 $grouparray[] = $mform->createElement('select', 'conditionsourcecmid', '',
121 $grouparray[] = $mform->createElement('select', 'conditionrequiredcompletion', '',
123 $group = $mform->createElement('group', 'conditioncompletiongroup',
124 get_string('completioncondition', 'condition'), $grouparray);
126 $count = count($fullcs->conditionscompletion
) +
1;
127 $this->repeat_elements(array($group), $count, array(),
128 'conditioncompletionrepeats', 'conditioncompletionadds', 2,
129 get_string('addcompletions', 'condition'), true);
130 $mform->addHelpButton('conditioncompletiongroup[0]',
131 'completionconditionsection', 'condition');
134 // Availability conditions - set up form values
135 if (!empty($CFG->enableavailability
)) {
137 foreach ($fullcs->conditionsgrade
as $gradeitemid => $minmax) {
138 $groupelements = $mform->getElement(
139 'conditiongradegroup[' . $num . ']')->getElements();
140 $groupelements[0]->setValue($gradeitemid);
141 $groupelements[2]->setValue(is_null($minmax->min
) ?
'' :
142 format_float($minmax->min
, 5, true, true));
143 $groupelements[4]->setValue(is_null($minmax->max
) ?
'' :
144 format_float($minmax->max
, 5, true, true));
148 if ($completion->is_enabled()) {
150 foreach ($fullcs->conditionscompletion
as $othercmid => $state) {
151 $groupelements = $mform->getElement('conditioncompletiongroup[' . $num . ']')->getElements();
152 $groupelements[0]->setValue($othercmid);
153 $groupelements[1]->setValue($state);
159 // Do we display availability info to students?
161 CONDITION_STUDENTVIEW_SHOW
=> get_string('showavailabilitysection_show', 'condition'),
162 CONDITION_STUDENTVIEW_HIDE
=> get_string('showavailabilitysection_hide', 'condition'));
163 $mform->addElement('select', 'showavailability',
164 get_string('showavailabilitysection', 'condition'), $showhide);
166 $mform->setDefault('showavailability', $this->_customdata
['showavailability']);
169 $this->add_action_buttons();
172 public function validation($data, $files) {
173 $errors = parent
::validation($data, $files);
174 // Conditions: Don't let them set dates which make no sense
175 if (array_key_exists('availablefrom', $data) &&
176 $data['availablefrom'] && $data['availableuntil'] &&
177 $data['availablefrom'] > $data['availableuntil']) {
178 $errors['availablefrom'] = get_string('badavailabledates', 'condition');