Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 20... Línea 20...
20
 * @package   block_rss_client
20
 * @package   block_rss_client
21
 * @copyright 2009 Tim Hunt
21
 * @copyright 2009 Tim Hunt
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
23
 */
Línea -... Línea 24...
-
 
24
 
-
 
25
defined('MOODLE_INTERNAL') || die();
-
 
26
 
-
 
27
require_once($CFG->libdir .'/simplepie/moodle_simplepie.php');
24
 
28
 
25
/**
29
/**
26
 * Form for editing RSS client block instances.
30
 * Form for editing RSS client block instances.
27
 *
31
 *
28
 * @copyright 2009 Tim Hunt
32
 * @copyright 2009 Tim Hunt
29
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
34
 */
-
 
35
class block_rss_client_edit_form extends block_edit_form {
-
 
36
 
-
 
37
    /** @var stdClass|null The new RSS feed URL object. */
-
 
38
    private ?stdClass $newrss = null;
31
class block_rss_client_edit_form extends block_edit_form {
39
 
32
    protected function specific_definition($mform) {
40
    protected function specific_definition($mform) {
Línea 33... Línea 41...
33
        global $CFG, $DB, $USER;
41
        global $CFG, $DB, $USER;
34
 
42
 
Línea 35... Línea 43...
35
        // Fields for editing block contents.
43
        // Fields for editing block contents.
36
        $mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));
44
        $mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));
37
 
-
 
38
        $mform->addElement('selectyesno', 'config_display_description', get_string('displaydescriptionlabel', 'block_rss_client'));
45
 
39
        $mform->setDefault('config_display_description', 0);
46
        $radiogroup = [
40
 
47
            $mform->createElement('radio', 'config_method',
-
 
48
                get_string('configmethodexisting', 'block_rss_client'), null, 'existing'),
-
 
49
            $mform->createElement('radio', 'config_method',
-
 
50
                get_string('configmethodnew', 'block_rss_client'), null, 'new'),
41
        $mform->addElement('text', 'config_shownumentries', get_string('shownumentrieslabel', 'block_rss_client'), array('size' => 5));
51
        ];
42
        $mform->setType('config_shownumentries', PARAM_INT);
52
        $mform->addGroup(
-
 
53
            elements: $radiogroup,
43
        $mform->addRule('config_shownumentries', null, 'numeric', null, 'client');
54
            name: 'config_method_group',
44
        if (!empty($CFG->block_rss_client_num_entries)) {
55
            separator: ['   '],
-
 
56
            appendName: false,
45
            $mform->setDefault('config_shownumentries', $CFG->block_rss_client_num_entries);
57
        );
-
 
58
        $mform->setDefault('config_method', 'existing');
-
 
59
 
-
 
60
        // Add new RSS feed.
Línea -... Línea 61...
-
 
61
        $mform->addElement('text', 'config_feedurl', get_string('feedurl', 'block_rss_client'));
46
        } else {
62
        $mform->setType('config_feedurl', PARAM_URL);
47
            $mform->setDefault('config_shownumentries', 5);
63
        $mform->hideIf('config_feedurl', 'config_method', 'ne', 'new');
48
        }
64
 
49
 
65
        // Select existing RSS feed.
50
        $insql = '';
66
        $insql = '';
Línea 66... Línea 82...
66
                $params);
82
                $params);
Línea 67... Línea 83...
67
 
83
 
68
        if ($rssfeeds) {
84
        if ($rssfeeds) {
69
            $select = $mform->addElement('select', 'config_rssid', get_string('choosefeedlabel', 'block_rss_client'), $rssfeeds);
85
            $select = $mform->addElement('select', 'config_rssid', get_string('choosefeedlabel', 'block_rss_client'), $rssfeeds);
70
            $select->setMultiple(true);
-
 
-
 
86
            $select->setMultiple(true);
71
 
87
            $mform->hideIf('config_rssid', 'config_method', 'ne', 'existing');
72
        } else {
88
        } else {
73
            $mform->addElement('static', 'config_rssid_no_feeds', get_string('choosefeedlabel', 'block_rss_client'),
89
            $mform->addElement('static', 'config_rssid_no_feeds', get_string('choosefeedlabel', 'block_rss_client'),
-
 
90
                    get_string('nofeeds', 'block_rss_client'));
74
                    get_string('nofeeds', 'block_rss_client'));
91
            $mform->hideIf('config_rssid_no_feeds', 'config_method', 'ne', 'existing');
Línea 75... Línea -...
75
        }
-
 
76
 
92
        }
