MDL-65754 mod_feedback: Remove unused v1 recaptcha
[moodle.git] / mod / feedback / item / captcha / lib.php
CommitLineData
1adbd2c3 1<?php
9c5bc7a5
AG
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
01910dff 17defined('MOODLE_INTERNAL') OR die('not allowed');
c70ad9f7 18require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
19
20class feedback_item_captcha extends feedback_item_base {
9c5bc7a5 21 protected $type = "captcha";
d4b1d58c 22
9c5bc7a5 23 public function build_editform($item, $feedback, $cm) {
6cc1599e 24 global $DB;
4fc649ae 25
6cc1599e 26 $editurl = new moodle_url('/mod/feedback/edit.php', array('id'=>$cm->id));
d4b1d58c 27
f567f34b 28 // There are no settings for recaptcha.
9c5bc7a5 29 if (isset($item->id) AND $item->id > 0) {
6cc1599e
AG
30 notice(get_string('there_are_no_settings_for_recaptcha', 'feedback'), $editurl->out());
31 exit;
a59ff6b0 32 }
4fc649ae 33
f567f34b 34 // Only one recaptcha can be in a feedback.
9c5bc7a5
AG
35 $params = array('feedback' => $feedback->id, 'typ' => $this->type);
36 if ($DB->record_exists('feedback_item', $params)) {
6cc1599e
AG
37 notice(get_string('only_one_captcha_allowed', 'feedback'), $editurl->out());
38 exit;
39 }
4fc649ae 40
6cc1599e 41 $this->item = $item;
f567f34b 42 $this->item_form = true; // Dummy.
4fc649ae 43
6cc1599e
AG
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');
2aad1094 49 $this->item->label = '';
6cc1599e
AG
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 = '';
a59ff6b0 58 }
c70ad9f7 59
9c5bc7a5 60 public function show_editform() {
a59ff6b0 61 }
4fc649ae 62
9c5bc7a5 63 public function is_cancelled() {
6cc1599e 64 return false;
a59ff6b0 65 }
d4b1d58c 66
9c5bc7a5 67 public function get_data() {
6cc1599e 68 return true;
a59ff6b0
AG
69 }
70
9c5bc7a5 71 public function save_item() {
a59ff6b0 72 global $DB;
4fc649ae 73
9c5bc7a5 74 if (!$this->item) {
a59ff6b0
AG
75 return false;
76 }
4fc649ae 77
9c5bc7a5 78 if (empty($this->item->id)) {
4fc649ae 79 $this->item->id = $DB->insert_record('feedback_item', $this->item);
9c5bc7a5 80 } else {
6cc1599e 81 $DB->update_record('feedback_item', $this->item);
a59ff6b0 82 }
4fc649ae
PS
83
84 return $DB->get_record('feedback_item', array('id'=>$this->item->id));
c70ad9f7 85 }
86
9c5bc7a5 87 public function get_printval($item, $value) {
c70ad9f7 88 return '';
89 }
90
9c5bc7a5 91 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
c70ad9f7 92 return $itemnr;
93 }
94
9c5bc7a5
AG
95 public function excelprint_item(&$worksheet, $row_offset,
96 $xls_formats, $item,
97 $groupid, $courseid = false) {
98 return $row_offset;
c70ad9f7 99 }
4fc649ae
PS
100
101 /**
c83c0522 102 * Returns the formatted name of the item for the complete form or response view
9d5fbd65 103 *
c83c0522
MG
104 * @param stdClass $item
105 * @param bool $withpostfix
106 * @return string
9d5fbd65 107 */
c83c0522
MG
108 public function get_display_name($item, $withpostfix = true) {
109 return get_string('captcha', 'feedback');
9d5fbd65 110 }
4fc649ae
PS
111
112 /**
c83c0522 113 * Adds an input element to the complete form
9d5fbd65 114 *
c83c0522
MG
115 * @param stdClass $item
116 * @param mod_feedback_complete_form $form
9d5fbd65 117 */
c83c0522
MG
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) {
195f646d 123 // Span to hold the element id. The id is used for drag and drop reordering.
c83c0522 124 $form->add_form_element($item,
195f646d 125 ['static', $inputname, $name, html_writer::span('', '', ['id' => 'feedback_item_' . $item->id])],
c83c0522
MG
126 false,
127 false);
9c5bc7a5 128 } else {
fdc6656a 129 // Add recaptcha element that is used during the form validation.
c83c0522 130 $form->add_form_element($item,
fdc6656a 131 ['recaptcha', $inputname . 'recaptcha', $name],
c83c0522
MG
132 false,
133 false);
fdc6656a
MG
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);
c83c0522
MG
137 }
138
139 // Add recaptcha validation to the form.
140 $form->add_validation_rule(function($values, $files) use ($item, $form) {
fdc6656a 141 $elementname = $item->typ . '_' . $item->id . 'recaptcha';
c83c0522 142 $recaptchaelement = $form->get_form_element($elementname);
f567f34b 143 if (empty($values['g-recaptcha-response'])) {
c83c0522 144 return array($elementname => get_string('required'));
f567f34b
JW
145 } else {
146 $response = $values['g-recaptcha-response'];
147 if (true !== ($result = $recaptchaelement->verify($response))) {
c83c0522
MG
148 return array($elementname => $result);
149 }
2aad1094 150 }
9c5bc7a5 151 return true;
c83c0522 152 });
4fc649ae 153
c70ad9f7 154 }
155
9c5bc7a5 156 public function create_value($data) {
6cc1599e
AG
157 global $USER;
158 return $USER->sesskey;
c70ad9f7 159 }
160
9c5bc7a5
AG
161 public function get_hasvalue() {
162 global $CFG;
163
f567f34b 164 // Is recaptcha configured in moodle?
9c5bc7a5
AG
165 if (empty($CFG->recaptchaprivatekey) OR empty($CFG->recaptchapublickey)) {
166 return 0;
167 }
c70ad9f7 168 return 1;
169 }
4fc649ae 170
9c5bc7a5 171 public function can_switch_require() {
6cc1599e
AG
172 return false;
173 }
e7fc2ff4 174
c83c0522
MG
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;
e7fc2ff4 187 }
d52973ae
JL
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
f567f34b 196 // With reCAPTCHA v2 the captcha will be rendered by the mobile client using just the publickey.
d52973ae
JL
197 $data[] = $CFG->recaptchapublickey;
198 return json_encode($data);
199 }
ac122465
JL
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 }
c70ad9f7 213}