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";
23 public function build_editform($item, $feedback, $cm) {
26 $editurl = new moodle_url('/mod/feedback/edit.php', array('id'=>$cm->id));
28 //ther 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());
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());
42 $this->item_form = true; //dummy
44 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
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 = '';
60 public function show_editform() {
63 public function is_cancelled() {
67 public function get_data() {
71 public function save_item() {
78 if (empty($this->item->id)) {
79 $this->item->id = $DB->insert_record('feedback_item', $this->item);
81 $DB->update_record('feedback_item', $this->item);
84 return $DB->get_record('feedback_item', array('id'=>$this->item->id));
87 public function get_printval($item, $value) {
91 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
95 public function excelprint_item(&$worksheet, $row_offset,
97 $groupid, $courseid = false) {
102 * Returns the formatted name of the item for the complete form or response view
104 * @param stdClass $item
105 * @param bool $withpostfix
108 public function get_display_name($item, $withpostfix = true) {
109 return get_string('captcha', 'feedback');
113 * Adds an input element to the complete form
115 * @param stdClass $item
116 * @param mod_feedback_complete_form $form
118 public function complete_form_element($item, $form) {
119 $name = $this->get_display_name($item);
120 $inputname = $item->typ . '_' . $item->id;
122 if ($form->get_mode() != mod_feedback_complete_form::MODE_COMPLETE) {
123 $form->add_form_element($item,
124 ['static', $inputname, $name],
128 $form->add_form_element($item,
129 ['recaptcha', $inputname, $name],
134 // Add recaptcha validation to the form.
135 $form->add_validation_rule(function($values, $files) use ($item, $form) {
136 $elementname = $item->typ . '_' . $item->id;
137 $recaptchaelement = $form->get_form_element($elementname);
138 if (empty($values['recaptcha_response_field'])) {
139 return array($elementname => get_string('required'));
140 } else if (!empty($values['recaptcha_challenge_field'])) {
141 $challengefield = $values['recaptcha_challenge_field'];
142 $responsefield = $values['recaptcha_response_field'];
143 if (true !== ($result = $recaptchaelement->verify($challengefield, $responsefield))) {
144 return array($elementname => $result);
147 return array($elementname => get_string('missingrecaptchachallengefield'));
154 public function create_value($data) {
156 return $USER->sesskey;
159 public function get_hasvalue() {
162 //is recaptcha configured in moodle?
163 if (empty($CFG->recaptchaprivatekey) OR empty($CFG->recaptchapublickey)) {
169 public function can_switch_require() {
174 * Returns the list of actions allowed on this item in the edit mode
176 * @param stdClass $item
177 * @param stdClass $feedback
179 * @return action_menu_link[]
181 public function edit_actions($item, $feedback, $cm) {
182 $actions = parent::edit_actions($item, $feedback, $cm);
183 unset($actions['update']);