Moodle, upload and store files using moodle forms - php

I'm having some dificulties implementing a moodle upload mechanism using moodle forms. My goal is to let the user/administrator upload images, store them and access later in a block.
Currently, I have this in the form:
$mform->addElement('filemanager', 'attachments', 'Pic:', null, array('subdirs' => 0, 'maxfiles' => 1,'accepted_types' => '*' ));
and this to save the file:
if ($draftitemid = file_get_submitted_draft_itemid('attachments')) {
file_save_draft_area_files($draftitemid, $context->id, 'mod_assignment', 'attachments', 0, array('subdirs' => false, 'maxfiles' => 1));
}
and I try to access the file like this:
file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . $context->id . '/mod_assignment/attachments')
I don't receive any errors but I can't access the file either. I'm using moodle 2.0.
Thanks in advance,
Take care

It sounds like you are looking to write a custom block, in which case you should be giving block_myblock as the component name instead of mod_assignment.
Each Moodle component which serves files needs to have its own function defined within lib.php to handle file requests. In your case that function wants to be called something like block_myblock_pluginfile().
A good example of this is block_html_pluginfile() which can be found in moodle/blocks/html/lib.php and does something very similar to what you are wanting.

Related

Show image from private storage to rest api client in laravel

I'm working on a laravel project and i have saved some pictures on a private storage folder .
This is config/filesystems.php :
'frontend' => [
'driver' => 'local',
'root' => storage_path('app/frontend'),
'url' => env('APP_URL') . '/storage/frontend',
],
I want to show images to client using rest api ( my client is a flutter application ) .
I have tried using some methods of File Storage in Laravel but it wont help .
This is a method that i have tried but when i use it's url it says to me Not Found:
Route::get('/show-pic', function () {
$pic = Picture::find(1);
$link = Storage::disk('frontend')->url($pic->avatar);
return response()->json($link , 200);
});
Also i dont want to get the content of pictures then return response containing content of picture ,
i just need url of picture .
Any suggestion will be helpful .
Thank you .
please try :
$link = Storage::disk('frontend')->getAdapter()->getPathPrefix();
or
$link = Storage::disk('frontend')->path($pic->avatar);
To solve this problem, you can use environment variables as follows:
APP_URL='your_app_url'
And then in your controller, you may access the url like this:
$imageUrl = $('APP_URL').'/uploads/'.$imageFileName;
this assumes that your image files are stored in storage/uploads
The easiest way to solve this issue :
Move your frontend folder inside storage\app\public
Then call
php artisan storage:link
And finally you may use the url method to get the URL for the given file
Storage::disk('public')->url('frontend/' . $pic->avatar);

How to make a custom theme using Laravel and where to store theme data?

I'm building a full website Using Laravel and this problem is facing me , I want to give the user full control to the site appearance and the ability to change website's layout without my help.
But when I'm thinking about someway to do so by database , there are several tables with tens of columns will be added beside the colors and sections that will be added to database every time the user will change something in the layout style.
Is there any other way to store the options of the theme in XML file or anything other than database?
** Note : when I checked the database of Wordpress I hadn't found any thing concerned to themes there , So where Wordpress store theme options?
For your XML-like storage you can use Laravel Config. Let's say you have the file config/templates.php. Inside you can have a lot of stuff like.
return [
'default_template' => 'layouts.templates.default',
'posts' => 'layouts.templates.post',
'footer' => 'layouts.includes.footer',
'options' => [
'full_with' => false,
'has_footer' => true,
'background_color' => '#fff',
'more_keys' => 'key_value'
]
];
Then anywhere in your app you can get the default template like
$view = config('templates.default_template'); //will get
return view($view, compact('params'));
To update a key, like has_footer, you do
config(['templates.options.has_footer' => false]); //will update
This should get you started I think
Simple Example
Let's say user changes default colour from and input and submit the form. You do
public function updateTheme(Request $request) {
if ( $request->has('background_colour') && $request->background_colour != '' ) {
config(['templates.options.background_colour' => $request->background_colour]);
}
}

Create new page in drupal

