Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
if (!defined('MOODLE_INTERNAL')) {
4
    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
5
}
6
 
7
require_once($CFG->libdir.'/formslib.php');
8
require_once($CFG->libdir.'/filelib.php');
9
require_once($CFG->libdir.'/completionlib.php');
10
require_once($CFG->libdir.'/gradelib.php');
11
 
12
/**
13
 * Default form for editing course section
14
 *
15
 * Course format plugins may specify different editing form to use
16
 */
17
class editsection_form extends moodleform {
18
 
19
    function definition() {
20
        global $CFG, $OUTPUT;
21
 
22
        $mform  = $this->_form;
23
        $course = $this->_customdata['course'];
24
        $sectioninfo = $this->_customdata['cs'];
25
 
26
        $mform->addElement('header', 'generalhdr', get_string('general'));
27
 
28
        $mform->addElement(
29
            'text',
30
            'name',
31
            get_string('sectionname'),
32
            [
33
                'placeholder' => $this->_customdata['defaultsectionname'],
34
                'size' => 30,
35
                'maxlength' => 255,
36
            ],
37
        );
38
        $mform->setType('name', PARAM_RAW);
39
        $mform->setDefault('name', $sectioninfo->name);
40
        $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
41
 
42
        /// Prepare course and the editor
43
 
44
        $mform->addElement('editor', 'summary_editor', get_string('description'), null, $this->_customdata['editoroptions']);
45
        $mform->setType('summary_editor', PARAM_RAW);
46
 
47
        $mform->addElement('hidden', 'id');
48
        $mform->setType('id', PARAM_INT);
49
 
1441 ariadna 50
        $mform->addElement('hidden', 'course', 0);
51
        $mform->setType('course', PARAM_INT);
52
 
1 efrain 53
        // additional fields that course format has defined
54
        $courseformat = course_get_format($course);
55
        $formatoptions = $courseformat->section_format_options(true);
56
        if (!empty($formatoptions)) {
57
            $elements = $courseformat->create_edit_form_elements($mform, true);
58
        }
59
 
60
        if (!empty($CFG->enableavailability)) {
61
            $mform->addElement('header', 'availabilityconditions',
62
                get_string('restrictaccess', 'availability'));
63
            $mform->setExpanded('availabilityconditions', false);
64
 
65
            // Availability field. This is just a textarea; the user interface
66
            // interaction is all implemented in JavaScript. The field is named
67
            // availabilityconditionsjson for consistency with moodleform_mod.
68
            $mform->addElement('textarea', 'availabilityconditionsjson',
69
                get_string('accessrestrictions', 'availability'),
70
                ['class' => 'd-none']
71
            );
72
            // Availability loading indicator.
73
            $loadingcontainer = $OUTPUT->container(
74
                $OUTPUT->render_from_template('core/loading', []),
75
                'd-flex justify-content-center py-5 icon-size-5',
76
                'availabilityconditions-loading'
77
            );
78
            $mform->addElement('html', $loadingcontainer);
79
        }
80
 
81
        $mform->_registerCancelButton('cancel');
82
    }
83
 
84
    public function definition_after_data() {
85
        global $CFG;
86
 
87
        $mform  = $this->_form;
88
        $course = $this->_customdata['course'];
89
 
90
        if (!empty($CFG->enableavailability)) {
91
            \core_availability\frontend::include_all_javascript($course, null,
92
                    $this->_customdata['cs']);
93
        }
94
 
95
        $this->add_action_buttons();
96
    }
97
 
98
    /**
99
     * Load in existing data as form defaults
100
     *
101
     * @param stdClass|array $default_values object or array of default values
102
     */
103
    function set_data($default_values) {
104
        if (!is_object($default_values)) {
105
            // we need object for file_prepare_standard_editor
106
            $default_values = (object)$default_values;
107
        }
108
        $editoroptions = $this->_customdata['editoroptions'];
109
        $default_values = file_prepare_standard_editor($default_values, 'summary', $editoroptions,
110
                $editoroptions['context'], 'course', 'section', $default_values->id);
111
        parent::set_data($default_values);
112
    }
113
 
114
    /**
115
     * Return submitted data if properly submitted or returns NULL if validation fails or
116
     * if there is no submitted data.
117
     *
118
     * @return object submitted data; NULL if not valid or not submitted or cancelled
119
     */
120
    function get_data() {
121
        $data = parent::get_data();
122
        if ($data !== null) {
123
            $editoroptions = $this->_customdata['editoroptions'];
124
            // Set name as an empty string if use default section name is checked.
125
            if ($data->name === false) {
126
                $data->name = '';
127
            }
128
            $data = file_postupdate_standard_editor($data, 'summary', $editoroptions,
129
                    $editoroptions['context'], 'course', 'section', $data->id);
130
            $course = $this->_customdata['course'];
131
            foreach (course_get_format($course)->section_format_options() as $option => $unused) {
132
                // fix issue with unset checkboxes not being returned at all
133
                if (!isset($data->$option)) {
134
                    $data->$option = null;
135
                }
136
            }
137
        }
138
        return $data;
139
    }
140
 
141
    public function validation($data, $files) {
142
        global $CFG;
143
        $errors = array();
144
 
145
        // Availability: Check availability field does not have errors.
146
        if (!empty($CFG->enableavailability)) {
147
            \core_availability\frontend::report_validation_errors($data, $errors);
148
        }
149
 
150
        return $errors;
151
    }
152
}