.
/**
* Define all the restore steps that will be used by the restore_htmlcert_activity_task.
*
* @package mod_htmlcert
* @copyright 2013 Mark Nelson , 2021 Klaus-Uwe Mitterer
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
/**
* Define the complete htmlcert structure for restore, with file and id annotations.
*
* @package mod_htmlcert
* @copyright 2013 Mark Nelson , 2021 Klaus-Uwe Mitterer
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class restore_htmlcert_activity_structure_step extends restore_activity_structure_step {
/**
* Define the different items to restore.
*
* @return array the restore paths
*/
protected function define_structure() {
// The array used to store the path to the items we want to restore.
$paths = array();
// The htmlcert instance.
$paths[] = new restore_path_element('htmlcert', '/activity/htmlcert');
// The templates.
$paths[] = new restore_path_element('htmlcert_template', '/activity/htmlcert/template');
// Check if we want the issues as well.
if ($this->get_setting_value('userinfo')) {
$paths[] = new restore_path_element('htmlcert_issue', '/activity/htmlcert/issues/issue');
}
// Return the paths wrapped into standard activity structure.
return $this->prepare_activity_structure($paths);
}
/**
* Handles restoring the htmlcert activity.
*
* @param stdClass $data the htmlcert data
*/
protected function process_htmlcert($data) {
global $DB;
$data = (object) $data;
$data->course = $this->get_courseid();
$data->timecreated = $this->apply_date_offset($data->timecreated);
$data->timemodified = $this->apply_date_offset($data->timemodified);
// Insert the htmlcert record.
$newitemid = $DB->insert_record('htmlcert', $data);
// Immediately after inserting record call this.
$this->apply_activity_instance($newitemid);
}
/**
* Handles restoring a htmlcert templage.
*
* @param stdClass $data the htmlcert data
*/
protected function process_htmlcert_template($data) {
global $DB;
$data = (object) $data;
$oldid = $data->id;
$data->contextid = $this->task->get_contextid();
$data->timecreated = $this->apply_date_offset($data->timecreated);
$data->timemodified = $this->apply_date_offset($data->timemodified);
$newitemid = $DB->insert_record('htmlcert_templates', $data);
$this->set_mapping('htmlcert_template', $oldid, $newitemid);
// Update the template id for the htmlcert.
$htmlcert = new stdClass();
$htmlcert->id = $this->get_new_parentid('htmlcert');
$htmlcert->templateid = $newitemid;
$DB->update_record('htmlcert', $htmlcert);
}
/**
* Handles restoring a htmlcert issue.
*
* @param stdClass $data the htmlcert data
*/
protected function process_htmlcert_issue($data) {
global $DB;
$data = (object) $data;
$oldid = $data->id;
$data->htmlcertid = $this->get_new_parentid('htmlcert');
$data->timecreated = $this->apply_date_offset($data->timecreated);
$data->userid = $this->get_mappingid('user', $data->userid);
$newitemid = $DB->insert_record('htmlcert_issues', $data);
$this->set_mapping('htmlcert_issue', $oldid, $newitemid);
}
/**
* Called immediately after all the other restore functions.
*/
protected function after_execute() {
parent::after_execute();
// Add the files.
$this->add_related_files('mod_htmlcert', 'intro', null);
// Note - we can't use get_old_contextid() as it refers to the module context.
$this->add_related_files('mod_htmlcert', 'image', null, $this->get_task()->get_info()->original_course_contextid);
}
}