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/>.
19 * recaptcha type form element
21 * Contains HTML class for a recaptcha type element
24 * @copyright 2008 Nicolas Connault <nicolasconnault@gmail.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once('HTML/QuickForm/input.php');
29 require_once('templatable_form_element.php');
32 * recaptcha type form element
34 * HTML class for a recaptcha type element
38 * @copyright 2008 Nicolas Connault <nicolasconnault@gmail.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class MoodleQuickForm_recaptcha
extends HTML_QuickForm_input
implements templatable
{
42 use templatable_form_element
{
43 export_for_template
as export_for_template_base
;
46 /** @var string html for help button, if empty then no help */
52 * @param string $elementName (optional) name of the recaptcha element
53 * @param string $elementLabel (optional) label for recaptcha element
54 * @param mixed $attributes (optional) Either a typical HTML attribute string
55 * or an associative array
57 public function __construct($elementName = null, $elementLabel = null, $attributes = null) {
58 parent
::__construct($elementName, $elementLabel, $attributes);
59 $this->_type
= 'recaptcha';
63 * Old syntax of class constructor. Deprecated in PHP7.
65 * @deprecated since Moodle 3.1
67 public function MoodleQuickForm_recaptcha($elementName = null, $elementLabel = null, $attributes = null) {
68 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
69 self
::__construct($elementName, $elementLabel, $attributes);
73 * Returns the reCAPTCHA element in HTML
75 * @return string The HTML to render
77 public function toHtml() {
79 require_once($CFG->libdir
. '/recaptchalib_v2.php');
81 return recaptcha_get_challenge_html(RECAPTCHA_API_URL
, $CFG->recaptchapublickey
);
85 * get html for help button
87 * @return string html for help button
89 function getHelpButton(){
90 return $this->_helpbutton
;
94 * Checks recaptcha response with Google.
96 * @param string $responsestr
99 public function verify($responsestr) {
101 require_once($CFG->libdir
. '/recaptchalib_v2.php');
103 $response = recaptcha_check_response(RECAPTCHA_VERIFY_URL
, $CFG->recaptchaprivatekey
,
104 getremoteaddr(), $responsestr);
105 if (!$response['isvalid']) {
106 $attributes = $this->getAttributes();
107 $attributes['error_message'] = $response['error'];
108 $this->setAttributes($attributes);
109 return $response['error'];
114 public function export_for_template(renderer_base
$output) {
115 $context = $this->export_for_template_base($output);
116 $context['html'] = $this->toHtml();
121 * Get force LTR option.
125 public function get_force_ltr() {