Scripted course format/section modifications, changes not recognised by restore?

Scripted course format/section modifications, changes not recognised by restore?

by James McLean -
Number of replies: 4
Hi Folks,

I'm currently working on a system to automate the copying of course content and material from course A to course B.

Our current workflow is such that we also have a system where an Academic can create thier Moodle course by selecting the right button from our upstream course management system - a cronjob calls the relevant Moodle functions and creates the course. Works brilliantly.

We're now extending this so that it will create Course B in Moodle as an empty stub, then automate the backup of Course A (selected in the upstream process) and restore to Course B. I have a test script that implements this, works as expected, when the Course format and Numsections of both courses are the same, naturally.

As I'm sure you can expect, people make mistakes, so in order to stop courses being copied into a stub setup incorrectly and rather than throwing an error - I have been attempting to modify the destination stub so that it will match the source. No issues with doing the actual code to make the change - it's quite simple - i'm simply loading two objects, one for each course, modifying the destination format and numsections to match the source, and submitting it to the update_course(); function.

Calling the function works, it doesn't error, the format is changed (in the m_course/mdl_course table) and the numsections is also changed, but the problem comes about with the actual restore - it's treating the course as if these changes had not been made at all - for example if the source course is set to 20 sections, and the restore needs 15 - when the restore happens it ignores most of them and only uses the 10 sections as they were actually originally configured.

If i manually create the destination stub in the right format and the right number of sections, then it all works flawlessly.

My question is, is there something ELSE that needs to be done when the update_course() has been run? I have tried marking the context dirty so that it will re-generate it, no dice, and i've gone through the actual course/edit.php page and cannot see anything else there i'm missing. I've also wrapped that specific code in a transaction using $db->StartTrans() and $db->CompleteTrans() from the ADOdb layer, no dice here either.

I have been told that we don't 'need' this feature for this initial version of it, but there is a LOT of courses that will be using this copy feature, and if it saves 5 mins of work for the people doing the work even on 1/3rd of the courses it could save a lot of work. I also think I might just be missing something 'simple', and would really like to have it do this correctly...

What am I missing?

TIA!


Average of ratings: -
In reply to James McLean

Re: Scripted course format/section modifications, changes not recognised by restore?

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
For an initial guess: did you remember to do rebuild_course_cache($courseid); ?

--sam


In reply to sam marshall

Re: Scripted course format/section modifications, changes not recognised by restore?

by James McLean -
Ahh! No, I did not do that Sam. I'll throw that in and see how it goes smile Thanks!
In reply to sam marshall

Re: Scripted course format/section modifications, changes not recognised by restore?

by James McLean -
Unfortunately that had no effect, Sam. Do you have any other ideas?
In reply to James McLean

Re: Scripted course format/section modifications, changes not recognised by restore?

by James McLean -
I bumped into this issue again today while in UAT for this script. Strange that it'd get to UAT before being discovered, but my workflow was such that I'd create a Moodle course manually, view it, then kick off the backup/restore procedure and view the final results making the problem 'invisible' to me as a developer.

Turns out that Moodle doesn't actually 'create' the sections in the database until the course is physically viewed in a browser.

What was happening was my script was creating the Moodle Course, then content was restored into it before it had been viewed in a browser and the section creation stuff in each course format include wasn't run. Because no sections were available in the database, Moodle would simply put all the content into Section 0 (which is created when the course is created). Smart enough, but annoying for people who don't understand why that's the case smile

Rectified the problem by creating the sections required when the course is actually created by our custom scripts and it worked as expected from there.