[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/feedback/item/captcha/ -> lib.php (source)

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


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1