MDL-77789 mod_h5pactivity: Only remove the current user state content

When a new attempt is created, the previous state content is removed.
There was a bug which was removing all the content data of a given
activity for all the users (instead of deleting only the content data
for the current user).
This commit is contained in:
Sara Arjona 2023-03-30 12:22:18 +02:00
parent f7a8df253b
commit de73be254f
3 changed files with 28 additions and 7 deletions

View File

@ -87,7 +87,7 @@ class attempt {
// Remove any xAPI State associated to this attempt.
$context = \context_module::instance($cm->id);
$xapihandler = handler::create('mod_h5pactivity');
$xapihandler->wipe_states($context->id);
$xapihandler->wipe_states($context->id, $user->id);
return new attempt($record);
}

View File

@ -8,6 +8,7 @@ Feature: Users can save the current state of an H5P activity
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 "course" exists:
| fullname | Course 1 |
@ -15,6 +16,7 @@ Feature: Users can save the current state of an H5P activity
And the following "course enrolments" exist:
| user | course | role |
| student1 | C1 | student |
| student2 | C1 | student |
| teacher1 | C1 | editingteacher |
And the following "permission overrides" exist:
| capability | permission | role | contextlevel | reference |
@ -126,10 +128,14 @@ Feature: Users can save the current state of an H5P activity
Scenario: Content state is removed when an attempt is created
Given the following config values are set as admin:
| enablesavestate | 1 | mod_h5pactivity|
# Save state content for student2, to check this data is not removed when student1 finishes their attempt.
And I am on the "Awesome H5P package" "h5pactivity activity" page logged in as student2
When I switch to "h5p-player" class iframe
And I switch to "h5p-iframe" class iframe
And I set the field with xpath "//input[contains(@aria-label,\"Blank input 1 of 4\")]" to "Vallhonesta"
# Create an attempt for student1.
And I am on the "Awesome H5P package" "h5pactivity activity" page logged in as student1
# Check there are no attempts.
And I should not see "Attempts report"
# Create an attempt.
When I switch to "h5p-player" class iframe
And I switch to "h5p-iframe" class iframe
And I set the field with xpath "//input[contains(@aria-label,\"Blank input 1 of 4\")]" to "Narnia"
@ -141,3 +147,9 @@ Feature: Users can save the current state of an H5P activity
And I switch to "h5p-player" class iframe
And I switch to "h5p-iframe" class iframe
And the field with xpath "//input[contains(@aria-label,\"Blank input 1 of 4\")]" does not match value "Narnia"
And I switch to the main frame
# Check the state content for student2 is still there.
And I am on the "Awesome H5P package" "h5pactivity activity" page logged in as student2
And I switch to "h5p-player" class iframe
And I switch to "h5p-iframe" class iframe
And the field with xpath "//input[contains(@aria-label,\"Blank input 1 of 4\")]" matches value "Vallhonesta"

View File

@ -68,22 +68,31 @@ class attempt_test extends \advanced_testcase {
global $CFG, $DB;
require_once($CFG->dirroot.'/lib/xapi/tests/helper.php');
list($cm, $student) = $this->generate_testing_scenario();
[$cm, $student, $course] = $this->generate_testing_scenario();
$student2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Save the current state for this activity (before creating the first attempt).
// Save the current state for this activity for student1 and student2 (before creating the first attempt).
$manager = manager::create_from_coursemodule($cm);
$this->setUser($student2);
test_helper::create_state([
'activity' => item_activity::create_from_id($manager->get_context()->id),
'component' => 'mod_h5pactivity',
], true);
$this->assertEquals(1, $DB->count_records('xapi_states'));
$this->setUser($student);
test_helper::create_state([
'activity' => item_activity::create_from_id($manager->get_context()->id),
'component' => 'mod_h5pactivity',
], true);
$this->assertEquals(2, $DB->count_records('xapi_states'));
// Create first attempt.
$attempt = attempt::new_attempt($student, $cm);
$this->assertEquals($student->id, $attempt->get_userid());
$this->assertEquals($cm->instance, $attempt->get_h5pactivityid());
$this->assertEquals(1, $attempt->get_attempt());
$this->assertEquals(0, $DB->count_records('xapi_states'));
$this->assertEquals(1, $DB->count_records('xapi_states'));
$this->assertEquals(0, $DB->count_records('xapi_states', ['userid' => $student->id]));
// Create a second attempt.
$attempt = attempt::new_attempt($student, $cm);