Force users to download resources not view in window.

Force users to download resources not view in window.

by Duncan Smith -
Number of replies: 25
Hi folks, finally running moodle 1.5 at my school. When I add word, excel, powerpoint documents as resources and the pupils click on the link they currently open in a new window - there is no prompt to open or save. However, at our school pupils do not have acces to the 'file' tab so cannot save from within the window. Is there a way with 1.5 to force this to happen?

Someone kindly helped me out with version 1.4 by using a different file.php that caused this to happen. However this is incompatible with version 1.5.

I would like this to happen as staff wish to add word worksheets etc which pupils download and work on then save to their area etc.

Any suggestions would be gratefully recieved.

Cheers, Dunks. big grin 
Average of ratings: -
In reply to Duncan Smith

Re: Force users to download resources not view in window.

by Rob Johnson -
Duncan - Try adding ?forcedownload=1 to the end of the links to the files you want to make downloadable.  This is only a problem with IEFirefox does not display these types of files (OpenOffice too) in the browser.  Using the force download parameter should do what you want.

Rob
Average of ratings: Useful (1)
In reply to Rob Johnson

Re: Force users to download resources not view in window.

by Duncan Smith -
Thanks for the reply Rob.

A bit of a newbie to the inner workings so your reply poses a couple of quick questions.

1. Is this solution to be added to each doc added as a resource? If so, are you aware of any solution that would force the filetype to do this ie: every word, excel and powerpoint to automatically try to download.
2. Where do I go to add this code?

Many thanks for your assistance.

Duncan
In reply to Duncan Smith

Re: Force users to download resources not view in window.

by Rob Johnson -
Duncan - Yes, you need to add the parameter to each resource. I use it a great deal. You add the parameter to the end of the link when you "Choose" the file. The link ends up looking something like, filename.doc?forcedownload=1.  I have attached a screenshot as well.  Give it a try.smile

Rob
Attachment forcedownload.gif
In reply to Rob Johnson

Re: Force users to download resources not view in window.

by Duncan Smith -
Rob, thanks for that. Works perfectly. However, as I had a concern that some of my teaching staff are not IT literate and purely uploading the resource is enough of a battle let alone adding the ?force.... etc I took your info & played with the file.php in the root moodle folder. There is a line that states:

    $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);

Changing the 0 after forcedownload', to 1 has had the same effect, but site wide???!!!

Hope this may be of use, many thanks for your help. Out of interest where are you based? Always interested to see where I am receiving help from as this is such a great global community!

I am Sandhurst in Berkshire, UK.
www.sandhurst.bracknell-forest.sch.uk/moodle is our effort!

In reply to Duncan Smith

Re: Force users to download resources not view in window.

by Rob Johnson -
Duncan - I will try out the hack on file.php.  Thanks.

I teach high school tech and geography classes (paperless, thanks to Moodle) in Torrance, California USA.

Rob
In reply to Duncan Smith

Re: Force users to download resources not view in window.

by Jamie Thompson -
Just to say, for anyone with the same issues. I can vouch that changing line 20 of file.php as Duncan describes does work site wide.

Previewing office documents in-browser was confusing the hell out of my users. This neatly sidesteps the issue. Thanks Duncan.
In reply to Duncan Smith

Re: Force users to download resources not view in window.

by Jon Witts -
Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Duncan et al...

Thanks!! I have been looking to do this somehow for quite some time! I had gone to the length of ensuring that all of the computers on our school network had the settings changed at a Windows level to not do this. (Very time consuming!) M$ Office Apps opening in the same window is a real pain.

I am off to change that setting right now!

Thanks!!

Jon
In reply to Jon Witts

Re: Force users to download resources not view in window.

by Jon Witts -
Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Ahh - so today I have just found a problem with this change.. It now tries to download all files from Moodle. For example we have lots of resources that are html pages.. these to are forced to download, breaking all of the relative links in the files. I have now changed the setting back to how it was originally.

