Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
defined('MOODLE_INTERNAL') OR die('not allowed');
18
require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
19
 
20
class feedback_item_captcha extends feedback_item_base {
21
    protected $type = "captcha";
22
 
23
    public function build_editform($item, $feedback, $cm) {
24
        global $DB;
25
 
26
        $editurl = new moodle_url('/mod/feedback/edit.php', array('id'=>$cm->id));
27
 
28
        // There are no settings for recaptcha.
29
        if (isset($item->id) AND $item->id > 0) {
30
            notice(get_string('there_are_no_settings_for_recaptcha', 'feedback'), $editurl->out());
31
            exit;
32
        }
33
 
34
        // Only one recaptcha can be in a feedback.
35
        $params = array('feedback' => $feedback->id, 'typ' => $this->type);
36
        if ($DB->record_exists('feedback_item', $params)) {
37
            notice(get_string('only_one_captcha_allowed', 'feedback'), $editurl->out());
38
            exit;
39
        }
40
 
41
        $this->item = $item;
42
        $this->item_form = true; // Dummy.
43
 
44
        $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
45
 
46
        $this->item->feedback = $feedback->id;
47
        $this->item->template = 0;
48
        $this->item->name = get_string('captcha', 'feedback');
49
        $this->item->label = '';
50
        $this->item->presentation = '';
51
        $this->item->typ = $this->type;
52
        $this->item->hasvalue = $this->get_hasvalue();
53
        $this->item->position = $lastposition + 1;
54
        $this->item->required = 1;
55
        $this->item->dependitem = 0;
56
        $this->item->dependvalue = '';
57
        $this->item->options = '';
58
    }
59
 
60
    public function show_editform() {
61
    }
62
 
63
    public function is_cancelled() {
64
        return false;
65
    }
66
 
67
    public function get_data() {
68
        return true;
69
    }
70
 
71
    public function save_item() {
72
        global $DB;
73
 
74
        if (!$this->item) {
75
            return false;
76
        }
77
 
78
        if (empty($this->item->id)) {
79
            $this->item->id = $DB->insert_record('feedback_item', $this->item);
80
        } else {
81
            $DB->update_record('feedback_item', $this->item);
82
        }
83
 
84
        return $DB->get_record('feedback_item', array('id'=>$this->item->id));
85
    }
86
 
87
    public function get_printval($item, $value) {
88
        return '';
89
    }
90
 
91
    public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
92
        return $itemnr;
93
    }
94
 
95
    public function excelprint_item(&$worksheet, $row_offset,
96
                             $xls_formats, $item,
97
                             $groupid, $courseid = false) {
98
        return $row_offset;
99
    }
100
 
101
    /**
102
     * Returns the formatted name of the item for the complete form or response view
103
     *
104
     * @param stdClass $item
105
     * @param bool $withpostfix
106
     * @return string
107
     */
108
    public function get_display_name($item, $withpostfix = true) {
109
        return get_string('captcha', 'feedback');
110
    }
111
 
112
    /**
113
     * Adds an input element to the complete form
114
     *
115
     * @param stdClass $item
116
     * @param mod_feedback_complete_form $form
117
     */
118
    public function complete_form_element($item, $form) {
119
        $name = $this->get_display_name($item);
120
        $inputname = $item->typ . '_' . $item->id;
121
 
122
        if ($form->get_mode() != mod_feedback_complete_form::MODE_COMPLETE) {
123
            // Span to hold the element id. The id is used for drag and drop reordering.
124
            $form->add_form_element($item,
125
                    ['static', $inputname, $name, html_writer::span('', '', ['id' => 'feedback_item_' . $item->id])],
126
                    false,
127
                    false);
128
        } else {
129
            // Add recaptcha element that is used during the form validation.
130
            $form->add_form_element($item,
131
                    ['recaptcha', $inputname . 'recaptcha', $name],
132
                    false,
133
                    false);
134
            // Add hidden element with value "1" that will be saved in the values table after completion.
135
            $form->add_form_element($item, ['hidden', $inputname, 1], false);
136
            $form->set_element_type($inputname, PARAM_INT);
137
        }
138
 
139
        // Add recaptcha validation to the form.
140
        $form->add_validation_rule(function($values, $files) use ($item, $form) {
141
            $elementname = $item->typ . '_' . $item->id . 'recaptcha';
142
            $recaptchaelement = $form->get_form_element($elementname);
143
            if (empty($values['g-recaptcha-response'])) {
144
                return array($elementname => get_string('required'));
145
            } else {
146
                $response = $values['g-recaptcha-response'];
147
                if (true !== ($result = $recaptchaelement->verify($response))) {
148
                    return array($elementname => $result);
149
                }
150
            }
151
            return true;
152
        });
153
 
154
    }
155
 
156
    public function create_value($data) {
157
        global $USER;
158
        return $USER->sesskey;
159
    }
160
 
161
    public function get_hasvalue() {
162
        global $CFG;
163
 
164
        // Is recaptcha configured in moodle?
165
        if (empty($CFG->recaptchaprivatekey) OR empty($CFG->recaptchapublickey)) {
166
            return 0;
167
        }
168
        return 1;
169
    }
170
 
171
    public function can_switch_require() {
172
        return false;
173
    }
174
 
175
    /**
176
     * Returns the list of actions allowed on this item in the edit mode
177
     *
178
     * @param stdClass $item
179
     * @param stdClass $feedback
180
     * @param cm_info $cm
181
     * @return action_menu_link[]
182
     */
183
    public function edit_actions($item, $feedback, $cm) {
184
        $actions = parent::edit_actions($item, $feedback, $cm);
185
        unset($actions['update']);
186
        return $actions;
187
    }
188
 
189
    public function get_data_for_external($item) {
190
        global $CFG;
191
 
192
        if (empty($CFG->recaptchaprivatekey) || empty($CFG->recaptchapublickey)) {
193
            return null;
194
        }
195
 
196
        // With reCAPTCHA v2 the captcha will be rendered by the mobile client using just the publickey.
197
        $data[] = $CFG->recaptchapublickey;
198
        return json_encode($data);
199
    }
200
 
201
    /**
202
     * Return the analysis data ready for external functions.
203
     *
204
     * @param stdClass $item     the item (question) information
205
     * @param int      $groupid  the group id to filter data (optional)
206
     * @param int      $courseid the course id (optional)
207
     * @return array an array of data with non scalar types json encoded
208
     * @since  Moodle 3.3
209
     */
210
    public function get_analysed_for_external($item, $groupid = false, $courseid = false) {
211
        return [];
212
    }
213
}