Merge branch 'MDL-87825-main' of https://github.com/sebastsg/moodle

This commit is contained in:
Sara Arjona
2026-02-11 07:00:27 +01:00
3 changed files with 245 additions and 3 deletions
@@ -18,7 +18,9 @@ namespace customfield_number\task;
use core\task\scheduled_task;
use core_customfield\category_controller;
use core_customfield\customfield\shared_handler;
use core_customfield\field_controller;
use core_customfield\handler;
use customfield_number\provider_base;
/**
@@ -65,10 +67,24 @@ class cron extends scheduled_task {
$category = category_controller::create(0, $cat);
// Create an instance of field controller for each field and recalculate the value if field provider is available.
$field = field_controller::create(0, $row, $category);
if ($provider = provider_base::instance($field)) {
if ($provider->is_available()) {
$provider->recalculate(null, $cat->component, $cat->area, $cat->itemid);
$provider = provider_base::instance($field);
if (!$provider || !$provider->is_available()) {
continue;
}
// For shared custom fields, all other component/areas must be recalculated.
if ($cat->component === 'core_customfield' && $cat->area === 'shared') {
$handlers = \core_component::get_component_classes_in_namespace(null, 'customfield');
// Remove shared handler since the shared area has no instances.
unset($handlers[shared_handler::class]);
foreach ($handlers as $handlerclass => $path) {
if (!is_subclass_of($handlerclass, handler::class)) {
continue;
}
$handler = $handlerclass::create();
$provider->recalculate(null, $handler->get_component(), $handler->get_area(), $handler->get_itemid());
}
} else {
$provider->recalculate(null, $cat->component, $cat->area, (int)$cat->itemid);
}
}
}
@@ -0,0 +1,105 @@
<?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/>.
declare(strict_types=1);
namespace customfield_number;
use core_customfield\data_controller;
/**
* Test provider that sets the field data to the current second of the hour.
*
* @package customfield_number
* @copyright 2026 Sebastian Gundersen <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class test_provider extends provider_base {
/**
* Get provider name.
*
* @return string
*/
public function get_name(): string {
return 'Test provider';
}
/**
* Check if the provider is available for the field.
*
* @return bool
*/
public function is_available(): bool {
return true;
}
/**
* Recalculate field value.
*
* @param int|null $instanceid
* @param string $component
* @param string $area
* @param int $itemid
*/
public function recalculate(
?int $instanceid = null,
string $component = 'core_course',
string $area = 'course',
int $itemid = 0,
): void {
global $DB;
$params = [
'fieldid' => $this->field->get('id'),
'component' => $component,
'area' => $area,
'itemid' => $itemid,
];
$sql = "SELECT *
FROM {customfield_data}
WHERE fieldid = :fieldid
AND component = :component
AND area = :area
AND itemid = :itemid";
if ($instanceid !== null) {
$sql .= " AND instanceid <> :instanceid";
$params['instanceid'] = $instanceid;
}
$records = $DB->get_recordset_sql($sql, $params);
if (!$records->valid() && $component === 'core_course' && $area === 'course') {
foreach ($DB->get_records('course') as $course) {
$data = data_controller::create(0, (object)['instanceid' => (int)$course->id], $this->field);
$data->set('contextid', \core\context\system::instance()->id);
$data->set('component', 'core_course');
$data->set('area', 'course');
$data->set('decvalue', \core\di::get(\core\clock::class)->time() % 3600);
$data->save();
}
}
foreach ($records as $record) {
$data = data_controller::create(0, $record, $this->field);
$data->set('contextid', \core\context\system::instance()->id);
$data->set('component', $component);
$data->set('area', $area);
$data->set('decvalue', \core\di::get(\core\clock::class)->time() % 3600);
$data->save();
}
$records->close();
}
}
@@ -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/>.
declare(strict_types=1);
namespace customfield_number\task;
use core_customfield\api;
use core_customfield\external\toggle_shared_category;
use core_customfield\field_controller;
use customfield_number\test_provider;
/**
* Test the cron task.
*
* @package customfield_number
* @covers \customfield_number\task\cron
* @copyright 2026 Sebastian Gundersen <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class cron_test extends \advanced_testcase {
/** @var field_controller Field */
private field_controller $field;
/**
* Set up.
*
* @return void
*/
protected function setUp(): void {
parent::setUp();
$this->resetAfterTest();
$this->setAdminUser();
require_once(__DIR__ . '/../fixtures/test_provider.php');
$category = $this->getDataGenerator()->create_custom_field_category([
'component' => 'core_customfield',
'area' => 'shared',
]);
toggle_shared_category::execute($category->get('id'), 'core_course', 'course', 0, true);
$this->field = $this->getDataGenerator()->create_custom_field([
'categoryid' => $category->get('id'),
'shortname' => 'seconds',
'type' => 'number',
'configdata' => [
'fieldtype' => test_provider::class,
],
]);
}
/**
* Test running the cron task to recalculate number custom field values when data is created by the provider.
*
* @return void
*/
public function test_execute_without_data(): void {
$clock = $this->mock_clock_with_frozen();
$fieldid = $this->field->get('id');
$fields = [$fieldid => $this->field];
$course = $this->getDataGenerator()->create_course();
$courseid = (int)$course->id;
// Confirm current value.
$data = api::get_instance_fields_data($fields, $courseid, true, 'core_course', 'course');
$this->assertNull($data[$fieldid]->get_value());
$data = api::get_instance_fields_data($fields, $courseid, true, 'core_customfield', 'shared');
$this->assertNull($data[$fieldid]->get_value());
// Run the cron task and confirm the value is updated.
(new cron())->execute();
$data = api::get_instance_fields_data($fields, $courseid, true, 'core_course', 'course');
$this->assertEquals($clock->time() % 3600, $data[$fieldid]->get_value());
$data = api::get_instance_fields_data($fields, $courseid, true, 'core_customfield', 'shared');
$this->assertNull($data[$fieldid]->get_value());
}
/**
* Test running the cron task to recalculate number custom field values when there is already data.
*
* @return void
*/
public function test_execute_with_existing_data(): void {
$clock = $this->mock_clock_with_frozen();
$fieldid = $this->field->get('id');
$fields = [$fieldid => $this->field];
$course = $this->getDataGenerator()->create_course([
'customfield_seconds' => 10,
]);
$courseid = (int)$course->id;
// Confirm current value.
$data = api::get_instance_fields_data($fields, $courseid, true, 'core_course', 'course');
$this->assertEquals(10, $data[$fieldid]->get_value());
$data = api::get_instance_fields_data($fields, $courseid, true, 'core_customfield', 'shared');
$this->assertNull($data[$fieldid]->get_value());
// Run the cron task and confirm the value is updated.
(new cron())->execute();
$data = api::get_instance_fields_data($fields, $courseid, true, 'core_course', 'course');
$this->assertEquals($clock->time() % 3600, $data[$fieldid]->get_value());
$data = api::get_instance_fields_data($fields, $courseid, true, 'core_customfield', 'shared');
$this->assertNull($data[$fieldid]->get_value());
}
}