mirror of
https://github.com/moodle/moodle.git
synced 2025-05-01 22:18:03 +02:00
MDL-51467 Availability: Course reset should change restrict dates
This commit is contained in:
parent
35d3e8b00b
commit
fc830670a2
@ -234,4 +234,68 @@ class condition extends \core_availability\condition {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes all date restrictions on a course by the specified shift amount.
|
||||
* Used by the course reset feature.
|
||||
*
|
||||
* @param int $courseid Course id
|
||||
* @param int $timeshift Offset in seconds
|
||||
*/
|
||||
public static function update_all_dates($courseid, $timeshift) {
|
||||
global $DB;
|
||||
|
||||
$modinfo = get_fast_modinfo($courseid);
|
||||
$anychanged = false;
|
||||
|
||||
// Adjust dates from all course modules.
|
||||
foreach ($modinfo->cms as $cm) {
|
||||
if (!$cm->availability) {
|
||||
continue;
|
||||
}
|
||||
$info = new \core_availability\info_module($cm);
|
||||
$tree = $info->get_availability_tree();
|
||||
$dates = $tree->get_all_children('availability_date\condition');
|
||||
$changed = false;
|
||||
foreach ($dates as $date) {
|
||||
$date->time += $timeshift;
|
||||
$changed = true;
|
||||
}
|
||||
|
||||
// Save the updated course module.
|
||||
if ($changed) {
|
||||
$DB->set_field('course_modules', 'availability', json_encode($tree->save()),
|
||||
array('id' => $cm->id));
|
||||
$anychanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Adjust dates from all course sections.
|
||||
foreach ($modinfo->get_section_info_all() as $section) {
|
||||
if (!$section->availability) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$info = new \core_availability\info_section($section);
|
||||
$tree = $info->get_availability_tree();
|
||||
$dates = $tree->get_all_children('availability_date\condition');
|
||||
$changed = false;
|
||||
foreach ($dates as $date) {
|
||||
$date->time += $timeshift;
|
||||
$changed = true;
|
||||
}
|
||||
|
||||
// Save the updated course module.
|
||||
if ($changed) {
|
||||
$DB->set_field('course_sections', 'availability', json_encode($tree->save()),
|
||||
array('id' => $section->id));
|
||||
$anychanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure course cache is cleared if required.
|
||||
if ($anychanged) {
|
||||
rebuild_course_cache($courseid, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,7 @@
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use availability_date\condition;
|
||||
use core_availability\tree;
|
||||
|
||||
/**
|
||||
* Unit tests for the date condition.
|
||||
@ -236,4 +237,53 @@ class availability_date_condition_testcase extends advanced_testcase {
|
||||
$information = $date->get_standalone_description(true, true, $info);
|
||||
$this->assertRegExp('~from.*5 March 2014([^0-9]*)$~', $information);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the update_all_dates function.
|
||||
*/
|
||||
public function test_update_all_dates() {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Create a course with 3 pages.
|
||||
$generator = $this->getDataGenerator();
|
||||
$course = $generator->create_course();
|
||||
$rec = array('course' => $course);
|
||||
$page1 = $generator->get_plugin_generator('mod_page')->create_instance($rec);
|
||||
$page2 = $generator->get_plugin_generator('mod_page')->create_instance($rec);
|
||||
$page3 = $generator->get_plugin_generator('mod_page')->create_instance($rec);
|
||||
|
||||
// Set the availability page 2 to a simple date condition. You can access
|
||||
// it from 1337 onwards.
|
||||
$simplecondition = tree::get_root_json(array(
|
||||
condition::get_json(condition::DIRECTION_FROM, 1337)));
|
||||
$DB->set_field('course_modules', 'availability',
|
||||
json_encode($simplecondition), array('id' => $page2->cmid));
|
||||
|
||||
// Set page 3 to a complex set of conditions including a nested date condition.
|
||||
// You can access it until 1459, *or* after 2810 if you belong to a group.
|
||||
$complexcondition = tree::get_root_json(array(
|
||||
condition::get_json(condition::DIRECTION_UNTIL, 1459),
|
||||
tree::get_nested_json(array(
|
||||
condition::get_json(condition::DIRECTION_FROM, 2810),
|
||||
\availability_group\condition::get_json()))),
|
||||
tree::OP_OR);
|
||||
$DB->set_field('course_modules', 'availability',
|
||||
json_encode($complexcondition), array('id' => $page3->cmid));
|
||||
|
||||
// Now use the update_all_dates function to move date forward 100000.
|
||||
condition::update_all_dates($course->id, 100000);
|
||||
|
||||
// Get the expected conditions after adjusting time, and compare to database.
|
||||
$simplecondition->c[0]->t = 101337;
|
||||
$complexcondition->c[0]->t = 101459;
|
||||
$complexcondition->c[1]->c[0]->t = 102810;
|
||||
$this->assertEquals($simplecondition, json_decode(
|
||||
$DB->get_field('course_modules', 'availability', array('id' => $page2->cmid))));
|
||||
$this->assertEquals($complexcondition, json_decode(
|
||||
$DB->get_field('course_modules', 'availability', array('id' => $page3->cmid))));
|
||||
|
||||
// The one without availability conditions should still be null.
|
||||
$this->assertNull($DB->get_field('course_modules', 'availability', array('id' => $page1->cmid)));
|
||||
}
|
||||
}
|
||||
|
@ -246,7 +246,12 @@ class condition extends \core_availability\condition {
|
||||
* @return stdClass Object representing condition
|
||||
*/
|
||||
public static function get_json($groupid = 0) {
|
||||
return (object)array('type' => 'group', 'id' => (int)$groupid);
|
||||
$result = (object)array('type' => 'group');
|
||||
// Id is only included if set.
|
||||
if ($groupid) {
|
||||
$result->id = (int)$groupid;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function get_user_list_sql($not, \core_availability\info $info, $onlyactive) {
|
||||
|
@ -5066,6 +5066,11 @@ function reset_course_userdata($data) {
|
||||
WHERE courseid=? AND instance=0";
|
||||
$DB->execute($updatesql, array($data->timeshift, $data->courseid));
|
||||
|
||||
// Update any date activity restrictions.
|
||||
if ($CFG->enableavailability) {
|
||||
\availability_date\condition::update_all_dates($data->courseid, $data->timeshift);
|
||||
}
|
||||
|
||||
$status[] = array('component' => $componentstr, 'item' => get_string('datechanged'), 'error' => false);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user