Course duplication without content

Course duplication without content

by john doe -
Number of replies: 5

Hi everybody!

I'm working on a duplication block. The aim is simple, on a course, when you click on a link in the block, you have to select the category of the new instance of your course , and this one will be duplicate in this new category.

my problem is that the duplication (backup and restore) works well but, all the content of the different resource are not transfered. 

I give you my sources of the two functions I use.

 

$bc = new backup_controller(backup::TYPE_1COURSE, $courseid, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_IMPORT, $userid);

$backupid = $bc->get_backupid();
$backupbasepath = $bc->get_plan()->get_basepath();
$bc->execute_plan();

[...]

$controller = new restore_controller($backupid, $ci,
backup::INTERACTIVE_NO, backup::MODE_SAMESITE, $ui,
backup::TARGET_NEW_COURSE);
$controller->execute_precheck();
$controller->execute_plan();

 

I don't understand where is my mistake. I am losing all my hair over this problem!

Please help!

Average of ratings: -
In reply to john doe

Re: Course duplication without content

by john doe -

sad nobody?

In reply to john doe

Re: Course duplication without content

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
You might want to wait at least 24 hours before concluding that no one can answer your question. However, in this case it may be difficult for anyone to answer, as backup in Moodle is complex.

Your best bet would be to read through the code for the backup & restore process, but off the top of my head, I'm fairly certain that the 'import' backup & restore type does not include user data (as that is not normally wanted when importing individual resources from one course to another).
Average of ratings: Useful (1)
In reply to Davo Smith

Re: Course duplication without content

by john doe -

sorry for my eagerness is that I'm going crazy with this story of backup / restore smile.

I just changed my backup function to set parameter "backup :: MODE_GENERAL"

$ bc = new backup_controller (backup :: TYPE_1COURSE $ courseid, backup :: FORMAT_MOODLE,
INTERACTIVE_NO :: backup, backup :: MODE_GENERAL $ userid);

but I am unable to recover with my restore function. I feel that the backup file is not created (in my temp / backup folder ...). So, I get an error like this:

Debug info:
Error code: tmp_backup_directory_not_found
$ a happy [tempdir] / backup/fb3738f70d9f6431ea4aa300dc85024c

there must be a subtlety that I can not understand. mixed

In reply to john doe

Re: Course duplication without content

by john doe -

I do a mistake in my previous post, the function is : 

$bc = new backup_controller(backup::TYPE_1COURSE, $courseid, backup::FORMAT_MOODLE,

backup::INTERACTIVE_NO, backup::MODE_GENERAL, $userid);

 

but the result is the same. 

if someone has an idea, I'm very interested.

In reply to john doe

Re: Course duplication without content

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
For the following code to work, you first need to apply the patch for MDL-34085 (it's still unfixed in 2.3, 2.4, 2.5 and master as of today).

This code assumes you'll run it from the command line and that it's placed at the root Moodle directory. As it's run from the command line, we need to use some user to get the roles and capabilities needed to perform the backup & restore actions. This example uses the admin user to keep the code of the example shorter, BUT DON'T DO THIS IN YOUR PRODUCTION CODE!

Remeber, this is just an example to show the basic functionnality. You should wrap all the operations with the checks for the right capabilities, and so on. You have been warned smile

<?php

define('CLI_SCRIPT', 1);

require_once(dirname(__FILE__).'/config.php');
require_once($CFG->dirroot . '/course/externallib.php');

// The id of the course we are importing FROM
$importcourseid = 2;

// The id of category where we want to place the CLONED course into.
$categoryid = 1;

// If the CLONED course should be visible or not.
$visible = 1;

// The CLONEing options (these are the defaults, change as needed).
$options = array(
array ('name' => 'activities', 'value' => 1),
array ('name' => 'blocks', 'value' => 1),
array ('name' => 'filters', 'value' => 1),
array ('name' => 'users', 'value' => 0),
array ('name' => 'role_assignments', 'value' => 0),
array ('name' => 'comments', 'value' => 0),
array ('name' => 'userscompletion', 'value' => 0),
array ('name' => 'logs', 'value' => 0),
array ('name' => 'grade_histories', 'value' => 0),
);

// To simplify the skeleton code, let's run the whole thing as an
// admin. ***NEVER EVER*** do this in production code.
$USER = get_admin();

try {
$newcourse = core_course_external::duplicate_course($importcourseid,
'The new course full name',
'The new course shortname',
$categoryid,
$visible,
$options);
} catch (exception $e) {
// Some debugging information to see what went wrong
var_dump($e);
}

echo 'Cloned new course with shortname "'. $newcourse['shortname'] . '" and id "' . $newcourse['id'] . "\"\n";