diff --git a/customfield/field/number/amd/build/recalculate.min.js b/customfield/field/number/amd/build/recalculate.min.js index 4d21dc56f0e..6a3ade8a1e9 100644 --- a/customfield/field/number/amd/build/recalculate.min.js +++ b/customfield/field/number/amd/build/recalculate.min.js @@ -6,6 +6,6 @@ define("customfield_number/recalculate",["exports","core/ajax","core/notificatio * @author 2024 Marina Glancy * @copyright 2024 Moodle Pty Ltd * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=function(){if(initialised)return;initialised=!0,document.addEventListener("click",(e=>{const target=e.target.closest(SELECTORS_wrapper+" "+SELECTORS_link);if(!target)return;const el=target.closest(SELECTORS_wrapper).querySelector(SELECTORS_value);if(!el)return;e.preventDefault();const fieldid=target.dataset.fieldid,instanceid=target.dataset.instanceid,pendingPromise=new _pending.default("recalculate_customfield_number");(0,_loadingicon.addIconToContainer)(el).then((()=>_ajax.default.call([{methodname:"customfield_number_recalculate_value",args:{fieldid:fieldid,instanceid:instanceid}}])[0])).then((data=>{el.innerHTML=data.value,pendingPromise.resolve()})).catch(_notification.default.exception)}))},_ajax=_interopRequireDefault(_ajax),_notification=_interopRequireDefault(_notification),_pending=_interopRequireDefault(_pending);const SELECTORS_wrapper='[data-fieldtype="wrapper"]',SELECTORS_value='[data-fieldtype="value"]',SELECTORS_link='[data-fieldtype="link"]';let initialised=!1})); + */Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=function(){if(initialised)return;document.addEventListener("click",(e=>{const target=e.target.closest(SELECTORS_wrapper+" "+SELECTORS_link);if(!target)return;const el=target.closest(SELECTORS_wrapper).querySelector(SELECTORS_value);if(!el)return;e.preventDefault();const{fieldid:fieldid,instanceid:instanceid}=target.dataset,pendingPromise=new _pending.default("recalculate_customfield_number");(0,_loadingicon.addIconToContainer)(el).then((()=>_ajax.default.call([{methodname:"customfield_number_recalculate_value",args:{fieldid:fieldid,instanceid:instanceid}}])[0])).then((data=>(el.innerHTML=data.value,pendingPromise.resolve()))).catch(_notification.default.exception)})),initialised=!0},_ajax=_interopRequireDefault(_ajax),_notification=_interopRequireDefault(_notification),_pending=_interopRequireDefault(_pending);const SELECTORS_wrapper='[data-fieldtype="wrapper"]',SELECTORS_value='[data-fieldtype="value"]',SELECTORS_link='[data-fieldtype="link"]';let initialised=!1})); //# sourceMappingURL=recalculate.min.js.map \ No newline at end of file diff --git a/customfield/field/number/amd/build/recalculate.min.js.map b/customfield/field/number/amd/build/recalculate.min.js.map index 3f00b321523..d365e1777f6 100644 --- a/customfield/field/number/amd/build/recalculate.min.js.map +++ b/customfield/field/number/amd/build/recalculate.min.js.map @@ -1 +1 @@ -{"version":3,"file":"recalculate.min.js","sources":["../src/recalculate.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * Allows to recalculate a single value on demand\n *\n * @module customfield_number/recalculate\n * @author 2024 Marina Glancy\n * @copyright 2024 Moodle Pty Ltd \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport Ajax from 'core/ajax';\nimport Notification from 'core/notification';\nimport {addIconToContainer} from 'core/loadingicon';\nimport Pending from 'core/pending';\n\nconst SELECTORS = {\n wrapper: '[data-fieldtype=\"wrapper\"]',\n value: '[data-fieldtype=\"value\"]',\n link: '[data-fieldtype=\"link\"]',\n};\n\nlet initialised = false;\n\n/**\n * Init\n */\nexport function init() {\n if (initialised) {\n return;\n }\n\n initialised = true;\n\n document.addEventListener('click', (e) => {\n const target = e.target.closest(SELECTORS.wrapper + \" \" + SELECTORS.link);\n if (!target) {\n return;\n }\n const el = target.closest(SELECTORS.wrapper).querySelector(SELECTORS.value);\n if (!el) {\n return;\n }\n e.preventDefault();\n const fieldid = target.dataset.fieldid;\n const instanceid = target.dataset.instanceid;\n\n const pendingPromise = new Pending('recalculate_customfield_number');\n addIconToContainer(el).then(() => {\n return Ajax.call([{\n methodname: 'customfield_number_recalculate_value',\n args: {fieldid, instanceid}\n }])[0];\n }).then((data) => {\n el.innerHTML = data.value;\n pendingPromise.resolve();\n return;\n }).catch(Notification.exception);\n });\n}\n"],"names":["initialised","document","addEventListener","e","target","closest","SELECTORS","el","querySelector","preventDefault","fieldid","dataset","instanceid","pendingPromise","Pending","then","Ajax","call","methodname","args","data","innerHTML","value","resolve","catch","Notification","exception"],"mappings":";;;;;;;;yFAyCQA,mBAIJA,aAAc,EAEdC,SAASC,iBAAiB,SAAUC,UAC1BC,OAASD,EAAEC,OAAOC,QAAQC,kBAAoB,IAAMA,oBACrDF,oBAGCG,GAAKH,OAAOC,QAAQC,mBAAmBE,cAAcF,qBACtDC,UAGLJ,EAAEM,uBACIC,QAAUN,OAAOO,QAAQD,QACzBE,WAAaR,OAAOO,QAAQC,WAE5BC,eAAiB,IAAIC,iBAAQ,sEAChBP,IAAIQ,MAAK,IACjBC,cAAKC,KAAK,CAAC,CACdC,WAAY,uCACZC,KAAM,CAACT,QAAAA,QAASE,WAAAA,eAChB,KACLG,MAAMK,OACLb,GAAGc,UAAYD,KAAKE,MACpBT,eAAeU,aAEhBC,MAAMC,sBAAaC,uJAzCxBpB,kBACO,6BADPA,gBAEK,2BAFLA,eAGI,8BAGNN,aAAc"} \ No newline at end of file +{"version":3,"file":"recalculate.min.js","sources":["../src/recalculate.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * Allows to recalculate a single value on demand\n *\n * @module customfield_number/recalculate\n * @author 2024 Marina Glancy\n * @copyright 2024 Moodle Pty Ltd \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport Ajax from 'core/ajax';\nimport Notification from 'core/notification';\nimport {addIconToContainer} from 'core/loadingicon';\nimport Pending from 'core/pending';\n\nconst SELECTORS = {\n wrapper: '[data-fieldtype=\"wrapper\"]',\n value: '[data-fieldtype=\"value\"]',\n link: '[data-fieldtype=\"link\"]',\n};\n\nlet initialised = false;\n\n/**\n * Init\n */\nexport function init() {\n if (initialised) {\n return;\n }\n\n document.addEventListener('click', (e) => {\n const target = e.target.closest(SELECTORS.wrapper + \" \" + SELECTORS.link);\n if (!target) {\n return;\n }\n const el = target.closest(SELECTORS.wrapper).querySelector(SELECTORS.value);\n if (!el) {\n return;\n }\n e.preventDefault();\n\n const {fieldid, instanceid} = target.dataset;\n\n const pendingPromise = new Pending('recalculate_customfield_number');\n addIconToContainer(el).then(() => {\n return Ajax.call([{\n methodname: 'customfield_number_recalculate_value',\n args: {fieldid, instanceid}\n }])[0];\n }).then((data) => {\n el.innerHTML = data.value;\n return pendingPromise.resolve();\n }).catch(Notification.exception);\n });\n\n initialised = true;\n}\n"],"names":["initialised","document","addEventListener","e","target","closest","SELECTORS","el","querySelector","preventDefault","fieldid","instanceid","dataset","pendingPromise","Pending","then","Ajax","call","methodname","args","data","innerHTML","value","resolve","catch","Notification","exception"],"mappings":";;;;;;;;yFAyCQA,mBAIJC,SAASC,iBAAiB,SAAUC,UAC1BC,OAASD,EAAEC,OAAOC,QAAQC,kBAAoB,IAAMA,oBACrDF,oBAGCG,GAAKH,OAAOC,QAAQC,mBAAmBE,cAAcF,qBACtDC,UAGLJ,EAAEM,uBAEIC,QAACA,QAADC,WAAUA,YAAcP,OAAOQ,QAE/BC,eAAiB,IAAIC,iBAAQ,sEAChBP,IAAIQ,MAAK,IACjBC,cAAKC,KAAK,CAAC,CACdC,WAAY,uCACZC,KAAM,CAACT,QAAAA,QAASC,WAAAA,eAChB,KACLI,MAAMK,OACLb,GAAGc,UAAYD,KAAKE,MACbT,eAAeU,aACvBC,MAAMC,sBAAaC,cAG1B1B,aAAc,2IAzCZM,kBACO,6BADPA,gBAEK,2BAFLA,eAGI,8BAGNN,aAAc"} \ No newline at end of file diff --git a/customfield/field/number/amd/src/recalculate.js b/customfield/field/number/amd/src/recalculate.js index e8ee632b8d2..46a8b0c4035 100644 --- a/customfield/field/number/amd/src/recalculate.js +++ b/customfield/field/number/amd/src/recalculate.js @@ -43,8 +43,6 @@ export function init() { return; } - initialised = true; - document.addEventListener('click', (e) => { const target = e.target.closest(SELECTORS.wrapper + " " + SELECTORS.link); if (!target) { @@ -55,8 +53,8 @@ export function init() { return; } e.preventDefault(); - const fieldid = target.dataset.fieldid; - const instanceid = target.dataset.instanceid; + + const {fieldid, instanceid} = target.dataset; const pendingPromise = new Pending('recalculate_customfield_number'); addIconToContainer(el).then(() => { @@ -66,8 +64,9 @@ export function init() { }])[0]; }).then((data) => { el.innerHTML = data.value; - pendingPromise.resolve(); - return; + return pendingPromise.resolve(); }).catch(Notification.exception); }); + + initialised = true; } diff --git a/customfield/field/number/classes/data_controller.php b/customfield/field/number/classes/data_controller.php index 28a651e6587..d31f4ace168 100644 --- a/customfield/field/number/classes/data_controller.php +++ b/customfield/field/number/classes/data_controller.php @@ -46,10 +46,11 @@ class data_controller extends \core_customfield\data_controller { public function instance_form_definition(MoodleQuickForm $mform): void { global $OUTPUT; - $field = $this->get_field(); $elementname = $this->get_form_element_name(); + + // If the field isn't editable (based on type/provider config), then display static element. + $field = $this->get_field(); if (!$field->is_editable()) { - // Display the value as static text. $instanceid = (int)$this->get('instanceid'); $data = ['value' => $this->export_value(), 'fieldid' => $field->get('id'), 'instanceid' => $instanceid]; $value = $OUTPUT->render_from_template('customfield_number/staticvalue', $data); diff --git a/customfield/field/number/classes/external/recalculate.php b/customfield/field/number/classes/external/recalculate.php index 2a01eef0f84..b2f0aac28f3 100644 --- a/customfield/field/number/classes/external/recalculate.php +++ b/customfield/field/number/classes/external/recalculate.php @@ -56,10 +56,13 @@ class recalculate extends external_api { */ public static function execute(int $fieldid, int $instanceid): array { // Parameter validation. - ['fieldid' => $fieldid, 'instanceid' => $instanceid] = self::validate_parameters( - self::execute_parameters(), - ['fieldid' => $fieldid, 'instanceid' => $instanceid] - ); + [ + 'fieldid' => $fieldid, + 'instanceid' => $instanceid, + ] = self::validate_parameters(self::execute_parameters(), [ + 'fieldid' => $fieldid, + 'instanceid' => $instanceid, + ]); // Access validation. $context = \context_system::instance(); diff --git a/customfield/field/number/classes/field_controller.php b/customfield/field/number/classes/field_controller.php index 9d06a183c1b..d1e6a5bab78 100644 --- a/customfield/field/number/classes/field_controller.php +++ b/customfield/field/number/classes/field_controller.php @@ -31,6 +31,7 @@ use MoodleQuickForm; * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class field_controller extends \core_customfield\field_controller { + /** * Add form elements for editing the custom field definition * @@ -102,14 +103,14 @@ class field_controller extends \core_customfield\field_controller { * Adds selector to provider for field population. * * @param MoodleQuickForm $mform - * @param array $providers + * @param provider_base[] $providers */ - protected function add_field_type_select(\MoodleQuickForm $mform, array $providers): void { + protected function add_field_type_select(MoodleQuickForm $mform, array $providers): void { $autooptions = []; foreach ($providers as $provider) { $autooptions[get_class($provider)] = $provider->get_name(); } - $options = [get_string('genericfield', 'customfield_number')]; + $options = ['' => get_string('genericfield', 'customfield_number')]; $options = array_merge($options, $autooptions); $mform->addElement('select', 'configdata[fieldtype]', get_string('fieldtype', 'customfield_number'), $options); } @@ -175,6 +176,7 @@ class field_controller extends \core_customfield\field_controller { if ($value === null) { return null; } + $decimalplaces = (int) $this->get_configdata_property('decimalplaces'); if (round((float) $value, $decimalplaces) == 0) { $value = $this->get_configdata_property('displaywhenzero'); @@ -188,11 +190,13 @@ class field_controller extends \core_customfield\field_controller { $value = $provider->prepare_export_value($value, $context); } else { $value = format_float((float)$value, $decimalplaces); + // Apply the display format. $format = $this->get_configdata_property('display') ?? '{value}'; $value = str_replace('{value}', $value, $format); } } + return format_string($value, true, ['context' => $context ?? system::instance()]); } @@ -202,7 +206,6 @@ class field_controller extends \core_customfield\field_controller { * @return bool */ public function is_editable(): bool { - return $this->get_configdata_property('fieldtype') == false; + return (string) $this->get_configdata_property('fieldtype') === ''; } - } diff --git a/customfield/field/number/classes/hook/add_custom_providers.php b/customfield/field/number/classes/hook/add_custom_providers.php index d89f7396844..abf43e7170c 100644 --- a/customfield/field/number/classes/hook/add_custom_providers.php +++ b/customfield/field/number/classes/hook/add_custom_providers.php @@ -30,9 +30,8 @@ use customfield_number\field_controller; */ #[\core\attribute\label('This hook allows adding custom providers to calculate custom field automatically like price for course')] class add_custom_providers { - /** - * @var array - */ + + /** @var provider_base[] $providers */ protected array $providers = []; /** diff --git a/customfield/field/number/classes/local/numberproviders/nofactivities.php b/customfield/field/number/classes/local/numberproviders/nofactivities.php index 8f5f50e0b3a..89f2f451e89 100644 --- a/customfield/field/number/classes/local/numberproviders/nofactivities.php +++ b/customfield/field/number/classes/local/numberproviders/nofactivities.php @@ -55,7 +55,7 @@ class nofactivities extends provider_base { * Add autocomplete field for selecting activity type. * Also add checkbox to display the field when the number of activities is zero. * - * @param \MoodleQuickForm $mform + * @param MoodleQuickForm $mform */ public function config_form_definition(MoodleQuickForm $mform): void { $options = []; @@ -87,14 +87,13 @@ class nofactivities extends provider_base { global $DB; $types = $this->field->get_configdata_property('activitytypes'); $displaywhenzero = $this->field->get_configdata_property('displaywhenzero'); - if (!empty($types)) { - // Prepare the SQL for non-empty types. - [$sqlin, $params] = $DB->get_in_or_equal($types, SQL_PARAMS_NAMED); - } else { + + if (empty($types)) { return; } // Subquery to select all modules of selected types. + [$sqlin, $params] = $DB->get_in_or_equal($types, SQL_PARAMS_NAMED); $cmsql = "SELECT m.id FROM {modules} m WHERE m.name $sqlin @@ -160,7 +159,7 @@ class nofactivities extends provider_base { * Preparation for export for number of activities provider. * * @param mixed $value String or float - * @param \context|null $context |null $context Context + * @param \context|null $context Context * @return ?string */ public function prepare_export_value(mixed $value, ?\context $context = null): ?string {