Has anyone figured out how to have section number and Recitation automatically created from Canvas. I am running WW 2.19 and using LTI 1.3. I had the Canvas team setup some custom fields but can not figure out how to modify users with the information.
In the LTI custom fields we set.
canvas_course_id=$Canvas.course.id
canvas_section_id=$Canvas.course.sectionIds
When looking at the debug logs I am seeing that information passed to webwork.
"https://purl.imsglobal.org/spec/lti/claim/custom" => {
"canvas_course_id" => ######,
"canvas_section_id" => ######,
},
My Modify user sub routine is set like this.
$LTI{v1p3}{modify_user} = sub {
my $self = shift;
my $user = shift;
# Access custom fields from the LTI claim
my $custom_fields = $self->{custom};
# Debugging statements to check values
warn "Canvas Course ID: " . ($custom_fields->{'canvas_course_id'} // 'undefined');
warn "Canvas Section ID: " . ($custom_fields->{'canvas_section_id'} // 'undefined');
# Map Canvas custom fields to WeBWorK user fields
$user->{section} = $custom_fields->{'canvas_course_id'};
$user->{recitation} = $custom_fields->{'canvas_section_id'};
};
But no matter what I do those keep coming back as undefined or empty. Any help is appreciated.
You will need to extract that information from the JWT claims. Those are saved in the controller stash. You can access that in the "modify_user" method with $self->{c}->stash->{lti_jwt_claims}. The particular value your are looking for should be $self->{c}->stash->{lti_jwt_claims}{'https://purl.imsglobal.org/spec/lti/claim/custom'}{canvas_section_id}.
$user->{section} = $self->{c}->stash->{lti_jwt_claims}{'https://purl.imsglobal.org/spec/lti/claim/custom'}{canvas_section_id};
I don't understand why this does not work in 2.19 anymore.
I modified it to
$LTI{v1p1}{modify_user} = sub {
my $self = shift;
my $user = shift;
my $r = $self->{r};
my @course_id = split /-/, $r->param("context_title");
$user->{"section"} = $course_id[2];
};
The error message is
Can't call method "param" on an undefined value at (eval 1302) line 40.
The debug info shows context_title => MATH-4390-05-Fall2023The configuration file suggests to use context_id without parameter: my @course_id = split /-/, $self->{context_id}; but that is just a long string in my case without section number in it.
It does work: $user->{"section"} = $self->{context_id}; but it is useless in my case.
If I try to use $user->{"section"} = $self->{context_title}; I get empty string.
Aren't both context_id and context_title just parameters?
How is it that context_title shows up in the parameters but I cannot get its value?
The "r" key of the $self object no longer exists. That was the modPerl Apache::Request object. WeBWorK 2.18 and 2.19 do not use modPerl anymore. Now there is a Mojolicious::Controller object. You can access that in the "c" key of the $self object. So use $self->{c} instead. The Mojolicious::Controller object has a "param" method that works the same as before. So the rest of your code should work.
Great! Thanks!