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,147 @@
<?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 date plugin
*
* @package customfield_date
* @copyright 2018 Daniel Neis Araujo <daniel@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customfield_date;
use core_customfield\api;
defined('MOODLE_INTERNAL') || die;
/**
* Class data
*
* @package customfield_date
* @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';
}
/**
* Add fields for editing data of a date field on a context.
*
* @param \MoodleQuickForm $mform
*/
public function instance_form_definition(\MoodleQuickForm $mform) {
$field = $this->get_field();
// Get the current calendar in use - see MDL-18375.
$calendartype = \core_calendar\type_factory::get_calendar_instance();
$config = $field->get('configdata');
// Always set the form element to "optional", even when it's required. Otherwise it defaults to the
// current date and is easy to miss.
$attributes = ['optional' => true];
if (!empty($config['mindate'])) {
$attributes['startyear'] = $calendartype->timestamp_to_date_array($config['mindate'])['year'];
}
if (!empty($config['maxdate'])) {
$attributes['stopyear'] = $calendartype->timestamp_to_date_array($config['maxdate'])['year'];
}
if (empty($config['includetime'])) {
$element = 'date_selector';
} else {
$element = 'date_time_selector';
}
$elementname = $this->get_form_element_name();
$mform->addElement($element, $elementname, $this->get_field()->get_formatted_name(), $attributes);
$mform->setType($elementname, PARAM_INT);
$mform->setDefault($elementname, time());
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);
$elementname = $this->get_form_element_name();
if (!empty($data[$elementname])) {
// Compare the date with min/max values, trim the date to the minute or to the day (depending on inludetime setting).
$includetime = $this->get_field()->get_configdata_property('includetime');
$machineformat = $includetime ? '%Y-%m-%d %H:%M' : '%Y-%m-%d';
$humanformat = $includetime ? get_string('strftimedatetimeshort') : get_string('strftimedatefullshort');
$value = userdate($data[$elementname], $machineformat, 99, false, false);
$mindate = $this->get_field()->get_configdata_property('mindate');
$maxdate = $this->get_field()->get_configdata_property('maxdate');
if ($mindate && userdate($mindate, $machineformat, 99, false, false) > $value) {
$errors[$elementname] = get_string('errormindate', 'customfield_date', userdate($mindate, $humanformat));
}
if ($maxdate && userdate($maxdate, $machineformat, 99, false, false) < $value) {
$errors[$elementname] = get_string('errormaxdate', 'customfield_date', userdate($maxdate, $humanformat));
}
}
return $errors;
}
/**
* Returns the default value as it would be stored in the database (not in human-readable format).
*
* @return mixed
*/
public function get_default_value() {
return 0;
}
/**
* 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;
}
// Check if time needs to be included.
if ($this->get_field()->get_configdata_property('includetime')) {
$format = get_string('strftimedaydatetime', 'langconfig');
} else {
$format = get_string('strftimedate', 'langconfig');
}
return userdate($value, $format);
}
}
@@ -0,0 +1,130 @@
<?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 date plugin
*
* @package customfield_date
* @copyright 2018 David Matamoros <davidmc@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customfield_date;
defined('MOODLE_INTERNAL') || die;
/**
* Class field
*
* @package customfield_date
* @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 {
/**
* Type of plugin data
*/
const TYPE = 'date';
/**
* Validate the data from the config form.
*
* @param array $data
* @param array $files
* @return array associative array of error messages
*/
public function config_form_validation(array $data, $files = array()): array {
$errors = array();
// Make sure the start year is not greater than the end year.
if (!empty($data['configdata']['mindate']) && !empty($data['configdata']['maxdate']) &&
$data['configdata']['mindate'] > $data['configdata']['maxdate']) {
$errors['configdata[mindate]'] = get_string('mindateaftermax', 'customfield_date');
}
return $errors;
}
/**
* Add fields for editing a date field.
*
* @param \MoodleQuickForm $mform
*/
public function config_form_definition(\MoodleQuickForm $mform) {
$config = $this->get('configdata');
// Add elements.
$mform->addElement('header', 'header_specificsettings', get_string('specificsettings', 'customfield_date'));
$mform->setExpanded('header_specificsettings', true);
$mform->addElement('advcheckbox', 'configdata[includetime]', get_string('includetime', 'customfield_date'));
$mform->addElement('date_time_selector', 'configdata[mindate]', get_string('mindate', 'customfield_date'),
['optional' => true]);
$mform->addElement('date_time_selector', 'configdata[maxdate]', get_string('maxdate', 'customfield_date'),
['optional' => true]);
$mform->hideIf('configdata[maxdate][hour]', 'configdata[includetime]');
$mform->hideIf('configdata[maxdate][minute]', 'configdata[includetime]');
$mform->hideIf('configdata[mindate][hour]', 'configdata[includetime]');
$mform->hideIf('configdata[mindate][minute]', 'configdata[includetime]');
}
/**
* 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 {
$format = get_string('strftimedate', 'langconfig');
$ret = [];
foreach ($values as $value) {
if ($value) {
$ret[$value] = userdate($value, $format);
}
}
if (!$ret) {
return []; // If the only dates found are 0, then do not show any options.
}
$ret[BLOCK_MYOVERVIEW_CUSTOMFIELD_EMPTY] = get_string('nocustomvalue', 'block_myoverview',
$this->get_formatted_name());
return $ret;
}
/**
* Convert given value into appropriate timestamp
*
* @param string $value
* @return int
*/
public function parse_value(string $value) {
$timestamp = strtotime($value);
// If we have a valid, positive timestamp then return it.
return $timestamp > 0 ? $timestamp : 0;
}
}
@@ -0,0 +1,85 @@
<?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_date.
*
* @package customfield_date
* @copyright 2018 Daniel Neis Araujo <danielneis@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customfield_date\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_date 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();
// For date field we want to use PrivacyAPI date format instead of export_value().
$exportdata->value = \core_privacy\local\request\transform::datetime($data->get_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,35 @@
<?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/>.
/**
* Customfields date plugin
*
* @package customfield_date
* @copyright 2018 David Matamoros
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$string['errormaxdate'] = 'Please enter a date no later than {$a}.';
$string['errormindate'] = 'Please enter a date on or after {$a}.';
$string['includetime'] = 'Include time';
$string['maxdate'] = 'Maximum value';
$string['mindate'] = 'Minimum value';
$string['mindateaftermax'] = 'The minimum value cannot be bigger than the maximum value.';
$string['pluginname'] = 'Date and time';
$string['privacy:metadata'] = 'The Date and time field type plugin doesn\'t store any personal data; it uses tables defined in core.';
$string['specificsettings'] = 'Date and time field settings';
+35
View File
@@ -0,0 +1,35 @@
<?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 date plugin
*
* @package customfield_date
* @copyright 2018 Daniel Neis Araujo <daniel@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Get icon mapping for font-awesome.
*/
function customfield_date_get_fontawesome_icon_map() {
return [
'customfield_date:checked' => 'fa-check-square-o',
'customfield_date:notchecked' => 'fa-square-o',
];
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 372 B

+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="16px" height="16px" viewBox="0 0 16 16" version="1.1" preserveAspectRatio="xMinYMid meet">
<g id="surface1">
<path style=" stroke:none;fill-rule:nonzero;fill:rgb(60%,60%,60%);fill-opacity:1;" d="M 13.144531 8.304688 L 13.144531 11.144531 C 13.144531 11.851562 12.890625 12.457031 12.386719 12.960938 C 11.886719 13.460938 11.28125 13.714844 10.570312 13.714844 L 3.144531 13.714844 C 2.433594 13.714844 1.828125 13.460938 1.324219 12.960938 C 0.824219 12.457031 0.570312 11.851562 0.570312 11.144531 L 0.570312 3.714844 C 0.570312 3.007812 0.824219 2.398438 1.324219 1.898438 C 1.828125 1.394531 2.433594 1.144531 3.144531 1.144531 L 10.570312 1.144531 C 10.945312 1.144531 11.292969 1.21875 11.617188 1.367188 C 11.707031 1.40625 11.757812 1.476562 11.777344 1.570312 C 11.792969 1.671875 11.769531 1.757812 11.695312 1.832031 L 11.257812 2.269531 C 11.199219 2.328125 11.132812 2.355469 11.054688 2.355469 C 11.035156 2.355469 11.007812 2.351562 10.972656 2.339844 C 10.835938 2.304688 10.703125 2.285156 10.570312 2.285156 L 3.144531 2.285156 C 2.75 2.285156 2.414062 2.425781 2.132812 2.707031 C 1.855469 2.984375 1.714844 3.320312 1.714844 3.714844 L 1.714844 11.144531 C 1.714844 11.535156 1.855469 11.871094 2.132812 12.152344 C 2.414062 12.429688 2.75 12.570312 3.144531 12.570312 L 10.570312 12.570312 C 10.964844 12.570312 11.300781 12.429688 11.582031 12.152344 C 11.859375 11.871094 12 11.535156 12 11.144531 L 12 8.875 C 12 8.796875 12.027344 8.730469 12.082031 8.679688 L 12.652344 8.105469 C 12.710938 8.046875 12.78125 8.019531 12.855469 8.019531 C 12.894531 8.019531 12.929688 8.027344 12.964844 8.042969 C 13.082031 8.09375 13.144531 8.179688 13.144531 8.304688 Z M 15.207031 3.9375 L 7.9375 11.207031 C 7.792969 11.347656 7.625 11.417969 7.429688 11.417969 C 7.230469 11.417969 7.0625 11.347656 6.917969 11.207031 L 3.082031 7.367188 C 2.9375 7.222656 2.867188 7.054688 2.867188 6.855469 C 2.867188 6.660156 2.9375 6.492188 3.082031 6.347656 L 4.0625 5.367188 C 4.207031 5.222656 4.375 5.152344 4.570312 5.152344 C 4.769531 5.152344 4.9375 5.222656 5.082031 5.367188 L 7.429688 7.714844 L 13.207031 1.9375 C 13.347656 1.792969 13.519531 1.722656 13.714844 1.722656 C 13.910156 1.722656 14.082031 1.792969 14.222656 1.9375 L 15.207031 2.917969 C 15.347656 3.0625 15.417969 3.230469 15.417969 3.429688 C 15.417969 3.625 15.347656 3.792969 15.207031 3.9375 Z M 15.207031 3.9375 "/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 266 B

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="16px" height="16px" viewBox="0 0 16 16" version="1.1" preserveAspectRatio="xMinYMid meet">
<g id="surface1">
<path style=" stroke:none;fill-rule:nonzero;fill:rgb(60%,60%,60%);fill-opacity:1;" d="M 11.714844 2.285156 L 4.285156 2.285156 C 3.894531 2.285156 3.554688 2.425781 3.277344 2.707031 C 2.996094 2.984375 2.855469 3.320312 2.855469 3.714844 L 2.855469 11.144531 C 2.855469 11.535156 2.996094 11.871094 3.277344 12.152344 C 3.554688 12.429688 3.894531 12.570312 4.285156 12.570312 L 11.714844 12.570312 C 12.105469 12.570312 12.445312 12.429688 12.722656 12.152344 C 13.003906 11.871094 13.144531 11.535156 13.144531 11.144531 L 13.144531 3.714844 C 13.144531 3.320312 13.003906 2.984375 12.722656 2.707031 C 12.445312 2.425781 12.105469 2.285156 11.714844 2.285156 Z M 14.285156 3.714844 L 14.285156 11.144531 C 14.285156 11.851562 14.035156 12.457031 13.53125 12.960938 C 13.027344 13.460938 12.421875 13.714844 11.714844 13.714844 L 4.285156 13.714844 C 3.578125 13.714844 2.972656 13.460938 2.46875 12.960938 C 1.964844 12.457031 1.714844 11.851562 1.714844 11.144531 L 1.714844 3.714844 C 1.714844 3.007812 1.964844 2.398438 2.46875 1.898438 C 2.972656 1.394531 3.578125 1.144531 4.285156 1.144531 L 11.714844 1.144531 C 12.421875 1.144531 13.027344 1.394531 13.53125 1.898438 C 14.035156 2.398438 14.285156 3.007812 14.285156 3.714844 Z M 14.285156 3.714844 "/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

@@ -0,0 +1,102 @@
@customfield @customfield_date @javascript
Feature: Managers can manage course custom fields date
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 date field
When I click on "Add a new custom field" "link"
And I click on "Date and time" "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 Date and time" "dialogue"
Then I should see "Test field"
And I log out
Scenario: Edit a custom course date field
When I click on "Add a new custom field" "link"
And I click on "Date and time" "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 Date and time" "dialogue"
And I click on "[data-role='editfield']" "css_element"
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 log out
Scenario: Delete a custom course date field
When I click on "Add a new custom field" "link"
And I click on "Date and time" "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 Date and time" "dialogue"
And I click on "[data-role='deletefield']" "css_element"
And I click on "Yes" "button" in the "Confirm" "dialogue"
Then I should not see "Test field"
And I log out
Scenario: A date field makerd to include time must show those fields on course form
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | Example 1 | teacher1@example.com |
And the following "courses" exist:
| fullname | shortname | format |
| Course 1 | C1 | topics |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
When I click on "Add a new custom field" "link"
And I click on "Date and time" "link"
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
| Include time | 1 |
And I click on "Save changes" "button" in the "Adding a new Date and time" "dialogue"
And I log out
Then I log in as "teacher1"
When I am on site homepage
When I am on "Course 1" course homepage
And I navigate to "Settings" in current page administration
And I expand all fieldsets
Then "#id_customfield_testfield_hour" "css_element" should be visible
Then "#id_customfield_testfield_minute" "css_element" should be visible
And I log out
Scenario: A date field makerd to not include time must not show those fields on course form
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | Example 1 | teacher1@example.com |
And the following "courses" exist:
| fullname | shortname | format |
| Course 1 | C1 | topics |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
When I click on "Add a new custom field" "link"
And I click on "Date and time" "link"
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
| Include time | |
And I click on "Save changes" "button" in the "Adding a new Date and time" "dialogue"
And I log out
Then I log in as "teacher1"
When I am on site homepage
When I am on "Course 1" course homepage
And I navigate to "Settings" in current page administration
And I expand all fieldsets
Then "#id_customfield_testfield_hour" "css_element" should not be visible
Then "#id_customfield_testfield_minute" "css_element" should not be visible
And I log out
@@ -0,0 +1,202 @@
<?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_date;
use core_customfield_generator;
use core_customfield_test_instance_form;
/**
* Functional test for customfield_date
*
* @package customfield_date
* @copyright 2019 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
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' => 'date']);
$this->cfields[2] = $this->get_generator()->create_field(
['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield2', 'type' => 'date',
'configdata' => ['required' => 1, 'includetime' => 0, 'mindate' => 946684800, 'maxdate' => 1893456000]]);
$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, 1546300800);
$this->cfdata[2] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[2]->id, 1546300800);
$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' => 'date'], $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'] = time();
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->assertEmpty($data->customfield_myfield1);
$this->assertNotEmpty($data->customfield_myfield2);
$handler->instance_form_save($data);
}
/**
* Test for min/max date validation
*/
public function test_instance_form_validation(): void {
$this->setAdminUser();
$handler = $this->cfcat->get_handler();
$submitdata = (array)$this->courses[1];
$data = data_controller::create(0, null, $this->cfields[2]);
// Submit with date less than mindate.
$submitdata['customfield_myfield2'] = 915148800;
$this->assertNotEmpty($data->instance_form_validation($submitdata, []));
// Submit with date more than maxdate.
$submitdata['customfield_myfield2'] = 1893557000;
$this->assertNotEmpty($data->instance_form_validation($submitdata, []));
}
/**
* Test for data_controller::get_value and export_value
*/
public function test_get_export_value(): void {
$this->assertEquals(1546300800, $this->cfdata[1]->get_value());
$this->assertStringMatchesFormat('%a 1 January 2019%a', $this->cfdata[1]->export_value());
// Field without data.
$d = \core_customfield\data_controller::create(0, null, $this->cfields[2]);
$this->assertEquals(0, $d->get_value());
$this->assertEquals(null, $d->export_value());
}
/**
* Data provider for {@see test_parse_value}
*
* @return array
*/
public function parse_value_provider(): array {
return [
// Valid times.
['2019-10-01', strtotime('2019-10-01')],
['2019-10-01 14:00', strtotime('2019-10-01 14:00')],
// Invalid times.
['ZZZZZ', 0],
['202-04-01', 0],
['2019-15-15', 0],
];
}
/**
* Test field parse_value method
*
* @param string $value
* @param int $expected
* @return void
*
* @dataProvider parse_value_provider
*/
public function test_parse_value(string $value, int $expected): void {
$this->assertSame($expected, $this->cfields[1]->parse_value($value));
}
/**
* Deleting fields and data
*/
public function test_delete(): void {
$this->cfcat->get_handler()->delete_all();
}
}
+30
View File
@@ -0,0 +1,30 @@
<?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 date plugin
*
* @package customfield_date
* @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_date';
$plugin->version = 2024042200;
$plugin->requires = 2024041600;