Merge branch 'MDL-66115_37' of https://github.com/dvdcastro/moodle into MOODLE_37_STABLE
[moodle.git] / user / language_form.php
blob50d091130e27c3150808366957d43b137f69c23e
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.
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/>.
17 /**
18 * Form to edit a users preferred language
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
25 if (!defined('MOODLE_INTERNAL')) {
26 die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
29 require_once($CFG->dirroot.'/lib/formslib.php');
30 require_once($CFG->dirroot.'/user/lib.php');
32 /**
33 * Class user_edit_form.
35 * @copyright 1999 Martin Dougiamas http://dougiamas.com
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class user_edit_language_form extends moodleform {
40 /**
41 * Define the form.
43 public function definition () {
44 global $CFG, $COURSE, $USER;
46 $mform = $this->_form;
47 $userid = $USER->id;
49 if (is_array($this->_customdata)) {
50 if (array_key_exists('userid', $this->_customdata)) {
51 $userid = $this->_customdata['userid'];
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);
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'));
66 $this->add_action_buttons(true, get_string('savechanges'));
69 /**
70 * Extend the form definition after the data has been parsed.
72 public function definition_after_data() {
73 global $CFG, $DB, $OUTPUT;
75 $mform = $this->_form;
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'));