Changing the backup/restore code

Changing the backup/restore code

by Penny Leach -
Number of replies: 8
I've just spent a wee bit of time hacking the backup & restore code to allow a teacher to import course data, without it looking like they're backing up a course and then restoring an existing course and I've come up against a bit of a quandry.

What I have done so far is to add a new link in course/view.php which goes to a page to find a course to import data FROM - it offers a dropdown of courses taught by the logged in teacher, and a dropdown of courses in the same category as the TO course, as well as a search mechanism. Once the teacher has selected the FROM course, they get into backup/backup.php where they can select which modules to import, as in the normal backup process. Then, once the backup is finished, there's a hook that continues on to backup/restore.php to begin importing it into the original course. (As well as a few hooks in other places to decide what strings get printed out to the user - like "Backup data" or whatever becomes "Import data")

I've got it to the point where it pretty much works, except that I'm not really happy with the number of screens the user has to go through. Once they've selected which modules they want to import, it should just do it automagically.

Looking at the files included by restore.php, it appears that they have dual functionality - to do actual stuff, and to report the outcomes of that stuff to the user. So I was thinking I could refactor them into functions in restorelib.php with a silent argument, so that for my purposes I could call the functions with silent=true and all the stuff would still happen but without the necessity of reporting it to the user and going through all the different screens. I imagine the same approach would work for backup.php and associated included files.

Equally, for normal backup/restore functionality, the different stages would call them with silent=false and the user would get to see all the backup/restore stuff.

Also, when restoring data now, we give the user a list of features that they can import, based on what has been exported, and for my purposes, they've only just selected what to import at the backup stage, and so this stage would need to be skipped as well.

So! The question is, what would be the impact of refactoring this out (is this a desirable modification for a patch?), or should I write a new set of php scripts that do very similar things to backup.php and restore.php but without the user notification?

I guess I am weighing up the consequences of modifying the existing core code against having a whole lot of duplicate code. My current preference is to refactor and then I will submit back a patch, but of course, there are likely to be repercussions that I haven't considered.

Cheers!
Penny
Average of ratings: -
In reply to Penny Leach

Re: Changing the backup/restore code

by Penny Leach -
OK, I have to start working on this, so I'm going to start refactoring it out. I'll post a patch when I'm done and people can debate it then if they have stuff to say ;)
In reply to Penny Leach

Re: Changing the backup/restore code

by Eloy Lafuente (stronk7) -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Peer reviewers Picture of Plugin developers Picture of Testers
Hi Penny,

sorry by the delay (again). sad

Although I'm absolutely sure that a complete re-factoring of both the backup and restore processes are needed (your "silent" option is a great example), thinking about the upcoming "Select of individual activities" feature (it'll imply another re-factoring to be done, sure), perhaps you could try the other alternative (duplicating some pieces of code).

As an example, you can see how the scheduled_backups execution has been implemented. Basically, I´ve merged the three screens of the backup process into a unique function re-executing all the original code. I think that this approach could be used for your interesting (and useful) backup add-on.

Basically all you have to do is to include all the code in:

"restore_precheck, restore_form, restore_check and restore_execute" scripts in a function, deleting all the output code.

I know that it isn't nice to duplicate it, but, when WE will start making the other re-factoring, we'll have that "silent" option in mind (if you remember it wink.

And this is my opinion, to be discussed, of course!!

Ciao smile
Average of ratings: Useful (1)
In reply to Eloy Lafuente (stronk7)

Re: Changing the backup/restore code

by Penny Leach -
Hi Eloy!

I'd almost finished when you posted this, so I just kept going.

We can definitely work together to make this nice when you're ready to start the other stuff, but for the now I have a (somewhat hackish) patch at http://nzvle.eduforge.org/import.patch

It's pretty basic, just a new menu item in the course view, a page to find a course to choose to import data from, a couple of hooks in the backup/* pages, a new variable in each of mod/*/backuplib.php called $modname_offer_import (for our purposes we don't want user data even being offered for import [this includes attendance roles and journals] - but this separates it from normal backup/restore stuff), and a couple new functions in restorelib.php.

Basically, there's a new param called $to being passed around which means, if we have a place to dump the course data into once we've finished backing up, act differently. This is also used in the security checks - I'm allowing users that aren't teachers of the FROM course to backup the FROM course, as long as they're a teacher of the TO course. There are a couple of ui changes as well that hinge on the $to variable.

Cheers!
Penny


In reply to Penny Leach

Re: Changing the backup/restore code

by Penny Leach -
There was a slight bug in this patch. On the off chance anybody has decided to use it, I've changed the original patch and posted a bugfix at http://nzvle.eduforge.org/import_bug.patch
In reply to Penny Leach

Re: Changing the backup/restore code

by Thomas Haynes -
 I would like teachers to be able to "import" assignments, resources and events from another teacher's class. It looks as if this patch might provide what I am looking for.

Will it allow you to pick which ones to import?

I can administratively backup and restore to do the same thing, but resources are duplicated.

If there were a way to create an assignment, etc. for three classes at one time and allow the teachers to modify details, it would be perfect. If I could selectively restore assignments to only pick the last few, it would work too.
In reply to Thomas Haynes

Re: Changing the backup/restore code

by Penny Leach -
At the moment, it gives you a list of all the activity modules in a course and asks you which ones you want - but it doesn't get any finer grain than that. So if you want to import assignments, you have to import _all_ the assignments.

Eloy has plans to refactor the backup/restore code to allow you to choose which particular assignments (for example) you want.

Once he starts working on this, I will probably try and be involved so that we'll be able to use the backup/restore functionality to move course data between courses - which is likely more what you're after.

Cheers
Penny
In reply to Penny Leach

Re: Changing the backup/restore code

by Thomas Haynes -
Penny...

Thanks for the reply.

As it is, I will probably backup the reference class once a week and restore to a sandbox area I have. Then I can go into phpmyadmin and delete all the recent additions that are duplicates and keep the ones from the past week. At that point,  I can back up and restore to the other classes. This will give them a common starting point and allow for variations for individual classes. It will keep the gradebooks separate and allow us to have a Math moodle site instead of an Algebra 1 moodle site.

I have not actually tried this, but I don't see why it would not work. It seems to meet the greatest number of goals.

Regards...   Tom
In reply to Penny Leach

Re: Changing the backup/restore code

by Dennis Daniels -
Importing pieces of courses is definitely what I'm after smile ... I've successfully loaded keduca tests over the web... it set me thinking about how I can share my own db of assignments and quizzes... sharing is key here... I know my fellow teachers have hundreds of assignments and tests... if we could share those via moodle... it sets my head spinning with ideas!

I'm really digging moodle!
Dennis