Authenticaed user role hack, to show "Enrol in this course" link

Authenticaed user role hack, to show "Enrol in this course" link

by Alex Parsy -
Number of replies: 6

I thought there should be a way of doing this without modifying the php code, but I couldn't figure it out even after spending a lot of time on the forums....

I had disabled guess log in and overrode roles to allow "Authenticated User" to view course. I want the registered but non-course enroled users to view the content of a course.

So after doing so, the "Authenticated user" can view course content but the link to "Enrol for this course" does not appear on the left admin block. I simply can't figure this out.

If I allow guess login and enable "course view" capability for guess on that course, the guess user CAN SEE the "enrol for this course" link.

Could someone tell me if there is a way to do this without hacking the PHP code ?


anyways, my hack was to change the block_admin.php to check for "moodle/lagacy:user" capability as well when presenting the "enrol for this course" link. see further below.

any help appreciated as to why I could not simply get this to work without hacking the code.

if (empty($course->metacourse) && ($course->id!==SITEID)) {
if (has_capability('moodle/legacy:guest', $context, NULL, false) )
{ // Are a guest now
$this->content->items[]='<a href="enrol.php?id='.$this->instance->pageid.'">'.get_string('enrolme', '', format_string($course->shortname)).'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/user.gif" class="icon" alt="" />';
} else if (has_capability('moodle/role:unassignself', $context, NULL, false) and get_user_roles($context, $USER->id, false)) { // Have some role
$this->content->items[]='<a href="unenrol.php?id='.$this->instance->pageid.'">'.get_string('unenrolme', '', format_string($course->shortname)).'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/user.gif" class="icon" alt="" />';
} else if ( has_capability('moodle/legacy:user', $context, NULL, false) ) {
$this->content->items[]='<a href="enrol.php?id='.$this->instance->pageid.'">'.get_string('enrolme', '', format_string($course->shortname)).'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/user.gif" class="icon" alt="" />';
}
}

Average of ratings: -
In reply to Alex Parsy

Re: Authenticaed user role hack, to show "Enrol in this course" link

by Helen Foster -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
Hi John,

The simplest way of enabling logged-in users to view a course without being required to enrol is just to enable guest access. From your post, it seems you have tried enabling guest access together with other things. Could you explain why just enabling guest access wasn't sufficient for your requirements.
In reply to Helen Foster

Re: Authenticaed user role hack, to show "Enrol in this course" link

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

Correct me if I'm wrong, but Guest access only works when the user has done "login as guest". That is absolutely anyone can access the course. Does guest access to a course also work for Authenticated Users? I need to try it.

If it works how I assumed (login as guest is required) then he presumably only wants people with a valid login to have access.

Off to try it...

EDIT:
Yes, you are right smile The help message is misleading though. It only talks about Guest users when it actually applies to *any* authenticated user (including the "login as guest" user).
In reply to Howard Miller

Re: Authenticaed user role hack, to show "Enrol in this course" link

by Helen Foster -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
Hi Howard,

Hopefully the guest role documentation explains everything about guest access. If not, please feel free to edit the page. wink
In reply to Helen Foster

Re: Authenticaed user role hack, to show "Enrol in this course" link

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Mmm... it's tricky because the page talks mostly about the "guest account". It would be easy to miss this useful feature. I'll have a think smile
In reply to Helen Foster

Re: Authenticaed user role hack, to show "Enrol in this course" link

by Alex Parsy -
Hi Helen,
I am sorry for long delay. I missed this post altogether.

allowing Guest access, as far as I can tell, means the user does not have to register with a login id and password.

We need a bit more control or feedback as to who is viewing the courses. If you have not registered and we haven't confirmed your registration using email acknowledgement, we would not want you to access or view any of the content.

But if you did registered, then you would be considered "Authenticated user" (again based on my underestanding). At that point it is OK for you to view the course content and if you wish, to "Enroll" for it.

Thanks much
In reply to Alex Parsy

Re: Authenticaed user role hack, to show "Enrol in this course" link

by Helen Foster -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
Hi John,

You can restrict guest access to logged-in users only by NOT showing the guest login button. Only registered users with a username and password can login to the site, then once they have done so, they can enter any courses which allow guest access without being required to enrol. I think the easiest way to understand this is to try it out for yourself!