1
classes\output\mobile.php
must return the 'templates', the 'javascript',
and 'otherdata' with the data required for the javascript and template.
e.g.
$otherdata = ['usefulstuff' => json_encode($usefulstuff)
, 'maxsize' => $maxsize
, 'filetypes' =>
'.png,.jpg,.jpeg,.pdf,.doc,.docx,.bmp,.odt', 'attachments' =>
""
];
return [
'templates' =>
[
[
'id' =>
'main',
'html' =>
$OUTPUT->render_from_template('myplugin/template_name', $data),
],
],
'javascript' => file_get_contents($CFG->dirroot . "myplugin_path/mobile/template_javascript.js"),
'otherdata' =>
$otherdata
];
2 template
part 1: the attachments selector
myplugin/template_name
This creates the functionality to select and gather the documents into the
mobile app.
<core-attachments
[files]="CONTENT_OTHERDATA.attachments"
[maxSize]="CONTENT_OTHERDATA.maxsize"
[maxSubmissions]="1"
[component]="myplugin"
[acceptedTypes]="CONTENT_OTHERDATA.filetypes"
[allowOffline]=false>
</core-attachments>
3 template
part 2
myplugin/template_name
We now need to let the user agree to send these documents.
<ion-button
(click)="updateContent(CONTENT_OTHERDATA)" color="primary" expand="block" name="myplugin_upload" successMessage="{{ 'plugin.myplugin.done' | translate }}" refreshOnSuccess="true">
Send
</ion-button>
4 javascript for the button
myplugin_path/mobile/template_javascript.js
this.updateContent = function(allinfo) {
let attachments = allinfo.attachments;
if ((!attachments.length) {
return false;
}
return this.saveFiles(allinfo);
};
this.saveFiles = async function
(inputData) {
let saveOffline = false;
let response;
const modal = await this.CoreDomUtilsProvider.showModalLoading('core.sending', true);
// Upload attachments.
try {
let attachmentsId; //
number | undefined;
attachmentsId = await this.uploadOrStoreSubmissionFiles(inputData.attachments, false);
const params = {
attachments: attachmentsId,
};
const site = await this.CoreSitesProvider.getSite();
response = await site.write('myplugin_add_attachments', params);
if (response.status === true) {
this.CoreDomUtilsProvider.showToast(
this.TranslateService.instant('plugin.myplugin.uploadsuccessful'), true, 6000);
} else {
this.CoreDomUtilsProvider.showToast(
this.TranslateService.instant('plugin.myplugin.uploadfailure'), true, 6000);
}
} catch (error) {
this.CoreDomUtilsProvider.showErrorModalDefault(error, 'Cannot
save files');
} finally {
modal.dismiss();
this.CoreFileUploaderProvider.clearTmpFiles(inputData.attachments);
this.CONTENT_OTHERDATA.attachments = [];
}
return response;
};
5 DB\Services.php
$functions
= [
'myplugin_add_attachments' =>
[
'classname'
=> 'myplugin_external',
'methodname'
=> 'add_attachments',
'description' => 'Upload
attachments',
'type'
=> 'write',
'services'
=> [MOODLE_OFFICIAL_MOBILE_SERVICE, 'local_mobile']
],
];
6 Classes\External.php
public static function add_attachments
The file gets uploaded into user draft files. So to access, make
public static function add_attachments($attachments), and include
$fs = get_file_storage();
$file = $fs->get_area_files($contextid, 'user', 'draft', $attachments
, "itemid, filepath,
filename", false, 0, 0, 0);
$uploadStatus = true;
foreach ($file as $f) {
$filecontent = $f->get_content();
$filename
= $f->get_filename();
$encoded = base64_encode($filecontent);
// Do stuff with file
}
$fs->delete_area_files($contextid, 'user', 'draft', $attachments);