Page->update() strange problem

hello, i have encountered a strange phenomenon.

i have a flag button, onclick userid gets written into a field of an ‘exercise’ page ‘flag_$color’ like so:

    $userId = $_POST['userId'];
    $exerciseId = $_POST['exerciseId'];
    $color = strtolower($_POST['color']);

    // Get the Kirby CMS page object based on the exercise ID
    $page = page($exerciseId);

    

    if ($page) {
        $fieldName = 'flag_' . strtolower($color);
        $existingFlags = $page->$fieldName();
    
      
        
        // Debug statement
    error_log('User ID: ' . $userId);
    error_log('Exercise ID: ' . $exerciseId);
    error_log('Color: ' . $color);
    error_log('Existing Flags: ' . $existingFlags);

        // Check if the user ID is already present in the field
        if (strpos($existingFlags, $userId) === false) { // Use strict comparison to ensure correct behavior
            // Update the field by appending the user ID with a comma
            $existingFlags = trim($existingFlags);
            if ($existingFlags !== '') {
                $updatedFlags = $existingFlags . ', ' . $userId;
            } else {
                $updatedFlags = $userId;
            }
            $page->update([$fieldName => $updatedFlags]);

             // Debug statement
            error_log('Field Name: ' . $fieldName);
            error_log('Updated Flags: ' . $updatedFlags);

this works great.

now the problem is that after the ‘flag_green’ field was correctly updated another field named ‘schueler’ gets completely reset!? (the schueler field w checkboxes has the purpose to select students to which the exercise is displayed.)

before:

----

Flag-green:

----

Schueler: Lee, Ole, Christoph, Felix, Colin, Olli

----

after:

----

Flag-green: sO4SaRWe

----

Schueler: 

----

now the really strange part is:
that only happens when logged in as role student! tested w other students acc aswell.

as admin, everything works and field ‘schueler’ stays intact.

nowhere in the whole flag-part i have a role conditional. nowhere i write to the field ‘schueler’…this is the only page->update() function i put in.

the blueprint:

                schueler:
                    type: checkboxes
                    options:
                      type: query
                      query:  kirby.users.filterBy('role', 'student').filterBy('teacher', kirby.user.name).sortBy('name', 'asc')
                      text: "{{ item.name }}"
                      value: "{{ item.name }}"
                flag_green:
                    label: Green Flags
                    type: text
                flag_red:
                    label: Red Flags
                    type: text

from the error logs

[Tue Jun 13 14:03:20.628094 2023] [php:notice] [pid 6328:tid 1888] [client ::1:50558] User ID: sO4SaRWe, referer: http://localhost/ue/rich/?student=Lee
[Tue Jun 13 14:03:20.628094 2023] [php:notice] [pid 6328:tid 1888] [client ::1:50558] Exercise ID: rich/categories/basics/asymp, referer: http://localhost/ue/rich/?student=Lee
[Tue Jun 13 14:03:20.628094 2023] [php:notice] [pid 6328:tid 1888] [client ::1:50558] Color: green, referer: http://localhost/ue/rich/?student=Lee
[Tue Jun 13 14:03:20.628094 2023] [php:notice] [pid 6328:tid 1888] [client ::1:50558] Existing Flags: , referer: http://localhost/ue/rich/?student=Lee
[Tue Jun 13 14:03:20.728271 2023] [php:notice] [pid 6328:tid 1888] [client ::1:50558] Field Name: flag_green, referer: http://localhost/ue/rich/?student=Lee
[Tue Jun 13 14:03:20.728271 2023] [php:notice] [pid 6328:tid 1888] [client ::1:50558] Updated Flags: sO4SaRWe, referer: http://localhost/ue/rich/?student=Lee

i really have no clue where to investigate further. any help very much appreciated!

regards
fusi

My guess is that this query doesn’t validate, don’t know what user you are using here to do the update

hi pixelijn,

thanks for your post. i think you are on the right track. in the blueprint, that is only accessible for teachers since students are not allowed in the panel, i want to have checkboxes which are students of the currently logged-in user, which can only be a teacher, since only they can access the panel. thats the plan so far.

of course this query

query: kirby.users.filterBy('role', 'student').filterBy('teacher', kirby.user.name).sortBy('name', 'asc')

returns nothing, when a student is logged in. this would also hint to the fact that the field gets nulled.

but i cant square it with the fact that a student never is in the panel, still somehow this query gets executed as if the student was somehow logged in the panel and the query gets executed and nulls the field and resets the checkboxes.

how can that be? can i improve the query? i need to find all the students of the teacher that is in the panel on the exercise page, to have checkboxes to select some students. the students have a field called ‘teacher’ with the teachers name.

tnx and regards
fusi

p.s.: ahh i think somehow $page->update() somehow updates not only the field but the whole set of fields… so when it updates and the student is logged in, whose students are null, it writes that into the txt. how can i prevent this?

That’s what I meant, maybe the stored values don’t validate for the currently logged-in user and are therefore set to null. But that needs to be verified.

What is the purpose of the schueler field in this context?

the blueprint is of an ‘exercise page’. teacher creates an exercise and then selects students to which the exercise is displayed.

…in the meantime i tried to grab the old schueler field values and pass them to the update function, but it seems that the blueprint query and its reset comes afterwards and also deletes newly written the schueler values.

maybe i could try to make a draft page with a structure yaml and store the the flags there (color/user_id/exercise_id)… so far so not good…:slight_smile:

Hm, I create a simple test case (with the only difference that I used a users field in the test page), then logged in as student role updated the test page from the frontend, but the schueler field values were not deleted.

What’s your Kirby version? Single or multi-language?

the version is 3.9.4. i have a plugin installed called bouncer.
i think its single language, on the system page nothing is mentioned.

it’s really strange.

couldnt resolve the issue and moved on. tnx for your help anyways!