2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 defined('MOODLE_INTERNAL') OR die('not allowed');
18 require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
20 class feedback_item_captcha extends feedback_item_base {
21 protected $type = "captcha";
22 private $commonparams;
23 private $item_form = false;
24 private $item = false;
25 private $feedback = false;
27 public function init() {
31 public function build_editform($item, $feedback, $cm) {
34 $editurl = new moodle_url('/mod/feedback/edit.php', array('id'=>$cm->id));
36 //ther are no settings for recaptcha
37 if (isset($item->id) AND $item->id > 0) {
38 notice(get_string('there_are_no_settings_for_recaptcha', 'feedback'), $editurl->out());
42 //only one recaptcha can be in a feedback
43 $params = array('feedback' => $feedback->id, 'typ' => $this->type);
44 if ($DB->record_exists('feedback_item', $params)) {
45 notice(get_string('only_one_captcha_allowed', 'feedback'), $editurl->out());
50 $this->feedback = $feedback;
51 $this->item_form = true; //dummy
53 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
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 = '';
69 public function show_editform() {
72 public function is_cancelled() {
76 public function get_data() {
80 public function save_item() {
87 if (empty($this->item->id)) {
88 $this->item->id = $DB->insert_record('feedback_item', $this->item);
90 $DB->update_record('feedback_item', $this->item);
93 return $DB->get_record('feedback_item', array('id'=>$this->item->id));
96 //liefert eine Struktur ->name, ->data = array(mit Antworten)
97 public function get_analysed($item, $groupid = false, $courseid = false) {
101 public function get_printval($item, $value) {
105 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
109 public function excelprint_item(&$worksheet, $row_offset,
111 $groupid, $courseid = false) {
116 * print the item at the edit-page of feedback
119 * @param object $item
122 public function print_item_preview($item) {
125 $align = right_to_left() ? 'right' : 'left';
128 $feedbackid = $item->feedback;
129 if ($feedbackid > 0) {
130 $feedback = $DB->get_record('feedback', array('id'=>$feedbackid));
131 $cm = get_coursemodule_from_instance("feedback", $feedback->id, $feedback->course);
137 $requiredmark = '<span class="feedback_required_mark">*</span>';
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);
148 * print the item at the complete-page of feedback
151 * @param object $item
152 * @param string $value
153 * @param bool $highlightrequire
156 public function print_item_complete($item, $value = '', $highlightrequire = false) {
157 global $SESSION, $CFG, $DB, $USER;
158 require_once($CFG->libdir.'/recaptchalib.php');
160 $align = right_to_left() ? 'right' : 'left';
163 $feedbackid = $item->feedback;
164 if ($feedbackid > 0) {
165 $feedback = $DB->get_record('feedback', array('id'=>$feedbackid));
166 $cm = get_coursemodule_from_instance("feedback", $feedback->id, $feedback->course);
172 //check if an false value even the value is not required
173 if ($highlightrequire AND !$this->check_value($value, $item)) {
180 $highlight = 'missingrequire';
184 $requiredmark = '<span class="feedback_required_mark">*</span>';
186 if (isset($SESSION->feedback->captchacheck) AND
187 $SESSION->feedback->captchacheck == $USER->sesskey AND
188 $value == $USER->sesskey) {
190 //print the question and label
191 echo '<div class="feedback_item_label_'.$align.'">';
192 echo '('.$item->label.') ';
193 echo format_text($item->name.$requiredmark, true, false, false);
194 $inputname = 'name="'.$item->typ.'_'.$item->id.'"';
195 echo '<input type="hidden" value="'.$USER->sesskey.'" '.$inputname.' />';
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');
207 $recaptureoptions = Array('theme'=>'custom', 'custom_theme_widget'=>'recaptcha_widget');
208 $html = html_writer::script(js_writer::set_variable('RecaptchaOptions', $recaptureoptions));
211 <div class="'.$highlight.'" id="recaptcha_widget" style="display:none">
213 <div id="recaptcha_image"></div>
214 <div class="recaptcha_only_if_incorrect_sol" style="color:red">'.
215 $strincorrectpleasetryagain.
217 <span class="recaptcha_only_if_image">
218 <label for="recaptcha_response_field">'.$strenterthewordsabove.$requiredmark.'</label>
220 <span class="recaptcha_only_if_audio">
221 <label for="recaptcha_response_field">'.$strenterthenumbersyouhear.'</label>
224 <input type="text" id="recaptcha_response_field" name="'.$item->typ.'_'.$item->id.'" />
226 <div><a href="javascript:Recaptcha.reload()">' . $strgetanothercaptcha . '</a></div>
227 <div class="recaptcha_only_if_image">
228 <a href="javascript:Recaptcha.switch_type(\'audio\')">' . $strgetanaudiocaptcha . '</a>
230 <div class="recaptcha_only_if_audio">
231 <a href="javascript:Recaptcha.switch_type(\'image\')">' . $strgetanimagecaptcha . '</a>
234 //we have to rename the challengefield
235 if (!empty($CFG->recaptchaprivatekey) AND !empty($CFG->recaptchapublickey)) {
236 $captchahtml = recaptcha_get_html($CFG->recaptchapublickey, null);
237 echo $html.$captchahtml;
242 * print the item at the complete-page of feedback
245 * @param object $item
246 * @param string $value
249 public function print_item_show_value($item, $value = '') {
252 $align = right_to_left() ? 'right' : 'left';
255 $feedbackid = $item->feedback;
256 if ($feedbackid > 0) {
257 $feedback = $DB->get_record('feedback', array('id'=>$feedbackid));
258 if ($cm = get_coursemodule_from_instance("feedback", $feedback->id, $feedback->course)) {
263 $requiredmark = '<span class="feedback_required_mark">*</span>';
265 //print the question and label
266 echo '<div class="feedback_item_label_'.$align.'">';
267 echo '('.$item->label.') ';
268 echo format_text($item->name.$requiredmark, true, false, false);
273 public function check_value($value, $item) {
274 global $SESSION, $CFG, $USER;
275 require_once($CFG->libdir.'/recaptchalib.php');
277 //is recaptcha configured in moodle?
278 if (empty($CFG->recaptchaprivatekey) OR empty($CFG->recaptchapublickey)) {
281 $challenge = optional_param('recaptcha_challenge_field', '', PARAM_RAW);
283 if ($value == $USER->sesskey AND $challenge == '') {
286 $remoteip = getremoteaddr(null);
287 $response = recaptcha_check_answer($CFG->recaptchaprivatekey, $remoteip, $challenge, $value);
288 if ($response->is_valid) {
289 $SESSION->feedback->captchacheck = $USER->sesskey;
292 unset($SESSION->feedback->captchacheck);
297 public function create_value($data) {
299 return $USER->sesskey;
302 //compares the dbvalue with the dependvalue
303 //dbvalue is value stored in the db
304 //dependvalue is the value to check
305 public function compare_value($item, $dbvalue, $dependvalue) {
306 if ($dbvalue == $dependvalue) {
312 public function get_presentation($data) {
316 public function get_hasvalue() {
319 //is recaptcha configured in moodle?
320 if (empty($CFG->recaptchaprivatekey) OR empty($CFG->recaptchapublickey)) {
326 public function can_switch_require() {
330 public function value_type() {
334 public function clean_input_value($value) {
335 return clean_param($value, $this->value_type());