How can I access the user's current course name?

How can I access the user's current course name?

by Matt Bury -
Number of replies: 26
Picture of Plugin developers
Hi,

I'm trying to access various types of data for use with Flash Remoting in Moodle. I've managed to get most of the variables I need but I can't seem to work out how to access the user's current course, i.e. the course page that the user is currently on.

I can get $SESSION->cal_course_referer, which gives me the current course id number but the course names seem to be bundled up in nested objects and arrays and I don't know how to access them.

Any help would be appreciated. Thanks in advance. smile
Average of ratings: -
In reply to Matt Bury

Re: How can I access the user's current course name?

by Antonio Piedras -
Hi Matt,
you should use the global variable $COURSE for your purpose.

Bye
In reply to Antonio Piedras

Re: How can I access the user's current course name?

by Matt Bury -
Picture of Plugin developers
Hi Antonio,

Thanks for the pointer. Could you be a little more specific? I've accessed all the objects $USER, $SESSION, $CFG and $COURSE but I can't find the variable that specifies the user's current course page. What I'd like to know is something like:

$COURSE->variable (variable = user's current course page name)

What is the name of this variable?

Looking forward to your reply.
In reply to Matt Bury

Re: How can I access the user's current course name?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
in your code, put

print_object($COURSE);

That will dump out the whole of $COURSE on screen, so you can see what is there. This is a very useful technique in general when working on Moodle.
In reply to Tim Hunt

Re: How can I access the user's current course name?

by Gordon Bateson -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
Matt,
perhaps you are looking for one of these:
  • $COURSE->fullname
  • $COURSE->shortname
As Tim says, print_object($COURSE) will tell the whole story.

FYI, $COURSE->modinfo tells you all the info about the activity mods. You can access it like this:
if ($modinfo = unserialize($COURSE->modinfo)) {
 foreach ($modinfo as $cmid => $cm) {
 // $cm->mod is the kind of activity (e.g. hotpot)
 // $cm->id is the id in the main table for this activity type,
 // and $cm->cm is the course_modules id (i.e. same as $cmid)
 // gotcha! the activity name is url encoded
 // and may have multilingual text in it so use ...
 $name = filter_text(urldecode($cm->name));
 }
} 
cheers
Gordon
In reply to Gordon Bateson

Re: How can I access the user's current course name?

by Matt Bury -
Picture of Plugin developers
Hi Gordon,

Thanks for the help. That's useful stuff to know.

I tried accessing $COURSE->fullname and $COURSE->shortname but I only got the name of my Moodle site.

I've printed out a bit of the $SESSION object to give you an idea of what I'm after:

(Object)#0
cal_course_referer = 6 // this is where I got the course id from i.e. $SESSION->cal_course_referer
cal_courses_shown = (Object)#1
10 = (Object)#2
category = "2"
cost = ""
enrol = ""
fullname = "Pre-Intermediate" // I'd like to access this variable here
groupmode = "0"
groupmodeforce = "0"
guest = "0"
id = "10"
idnumber = ""
newsitems = "5"
shortname = "Pre-Int"
sortorder = "4003"
startdate = "1222732800"
student = "Student"
students = "Students"
summary = " Materials and activities for English at Pre-Intermediate level."
teacher = "Teacher"
teachers = "Teachers"
visible = "1"
11 = (Object)#3

What I can't work out is how to access the current course name from the array of objects in $SESSION. A far as I can see $SESSION contains all the courses that the current user is enrolled on and you have to access the current course using something like the $SESSION->cal_course_referer as an index. Am I right? Any help? Or is there a simpler method to get it, like get_string()?

Again, thanks for all your help. smile
In reply to Matt Bury

Re: How can I access the user's current course name?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I think $COURSE is only set up when you call require_login($courseid); - otherwise it defaults to the site 'course'.
In reply to Matt Bury

Re: How can I access the user's current course name?

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
It looks like $COURSE is not being set properly for that script (or it is actually somewhere in the top front page course).

The PHP/Moodle script should be doing something like this at the top to authenticate the user properly and set the $COURSE global as well:

require_course_login($course, true, $cm);

(based on data gained from the parameters to the script)
Average of ratings: Useful (1)
In reply to Matt Bury

Re: How can I access the user's current course name?

by Jamie Pratt -
Generally as Martin said the course id or a course module id or similar is passed from page to page as url GET or POST parameters. It is not a session variable.

I outlined a way in this post and here http://docs.moodle.org/en/Development_talk:Web_services#Using_web_services_to_allow_client_side_code_such_as_js.2C_Flash.2FFlex_or_java_to_communicate_with_Moodle that we could make that data available in services called by a Flash movie.

Something similar will also probably be needed by other client side code that calls Moodle web services of one sort or another.
In reply to Jamie Pratt

Re: How can I access the user's current course name?

by Matt Bury -
Picture of Plugin developers
Thanks for all your help.

I'm not a PHP developer (just learning some basic OOP PHP5) and I don't know Moodle code very well at all so please be gentle with me. ;)

So from what I can understand, there's some kind of problem with the $COURSE->fullname and $COURSE->shortname variables not being set. Is that right?