77
        if (has_any_capability(array('block/rss_client:manageanyfeeds', 'block/rss_client:manageownfeeds'), $this->block->context)) {
93
 
-
 
94
        // Subheading: Display settings for RSS feed.
78
            $mform->addElement('static', 'nofeedmessage', '',
95
        $startsubheading = '<div class="row"><h4 class="col-md-12 col-form-label d-flex">';
79
                    '<a href="' . $CFG->wwwroot . '/blocks/rss_client/managefeeds.php?courseid=' . $this->page->course->id . '">' .
-
 
Línea 80... Línea 96...
80
                    get_string('feedsaddedit', 'block_rss_client') . '</a>');
96
        $endsubheading = '</h4></div>';
81
        }
97
        $mform->addElement('html', $startsubheading . get_string('displaysettings', 'block_rss_client') . $endsubheading);
Línea -... Línea 98...
-
 
98
 
-
 
99
        $mform->addElement('text', 'config_title', get_string('uploadlabel'));
-
 
100
        $mform->setType('config_title', PARAM_NOTAGS);
-
 
101
 
-
 
102
        $mform->addElement('selectyesno', 'config_display_description', get_string('displaydescriptionlabel', 'block_rss_client'));
-
 
103
        $mform->setDefault('config_display_description', 0);
-
 
104
 
-
 
105
        $mform->addElement('text', 'config_shownumentries', get_string('shownumentrieslabel', 'block_rss_client'), ['size' => 5]);
-
 
106
        $mform->setType('config_shownumentries', PARAM_INT);
-
 
107
        $mform->addRule('config_shownumentries', null, 'numeric', null, 'client');
-
 
108
        if (!empty($CFG->block_rss_client_num_entries)) {
-
 
109
            $mform->setDefault('config_shownumentries', $CFG->block_rss_client_num_entries);
82
 
110
        } else {
83
        $mform->addElement('text', 'config_title', get_string('uploadlabel'));
111
            $mform->setDefault('config_shownumentries', 5);
Línea 84... Línea 112...
84
        $mform->setType('config_title', PARAM_NOTAGS);
112
        }
85
 
113
 
86
        $mform->addElement('selectyesno', 'config_block_rss_client_show_channel_link', get_string('clientshowchannellinklabel', 'block_rss_client'));
114
        $mform->addElement('selectyesno', 'config_block_rss_client_show_channel_link', get_string('clientshowchannellinklabel', 'block_rss_client'));
Línea 87... Línea 115...
87
        $mform->setDefault('config_block_rss_client_show_channel_link', 0);
115
        $mform->setDefault('config_block_rss_client_show_channel_link', 0);
-
 
116
 
-
 
117
        $mform->addElement('selectyesno', 'config_block_rss_client_show_channel_image', get_string('clientshowimagelabel', 'block_rss_client'));
-
 
118
        $mform->setDefault('config_block_rss_client_show_channel_image', 0);
-
 
119
    }
-
 
120
 
-
 
121
    /**
-
 
122
     * Overriding the get_data function to insert a new RSS ID.
-
 
123
     */
-
 
124
    public function get_data(): ?stdClass {
-
 
125
        $data = parent::get_data();
-
 
126
        // Force the 'existing` method as a default.
-
 
127
        $data->config_method = 'existing';
-
 
128
        // Sanitize the title to prevent XSS (Cross-Site Scripting) attacks by encoding special characters into HTML entities.
-
 
129
        $data->config_title = htmlspecialchars($data->config_title, ENT_QUOTES, 'utf-8');
-
 
130
        // If the new RSS is not empty then add the ID to the config_rssid.
-
 
131
        if ($data && $this->newrss) {
-
 
132
            $data->config_rssid[] = $this->newrss->id;
-
 
133
        }
-
 
134
        return $data;
-
 
135
    }