Iam new in drupal. I want to create a page and want to add data in page directly with out using backend content. I have created a page like this, page-test.tpl.php. How can I call this newly created page in browser?
First you have to create your own custom module.Let the module name be test.Create 2 files namely test.info and test.module.Declare a hook_menu function inside the test.module file.Since your module name is test your hook_menu will be named as test_menu.
Hook_menu enables modules to register paths in order to define how URL requests are handled. Paths may be registered for URL handling only, or they can register a link to be placed in a menu.
Test.module
function test_menu() {
$items = array();
$items['test'] = array(
'title' => 'My Page',
'description' => 'Study Hard',
'page callback' => 'test_simple', //Calls the function
'access arguments' => array('access content'),
);
return $items;
}
function test_simple() {
return array('#markup' => '<p>' . t('My Simple page') . '</p>');
}
Test.info
name = Test
description = Provides a sample page.
core = 7.x
After adding this try clearing the drupal cache and then navigate to /test.Try this link if you are a beginner.Try reading hook_theme which allows you to use your custom template.More about hook_theme here.Hope it might help u mate.. :)
Pages in drupal are stored in the database, not in actual code files like old websites used to. The template files (.tpl.php) are exactly that - templates. So for example, lets say you have blog content. You'd have to create a content type of 'Blog'. you could then create a template file (ex. node--blog.tpl.php) which would format the way the blog pages would look. But you would still need to add new pages through the drupal interface in order to add them to the site. You could always use an input type of PHP if you'd like to include php in the body of the page.
If you simply want to display a php file in your browser from your site, you can just upload a php file of your choice to your sites/default/files directory and access it directly via the file path.

SugarCRM - How to set the default homepage for all users

I am using SugarCRM Version 5.2.0k (Build 5837). I would like to be able to set a default home page (with dashlets I've created myself) that will be the same for all users, can anyone advice on best way to do this?
Thanks in advance for your help
I'd like to know how to do this too... see here for some ideas, but it's clear that it's not a supported feature.
I wonder if you can write a module that installs a hook for post user creation (assuming that this hook is provided) and then populate the appropriate part of the user preferences table when the hook is invoked. Of course, your module will probably break with each upgrade of SurgarCRM, so this might be more trouble than it i worth.
Edit:
I had a look at the Dash Manager module that is referenced in the thread I linked to above. It's approach is to copy the preferences of the admin user to all other users when the administrator clicks a link in the admin page. So, the admin user is used as a sort of template for other users. Rudimentary solution, but not a bad start - using a template user and treating the preferences (as stored in the DB table) as opaque seems like the way to go.
It's quite easy to do it.
I have done it in SugarCRM 6.5.23.
Here I have mentioned steps to do it:
Just copy sugarcrm_root/modules/Home/index.php and paste it in SugarCRM_root/custom/modules/Home/index.php.
Now you can customize it's behavior as you want.
You can remove default dashlets and add your own dashlets by creating one file at SugarCRM_root/custom/modules/Home/dashlets.php and add this code in it:
<?php
unset($defaultDashlets);
$defaultDashlets = array(
'CustomDashlet' => 'ModuleName',
'UpcomingAppointmentsDashlet' => 'Meetings', //Example
);
Once you do this thing still you have 3 dashlets left in your hook code you can remove it if it's needed code for that hook is like this:
$dashlets[create_guid()] = array(
'className' => 'iFrameDashlet',
'module' => 'Home',
'forceColumn' => 0,
'fileLocation' => $dashletsFiles['iFrameDashlet']['file'],
'options' => array('titleLabel' => 'LBL_DASHLET_DISCOVER_SUGAR_PRO',
'url' => '...',
'height' => 315,
));
Hope this will help you. :)

Howto: Drupal File Upload Form

I'm having difficulty figuring out how to write a module with a form that uploads files, in Drupal 6. Can anyone explain this, or point me to a good example/documentation discussing it?
EDIT:
Here is entirely what I am trying to do:
User uploads a .csv
Module reads the first line of the file to get fields
User matches csv fields with db fields
Each csv line is saved as a node (preview it first)
So far, I can do 1, 2, and 4 successfully. But it's unclear exactly how the steps should interact with each other ($form_state['redirect']? how should that be used?), and what the best practices are. And for 3, should I save that as session data?
How do I pass the file data between the various steps?
I know that node_import exists, but it's never worked for me, and my bug requests go ignored.
2nd EDIT: I used this at the start and end of every page that needed to deal with the file:
$file = unserialize($_SESSION['file']);
//alter $file object
$_SESSION['file'] = serialize(file);
I'm not sure it it's best practices, but it's been working.
This is not too difficult, you can see some info here. An example of a form with only a file upload.
function myform_form($form_state) {
$form = array('#attributes' => array('enctype' => 'multipart/form-data'));
$form['file'] = array(
'#type' => 'file',
'#title' => t('Upload video'),
'#size' => 48,
'#description' => t('Pick a video file to upload.'),
);
return $form;
}
EDIT:
Now to save the file use the file_save_upload function:
function myform_form_submit($form, $form_state) {
$validators = array();
$file = file_save_upload('file', $validators, 'path');
file_set_status($file, FILE_STATUS_PERMANENT);
}
2nd EDIT:
There's a lot of questions and ways to do the things you described. I wont go to much into the actual code of how to handle a csv file. What I would suggest is that you use the file id to keep track of the file. That would enable you to make urls that take a fid and use that to load the file you want to work on.
To get from your form to the next step, you can use the #redirect form property to get your users to the next step. From there is really depends how you do things, what you'll need to do.

Categories