From 66ad660accb4679678a92b33166a1a1df187c89f Mon Sep 17 00:00:00 2001 From: Ilya Tregubov Date: Mon, 27 Jun 2022 14:40:37 +0400 Subject: [PATCH] MDL-69690 mod_workshop: Fix fieldname for workshop. --- grade/classes/component_gradeitems.php | 15 ++++-- .../local/gradeitem/fieldname_mapping.php | 48 +++++++++++++++++++ mod/workshop/classes/grades/gradeitems.php | 24 +++++++++- .../tests/behat/workshop_edit_form.feature | 47 ++++++++++++++++++ version.php | 2 +- 5 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 grade/classes/local/gradeitem/fieldname_mapping.php create mode 100644 mod/workshop/tests/behat/workshop_edit_form.feature diff --git a/grade/classes/component_gradeitems.php b/grade/classes/component_gradeitems.php index daa260d3d9a..7a3e78221e0 100644 --- a/grade/classes/component_gradeitems.php +++ b/grade/classes/component_gradeitems.php @@ -132,13 +132,20 @@ class component_gradeitems { * @return string The translated field name */ public static function get_field_name_for_itemnumber(string $component, int $itemnumber, string $fieldname): string { - $itemname = static::get_itemname_from_itemnumber($component, $itemnumber); - if ($itemname) { - return "{$fieldname}_{$itemname}"; + $classname = "{$component}\grades\gradeitems"; + + if (class_exists($classname) && is_subclass_of($classname, 'core_grades\local\gradeitem\fieldname_mapping')) { + $fieldname = $classname::get_field_name_for_itemnumber($component, $itemnumber, $fieldname); + } else { + $itemname = static::get_itemname_from_itemnumber($component, $itemnumber); + + if ($itemname) { + $fieldname .= '_' . $itemname; + } } - return $fieldname; + } /** diff --git a/grade/classes/local/gradeitem/fieldname_mapping.php b/grade/classes/local/gradeitem/fieldname_mapping.php new file mode 100644 index 00000000000..b29e6ff3df0 --- /dev/null +++ b/grade/classes/local/gradeitem/fieldname_mapping.php @@ -0,0 +1,48 @@ +. + +/** + * Grade item, fieldname mapping. + * + * @package core_grades + * @copyright Ilya Tregubov + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +declare(strict_types = 1); + +namespace core_grades\local\gradeitem; + +/** + * Grade item, fieldname mapping. + * + * @package core_grades + * @copyright Ilya Tregubov + */ +interface fieldname_mapping { + + /** + * Get the suffixed field name for an activity field mapped from its itemnumber. + * + * For legacy reasons, the first itemnumber has no suffix on field names. + * + * @param string $component The component that the grade item belongs to + * @param int $itemnumber The grade itemnumber + * @param string $fieldname The name of the field to be rewritten + * @return string The translated field name + */ + public static function get_field_name_for_itemnumber(string $component, int $itemnumber, string $fieldname): string; +} diff --git a/mod/workshop/classes/grades/gradeitems.php b/mod/workshop/classes/grades/gradeitems.php index 297e11a06e2..78a5e0cc802 100644 --- a/mod/workshop/classes/grades/gradeitems.php +++ b/mod/workshop/classes/grades/gradeitems.php @@ -26,6 +26,8 @@ declare(strict_types = 1); namespace mod_workshop\grades; +use core_grades\component_gradeitems; +use core_grades\local\gradeitem\fieldname_mapping; use \core_grades\local\gradeitem\itemnumber_mapping; /** @@ -35,7 +37,7 @@ use \core_grades\local\gradeitem\itemnumber_mapping; * @copyright Andrew Nicols * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class gradeitems implements itemnumber_mapping { +class gradeitems implements itemnumber_mapping, fieldname_mapping { /** * Return the list of grade item mappings for the workshop. * @@ -47,4 +49,24 @@ class gradeitems implements itemnumber_mapping { 1 => 'grading', ]; } + + /** + * Get the suffixed field name for an activity field mapped from its itemnumber. + * + * For legacy reasons, the first itemnumber has no suffix on field names. + * + * @param string $component The component that the grade item belongs to + * @param int $itemnumber The grade itemnumber + * @param string $fieldname The name of the field to be rewritten + * @return string The translated field name + */ + public static function get_field_name_for_itemnumber(string $component, int $itemnumber, string $fieldname): string { + $itemname = component_gradeitems::get_itemname_from_itemnumber($component, $itemnumber); + + if ($itemname) { + return "{$itemname}{$fieldname}"; + } + + return $fieldname; + } } diff --git a/mod/workshop/tests/behat/workshop_edit_form.feature b/mod/workshop/tests/behat/workshop_edit_form.feature new file mode 100644 index 00000000000..49e5ea55538 --- /dev/null +++ b/mod/workshop/tests/behat/workshop_edit_form.feature @@ -0,0 +1,47 @@ +@mod @mod_workshop +Feature: Workshop assessment with grade to pass + In order to use workshop activity + As a teacher + I need to be able to setup workshop with require assessment grade and grade to pass + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | student1 | Sam1 | Student1 | student1@example.com | + | teacher1 | Terry1 | Teacher1 | teacher1@example.com | + And the following "courses" exist: + | fullname | shortname | format | enablecompletion | + | Course1 | c1 | topics | 1 | + And the following "course enrolments" exist: + | user | course | role | + | student1 | c1 | student | + | teacher1 | c1 | editingteacher | + + @javascript + Scenario: Setup workshop with assessment grade and pass grade set + And I log in as "teacher1" + And I am on "Course1" course homepage with editing mode on + And I add a "Workshop" to section "0" + And I expand all fieldsets + When I set the following fields to these values: + | Workshop name | Test workshop | + | Completion tracking | Show activity as complete when conditions are met | + | Require grade | Assessment | + | completionpassgrade | 1 | + And I press "Save and display" + And I should see "This activity does not have a valid grade to pass set. It may be set in the Grade section of the activity settings." + And I set the field "Assessment grade to pass" to "" + And I press "Save and display" + And I should see "This activity does not have a valid grade to pass set. It may be set in the Grade section of the activity settings." + And I set the field "Assessment grade to pass" to "81" + And I press "Save and display" + And I should see "The grade to pass can not be greater than the maximum possible grade 20" + And I set the field "Assessment grade to pass" to "hello" + And I press "Save and display" + And I should see "You must enter a number here." + And I set the field "Assessment grade to pass" to "12,34" + And I press "Save and display" + And I should see "You must enter a number here." + And I set the field "Assessment grade to pass" to "10" + And I press "Save and display" + Then I should see "Setup phase" in the "h3#mod_workshop-userplanheading" "css_element" diff --git a/version.php b/version.php index 8d402e57778..f21329c63d0 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2022112800.07; // 20221128 = branching date YYYYMMDD - do not modify! +$version = 2022112800.08; // 20221128 = branching date YYYYMMDD - do not modify! // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '4.1+ (Build: 20230105)'; // Human-friendly version name