Can you help me code this?

Can you help me code this?

by N Hansen -
Number of replies: 4
I've got a plug in for my Moodle that I am working on. I need it to be restricted in access to only one course. Let's say the course id is 5. Here's what I have so far-this only restricts users based on their login status.:

    include("../../config.php");
    require_login();

    if ( isguest() ) {
        error("You cannot use this page as a guest!!!");
    }

I need it to check whether they are enrolled in course 5 or not. How do I do this?
Average of ratings: -
In reply to N Hansen

Re: Can you help me code this?

by Jan Dierckx -

You can add an argument to the function require_login:

The following ...

include("../../config.php");
require_login(5);

... will check if users are logged in and are allowed inside course with id number 5. It doesn't return a true or false value, so you can't use it inside an if() statement, but it does the appropriate things all by itself:

  • If they are not logged in they are redirected to the login page

  • If they are not enrolled in course 5 and this course allows enrollments they are asked if they want to enroll

  • If they are not enrolled in course 5 and this course does not allow enrollments, they are redirected to the frontpage.

More information (also about other arguments you can add to the function) can be found inside moodle/lib/moodlelib.php

In Moodle 1.6 you can find it around line 1512. Look for function require_login

HTH

In reply to Jan Dierckx

Re: Can you help me code this?

by N Hansen -
Thanks Jan-That does the trick, however, the problem is that the page in question that I want to block them from accessing is itself a tiny popup and so if the student isn't logged in or isn't enrolled in the course the page it redirects them to is also in a tiny popup and it is so small they can't see the page they have been redirected to. Is there a way to specify explicitly where they are to be redirected to avoid this?


In reply to N Hansen

Re: Can you help me code this?

by Jan Dierckx -

You could have a look at how the resource module (which also allows popups) handles this sort of thing, but it's not easy...

It's difficult to talk without a piece of your code at hand, but I suppose what you have now is ...

page P which writes to screen the content of the popup

page A which has a link (with target="blahbmah") to open that popup.

The trick is to add one page to go in between these two: a page B which is not itself a popup but a normal page which has some javascript in it to automatically open an extra popupwindow.

Then page A has a normal link to call page B. Page B contains the check if the user is allowed to see the contents of the popup, but page B is not itself a popup. As such, if a user needs to login, the instructions are shown on a normal page, not inside the popup.

Inside page B there is the following code:

            echo "\n<script language="javascript" type="text/javascript">";
            echo "\n<!--\n";
            echo "openpopup('pageP.php','nameofpopupwindow','popupoptions');\n";
            echo "\n-->\n";
            echo '</script>';

When page B is sent to the browser, the javascript is executed and page P is displayed inside a popup. (the openpopup function is automatically loaded by Moodle)

Page B remains visible on the background. This also has the following advantage: In case people use a popup blocker, your popup may not be shown. Then you can add another link inside this page B with a message like:
Normally you should see a little popup, if it does not please click on the following link: ... (see resource module)

Not easy ... mixed

In reply to Jan Dierckx

Re: Can you help me code this?

by N Hansen -
I think the problem is that the page that they are not allowed to continue with is itself of type B.

But in any case, I think the problem can be solved in another way. The item in question is normally accessed through a button in the html editor (Janne helped me to code it). I only encounter this problem if I try to access the page via that button. If I try to go to it directly it redirects me to a proper sized page. I will wait until Janne can tell me how to hide the button in all the other courses (since I haven't been able to figure out how to do it myself) and then the issue shouldn't arise at all.