[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/user/profile/field/checkbox/ -> field.class.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  /**
  18   * Strings for component 'profilefield_checkbox', language 'en', branch 'MOODLE_20_STABLE'
  19   *
  20   * @package   profilefield_checkbox
  21   * @copyright  2008 onwards Shane Elliot {@link http://pukunui.com}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * Class profile_field_checkbox
  27   *
  28   * @copyright  2008 onwards Shane Elliot {@link http://pukunui.com}
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class profile_field_checkbox extends profile_field_base {
  32  
  33      /**
  34       * Constructor method.
  35       * Pulls out the options for the checkbox from the database and sets the
  36       * the corresponding key for the data if it exists
  37       *
  38       * @param int $fieldid
  39       * @param int $userid
  40       */
  41      public function __construct($fieldid=0, $userid=0) {
  42          global $DB;
  43          // First call parent constructor.
  44          parent::__construct($fieldid, $userid);
  45  
  46          if (!empty($this->field)) {
  47              $datafield = $DB->get_field('user_info_data', 'data', array('userid' => $this->userid, 'fieldid' => $this->fieldid));
  48              if ($datafield !== false) {
  49                  $this->data = $datafield;
  50              } else {
  51                  $this->data = $this->field->defaultdata;
  52              }
  53          }
  54      }
  55  
  56      /**
  57       * Old syntax of class constructor. Deprecated in PHP7.
  58       *
  59       * @deprecated since Moodle 3.1
  60       */
  61      public function profile_field_checkbox($fieldid=0, $userid=0) {
  62          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  63          self::__construct($fieldid, $userid);
  64      }
  65  
  66      /**
  67       * Add elements for editing the profile field value.
  68       * @param moodleform $mform
  69       */
  70      public function edit_field_add($mform) {
  71          // Create the form field.
  72          $checkbox = $mform->addElement('advcheckbox', $this->inputname, format_string($this->field->name));
  73          if ($this->data == '1') {
  74              $checkbox->setChecked(true);
  75          }
  76          $mform->setType($this->inputname, PARAM_BOOL);
  77          if ($this->is_required() and !has_capability('moodle/user:update', context_system::instance())) {
  78              $mform->addRule($this->inputname, get_string('required'), 'nonzero', null, 'client');
  79          }
  80      }
  81  
  82      /**
  83       * Display the data for this field
  84       *
  85       * @return string HTML.
  86       */
  87      public function display_data() {
  88          $options = new stdClass();
  89          $options->para = false;
  90          $checked = intval($this->data) === 1 ? 'checked="checked"' : '';
  91          return '<input disabled="disabled" type="checkbox" name="'.$this->inputname.'" '.$checked.' />';
  92      }
  93  
  94  }
  95  
  96  


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