Proyectos de Subversion Moodle

Rev

| 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
 
50
        // additional fields that course format has defined
51
        $courseformat = course_get_format($course);
52
        $formatoptions = $courseformat->section_format_options(true);
53
        if (!empty($formatoptions)) {
54
            $elements = $courseformat->create_edit_form_elements($mform, true);
55
        }
56
 
57
        if (!empty($CFG->enableavailability)) {
58
            $mform->addElement('header', 'availabilityconditions',
59
                get_string('restrictaccess', 'availability'));
60
            $mform->setExpanded('availabilityconditions', false);
61
 
62
            // Availability field. This is just a textarea; the user interface
63
            // interaction is all implemented in JavaScript. The field is named
64
            // availabilityconditionsjson for consistency with moodleform_mod.
65
            $mform->addElement('textarea', 'availabilityconditionsjson',
66
                get_string('accessrestrictions', 'availability'),
67
                ['class' => 'd-none']
68
            );
69
            // Availability loading indicator.
70
            $loadingcontainer = $OUTPUT->container(
71
                $OUTPUT->render_from_template('core/loading', []),
72
                'd-flex justify-content-center py-5 icon-size-5',
73
                'availabilityconditions-loading'
74
            );
75
            $mform->addElement('html', $loadingcontainer);
76
        }
77
 
78
        $mform->_registerCancelButton('cancel');
79
    }
80
 
81
    public function definition_after_data() {
82
        global $CFG;
83
 
84
        $mform  = $this->_form;
85
        $course = $this->_customdata['course'];
86
 
87
        if (!empty($CFG->enableavailability)) {
88
            \core_availability\frontend::include_all_javascript($course, null,
89
                    $this->_customdata['cs']);
90
        }
91
 
92
        $this->add_action_buttons();
93
    }
94
 
95
    /**
96
     * Load in existing data as form defaults
97
     *
98
     * @param stdClass|array $default_values object or array of default values
99
     */
100
    function set_data($default_values) {
101
        if (!is_object($default_values)) {
102
            // we need object for file_prepare_standard_editor
103
            $default_values = (object)$default_values;
104
        }
105
        $editoroptions = $this->_customdata['editoroptions'];
106
        $default_values = file_prepare_standard_editor($default_values, 'summary', $editoroptions,
107
                $editoroptions['context'], 'course', 'section', $default_values->id);
108
        parent::set_data($default_values);
109
    }
110
 
111
    /**
112
     * Return submitted data if properly submitted or returns NULL if validation fails or
113
     * if there is no submitted data.
114
     *
115
     * @return object submitted data; NULL if not valid or not submitted or cancelled
116
     */
117
    function get_data() {
118
        $data = parent::get_data();
119
        if ($data !== null) {
120
            $editoroptions = $this->_customdata['editoroptions'];
121
            // Set name as an empty string if use default section name is checked.
122
            if ($data->name === false) {
123
                $data->name = '';
124
            }
125
            $data = file_postupdate_standard_editor($data, 'summary', $editoroptions,
126
                    $editoroptions['context'], 'course', 'section', $data->id);
127
            $course = $this->_customdata['course'];
128
            foreach (course_get_format($course)->section_format_options() as $option => $unused) {
129
                // fix issue with unset checkboxes not being returned at all
130
                if (!isset($data->$option)) {
131
                    $data->$option = null;
132
                }
133
            }
134
        }
135
        return $data;
136
    }
137
 
138
    public function validation($data, $files) {
139
        global $CFG;
140
        $errors = array();
141
 
142
        // Availability: Check availability field does not have errors.
143
        if (!empty($CFG->enableavailability)) {
144
            \core_availability\frontend::report_validation_errors($data, $errors);
145
        }
146
 
147
        return $errors;
148
    }
149
}