Retrieve the course id upon creating

Retrieve the course id upon creating

by Tzahi Zohar -
Number of replies: 17

Hey,

 

I was wondring how can I get designated course id when the course is created, that is before I enter the course itself where the url ends with "id=[num]".

 

meanning when I press "save changes" on the "create course" page I want to know what id deos this course have.

through the "edit_course.php" file.

 

any help?

 

Thanks in advanced

Average of ratings: -
In reply to Tzahi Zohar

Re: Retrieve the course id upon creating

by Eric Millin -

If you're using mysql, you can get the id for the last inserted record with mysql_insert_id().

In reply to Eric Millin

Re: Retrieve the course id upon creating

by Eric Millin -

Assuming that you're using Moodle 2.0, you can also get the id as the return of "$DB->insert_record(arg1, ...)".

In the future, it'd be helpful if you can specify what version of Moodle you're using.

In reply to Eric Millin

Re: Retrieve the course id upon creating

by Tzahi Zohar -

Thanks,

 

1. if I will ran the sql query on the window.onUnload (js) what will happen first:

a. the insertion of the new course to the sql.

b. retriving the [one before last] course entered.

2. can someone post an example with "$DB->insert_record(arg1, ...)" regarding how to retrive the course id? what are the arg?

 

Thanks!

In reply to Eric Millin

Re: Retrieve the course id upon creating

by Tzahi Zohar -

Thanks,

 

1. if I will ran the sql query on the window.onUnload (js) what will happen first:

a. the insertion of the new course to the sql.

b. retriving the [one before last] course entered.

2. can someone post an example with "$DB->insert_record(arg1, ...)" regarding how to retrive the course id? what are the arg?

BTW Moodle v2.0

Thanks!

In reply to Tzahi Zohar

Re: Retrieve the course id upon creating

by Eric Millin -

Check out the documentation on the $DB global object.  After you've read the documentation, you can learn more by reading the documentation in the code.  To find a function, search Moodle for "function function_name(".  You'll find details on all the parameters and get an idea of what's happening.

Also, your life of working with code will improve exponentially if you take a few hours and learn to use a debugger.  A debugger can show you just about everything you need to know. Specifically, you'll want to learn to use breakpoints.  For js, try Firebug.  For php, you'll need an IDE such as Netbeans, Eclipse, PhpStorm, etc, plus Xdebug or Zend.

The two hours it takes to learn debugging will save you literally days down the road.

 

In reply to Eric Millin

Re: Retrieve the course id upon creating

by Tzahi Zohar -

Thx for the advices.

 

I do know how to use a debugger...

but I can't use it becuase moodle is installed on a remote server which run the code.

In reply to Tzahi Zohar

Re: Retrieve the course id upon creating

by Anne Krijger -

Hi Tzahi,

If that remote machine can connect to the computer you run your editor on, then you can even control a remote server.

The php.ini settings

xdebug.remote_host = "localhost"
and
xdebug.remote_port = 9000

control this.
In the example above the 'remote' server is the same AKA localhost.

But if your for example you server has an IP of 10.0.0.1 and the computer you'd like to control it with an IP of 10.0.0.2,

setting the php.ini on the 10.0.0.1 server to

xdebug.remote_host = "10.0.0.2"
and
xdebug.remote_port = 9000

Would allow the server (10.0.0.1) to connect to your computer at 10.0.0.2. And then the computer at 10.0.0.2 to control the remote server at 10.0.0.1.

Assuming that there is no firewall in between that would block port 9000.

HTH,

Anne.

In reply to Tzahi Zohar

Re: Retrieve the course id upon creating

by Eric Millin -

If you've gone through everything and still need help, then explain what you're trying to do.  There are many ways to get the id.

In reply to Eric Millin

Re: Retrieve the course id upon creating

by Tzahi Zohar -

Thank you very much for this information, I am sure it will come handy.

I still am not sure about how the query should be structured.

let me try and shade some light on what I  am trying to do.

 

as soon as I create a new course I would like to (through JS which will get the event of exiting the course page) to retrive (the course which I just created) ID.

If someone could help me with:

1. a JS function that get's triggered on leaving the edit_course.php page.

2. that will trigger a SQL query to retrive the (just created) course id.

 

I need that id in order to do some crawling on the course page.

 

again, Thanks in advanced,

In reply to Tzahi Zohar

Re: Retrieve the course id upon creating

by Darko Miletić -
Picture of Core developers Picture of Plugin developers

This is not that hard. You can do several things:

1. Create a simple php file that gets last id (biggest number) from course table and prints it out. Call this php from your javascript and you have it.

2. If you want to be even more sure create a separate module, for example block and register course_created event listener. Your listener will be called after course is created but before it is being redirected so you have a world of options there.

http://docs.moodle.org/en/Development:Events_API

 

 

In reply to Tzahi Zohar

Re: Retrieve the course id upon creating

by Eric Millin -

Darko has some good suggestions.

Also do you have to have the id on leaving?  Depending on what you need to do, you can simply add javascript or php to the page  that opens after the new course is created (I believe it's "Enrol Users").  Snag the id from the url and then retrieve the info you want from the database, manipulate the page, etc.

In reply to Eric Millin

Re: Retrieve the course id upon creating

by Olumuyiwa Taiwo -
Picture of Plugin developers

Tzahi,

If you're willing to change a core file (course/edit.php), you can grab the course ID after the line that says

$course = create_course($data, $editoroptions);

(about line 111 in my copy of edit.php)

-- Muyi

In reply to Olumuyiwa Taiwo

Re: Retrieve the course id upon creating

by Tzahi Zohar -

Thanks for all the replays, I took the last advice but when I add this:

echo ""+$course+"";

 

I get 0, meannig the id is called afterwards.

 

I inserted this line at the bottom of the edit_form.php file.

 

any suggestions? or if someone could paste example of quering the DB for this id?

In reply to Tzahi Zohar

Re: Retrieve the course id upon creating

by Eric Millin -

You didn't get the id because the data from edit_form.php is processed on line 106 of the file Olumuyiwa mentioned.  Follow his suggestion and insert your code on line 112.  You can access $course and have the info you want.

In reply to Eric Millin

Re: Retrieve the course id upon creating

by Tzahi Zohar -
Thx, when I post: echo ""+$course.id+""; on line 112 as suggested I got "1". the actual number I should have got was 11 (this is the 11 course I am starting). any ideas?
In reply to Tzahi Zohar

Re: Retrieve the course id upon creating

by Tzahi Zohar -

I am loosing it here smile can someone tell me what is wrong with this:

$query = "SELECT max(id) FROM moodle.mdl_course";

$DB->get_record_sql($query, array $params=null);

echo ""+$DB+"";

thanks!

In reply to Tzahi Zohar

Re: Retrieve the course id upon creating

by Olumuyiwa Taiwo -
Picture of Plugin developers

Tzahi,

The $course and $DB variables are objects, so to examine them you should be using things like

echo $course->id;

print_r ($course);

print_r ($DB);

var_dump ($course);

var_dump ($DB);

Your $DB->get_record_sql statement should read

$DB->get_record_sql ($query);

You may want to take a look at where similar things are used in the Moodle codebase in order to get an idea how to do the same.

-- Muyi