You've mentioned these methods:

require_login($courseid);

require_course_login($course, true, $cm);

Can I access these by including moodle/config.php? How would I use them in a service script? i.e. in moodle/lib/amfphp/services/MyServiceName.php Am I on the right track or barking up the wrong tree?

Thanks again.

In reply to Matt Bury

Re: How can I access the user's current course name?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Ah, if you are doing anything in Moodle, you have to include config.php. In this case, the call would be:

require_once('../../../config.php');

And normally your script will need to take some parameters in the URL to tell it what to do. Remember, HTTP is a stateless protocol. A URL needs to uniquely determine the thing to display.
In reply to Tim Hunt

Re: How can I access the user's current course name?

by Matt Bury -
Picture of Plugin developers
Ok, at the moment, I'm deploying the view page as a resource and I'm collecting $_GET parameters for stuff like which SWF file to use and which DB records to access (I have a DB table of interactions which identifies which DB table and which interaction to populate the SWF file with).

I have a MDL.php service class for Moodle variables which works perfectly except for the course names:

class MDL
{
public $logged_in;
public $course_id;
public $course_fullname;
public $course_shortname;
public $user_id;
public $data_path;

public function __construct()
{
global $CFG;
require_once('../../../config.php');
if (isguestuser() || isloggedin()){
$this->logged_in = true;
global $USER;
global $SESSION;
global $COURSE;
// set variables
$this->course_id = $SESSION->cal_course_referer;
$this->course_fullname = $COURSE->fullname;
$this->course_shortname = $COURSE->shortname;
$this->user_id = $USER->id;
$this->data_path = $CFG->wwwroot . '/file.php/' . $this->course_id . '/';
} else {
$this->logged_in = false;
}
}
/**
*temporary function for testing - gets current course full name
*@returns string
*/
public function get_course_fullname()
{
if($this->logged_in) {
return $this->course_fullname; // at the moment this just returns the site name
} else {
return false;
}
}
/**
*cleans up variables for garbage collector
*@returns nothing
*/
public function __destruct()
{
unset($this->logged_in);
unset($this->mdl_course_id);
unset($this->mdl_user_id);
unset($this->mdl_data_path);
}
}

So how can I make this class work properly? I can actually print out the variables I want to access (see above) but I don't know how to access them directly in this script.
In reply to Matt Bury

Re: How can I access the user's current course name?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Have you tried looking at any existing Moodle code to see what it looks like and how it works?

These are probably two reasonably good idiomatic examples of Moodle code:

http://cvs.moodle.org/moodle/mod/forum/discuss.php?view=markup&pathrev=MOODLE_19_STABLE

http://cvs.moodle.org/moodle/course/request.php?view=markup&pathrev=MOODLE_19_STABLE


In reply to Tim Hunt

Re: How can I access the user's current course name?

by Matt Bury -
Picture of Plugin developers
Thanks for the pointers but as I said earlier, I'm not really a PHP developer. Straightforward PHP is relatively unfamiliar to me and I have very little experience with Moodle code.

As far as I can see, it should be possible to access the course full name and short name from the $SESSION object, since it contains the current course page id number and all the course names that the current user is enrolled on. Perhaps array/object access notation?

I can do all this stuff pretty easily in ActionScript, just not in PHP. I'm in the process of learning PHP5 so couldn't somebody here possibly be so kind as to type up a little bit of code to access one variable? In this case $SESSION -> object #6 -> 'fullname'... please? It'd really help me out a lot. smile
In reply to Matt Bury

Re: How can I access the user's current course name?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
But Moodle is a PHP application, so if you want to customise Moodle, you are going to have to learn a bit of PHP.

