General developer forum

 
 
Picture of chandrika harathi
create moodle course via insert statement
 

Long story short:

I developed a program to insert into MDL_COURSE table directly.  Looks like I am missing something as the course count comes up in the interface under the category but the course is not visible: not even greyed out. When I click on the Miscellaneous catgory. I see only 1 course.

I insert data in: CATEGORY, FULLNAME, SHORTNAME, IDNUMER.

Left defaults for the rest.

What should be in SORTORDER ?

Thanks

 
Average of ratings: -
Davo
Re: create moodle course via insert statement
Group DevelopersGroup Particularly helpful Moodlers

This does not sound like a good idea - you really should let the Moodle functions handle this process or, if you want to trigger it externally, try using the webservices available in Moodle 2.1/2.2 - the service 'core_course_create_courses' is what you are looking for.

If you *really* need to insert courses directly into the database, then have a look at the 'create_courses' function in 'course/externallib.php', which calls the 'create_course' function in 'course/lib.php'. These functions will show you what needs to be configured for the course to be set up correctly (but, I can't say this enough times, directly adding records to the course table is unlikely to be what you really want to do).

 
Average of ratings:Useful (2)
Rosario playing soccer
Re: create moodle course via insert statement
Group Particularly helpful Moodlers

As Davo says, YOU SHOULD use the Moodle API, that's why it is there. And this way you will never have to bother about any changes because the API functions will handle this for you and you can leave your code as it is. You may be interested in my own uploadusersandcourses.php code I wrote for Moodle 1.9.x (you will find it in the forums here). You may customize it to fit your needs or you may take it as example to learn how to do it. And I wrote it also to be used as cron-job whenever you want: uploadusersandcourses_silently.php

Rosario

 
Average of ratings: -
Picture of chandrika harathi
Re: create moodle course via insert statement
 

Thanks Davo and Rosario: your help is much appreciated.

This is Moodle 1.9.13.

I would definitely check out your uploadusersandcourses.php.

Also any pointers to the APIs for this is highly appreciated. I could not find much on this in the documentation.

Thanks  in advance,

 
Average of ratings: -
Rosario playing soccer
Re: create moodle course via insert statement
Group Particularly helpful Moodlers

There you are:

http://moodle.org/mod/forum/discuss.php?d=128317

And for the APIs start with the developer Documentation and consult the Moodle Xref here:

http://xref.moodle.org/nav.html?index.html

The best is still to use an existing Moodle php-script doing more or less what you want to do, study it and start changing it to fit your needs. I did so, using the existing uploaduser.php you can find in moodleRoot/admin/

Then I progressively added what I found in the library functions used to CREATE a course, and then added code for the silent-cron-job-version with code I found in the /admin/cron.php function, etc. etc. so that this one can be scheduled as cron/batch job.

Rosario

 
Average of ratings: -
Picture of chandrika harathi
Re: create moodle course via insert statement
 

I have downloaded the uploadusersandcourses04.zip figuring out the latest is the best. I am basically .net developer dabbling with pHp and am trying towork my way into php. Our courses are nested in at least 1 Main category and 2 subcategories: so I am trying to debug and understand the course creation. Here is the usual structure

Main Category Spring 2012:

   Sub-category 1: school1
     Sub-category 2:Program1
                           Course1
                           Course2
     Sub-category 2:Program2
                           Course3
                           Course4
                           
    Sub-category 1: school2
Sub-category 2:Program2-4School2
                           Course14School2
                           Course24School2:

This is pretty complicated and I thought that direct insert is the best as I need to build the categories 1st before creating courses.

Anything to simplify is the process.

 
Average of ratings: -
Rosario playing soccer
Re: create moodle course via insert statement
Group Particularly helpful Moodlers

FIRST) NEVER use numbers in Category- or Course names, because you will be changing them all the time which means unnecessary extra work. Use more neutral names that are likely to never change. One of my Moodle-Admins did this at a University-Level, not knowing, that I had stopped this already in 2004/2005 and so he began renaming, copying and moving around a lot of courses at every start of a new semester. Of course he thought to archive old courses this way, but this was not only wasting time to copy and move around, this meant also wasting a lot of space on the data-disk, because doubling a course means to have FOUR times the data, twice as course and twice as a backup-zip-file. So stop this before starting it. If you need an archiving strategy, choose one which is optimized. e.g. copying only the backup-zip-files to a separata archiving-data-disk or letting the teachers download the needed zip-files directly, so as not to bother you, etc.

