Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   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   * Textarea profile field define.
  19   *
  20   * @package   profilefield_social
  21   * @copyright 2020 Bas Brands <bas@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * Class profile_define_social.
  27   *
  28   * @copyright  2020 Bas Brands <bas@moodle.com>
  29   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class profile_define_social extends profile_define_base {
  32  
  33      /**
  34       * Prints out the form snippet for the part of creating or editing a profile field common to all data types.
  35       *
  36       * @param moodleform $form instance of the moodleform class
  37       */
  38      public function define_form_common(&$form) {
  39          $availablenetworks = profilefield_social\helper::get_networks();
  40          $networks = array_merge(['0' => get_string('select')], $availablenetworks);
  41  
  42          $form->addElement('hidden', 'defaultdata', '');
  43          $form->setType('defaultdata', PARAM_TEXT);
  44  
  45          $form->addElement('select', 'param1', get_string('networktype', 'profilefield_social'), $networks);
  46          $form->addRule('param1', get_string('required'), 'required', null, 'client');
  47          $form->setType('param1', PARAM_TEXT);
  48  
  49          parent::define_form_common($form);
  50          $form->removeElement('name');
  51      }
  52  
  53      /**
  54       * Alter form based on submitted or existing data.
  55       *
  56       * @param moodleform $form
  57       */
  58      public function define_after_data(&$form) {
  59          if (isset($form->_defaultValues['name'])) {
  60              $form->setDefault('param1', $form->_defaultValues['name']);
  61          }
  62      }
  63  
  64      /**
  65       * Validate the data from the add/edit profile field form that is common to all data types.
  66       *
  67       * Generally this method should not be overwritten by child classes.
  68       *
  69       * @param stdClass|array $data from the add/edit profile field form
  70       * @param array $files
  71       * @return  array    associative array of error messages
  72       */
  73      public function define_validate_common($data, $files) {
  74          $err = parent::define_validate_common($data, $files);
  75  
  76          $networks = profilefield_social\helper::get_networks();
  77          if (empty($data->param1) || !array_key_exists($data->param1, $networks)) {
  78              $err['param1'] = get_string('invalidnetwork', 'profilefield_social');
  79          }
  80          return $err;
  81      }
  82  
  83      /**
  84       * Preprocess data from the add/edit profile field form before it is saved.
  85       *
  86       * This method is a hook for the child classes to overwrite.
  87       *
  88       * @param array|stdClass $data from the add/edit profile field form
  89       * @return array|stdClass processed data object
  90       */
  91      public function define_save_preprocess($data) {
  92          $data->name = $data->param1;
  93          return $data;
  94      }
  95  }