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
@@ -0,0 +1,121 @@
<?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/>.
/**
* Select plugin data controller
*
* @package customfield_select
* @copyright 2018 Daniel Neis Araujo <daniel@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customfield_select;
defined('MOODLE_INTERNAL') || die;
/**
* Class data
*
* @package customfield_select
* @copyright 2018 Daniel Neis Araujo <daniel@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class data_controller extends \core_customfield\data_controller {
/**
* Return the name of the field where the information is stored
* @return string
*/
public function datafield(): string {
return 'intvalue';
}
/**
* Returns the default value as it would be stored in the database (not in human-readable format).
*
* @return mixed
*/
public function get_default_value() {
$defaultvalue = $this->get_field()->get_configdata_property('defaultvalue');
if ('' . $defaultvalue !== '') {
$key = array_search($defaultvalue, $this->get_field()->get_options());
if ($key !== false) {
return $key;
}
}
return 0;
}
/**
* Add fields for editing a textarea field.
*
* @param \MoodleQuickForm $mform
*/
public function instance_form_definition(\MoodleQuickForm $mform) {
$field = $this->get_field();
$config = $field->get('configdata');
$options = $field->get_options();
$elementname = $this->get_form_element_name();
$mform->addElement('select', $elementname, $this->get_field()->get_formatted_name(), $options);
if (($defaultkey = array_search($config['defaultvalue'], $options)) !== false) {
$mform->setDefault($elementname, $defaultkey);
}
if ($field->get_configdata_property('required')) {
$mform->addRule($elementname, null, 'required', null, 'client');
}
}
/**
* Validates data for this field.
*
* @param array $data
* @param array $files
* @return array
*/
public function instance_form_validation(array $data, array $files): array {
$errors = parent::instance_form_validation($data, $files);
if ($this->get_field()->get_configdata_property('required')) {
// Standard required rule does not work on select element.
$elementname = $this->get_form_element_name();
if (empty($data[$elementname])) {
$errors[$elementname] = get_string('err_required', 'form');
}
}
return $errors;
}
/**
* Returns value in a human-readable format
*
* @return mixed|null value or null if empty
*/
public function export_value() {
$value = $this->get_value();
if ($this->is_empty($value)) {
return null;
}
$options = $this->get_field()->get_options();
if (array_key_exists($value, $options)) {
return $options[$value];
}
return null;
}
}
@@ -0,0 +1,135 @@
<?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 customfield_select;
use coding_exception;
/**
* Class field
*
* @package customfield_select
* @copyright 2018 David Matamoros <davidmc@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class field_controller extends \core_customfield\field_controller {
/**
* Customfield type
*/
const TYPE = 'select';
/**
* Add fields for editing a select field.
*
* @param \MoodleQuickForm $mform
*/
public function config_form_definition(\MoodleQuickForm $mform) {
$mform->addElement('header', 'header_specificsettings', get_string('specificsettings', 'customfield_select'));
$mform->setExpanded('header_specificsettings', true);
$mform->addElement('textarea', 'configdata[options]', get_string('menuoptions', 'customfield_select'));
$mform->setType('configdata[options]', PARAM_TEXT);
$mform->addElement('text', 'configdata[defaultvalue]', get_string('defaultvalue', 'core_customfield'), 'size="50"');
$mform->setType('configdata[defaultvalue]', PARAM_TEXT);
}
/**
* @deprecated since Moodle 3.10 - MDL-68569 please use $field->get_options
*/
public static function get_options_array(): void {
throw new coding_exception('get_options_array() is deprecated, please use $field->get_options() instead');
}
/**
* Return configured field options
*
* @return array
*/
public function get_options(): array {
$optionconfig = $this->get_configdata_property('options');
if ($optionconfig) {
$context = $this->get_handler()->get_configuration_context();
$options = array_map(
fn(string $option) => format_string($option, true, ['context' => $context]),
preg_split("/\s*\n\s*/", trim($optionconfig), -1, PREG_SPLIT_NO_EMPTY),
);
} else {
$options = array();
}
return array_merge([''], $options);
}
/**
* Validate the data from the config form.
* Sub classes must reimplement it.
*
* @param array $data from the add/edit profile field form
* @param array $files
* @return array associative array of error messages
*/
public function config_form_validation(array $data, $files = array()): array {
$options = preg_split("/\s*\n\s*/", trim($data['configdata']['options']));
$errors = [];
if (!$options || count($options) < 2) {
$errors['configdata[options]'] = get_string('errornotenoughoptions', 'customfield_select');
} else if (!empty($data['configdata']['defaultvalue'])) {
$defaultkey = array_search($data['configdata']['defaultvalue'], $options);
if ($defaultkey === false) {
$errors['configdata[defaultvalue]'] = get_string('errordefaultvaluenotinlist', 'customfield_select');
}
}
return $errors;
}
/**
* Does this custom field type support being used as part of the block_myoverview
* custom field grouping?
* @return bool
*/
public function supports_course_grouping(): bool {
return true;
}
/**
* If this field supports course grouping, then this function needs overriding to
* return the formatted values for this.
* @param array $values the used values that need formatting
* @return array
*/
public function course_grouping_format_values($values): array {
$options = $this->get_options();
$ret = [];
foreach ($values as $value) {
if (isset($options[$value])) {
$ret[$value] = $options[$value];
}
}
$ret[BLOCK_MYOVERVIEW_CUSTOMFIELD_EMPTY] = get_string('nocustomvalue', 'block_myoverview',
$this->get_formatted_name());
return $ret;
}
/**
* Locate the value parameter in the field options array, and return it's index
*
* @param string $value
* @return int
*/
public function parse_value(string $value) {
return (int) array_search($value, $this->get_options());
}
}
@@ -0,0 +1,83 @@
<?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/>.
/**
* Privacy Subsystem implementation for customfield_select.
*
* @package customfield_select
* @copyright 2018 Daniel Neis Araujo <danielneis@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customfield_select\privacy;
use core_customfield\data_controller;
use core_customfield\privacy\customfield_provider;
use core_privacy\local\request\writer;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem for customfield_select implementing null_provider.
*
* @copyright 2018 Daniel Neis Araujo <danielneis@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider, customfield_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
/**
* Preprocesses data object that is going to be exported
*
* @param data_controller $data
* @param \stdClass $exportdata
* @param array $subcontext
*/
public static function export_customfield_data(data_controller $data, \stdClass $exportdata, array $subcontext) {
$context = $data->get_context();
$exportdata->value = $data->export_value();
writer::with_context($context)
->export_data($subcontext, $exportdata);
}
/**
* Allows plugins to delete everything they store related to the data (usually files)
*
* @param string $dataidstest
* @param array $params
* @param array $contextids
* @return mixed|void
*/
public static function before_delete_data(string $dataidstest, array $params, array $contextids) {
}
/**
* Allows plugins to delete everything they store related to the field configuration (usually files)
*
* @param string $fieldidstest
* @param array $params
* @param array $contextids
*/
public static function before_delete_fields(string $fieldidstest, array $params, array $contextids) {
}
}
@@ -0,0 +1,33 @@
<?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/>.
/**
* Customfield text field plugin strings
*
* @package customfield_select
* @copyright 2018 Toni Barbera <toni@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$string['errordefaultvaluenotinlist'] = 'The default value must be one of the options from the list above.';
$string['errornotenoughoptions'] = 'Please provide at least two options, with each on a new line.';
$string['invalidoption'] = 'Invalid option selected';
$string['menuoptions'] = 'Menu options (one per line)';
$string['pluginname'] = 'Dropdown menu';
$string['privacy:metadata'] = 'The Dropdown menu field type plugin doesn\'t store any personal data; it uses tables defined in core.';
$string['specificsettings'] = 'Dropdown menu field settings';
@@ -0,0 +1,85 @@
@customfield @customfield_select @javascript
Feature: Managers can manage course custom fields select
In order to have additional data on the course
As a manager
I need to create, edit, remove and sort custom fields
Background:
Given the following "custom field categories" exist:
| name | component | area | itemid |
| Category for test | core_course | course | 0 |
And I log in as "admin"
And I navigate to "Courses > Default settings > Course custom fields" in site administration
Scenario: Create a custom course select field
When I click on "Add a new custom field" "link"
And I click on "Dropdown menu" "link"
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
And I set the field "Menu options (one per line)" to multiline:
"""
a
b
"""
And I click on "Save changes" "button" in the "Adding a new Dropdown menu" "dialogue"
Then I should see "Test field"
And I log out
Scenario: Edit a custom course select field
When I click on "Add a new custom field" "link"
And I click on "Dropdown menu" "link"
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
And I set the field "Menu options (one per line)" to multiline:
"""
a
b
"""
And I click on "Save changes" "button" in the "Adding a new Dropdown menu" "dialogue"
And I click on "Edit" "link" in the "Test field" "table_row"
And I set the following fields to these values:
| Name | Edited field |
And I click on "Save changes" "button" in the "Updating Test field" "dialogue"
Then I should see "Edited field"
And I should not see "Test field"
And I log out
Scenario: Delete a custom course select field
When I click on "Add a new custom field" "link"
And I click on "Dropdown menu" "link"
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
And I set the field "Menu options (one per line)" to multiline:
"""
a
b
"""
And I click on "Save changes" "button" in the "Adding a new Dropdown menu" "dialogue"
And I click on "Delete" "link" in the "Test field" "table_row"
And I click on "Yes" "button" in the "Confirm" "dialogue"
Then I should not see "Test field"
And I log out
Scenario: Validation of custom course select field configuration
When I click on "Add a new custom field" "link"
And I click on "Dropdown menu" "link"
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
And I click on "Save changes" "button" in the "Adding a new Dropdown menu" "dialogue"
And I should see "Please provide at least two options, with each on a new line." in the "Menu options (one per line)" "form_row"
And I set the field "Menu options (one per line)" to multiline:
"""
a
b
"""
And I set the field "Default value" to "c"
And I click on "Save changes" "button" in the "Adding a new Dropdown menu" "dialogue"
And I should see "The default value must be one of the options from the list above" in the "Default value" "form_row"
And I set the field "Default value" to "b"
And I click on "Save changes" "button" in the "Adding a new Dropdown menu" "dialogue"
And "testfield" "text" should exist in the "Test field" "table_row"
And I log out
@@ -0,0 +1,225 @@
<?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 customfield_select;
use core_customfield_generator;
use core_customfield_test_instance_form;
use stdClass;
/**
* Functional test for customfield_select
*
* @package customfield_select
* @covers \customfield_select\data_controller
* @covers \customfield_select\field_controller
* @copyright 2019 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class plugin_test extends \advanced_testcase {
/** @var stdClass[] */
private $courses = [];
/** @var \core_customfield\category_controller */
private $cfcat;
/** @var \core_customfield\field_controller[] */
private $cfields;
/** @var \core_customfield\data_controller[] */
private $cfdata;
/**
* Tests set up.
*/
public function setUp(): void {
$this->resetAfterTest();
$this->cfcat = $this->get_generator()->create_category();
$this->cfields[1] = $this->get_generator()->create_field(
['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield1', 'type' => 'select',
'configdata' => ['options' => "a\nb\nc"]]);
$this->cfields[2] = $this->get_generator()->create_field(
['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield2', 'type' => 'select',
'configdata' => ['required' => 1, 'options' => "a\nb\nc"]]);
$this->cfields[3] = $this->get_generator()->create_field(
['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield3', 'type' => 'select',
'configdata' => ['defaultvalue' => 'b', 'options' => "a\nb\nc"]]);
$this->courses[1] = $this->getDataGenerator()->create_course();
$this->courses[2] = $this->getDataGenerator()->create_course();
$this->courses[3] = $this->getDataGenerator()->create_course();
$this->cfdata[1] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[1]->id, 1);
$this->cfdata[2] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[2]->id, 1);
$this->setUser($this->getDataGenerator()->create_user());
}
/**
* Get generator
* @return core_customfield_generator
*/
protected function get_generator(): core_customfield_generator {
return $this->getDataGenerator()->get_plugin_generator('core_customfield');
}
/**
* Test for initialising field and data controllers
*/
public function test_initialise(): void {
$f = \core_customfield\field_controller::create($this->cfields[1]->get('id'));
$this->assertTrue($f instanceof field_controller);
$f = \core_customfield\field_controller::create(0, (object)['type' => 'select'], $this->cfcat);
$this->assertTrue($f instanceof field_controller);
$d = \core_customfield\data_controller::create($this->cfdata[1]->get('id'));
$this->assertTrue($d instanceof data_controller);
$d = \core_customfield\data_controller::create(0, null, $this->cfields[1]);
$this->assertTrue($d instanceof data_controller);
}
/**
* Test for configuration form functions
*
* Create a configuration form and submit it with the same values as in the field
*/
public function test_config_form(): void {
$this->setAdminUser();
$submitdata = (array)$this->cfields[1]->to_record();
$submitdata['configdata'] = $this->cfields[1]->get('configdata');
$submitdata = \core_customfield\field_config_form::mock_ajax_submit($submitdata);
$form = new \core_customfield\field_config_form(null, null, 'post', '', null, true,
$submitdata, true);
$form->set_data_for_dynamic_submission();
$this->assertTrue($form->is_validated());
$form->process_dynamic_submission();
}
/**
* Test for instance form functions
*/
public function test_instance_form(): void {
global $CFG;
require_once($CFG->dirroot . '/customfield/tests/fixtures/test_instance_form.php');
$this->setAdminUser();
$handler = $this->cfcat->get_handler();
// First try to submit without required field.
$submitdata = (array)$this->courses[1];
core_customfield_test_instance_form::mock_submit($submitdata, []);
$form = new core_customfield_test_instance_form('POST',
['handler' => $handler, 'instance' => $this->courses[1]]);
$this->assertFalse($form->is_validated());
// Now with required field.
$submitdata['customfield_myfield2'] = 1;
core_customfield_test_instance_form::mock_submit($submitdata, []);
$form = new core_customfield_test_instance_form('POST',
['handler' => $handler, 'instance' => $this->courses[1]]);
$this->assertTrue($form->is_validated());
$data = $form->get_data();
$this->assertNotEmpty($data->customfield_myfield1);
$this->assertNotEmpty($data->customfield_myfield2);
$handler->instance_form_save($data);
}
/**
* Test for data_controller::get_value and export_value
*/
public function test_get_export_value(): void {
$this->assertEquals(1, $this->cfdata[1]->get_value());
$this->assertEquals('a', $this->cfdata[1]->export_value());
// Field without data but with a default value.
$d = \core_customfield\data_controller::create(0, null, $this->cfields[3]);
$this->assertEquals(2, $d->get_value());
$this->assertEquals('b', $d->export_value());
}
/**
* Test getting field options, formatted
*/
public function test_get_options(): void {
filter_set_global_state('multilang', TEXTFILTER_ON);
filter_set_applies_to_strings('multilang', true);
$field = $this->get_generator()->create_field([
'categoryid' => $this->cfcat->get('id'),
'type' => 'select',
'shortname' => 'myselect',
'configdata' => [
'options' => <<<EOF
<span lang="en" class="multilang">Beginner</span><span lang="es" class="multilang">Novato</span>
<span lang="en" class="multilang">Intermediate</span><span lang="es" class="multilang">Intermedio</span>
<span lang="en" class="multilang">Advanced</span><span lang="es" class="multilang">Avanzado</span>
EOF,
],
]);
$this->assertEquals([
'',
'Beginner',
'Intermediate',
'Advanced',
], $field->get_options());
}
/**
* Data provider for {@see test_parse_value}
*
* @return array
*/
public static function parse_value_provider(): array {
return [
['Red', 1],
['Blue', 2],
['Green', 3],
['Mauve', 0],
];
}
/**
* Test field parse_value method
*
* @param string $value
* @param int $expected
*
* @dataProvider parse_value_provider
*/
public function test_parse_value(string $value, int $expected): void {
$field = $this->get_generator()->create_field([
'categoryid' => $this->cfcat->get('id'),
'type' => 'select',
'shortname' => 'myselect',
'configdata' => [
'options' => "Red\nBlue\nGreen",
],
]);
$this->assertSame($expected, $field->parse_value($value));
}
/**
* Deleting fields and data
*/
public function test_delete(): void {
$this->cfcat->get_handler()->delete_all();
}
}
+29
View File
@@ -0,0 +1,29 @@
<?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/>.
/**
* Customfield Select Type
*
* @package customfield_select
* @copyright 2018 Toni Barbera <toni@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->component = 'customfield_select';
$plugin->version = 2024042200;
$plugin->requires = 2024041600;