From 4588a6ac0fa601a45d033d1b2e17b2cc67c9b511 Mon Sep 17 00:00:00 2001 From: Irina Hoppe Date: Wed, 20 Dec 2023 11:39:34 +0100 Subject: [PATCH 1/8] WIP: added custom completion rules --- classes/completion/custom_completion.php | 105 +++++++++++++++++++++++ db/install.xml | 2 + db/upgrade.php | 19 ++++ lib.php | 70 +++++++++++++++ locallib.php | 48 ++++++++++- mod_form.php | 38 ++++++++ version.php | 2 +- 7 files changed, 281 insertions(+), 3 deletions(-) create mode 100644 classes/completion/custom_completion.php diff --git a/classes/completion/custom_completion.php b/classes/completion/custom_completion.php new file mode 100644 index 00000000..c08af3c2 --- /dev/null +++ b/classes/completion/custom_completion.php @@ -0,0 +1,105 @@ +. + +declare(strict_types=1); + +namespace mod_ratingallocate\completion; + +use core_completion\activity_custom_completion; + +/** + * Activity custom completion subclass for the ratingallocate activity. + * + * Class for defining the custom completion rules of ratingallocate and fetching the completion statuses + * of the custom completion rules for a given ratingallocate instance and a user. + * + * @package mod_ratingallocate + * @copyright Irina Hoppe Uni Münster + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class custom_completion extends activity_custom_completion { + + /** + * Fetches the completion state for a given completion rule. + * + * @param string $rule The completion rule. + * @return int The completion state. + * @throws \moodle_exception + */ + public function get_state(string $rule): int { + global $DB; + + $this->validate_rule($rule); + + $userid = $this->userid; + $ratingallocateid = $this->cm->instance; + + if (!$DB->get_record('ratingallocate', ['id' => $ratingallocateid])) { + throw new \moodle_exception('Unable to find ratingallocate instance with id ' . $ratingallocateid); + } + + if ($rule == 'completionvote') { + $sql = "SELECT * FROM {ratingallocate_ratings} r INNER JOIN {ratingallocate_choices} c on r.choiceid=c.id " . + "WHERE r.userid= :userid AND c.ratingallocateid= :ratingallocateid"; + $votesofuser = $DB->get_records_sql($sql, ['userid' => $userid, 'ratingallocateid' => $ratingallocateid]); + $status = count($votesofuser) > 0; + } else if ($rule == 'completionallocation') { + $sql = "SELECT * FROM {ratingallocate_allocations} WHERE userid= :userid AND ratingallocateid= :ratingallocateid"; + $allocationsofuser = $DB->get_records_sql($sql, ['userid' => $userid, 'ratingallocateid' => $ratingallocateid]); + $status = count($allocationsofuser) > 0; + } + + return $status ? COMPLETION_COMPLETE : COMPLETION_INCOMPLETE; + } + + /** + * Fetch the list of custom completion rules that this module defines. + * + * @return array + */ + public static function get_defined_custom_rules(): array { + return [ + 'completionvote', + 'completionallocation' + ]; + } + + /** + * Returns an associative array of the descriptions of custom completion rules. + * + * @return array + */ + public function get_custom_rule_descriptions(): array { + return [ + 'completionvote' => get_string('comletionvote_desc', RATINGALLOCATE_MOD_NAME), + 'completionallocation' => get_string('completionallocation_desc', RATINGALLOCATE_MOD_NAME) + ]; + } + + /** + * Returns an array of all completion rules, in the order they should be displayed to users. + * + * @return array + */ + public function get_sort_order(): array { + return [ + 'completionview', + 'completionvote', + 'completionallocation' + ]; + } +} + diff --git a/db/install.xml b/db/install.xml index 5433f236..3ca1d6f0 100644 --- a/db/install.xml +++ b/db/install.xml @@ -23,6 +23,8 @@ + + diff --git a/db/upgrade.php b/db/upgrade.php index e111a143..1ab55f38 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -218,5 +218,24 @@ function xmldb_ratingallocate_upgrade($oldversion) { upgrade_mod_savepoint(true, 2023050900, 'ratingallocate'); } + if ($oldversion < 2023122000) { + + // Define completionrules fields to be added to ratingallocate. + $table = new xmldb_table('ratingallocate'); + $votefield = new xmldb_field('completionvote', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, false, '0'); + $allocationfield = new xmldb_field('completionallocation', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, false, '0'); + + // Conditionally launch add field notification_send. + if (!$dbman->field_exists($table, $votefield)) { + $dbman->add_field($table, $votefield); + } + if (!$dbman->field_exists($table, $allocationfield)) { + $dbman->add_field($table, $allocationfield); + } + + // Ratingallocate savepoint reached. + upgrade_mod_savepoint(true, 2023121300, 'ratingallocate'); + } + return true; } diff --git a/lib.php b/lib.php index 53ee070d..f736d5ff 100644 --- a/lib.php +++ b/lib.php @@ -71,6 +71,8 @@ function ratingallocate_supports($feature) { return true; case FEATURE_COMPLETION_TRACKS_VIEWS: return true; + case FEATURE_COMPLETION_HAS_RULES: + return true; default : return null; } @@ -734,3 +736,71 @@ function ratingallocate_reset_course_form_definition($mform) { function ratingallocate_reset_course_form_defaults($course) { return ['reset_ratings_and_allocations' => 1]; } + +function ratingallocate_get_coursemodule_info($coursemodule) { + global $DB; + + $dbparams = array('id' => $coursemodule->instance); + $fields = 'id, name, intro, introformat, accesstimestart, accesstimestop, completionvote, completionallocation'; + if (!$ratingallocate = $DB->get_record(RATINGALLOCATE_MOD_NAME, $dbparams, $fields)) { + return false; + } + + $result = new cached_cm_info(); + $result->name = $ratingallocate->name; + + if ($coursemodule->showdescription) { + // Convert intro to html. Do not filter cached version, filters run at display time. + $result->content = format_module_intro(RATINGALLOCATE_MOD_NAME, $ratingallocate, $coursemodule->id, false); + } + + // Populate the custom completion rules as key => value pairs, but only if the completion mode is 'automatic'. + if ($ratingallocate->completion == COMPLETION_TRACKING_AUTOMATIC) { + $result->customdata['customcompletionrules']['completionvote'] = $ratingallocate->completionvote; + $result->customdata['customcompletionrules']['completionallocation'] = $ratingallocate->completionallocation; + } + + if ($ratingallocate->accesstimestart) { + $result->customdata['accesstimestart'] = $ratingallocate->accesstimestart; + } + if ($ratingallocate->accesstimestop) { + $result->customdata['accesstimestop'] = $ratingallocate->accesstimestop; + } + + return $result; +} + +/** + * Callback which returns human-readable strings describing the active completion custom rules for the module instance. + * + * @param cm_info|stdClass $cm object with fields ->completion and ->customdata['customcompletionrules'] + * @return array $descriptions the array of descriptions for the custom rules. + */ +function mod_ratingallocate_get_completion_active_rule_descriptions($cm) +{ + // Values will be present in cm_info, and we assume these are up to date. + if (empty($cm->customdata['customcompletionrules']) || $cm->completion != COMPLETION_TRACKING_AUTOMATIC) { + return []; + } + + $descriptions = []; + + foreach ($cm->customdata['customcompletionrules'] as $key => $val) { + switch ($key) { + case 'completionvote': + if ($val == 1) { + $descriptions[] = get_string('copletionvotedesc', RATINGALLOCATE_MOD_NAME); + } + break; + case 'completionallocation': + if ($val == 1) { + $descriptions[] = get_string('copletionallocationdesc', RATINGALLOCATE_MOD_NAME); + } + break; + default: + break; + } + } + + return $descriptions; +} diff --git a/locallib.php b/locallib.php index 615d0e70..ca57d88e 100644 --- a/locallib.php +++ b/locallib.php @@ -180,6 +180,7 @@ class ratingallocate { /** * Returns all users enrolled in the course the ratingallocate is in, who were able to access the activity + * @returns Array of user records * @throws moodle_exception */ public function get_raters_in_course(): array { @@ -1292,6 +1293,15 @@ public function distrubute_choices() { $distributor = new solver_edmonds_karp(); $timestart = microtime(true); $distributor->distribute_users($this); + + $completion = new completion_info($this->course); + $raters = $this->get_raters_in_course(); + if ($completion->is_enabled($this->coursemodule)) { + foreach ($raters as $rater) { + $completion->update_state($this->coursemodule, COMPLETION_COMPLETE, $rater->id); + } + + } $timeneeded = (microtime(true) - $timestart); // Set algorithm status to finished. @@ -1499,6 +1509,15 @@ public function get_allocations() { */ public function clear_all_allocations() { $this->db->delete_records('ratingallocate_allocations', array('ratingallocateid' => intval($this->ratingallocateid))); + $raters = $this->get_raters_in_course(); + + $completion = new completion_info($this->course); + if ($completion->is_enabled($this->coursemodule)) { + foreach ($raters as $rater) { + $completion->update_state($this->coursemodule, COMPLETION_INCOMPLETE, $rater->id); + } + } + } /** @@ -1637,7 +1656,7 @@ public function get_users_with_ratings() { * Deletes all ratings in this ratingallocate */ public function delete_all_ratings() { - global $DB; + global $DB, $USER; $transaction = $DB->start_delegated_transaction(); @@ -1658,6 +1677,11 @@ public function delete_all_ratings() { $transaction->allow_commit(); + $completion = new completion_info($this->course); + if ($completion->is_enabled($this->coursemodule)) { + $completion->update_state($this->coursemodule, COMPLETION_INCOMPLETE, $USER->id); + } + // Logging. $event = \mod_ratingallocate\event\all_ratings_deleted::create_simple( context_module::instance($this->coursemodule->id), $this->ratingallocateid); @@ -1692,6 +1716,11 @@ public function delete_ratings_of_user($userid) { $transaction->allow_commit(); + $completion = new completion_info($this->course); + if ($completion->is_enabled($this->coursemodule)) { + $completion->update_state($this->coursemodule, COMPLETION_INCOMPLETE, $userid); + } + // Logging. $event = \mod_ratingallocate\event\rating_deleted::create_simple( context_module::instance($this->coursemodule->id), $this->ratingallocateid); @@ -1748,7 +1777,10 @@ public function save_ratings_to_db($userid, array $data) { $transaction->allow_commit(); $completion = new completion_info($this->course); - $completion->set_module_viewed($this->coursemodule); + if ($completion->is_enabled()) { + $completion->set_module_viewed($this->coursemodule, $userid); + $completion->update_state($this->coursemodule, COMPLETION_UNKNOWN, $userid); + } // Logging. $event = \mod_ratingallocate\event\rating_saved::create_simple( @@ -1933,6 +1965,10 @@ public function remove_allocation($choiceid, $userid) { 'choiceid' => $choiceid, 'userid' => $userid )); + $completion = new completion_info($this->course); + if ($completion->is_enabled($this->coursemodule)) { + $completion->update_state($this->coursemodule, COMPLETION_INCOMPLETE, $userid); + } return true; } @@ -1946,6 +1982,10 @@ public function remove_allocations($userid) { 'userid' => $userid, 'ratingallocateid' => $this->ratingallocateid )); + $completion = new completion_info($this->course); + if ($completion->is_enabled($this->coursemodule)) { + $completion->update_state($this->coursemodule, COMPLETION_INCOMPLETE, $userid); + } } /** @@ -1960,6 +2000,10 @@ public function add_allocation($choiceid, $userid) { 'userid' => $userid, 'ratingallocateid' => $this->ratingallocateid )); + $completion = new completion_info($this->course); + if ($completion->is_enabled($this->coursemodule)) { + $completion->update_state($this->coursemodule, COMPLETION_COMPLETE, $userid); + } return true; } diff --git a/mod_form.php b/mod_form.php index 61f3099e..f15ec111 100644 --- a/mod_form.php +++ b/mod_form.php @@ -297,4 +297,42 @@ public function validation($data, $files) { private function get_settingsfield_identifier($strategy, $key) { return self::STRATEGY_OPTIONS . '[' . $strategy . '][' . $key . ']'; } + + /** + * Add elements for setting the custom completion rules. + * + * @return array List of added element names. + */ + public function add_completion_rules() { + $mform = $this->_form; + $suffix = $this->get_suffix(); + + $mform->addElement('checkbox', $this->get_suffixed_name('vote'), ' ', get_string('completionvote', RATINGALLOCATE_MOD_NAME)); + $mform->addElement('checkbox', $this->get_suffixed_name('allocation'), ' ', get_string('completionallocation', RATINGALLOCATE_MOD_NAME)); + + //Set default to not checked. + $mform->setDefault($this->get_suffixed_name('vote'), 0); + $mform->setDefault($this->get_suffixed_name('allocation'), 0); + + //Add help buttons. + $mform->addHelpButton($this->get_suffixed_name('vote'), 'completionvote', RATINGALLOCATE_MOD_NAME); + $mform->addHelpButton($this->get_suffixed_name('allocation'), 'completionallocation', RATINGALLOCATE_MOD_NAME); + + return [$this->get_suffixed_name('vote'), $this->get_suffixed_name('allocation')]; + } + + protected function get_suffixed_name(string $fieldname): string { + return $fieldname . $this->get_suffix(); + } + + /** + * Called during validaiton to see wether some activitiy-specific completion rules are selected. + * + * @param array $data Input data not yet validated. + * @return bool True if one or more rules are enabled, false if none are. + */ + public function completion_rule_enabled($data) { + return ($data[$this->get_suffixed_name('vote')] == 1 || $data[$this->get_suffixed_name('allocation')] == 1); + } + } diff --git a/version.php b/version.php index 57cc2f0d..ceda13eb 100644 --- a/version.php +++ b/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2023101900; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2023122000; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2020061500; // Requires Moodle 3.9+. $plugin->maturity = MATURITY_STABLE; $plugin->release = 'v4.3-r1'; From 00d4a65613ab57f5f38a3012114ca233d4cd9baf Mon Sep 17 00:00:00 2001 From: Irina Hoppe Date: Wed, 17 Jan 2024 10:32:26 +0100 Subject: [PATCH 2/8] added language strings and fixed some syntax errors --- classes/completion/custom_completion.php | 2 +- db/upgrade.php | 4 ++-- lang/en/ratingallocate.php | 7 +++++++ lib.php | 2 +- mod_form.php | 6 +++--- version.php | 2 +- 6 files changed, 15 insertions(+), 8 deletions(-) diff --git a/classes/completion/custom_completion.php b/classes/completion/custom_completion.php index c08af3c2..9e2a35d2 100644 --- a/classes/completion/custom_completion.php +++ b/classes/completion/custom_completion.php @@ -84,7 +84,7 @@ public static function get_defined_custom_rules(): array { */ public function get_custom_rule_descriptions(): array { return [ - 'completionvote' => get_string('comletionvote_desc', RATINGALLOCATE_MOD_NAME), + 'completionvote' => get_string('completionvote_desc', RATINGALLOCATE_MOD_NAME), 'completionallocation' => get_string('completionallocation_desc', RATINGALLOCATE_MOD_NAME) ]; } diff --git a/db/upgrade.php b/db/upgrade.php index 1ab55f38..33a4f531 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -218,7 +218,7 @@ function xmldb_ratingallocate_upgrade($oldversion) { upgrade_mod_savepoint(true, 2023050900, 'ratingallocate'); } - if ($oldversion < 2023122000) { + if ($oldversion < 2024011700) { // Define completionrules fields to be added to ratingallocate. $table = new xmldb_table('ratingallocate'); @@ -234,7 +234,7 @@ function xmldb_ratingallocate_upgrade($oldversion) { } // Ratingallocate savepoint reached. - upgrade_mod_savepoint(true, 2023121300, 'ratingallocate'); + upgrade_mod_savepoint(true, 2024011700, 'ratingallocate'); } return true; diff --git a/lang/en/ratingallocate.php b/lang/en/ratingallocate.php index 325b03e9..98b34f43 100644 --- a/lang/en/ratingallocate.php +++ b/lang/en/ratingallocate.php @@ -303,6 +303,13 @@ $string['err_required'] = 'You need to provide a value for this field.'; $string['err_minimum'] = 'The minimum value for this field is {$a}.'; $string['err_maximum'] = 'The maximum value for this field is {$a}.'; + +$string['completionvote'] = "Vote in the activity"; +$string['completionallocation'] = "Been allocated to a choice"; +$string['completionvote_help'] = "To complete this activity, users have to submit a vote."; +$string['completionallocation_help'] = "To complete this activity, users have to be allocated to a choice."; +$string['completionvote_desc'] = "Vote"; +$string['completionallocation_desc'] = "Be allocated"; // // $string['show_choices_header'] = 'List of all choices'; diff --git a/lib.php b/lib.php index f736d5ff..6e5b3bc1 100644 --- a/lib.php +++ b/lib.php @@ -755,7 +755,7 @@ function ratingallocate_get_coursemodule_info($coursemodule) { } // Populate the custom completion rules as key => value pairs, but only if the completion mode is 'automatic'. - if ($ratingallocate->completion == COMPLETION_TRACKING_AUTOMATIC) { + if ($coursemodule->completion == COMPLETION_TRACKING_AUTOMATIC) { $result->customdata['customcompletionrules']['completionvote'] = $ratingallocate->completionvote; $result->customdata['customcompletionrules']['completionallocation'] = $ratingallocate->completionallocation; } diff --git a/mod_form.php b/mod_form.php index f15ec111..26c724c4 100644 --- a/mod_form.php +++ b/mod_form.php @@ -307,8 +307,8 @@ public function add_completion_rules() { $mform = $this->_form; $suffix = $this->get_suffix(); - $mform->addElement('checkbox', $this->get_suffixed_name('vote'), ' ', get_string('completionvote', RATINGALLOCATE_MOD_NAME)); - $mform->addElement('checkbox', $this->get_suffixed_name('allocation'), ' ', get_string('completionallocation', RATINGALLOCATE_MOD_NAME)); + $mform->addElement('advcheckbox', $this->get_suffixed_name('vote'), ' ', get_string('completionvote', RATINGALLOCATE_MOD_NAME)); + $mform->addElement('advcheckbox', $this->get_suffixed_name('allocation'), ' ', get_string('completionallocation', RATINGALLOCATE_MOD_NAME)); //Set default to not checked. $mform->setDefault($this->get_suffixed_name('vote'), 0); @@ -322,7 +322,7 @@ public function add_completion_rules() { } protected function get_suffixed_name(string $fieldname): string { - return $fieldname . $this->get_suffix(); + return 'completion' . $fieldname; } /** diff --git a/version.php b/version.php index ceda13eb..d64d003d 100644 --- a/version.php +++ b/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2023122000; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2024011700; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2020061500; // Requires Moodle 3.9+. $plugin->maturity = MATURITY_STABLE; $plugin->release = 'v4.3-r1'; From 0a66f5ee529f243330cd15323771243dc184ee55 Mon Sep 17 00:00:00 2001 From: Irina Hoppe Date: Wed, 17 Jan 2024 13:52:58 +0100 Subject: [PATCH 3/8] some codestyle fixes and added behat tests for custom completion rules --- lib.php | 3 +- mod_form.php | 4 +- .../completion_condition_allocation.feature | 43 ++++++++++++++++++ tests/behat/completion_condition_vote.feature | 44 +++++++++++++++++++ tests/behat/completion_manual.feature | 39 ++++++++++++++++ 5 files changed, 129 insertions(+), 4 deletions(-) create mode 100644 tests/behat/completion_condition_allocation.feature create mode 100644 tests/behat/completion_condition_vote.feature create mode 100644 tests/behat/completion_manual.feature diff --git a/lib.php b/lib.php index 6e5b3bc1..099c24ba 100644 --- a/lib.php +++ b/lib.php @@ -776,8 +776,7 @@ function ratingallocate_get_coursemodule_info($coursemodule) { * @param cm_info|stdClass $cm object with fields ->completion and ->customdata['customcompletionrules'] * @return array $descriptions the array of descriptions for the custom rules. */ -function mod_ratingallocate_get_completion_active_rule_descriptions($cm) -{ +function mod_ratingallocate_get_completion_active_rule_descriptions($cm) { // Values will be present in cm_info, and we assume these are up to date. if (empty($cm->customdata['customcompletionrules']) || $cm->completion != COMPLETION_TRACKING_AUTOMATIC) { return []; diff --git a/mod_form.php b/mod_form.php index 26c724c4..87cc9602 100644 --- a/mod_form.php +++ b/mod_form.php @@ -310,11 +310,11 @@ public function add_completion_rules() { $mform->addElement('advcheckbox', $this->get_suffixed_name('vote'), ' ', get_string('completionvote', RATINGALLOCATE_MOD_NAME)); $mform->addElement('advcheckbox', $this->get_suffixed_name('allocation'), ' ', get_string('completionallocation', RATINGALLOCATE_MOD_NAME)); - //Set default to not checked. + // Set default to not checked. $mform->setDefault($this->get_suffixed_name('vote'), 0); $mform->setDefault($this->get_suffixed_name('allocation'), 0); - //Add help buttons. + // Add help buttons. $mform->addHelpButton($this->get_suffixed_name('vote'), 'completionvote', RATINGALLOCATE_MOD_NAME); $mform->addHelpButton($this->get_suffixed_name('allocation'), 'completionallocation', RATINGALLOCATE_MOD_NAME); diff --git a/tests/behat/completion_condition_allocation.feature b/tests/behat/completion_condition_allocation.feature new file mode 100644 index 00000000..b03cebf0 --- /dev/null +++ b/tests/behat/completion_condition_allocation.feature @@ -0,0 +1,43 @@ +@mod @mod_ratingallocate @core_completion +Feature: Set a ratingallocate activity marked as completed when a user has been allocated + In order to ensure a student has been allocated + As a teacher + I need to set the ratingallocate to complete when the student has an allocation + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | student1 | Student | 1 | student1@example.com | + | student2 | Student | 2 | student2@example.com | + | teacher1 | Teacher | 1 | teacher1@example.com | + And the following "courses" exist: + | fullname | shortname | category | enablecompletion | + | Course 1 | C1 | 0 | 1 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + | student2 | C1 | student | + And the following "activities" exist: + | activity | course | idnumber | name | completion | + | ratingallocate | C1 | ra1 | My Fair Allocation | 3 | + And the following choices exist: + | title | explanation | maxsize | ratingallocate | + | C1 | Test | 1 | My Fair Allocation | + | C2 | Test | 0 | My Fair Allocation | + And the following ratings exist: + | choice | user | rating | + | C1 | student1 | 1 | + | C1 | student2 | 0 | + | C2 | student1 | 0 | + | C2 | student2 | 0 | + And I run the scheduled task "mod_ratingallocate\task\cron_task" + + @javascript + Scenario: User completes ratingallocate only if they have been allocated + When I log in as "teacher1" + And I am on "Course 1" course homepage + And I navigate to "Reports" in current page administration + And I click on "Activity completion" "link" + Then "Completed" "icon" should exist in the "Student 1" "table_row" + And "Completed" "icon" should not exist in the "Student 2" "table_row" diff --git a/tests/behat/completion_condition_vote.feature b/tests/behat/completion_condition_vote.feature new file mode 100644 index 00000000..24e9ba33 --- /dev/null +++ b/tests/behat/completion_condition_vote.feature @@ -0,0 +1,44 @@ +@mod @mod_ratingallocate @core_completion +Feature: Set a ratingallocate activity marked as completed when a user submits a vote + In order to ensure a student has voted in the activity + As a teacher + I need to set the ratingallocate to complete when the student has voted + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | student1 | Student | 1 | student1@example.com | + | student2 | Student | 2 | student2@example.com | + | teacher1 | Teacher | 1 | teacher1@example.com | + And the following "courses" exist: + | fullname | shortname | category | enablecompletion | + | Course 1 | C1 | 0 | 1 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + | student2 | C1 | student | + And the following "activities" exist: + | activity | course | idnumber | name | completion | + | ratingallocate | C1 | ra1 | My Fair Allocation | 2 | + And I log in as "teacher1" + And I am on the "My Fair Allocation" "ratingallocate activity" page + And I press "Edit Choices" + And I add a new choice with the values: + | title | My first choice | + | Description (optional) | Test 1 | + | maxsize | 2 | + + @javascript + Scenario: User completes ratingallocate only if they voted + When I log in as "student1" + And I am on the "My Fair Allocation" "ratingallocate activity" page + And I press "Edit Rating" + And I press "Save changes" + And I log out + And I log in as "teacher1" + And I am on "Course 1" course homepage + And I navigate to "Reports" in current page administration + And I click on "Activity completion" "link" + Then "Completed" "icon" should exist in the "Student 1" "table_row" + And "Completed" "icon" should not exist in the "Student 2" "table_row" diff --git a/tests/behat/completion_manual.feature b/tests/behat/completion_manual.feature new file mode 100644 index 00000000..057ecd60 --- /dev/null +++ b/tests/behat/completion_manual.feature @@ -0,0 +1,39 @@ +@mod @mod_ratingallocate @core_completion +Feature: Manually mark a ratingallocate activity as completed + In order to meet manual ratingallocate completion requirements + As a student + I need to be able to view and modify my ratingallocate manual completion status + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | student1 | Student | 1 | student1@example.com | + | teacher1 | Teacher | 1 | teacher1@example.com | + And the following "courses" exist: + | fullname | shortname | category | enablecompletion | + | Course 1 | C1 | 0 | 1 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + And the following "activities" exist: + | activity | course | idnumber | name | completion | + | ratingallocate | C1 | ra1 | My Fair Allocation | 1 | + And I log in as "teacher1" + And I am on the "My Fair Allocation" "ratingallocate activity" page + And I press "Edit Choices" + And I add a new choice with the values: + | title | My first choice | + | Description (optional) | Test 1 | + | maxsize | 2 | + + @javascript + Scenario: Use manual completion + Given I am on the "My Fair Allocation" "ratingallocate activity" page logged in as teacher1 + And the manual completion button for "My Fair Allocation" should be disabled + And I log out + # Student view. + When I am on the "My Fair Allocation" "ratingallocate activity" page logged in as student1 + Then the manual completion button of "My Fair Allocation" is displayed as "Mark as done" + And I toggle the manual completion state of "My Fair Allocation" + And the manual completion button of "My Fair Allocation" is displayed as "Done" From 3095c7db87d11f5a880c902ba3a6efc51d93409a Mon Sep 17 00:00:00 2001 From: Irina Hoppe Date: Mon, 22 Jan 2024 10:00:08 +0100 Subject: [PATCH 4/8] modified behat backgrounds according to ratingallocate settings form --- tests/behat/completion_condition_allocation.feature | 9 +++++++-- tests/behat/completion_condition_vote.feature | 9 +++++++-- tests/behat/completion_manual.feature | 8 ++++++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/tests/behat/completion_condition_allocation.feature b/tests/behat/completion_condition_allocation.feature index b03cebf0..b389e6a6 100644 --- a/tests/behat/completion_condition_allocation.feature +++ b/tests/behat/completion_condition_allocation.feature @@ -19,8 +19,13 @@ Feature: Set a ratingallocate activity marked as completed when a user has been | student1 | C1 | student | | student2 | C1 | student | And the following "activities" exist: - | activity | course | idnumber | name | completion | - | ratingallocate | C1 | ra1 | My Fair Allocation | 3 | + | activity | course | idnumber | name | + | ratingallocate | C1 | ra1 | My Fair Allocation | + And I am on the "My Fair Allocation" "ratingallocate activity editing" page + And I expand "Completion conditions" node + And I select the radio "id_completion_2" + And I check "id_completionallocation" + And I press "id_submitbutton" And the following choices exist: | title | explanation | maxsize | ratingallocate | | C1 | Test | 1 | My Fair Allocation | diff --git a/tests/behat/completion_condition_vote.feature b/tests/behat/completion_condition_vote.feature index 24e9ba33..af1b27fb 100644 --- a/tests/behat/completion_condition_vote.feature +++ b/tests/behat/completion_condition_vote.feature @@ -19,8 +19,8 @@ Feature: Set a ratingallocate activity marked as completed when a user submits a | student1 | C1 | student | | student2 | C1 | student | And the following "activities" exist: - | activity | course | idnumber | name | completion | - | ratingallocate | C1 | ra1 | My Fair Allocation | 2 | + | activity | course | idnumber | name | + | ratingallocate | C1 | ra1 | My Fair Allocation | And I log in as "teacher1" And I am on the "My Fair Allocation" "ratingallocate activity" page And I press "Edit Choices" @@ -28,6 +28,11 @@ Feature: Set a ratingallocate activity marked as completed when a user submits a | title | My first choice | | Description (optional) | Test 1 | | maxsize | 2 | + And I am on the "My Fair Allocation" "ratingallocate activity editing" page + And I expand "Completion conditions" node + And I select the radio "id_completion_2" + And I check "id_completionvote" + And I press "id_submitbutton" @javascript Scenario: User completes ratingallocate only if they voted diff --git a/tests/behat/completion_manual.feature b/tests/behat/completion_manual.feature index 057ecd60..7698507d 100644 --- a/tests/behat/completion_manual.feature +++ b/tests/behat/completion_manual.feature @@ -17,8 +17,8 @@ Feature: Manually mark a ratingallocate activity as completed | teacher1 | C1 | editingteacher | | student1 | C1 | student | And the following "activities" exist: - | activity | course | idnumber | name | completion | - | ratingallocate | C1 | ra1 | My Fair Allocation | 1 | + | activity | course | idnumber | name | + | ratingallocate | C1 | ra1 | My Fair Allocation | And I log in as "teacher1" And I am on the "My Fair Allocation" "ratingallocate activity" page And I press "Edit Choices" @@ -26,6 +26,10 @@ Feature: Manually mark a ratingallocate activity as completed | title | My first choice | | Description (optional) | Test 1 | | maxsize | 2 | + And I am on the "My Fair Allocation" "ratingallocate activity editing" page + And I expand "Completion conditions" node + And I select the radio "id_completion_1" + And I press "id_submitbutton" @javascript Scenario: Use manual completion From 440d18150d974525b833b2c013cc38892d965677 Mon Sep 17 00:00:00 2001 From: Irina Hoppe Date: Fri, 2 Feb 2024 09:48:14 +0100 Subject: [PATCH 5/8] modified behat and phpunit tests --- mod_form.php | 1 - tests/behat/completion_condition_allocation.feature | 9 ++------- tests/behat/completion_condition_vote.feature | 9 ++------- tests/behat/completion_manual.feature | 8 ++------ tests/mod_generator_test.php | 4 +++- 5 files changed, 9 insertions(+), 22 deletions(-) diff --git a/mod_form.php b/mod_form.php index 87cc9602..174ec6ca 100644 --- a/mod_form.php +++ b/mod_form.php @@ -305,7 +305,6 @@ private function get_settingsfield_identifier($strategy, $key) { */ public function add_completion_rules() { $mform = $this->_form; - $suffix = $this->get_suffix(); $mform->addElement('advcheckbox', $this->get_suffixed_name('vote'), ' ', get_string('completionvote', RATINGALLOCATE_MOD_NAME)); $mform->addElement('advcheckbox', $this->get_suffixed_name('allocation'), ' ', get_string('completionallocation', RATINGALLOCATE_MOD_NAME)); diff --git a/tests/behat/completion_condition_allocation.feature b/tests/behat/completion_condition_allocation.feature index b389e6a6..75f90bd7 100644 --- a/tests/behat/completion_condition_allocation.feature +++ b/tests/behat/completion_condition_allocation.feature @@ -19,13 +19,8 @@ Feature: Set a ratingallocate activity marked as completed when a user has been | student1 | C1 | student | | student2 | C1 | student | And the following "activities" exist: - | activity | course | idnumber | name | - | ratingallocate | C1 | ra1 | My Fair Allocation | - And I am on the "My Fair Allocation" "ratingallocate activity editing" page - And I expand "Completion conditions" node - And I select the radio "id_completion_2" - And I check "id_completionallocation" - And I press "id_submitbutton" + | activity | course | idnumber | name | completion | completionallocation | + | ratingallocate | C1 | ra1 | My Fair Allocation | 2 | 1 | And the following choices exist: | title | explanation | maxsize | ratingallocate | | C1 | Test | 1 | My Fair Allocation | diff --git a/tests/behat/completion_condition_vote.feature b/tests/behat/completion_condition_vote.feature index af1b27fb..82a376b8 100644 --- a/tests/behat/completion_condition_vote.feature +++ b/tests/behat/completion_condition_vote.feature @@ -19,8 +19,8 @@ Feature: Set a ratingallocate activity marked as completed when a user submits a | student1 | C1 | student | | student2 | C1 | student | And the following "activities" exist: - | activity | course | idnumber | name | - | ratingallocate | C1 | ra1 | My Fair Allocation | + | activity | course | idnumber | name | completion | completionvote | + | ratingallocate | C1 | ra1 | My Fair Allocation | 2 | 1 | And I log in as "teacher1" And I am on the "My Fair Allocation" "ratingallocate activity" page And I press "Edit Choices" @@ -28,11 +28,6 @@ Feature: Set a ratingallocate activity marked as completed when a user submits a | title | My first choice | | Description (optional) | Test 1 | | maxsize | 2 | - And I am on the "My Fair Allocation" "ratingallocate activity editing" page - And I expand "Completion conditions" node - And I select the radio "id_completion_2" - And I check "id_completionvote" - And I press "id_submitbutton" @javascript Scenario: User completes ratingallocate only if they voted diff --git a/tests/behat/completion_manual.feature b/tests/behat/completion_manual.feature index 7698507d..057ecd60 100644 --- a/tests/behat/completion_manual.feature +++ b/tests/behat/completion_manual.feature @@ -17,8 +17,8 @@ Feature: Manually mark a ratingallocate activity as completed | teacher1 | C1 | editingteacher | | student1 | C1 | student | And the following "activities" exist: - | activity | course | idnumber | name | - | ratingallocate | C1 | ra1 | My Fair Allocation | + | activity | course | idnumber | name | completion | + | ratingallocate | C1 | ra1 | My Fair Allocation | 1 | And I log in as "teacher1" And I am on the "My Fair Allocation" "ratingallocate activity" page And I press "Edit Choices" @@ -26,10 +26,6 @@ Feature: Manually mark a ratingallocate activity as completed | title | My first choice | | Description (optional) | Test 1 | | maxsize | 2 | - And I am on the "My Fair Allocation" "ratingallocate activity editing" page - And I expand "Completion conditions" node - And I select the radio "id_completion_1" - And I press "id_submitbutton" @javascript Scenario: Use manual completion diff --git a/tests/mod_generator_test.php b/tests/mod_generator_test.php index f0331ce4..f4b16450 100644 --- a/tests/mod_generator_test.php +++ b/tests/mod_generator_test.php @@ -71,7 +71,9 @@ public function test_create_instance() { 'notificationsend' => '0', 'algorithmstarttime' => null, 'algorithmstatus' => '0', - 'runalgorithmbycron' => '1' + 'runalgorithmbycron' => '1', + 'completionvote' => '0', + 'completionallocation' => '0' ); $this->assertEquals(json_decode(json_encode($expectedvaluesdb, false)), reset($records)); From 15968eee10745a506321c4e0c065232f7ee3f144 Mon Sep 17 00:00:00 2001 From: Irina Hoppe Date: Fri, 2 Feb 2024 10:19:53 +0100 Subject: [PATCH 6/8] fixed behat test for completionallocation --- tests/behat/completion_condition_allocation.feature | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/behat/completion_condition_allocation.feature b/tests/behat/completion_condition_allocation.feature index 75f90bd7..e083291f 100644 --- a/tests/behat/completion_condition_allocation.feature +++ b/tests/behat/completion_condition_allocation.feature @@ -31,7 +31,12 @@ Feature: Set a ratingallocate activity marked as completed when a user has been | C1 | student2 | 0 | | C2 | student1 | 0 | | C2 | student2 | 0 | + And I log in as "teacher1" + And I am on the "My Fair Allocation" "ratingallocate activity editing" page And I run the scheduled task "mod_ratingallocate\task\cron_task" + And I am on the "My Fair Allocation" "ratingallocate activity" page + And I press "Publish Allocation" + And I log out @javascript Scenario: User completes ratingallocate only if they have been allocated From 64341f8bf3b6c5bcfa39cd6b6dfac522dee5ae04 Mon Sep 17 00:00:00 2001 From: Irina Hoppe Date: Fri, 2 Feb 2024 10:37:45 +0100 Subject: [PATCH 7/8] fixed change of completion status for manual allocation and allocation by cron --- locallib.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/locallib.php b/locallib.php index ca57d88e..6a719b95 100644 --- a/locallib.php +++ b/locallib.php @@ -287,6 +287,13 @@ private function process_action_start_distribution() { \core\output\notification::NOTIFY_SUCCESS); } } + $raters = $this->get_raters_in_course(); + $completion = new completion_info($this->course); + if ($completion->is_enabled($this->coursemodule)) { + foreach ($raters as $rater) { + $completion->update_state($this->coursemodule, COMPLETION_UNKNOWN, $rater->id); + } + } redirect(new moodle_url('/mod/ratingallocate/view.php', array('id' => $this->coursemodule->id))); return; @@ -664,6 +671,13 @@ private function process_action_manual_allocation() { array('id' => $this->coursemodule->id, 'action' => ACTION_MANUAL_ALLOCATION))); } } + $raters = $this->get_raters_in_course(); + $completion = new completion_info($this->course); + if ($completion->is_enabled($this->coursemodule)) { + foreach ($raters as $rater) { + $completion->update_state($this->coursemodule, COMPLETION_UNKNOWN, $rater->id); + } + } } $output .= $OUTPUT->heading(get_string('manual_allocation', RATINGALLOCATE_MOD_NAME), 2); From 6cf36251fcb3f8874c2699275eb34fd7b3830c2e Mon Sep 17 00:00:00 2001 From: Irina Hoppe Date: Fri, 2 Feb 2024 13:39:23 +0100 Subject: [PATCH 8/8] changed behat test for completionallocation --- tests/behat/completion_condition_allocation.feature | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/behat/completion_condition_allocation.feature b/tests/behat/completion_condition_allocation.feature index e083291f..66a5eb8e 100644 --- a/tests/behat/completion_condition_allocation.feature +++ b/tests/behat/completion_condition_allocation.feature @@ -32,9 +32,8 @@ Feature: Set a ratingallocate activity marked as completed when a user has been | C2 | student1 | 0 | | C2 | student2 | 0 | And I log in as "teacher1" - And I am on the "My Fair Allocation" "ratingallocate activity editing" page - And I run the scheduled task "mod_ratingallocate\task\cron_task" And I am on the "My Fair Allocation" "ratingallocate activity" page + And I run the scheduled task "mod_ratingallocate\task\cron_task" And I press "Publish Allocation" And I log out