first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+157
View File
@@ -0,0 +1,157 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Form to edit a users preferred language
*
* @copyright 2015 Shamim Rezaie http://foodle.org
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package core_user
*/
namespace core_user\form;
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
}
require_once($CFG->dirroot.'/lib/formslib.php');
/**
* Class user_edit_calendar_form.
*
* @copyright 2015 Shamim Rezaie http://foodle.org
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class calendar_form extends \moodleform {
/**
* Define the form.
*/
public function definition() {
global $CFG, $USER;
$mform = $this->_form;
$userid = $USER->id;
if (is_array($this->_customdata)) {
if (array_key_exists('userid', $this->_customdata)) {
$userid = $this->_customdata['userid'];
}
}
// Add some extra hidden fields.
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
// We do not want to show this option unless there is more than one calendar type to display.
if (count(\core_calendar\type_factory::get_list_of_calendar_types()) > 1) {
$calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
$mform->addElement('select', 'calendartype', get_string('preferredcalendar', 'calendar'), $calendartypes);
$mform->setType('calendartype', PARAM_ALPHANUM);
$mform->setDefault('calendartype', $CFG->calendartype);
} else {
$mform->addElement('hidden', 'calendartype', $CFG->calendartype);
$mform->setType('calendartype', PARAM_ALPHANUM);
}
// Date / Time settings.
$options = array(
'0' => get_string('default', 'calendar'),
CALENDAR_TF_12 => get_string('timeformat_12', 'calendar'),
CALENDAR_TF_24 => get_string('timeformat_24', 'calendar')
);
$mform->addElement('select', 'timeformat', get_string('pref_timeformat', 'calendar'), $options);
$mform->addHelpButton('timeformat', 'pref_timeformat', 'calendar');
// First day of week.
$options = array(
0 => get_string('sunday', 'calendar'),
1 => get_string('monday', 'calendar'),
2 => get_string('tuesday', 'calendar'),
3 => get_string('wednesday', 'calendar'),
4 => get_string('thursday', 'calendar'),
5 => get_string('friday', 'calendar'),
6 => get_string('saturday', 'calendar')
);
$mform->addElement('select', 'startwday', get_string('pref_startwday', 'calendar'), $options);
$mform->addHelpButton('startwday', 'pref_startwday', 'calendar');
// Maximum events to display.
$options = array();
for ($i = 1; $i <= 20; $i++) {
$options[$i] = $i;
}
$mform->addElement('select', 'maxevents', get_string('pref_maxevents', 'calendar'), $options);
$mform->addHelpButton('maxevents', 'pref_maxevents', 'calendar');
// Calendar lookahead.
$options = array(365 => new \lang_string('numyear', '', 1),
270 => get_string('nummonths', '', 9),
180 => get_string('nummonths', '', 6),
150 => get_string('nummonths', '', 5),
120 => get_string('nummonths', '', 4),
90 => get_string('nummonths', '', 3),
60 => get_string('nummonths', '', 2),
30 => get_string('nummonth', '', 1),
21 => get_string('numweeks', '', 3),
14 => get_string('numweeks', '', 2),
7 => get_string('numweek', '', 1),
6 => get_string('numdays', '', 6),
5 => get_string('numdays', '', 5),
4 => get_string('numdays', '', 4),
3 => get_string('numdays', '', 3),
2 => get_string('numdays', '', 2),
1 => get_string('numday', '', 1));
$mform->addElement('select', 'lookahead', get_string('pref_lookahead', 'calendar'), $options);
$mform->addHelpButton('lookahead', 'pref_lookahead', 'calendar');
// Remember event filtering.
$options = array(
0 => get_string('no'),
1 => get_string('yes')
);
$mform->addElement('select', 'persistflt', get_string('pref_persistflt', 'calendar'), $options);
$mform->addHelpButton('persistflt', 'pref_persistflt', 'calendar');
$this->add_action_buttons(true, get_string('savechanges'));
}
/**
* Extend the form definition after the data has been parsed.
*/
public function definition_after_data() {
global $CFG;
$mform = $this->_form;
// If calendar type does not exist, use site default calendar type.
if ($calendarselected = $mform->getElementValue('calendartype')) {
if (is_array($calendarselected)) {
// There are multiple calendar types available.
$calendar = reset($calendarselected);
} else {
// There is only one calendar type available.
$calendar = $calendarselected;
}
// Check calendar type exists.
if (!array_key_exists($calendar, \core_calendar\type_factory::get_list_of_calendar_types())) {
$calendartypeel = $mform->getElement('calendartype');
$calendartypeel->setValue($CFG->calendartype);
}
}
}
}
@@ -0,0 +1,109 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_user\form;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot.'/lib/formslib.php');
/**
* Contact site support form.
*
* @package core_user
* @copyright 2022 Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class contactsitesupport_form extends \moodleform {
/**
* Define the contact site support form.
*/
public function definition(): void {
global $CFG;
$mform = $this->_form;
$user = $this->_customdata;
$strrequired = get_string('required');
// Name.
$mform->addElement('text', 'name', get_string('name'));
$mform->addRule('name', $strrequired, 'required', null, 'client');
$mform->setType('name', PARAM_TEXT);
// Email.
$mform->addElement('text', 'email', get_string('email'));
$mform->addRule('email', get_string('missingemail'), 'required', null, 'client');
$mform->setType('email', PARAM_EMAIL);
// Subject.
$mform->addElement('text', 'subject', get_string('subject'));
$mform->addRule('subject', $strrequired, 'required', null, 'client');
$mform->setType('subject', PARAM_TEXT);
// Message.
$mform->addElement('textarea', 'message', get_string('message'));
$mform->addRule('message', $strrequired, 'required', null, 'client');
$mform->setType('message', PARAM_TEXT);
// If the user is logged in set name and email fields to the current user info.
if (isloggedin() && !isguestuser()) {
$mform->setDefault('name', fullname($user));
$mform->hardFreeze('name');
$mform->setDefault('email', $user->email);
$mform->hardFreeze('email');
}
if (!empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey)) {
$mform->addElement('recaptcha', 'recaptcha_element', get_string('security_question', 'auth'));
$mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth');
$mform->closeHeaderBefore('recaptcha_element');
}
$this->add_action_buttons(true, get_string('submit'));
}
/**
* Validate user supplied data on the contact site support form.
*
* @param array $data array of ("fieldname"=>value) of submitted data
* @param array $files array of uploaded files "element_name"=>tmp_file_path
* @return array of "element_name"=>"error_description" if there are errors,
* or an empty array if everything is OK (true allowed for backwards compatibility too).
*/
public function validation($data, $files): array {
$errors = parent::validation($data, $files);
if (!validate_email($data['email'])) {
$errors['email'] = get_string('invalidemail');
}
if ($this->_form->elementExists('recaptcha_element')) {
$recaptchaelement = $this->_form->getElement('recaptcha_element');
if (!empty($this->_form->_submitValues['g-recaptcha-response'])) {
$response = $this->_form->_submitValues['g-recaptcha-response'];
if (!$recaptchaelement->verify($response)) {
$errors['recaptcha_element'] = get_string('incorrectpleasetryagain', 'auth');
}
} else {
$errors['recaptcha_element'] = get_string('missingrecaptchachallengefield');
}
}
return $errors;
}
}
@@ -0,0 +1,53 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_user\form;
use \core_contentbank\content;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot.'/lib/formslib.php');
/**
* Form to edit a user's preferences concerning the content bank.
*
* @package core_user
* @copyright 2020 François Moreau
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class contentbank_user_preferences_form extends \moodleform {
/**
* Define the form.
*/
public function definition() {
global $CFG, $USER;
$mform = $this->_form;
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$options = [
content::VISIBILITY_PUBLIC => get_string('visibilitychoicepublic', 'core_contentbank'),
content::VISIBILITY_UNLISTED => get_string('visibilitychoiceunlisted', 'core_contentbank')
];
$mform->addElement('select', 'contentvisibility', get_string('visibilitypref', 'core_contentbank'), $options);
$mform->addHelpButton('contentvisibility', 'visibilitypref', 'core_contentbank');
$this->add_action_buttons(true, get_string('savechanges'));
}
}
@@ -0,0 +1,64 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Form to allow user to set their default home page
*
* @package core_user
* @copyright 2019 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_user\form;
use lang_string;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot . '/lib/formslib.php');
/**
* Form class
*
* @copyright 2019 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class defaulthomepage_form extends \moodleform {
/**
* Define the form.
*/
public function definition() {
global $CFG;
$mform = $this->_form;
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$options = [HOMEPAGE_SITE => new lang_string('home')];
if (!empty($CFG->enabledashboard)) {
$options[HOMEPAGE_MY] = new lang_string('mymoodle', 'admin');
}
$options[HOMEPAGE_MYCOURSES] = new lang_string('mycourses', 'admin');
$mform->addElement('select', 'defaulthomepage', get_string('defaulthomepageuser'), $options);
$mform->addHelpButton('defaulthomepage', 'defaulthomepageuser');
$mform->setDefault('defaulthomepage', get_default_home_page());
$this->add_action_buttons(true, get_string('savechanges'));
}
}
+192
View File
@@ -0,0 +1,192 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_user\form;
use html_writer;
use moodle_url;
/**
* Manage user private area files form
*
* @package core_user
* @copyright 2010 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class private_files extends \core_form\dynamic_form {
/**
* Add elements to this form.
*/
public function definition() {
global $OUTPUT;
$mform = $this->_form;
$options = $this->get_options();
// Show file area space usage.
$maxareabytes = $options['areamaxbytes'];
if ($maxareabytes != FILE_AREA_MAX_BYTES_UNLIMITED) {
$fileareainfo = file_get_file_area_info($this->get_context_for_dynamic_submission()->id, 'user', 'private');
// Display message only if we have files.
if ($fileareainfo['filecount']) {
$a = (object) [
'used' => display_size($fileareainfo['filesize_without_references']),
'total' => display_size($maxareabytes, 0)
];
$quotamsg = get_string('quotausage', 'moodle', $a);
$notification = new \core\output\notification($quotamsg, \core\output\notification::NOTIFY_INFO);
$mform->addElement('static', 'areabytes', '', $OUTPUT->render($notification));
}
}
$mform->addElement('filemanager', 'files_filemanager', get_string('files'), null, $options);
if ($link = $this->get_emaillink()) {
$emaillink = html_writer::link(new moodle_url('mailto:' . $link), $link);
$mform->addElement('static', 'emailaddress', '',
get_string('emailtoprivatefiles', 'moodle', $emaillink));
}
$mform->setType('returnurl', PARAM_LOCALURL);
// The 'nosubmit' param (default false) determines whether we should show the standard form action buttons (save/cancel).
// This value is set when the form is displayed within a modal, which adds the action buttons itself.
if (!$this->optional_param('nosubmit', false, PARAM_BOOL)) {
$this->add_action_buttons();
}
}
/**
* Validate incoming data.
*
* @param array $data
* @param array $files
* @return array
*/
public function validation($data, $files) {
$errors = array();
$draftitemid = $data['files_filemanager'];
$options = $this->get_options();
if (file_is_draft_area_limit_reached($draftitemid, $options['areamaxbytes'])) {
$errors['files_filemanager'] = get_string('userquotalimit', 'error');
}
return $errors;
}
/**
* Link to email private files
*
* @return string|null
* @throws \coding_exception
*/
protected function get_emaillink() {
global $USER;
// Attempt to generate an inbound message address to support e-mail to private files.
$generator = new \core\message\inbound\address_manager();
$generator->set_handler('\core\message\inbound\private_files_handler');
$generator->set_data(-1);
return $generator->generate($USER->id);
}
/**
* Check if current user has access to this form, otherwise throw exception
*
* Sometimes permission check may depend on the action and/or id of the entity.
* If necessary, form data is available in $this->_ajaxformdata or
* by calling $this->optional_param()
*/
protected function check_access_for_dynamic_submission(): void {
require_capability('moodle/user:manageownfiles', $this->get_context_for_dynamic_submission());
}
/**
* Returns form context
*
* If context depends on the form data, it is available in $this->_ajaxformdata or
* by calling $this->optional_param()
*
* @return \context
*/
protected function get_context_for_dynamic_submission(): \context {
global $USER;
return \context_user::instance($USER->id);
}
/**
* File upload options
*
* @return array
* @throws \coding_exception
*/
protected function get_options(): array {
global $CFG;
$maxbytes = $CFG->userquota;
$maxareabytes = $CFG->userquota;
if (has_capability('moodle/user:ignoreuserquota', $this->get_context_for_dynamic_submission())) {
$maxbytes = USER_CAN_IGNORE_FILE_SIZE_LIMITS;
$maxareabytes = FILE_AREA_MAX_BYTES_UNLIMITED;
}
return ['subdirs' => 1, 'maxbytes' => $maxbytes, 'maxfiles' => -1, 'accepted_types' => '*',
'areamaxbytes' => $maxareabytes];
}
/**
* Process the form submission, used if form was submitted via AJAX
*
* This method can return scalar values or arrays that can be json-encoded, they will be passed to the caller JS.
*
* Submission data can be accessed as: $this->get_data()
*
* @return mixed
*/
public function process_dynamic_submission() {
file_postupdate_standard_filemanager($this->get_data(), 'files',
$this->get_options(), $this->get_context_for_dynamic_submission(), 'user', 'private', 0);
return null;
}
/**
* Load in existing data as form defaults
*
* Can be overridden to retrieve existing values from db by entity id and also
* to preprocess editor and filemanager elements
*
* Example:
* $this->set_data(get_entity($this->_ajaxformdata['id']));
*/
public function set_data_for_dynamic_submission(): void {
$data = new \stdClass();
file_prepare_standard_filemanager($data, 'files', $this->get_options(),
$this->get_context_for_dynamic_submission(), 'user', 'private', 0);
$this->set_data($data);
}
/**
* Returns url to set in $PAGE->set_url() when form is being rendered or submitted via AJAX
*
* This is used in the form elements sensitive to the page url, such as Atto autosave in 'editor'
*
* If the form has arguments (such as 'id' of the element being edited), the URL should
* also have respective argument.
*
* @return \moodle_url
*/
protected function get_page_url_for_dynamic_submission(): \moodle_url {
return new moodle_url('/user/files.php');
}
}
+125
View File
@@ -0,0 +1,125 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_user\form;
use context;
use core_form\dynamic_form;
use moodle_url;
/**
* Modal form to edit profile category
*
* @package core_user
* @copyright 2021 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class profile_category_form extends dynamic_form {
/**
* Form definition
*/
protected function definition() {
$mform = $this->_form;
$strrequired = get_string('required');
// Add some extra hidden fields.
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'action', 'editcategory');
$mform->setType('action', PARAM_ALPHANUMEXT);
$mform->addElement('text', 'name', get_string('profilecategoryname', 'admin'), 'maxlength="255" size="30"');
$mform->setType('name', PARAM_TEXT);
$mform->addRule('name', $strrequired, 'required', null, 'client');
}
/**
* Perform some moodle validation.
*
* @param array $data
* @param array $files
* @return array
*/
public function validation($data, $files) {
global $DB;
$errors = parent::validation($data, $files);
$duplicate = $DB->get_field('user_info_category', 'id', ['name' => $data['name']]);
// Check the name is unique.
if (!empty($data['id'])) { // We are editing an existing record.
$olddata = $DB->get_record('user_info_category', ['id' => $data['id']]);
// Name has changed, new name in use, new name in use by another record.
$dupfound = (($olddata->name !== $data['name']) && $duplicate && ($data['id'] != $duplicate));
} else { // New profile category.
$dupfound = $duplicate;
}
if ($dupfound ) {
$errors['name'] = get_string('profilecategorynamenotunique', 'admin');
}
return $errors;
}
/**
* Returns context where this form is used
*
* @return context
*/
protected function get_context_for_dynamic_submission(): context {
return \context_system::instance();
}
/**
* Checks if current user has access to this form, otherwise throws exception
*/
protected function check_access_for_dynamic_submission(): void {
require_capability('moodle/site:config', $this->get_context_for_dynamic_submission());
}
/**
* Process the form submission, used if form was submitted via AJAX
*/
public function process_dynamic_submission() {
global $CFG;
require_once($CFG->dirroot.'/user/profile/definelib.php');
profile_save_category($this->get_data());
}
/**
* Load in existing data as form defaults
*/
public function set_data_for_dynamic_submission(): void {
global $DB;
if ($id = $this->optional_param('id', 0, PARAM_INT)) {
$this->set_data($DB->get_record('user_info_category', ['id' => $id], '*', MUST_EXIST));
}
}
/**
* Returns url to set in $PAGE->set_url() when form is being rendered or submitted via AJAX
*
* @return moodle_url
*/
protected function get_page_url_for_dynamic_submission(): moodle_url {
$id = $this->optional_param('id', 0, PARAM_INT);
return new moodle_url('/user/profile/index.php',
['action' => 'editcategory', 'id' => $id]);
}
}
+183
View File
@@ -0,0 +1,183 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_user\form;
use context;
use core_form\dynamic_form;
use moodle_url;
use profile_define_base;
/**
* Class field_form used for profile fields.
*
* @package core_user
* @copyright 2007 onwards Shane Elliot {@link http://pukunui.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class profile_field_form extends dynamic_form {
/** @var profile_define_base $field */
public $field;
/** @var \stdClass */
protected $fieldrecord;
/**
* Define the form
*/
public function definition() {
global $CFG;
require_once($CFG->dirroot.'/user/profile/definelib.php');
$mform = $this->_form;
// Everything else is dependant on the data type.
$datatype = $this->get_field_record()->datatype;
require_once($CFG->dirroot.'/user/profile/field/'.$datatype.'/define.class.php');
$newfield = 'profile_define_'.$datatype;
$this->field = new $newfield();
// Add some extra hidden fields.
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'action', 'editfield');
$mform->setType('action', PARAM_ALPHANUMEXT);
$mform->addElement('hidden', 'datatype', $datatype);
$mform->setType('datatype', PARAM_ALPHA);
$this->field->define_form($mform);
}
/**
* Alter definition based on existing or submitted data
*/
public function definition_after_data() {
$mform = $this->_form;
$this->field->define_after_data($mform);
}
/**
* Perform some moodle validation.
* @param array $data
* @param array $files
* @return array
*/
public function validation($data, $files) {
return $this->field->define_validate($data, $files);
}
/**
* Returns the defined editors for the field.
* @return array
*/
public function editors(): array {
$editors = $this->field->define_editors();
return is_array($editors) ? $editors : [];
}
/**
* Returns context where this form is used
*
* @return context
*/
protected function get_context_for_dynamic_submission(): context {
return \context_system::instance();
}
/**
* Checks if current user has access to this form, otherwise throws exception
*/
protected function check_access_for_dynamic_submission(): void {
require_capability('moodle/site:config', $this->get_context_for_dynamic_submission());
}
/**
* Process the form submission, used if form was submitted via AJAX
*/
public function process_dynamic_submission() {
global $CFG;
require_once($CFG->dirroot.'/user/profile/definelib.php');
profile_save_field($this->get_data(), $this->editors());
}
/**
* Load in existing data as form defaults
*/
public function set_data_for_dynamic_submission(): void {
$field = $this->get_field_record();
// Clean and prepare description for the editor.
$description = clean_text($field->description, $field->descriptionformat);
$field->description = ['text' => $description, 'format' => $field->descriptionformat, 'itemid' => 0];
// Convert the data format for.
if (is_array($this->editors())) {
foreach ($this->editors() as $editor) {
if (isset($field->$editor)) {
$editordesc = clean_text($field->$editor, $field->{$editor.'format'});
$field->$editor = ['text' => $editordesc, 'format' => $field->{$editor.'format'}, 'itemid' => 0];
}
}
}
$this->set_data($field);
}
/**
* Returns url to set in $PAGE->set_url() when form is being rendered or submitted via AJAX
*
* @return moodle_url
*/
protected function get_page_url_for_dynamic_submission(): moodle_url {
$id = $this->optional_param('id', 0, PARAM_INT);
$datatype = $this->optional_param('datatype', 'text', PARAM_PLUGIN);
return new moodle_url('/user/profile/index.php',
['action' => 'editfield', 'id' => $id, 'datatype' => $id ? null : $datatype]);
}
/**
* Record for the field from the database (or generic record for a new field)
*
* @return false|mixed|\stdClass
* @throws \coding_exception
* @throws \dml_exception
*/
public function get_field_record() {
global $DB;
if (!$this->fieldrecord) {
$id = $this->optional_param('id', 0, PARAM_INT);
if (!$id || !($this->fieldrecord = $DB->get_record('user_info_field', ['id' => $id]))) {
$datatype = $this->optional_param('datatype', 'text', PARAM_PLUGIN);
$this->fieldrecord = new \stdClass();
$this->fieldrecord->datatype = $datatype;
$this->fieldrecord->description = '';
$this->fieldrecord->descriptionformat = FORMAT_HTML;
$this->fieldrecord->defaultdata = '';
$this->fieldrecord->defaultdataformat = FORMAT_HTML;
$this->fieldrecord->categoryid = $this->optional_param('categoryid', 0, PARAM_INT);
}
if (!\core_component::get_component_directory('profilefield_'.$this->fieldrecord->datatype)) {
throw new \moodle_exception('fieldnotfound', 'customfield');
}
}
return $this->fieldrecord;
}
}