Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MBS-9449_Filereport_for_uploaded_files #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 85 additions & 26 deletions classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,41 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class manager {

/**
* The report about imported files.
* @var array
*/
protected static $filereport;

/**
* Manager constructor.
*/
public function __construct() {
self::$filereport = [
'has_data' => false,
'files_imported' => [],
'files_error' => [],
'files_replaced' => [],
'stats' => [
'total' => 0,
'imported' => 0,
'error' => 0,
'replaced' => 0,
],
];
}


/**
* Get the file report.
* @return array The file report.
*/
public static function get_file_report() {
return self::$filereport;
}


/**
* Add a file to the repository.
* @param stored_file $file
Expand Down Expand Up @@ -118,6 +153,9 @@ public static function update_item(int $itemid, array $metadata) {
}
}




/**
* Process metadata for a source.
* @param int $sourceid The id of the source.
Expand Down Expand Up @@ -185,10 +223,11 @@ public static function import_files_from_zip(stored_file $zip, int $sourceid, bo
self::process_metadata($sourceid);

$fs->delete_area_files(\context_system::instance()->id, 'repository_imagehub', 'temp', $sourceid, '/');

}

/**
* Import files from a directory.
* Import files from a directory and create a detailed report.
* @param stored_file $directory The directory.
* @param int $sourceid The id of the source.
* @param bool $deleteold Whether to delete old files. Default is false.
Expand All @@ -213,37 +252,57 @@ public static function import_files_from_directory(stored_file $directory, int $
);

foreach ($files as $file) {
$targetfile = $fs->get_file(
\context_system::instance()->id,
'repository_imagehub',
'images',
$sourceid,
$file->get_filepath(),
$file->get_filename()
);
if (!$targetfile) {
$targetfile = $fs->create_file_from_storedfile([
'contextid' => \context_system::instance()->id,
'component' => 'repository_imagehub',
'filearea' => 'images',
'itemid' => $file->get_itemid(),
'filepath' => $file->get_filepath(),
'filename' => $file->get_filename(),
], $file);
$DB->insert_record('repository_imagehub', [
'fileid' => $targetfile->get_id(),
'source' => $sourceid,
]);
} else {
if ($targetfile->get_contenthash() != $file->get_contenthash()) {
$targetfile->replace_file_with($file);
$DB->update_record('repository_imagehub', [
try {
$targetfile = $fs->get_file(
\context_system::instance()->id,
'repository_imagehub',
'images',
$sourceid,
$file->get_filepath(),
$file->get_filename()
);
if (!$targetfile) {
$targetfile = $fs->create_file_from_storedfile([
'contextid' => \context_system::instance()->id,
'component' => 'repository_imagehub',
'filearea' => 'images',
'itemid' => $file->get_itemid(),
'filepath' => $file->get_filepath(),
'filename' => $file->get_filename(),
], $file);
$DB->insert_record('repository_imagehub', [
'fileid' => $targetfile->get_id(),
'source' => $sourceid,
]);

// Parse metadata.json and get 'filename'-value.
if ($file->get_filename() == 'metadata.json') {
$targetfilename = json_decode($file->get_content(), true)['filename'];
}
self::$filereport['files_imported'][] = $file->get_filepath() . $targetfilename;
} else {
if ($targetfile->get_contenthash() != $file->get_contenthash()) {
$targetfile->replace_file_with($file);
$DB->update_record('repository_imagehub', [
'fileid' => $targetfile->get_id(),
'source' => $sourceid,
]);
self::$filereport['files_replaced'][] = $file->get_filepath() . $file->get_filename();
}
}
} catch (\Exception $e) {
// Skip file if an error occurs.
self::$filereport['files_error'][] = $file->get_filepath() . $file->get_filename();
}
}

// Calculate the stats.
self::$filereport['stats']['error'] = count(self::$filereport['files_error']);
self::$filereport['stats']['imported'] = count(self::$filereport['files_imported']);
self::$filereport['stats']['replaced'] = count(self::$filereport['files_replaced']);
// Set has_data to true if anything has been imported in order to show filereport in filereport.mustache-template.
self::$filereport['has_data'] = self::$filereport['stats']['imported'] > 0 || self::$filereport['stats']['replaced'] > 0;

}

/**
Expand Down
8 changes: 8 additions & 0 deletions lang/en/repository_imagehub.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,29 @@

defined('MOODLE_INTERNAL') || die();

$string['addfilesdescription'] = 'or add files to the repository:';
$string['addsource'] = 'Add source';
$string['backtofiles'] = 'Back to source management';
$string['configplugin'] = 'Imagehub repository type configuration';
$string['delete'] = 'Delete source "{$a}"';
$string['deletewarning'] = 'Are you sure you want to delete this source.';
$string['editsource'] = 'Edit source';
$string['errorfiles'] = 'Files with errors:';
$string['filesimported'] = '{$a} files imported';
$string['filesreplaced'] = '{$a} files replaced';
$string['filesskipped'] = '{$a} files skipped';
$string['imagehub:managerepositories'] = 'Manage Imagehub sources';
$string['imagehub:view'] = 'View Imagehub repository';
$string['importedfiles'] = 'Imported files:';
$string['importreport'] = 'File Import Report';
$string['linktomanagesources'] = '<a href="{$a}">Manage Repository</a>';
$string['linktomanagesources_description'] = 'Manage and add new images';
$string['manage_files'] = 'Manage files';
$string['managefiles'] = 'Manage files';
$string['managesources'] = 'Manage sources';
$string['pluginname'] = 'Imagehub';
$string['privacy:metadata'] = 'The Imagehub plugin doesn\'t store any personal data.';
$string['replacedfiles'] = 'Replaced files:';
$string['repositorynotenabled'] = 'Imagehub repository is not enabled yet. Click on "Save" to enable it.';
$string['sourcetitle'] = 'Title';
$string['sourcetype'] = 'Type';
Expand Down
3 changes: 1 addition & 2 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function get_listing($path = '', $page = '') {
/**
* Search for files.
*
* @param string $search_text
* @param string $search
* @param int $page
* @return void
*/
Expand Down Expand Up @@ -206,7 +206,6 @@ public static function create($type, $userid, $context, $params, $readonly = 0)
*/
public static function type_config_form($mform, $classname = 'repository_imagehub') {
$type = repository::get_type_by_typename('imagehub');

// Link to managesources.
if ($type !== null) {
$url = new moodle_url('/repository/imagehub/managesources.php');
Expand Down
21 changes: 16 additions & 5 deletions managefiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@
if ($source->type == \repository_imagehub::SOURCE_TYPE_ZIP_VALUE) {
$draftarea = $fs->get_area_files(core\context\user::instance($USER->id)->id, 'user', 'draft', $draftitemid, '', false);
if (count($draftarea) == 1) {
$filemanager = new \repository_imagehub\manager();
$zipfile = array_pop($draftarea);
\repository_imagehub\manager::import_files_from_zip($zipfile, $sourceid);
$filemanager::import_files_from_zip($zipfile, $sourceid);
$filereport = $filemanager::get_file_report();


}
} else {
file_save_draft_area_files(
Expand All @@ -76,15 +80,22 @@
}
}

$data = (array)$managefilesform->get_data();
$managefilesform->data_preprocessing($data);
$managefilesform->set_data($data);
$managefilesform->display();
// File report.
echo $OUTPUT->render_from_template('repository_imagehub/filereport', $filereport);

// Backlink.
echo($OUTPUT->render_from_template('repository_imagehub/backlink', [
'linkto' => new moodle_url('/repository/imagehub/managesources.php'),
'linktext' => get_string('backtofiles', 'repository_imagehub'),
]));


$data = (array)$managefilesform->get_data();
$managefilesform->data_preprocessing($data);
$managefilesform->set_data($data);
$managefilesform->display();




echo $OUTPUT->footer();
8 changes: 5 additions & 3 deletions templates/backlink.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
"linktext" : "Back to the main page"
}
}}
<div class="row">
<div class="col offset-3">
<div class="row mform flex my-6 ">
<div class="col-md-9 d-flex justify-content-between align-items-center">
<a href="{{{linkto}}}" class="btn btn-secondary">
{{linktext}}
<i class="fas fa-angle-left mr-2"></i> {{linktext}}
</a>