The ability to be able to choose file types to force download and those not to force it what I really need... It is only office documents (.doc, .ppt and .xls ) that need to be forced.

would it be possible to have an if else statement around line 20 that checked for the ending of the file name and changed the parameter accordingly...

Something like...

$relativepath = get_file_argument('file.php');

if ($filetype == "doc") {
$forcedownload = optional_param('forcedownload', 1, PARAM_BOOL);
}
else {
$forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
}

Would need to define variable $filetype , could even have it as an array that could be defined with all of the file types that we want to force to download, and have this settable in the site configuration.

Any pointers for this one? Would be a very useful feature, and stop Microsoft office from changing the way people want their sites to operate.

Jon
In reply to Jon Witts

Re: Force users to download resources not view in window.

by Rob Johnson -
As in my original reply, I use the parameter on a link by link basis. I have some html files that are templates and need to downloaded and other that are to be viewed in the browser. You can even force individual video and audio files to download instead of playing with the multimedia filter.

It may be a lot of work to add the ?forcedownload=1 to links after the fact, but it does provide the flexibility that you need.

Rob
In reply to Rob Johnson

Re: Force users to download resources not view in window.

by Jon Witts -
Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
I agree that being able to add the parameter does provide the flexibility, and this would still be present with my proposed change. The difference is that the site admin could define a list of file types that they wanted to force download by default. This would allow the admin control of how files are handled on their site file type by type, without a teacher having to be taught how to add the parameter extensions to their files.

Those teachers that had a specific case, that needed files to be downloaded that were by default not, could easily be shown the parameter for their needs. The amount of time required for me to change every single Microsoft Office link through out our whole Moodle install is completely unfeasible. (It would take days!!!) I think that this solution would prove a lot more useful, and could even be incorporated into the site config page, so that the admin can set the file types there that they want to be downloaded. (No need to go into the code!)

I am going to have a look at my PHP books tonight when I get home and brush up on regular expressions, as I am sure that I can get this to work, at least within the code.. Then I will look at incorporating it into the site config page...

I will keep you all posted.

Jon

____________________________________

Umm so thought this might work but it doesn't.... thoughtful

if ($mimeinfo == 'doc') {
$forcedownload = optional_param('forcedownload', 1, PARAM_BOOL);
} else if ($mimeinfo == 'xls') {
$forcedownload = optional_param('forcedownload', 1, PARAM_BOOL);
} else if ($mimeinfo == 'ppt') {
$forcedownload = optional_param('forcedownload', 1, PARAM_BOOL);
} else {
$forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
}

I am trying to use the array that is defined in lib/filelib.php

This array is already defining all of the file types, and I thought I would be able to just use the element of that array to check whether we want it to force download or not... Back to the books I think.....
In reply to Jon Witts

Re: Force users to download resources not view in window.

by John Mc Hale -

Right-click link, choose 'Save Target As' command saves an awful lot of hassle!

Regards.

In reply to John Mc Hale

Re: Force users to download resources not view in window.

by Jon Witts -
Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
I see your point, but I am trying to get a uniform behavior for file types without the user or teacher having to do anything different from how they normally would. Microsoft Office applications embedding themselves in Internet Explorer has caused issues with a number of our staff, as most of the Office application commands are not present in IE.

Some staff already direct students to right click and "Save As" but there are many staff who are only just getting used to uploading resources, let alone giving technical instructions to students!
In reply to Jon Witts

Re: Force users to download resources not view in window.

by Guy Thomas -
Picture of Core developers Picture of Plugin developers
I've made moodle download the file instead of opening it in a window.

Modify lib/filelib.php to include code highlighted in yellow

