Implemented import and export of images

This commit is contained in:
PMKuipers 2023-12-12 22:42:37 +01:00
parent 11c5b0fa8a
commit 0de002bf3a
2 changed files with 133 additions and 8 deletions

View File

@ -345,7 +345,7 @@ class studyplan {
public static function add($fields, $bare = false) : self { public static function add($fields, $bare = false) : self {
global $CFG, $DB; global $CFG, $DB;
$addable = ['name', 'shortname', 'description', 'idnumber', 'context_id', 'aggregation', 'aggregation_config']; $addable = ['name', 'shortname', 'description', 'descriptionformat', 'idnumber', 'context_id', 'aggregation', 'aggregation_config'];
$info = ['enddate' => null ]; $info = ['enddate' => null ];
foreach ($addable as $f) { foreach ($addable as $f) {
if (array_key_exists($f, $fields)) { if (array_key_exists($f, $fields)) {
@ -360,7 +360,7 @@ class studyplan {
// On import, adding an empty page messes things up , so we have an option to skip this.... // On import, adding an empty page messes things up , so we have an option to skip this....
if (!$bare) { if (!$bare) {
$pageaddable = ['name', 'shortname', 'description', 'periods', 'startdate', 'enddate']; $pageaddable = ['name', 'shortname', 'description', 'descriptionformat', 'periods', 'startdate', 'enddate'];
$pageinfo = ['studyplan_id' => $id]; $pageinfo = ['studyplan_id' => $id];
foreach ($pageaddable as $f) { foreach ($pageaddable as $f) {
if (array_key_exists($f, $fields)) { if (array_key_exists($f, $fields)) {
@ -740,18 +740,74 @@ class studyplan {
* @return array information model * @return array information model
*/ */
public function export_model() { public function export_model() {
$exportfiles = $this->export_files('studyplan');
$iconfiles = $this->export_files('icon');
$model = [ $model = [
'name' => $this->r->name, 'name' => $this->r->name,
'shortname' => $this->r->shortname, 'shortname' => $this->r->shortname,
'description' => $this->r->description, 'description' => $this->r->description,
'descriptionformat' => $this->r->descriptionformat,
"aggregation" => $this->r->aggregation, "aggregation" => $this->r->aggregation,
"aggregation_config" => json_decode($this->aggregator->config_string()), "aggregation_config" => json_decode($this->aggregator->config_string()),
'aggregation_info' => $this->aggregator->basic_model(), 'aggregation_info' => $this->aggregator->basic_model(),
'pages' => $this->export_pages_model(), 'pages' => $this->export_pages_model(),
'files' => $exportfiles,
'iconfiles' => $iconfiles,
]; ];
return $model; return $model;
} }
/**
* Export files from file storage
* @param string $area Name of the file area to export
* @return array information model
*/
public function export_files($area) {
$exportfiles = [];
$fs = get_file_storage();
$files = $fs->get_area_files(
\context_system::instance()->id,
'local_treestudyplan',
$area,
$this->id);
foreach ($files as $file) {
if ($file->get_filename() != ".") {
$contents = $file->get_content();
$exportfiles[] = [
"name" => $file->get_filename(),
"path" => $file->get_filepath(),
"content" => convert_uuencode($contents),
];
}
}
return $exportfiles;
}
/**
* Import previously exported files into the file storage
* @param string $area Name of the file area to import
* @param mixed $importfiles List of files to import from string in the format exported in export_model()
*
*/
public function import_files($importfiles,$area) {
$fs = get_file_storage();
foreach($importfiles as $file) {
if ($file['name'] != ".") {
$fileinfo = [
'contextid' =>\context_system::instance()->id, // ID of the system context.
'component' => 'local_treestudyplan', // Your component name.
'filearea' => $area, // Usually = table name.
'itemid' => $this->id, // ID of the studyplanpage.
'filepath' => $file["path"], // Path of the file.
'filename' => $file["name"], // Name of the file..
];
$fs->create_file_from_string($fileinfo, convert_uudecode($file["content"]));
}
}
}
/** /**
* Export all pages * Export all pages
* @return array information model * @return array information model
@ -778,16 +834,27 @@ class studyplan {
$content = json_decode($content, true); $content = json_decode($content, true);
if ($content["type"] == "studyplan" && $content["version"] >= 2.0) { if ($content["type"] == "studyplan" && $content["version"] >= 2.0) {
$planmodel = $content["studyplan"];
// Make sure the aggregation_config is re-encoded as json text. // Make sure the aggregation_config is re-encoded as json text.
$content["studyplan"]["aggregation_config"] = json_encode($content["studyplan"]["aggregation_config"]); $planmodel["aggregation_config"] = json_encode( $planmodel["aggregation_config"]);
// And make sure the context_id is set to the provided context for import. // And make sure the context_id is set to the provided context for import.
$content["studyplan"]["context_id"] = $contextid; $planmodel["context_id"] = $contextid;
// Create a new plan, based on the given parameters - this is the import studyplan part. // Create a new plan, based on the given parameters - this is the import studyplan part.
$plan = self::add($content["studyplan"], true); $plan = self::add( $planmodel, true);
// Import the files
if (isset( $planmodel['files']) && is_array( $planmodel['files'])) {
$plan->import_files( $planmodel['files'],"studyplan");
}
// Import the icon
if (isset( $planmodel['iconfiles']) && is_array( $planmodel['iconfiles'])) {
$plan->import_files( $planmodel['iconfiles'],"icon");
}
// Now import each page. // Now import each page.
return $plan->import_pages_model($content["studyplan"]["pages"]); return $plan->import_pages_model($planmodel["pages"]);
} else { } else {
debugging("Invalid format and type: {$content['type']} version {$content['version']}"); debugging("Invalid format and type: {$content['type']} version {$content['version']}");
@ -831,6 +898,9 @@ class studyplan {
$this->page_cache[] = $page; $this->page_cache[] = $page;
$page->import_periods_model($p["perioddesc"]); $page->import_periods_model($p["perioddesc"]);
$page->import_studylines_model($p["studylines"]); $page->import_studylines_model($p["studylines"]);
if ($p['files']) {
$page->import_files($p["files"],'studyplanpage');
}
} }
return true; return true;
} }

View File

@ -273,7 +273,7 @@ class studyplanpage {
} }
$addable = ['studyplan_id', 'fullname', 'shortname', 'description', 'periods', 'startdate', 'enddate']; $addable = ['studyplan_id', 'fullname', 'shortname', 'description', 'descriptionformat', 'periods', 'startdate', 'enddate'];
$info = ['enddate' => null ]; $info = ['enddate' => null ];
foreach ($addable as $f) { foreach ($addable as $f) {
if (array_key_exists($f, $fields)) { if (array_key_exists($f, $fields)) {
@ -298,7 +298,7 @@ class studyplanpage {
*/ */
public function edit($fields) : self { public function edit($fields) : self {
global $DB; global $DB;
$editable = ['fullname', 'shortname', 'description', 'periods', 'startdate', 'enddate']; $editable = ['fullname', 'shortname', 'description', 'descriptionformat', 'periods', 'startdate', 'enddate'];
$info = ['id' => $this->id, ]; $info = ['id' => $this->id, ];
foreach ($editable as $f) { foreach ($editable as $f) {
if (array_key_exists($f, $fields)) { if (array_key_exists($f, $fields)) {
@ -614,19 +614,74 @@ class studyplanpage {
* @return array information model * @return array information model
*/ */
public function export_model() { public function export_model() {
$exportfiles = $this->export_files('studyplanpage');
$model = [ $model = [
'fullname' => $this->r->fullname, 'fullname' => $this->r->fullname,
'shortname' => $this->r->shortname, 'shortname' => $this->r->shortname,
'description' => $this->r->description, 'description' => $this->r->description,
'descriptionformat' => $this->r->descriptionformat,
'periods' => $this->r->periods, 'periods' => $this->r->periods,
'startdate' => $this->r->startdate, 'startdate' => $this->r->startdate,
'enddate' => $this->r->enddate, 'enddate' => $this->r->enddate,
'studylines' => $this->export_studylines_model(), 'studylines' => $this->export_studylines_model(),
'perioddesc' => period::page_model($this), 'perioddesc' => period::page_model($this),
'files' => $exportfiles,
]; ];
return $model; return $model;
} }
/**
* Export files from file storage
* @param string $area Name of the file area to export
* @return array information model
*/
public function export_files($area) {
$exportfiles = [];
$fs = get_file_storage();
$files = $fs->get_area_files(
\context_system::instance()->id,
'local_treestudyplan',
$area,
$this->id);
foreach ($files as $file) {
if ($file->get_filename() != ".") {
$contents = $file->get_content();
$exportfiles[] = [
"name" => $file->get_filename(),
"path" => $file->get_filepath(),
"content" => convert_uuencode($contents),
];
}
}
return $exportfiles;
}
/**
* Import previously exported files into the file storage
* @param string $area Name of the file area to export
* @param mixed $importfiles List of files to import from string in the format exported in export_model()
*
*/
public function import_files($importfiles,$area) {
$fs = get_file_storage();
foreach($importfiles as $file) {
if ($file['name'] != ".") {
$fileinfo = [
'contextid' =>\context_system::instance()->id, // ID of the system context.
'component' => 'local_treestudyplan', // Your component name.
'filearea' => $area, // Usually = table name.
'itemid' => $this->id, // ID of the studyplanpage.
'filepath' => $file["path"], // Path of the file.
'filename' => $file["name"], // Name of the file..
];
$fs->create_file_from_string($fileinfo, convert_uudecode($file["content"]));
}
}
}
/** /**
* Export this pages periods into an array before serialization * Export this pages periods into an array before serialization
* @return array * @return array