<div class="mr-6 ">{{#str}}addfilesdescription, repository_imagehub{{/str}}</div>
</div>
</div>
90 changes: 90 additions & 0 deletions templates/filereport.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{{!
This file is part of Moodle - http://moodle.org/

Moodle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template repository_imagehub/filereport

Template to display file import results

Example context (json):
{
"has_data": true,
"files_imported": [
{
"path": "path/to/image1/image.svg",
"has_metadata": true
},
{
"path": "path/to/image2/image.png",
"has_metadata": false
}
],
"files_replaced": [
{
"path": "path/to/image3/image.svg",
"has_metadata": true
}
],
"files_error": [
{
"path": "path/to/image4/image.png",
"has_metadata": false,
"error_message": "Error message"
}
],
"stats": {
"imported": 2,
"replaced": 1,
"error": 1
}
}
}}

{{#has_data}}
<div class="repository__imagehub__filereport mform my-4 d-flex justify-content-center">
<div class="col-md-9 alert alert-info">
<h4 class="mb-3"><i class="fa-solid fa-clipboard-list mr-2"></i> {{#str}}importreport, repository_imagehub{{/str}}</h4>

<div class="import-summary mb-1">
<button data-toggle="collapse" href="#repository__imagehub__filereport__filelist--imported" class="badge badge-success">{{#str}}filesimported, repository_imagehub, {{stats.imported}}{{/str}}</button>
<button class="badge badge-secondary">{{#str}}filesskipped, repository_imagehub, {{stats.error}}{{/str}}</button>
</div>
<div class="collapse" id="repository__imagehub__filereport__filelist--imported">
<div class="card card-body">
{{#files_imported.0}}
<div class="imported-files">
<ul class="list-unstyled fs-6 text">
{{#files_imported}}
<li><i class="fa fa-check text-success"></i> {{.}}</li>
{{/files_imported}}
</ul>
</div>
{{/files_imported.0}}
</div>
</div>

{{#files_error.0}}
<div class="skipped-files">
<ul class="list-unstyled fs-6 text">
{{#files_error}}
<li><i class="fa fa-minus text-secondary"></i> {{.}}</li>
{{/files_error}}
</ul>
</div>
{{/files_error.0}}
</div>
</div>
{{/has_data}}
Loading