Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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   * Form to edit a users preferred language
  19   *
  20   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   * @package core_user
  23   */
  24  
  25  if (!defined('MOODLE_INTERNAL')) {
  26      die('Direct access to this script is forbidden.');    //  It must be included from a Moodle page.
  27  }
  28  
  29  require_once($CFG->dirroot.'/lib/formslib.php');
  30  require_once($CFG->dirroot.'/user/lib.php');
  31  
  32  /**
  33   * Class user_edit_form.
  34   *
  35   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  36   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class user_edit_language_form extends moodleform {
  39  
  40      /**
  41       * Define the form.
  42       */
  43      public function definition () {
  44          global $CFG, $COURSE, $USER;
  45  
  46          $mform = $this->_form;
  47          $userid = $USER->id;
  48  
  49          if (is_array($this->_customdata)) {
  50              if (array_key_exists('userid', $this->_customdata)) {
  51                  $userid = $this->_customdata['userid'];
  52              }
  53          }
  54  
  55          // Add some extra hidden fields.
  56          $mform->addElement('hidden', 'id');
  57          $mform->setType('id', PARAM_INT);
  58          $mform->addElement('hidden', 'course', $COURSE->id);
  59          $mform->setType('course', PARAM_INT);
  60  
  61          $purpose = user_edit_map_field_purpose($userid, 'lang');
  62          $translations = get_string_manager()->get_list_of_translations();
  63          $mform->addElement('select', 'lang', get_string('preferredlanguage'), $translations, $purpose);
  64          $mform->setDefault('lang', core_user::get_property_default('lang'));
  65  
  66          $this->add_action_buttons(true, get_string('savechanges'));
  67      }
  68  
  69      /**
  70       * Extend the form definition after the data has been parsed.
  71       */
  72      public function definition_after_data() {
  73          global $CFG, $DB, $OUTPUT;
  74  
  75          $mform = $this->_form;
  76  
  77          // If language does not exist, use site default lang.
  78          if ($langsel = $mform->getElementValue('lang')) {
  79              $lang = reset($langsel);
  80              // Check lang exists.
  81              if (!get_string_manager()->translation_exists($lang, false)) {
  82                  $langel =& $mform->getElement('lang');
  83                  $langel->setValue(core_user::get_property_default('lang'));
  84              }
  85          }
  86  
  87      }
  88  
  89  }
  90  
  91