function send_file($path, $filename, $lifetime=86400 , $filter=0, $pathisstring=false, $forcedownload=false) {
global $CFG;

if (mimeinfo('type', $filename)=='application/vnd.ms-powerpoint'){
$forcedownload=true;
}
In reply to Guy Thomas

Re: Force users to download resources not view in window.

by Dale Jones -
Nice solution, works a treat.

For some reason, adding ?forcedownload=1 to the filename doesn't work for me in 1.8. It used to in 1.6.
In reply to Dale Jones

Re: Force users to download resources not view in window.

by Rob Johnson -
I use it in 1.8 and it does work on my production site, and on my test site.smile
In reply to Guy Thomas

Re: Force users to download resources not view in window.

by Duncan Smith -
Having posted my original solution of modifying file.php I have moved schools and set up a new moodle using 1.8.

I cannot force users to download using the file.php or the lib/filelib.php method. Only adding the code to each individual file works at upload works and I do not want to be restricted to this method.

Is anyone else having this issue? II have tested on pc's with IE6 & IE7 - same result, always opens in the browser. The school has another .asp system running and this does force users to download so I can't believe it's a system setting???
In reply to Guy Thomas

Re: Force users to download resources not view in window.

by Guy Thomas -
Picture of Core developers Picture of Plugin developers

I've created a more elegant hack for forcing files to download.


See:

http://moodle.org/mod/data/view.php?d=13&rid=1115

In reply to Guy Thomas

Re: Force users to download resources not view in window.

by Christoph Ruda -

Hi Guy,

Many thanks for this hack ,which is indeed quite elegant. We had a problem here where some of our Vista installations had a problem with downloading and opening .doc files from links to files rather than resources (where you can set the 'force download' option). This was discussed here.

After applying your solution things seem to work fine, more testing is needed still but things look promising.

Christoph

In reply to Jon Witts

Re: Force users to download resources not view in window.

by viresh Barahanpur -

Hi,

I have installed moodle1.9.2 but i am having problems with fourcedownload when i am adding a new resource with load button and with check box of forcedownload Its say an Error: Message that : "Could not add an instance of the resource.". I Had tried with fourm got the solutions with patches to add in file.php nad resource.class.php and modedit.php but all of the moodle version 1.9 only hence not working please can any one answer me.

In reply to Duncan Smith

Re: Force users to download resources not view in window.

by Just H -
You could also upload all word, excel, powerpoint documents as zip files. As far as I'm aware the user has to download a zip file, it would mean no hacking core files and therefore upgrade friendlier and it will also make it faster for them as an added bonus smile
In reply to Just H

Re: Force users to download resources not view in window.

by curious engine -
More than 2 weeks, I have been trying to resolve the problem:

option to force download files in Moodle 1.9 Stable version..

IN VAIN!!!

I am frustrated. The thing I want to ask Moodle is : why don't you update the stable version and put the version in which the MDF- resolved version of users being able to download the resource files (not only pdf but also word files).

I am tired to trying to hack the resource.class.php, the file.php, lib/filelib.php and whatever else!!

Is this open source: with enough eyeballs, all bugs are not present!???
I even took the courage to install the version 1.7 hoping that this bug would not be present there but all the FILES STILL ARE BEING OPENED IN A NEW WINDOW.
In reply to curious engine

Re: Force users to download resources not view in window.

by Rob Johnson -
Using the ?forcedownload=1 parameter mentioned above works in my 1.9+ installation. Word files should only be an issue with IE. Firefox will ask if you want to download by default.
In reply to Rob Johnson

Re: Force users to download resources not view in window.

by curious engine -
Hi rob
i've tried applying this parameter.

but it does not work.

Even the forcedownload checkbox is here.

still no effect
In reply to curious engine

Re: Force users to download resources not view in window.

by Rob Johnson -
I just tried a word file using IE 7 and Moodle 1.9+. I had not noticed the checkbox before 1.9. If you are linking from within a Label, Webpage, or Assignment using Moodle's editor, you will still need to add ?forcedownload=1 to the end of the link.

This aside, IE7 seems to behave more like Firefox with files than IE6. Even if I do not check the checkbox, IE7 still opens a dialog box asking if I want to save the file. What browser and OS are you using? Can you reproduce the issue from other computers?

Rob