Implemented import and export of images
This commit is contained in:
parent
11c5b0fa8a
commit
0de002bf3a
|
@ -345,7 +345,7 @@ class studyplan {
|
|||
public static function add($fields, $bare = false) : self {
|
||||
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 ];
|
||||
foreach ($addable as $f) {
|
||||
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....
|
||||
if (!$bare) {
|
||||
|
||||
$pageaddable = ['name', 'shortname', 'description', 'periods', 'startdate', 'enddate'];
|
||||
$pageaddable = ['name', 'shortname', 'description', 'descriptionformat', 'periods', 'startdate', 'enddate'];
|
||||
$pageinfo = ['studyplan_id' => $id];
|
||||
foreach ($pageaddable as $f) {
|
||||
if (array_key_exists($f, $fields)) {
|
||||
|
@ -740,18 +740,74 @@ class studyplan {
|
|||
* @return array information model
|
||||
*/
|
||||
public function export_model() {
|
||||
$exportfiles = $this->export_files('studyplan');
|
||||
$iconfiles = $this->export_files('icon');
|
||||
$model = [
|
||||
'name' => $this->r->name,
|
||||
'shortname' => $this->r->shortname,
|
||||
'description' => $this->r->description,
|
||||
'descriptionformat' => $this->r->descriptionformat,
|
||||
"aggregation" => $this->r->aggregation,
|
||||
"aggregation_config" => json_decode($this->aggregator->config_string()),
|
||||
'aggregation_info' => $this->aggregator->basic_model(),
|
||||
'pages' => $this->export_pages_model(),
|
||||
'files' => $exportfiles,
|
||||
'iconfiles' => $iconfiles,
|
||||
];
|
||||
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
|
||||
* @return array information model
|
||||
|
@ -778,16 +834,27 @@ class studyplan {
|
|||
$content = json_decode($content, true);
|
||||
if ($content["type"] == "studyplan" && $content["version"] >= 2.0) {
|
||||
|
||||
$planmodel = $content["studyplan"];
|
||||
// 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.
|
||||
$content["studyplan"]["context_id"] = $contextid;
|
||||
$planmodel["context_id"] = $contextid;
|
||||
|
||||
// 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.
|
||||
return $plan->import_pages_model($content["studyplan"]["pages"]);
|
||||
return $plan->import_pages_model($planmodel["pages"]);
|
||||
|
||||
} else {
|
||||
debugging("Invalid format and type: {$content['type']} version {$content['version']}");
|
||||
|
@ -831,6 +898,9 @@ class studyplan {
|
|||
$this->page_cache[] = $page;
|
||||
$page->import_periods_model($p["perioddesc"]);
|
||||
$page->import_studylines_model($p["studylines"]);
|
||||
if ($p['files']) {
|
||||
$page->import_files($p["files"],'studyplanpage');
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -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 ];
|
||||
foreach ($addable as $f) {
|
||||
if (array_key_exists($f, $fields)) {
|
||||
|
@ -298,7 +298,7 @@ class studyplanpage {
|
|||
*/
|
||||
public function edit($fields) : self {
|
||||
global $DB;
|
||||
$editable = ['fullname', 'shortname', 'description', 'periods', 'startdate', 'enddate'];
|
||||
$editable = ['fullname', 'shortname', 'description', 'descriptionformat', 'periods', 'startdate', 'enddate'];
|
||||
$info = ['id' => $this->id, ];
|
||||
foreach ($editable as $f) {
|
||||
if (array_key_exists($f, $fields)) {
|
||||
|
@ -614,19 +614,74 @@ class studyplanpage {
|
|||
* @return array information model
|
||||
*/
|
||||
public function export_model() {
|
||||
$exportfiles = $this->export_files('studyplanpage');
|
||||
|
||||
$model = [
|
||||
'fullname' => $this->r->fullname,
|
||||
'shortname' => $this->r->shortname,
|
||||
'description' => $this->r->description,
|
||||
'descriptionformat' => $this->r->descriptionformat,
|
||||
'periods' => $this->r->periods,
|
||||
'startdate' => $this->r->startdate,
|
||||
'enddate' => $this->r->enddate,
|
||||
'studylines' => $this->export_studylines_model(),
|
||||
'perioddesc' => period::page_model($this),
|
||||
'files' => $exportfiles,
|
||||
];
|
||||
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
|
||||
* @return array
|
||||
|
|
Loading…
Reference in New Issue
Block a user