Moodle File API : multiple rows? - moodle

In a Moodle form I perform a file upload using the filemanager element:
$mform->addElement('filemanager', 'attachment',get_string('displayedcontent', 'block_helloworld'), null, $filemanageropts);
Once the form is validated, when I record my instance in the database, I also save the uploaded file using the following function:
file_save_draft_area_files($form_submitted_data->attachment, $context->id, 'block_helloworld', 'attachment',
$form_submitted_data->attachment, array('subdirs' => 0, 'maxbytes' => 500000, 'maxfiles' => 1));
This is working fine but when I take a look at the DB table mdl_files, I saw that for my file there are 4 rows:
component fileare itemid filepath filename
block_helloworld attachment 706783489 / .
block_helloworld attachment 706783489 / test5.pdf
user draft 706783489 / .
user draft 706783489 / test5.pdf
There are 2 rows for my uploaded file in my component block_helloworld and in the component user.
One row has a filename but not the other one!
This sounds strange. Is that normal?
When I perform file deletion, how to delete all these files?
Note: I am using moodle v3.0.6

As far as I remember, this is normal behaviour. I had this issue, too, but when you cross check (like doing a file upload into a course) you will notice that there are 2 rows, too. Not sure, why but for me it was normal behaviour

The 4 entries are:
The folder that your file is in
The file itself
The folder that the draft version of the file is stored in whilst the form is being edited
The draft file whilst the form is being edited
The draft files will be automatically cleaned up after a day or so.

Related

magento 2 export not working , after clicking continue its redirect to the dashboard

Product CSV created in var folder but it's not getting the download. Or CSV file shows 302 error.
I tried all the solutions given for not working export. is there any other way?
1.expected result: It should download a CSV file
2.actual result: It only generates a file in the var folder but not getting a download.
You have to remove deprecated "modules_disable_output" values from core_config_data,
Before run below query please take backup of core_config_data table.
DELETE FROM core_config_data WHERE path LIKE
"advanced/modules_disable_output/%";
And clear cache
Hope it will work for you.

How to make a section optional when mapped to optional data in a Word OpenXml Part?

I'm using OpenXml SDK to generate word 2013 files. I'm running on a server (part of a server solution), so automation is not an option.
Basically I have an xml file that is output from a backend system. Here's a very simplified example:
<my:Data
xmlns:my="https://schemas.mycorp.com">
<my:Customer>
<my:Details>
<my:Name>Customer Template</my:Name>
</my:Details>
<my:Orders>
<my:Count>2</my:Count>
<my:OrderList>
<my:Order>
<my:Id>1</my:Id>
<my:Date>19/04/2017 10:16:04</my:Date>
</my:Order>
<my:Order>
<my:Id>2</my:Id>
<my:Date>20/04/2017 10:16:04</my:Date>
</my:Order>
</my:OrderList>
</my:Orders>
</my:Customer>
</my:Data>
Then I use Word's Xml Mapping pane to map this data to content control:
I simply duplicate the word file, and write new Xml data when generating new files.
This is working as expected. When I update the xml part, it reflects the data from my backend.
Thought, there's a case that does not works. If a customer has no order, the template content is kept in the document. The xml data is :
<my:Data
xmlns:my="https://schemas.mycorp.com">
<my:Customer>
<my:Details>
<my:Name>Some customer</my:Name>
</my:Details>
<my:Orders>
<my:Count>0</my:Count>
<my:OrderList>
</my:OrderList>
</my:Orders>
</my:Customer>
</my:Data>
(see the empty order list).
In Word, the xml pane reflects the correct data (meaning no Order node):
But as you can see, the template content is still here.
Basically, I'd like to hide the order list when there's no order (or at least an empty table).
How can I do that?
PS: If it can help, I uploaded the word and xml files, and a small PowerShell script that injects the data : repro.zip
Thanks for sharing your files so we can better help you.
I had a difficult time trying to solve your problem with your existing Word Content Controls, XML files and the PowerShell script that added the XML to the Word document. I found what seemed to be Microsoft's VSTO example solution to your problem, but I couldn't get this to work cleanly.
I was however able to write a simple C# console application that generates a Word file based on your XML data. The OpenXML code to generate the Word file was generated code from the Open XML Productivity Tool. I then added some logic to read your XML file and generate the second table rows dynamically depending on how many orders there are in the data. I have uploaded the code for you to use if you are interested in this solution. Note: The xml data file should be in c:\temp and the generated word files will be in c:\temp also.
Another added bonus to this solution is if you were to add all of the customer data into one XML file, the application will create separate word files in your temp directory like so:
customer_<name1>.docx
customer_<name2>.docx
customer_<name3>.docx
etc.
Here is the document generated from the first xml file
Here is the document generated from the second xml file with the empty row
Hope this helps.

