-
Improvement
-
Resolution: Fixed
-
Minor
-
3.7
-
MOODLE_37_STABLE
-
MOODLE_37_STABLE
-
MDL-65257_master -
The original mock-up includes a user and group image (see attached image). I have been trying to utilise MDL-36754 with no luck. The reason -
https://github.com/moodle/moodle/blob/master/lib/moodlelib.php#L3186 will prevent a token from working if it is created with a userid of 0 (which will happen in a scheduled task). I don't see how it is possible from a scheduled task to avoid this. Whenever you call moodle_url::make_pluginfile_url() and specify you want to create a token it calls https://github.com/moodle/moodle/blob/master/lib/weblib.php#L789 which then creates a token using $USER->id if it doesn't exist (see https://github.com/moodle/moodle/blob/master/lib/moodlelib.php#L3264).
The user already exists, but there is no token, so when creating the URL for them (see snippets below) it automatically creates a token (with userid 0 since its from a scheduled task where $USER->id is 0 (there is no option to pass a user id)). Frankly, I am not sure what is happening in forum to prevent this, but it is late and I have been staring at code for a while. My guess is it sets up the tokens before it generates the URLs (which seems counter-intuitive looking at the API which leans towards creating the token on the fly). Perhaps what I am trying to achieve is not possible. I could hack the $USER->id but obviously that is not an acceptable option.
$group = new \stdClass(); |
$group->id = $conversation->groupid; |
$group->picture = $conversation->picture; |
$group->hidepicture = $conversation->hidepicture; |
$group->courseid = $conversation->courseid; |
$grouppictureurl = $renderer->image_url('g/g1')->out(false); // Default image. |
if ($url = get_group_picture_url($group, $group->courseid, false, true)) { |
$grouppictureurl = $url->out(false); |
}
|
$user = new \stdClass(); |
$usernamefields = explode(',' ,\user_picture::fields()); |
foreach ($usernamefields as $usernamefield) { |
$user->$usernamefield = $message->$usernamefield; |
}
|
$user->id = $message->useridfrom; |
|
$userpicture = new \user_picture($user); |
$userpicture->includetoken = true; |
|
$userpictureurl = $userpicture->get_url($PAGE)->out(false); |