Actually, there is a more fundamental than that, Moodle is a database-driven web application running on a server. ActionScript (that's Flash, right?) use used to build stand-alone applications. The way the two things work, and hence the way the code needs to be structured, is completely different.

I really think your quickest way to learn would be to take some working Moodle code, and start tinkering with it until you understand how it works, and what effect your changes will have when you change it.
In reply to Tim Hunt

Re: How can I access the user's current course name?

by Matt Bury -
Picture of Plugin developers
Hi Tim,

I do understand the basic principles of server-side applications and the 'stateless environment' and I do have some experience of using APIs, databases and web services.

I'm still no nearer to knowing how I can get $SESSION -> object [$cal_course_referer] (in this case #6) -> 'fullname'.

There appears to be an array of course objects so I should, somehow be able to access the right object in that array... something like:

$SESSION->courses_array[$cal_course_referer]->object->fullname;

But I don't know PHP well enough yet to know the basic syntax for that. Then again, there might even be a getter method... perhaps something like get_blah($SESSION); I've no idea.

Please help.
In reply to Matt Bury

Re: How can I access the user's current course name?

by Gordon Bateson -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
Matt,
you are tinkering with the resource view page? That page has an "id" associated with it which is the id of the record in the course_modules table for that resource. The "id" is not stored in the $_SESSION array (at least I don't think it is). It is passed to the script thusly:
If you extract the course_modules record for this resource it will lead you to the course record which you are currently trying to get at

Something like the following will work, as long as the script is getting the 'id' parameter passed into it.
if (! $id = optional_param('id', 0, PARAM_INT)) {
die('course module id is missing');
}
if (! $cm = get_record('course_modules', 'id', $id)) {
die('course module id is invalid');
}
if (! $course = get_record('course', 'id', $cm->course)) {
die('invalid course in course module record '.$cm->id);
}
// check access to this course module
require_login($course->id, false, $cm);
And one final thought, is that if your code snippet is included and used somewhere within "resource/view.php" then if you take a look at the top of that script you will see it uses the following global variables which may be useful for you:
  • $cm
  • $resource
  • $course
So maybe all the hard work has already be done for you smile

cheers
Gordon
Average of ratings: Useful (1)
In reply to Gordon Bateson

Re: How can I access the user's current course name?

by Matt Bury -
Picture of Plugin developers
Hi Gordon,

Thanks for the info. It'll come in handy.

However, I'm not altering the "Link to a file or website..." page in any way. I'm only using it to link to my view.php page and passing a couple of custom parameters as $_GET variables, namely which SWF database entry to get the SWF URL and embed parameters from, and which interactions database entry to populate the embedded SWF file with the interaction data (i.e. which DB table and id contains the relevant text, questions, audio, images, etc.).

The frustrating thing is that I can pass the $SESSION object into a SWF file and see the data I want to get but I can't, for the life of me, work out how to get to it in PHP on the server, rather than sending the whole $SESSION object to the client which doesn't strike me as a good idea to pass that information back and forth!

Yes, it would be possible to pass the course name from the "Link to a file or website..." page. I'm just trying to make deploying SWFs in Moodle as simple for the user as possible by automating stuff like that. It's something that most view pages in Moodle do, I'm just surprised that no-one has given me a way of getting it yet.
In reply to Matt Bury

Re: How can I access the user's current course name?

by Gordon Bateson -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
Is it possible just to pass the course id to the client, so that you can then pass that back to the server and use it to get the course record?
In reply to Gordon Bateson

Re: How can I access the user's current course name?

by Matt Bury -
Picture of Plugin developers
I've got the course id in my service script. Couldn't I get the the course name with that somehow?

i.e. get_string('course_fullname',$this->course_id);

Or something like that?
In reply to Matt Bury

Re: How can I access the user's current course name?

by Gordon Bateson -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
Oh I see, in that case, try this ...
  • $coursename = filter_text(get_field('course', 'fullname', 'id', $this->course_id));
or if you want the whole course record ...
  • $course = get_record('course', 'id', $this->course_id);
    $coursename = filter_text($course->fullname);
cheers!
Gordon
In reply to Gordon Bateson

Re: How can I access the user's current course name?

by Matt Bury -
Picture of Plugin developers
Hi Gordon,

That's great, exactly what I was looking for and more! Being able to get the whole course object is fantastic.

BTW, is there any documentation on these getter methods and Moodle getter methods in general? You know, something like php.net: http://es.php.net/manual-lookup.php?pattern=get

Thanks a million! smile
In reply to Matt Bury

Re: How can I access the user's current course name?

by Gordon Bateson -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
To a certain extent, the Moodle scripts libraries are "self-documenting" so you can find alot of commenting in the scripts.

The main script you would be interested in for getting and setting stuff in the database is "lib/dmllib.php".

The automatically generated phpdocs site may be useful for you too:
hope that helps
Gordon
In reply to Gordon Bateson

Re: How can I access the user's current course name?

by Matt Bury -
Picture of Plugin developers
Hi Gordon,

I've bookmarked it. Thanks! smile
In reply to Matt Bury

Re: How can I access the user's current course name?

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Matt,

Please note that Gordon himself is an invaluable human resource and should be bookmarked.wink

Joseph

In reply to Matt Bury

Re: How can I access the user's current course name?

by Vinícius Machado de Melo -
Well, I am also new to Moodle plugins development, and I got a similar problem. The situation was I did a webpage to get some information about the students of the current course from the database. This page is accessed via a link inside the course page.

Inside its code, when I accessed the global $COURSE->id, I was getting the Moodle ID instead of the current course ID, I dumped every single GLOBAL variable trying to find a way trhough, so I did the following:

I got the url of course page by $_SESSION['SESSION']->fromdiscussion. It gives you something like this:  http://moodle/moodle/course/view.php?id=7 (the url of your course page). Then I tokenized it, taking the ID = 7, that corresponds to my course ID.

I hope it can help who is in need.
In reply to Vinícius Machado de Melo

Re: How can I access the user's current course name?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

In case anyone else reads this - please do not do what is suggested above - that does not sound at all like a reliable way of working.

If you are inserting a link into a course page, then the link needs to include the id of the course that it is linked from, otherwise that page is not part of any course, it is just a system-level page, outside of the Moodle course structure.

The correct solution would be to check $COURSE->id (or, these days, $PAGE->course->id, although they should always be the same), then generate a link that includes '?courseid='.$PAGE->course->id.