-
 
136
 
-
 
137
    /**
-
 
138
     * Overriding the definition_after_data to empty the input.
-
 
139
     */
-
 
140
    public function definition_after_data(): void {
-
 
141
        parent::definition_after_data();
-
 
142
        $mform =& $this->_form;
-
 
143
        // If form is not submitted then empty the feed URL.
-
 
144
        if (!$this->is_submitted()) {
-
 
145
            $mform->getElement('config_feedurl')->setValue('');
-
 
146
        }
-
 
147
    }
-
 
148
 
-
 
149
    /**
-
 
150
     * Overriding the validation to validate the RSS URL and store it to the database.
-
 
151
     *
-
 
152
     * If there are no errors, insert the new feed to the database and store the object in
-
 
153
     * the private property so it can be saved to the RSS block config.
-
 
154
     *
-
 
155
     * @param array $data Data from the form.
-
 
156
     * @param array $files Files form the form.
-
 
157
     * @return array of errors from validation.
-
 
158
     */
-
 
159
    public function validation($data, $files): array {
-
 
160
        global $USER, $DB;
-
 
161
        $errors = parent::validation($data, $files);
-
 
162
 
-
 
163
        if ($data['config_method'] === "new") {
-
 
164
            // If the "New" method is selected and the feed URL is not empty, then proceed.
-
 
165
            if ($data['config_feedurl']) {
-
 
166
                if (!filter_var($data['config_feedurl'], FILTER_VALIDATE_URL)) {
-
 
167
                    $errors['config_feedurl'] = get_string('couldnotfindloadrssfeed', 'block_rss_client');
-
 
168
                    return $errors;
-
 
169
                }
-
 
170
                try {
-
 
171
                    $rss = new moodle_simplepie();
-
 
172
                    // Set timeout for longer than normal to try and grab the feed.
-
 
173
                    $rss->set_timeout(10);
-
 
174
                    $rss->set_feed_url($data['config_feedurl']);
-
 
175
                    $rss->set_autodiscovery_cache_duration(0);
-
 
176
                    $rss->set_autodiscovery_level(moodle_simplepie::LOCATOR_ALL);
-
 
177
                    $rss->init();
-
 
178
                    if ($rss->error()) {
-
 
179
                        $errors['config_feedurl'] = get_string('couldnotfindloadrssfeed', 'block_rss_client');
-
 
180
                    } else {
-
 
181
                        // Return URL without quoting.
-
 
182
                        $discoveredurl = new moodle_url($rss->subscribe_url());
-
 
183
                        $theurl = $discoveredurl->out(false);
-
 
184
                        // Save the RSS to the database.
-
 
185
                        $this->newrss = new stdClass;
-
 
186
                        $this->newrss->userid = $USER->id;
-
 
187
                        $this->newrss->title = $rss->get_title();
-
 
188
                        $this->newrss->description = $rss->get_description();
-
 
189
                        $this->newrss->url = $theurl;
-
 
190
                        $newrssid = $DB->insert_record('block_rss_client', $this->newrss);
-
 
191
                        $this->newrss->id = $newrssid;
-
 
192
                    }
-
 
193
                } catch (Exception $e) {
-
 
194
                    $errors['config_feedurl'] = get_string('couldnotfindloadrssfeed', 'block_rss_client');
-
 
195
                }
-
 
196
            } else {
-
 
197
                // If the "New" method is selected, but the feed URL is empty, then raise error.
-
 
198
                $errors['config_feedurl'] = get_string('err_required', 'form');
-
 
199
            }
-
 
200
 
88
 
201
        }
89
        $mform->addElement('selectyesno', 'config_block_rss_client_show_channel_image', get_string('clientshowimagelabel', 'block_rss_client'));
202
 
90
        $mform->setDefault('config_block_rss_client_show_channel_image', 0);
203
        return $errors;
91
    }
204
    }
92
 
205