SECOND) For programming/automation reasons you need fixed names so that you can change my Translation-Table for categories directly in my code. I have TWO Translation tables set up, so that I can switch the table to be used, depending on the server on which I create the courses. This means I have to use one table to translate the same category mentioned in the import file to use on the production server and one to use if I test the import on my test-server, where the same categories have different Moodle-IDs from the production server.

THIRD) Be sure to never import such things directly on your production server because you could mess up your Moodle-Database. Do a first run and all tests on a test-server first. Even after 3 years I continue to test the whole import on the test-server first. And if everything works fine, I repeat the import on the production server. My actual version 05 on which I am working at present, is optimized for our environment and will be starting to run as cron job, so that our teachers can compose Moodle course orders directly in a web-interface showing them all the courses registred in our School Administration System (where you have also the Teachers, the Students, the courses they should attend, their grades, etc. etc.) So this cron job will run once a day and create the desired courses on Moodle. In other words, I have interfaced our School Administration System with Moodle simply by exporting what I need to know out to a cvs file. Then I import it to Moodle.

Of course, if you provide the correct Moodle category IDs in your import file, then you can switch off my translation tables directly in the code.

Rosario

 
Average of ratings: -
Picture of chandrika harathi
Re: create moodle course via insert statement
 

Thanks Rosario,

Our academic units are very different and our course categories are dynamic parts of the SIS course information. Each semester, there will be newer categories and this is something I need to deal with and speck it out.

Your translation table works out cool I think if there are no nested categories. We do not have numbers but our categories are alpha numeric. My strategy right now is to build the categories first before populating the courses.

 
Average of ratings: -
Rosario playing soccer
Re: create moodle course via insert statement
Group Particularly helpful Moodlers

>>Your translation table works out cool I think if there are no nested categories.

Please note that THIS IS ONLY sort of a fata morgana, the categories have actually ID-Numbers as the courses do in their respective Moodle Database-Tables. But these tables ARE NOT hierarchically ordered, the opposite is true, they are FLAT. So hierarchy or call it nesting is only achieved by a trick in the sortorder and path fields. But if you use alpha-numeric titles for your categories, you need to adapt my translation table to map them to the correct Moodle IDs.

I understand that you are going to export from your own school administration system, every semester or year right? Then, if you create categories in Moodle, their IDs will be incrementing all the time and you have to adapt your translation table all the time too...

As I said, it would be easier if you could at least fix your categories for a longer period so as to import the courses always into these existing categories as I am doing since I wrote my uploadusersandcourses.php script.

By the way, I have planned to create also categories with my script, if they do not exist. But THIS WOULD MEAN that our exports should contain also a column to indicate the correct path WHERE to create this category, as I said, to simulate a nested/hierarchical system. Until we have such a solution it is easier to quickly create the categories in the Moodle GUI. You may look at the URLs Moodle uses to create a category and put them into a script. This way you could simulate calling ../course/editcategory.php with the needed parameters to quickly create new categories prior to importing the courses.

Rosario

 
Average of ratings: -
Picture of chandrika harathi
Re: create moodle course via insert statement
 

Yes:  The course categories are FLAT in one table only in Moodle. It is just how they are displayed.

Yes: I am exporting the course information from school's administration system: every semester and academic year. Many categories are retained but newer ones come in as these are dynamically built as a part of course short name.

I need to get the categories before creating courses. I am looking to create an XML file for categories from school's admininstration system and build in Moodle. This way I do not need to change the source code. Also application administrators can change the XML category elements. Still in design phase.

I am deep diving into php: ASP.NET, c  is my native programming language. I am trying to understand php.

 
Average of ratings: -
Rosario playing soccer
Re: create moodle course via insert statement
Group Particularly helpful Moodlers

>>I am deep diving into php: ASP.NET, c  is my native programming language. I am trying to understand php.

Not problem at all. The hardest will only be to find and understand the Moodle API calls.

Good Luck, Rosario

 
Average of ratings: -
Picture of David boudreault
Re: create moodle course via insert statement
 

I  want to create the course via the web service with moodle 2.3

Do you have a moodle 2.3 version of your php script ?

Thanks

 
Average of ratings: -
Rosario playing soccer
Re: create moodle course via insert statement
Group Particularly helpful Moodlers

Yes, I reengineered last July. You will find it in the same thread. Please search for Rosario uploadusersandcourses.

I do not use the web-service. I still use php-api-calls to Moodle functions. And it works in production since then on our Moodle .23

Rosario

 
Average of ratings: -