Skip to content

Instantly share code, notes, and snippets.

@danielneis
Created April 30, 2020 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielneis/89239c34619766411a02544e9daba1d4 to your computer and use it in GitHub Desktop.
Save danielneis/89239c34619766411a02544e9daba1d4 to your computer and use it in GitHub Desktop.
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* CLI script to bulk update availability conditions.
*
* @package core
* @subpackage cli
* @copyright 2020 Daniel Neis Araujo <danielneis@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('CLI_SCRIPT', true);
require(__DIR__ . '/../../config.php');
require_once($CFG->libdir . '/clilib.php');
require_once($CFG->libdir . '/gradelib.php');
require_once($CFG->dirroot . '/course/lib.php');
require_once($CFG->dirroot . '/course/modlib.php');
$courses = $DB->get_records('course', []);
$USER = get_admin();
$newdate = strtotime('2020-12-31 23:55');
foreach ($courses as $course) {
$coursemods = get_course_mods($course->id);
foreach ($coursemods as $cm) {
$cm = get_coursemodule_from_id('', $cm->id, 0, false, MUST_EXIST);
list($cm, $context, $module, $moduleinfo, $cw) = get_moduleinfo_data($cm, $course);
// Attempt to include module library before we make any changes to DB.
include_modulelib($moduleinfo->modulename);
$moduleinfo->course = $course->id;
$moduleinfo = set_moduleinfo_defaults($moduleinfo);
if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) {
$moduleinfo->groupmode = $cm->groupmode; // Keep original.
}
if (!empty($CFG->enableavailability)) {
if (property_exists($moduleinfo, 'availabilityconditionsjson')) {
if ($moduleinfo->availabilityconditionsjson !== '') {
$decoded = json_decode($moduleinfo->availabilityconditionsjson);
if ($decoded) {
foreach ($decoded->c as $condition) {
if (($condition->type == 'date') && ($condition->d == '<')) {
$condition->t = $newdate;
}
}
$cm->availability = json_encode($decoded);
} else {
$cm->availability = null;
}
} else {
$cm->availability = null;
}
} else if (property_exists($moduleinfo, 'availability')) {
$cm->availability = $moduleinfo->availability;
}
// If there is any availability data, verify it.
if ($cm->availability) {
$tree = new \core_availability\tree(json_decode($cm->availability));
// Save time and database space by setting null if the only data
// is an empty tree.
if ($tree->is_empty()) {
$cm->availability = null;
}
}
}
$DB->update_record('course_modules', $cm);
$modcontext = context_module::instance($moduleinfo->coursemodule);
$cm->name = $moduleinfo->name;
\core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger();
$moduleinfo = edit_module_post_actions($moduleinfo, $course);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment