Skip to content

Instantly share code, notes, and snippets.

@xyulex
Created May 31, 2017 13:21
Show Gist options
  • Save xyulex/e34b797957b3633e9691e67345fe3f03 to your computer and use it in GitHub Desktop.
Save xyulex/e34b797957b3633e9691e67345fe3f03 to your computer and use it in GitHub Desktop.
MOODLE: CLI delete courses
#!/usr/bin/php
<?php
define('CLI_SCRIPT', true);
require_once('/var/www/html/altamar/config.php');
require_once('/var/www/html/altamar/course/lib.php');
// to delete a specific course id
if (isset($argv[1])) {
print_r("Deleting course {$argv[1]}...\n");
$id = $argv[1];
$course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
delete_course($course);
fix_course_sortorder(); // important!
}
else {
print_r("Deleting all courses...\n");
// Get array of all courses
$courses = get_courses();
print_r($courses);
print_r("Courses count: " . count($courses) . "\n");
if(count($courses) > 1) { // there is one default course of moodle
foreach ($courses as &$course) {
delete_course($course);
fix_course_sortorder(); // important!
}
}
else {
print_r("\nNo course in the system!\n");
}
}
exit;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment