mirror of
https://github.com/moodle/moodle.git
synced 2025-08-10 03:16:42 +02:00
MDL-80191 backup: prepare UI for subsections
Backup and restore settings needs to be refactored before introducing subsections. With the current code adding two new hierachy levels (subsections and activities in subsections) will require many unnecessary lines of code because most of the structure is hard-coded. With this patch when backup/restore present the course structure uses a stack to control how mani divs are opened, instead of relying on adhoc class attributes per each div. About the tasks for sections and activities, the class has been refactored. This way when subsections are introduced it will require less lines of code and the final patch will be more comprehensible.
This commit is contained in:
parent
ad7fc69c25
commit
7482013cd2
6 changed files with 319 additions and 148 deletions
|
@ -123,12 +123,12 @@ abstract class backup_activity_task extends backup_task {
|
|||
}
|
||||
|
||||
// Add some extra settings that related processors are going to need
|
||||
$this->add_setting(new backup_activity_generic_setting(backup::VAR_MODID, base_setting::IS_INTEGER, $this->moduleid));
|
||||
$this->add_setting(new backup_activity_generic_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $this->get_courseid()));
|
||||
$this->add_setting(new backup_activity_generic_setting(backup::VAR_SECTIONID, base_setting::IS_INTEGER, $this->sectionid));
|
||||
$this->add_setting(new backup_activity_generic_setting(backup::VAR_MODNAME, base_setting::IS_FILENAME, $this->modulename));
|
||||
$this->add_setting(new backup_activity_generic_setting(backup::VAR_ACTIVITYID, base_setting::IS_INTEGER, $this->activityid));
|
||||
$this->add_setting(new backup_activity_generic_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $this->contextid));
|
||||
$this->add_section_setting(backup::VAR_MODID, base_setting::IS_INTEGER, $this->moduleid);
|
||||
$this->add_section_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $this->get_courseid());
|
||||
$this->add_section_setting(backup::VAR_SECTIONID, base_setting::IS_INTEGER, $this->sectionid);
|
||||
$this->add_section_setting(backup::VAR_MODNAME, base_setting::IS_FILENAME, $this->modulename);
|
||||
$this->add_section_setting(backup::VAR_ACTIVITYID, base_setting::IS_INTEGER, $this->activityid);
|
||||
$this->add_section_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $this->contextid);
|
||||
|
||||
// Create the activity directory
|
||||
$this->add_step(new create_taskbasepath_directory('create_activity_directory'));
|
||||
|
@ -269,58 +269,99 @@ abstract class backup_activity_task extends backup_task {
|
|||
$settingprefix = $this->modulename . '_' . $this->moduleid . '_';
|
||||
|
||||
// All these are common settings to be shared by all activities.
|
||||
|
||||
// Define activity_include (to decide if the whole task must be really executed)
|
||||
// Dependent of:
|
||||
// - activities root setting.
|
||||
// - section_included setting (if exists).
|
||||
$settingname = $settingprefix . 'included';
|
||||
$activityincluded = new backup_activity_generic_setting($settingname, base_setting::IS_BOOLEAN, true);
|
||||
$activityincluded->get_ui()->set_icon(new image_icon('monologo', get_string('pluginname', $this->modulename),
|
||||
$this->modulename, array('class' => 'iconlarge icon-post ml-1')));
|
||||
$this->add_setting($activityincluded);
|
||||
// Look for "activities" root setting.
|
||||
$activities = $this->plan->get_setting('activities');
|
||||
$activities->add_dependency($activityincluded);
|
||||
$activityincluded = $this->add_activity_included_setting($settingprefix);
|
||||
|
||||
if (question_module_uses_questions($this->modulename)) {
|
||||
$questionbank = $this->plan->get_setting('questionbank');
|
||||
$questionbank->add_dependency($activityincluded);
|
||||
}
|
||||
|
||||
$this->add_activity_userinfo_setting($settingprefix, $activityincluded);
|
||||
|
||||
// End of common activity settings, let's add the particular ones.
|
||||
$this->define_my_settings();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a setting to the task. This method is used to add a setting to the task
|
||||
*
|
||||
* @param int|string $identifier the identifier of the setting
|
||||
* @param string $type the type of the setting
|
||||
* @param string|int $value the value of the setting
|
||||
* @return section_backup_setting the setting added
|
||||
*/
|
||||
protected function add_section_setting(int|string $identifier, string $type, string|int $value): activity_backup_setting {
|
||||
$setting = new backup_activity_generic_setting($identifier, $type, $value);
|
||||
$this->add_setting($setting);
|
||||
return $setting;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the section include setting to the task.
|
||||
*
|
||||
* @param string $settingprefix the identifier of the setting
|
||||
* @return activity_backup_setting the setting added
|
||||
*/
|
||||
protected function add_activity_included_setting(string $settingprefix): activity_backup_setting {
|
||||
// Define activity_include (to decide if the whole task must be really executed)
|
||||
// Dependent of:
|
||||
// - activities root setting.
|
||||
// - sectionincluded setting (if exists).
|
||||
$settingname = $settingprefix . 'included';
|
||||
$activityincluded = new backup_activity_generic_setting($settingname, base_setting::IS_BOOLEAN, true);
|
||||
$activityincluded->get_ui()->set_icon(new image_icon('monologo', get_string('pluginname', $this->modulename),
|
||||
$this->modulename, array('class' => 'iconlarge icon-post ml-1')));
|
||||
$this->add_setting($activityincluded);
|
||||
|
||||
// Look for "activities" root setting.
|
||||
$activities = $this->plan->get_setting('activities');
|
||||
$activities->add_dependency($activityincluded);
|
||||
|
||||
// Look for "sectionincluded" section setting (if exists).
|
||||
$settingname = 'section_' . $this->sectionid . '_included';
|
||||
if ($this->plan->setting_exists($settingname)) {
|
||||
$sectionincluded = $this->plan->get_setting($settingname);
|
||||
$sectionincluded->add_dependency($activityincluded);
|
||||
}
|
||||
return $activityincluded;
|
||||
}
|
||||
|
||||
// Define activityuserinfo. Dependent of:
|
||||
/**
|
||||
* Add the section userinfo setting to the task.
|
||||
*
|
||||
* @param string $settingprefix the identifier of the setting
|
||||
* @param activity_backup_setting $includefield the setting to depend on
|
||||
* @return activity_backup_setting the setting added
|
||||
*/
|
||||
protected function add_activity_userinfo_setting(
|
||||
string $settingprefix,
|
||||
activity_backup_setting $includefield
|
||||
): activity_backup_setting {
|
||||
// Define activity_userinfo. Dependent of:
|
||||
// - users root setting.
|
||||
// - sectionuserinfo setting (if exists).
|
||||
// - activityincluded setting.
|
||||
// - includefield setting.
|
||||
$settingname = $settingprefix . 'userinfo';
|
||||
$activityuserinfo = new backup_activity_userinfo_setting($settingname, base_setting::IS_BOOLEAN, true);
|
||||
|
||||
$activityuserinfo->get_ui()->set_label('-');
|
||||
$activityuserinfo->get_ui()->set_visually_hidden_label(
|
||||
get_string('includeuserinfo_instance', 'core_backup', $this->name)
|
||||
);
|
||||
$this->add_setting($activityuserinfo);
|
||||
|
||||
// Look for "users" root setting.
|
||||
$users = $this->plan->get_setting('users');
|
||||
$users->add_dependency($activityuserinfo);
|
||||
|
||||
// Look for "sectionuserinfo" section setting (if exists).
|
||||
$settingname = 'section_' . $this->sectionid . '_userinfo';
|
||||
if ($this->plan->setting_exists($settingname)) {
|
||||
$sectionuserinfo = $this->plan->get_setting($settingname);
|
||||
$sectionuserinfo->add_dependency($activityuserinfo);
|
||||
}
|
||||
// Look for "activityincluded" setting.
|
||||
$activityincluded->add_dependency($activityuserinfo);
|
||||
|
||||
// End of common activity settings, let's add the particular ones.
|
||||
$this->define_my_settings();
|
||||
$includefield->add_dependency($activityuserinfo);
|
||||
return $activityuserinfo;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -37,6 +37,11 @@ class backup_section_task extends backup_task {
|
|||
|
||||
protected $sectionid;
|
||||
|
||||
/**
|
||||
* @var stdClass $section The database section object.
|
||||
*/
|
||||
protected stdClass $section;
|
||||
|
||||
/**
|
||||
* Constructor - instantiates one object of this class
|
||||
*/
|
||||
|
@ -48,6 +53,7 @@ class backup_section_task extends backup_task {
|
|||
throw new backup_task_exception('section_task_section_not_found', $sectionid);
|
||||
}
|
||||
|
||||
$this->section = $section;
|
||||
$this->sectionid = $sectionid;
|
||||
|
||||
parent::__construct($name, $plan);
|
||||
|
@ -72,11 +78,11 @@ class backup_section_task extends backup_task {
|
|||
|
||||
// Set the backup::VAR_CONTEXTID setting to course context as far as next steps require that
|
||||
$coursectxid = context_course::instance($this->get_courseid())->id;
|
||||
$this->add_setting(new backup_activity_generic_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $coursectxid));
|
||||
$this->add_section_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $coursectxid);
|
||||
|
||||
// Add some extra settings that related processors are going to need
|
||||
$this->add_setting(new backup_activity_generic_setting(backup::VAR_SECTIONID, base_setting::IS_INTEGER, $this->sectionid));
|
||||
$this->add_setting(new backup_activity_generic_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $this->get_courseid()));
|
||||
$this->add_section_setting(backup::VAR_SECTIONID, base_setting::IS_INTEGER, $this->sectionid);
|
||||
$this->add_section_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $this->get_courseid());
|
||||
|
||||
// Create the section directory
|
||||
$this->add_step(new create_taskbasepath_directory('create_section_directory'));
|
||||
|
@ -145,32 +151,74 @@ class backup_section_task extends backup_task {
|
|||
// All the settings related to this activity will include this prefix.
|
||||
$settingprefix = 'section_' . $this->sectionid . '_';
|
||||
|
||||
// All these are common settings to be shared by all sections.
|
||||
$incudefield = $this->add_section_included_setting($settingprefix);
|
||||
$this->add_section_userinfo_setting($settingprefix, $incudefield);
|
||||
}
|
||||
|
||||
$section = $DB->get_record('course_sections', array('id' => $this->sectionid), '*', MUST_EXIST);
|
||||
$course = $DB->get_record('course', array('id' => $section->course), '*', MUST_EXIST);
|
||||
/**
|
||||
* Add a setting to the task. This method is used to add a setting to the task
|
||||
*
|
||||
* @param int|string $identifier the identifier of the setting
|
||||
* @param string $type the type of the setting
|
||||
* @param string|int $value the value of the setting
|
||||
* @return section_backup_setting the setting added
|
||||
*/
|
||||
protected function add_section_setting(int|string $identifier, string $type, string|int $value): section_backup_setting {
|
||||
$setting = new backup_section_generic_setting($identifier, $type, $value);
|
||||
$this->add_setting($setting);
|
||||
return $setting;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the section included setting to the task.
|
||||
*
|
||||
* @param string $settingprefix the identifier of the setting
|
||||
* @return section_backup_setting the setting added
|
||||
*/
|
||||
protected function add_section_included_setting(string $settingprefix): section_backup_setting {
|
||||
global $DB;
|
||||
$course = $DB->get_record('course', ['id' => $this->section->course], '*', MUST_EXIST);
|
||||
|
||||
// Define sectionincluded (to decide if the whole task must be really executed).
|
||||
$settingname = $settingprefix . 'included';
|
||||
|
||||
$sectionincluded = new backup_section_included_setting($settingname, base_setting::IS_BOOLEAN, true);
|
||||
$sectionincluded->get_ui()->set_label(get_section_name($course, $section));
|
||||
|
||||
$sectionincluded->get_ui()->set_label(get_section_name($course, $this->section));
|
||||
$this->add_setting($sectionincluded);
|
||||
|
||||
return $sectionincluded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the section userinfo setting to the task.
|
||||
*
|
||||
* @param string $settingprefix the identifier of the setting
|
||||
* @param section_backup_setting $includefield the setting to depend on
|
||||
* @return section_backup_setting the setting added
|
||||
*/
|
||||
protected function add_section_userinfo_setting(
|
||||
string $settingprefix,
|
||||
section_backup_setting $includefield
|
||||
): section_backup_setting {
|
||||
// Define sectionuserinfo. Dependent of:
|
||||
// - users root setting.
|
||||
// - sectionincluded setting.
|
||||
// - section_included setting.
|
||||
$settingname = $settingprefix . 'userinfo';
|
||||
|
||||
$sectionuserinfo = new backup_section_userinfo_setting($settingname, base_setting::IS_BOOLEAN, true);
|
||||
|
||||
$sectionuserinfo->get_ui()->set_label(get_string('includeuserinfo', 'backup'));
|
||||
$sectionuserinfo->get_ui()->set_visually_hidden_label(
|
||||
get_string('section_prefix', 'core_backup', $section->name ?: $section->section)
|
||||
get_string('section_prefix', 'core_backup', $this->section->name ?: $this->section->section)
|
||||
);
|
||||
$this->add_setting($sectionuserinfo);
|
||||
|
||||
// Look for "users" root setting.
|
||||
$users = $this->plan->get_setting('users');
|
||||
$users->add_dependency($sectionuserinfo);
|
||||
// Look for "section_included" section setting.
|
||||
$sectionincluded->add_dependency($sectionuserinfo);
|
||||
$includefield->add_dependency($sectionuserinfo);
|
||||
|
||||
return $sectionuserinfo;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -288,42 +288,71 @@ abstract class restore_activity_task extends restore_task {
|
|||
* Define the common setting that any restore activity will have
|
||||
*/
|
||||
protected function define_settings() {
|
||||
|
||||
// All the settings related to this activity will include this prefix
|
||||
$settingprefix = $this->info->modulename . '_' . $this->info->moduleid . '_';
|
||||
|
||||
// All these are common settings to be shared by all activities
|
||||
$activityincluded = $this->add_activity_included_setting($settingprefix);
|
||||
$this->add_activity_userinfo_setting($settingprefix, $activityincluded);
|
||||
|
||||
// Define activity_include (to decide if the whole task must be really executed)
|
||||
// End of common activity settings, let's add the particular ones.
|
||||
$this->define_my_settings();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the activity included setting to the task.
|
||||
*
|
||||
* @param string $settingprefix the identifier of the setting
|
||||
* @return activity_backup_setting the setting added
|
||||
*/
|
||||
protected function add_activity_included_setting(string $settingprefix): activity_backup_setting {
|
||||
// Define activity_included (to decide if the whole task must be really executed)
|
||||
// Dependent of:
|
||||
// - activities root setting
|
||||
// - section_included setting (if exists)
|
||||
// - activities root setting.
|
||||
// - sectionincluded setting (if exists).
|
||||
$settingname = $settingprefix . 'included';
|
||||
$activity_included = new restore_activity_generic_setting($settingname, base_setting::IS_BOOLEAN, true);
|
||||
$activity_included->get_ui()->set_icon(new image_icon('monologo', get_string('pluginname', $this->modulename),
|
||||
$this->modulename, array('class' => 'iconlarge icon-post ml-1')));
|
||||
$this->add_setting($activity_included);
|
||||
// Look for "activities" root setting
|
||||
|
||||
$activityincluded = new restore_activity_generic_setting($settingname, base_setting::IS_BOOLEAN, true);
|
||||
|
||||
$activityincluded->get_ui()->set_icon(new image_icon('monologo', get_string('pluginname', $this->modulename),
|
||||
$this->modulename, ['class' => 'iconlarge icon-post ml-1']));
|
||||
$this->add_setting($activityincluded);
|
||||
// Look for "activities" root setting.
|
||||
$activities = $this->plan->get_setting('activities');
|
||||
$activities->add_dependency($activity_included);
|
||||
// Look for "section_included" section setting (if exists)
|
||||
$activities->add_dependency($activityincluded);
|
||||
// Look for "sectionincluded" section setting (if exists).
|
||||
$settingname = 'section_' . $this->info->sectionid . '_included';
|
||||
if ($this->plan->setting_exists($settingname)) {
|
||||
$section_included = $this->plan->get_setting($settingname);
|
||||
$section_included->add_dependency($activity_included);
|
||||
$sectionincluded = $this->plan->get_setting($settingname);
|
||||
$sectionincluded->add_dependency($activityincluded);
|
||||
}
|
||||
|
||||
// Define activity_userinfo. Dependent of:
|
||||
// - users root setting
|
||||
// - section_userinfo setting (if exists)
|
||||
// - activity_included setting.
|
||||
return $activityincluded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the activity userinfo setting to the task.
|
||||
*
|
||||
* @param string $settingprefix the identifier of the setting
|
||||
* @param activity_backup_setting $includefield the activity included setting
|
||||
* @return activity_backup_setting the setting added
|
||||
*/
|
||||
protected function add_activity_userinfo_setting(
|
||||
string $settingprefix,
|
||||
activity_backup_setting $includefield
|
||||
): activity_backup_setting {
|
||||
// Define activityuserinfo. Dependent of:
|
||||
// - users root setting.
|
||||
// - sectionuserinfo setting (if exists).
|
||||
// - activity included setting.
|
||||
$settingname = $settingprefix . 'userinfo';
|
||||
$defaultvalue = false;
|
||||
if (isset($this->info->settings[$settingname]) && $this->info->settings[$settingname]) { // Only enabled when available
|
||||
$defaultvalue = true;
|
||||
}
|
||||
|
||||
$activity_userinfo = new restore_activity_userinfo_setting($settingname, base_setting::IS_BOOLEAN, $defaultvalue);
|
||||
$activityuserinfo = new restore_activity_userinfo_setting($settingname, base_setting::IS_BOOLEAN, $defaultvalue);
|
||||
|
||||
if (!$defaultvalue) {
|
||||
// This is a bit hacky, but if there is no user data to restore, then
|
||||
// we replace the standard check-box with a select menu with the
|
||||
|
@ -333,30 +362,34 @@ abstract class restore_activity_task extends restore_task {
|
|||
// It would probably be better design to have a special UI class
|
||||
// setting_ui_checkbox_or_no, rather than this hack, but I am not
|
||||
// going to do that today.
|
||||
$activity_userinfo->set_ui(new backup_setting_ui_select($activity_userinfo, '-',
|
||||
array(0 => get_string('no'))));
|
||||
$activityuserinfo->set_ui(
|
||||
new backup_setting_ui_select(
|
||||
$activityuserinfo,
|
||||
'-',
|
||||
[0 => get_string('no')]
|
||||
)
|
||||
);
|
||||
} else {
|
||||
$activity_userinfo->get_ui()->set_label('-');
|
||||
$activityuserinfo->get_ui()->set_label('-');
|
||||
}
|
||||
|
||||
$this->add_setting($activity_userinfo);
|
||||
$this->add_setting($activityuserinfo);
|
||||
|
||||
// Look for "users" root setting
|
||||
// Look for "users" root setting.
|
||||
$users = $this->plan->get_setting('users');
|
||||
$users->add_dependency($activity_userinfo);
|
||||
$users->add_dependency($activityuserinfo);
|
||||
|
||||
// Look for "section_userinfo" section setting (if exists)
|
||||
// Look for "sectionuserinfo" section setting (if exists).
|
||||
$settingname = 'section_' . $this->info->sectionid . '_userinfo';
|
||||
if ($this->plan->setting_exists($settingname)) {
|
||||
$section_userinfo = $this->plan->get_setting($settingname);
|
||||
$section_userinfo->add_dependency($activity_userinfo);
|
||||
$sectionuserinfo = $this->plan->get_setting($settingname);
|
||||
$sectionuserinfo->add_dependency($activityuserinfo);
|
||||
}
|
||||
|
||||
// Look for "activity_included" setting.
|
||||
$activity_included->add_dependency($activity_userinfo);
|
||||
// Look for "activity included" setting.
|
||||
$includefield->add_dependency($activityuserinfo);
|
||||
|
||||
// End of common activity settings, let's add the particular ones.
|
||||
$this->define_my_settings();
|
||||
return $activityuserinfo;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -150,36 +150,67 @@ class restore_section_task extends restore_task {
|
|||
* Define the common setting that any restore section will have
|
||||
*/
|
||||
protected function define_settings() {
|
||||
|
||||
// All the settings related to this activity will include this prefix
|
||||
$settingprefix = 'section_' . $this->info->sectionid . '_';
|
||||
|
||||
// All these are common settings to be shared by all sections
|
||||
// All these are common settings to be shared by all sections.
|
||||
$sectionincluded = $this->add_section_included_setting($settingprefix);
|
||||
$this->add_section_userinfo_setting($settingprefix, $sectionincluded);
|
||||
}
|
||||
|
||||
// Define section_included (to decide if the whole task must be really executed)
|
||||
/**
|
||||
* Add the section included setting to the task.
|
||||
*
|
||||
* @param string $settingprefix the identifier of the setting
|
||||
* @return section_backup_setting the setting added
|
||||
*/
|
||||
protected function add_section_included_setting(string $settingprefix): section_backup_setting {
|
||||
global $DB;
|
||||
// Define sectionincluded (to decide if the whole task must be really executed).
|
||||
$settingname = $settingprefix . 'included';
|
||||
$section_included = new restore_section_included_setting($settingname, base_setting::IS_BOOLEAN, true);
|
||||
|
||||
$sectionincluded = new restore_section_included_setting($settingname, base_setting::IS_BOOLEAN, true);
|
||||
|
||||
if (is_number($this->info->title)) {
|
||||
$label = get_string('includesection', 'backup', $this->info->title);
|
||||
} elseif (empty($this->info->title)) { // Don't throw error if title is empty, gracefully continue restore.
|
||||
$this->log('Section title missing in backup for section id '.$this->info->sectionid, backup::LOG_WARNING, $this->name);
|
||||
$this->log(
|
||||
'Section title missing in backup for section id ' . $this->info->sectionid,
|
||||
backup::LOG_WARNING,
|
||||
$this->name
|
||||
);
|
||||
$label = get_string('unnamedsection', 'backup');
|
||||
} else {
|
||||
$label = $this->info->title;
|
||||
}
|
||||
$section_included->get_ui()->set_label($label);
|
||||
$this->add_setting($section_included);
|
||||
$sectionincluded->get_ui()->set_label($label);
|
||||
$this->add_setting($sectionincluded);
|
||||
|
||||
// Define section_userinfo. Dependent of:
|
||||
// - users root setting
|
||||
// - section_included setting.
|
||||
return $sectionincluded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the section userinfo setting to the task.
|
||||
*
|
||||
* @param string $settingprefix the identifier of the setting
|
||||
* @param section_backup_setting $includefield the section included setting
|
||||
* @return section_backup_setting the setting added
|
||||
*/
|
||||
protected function add_section_userinfo_setting(
|
||||
string $settingprefix,
|
||||
section_backup_setting $includefield
|
||||
): section_backup_setting {
|
||||
// Define sectionuserinfo. Dependent of:
|
||||
// - users root setting.
|
||||
// - sectionincluded setting.
|
||||
$settingname = $settingprefix . 'userinfo';
|
||||
$defaultvalue = false;
|
||||
if (isset($this->info->settings[$settingname]) && $this->info->settings[$settingname]) { // Only enabled when available
|
||||
$defaultvalue = true;
|
||||
}
|
||||
|
||||
$section_userinfo = new restore_section_userinfo_setting($settingname, base_setting::IS_BOOLEAN, $defaultvalue);
|
||||
$sectionuserinfo = new restore_section_userinfo_setting($settingname, base_setting::IS_BOOLEAN, $defaultvalue);
|
||||
|
||||
if (!$defaultvalue) {
|
||||
// This is a bit hacky, but if there is no user data to restore, then
|
||||
// we replace the standard check-box with a select menu with the
|
||||
|
@ -189,19 +220,23 @@ class restore_section_task extends restore_task {
|
|||
// It would probably be better design to have a special UI class
|
||||
// setting_ui_checkbox_or_no, rather than this hack, but I am not
|
||||
// going to do that today.
|
||||
$section_userinfo->set_ui(new backup_setting_ui_select($section_userinfo, get_string('includeuserinfo','backup'),
|
||||
array(0 => get_string('no'))));
|
||||
$sectionuserinfo->set_ui(
|
||||
new backup_setting_ui_select($sectionuserinfo,
|
||||
get_string('includeuserinfo', 'backup'), [0 => get_string('no')])
|
||||
);
|
||||
} else {
|
||||
$section_userinfo->get_ui()->set_label(get_string('includeuserinfo','backup'));
|
||||
$sectionuserinfo->get_ui()->set_label(get_string('includeuserinfo', 'backup'));
|
||||
}
|
||||
|
||||
$this->add_setting($section_userinfo);
|
||||
$this->add_setting($sectionuserinfo);
|
||||
|
||||
// Look for "users" root setting.
|
||||
$users = $this->plan->get_setting('users');
|
||||
$users->add_dependency($section_userinfo);
|
||||
$users->add_dependency($sectionuserinfo);
|
||||
|
||||
// Look for "section_included" section setting.
|
||||
$section_included->add_dependency($section_userinfo);
|
||||
// Look for "section included" section setting.
|
||||
$includefield->add_dependency($sectionuserinfo);
|
||||
|
||||
return $sectionuserinfo;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -103,7 +103,20 @@ abstract class base_task implements checksumable, executable, loggable {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a setting with the given name exists.
|
||||
*
|
||||
* This method find first in the current settings and then in the plan settings.
|
||||
*
|
||||
* @param string $name Setting name
|
||||
* @return bool
|
||||
*/
|
||||
public function setting_exists($name) {
|
||||
foreach ($this->settings as $setting) {
|
||||
if ($setting->get_name() == $name) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return $this->plan->setting_exists($name);
|
||||
}
|
||||
|
||||
|
|
|
@ -48,22 +48,10 @@ abstract class base_moodleform extends moodleform {
|
|||
protected $uistage = null;
|
||||
|
||||
/**
|
||||
* True if we have a course div open, false otherwise
|
||||
* @var bool
|
||||
* Group stack to control open and closed div groups.
|
||||
* @var array
|
||||
*/
|
||||
protected $coursediv = false;
|
||||
|
||||
/**
|
||||
* True if we have a section div open, false otherwise
|
||||
* @var bool
|
||||
*/
|
||||
protected $sectiondiv = false;
|
||||
|
||||
/**
|
||||
* True if we have an activity div open, false otherwise
|
||||
* @var bool
|
||||
*/
|
||||
protected $activitydiv = false;
|
||||
protected array $groupstack = [];
|
||||
|
||||
/**
|
||||
* Creates the form
|
||||
|
@ -177,20 +165,12 @@ abstract class base_moodleform extends moodleform {
|
|||
}
|
||||
|
||||
/**
|
||||
* Closes any open divs
|
||||
* Closes any open divs.
|
||||
*/
|
||||
public function close_task_divs() {
|
||||
if ($this->activitydiv) {
|
||||
while (!empty($this->groupstack)) {
|
||||
$this->_form->addElement('html', html_writer::end_tag('div'));
|
||||
$this->activitydiv = false;
|
||||
}
|
||||
if ($this->sectiondiv) {
|
||||
$this->_form->addElement('html', html_writer::end_tag('div'));
|
||||
$this->sectiondiv = false;
|
||||
}
|
||||
if ($this->coursediv) {
|
||||
$this->_form->addElement('html', html_writer::end_tag('div'));
|
||||
$this->coursediv = false;
|
||||
array_pop($this->groupstack);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -244,7 +224,7 @@ abstract class base_moodleform extends moodleform {
|
|||
list($identifier, $component) = $setting->get_help();
|
||||
$this->_form->addHelpButton($setting->get_ui_name(), $identifier, $component);
|
||||
}
|
||||
$this->_form->addElement('html', html_writer::end_tag('div'));
|
||||
$this->pop_group();
|
||||
}
|
||||
$this->_form->setDefaults($defaults);
|
||||
return true;
|
||||
|
@ -265,54 +245,32 @@ abstract class base_moodleform extends moodleform {
|
|||
* @param backup_setting $setting
|
||||
*/
|
||||
protected function add_html_formatting(backup_setting $setting) {
|
||||
$mform = $this->_form;
|
||||
$isincludesetting = (strpos($setting->get_name(), '_include') !== false);
|
||||
if ($isincludesetting && $setting->get_level() != backup_setting::ROOT_LEVEL) {
|
||||
switch ($setting->get_level()) {
|
||||
case backup_setting::COURSE_LEVEL:
|
||||
if ($this->activitydiv) {
|
||||
$this->_form->addElement('html', html_writer::end_tag('div'));
|
||||
$this->activitydiv = false;
|
||||
}
|
||||
if ($this->sectiondiv) {
|
||||
$this->_form->addElement('html', html_writer::end_tag('div'));
|
||||
$this->sectiondiv = false;
|
||||
}
|
||||
if ($this->coursediv) {
|
||||
$this->_form->addElement('html', html_writer::end_tag('div'));
|
||||
}
|
||||
$mform->addElement('html', html_writer::start_tag('div', array('class' => 'grouped_settings course_level')));
|
||||
$mform->addElement('html', html_writer::start_tag('div', array('class' => 'include_setting course_level')));
|
||||
$this->coursediv = true;
|
||||
$this->pop_groups_to('course');
|
||||
$this->push_group_start('course', 'grouped_settings course_level');
|
||||
$this->push_group_start(null, 'include_setting course_level');
|
||||
break;
|
||||
case backup_setting::SECTION_LEVEL:
|
||||
if ($this->activitydiv) {
|
||||
$this->_form->addElement('html', html_writer::end_tag('div'));
|
||||
$this->activitydiv = false;
|
||||
}
|
||||
if ($this->sectiondiv) {
|
||||
$this->_form->addElement('html', html_writer::end_tag('div'));
|
||||
}
|
||||
$mform->addElement('html', html_writer::start_tag('div', array('class' => 'grouped_settings section_level')));
|
||||
$mform->addElement('html', html_writer::start_tag('div', array('class' => 'include_setting section_level')));
|
||||
$this->sectiondiv = true;
|
||||
$this->pop_groups_to('course');
|
||||
$this->push_group_start('section', 'grouped_settings section_level');
|
||||
$this->push_group_start(null, 'include_setting section_level');
|
||||
break;
|
||||
case backup_setting::ACTIVITY_LEVEL:
|
||||
if ($this->activitydiv) {
|
||||
$this->_form->addElement('html', html_writer::end_tag('div'));
|
||||
}
|
||||
$mform->addElement('html', html_writer::start_tag('div', array('class' => 'grouped_settings activity_level')));
|
||||
$mform->addElement('html', html_writer::start_tag('div', array('class' => 'include_setting activity_level')));
|
||||
$this->activitydiv = true;
|
||||
$this->pop_groups_to('section');
|
||||
$this->push_group_start('activity', 'grouped_settings activity_level');
|
||||
$this->push_group_start(null, 'include_setting activity_level');
|
||||
break;
|
||||
default:
|
||||
$mform->addElement('html', html_writer::start_tag('div', array('class' => 'normal_setting')));
|
||||
$this->push_group_start(null, 'normal_setting');
|
||||
break;
|
||||
}
|
||||
} else if ($setting->get_level() == backup_setting::ROOT_LEVEL) {
|
||||
$mform->addElement('html', html_writer::start_tag('div', array('class' => 'root_setting')));
|
||||
$this->push_group_start('root', 'root_setting');
|
||||
} else {
|
||||
$mform->addElement('html', html_writer::start_tag('div', array('class' => 'normal_setting')));
|
||||
$this->push_group_start(null, 'normal_setting');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -350,12 +308,55 @@ abstract class base_moodleform extends moodleform {
|
|||
$label .= $OUTPUT->render($labelicon);
|
||||
}
|
||||
$this->_form->addElement('static', 'static_'.$settingui->get_name(), $label, $settingui->get_static_value().$icon);
|
||||
$this->_form->addElement('html', html_writer::end_tag('div'));
|
||||
$this->pop_group();
|
||||
}
|
||||
$this->_form->addElement('hidden', $settingui->get_name(), $settingui->get_value());
|
||||
$this->_form->setType($settingui->get_name(), $settingui->get_param_validation());
|
||||
}
|
||||
|
||||
/**
|
||||
* Pushes a group start to the form.
|
||||
*
|
||||
* This method will create a new group div in the form and add it to the group stack.
|
||||
* The name can be used to close all stacked groups up to a certain group.
|
||||
*
|
||||
* @param string|null $name The name of the group, if any.
|
||||
* @param string $classes The classes to add to the div.
|
||||
*/
|
||||
protected function push_group_start(?string $name, string $classes) {
|
||||
$mform = $this->_form;
|
||||
$this->groupstack[] = $name;
|
||||
$mform->addElement('html', html_writer::start_tag('div', ['class' => $classes]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Pops groups from the stack until the given group name is reached.
|
||||
*
|
||||
* @param string $name The name of the group to pop to.
|
||||
*/
|
||||
protected function pop_groups_to(string $name) {
|
||||
if (empty($this->groupstack)) {
|
||||
return;
|
||||
}
|
||||
while (!empty($this->groupstack) && end($this->groupstack) !== $name) {
|
||||
$this->pop_group();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pops a group from the stack and closes the div.
|
||||
*
|
||||
* @return string|null The name of the group that was popped, or null if the stack is empty.
|
||||
*/
|
||||
protected function pop_group(): ?string {
|
||||
if (empty($this->groupstack)) {
|
||||
return null;
|
||||
}
|
||||
$mform = $this->_form;
|
||||
$mform->addElement('html', html_writer::end_tag('div'));
|
||||
return array_pop($this->groupstack);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds dependencies to the form recursively
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue