forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-82715 customfield_number: Add automatically populated providers.
Added number of activity provider and also hooks for plugins.
- Loading branch information
1 parent
09e56f2
commit c6b1680
Showing
20 changed files
with
1,259 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
issueNumber: MDL-82715 | ||
notes: | ||
customfield_number: | ||
- message: >+ | ||
New 'customfield_number\hook\add_custom_providers' hook has been added. | ||
It allows automatic calculation of number course custom field. | ||
Added new class | ||
'\customfield_number\local\numberproviders\nofactivities' | ||
that allows to automatically calculate number of activities of a given | ||
type in a given course. | ||
Added new webservice customfield_number_recalculate_value to recalculate | ||
a value of number course custom field. | ||
Added 'customfield_number\task\cron' cron task that recalculates | ||
automatically calculated number course custom fields. | ||
type: improved |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// 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/>. | ||
|
||
/** | ||
* Allows to recalculate a single value on demand | ||
* | ||
* @module customfield_number/recalculate | ||
* @author 2024 Marina Glancy | ||
* @copyright 2024 Moodle Pty Ltd <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
import Ajax from 'core/ajax'; | ||
import Notification from 'core/notification'; | ||
import {addIconToContainer} from 'core/loadingicon'; | ||
import Pending from 'core/pending'; | ||
|
||
const SELECTORS = { | ||
wrapper: '[data-fieldtype="wrapper"]', | ||
value: '[data-fieldtype="value"]', | ||
link: '[data-fieldtype="link"]', | ||
}; | ||
|
||
let initialised = false; | ||
|
||
/** | ||
* Init | ||
*/ | ||
export function init() { | ||
if (initialised) { | ||
return; | ||
} | ||
|
||
initialised = true; | ||
|
||
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; | ||
const instanceid = target.dataset.instanceid; | ||
|
||
const pendingPromise = new Pending('recalculate_customfield_number'); | ||
addIconToContainer(el).then(() => { | ||
return Ajax.call([{ | ||
methodname: 'customfield_number_recalculate_value', | ||
args: {fieldid, instanceid} | ||
}])[0]; | ||
}).then((data) => { | ||
el.innerHTML = data.value; | ||
pendingPromise.resolve(); | ||
return; | ||
}).catch(Notification.exception); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?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\external; | ||
|
||
use core\exception\invalid_parameter_exception; | ||
use core_external\external_function_parameters; | ||
use core_external\external_single_structure; | ||
use core_external\external_api; | ||
use core_external\external_value; | ||
use customfield_number\provider_base; | ||
|
||
/** | ||
* Implementation of web service customfield_number_recalculate_value | ||
* | ||
* @package customfield_number | ||
* @author 2024 Marina Glancy | ||
* @copyright 2024 Moodle Pty Ltd <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class recalculate extends external_api { | ||
|
||
/** | ||
* Describes the parameters for customfield_number_recalculate_value | ||
* | ||
* @return external_function_parameters | ||
*/ | ||
public static function execute_parameters(): external_function_parameters { | ||
return new external_function_parameters([ | ||
'fieldid' => new external_value(PARAM_INT, 'Field id', VALUE_REQUIRED), | ||
'instanceid' => new external_value(PARAM_INT, 'Instance id', VALUE_REQUIRED), | ||
]); | ||
} | ||
|
||
/** | ||
* Implementation of web service customfield_number_recalculate_value | ||
* | ||
* @param int $fieldid | ||
* @param int $instanceid | ||
* @return array | ||
*/ | ||
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] | ||
); | ||
|
||
// Access validation. | ||
$context = \context_system::instance(); | ||
self::validate_context($context); | ||
|
||
$field = \core_customfield\field_controller::create($fieldid); | ||
$provider = provider_base::instance($field); | ||
if (!$provider) { | ||
throw new invalid_parameter_exception('Invalid parameter'); | ||
} | ||
|
||
$handler = $field->get_handler(); | ||
if (!$handler->can_edit($field, $instanceid)) { | ||
throw new \moodle_exception('nopermissions', '', '', get_string('update')); | ||
} | ||
|
||
$provider->recalculate($instanceid); | ||
|
||
$data = \core_customfield\api::get_instance_fields_data( | ||
[$fieldid => $field], $instanceid)[$fieldid]; | ||
|
||
return ['value' => $data->export_value()]; | ||
} | ||
|
||
/** | ||
* Describe the return structure for customfield_number_recalculate_value | ||
* | ||
* @return external_single_structure | ||
*/ | ||
public static function execute_returns(): external_single_structure { | ||
return new external_single_structure([ | ||
'value' => new external_value(PARAM_RAW, 'Recalculated value (prepared for display)'), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.