[ Index ]

PHP Cross Reference of Unnamed Project

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  
  23      public function build_editform($item, $feedback, $cm) {
  24          global $DB;
  25  
  26          $editurl = new moodle_url('/mod/feedback/edit.php', array('id'=>$cm->id));
  27  
  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());
  31              exit;
  32          }
  33  
  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());
  38              exit;
  39          }
  40  
  41          $this->item = $item;
  42          $this->item_form = true; //dummy
  43  
  44          $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
  45  
  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 = '';
  58      }
  59  
  60      public function show_editform() {
  61      }
  62  
  63      public function is_cancelled() {
  64          return false;
  65      }
  66  
  67      public function get_data() {
  68          return true;
  69      }
  70  
  71      public function save_item() {
  72          global $DB;
  73  
  74          if (!$this->item) {
  75              return false;
  76          }
  77  
  78          if (empty($this->item->id)) {
  79              $this->item->id = $DB->insert_record('feedback_item', $this->item);
  80          } else {
  81              $DB->update_record('feedback_item', $this->item);
  82          }
  83  
  84          return $DB->get_record('feedback_item', array('id'=>$this->item->id));
  85      }
  86  
  87      public function get_printval($item, $value) {
  88          return '';
  89      }
  90  
  91      public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
  92          return $itemnr;
  93      }
  94  
  95      public function excelprint_item(&$worksheet, $row_offset,
  96                               $xls_formats, $item,
  97                               $groupid, $courseid = false) {
  98          return $row_offset;
  99      }
 100  
 101      /**
 102       * Returns the formatted name of the item for the complete form or response view
 103       *
 104       * @param stdClass $item
 105       * @param bool $withpostfix
 106       * @return string
 107       */
 108      public function get_display_name($item, $withpostfix = true) {
 109          return get_string('captcha', 'feedback');
 110      }
 111  
 112      /**
 113       * Adds an input element to the complete form
 114       *
 115       * @param stdClass $item
 116       * @param mod_feedback_complete_form $form
 117       */
 118      public function complete_form_element($item, $form) {
 119          $name = $this->get_display_name($item);
 120          $inputname = $item->typ . '_' . $item->id;
 121  
 122          if ($form->get_mode() != mod_feedback_complete_form::MODE_COMPLETE) {
 123              $form->add_form_element($item,
 124                      ['static', $inputname, $name],
 125                      false,
 126                      false);
 127          } else {
 128              $form->add_form_element($item,
 129                      ['recaptcha', $inputname, $name],
 130                      false,
 131                      false);
 132          }
 133  
 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);
 145                  }
 146              } else {
 147                  return array($elementname => get_string('missingrecaptchachallengefield'));
 148              }
 149              return true;
 150          });
 151  
 152      }
 153  
 154      public function create_value($data) {
 155          global $USER;
 156          return $USER->sesskey;
 157      }
 158  
 159      public function get_hasvalue() {
 160          global $CFG;
 161  
 162          //is recaptcha configured in moodle?
 163          if (empty($CFG->recaptchaprivatekey) OR empty($CFG->recaptchapublickey)) {
 164              return 0;
 165          }
 166          return 1;
 167      }
 168  
 169      public function can_switch_require() {
 170          return false;
 171      }
 172  
 173      /**
 174       * Returns the list of actions allowed on this item in the edit mode
 175       *
 176       * @param stdClass $item
 177       * @param stdClass $feedback
 178       * @param cm_info $cm
 179       * @return action_menu_link[]
 180       */
 181      public function edit_actions($item, $feedback, $cm) {
 182          $actions = parent::edit_actions($item, $feedback, $cm);
 183          unset($actions['update']);
 184          return $actions;
 185      }
 186  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1