Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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   * Menu profile field definition.
  19   *
  20   * @package    profilefield_menu
  21   * @copyright  2007 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_define_menu
  27   *
  28   * @copyright  2007 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_define_menu extends profile_define_base {
  32  
  33      /**
  34       * Adds elements to the form for creating/editing this type of profile field.
  35       * @param moodleform $form
  36       */
  37      public function define_form_specific($form) {
  38          // Param 1 for menu type contains the options.
  39          $form->addElement('textarea', 'param1', get_string('profilemenuoptions', 'admin'), array('rows' => 6, 'cols' => 40));
  40          $form->setType('param1', PARAM_TEXT);
  41  
  42          // Default data.
  43          $form->addElement('text', 'defaultdata', get_string('profiledefaultdata', 'admin'), 'size="50"');
  44          $form->setType('defaultdata', PARAM_TEXT);
  45      }
  46  
  47      /**
  48       * Validates data for the profile field.
  49       *
  50       * @param array $data
  51       * @param array $files
  52       * @return array
  53       */
  54      public function define_validate_specific($data, $files) {
  55          $err = array();
  56  
  57          $data->param1 = str_replace("\r", '', $data->param1);
  58  
  59          // Check that we have at least 2 options.
  60          if (($options = explode("\n", $data->param1)) === false) {
  61              $err['param1'] = get_string('profilemenunooptions', 'admin');
  62          } else if (count($options) < 2) {
  63              $err['param1'] = get_string('profilemenutoofewoptions', 'admin');
  64          } else if (!empty($data->defaultdata) and !in_array($data->defaultdata, $options)) {
  65              // Check the default data exists in the options.
  66              $err['defaultdata'] = get_string('profilemenudefaultnotinoptions', 'admin');
  67          }
  68          return $err;
  69      }
  70  
  71      /**
  72       * Processes data before it is saved.
  73       * @param array|stdClass $data
  74       * @return array|stdClass
  75       */
  76      public function define_save_preprocess($data) {
  77          $data->param1 = str_replace("\r", '', $data->param1);
  78  
  79          return $data;
  80      }
  81  
  82  }
  83  
  84