Skip to content

Commit

Permalink
Merge branch 'master' of github.com:tmuras/moosh
Browse files Browse the repository at this point in the history
  • Loading branch information
odziminski committed May 15, 2023
2 parents 66b6983 + b6e967c commit 38e330b
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 263 deletions.
24 changes: 21 additions & 3 deletions Moosh/Command/Moodle39/Course/CourseBackup.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ public function __construct()

$this->addOption('f|filename:', 'path to filename to save the course backup');
$this->addOption('p|path:', 'path to save the course backup');
$this->addOption('s|section:', 'include only this section in backup');
$this->addOption('F|fullbackup', 'do full backup instead of general');
$this->addOption('template', 'do template backup instead of general');

$this->addArgument('id');

}

public function execute()
Expand All @@ -33,18 +35,28 @@ public function execute()

//check if course id exists
$course = $DB->get_record('course', array('id' => $this->arguments[0]), '*', MUST_EXIST);

$shortname = str_replace(' ', '_', $course->shortname);

$options = $this->expandedOptions;

if ($options['section']) {
$section = $DB->get_record('course_sections', array('course' => $this->arguments[0], 'section' => $options['section']), '*', MUST_EXIST);
}

$cwd=$this->cwd;
if (trim($options['path'])!="") {
$cwd=$options['path'];
}

if (!$options['filename']) {
$options['filename'] = $cwd . '/backup_' . $this->arguments[0] . "_". str_replace('/','_',$shortname) . '_' . date('Y.m.d') . '.mbz';
if ($options['section']) {
// clean up the section name, remove invalid characters, etc. so we can use it in the filename
$sectionfilename = str_replace(' ','-',$section->name);
$sectionfilename = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $sectionfilename); $sectionfilename = mb_ereg_replace("([\.]{2,})", '', $sectionfilename);
$options['filename'] = $cwd . '/backup_' . $this->arguments[0] . "_". str_replace('/','_',$shortname) . '_' . $section->section . '_' . $sectionfilename . '_' . date('Y.m.d') . '.mbz';
} else {
$options['filename'] = $cwd . '/backup_' . $this->arguments[0] . "_". str_replace('/','_',$shortname) . '_' . date('Y.m.d') . '.mbz';
}
} elseif ($options['filename'][0] != '/') {
$options['filename'] = $cwd .'/' .$options['filename'];
}
Expand All @@ -54,8 +66,14 @@ public function execute()
cli_error("File '{$options['filename']}' already exists, I will not over-write it.");
}

$bc = new backup_controller(\backup::TYPE_1COURSE, $this->arguments[0], backup::FORMAT_MOODLE,
if ($options['section']) {
$bc = new backup_controller(\backup::TYPE_1SECTION, $section->id, backup::FORMAT_MOODLE,
backup::INTERACTIVE_YES, backup::MODE_GENERAL, $USER->id);
} else {
$bc = new backup_controller(\backup::TYPE_1COURSE, $this->arguments[0], backup::FORMAT_MOODLE,
backup::INTERACTIVE_YES, backup::MODE_GENERAL, $USER->id);
}


if ($options['fullbackup']) {
$tasks = $bc->get_plan()->get_tasks();
Expand Down
54 changes: 54 additions & 0 deletions Moosh/Command/Moodle39/Course/CourseMove.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* moosh - Moodle Shell
*
* @copyright 2012 onwards Tomasz Muras
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace Moosh\Command\Moodle39\Course;

use Moosh\MooshCommand;

class CourseMove extends MooshCommand
{
public function __construct()
{
parent::__construct('move', 'course');

$this->addArgument('course_ids');
$this->addArgument('destination_category_id');

$this->maxArguments = 2;
}

public function execute()
{
global $DB;

$courseIdsArg = $this->arguments[0];
$categoryId = $this->arguments[1];

if (!$DB->record_exists('course_categories', array('id' => $categoryId))) {
cli_error("Category with id $categoryId not found");
}

$category = \core_course_category::get($categoryId);

$courseIds = array_map('intval', explode(',', $courseIdsArg));
foreach ($courseIds as $courseId) {
if (!$DB->record_exists('course', array('id' => $courseId))) {
cli_error("Course with id $courseId not found");
}
}

\core_course\management\helper::move_courses_into_category($category, $courseIds);
}

protected function getArgumentsHelp()
{
return parent::getArgumentsHelp()
. "\n\t* You can specify a comma separated list of course ids as the first argument.";
}
}
13 changes: 13 additions & 0 deletions Moosh/Command/Moodle39/Course/CourseRestore.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,28 @@ public function execute() {
$xmlfile = $path . DIRECTORY_SEPARATOR . "moodle.xml";
}

// Different XML filename in single-section backup
if (!file_exists($xmlfile)) {
$xmlfile = $path . DIRECTORY_SEPARATOR . "moodle_backup.xml";
}

$xml = simplexml_load_file($xmlfile);
$fullname = $xml->xpath('/course/fullname');
if (!$fullname) {
$fullname = $xml->xpath('/MOODLE_BACKUP/COURSE/HEADER/FULLNAME');
}
// Different XML filename in single-section backup
if (!$fullname) {
$fullname = $xml->xpath('/moodle_backup/information/original_course_fullname');
}
$shortname = $xml->xpath('/course/shortname');
if (!$shortname) {
$shortname = $xml->xpath('/MOODLE_BACKUP/COURSE/HEADER/SHORTNAME');
}
// Different XML filename in single-section backup
if (!$shortname) {
$shortname = $xml->xpath('/moodle_backup/information/original_course_shortname');
}

$fullname = (string)($fullname[0]);
$shortname = (string)($shortname[0]);
Expand Down
Loading

0 comments on commit 38e330b

Please sign in to comment.