Merge branch 'MDL-27675_master' of git://github.com/grabs/moodle
[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
AG
21 protected $type = "captcha";
22 private $commonparams;
23 private $item_form = false;
24 private $item = false;
25 private $feedback = false;
a59ff6b0 26
9c5bc7a5 27 public function init() {
d4b1d58c 28
c70ad9f7 29 }
d4b1d58c 30
9c5bc7a5 31 public function build_editform($item, $feedback, $cm) {
6cc1599e 32 global $DB;
4fc649ae 33
6cc1599e 34 $editurl = new moodle_url('/mod/feedback/edit.php', array('id'=>$cm->id));
d4b1d58c 35
6cc1599e 36 //ther are no settings for recaptcha
9c5bc7a5 37 if (isset($item->id) AND $item->id > 0) {
6cc1599e
AG
38 notice(get_string('there_are_no_settings_for_recaptcha', 'feedback'), $editurl->out());
39 exit;
a59ff6b0 40 }
4fc649ae 41
6cc1599e 42 //only one recaptcha can be in a feedback
9c5bc7a5
AG
43 $params = array('feedback' => $feedback->id, 'typ' => $this->type);
44 if ($DB->record_exists('feedback_item', $params)) {
6cc1599e
AG
45 notice(get_string('only_one_captcha_allowed', 'feedback'), $editurl->out());
46 exit;
47 }
4fc649ae 48
6cc1599e
AG
49 $this->item = $item;
50 $this->feedback = $feedback;
51 $this->item_form = true; //dummy
4fc649ae 52
6cc1599e
AG
53 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
54
55 $this->item->feedback = $feedback->id;
56 $this->item->template = 0;
57 $this->item->name = get_string('captcha', 'feedback');
58 $this->item->label = get_string('captcha', 'feedback');
59 $this->item->presentation = '';
60 $this->item->typ = $this->type;
61 $this->item->hasvalue = $this->get_hasvalue();
62 $this->item->position = $lastposition + 1;
63 $this->item->required = 1;
64 $this->item->dependitem = 0;
65 $this->item->dependvalue = '';
66 $this->item->options = '';
a59ff6b0 67 }
c70ad9f7 68
9c5bc7a5 69 public function show_editform() {
a59ff6b0 70 }
4fc649ae 71
9c5bc7a5 72 public function is_cancelled() {
6cc1599e 73 return false;
a59ff6b0 74 }
d4b1d58c 75
9c5bc7a5 76 public function get_data() {
6cc1599e 77 return true;
a59ff6b0
AG
78 }
79
9c5bc7a5 80 public function save_item() {
a59ff6b0 81 global $DB;
4fc649ae 82
9c5bc7a5 83 if (!$this->item) {
a59ff6b0
AG
84 return false;
85 }
4fc649ae 86
9c5bc7a5 87 if (empty($this->item->id)) {
4fc649ae 88 $this->item->id = $DB->insert_record('feedback_item', $this->item);
9c5bc7a5 89 } else {
6cc1599e 90 $DB->update_record('feedback_item', $this->item);
a59ff6b0 91 }
4fc649ae
PS
92
93 return $DB->get_record('feedback_item', array('id'=>$this->item->id));
c70ad9f7 94 }
95
96 //liefert eine Struktur ->name, ->data = array(mit Antworten)
9c5bc7a5 97 public function get_analysed($item, $groupid = false, $courseid = false) {
c70ad9f7 98 return null;
99 }
100
9c5bc7a5 101 public function get_printval($item, $value) {
c70ad9f7 102 return '';
103 }
104
9c5bc7a5 105 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
c70ad9f7 106 return $itemnr;
107 }
108
9c5bc7a5
AG
109 public function excelprint_item(&$worksheet, $row_offset,
110 $xls_formats, $item,
111 $groupid, $courseid = false) {
112 return $row_offset;
c70ad9f7 113 }
4fc649ae
PS
114
115 /**
9d5fbd65
AG
116 * print the item at the edit-page of feedback
117 *
118 * @global object
119 * @param object $item
120 * @return void
121 */
9c5bc7a5 122 public function print_item_preview($item) {
6cc1599e 123 global $DB;
9d5fbd65
AG
124
125 $align = right_to_left() ? 'right' : 'left';
126
9d5fbd65
AG
127 $cmid = 0;
128 $feedbackid = $item->feedback;
9c5bc7a5 129 if ($feedbackid > 0) {
9d5fbd65 130 $feedback = $DB->get_record('feedback', array('id'=>$feedbackid));
9c5bc7a5
AG
131 $cm = get_coursemodule_from_instance("feedback", $feedback->id, $feedback->course);
132 if ($cm) {
9d5fbd65
AG
133 $cmid = $cm->id;
134 }
135 }
c70ad9f7 136
6cc1599e 137 $requiredmark = '<span class="feedback_required_mark">*</span>';
4fc649ae 138
afdb1920
AG
139 //print the question and label
140 echo '<div class="feedback_item_label_'.$align.'">';
141 echo '('.$item->label.') ';
142 echo format_text($item->name.$requiredmark, true, false, false);
143 echo '</div>';
4fc649ae 144
9d5fbd65 145 }
4fc649ae
PS
146
147 /**
9d5fbd65
AG
148 * print the item at the complete-page of feedback
149 *
150 * @global object
151 * @param object $item
152 * @param string $value
153 * @param bool $highlightrequire
154 * @return void
155 */
9c5bc7a5 156 public function print_item_complete($item, $value = '', $highlightrequire = false) {
6cc1599e
AG
157 global $SESSION, $CFG, $DB, $USER;
158 require_once($CFG->libdir.'/recaptchalib.php');
d4b1d58c 159
e372f4c7 160 $align = right_to_left() ? 'right' : 'left';
d4b1d58c 161
c70ad9f7 162 $cmid = 0;
9d5fbd65 163 $feedbackid = $item->feedback;
9c5bc7a5 164 if ($feedbackid > 0) {
9d5fbd65 165 $feedback = $DB->get_record('feedback', array('id'=>$feedbackid));
9c5bc7a5
AG
166 $cm = get_coursemodule_from_instance("feedback", $feedback->id, $feedback->course);
167 if ($cm) {
9d5fbd65 168 $cmid = $cm->id;
c70ad9f7 169 }
170 }
d4b1d58c 171
c70ad9f7 172 //check if an false value even the value is not required
9c5bc7a5 173 if ($highlightrequire AND !$this->check_value($value, $item)) {
c70ad9f7 174 $falsevalue = true;
9c5bc7a5 175 } else {
c70ad9f7 176 $falsevalue = false;
177 }
d4b1d58c 178
9c5bc7a5 179 if ($falsevalue) {
6cc1599e 180 $highlight = 'missingrequire';
9c5bc7a5 181 } else {
c70ad9f7 182 $highlight = '';
183 }
6cc1599e 184 $requiredmark = '<span class="feedback_required_mark">*</span>';
4fc649ae 185
9c5bc7a5
AG
186 if (isset($SESSION->feedback->captchacheck) AND
187 $SESSION->feedback->captchacheck == $USER->sesskey AND
188 $value == $USER->sesskey) {
189
6cc1599e
AG
190 //print the question and label
191 echo '<div class="feedback_item_label_'.$align.'">';
192 echo '('.$item->label.') ';
afdb1920 193 echo format_text($item->name.$requiredmark, true, false, false);
9c5bc7a5
AG
194 $inputname = 'name="'.$item->typ.'_'.$item->id.'"';
195 echo '<input type="hidden" value="'.$USER->sesskey.'" '.$inputname.' />';
afdb1920 196 echo '</div>';
6cc1599e
AG
197 return;
198 }
199
200 $strincorrectpleasetryagain = get_string('incorrectpleasetryagain', 'auth');
201 $strenterthewordsabove = get_string('enterthewordsabove', 'auth');
202 $strenterthenumbersyouhear = get_string('enterthenumbersyouhear', 'auth');
203 $strgetanothercaptcha = get_string('getanothercaptcha', 'auth');
204 $strgetanaudiocaptcha = get_string('getanaudiocaptcha', 'auth');
205 $strgetanimagecaptcha = get_string('getanimagecaptcha', 'auth');
206
207 $recaptureoptions = Array('theme'=>'custom', 'custom_theme_widget'=>'recaptcha_widget');
208 $html = html_writer::script(js_writer::set_variable('RecaptchaOptions', $recaptureoptions));
209 $html .= '
210
211 <div class="'.$highlight.'" id="recaptcha_widget" style="display:none">
212
213 <div id="recaptcha_image"></div>
9c5bc7a5
AG
214 <div class="recaptcha_only_if_incorrect_sol" style="color:red">'.
215 $strincorrectpleasetryagain.
216 '</div>
217 <span class="recaptcha_only_if_image">
218 <label for="recaptcha_response_field">'.$strenterthewordsabove.$requiredmark.'</label>
219 </span>
220 <span class="recaptcha_only_if_audio">
221 <label for="recaptcha_response_field">'.$strenterthenumbersyouhear.'</label>
222 </span>
4fc649ae 223
6cc1599e 224 <input type="text" id="recaptcha_response_field" name="'.$item->typ.'_'.$item->id.'" />
4fc649ae 225
6cc1599e 226 <div><a href="javascript:Recaptcha.reload()">' . $strgetanothercaptcha . '</a></div>
9c5bc7a5
AG
227 <div class="recaptcha_only_if_image">
228 <a href="javascript:Recaptcha.switch_type(\'audio\')">' . $strgetanaudiocaptcha . '</a>
229 </div>
230 <div class="recaptcha_only_if_audio">
231 <a href="javascript:Recaptcha.switch_type(\'image\')">' . $strgetanimagecaptcha . '</a>
232 </div>
6cc1599e
AG
233 </div>';
234 //we have to rename the challengefield
9c5bc7a5
AG
235 if (!empty($CFG->recaptchaprivatekey) AND !empty($CFG->recaptchapublickey)) {
236 $captchahtml = recaptcha_get_html($CFG->recaptchapublickey, null);
237 echo $html.$captchahtml;
238 }
9d5fbd65
AG
239 }
240
4fc649ae 241 /**
9d5fbd65
AG
242 * print the item at the complete-page of feedback
243 *
244 * @global object
245 * @param object $item
246 * @param string $value
247 * @return void
248 */
9c5bc7a5 249 public function print_item_show_value($item, $value = '') {
6cc1599e 250 global $DB;
9d5fbd65
AG
251
252 $align = right_to_left() ? 'right' : 'left';
253
9d5fbd65 254 $cmid = 0;
6cc1599e 255 $feedbackid = $item->feedback;
9c5bc7a5 256 if ($feedbackid > 0) {
6cc1599e 257 $feedback = $DB->get_record('feedback', array('id'=>$feedbackid));
9c5bc7a5 258 if ($cm = get_coursemodule_from_instance("feedback", $feedback->id, $feedback->course)) {
6cc1599e
AG
259 $cmid = $cm->id;
260 }
c70ad9f7 261 }
9d5fbd65 262
6cc1599e 263 $requiredmark = '<span class="feedback_required_mark">*</span>';
4fc649ae 264
afdb1920
AG
265 //print the question and label
266 echo '<div class="feedback_item_label_'.$align.'">';
6cc1599e
AG
267 echo '('.$item->label.') ';
268 echo format_text($item->name.$requiredmark, true, false, false);
afdb1920 269 echo '</div>';
c70ad9f7 270 }
271
9d5fbd65 272
9c5bc7a5 273 public function check_value($value, $item) {
6cc1599e
AG
274 global $SESSION, $CFG, $USER;
275 require_once($CFG->libdir.'/recaptchalib.php');
4fc649ae 276
9c5bc7a5
AG
277 //is recaptcha configured in moodle?
278 if (empty($CFG->recaptchaprivatekey) OR empty($CFG->recaptchapublickey)) {
279 return true;
280 }
6cc1599e 281 $challenge = optional_param('recaptcha_challenge_field', '', PARAM_RAW);
4fc649ae 282
9c5bc7a5 283 if ($value == $USER->sesskey AND $challenge == '') {
6cc1599e
AG
284 return true;
285 }
286 $remoteip = getremoteaddr(null);
287 $response = recaptcha_check_answer($CFG->recaptchaprivatekey, $remoteip, $challenge, $value);
9c5bc7a5 288 if ($response->is_valid) {
6cc1599e 289 $SESSION->feedback->captchacheck = $USER->sesskey;
c70ad9f7 290 return true;
291 }
6cc1599e
AG
292 unset($SESSION->feedback->captchacheck);
293
c70ad9f7 294 return false;
295 }
296
9c5bc7a5 297 public function create_value($data) {
6cc1599e
AG
298 global $USER;
299 return $USER->sesskey;
c70ad9f7 300 }
301
73043833
AG
302 //compares the dbvalue with the dependvalue
303 //dbvalue is value stored in the db
304 //dependvalue is the value to check
9c5bc7a5
AG
305 public function compare_value($item, $dbvalue, $dependvalue) {
306 if ($dbvalue == $dependvalue) {
73043833
AG
307 return true;
308 }
309 return false;
310 }
4fc649ae 311
9c5bc7a5 312 public function get_presentation($data) {
6cc1599e 313 return '';
c70ad9f7 314 }
315
9c5bc7a5
AG
316 public function get_hasvalue() {
317 global $CFG;
318
319 //is recaptcha configured in moodle?
320 if (empty($CFG->recaptchaprivatekey) OR empty($CFG->recaptchapublickey)) {
321 return 0;
322 }
c70ad9f7 323 return 1;
324 }
4fc649ae 325
9c5bc7a5 326 public function can_switch_require() {
6cc1599e
AG
327 return false;
328 }
e7fc2ff4
AG
329
330 public function value_type() {
331 return PARAM_RAW;
332 }
333
d2448b8b 334 public function clean_input_value($value) {
e7fc2ff4
AG
335 return clean_param($value, $this->value_type());
336 }
c70ad9f7 337}