Skip to content

Commit

Permalink
Merge branch 'master-18177' of https://github.com/timpricecatalyst/mo…
Browse files Browse the repository at this point in the history
  • Loading branch information
danpoltawski committed Feb 24, 2015
2 parents 6856894 + 91d10e7 commit 73d8a50
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 48 deletions.
6 changes: 6 additions & 0 deletions admin/settings/courses.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@
$temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_general_logs', new lang_string('generallogs','backup'), new lang_string('configgenerallogs','backup'), array('value'=>0, 'locked'=>0)));
$temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_general_histories', new lang_string('generalhistories','backup'), new lang_string('configgeneralhistories','backup'), array('value'=>0, 'locked'=>0)));
$temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_general_questionbank', new lang_string('generalquestionbank','backup'), new lang_string('configgeneralquestionbank','backup'), array('value'=>1, 'locked'=>0)));
$temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_general_groups',
new lang_string('generalgroups', 'backup'), new lang_string('configgeneralgroups', 'backup'),
array('value' => 1, 'locked' => 0)));

$ADMIN->add('backups', $temp);

// Create a page for general import configuration and defaults.
Expand Down Expand Up @@ -271,6 +275,8 @@
$temp->add(new admin_setting_configcheckbox('backup/backup_auto_logs', new lang_string('generallogs', 'backup'), new lang_string('configgenerallogs', 'backup'), 0));
$temp->add(new admin_setting_configcheckbox('backup/backup_auto_histories', new lang_string('generalhistories','backup'), new lang_string('configgeneralhistories','backup'), 0));
$temp->add(new admin_setting_configcheckbox('backup/backup_auto_questionbank', new lang_string('generalquestionbank','backup'), new lang_string('configgeneralquestionbank','backup'), 1));
$temp->add(new admin_setting_configcheckbox('backup/backup_auto_groups', new lang_string('generalgroups', 'backup'),
new lang_string('configgeneralgroups', 'backup'), 1));

