|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00017 defined('MOODLE_INTERNAL') OR die('not allowed'); 00018 require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php'); 00019 00020 class feedback_item_captcha extends feedback_item_base { 00021 protected $type = "captcha"; 00022 private $commonparams; 00023 private $item_form = false; 00024 private $item = false; 00025 private $feedback = false; 00026 00027 public function init() { 00028 00029 } 00030 00031 public function build_editform($item, $feedback, $cm) { 00032 global $DB; 00033 00034 $editurl = new moodle_url('/mod/feedback/edit.php', array('id'=>$cm->id)); 00035 00036 //ther are no settings for recaptcha 00037 if (isset($item->id) AND $item->id > 0) { 00038 notice(get_string('there_are_no_settings_for_recaptcha', 'feedback'), $editurl->out()); 00039 exit; 00040 } 00041 00042 //only one recaptcha can be in a feedback 00043 $params = array('feedback' => $feedback->id, 'typ' => $this->type); 00044 if ($DB->record_exists('feedback_item', $params)) { 00045 notice(get_string('only_one_captcha_allowed', 'feedback'), $editurl->out()); 00046 exit; 00047 } 00048 00049 $this->item = $item; 00050 $this->feedback = $feedback; 00051 $this->item_form = true; //dummy 00052 00053 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id)); 00054 00055 $this->item->feedback = $feedback->id; 00056 $this->item->template = 0; 00057 $this->item->name = get_string('captcha', 'feedback'); 00058 $this->item->label = get_string('captcha', 'feedback'); 00059 $this->item->presentation = ''; 00060 $this->item->typ = $this->type; 00061 $this->item->hasvalue = $this->get_hasvalue(); 00062 $this->item->position = $lastposition + 1; 00063 $this->item->required = 1; 00064 $this->item->dependitem = 0; 00065 $this->item->dependvalue = ''; 00066 $this->item->options = ''; 00067 } 00068 00069 public function show_editform() { 00070 } 00071 00072 public function is_cancelled() { 00073 return false; 00074 } 00075 00076 public function get_data() { 00077 return true; 00078 } 00079 00080 public function save_item() { 00081 global $DB; 00082 00083 if (!$this->item) { 00084 return false; 00085 } 00086 00087 if (empty($this->item->id)) { 00088 $this->item->id = $DB->insert_record('feedback_item', $this->item); 00089 } else { 00090 $DB->update_record('feedback_item', $this->item); 00091 } 00092 00093 return $DB->get_record('feedback_item', array('id'=>$this->item->id)); 00094 } 00095 00096 //liefert eine Struktur ->name, ->data = array(mit Antworten) 00097 public function get_analysed($item, $groupid = false, $courseid = false) { 00098 return null; 00099 } 00100 00101 public function get_printval($item, $value) { 00102 return ''; 00103 } 00104 00105 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) { 00106 return $itemnr; 00107 } 00108 00109 public function excelprint_item(&$worksheet, $row_offset, 00110 $xls_formats, $item, 00111 $groupid, $courseid = false) { 00112 return $row_offset; 00113 } 00114 00122 public function print_item_preview($item) { 00123 global $DB; 00124 00125 $align = right_to_left() ? 'right' : 'left'; 00126 00127 $cmid = 0; 00128 $feedbackid = $item->feedback; 00129 if ($feedbackid > 0) { 00130 $feedback = $DB->get_record('feedback', array('id'=>$feedbackid)); 00131 $cm = get_coursemodule_from_instance("feedback", $feedback->id, $feedback->course); 00132 if ($cm) { 00133 $cmid = $cm->id; 00134 } 00135 } 00136 00137 $requiredmark = '<span class="feedback_required_mark">*</span>'; 00138 00139 //print the question and label 00140 echo '<div class="feedback_item_label_'.$align.'">'; 00141 echo '('.$item->label.') '; 00142 echo format_text($item->name.$requiredmark, true, false, false); 00143 echo '</div>'; 00144 00145 } 00146 00156 public function print_item_complete($item, $value = '', $highlightrequire = false) { 00157 global $SESSION, $CFG, $DB, $USER; 00158 require_once($CFG->libdir.'/recaptchalib.php'); 00159 00160 $align = right_to_left() ? 'right' : 'left'; 00161 00162 $cmid = 0; 00163 $feedbackid = $item->feedback; 00164 if ($feedbackid > 0) { 00165 $feedback = $DB->get_record('feedback', array('id'=>$feedbackid)); 00166 $cm = get_coursemodule_from_instance("feedback", $feedback->id, $feedback->course); 00167 if ($cm) { 00168 $cmid = $cm->id; 00169 } 00170 } 00171 00172 //check if an false value even the value is not required 00173 if ($highlightrequire AND !$this->check_value($value, $item)) { 00174 $falsevalue = true; 00175 } else { 00176 $falsevalue = false; 00177 } 00178 00179 if ($falsevalue) { 00180 $highlight = 'missingrequire'; 00181 } else { 00182 $highlight = ''; 00183 } 00184 $requiredmark = '<span class="feedback_required_mark">*</span>'; 00185 00186 if (isset($SESSION->feedback->captchacheck) AND 00187 $SESSION->feedback->captchacheck == $USER->sesskey AND 00188 $value == $USER->sesskey) { 00189 00190 //print the question and label 00191 echo '<div class="feedback_item_label_'.$align.'">'; 00192 echo '('.$item->label.') '; 00193 echo format_text($item->name.$requiredmark, true, false, false); 00194 $inputname = 'name="'.$item->typ.'_'.$item->id.'"'; 00195 echo '<input type="hidden" value="'.$USER->sesskey.'" '.$inputname.' />'; 00196 echo '</div>'; 00197 return; 00198 } 00199 00200 $strincorrectpleasetryagain = get_string('incorrectpleasetryagain', 'auth'); 00201 $strenterthewordsabove = get_string('enterthewordsabove', 'auth'); 00202 $strenterthenumbersyouhear = get_string('enterthenumbersyouhear', 'auth'); 00203 $strgetanothercaptcha = get_string('getanothercaptcha', 'auth'); 00204 $strgetanaudiocaptcha = get_string('getanaudiocaptcha', 'auth'); 00205 $strgetanimagecaptcha = get_string('getanimagecaptcha', 'auth'); 00206 00207 $recaptureoptions = Array('theme'=>'custom', 'custom_theme_widget'=>'recaptcha_widget'); 00208 $html = html_writer::script(js_writer::set_variable('RecaptchaOptions', $recaptureoptions)); 00209 $html .= ' 00210 00211 <div class="'.$highlight.'" id="recaptcha_widget" style="display:none"> 00212 00213 <div id="recaptcha_image"></div> 00214 <div class="recaptcha_only_if_incorrect_sol" style="color:red">'. 00215 $strincorrectpleasetryagain. 00216 '</div> 00217 <span class="recaptcha_only_if_image"> 00218 <label for="recaptcha_response_field">'.$strenterthewordsabove.$requiredmark.'</label> 00219 </span> 00220 <span class="recaptcha_only_if_audio"> 00221 <label for="recaptcha_response_field">'.$strenterthenumbersyouhear.'</label> 00222 </span> 00223 00224 <input type="text" id="recaptcha_response_field" name="'.$item->typ.'_'.$item->id.'" /> 00225 00226 <div><a href="javascript:Recaptcha.reload()">' . $strgetanothercaptcha . '</a></div> 00227 <div class="recaptcha_only_if_image"> 00228 <a href="javascript:Recaptcha.switch_type(\'audio\')">' . $strgetanaudiocaptcha . '</a> 00229 </div> 00230 <div class="recaptcha_only_if_audio"> 00231 <a href="javascript:Recaptcha.switch_type(\'image\')">' . $strgetanimagecaptcha . '</a> 00232 </div> 00233 </div>'; 00234 //we have to rename the challengefield 00235 if (!empty($CFG->recaptchaprivatekey) AND !empty($CFG->recaptchapublickey)) { 00236 $captchahtml = recaptcha_get_html($CFG->recaptchapublickey, null); 00237 echo $html.$captchahtml; 00238 } 00239 } 00240 00249 public function print_item_show_value($item, $value = '') { 00250 global $DB; 00251 00252 $align = right_to_left() ? 'right' : 'left'; 00253 00254 $cmid = 0; 00255 $feedbackid = $item->feedback; 00256 if ($feedbackid > 0) { 00257 $feedback = $DB->get_record('feedback', array('id'=>$feedbackid)); 00258 if ($cm = get_coursemodule_from_instance("feedback", $feedback->id, $feedback->course)) { 00259 $cmid = $cm->id; 00260 } 00261 } 00262 00263 $requiredmark = '<span class="feedback_required_mark">*</span>'; 00264 00265 //print the question and label 00266 echo '<div class="feedback_item_label_'.$align.'">'; 00267 echo '('.$item->label.') '; 00268 echo format_text($item->name.$requiredmark, true, false, false); 00269 echo '</div>'; 00270 } 00271 00272 00273 public function check_value($value, $item) { 00274 global $SESSION, $CFG, $USER; 00275 require_once($CFG->libdir.'/recaptchalib.php'); 00276 00277 //is recaptcha configured in moodle? 00278 if (empty($CFG->recaptchaprivatekey) OR empty($CFG->recaptchapublickey)) { 00279 return true; 00280 } 00281 $challenge = optional_param('recaptcha_challenge_field', '', PARAM_RAW); 00282 00283 if ($value == $USER->sesskey AND $challenge == '') { 00284 return true; 00285 } 00286 $remoteip = getremoteaddr(null); 00287 $response = recaptcha_check_answer($CFG->recaptchaprivatekey, $remoteip, $challenge, $value); 00288 if ($response->is_valid) { 00289 $SESSION->feedback->captchacheck = $USER->sesskey; 00290 return true; 00291 } 00292 unset($SESSION->feedback->captchacheck); 00293 00294 return false; 00295 } 00296 00297 public function create_value($data) { 00298 global $USER; 00299 return $USER->sesskey; 00300 } 00301 00302 //compares the dbvalue with the dependvalue 00303 //dbvalue is value stored in the db 00304 //dependvalue is the value to check 00305 public function compare_value($item, $dbvalue, $dependvalue) { 00306 if ($dbvalue == $dependvalue) { 00307 return true; 00308 } 00309 return false; 00310 } 00311 00312 public function get_presentation($data) { 00313 return ''; 00314 } 00315 00316 public function get_hasvalue() { 00317 global $CFG; 00318 00319 //is recaptcha configured in moodle? 00320 if (empty($CFG->recaptchaprivatekey) OR empty($CFG->recaptchapublickey)) { 00321 return 0; 00322 } 00323 return 1; 00324 } 00325 00326 public function can_switch_require() { 00327 return false; 00328 } 00329 }