Get path of uploaded image in Moodle

I have added custom column to store company logo. I have used file api of moodle like :
$mform->addElement('filepicker', 'certificatelogo', 'Company Logo', null,
array('maxbytes' => $maxbytes, 'accepted_types' => '*'));
$mform->setDefault('certificatelogo', '0');
$mform->addHelpButton('certificatelogo', 'certificatelogo', 'certificate');
Once the form is submitted itemid will be stored in custom column. Say "648557354"
Now I need to get image to print logo on certificate. How can I get image path from itemid? Do I need to store any other information to retrieve image?
The itemid returned is the temporary id of the draft area where the file is stored whilst the form is being displayed. You need to copy the file into its 'real' location, when the form is submitted, otherwise the file will be automatically deleted after a few days (and it will only be accessible to the user who originally uploaded it).
I'd always recommend using the filemanager element, if you are planning on keeping the file around (filepicker elements are for files you want to process and discard, such as when uploading a CSV file data to parse and add to the database).
Details of how to use it are here:
https://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms#filemanager
But the basic steps are:
Copy any existing files from the 'real' area to the draft area (file_prepare_standard_filemanager).
Display the form.
On submission, copy files from the draft area to the 'real' area (file_postupdate_standard_filemanager).
When you want to display the file to the user, get a list of files stored in the file area (defined by the component, filearea, context and, optionally, itemid, you used in file_prepare_standard_filemanager and file_postupdate_standard_filemanager). You can do this with: $fs = get_file_storage(); $fs->get_area_files().
For those files (maybe only 1 file, in your case), generate the URL with moodle_url::make_pluginfile_url.
Make sure your plugin has a PLUGINNAME_pluginfile() function in lib.php, to examine incoming file requests, do security checks on them, then serve the file.
There is a reasonable example of all of this at: https://github.com/AndyNormore/filemanager

How can I rename file uploaded to s3 using javascript api?

'pickAndStore' method allows me to specify full path to the file, but I don't know it's extension at this point (file path has to be defined before file is uploaded, so it's not possible to provide a path with correct extension).
if I use 'pick' and then 'store' I have 2 files (because both methods uploads file to the s3). I can delete 'old' file, but it's not optimal and can be pain (take ages) with really big files.
Is there any better solution? Ideally to rename existing file.
Currently, there is no workaround for renaming file.
However, in our Javascript API v2 we are planing to add new callback function. onStart callback will be fired after user pick file but before file uploading. There could be option like renaming file based on original filename.
We will keep you updated.

Umbraco 7. forms - path error when form transferred to document in Workflow (file upload)

I have a form with several fields that has a workflow to save as a document.
One of the fields is a File Upload.
This is matched to a document field of an Upload type.
All works well other than the File Upload in the resulting document has an error in the URL that is shown in the document:
It includes: ....umbraco/~/media/forms/upload....
Whereas it should be: ...media/forms/upload/...
The file uploaded is there, but the saved link is incorrect.
This does not seems to refer to the settings in FileSystemProviders.config or UmbracoForms.config
The later has a key for UploadStorageDirectory but has no effect.
Any suggestions would be great... John