How to access Joomla objects in a Joomdle PHP file

7 years 4 months ago 7 years 4 months ago by Rahul Paul.
Rahul Paul
Posts: 9
More
Topic Author
How to access Joomla objects in a Joomdle PHP file #1
I am using Joomla 2.4, Moodle 2.4 and Joomdle 0.82.

I need to modify the "joomla root/moodle/auth/joomdle/land.php" and include a custom redirect based on the GROUP that a user belongs to.

For this I am using code similar to the following in land.php just before the last "redirect" line:

if (condition){

jimport( 'joomla.user.user' );
$user = JFactory::getUser();
jimport( 'joomla.access.access' );
$groups = JAccess::getGroupsByUser($user->get('id'));

/*12 is the group id for an enrolled student*/

if(in_array(12, $groups))
$redirect_url = "/newurl";

}

The above code stops execution the moment it encounters the first jimport() function, since, I assume, the Joomla framework has not been imported into land.php.

I have tried several things and the last thing that I have done is put the following 2 lines at the top but nothing works.

require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/includes/defines.php';
require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/includes/framework.php';

Actually, what I am trying to check is whether the user is already enrolled in ANY course (he would then have a Joomla group id of 12) and if yes then I need to redirect to a specific page depending on another condition.

I am at a total loss as to how to make the above code execute. I am not a PHP guy, so please excuse my ignorance.

Any help will be greatly appreciated. Thanks.

Please Log in or Create an account to join the conversation.

7 years 4 months ago
Chris
Posts: 2118
More
How to access Joomla objects in a Joomdle PHP file #2
Antonio is engaged this weekend so will only be able to comment after a few days.

As I am not a php guy either, I am not sure if you can just call in the Joomla framework exactly as you have done. According to Joomla.org you can load up the framework but I have not read through in detail ....
- In J 1.5 it was done like this docs.joomla.org/Initializing_the_Joomla!...n_an_external_script k
- In J2.5, I found this discussion stackoverflow.com/questions/15042262/acc...to-get-article-by-id k

I am sure there are plenty of other discussions on how to use the framework from an external script - I shall leave this to you.

You don't say which Group information you are trying to use. Joomla groups or Moodle? If Moodle, you should not need to even call the Joomla framework, therefore, I assume you are talking about Joomla groups?

Assuming Joomla groups I would look at a few options:
- Load the Joomla framework as you are trying
- Since you are hacking code, why not add the group info as a new parameter being passed to land.php from Joomdle
- Alter the redirect url to include an additional parameter. Use a direct in Joomla to capture this and do the work. This would probably require a plugin, so maybe a lot of work
- Override a specific Joomdle view and include some code there to check the group and redirect as required

Personally, I like the later as it does not hack the Joomdle code but do not know if this will work as I imagine my gut feel is that it will do.

Of course the final option is to wait for the php expert :)

Please Log in or Create an account to join the conversation.

7 years 4 months ago
Antonio Durán
Posts: 7252
More
How to access Joomla objects in a Joomdle PHP file #3
No, you can't call Joomla framework in a Moodle page (unles you include all required libraries).

It seems you could use the moodle API to get the user courses and redirect him based on that, instead of using the info from Joomla.

Please Log in or Create an account to join the conversation.

7 years 4 months ago
Rahul Paul
Posts: 9
More
Topic Author
How to access Joomla objects in a Joomdle PHP file #4
Thank you Chris and Antonia for your replies.

Since, I just need to check if the current user is a Student in any course, I actually do not need to check Joomla groups for that info.

I have finally modified the Joomdle land.php and just before the redirect() function call I use code similar to the following to check if the current user is a student:

global $DB;
$sql = "SELECT count(*) FROM mdl_user_enrolments a,mdl_user c WHERE c.username='".$username."' AND a.userid=c.id" ;
$n = $DB->count_records_sql($sql);
if($n==0) {
//user not enrolled
fwrite($fh, 'username is not a student: ');
} else{
//user already enrolled
fwrite($fh, 'username is a student: ');
}


To check if the current user is a student, I could have also used something like:

$context = get_context_instance(CONTEXT_COURSE,$COURSE->id);

if (has_capability('moodle/legacy:student', $context, $USER->id, false) ) {
echo "Student";
}

But the above code, tells you if a user is a student in the context of a specific course - I need to know if the user is a student in ANY course - hence the direct database access to find out.

Thanks again for your help.

Please Log in or Create an account to join the conversation.