[V2.6] - Invalid User error on Unsubscribe

[V2.6] - Invalid User error on Unsubscribe

by Hugh Edwards -
Number of replies: 5

Hello. 

A puzzling one this. On the forum emails that are being sent out to all my users, there are the two usual links at the bottom: Unsubscribe from this forum and Unsubscribe from all forums.

Unsubscribe from this forum linkhttps://www.gravyforthebrain.com/moodle/mod/forum/subscribe.php?id=6

Unsubscribe for all forums linkhttps://www.gravyforthebrain.com/moodle/mod/forum/unsubscribeall.php

The first link works fine and if you're not logged in it presents you with the login screen. However, if you click the unsubscribe all link, and you're not logged in, you get:

"Invalid User Error." ... and no option to login. 

Any ideas how to fix this? It's currently a v bad UX for my users...

smile Hugh. 

Average of ratings: -
In reply to Hugh Edwards

Re: [V2.6] - Invalid User error on Unsubscribe

by Hugh Edwards -

Any ideas anyone? smile 

In reply to Hugh Edwards

Re: [V2.6] - Invalid User error on Unsubscribe

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Somewhat surprisingly - it looks like a bug. 

This line (which requires a valid $USER global)...

$PAGE->set_context(context_user::instance($USER->id));

is called BEFORE this line...

require_login(null, false);

which checks if you are logged in. The weird thing is that I'm looking at this in 2.4 code. I find it hard to believe this has been missed. Off to investigate. 

 

EDIT:

I have found another thread reporting the same problem but no bug report, so MDL-43155

Average of ratings: Useful (2)
In reply to Howard Miller

Re: [V2.6] - Invalid User error on Unsubscribe

by Hugh Edwards -

Hi Howard, Thanks for that! Can you tell me which file so I can edit it? If all I need to do is swap the lines round, then I can do that as a workaround...

smile

In reply to Hugh Edwards

Re: [V2.6] - Invalid User error on Unsubscribe

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

mod/forum/unsubscribeall.php wink

I haven't tried it, but move the require_login call to before the line that references $USER. That should do it. 

In reply to Howard Miller

Re: [V2.6] - Invalid User error on Unsubscribe

by Hugh Edwards -

Hi Howard, 

Thank you for that - it does indeed work - all tested here. So For everyone else, here is the amended code as a bug-fix:

File: mod/forum/unsubscribeall.php

Code:

<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>;.

/**
* @package mod-forum
* @copyright 2008 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once("../../config.php");
require_once("lib.php");

$confirm = optional_param('confirm', false, PARAM_BOOL);

$PAGE->set_url('/mod/forum/unsubscribeall.php');

// Do not autologin guest. Only proper users can have forum subscriptions.
require_login(null, false);

$PAGE->set_context(context_user::instance($USER->id));

 

$return = $CFG->wwwroot.'/';

if (isguestuser()) {
redirect($return);
}

$strunsubscribeall = get_string('unsubscribeall', 'forum');
$PAGE->navbar->add(get_string('modulename', 'forum'));
$PAGE->navbar->add($strunsubscribeall);
$PAGE->set_title($strunsubscribeall);
$PAGE->set_heading(format_string($COURSE->fullname));
echo $OUTPUT->header();
echo $OUTPUT->heading($strunsubscribeall);

if (data_submitted() and $confirm and confirm_sesskey()) {
$forums = forum_get_optional_subscribed_forums();

foreach($forums as $forum) {
forum_unsubscribe($USER->id, $forum->id);
}
$DB->set_field('user', 'autosubscribe', 0, array('id'=>$USER->id));

echo $OUTPUT->box(get_string('unsubscribealldone', 'forum'));
echo $OUTPUT->continue_button($return);
echo $OUTPUT->footer();
die;

} else {
$a = count(forum_get_optional_subscribed_forums());

if ($a) {
$msg = get_string('unsubscribeallconfirm', 'forum', $a);
echo $OUTPUT->confirm($msg, new moodle_url('unsubscribeall.php', array('confirm'=>1)), $return);
echo $OUTPUT->footer();
die;

} else {
echo $OUTPUT->box(get_string('unsubscribeallempty', 'forum'));
echo $OUTPUT->continue_button($return);
echo $OUTPUT->footer();
die;
}
}