//$temp->add(new admin_setting_configcheckbox('backup/backup_auto_messages', new lang_string('messages', 'message'), new lang_string('backupmessageshelp','message'), 0));
//$temp->add(new admin_setting_configcheckbox('backup/backup_auto_blogs', new lang_string('blogs', 'blog'), new lang_string('backupblogshelp','blog'), 0));
Expand Down
34 changes: 30 additions & 4 deletions availability/classes/info.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,17 +311,25 @@ protected function warn_about_invalid_availability(\coding_exception $e) {
* @param int $courseid Target course id
* @param \base_logger $logger Logger for any warnings
* @param int $dateoffset Date offset to be added to any dates (0 = none)
* @param \base_task $task Restore task
*/
public function update_after_restore($restoreid, $courseid, \base_logger $logger, $dateoffset) {
public function update_after_restore($restoreid, $courseid, \base_logger $logger,
$dateoffset, \base_task $task) {
$tree = $this->get_availability_tree();
// Set static data for use by get_restore_date_offset function.
self::$restoreinfo = array('restoreid' => $restoreid, 'dateoffset' => $dateoffset);
self::$restoreinfo = array('restoreid' => $restoreid, 'dateoffset' => $dateoffset,
'task' => $task);
$changed = $tree->update_after_restore($restoreid, $courseid, $logger,
$this->get_thing_name());
if ($changed) {
// Save modified data.
$structure = $tree->save();
$this->set_in_database(json_encode($structure));
if ($tree->is_empty()) {
// If the tree is empty, but the tree has changed, remove this condition.
$this->set_in_database(null);
} else {
$structure = $tree->save();
$this->set_in_database(json_encode($structure));
}
}
}

Expand All @@ -343,6 +351,24 @@ public static function get_restore_date_offset($restoreid) {
return self::$restoreinfo['dateoffset'];
}

/**
* Gets the restore task (specifically, the task that calls the
* update_after_restore method) for the current restore.
*
* @param string $restoreid Restore identifier
* @return \base_task Restore task
* @throws coding_exception If not in a restore (or not in that restore)
*/
public static function get_restore_task($restoreid) {
if (!self::$restoreinfo) {
throw new coding_exception('Only valid during restore');
}
if (self::$restoreinfo['restoreid'] !== $restoreid) {
throw new coding_exception('Data not available for that restore id');
}
return self::$restoreinfo['task'];
}

/**
* Obtains the name of the item (cm_info or section_info, at present) that
* this is controlling availability of. Name should be formatted ready
Expand Down
16 changes: 12 additions & 4 deletions availability/classes/tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -663,10 +663,18 @@ protected function recursive_get_all_children($classname, array &$result) {
public function update_after_restore($restoreid, $courseid,
\base_logger $logger, $name) {
$changed = false;
foreach ($this->children as $child) {
$thischanged = $child->update_after_restore($restoreid, $courseid,
$logger, $name);
$changed = $changed || $thischanged;
foreach ($this->children as $index => $child) {
if ($child->include_after_restore($restoreid, $courseid, $logger, $name,
info::get_restore_task($restoreid))) {
$thischanged = $child->update_after_restore($restoreid, $courseid,
$logger, $name);
$changed = $changed || $thischanged;
} else {
unset($this->children[$index]);
unset($this->showchildren[$index]);
$this->showchildren = array_values($this->showchildren);
$changed = true;
}
}
return $changed;
}
Expand Down
23 changes: 22 additions & 1 deletion availability/classes/tree_node.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,34 @@ public abstract function is_available_for_all($not = false);
*/
public abstract function save();

/**
* Checks whether this node should be included after restore or not. The
* node may be removed depending on restore settings, which you can get from
* the $task object.
*
* By default nodes are still included after restore.
*
* @param string $restoreid Restore ID
* @param int $courseid ID of target course
* @param \base_logger $logger Logger for any warnings
* @param string $name Name of this item (for use in warning messages)
* @param \base_task $task Current restore task
* @return bool True if there was any change
*/
public function include_after_restore($restoreid, $courseid, \base_logger $logger, $name,
\base_task $task) {
return true;
}

/**
* Updates this node after restore, returning true if anything changed.
* The default behaviour is simply to return false. If there is a problem
* with the update, $logger can be used to output a warning.
*
* Note: If you need information about the date offset, call
* \core_availability\info::get_restore_date_offset($restoreid).
* \core_availability\info::get_restore_date_offset($restoreid). For
* information on the restoring task and its settings, call
* \core_availability\info::get_restore_task($restoreid).
*
* @param string $restoreid Restore ID
* @param int $courseid ID of target course
Expand Down
17 changes: 17 additions & 0 deletions availability/condition/group/classes/condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ protected function get_debug_string() {
return $this->groupid ? '#' . $this->groupid : 'any';
}

/**
* Include this condition only if we are including groups in restore, or
* if it's a generic 'same activity' one.
*
* @param int $restoreid The restore Id.
* @param int $courseid The ID of the course.
* @param base_logger $logger The logger being used.
* @param string $name Name of item being restored.
* @param base_task $task The task being performed.
*
* @return Integer groupid
*/
public function include_after_restore($restoreid, $courseid, \base_logger $logger,
$name, \base_task $task) {
return !$this->groupid || $task->get_setting_value('groups');
}

public function update_after_restore($restoreid, $courseid, \base_logger $logger, $name) {
global $DB;
if (!$this->groupid) {
Expand Down
17 changes: 17 additions & 0 deletions availability/condition/grouping/classes/condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,23 @@ protected function get_debug_string() {
}
}

/**
* Include this condition only if we are including groups in restore, or
* if it's a generic 'same activity' one.
*
* @param int $restoreid The restore Id.
* @param int $courseid The ID of the course.
* @param base_logger $logger The logger being used.
* @param string $name Name of item being restored.
* @param base_task $task The task being performed.
*
* @return Integer groupid
*/
public function include_after_restore($restoreid, $courseid, \base_logger $logger,
$name, \base_task $task) {
return !$this->groupingid || $task->get_setting_value('groups');
}

public function update_after_restore($restoreid, $courseid, \base_logger $logger, $name) {
global $DB;
if (!$this->groupingid) {
Expand Down
7 changes: 7 additions & 0 deletions availability/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ This files describes API changes in /availability/*.

The information here is intended only for developers.

=== 2.9 ===

* Condition plugins can now implement a new include_after_restore function to
indicate that they should be removed during the restore process. (This is
implemented so that group and grouping conditions are removed if groups are
not restored.)

=== 2.8 ===

* There is a new API function in the info_module/info_section objects (and
Expand Down
6 changes: 4 additions & 2 deletions backup/moodle2/backup_activity_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ public function build() {
// activity and from its related course_modules record and availability
$this->add_step(new backup_module_structure_step('module_info', 'module.xml'));

// Annotate the groups used in already annotated groupings
$this->add_step(new backup_annotate_groups_from_groupings('annotate_groups'));
// Annotate the groups used in already annotated groupings if groups are to be backed up.
if ($this->get_setting_value('groups')) {
$this->add_step(new backup_annotate_groups_from_groupings('annotate_groups'));
}

// Here we add all the common steps for any activity and, in the point of interest
// we call to define_my_steps() is order to get the particular ones inserted in place.
Expand Down
15 changes: 9 additions & 6 deletions backup/moodle2/backup_course_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,18 @@ public function build() {
// Annotate enrolment custom fields.
$this->add_step(new backup_enrolments_execution_step('annotate_enrol_custom_fields'));

// Annotate all the groups and groupings belonging to the course
$this->add_step(new backup_annotate_course_groups_and_groupings('annotate_course_groups'));
// Annotate all the groups and groupings belonging to the course. This can be optional.
if ($this->get_setting_value('groups')) {
$this->add_step(new backup_annotate_course_groups_and_groupings('annotate_course_groups'));
}

// Annotate the groups used in already annotated groupings (note this may be
// unnecessary now that we are annotating all the course groups and groupings in the
// step above. But we keep it working in case we decide, someday, to introduce one
// setting to transform the step above into an optional one. This is here to support
// course->defaultgroupingid
$this->add_step(new backup_annotate_groups_from_groupings('annotate_groups_from_groupings'));
// step above). This is here to support course->defaultgroupingid.
// This may not be required to annotate if groups are not being backed up.
if ($this->get_setting_value('groups')) {
$this->add_step(new backup_annotate_groups_from_groupings('annotate_groups_from_groupings'));
}

// Annotate the question_categories belonging to the course context (conditionally).
if ($this->get_setting_value('questionbank')) {
Expand Down
4 changes: 4 additions & 0 deletions backup/moodle2/backup_root_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,9 @@ protected function define_settings() {
$questionbank = new backup_generic_setting('questionbank', base_setting::IS_BOOLEAN, true);
$questionbank->set_ui(new backup_setting_ui_checkbox($questionbank, get_string('rootsettingquestionbank', 'backup')));
$this->add_setting($questionbank);

$groups = new backup_groups_setting('groups', base_setting::IS_BOOLEAN, true);
$groups->set_ui(new backup_setting_ui_checkbox($groups, get_string('rootsettinggroups', 'backup')));
$this->add_setting($groups);
}
}
9 changes: 9 additions & 0 deletions backup/moodle2/backup_settingslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ public function set_ui_filename($label, $value, array $options = null) {
*/
class backup_users_setting extends backup_generic_setting {}

/**
* root setting to control if backup will include group information depends on @backup_users_setting
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright 2014 Matt Sammarco
*/
class backup_groups_setting extends backup_generic_setting {
}

/**
* root setting to control if backup will include activities or no.
* A lot of other settings (_included at activity levels)
Expand Down
48 changes: 26 additions & 22 deletions backup/moodle2/backup_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1149,8 +1149,10 @@ class backup_groups_structure_step extends backup_structure_step {

protected function define_structure() {

// To know if we are including users
$users = $this->get_setting_value('users');
// To know if we are including users.
$userinfo = $this->get_setting_value('users');
// To know if we are including groups and groupings.
$groupinfo = $this->get_setting_value('groups');

// Define each element separated

Expand Down Expand Up @@ -1190,27 +1192,29 @@ protected function define_structure() {

// Define sources

$group->set_source_sql("
SELECT g.*
FROM {groups} g
JOIN {backup_ids_temp} bi ON g.id = bi.itemid
WHERE bi.backupid = ?
AND bi.itemname = 'groupfinal'", array(backup::VAR_BACKUPID));

// This only happens if we are including users
if ($users) {
$member->set_source_table('groups_members', array('groupid' => backup::VAR_PARENTID));
// This only happens if we are including groups/groupings.
if ($groupinfo) {
$group->set_source_sql("
SELECT g.*
FROM {groups} g
JOIN {backup_ids_temp} bi ON g.id = bi.itemid
WHERE bi.backupid = ?
AND bi.itemname = 'groupfinal'", array(backup::VAR_BACKUPID));

$grouping->set_source_sql("
SELECT g.*
FROM {groupings} g
JOIN {backup_ids_temp} bi ON g.id = bi.itemid
WHERE bi.backupid = ?
AND bi.itemname = 'groupingfinal'", array(backup::VAR_BACKUPID));
$groupinggroup->set_source_table('groupings_groups', array('groupingid' => backup::VAR_PARENTID));

// This only happens if we are including users.
if ($userinfo) {
$member->set_source_table('groups_members', array('groupid' => backup::VAR_PARENTID));
}
}

$grouping->set_source_sql("
SELECT g.*
FROM {groupings} g
JOIN {backup_ids_temp} bi ON g.id = bi.itemid
WHERE bi.backupid = ?
AND bi.itemname = 'groupingfinal'", array(backup::VAR_BACKUPID));

$groupinggroup->set_source_table('groupings_groups', array('groupingid' => backup::VAR_PARENTID));

// Define id annotations (as final)

$member->annotate_ids('userfinal', 'userid');
Expand Down Expand Up @@ -1241,7 +1245,7 @@ protected function define_structure() {
// To know if we are including role assignments
$roleassignments = $this->get_setting_value('role_assignments');

// Define each element separated
// Define each element separate.

$users = new backup_nested_element('users');

Expand Down
17 changes: 17 additions & 0 deletions backup/moodle2/restore_root_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,22 @@ protected function define_settings() {
// The restore does not process the grade histories when some activities are ignored.
// So let's define a dependency to prevent false expectations from our users.
$activities->add_dependency($gradehistories);

// Define groups and groupings.
$defaultvalue = false;
$changeable = false;
if (isset($rootsettings['groups']) && $rootsettings['groups']) { // Only enabled when available.
$defaultvalue = true;
$changeable = true;
} else if (!isset($rootsettings['groups'])) {
// It is likely this is an older backup that does not contain information on the group setting,
// in which case groups should be restored and this setting can be changed.
$defaultvalue = true;
$changeable = true;
}
$groups = new restore_groups_setting('groups', base_setting::IS_BOOLEAN, $defaultvalue);
$groups->set_ui(new backup_setting_ui_checkbox($groups, get_string('rootsettinggroups', 'backup')));
$groups->get_ui()->set_changeable($changeable);
$this->add_setting($groups);
}
}
9 changes: 9 additions & 0 deletions backup/moodle2/restore_settingslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ class restore_generic_setting extends root_backup_setting {}
*/
class restore_users_setting extends restore_generic_setting {}

/**
* root setting to control if restore will create groups/grouping information. Depends on @restore_users_setting
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright 2014 Matt Sammarco
*/
class restore_groups_setting extends restore_generic_setting {
}

/**
* root setting to control if restore will create role assignments
* or no (any level), depends of @restore_users_setting
Expand Down
Loading

0 comments on commit 73d8a50

Please sign in to comment.