Reduce clicks by enabling Multiple File Uploads for Moodle 1.9.x

2957

Learning Outcome

WIRIS

At the end of this post, you will know how to enable multiple file uploads to save on the amount of work (and mouse-clicks!) needed to upload N number of files in a link resource or assignment activity.

 

Necessity, (who) is the Mother of invention, said Plato.

Two days ago, I gave my Information Technology class an assignment where they had to upload nine  Microsoft Word files within a week.

I had earlier enabled Moodle’s  “Advanced uploading of files” assignment activity for that purpose. Here is one student’s response.

The frank complaint that started me on a Quest.

 

Simon, one of the more IT-savvy and inquisitive students, had posted a comment on my course page’s Whisper box. It caught my eye and I read these words:

Sir, can you make moodurian able to update more files at once? it’s a waste of time (sic) update file by file.

Waste of time? What waste of time? I replied that all one needs to do is to zip up your files, then upload that zipped file. A simple solution right? If it were that simple and convenient (which it isn’t), then how come none of my students had resorted to that little trick? Instead they obediently uploaded one file at a time like robots. Automatons.

Was Simon’s comment a plea for help and a clear sign of frustration?

I began to think about Simon’s brazen comment. I pondered. Then it dawned upon me that I was subjecting my students to mouse-click “hell”. Why? Because the “Advanced uploading of files” assignment activity only allows one file to be browsed, picked and uploaded at a time. Moodle 1.9 is good, no, great at many things, but this is NOT one of them. I then thought “Why NOT” change the file picker in Moodle 1.9? Why not, indeed!

How long and how many clicks do you think it would take you to upload 10 files in an assignment?

I then decided to embark on a noble Quest to improve Moodle 1.9’s file picker. I searched moodle.org for information. I wanted to know how to select multiple-selected files from a dialog box. What? Pressing the Control key and left-clicking the mouse doesn’t select multiple files? Argh!

Okay, perhaps Moodle 2.2 doesn’t suffer from such an interface feature. I was wrong. In fact I became more frustrated because my Moodle 2.2 site’s file picker only allowed one file to be picked from my Windows XP Desktop. Bummer! (Note: I bet this problem will be a thing of the past with Moodle 2.3, 2.4, 2.5, etc.)

Even mighty Moodle 2.2 doesn’t allow me to multiple select files in its file picker. Call me picky about the picker, but I’d prefer to have another file selected with a Control-key press and a left mouse click. Can I have fish and French fries with that too?

Professor Higgins, in the movie My Fair Lady, sighed and asked “Why can’t a woman be more like a man?”. Well, why can’t Moodle be more like Facebook?

Why can’t Moodle be more like Facebook? Sigh.

Need another example?

Or more like Dropbox? Why not?

Facebook has the multiple-file select dialog box when selecting images to be put into a photo album. Dropbox has the same thing to select more than one file at a time. So why not Moodle? Even as I type this, Moodle 2.3 is just around the corner and has features that are getting at least one person very, very excited. The file picker in Moodle 2.3 has been improved, and may even have that multiple-file select feature (I don’t know, I haven’t tried it out) that will render this post and my work obsolete.

After many hours over the last two days, I finally chanced upon two forum threads on Moodle.org that shed some light on my quest. Christian Kloimboeck’s post and Gustav W Delius’s post on Moodle.org proved to be promising.

After reading Gustav W Delius’ post, I knew that it was possible. C’mon, after all this is Computer Science, right? “England’s winning, anything’s possible!” (Flushed Away, 2007).

Diving in Christian’s post, I decided to get my finger tips dirty by actually doing some coding. After many hours, I am happy to report that I had managed to achieve something that might be useful for Moodle 1.9 administrators, teachers, students and users all over the world:

Now ain’t that a sight for sore eyes?

Just for fun, I also did this:

Enabling multiple file uploads in a “Link to a file or website” resource. Cool.

Christian Kloimboeck, I want to shake your hand and say “Thank you!” for your post on Moodle.org. Thanks to your ingenious code here, I managed to create something out of nothing.

Now then, where have we seen a file browser/picker like this before? Hmm…how about on Facebook? Yeah, that right! So if even Facebook has such an interface to upload files, then we can’t go wrong with modifying Moodle 1.9.15 to do the same right?

This appears on FB when you have trouble uploading photos on FB due to slow Internet speeds.

What about Dropbox, then?

Even Dropbox has a Basic Uploader in case of problems.Yup, Moodle 1.9’s basic uploader has been in existance for a long time.All it needs is some Web 2.0 revamped interface like Moodle 2.3.

So here’s what I achieved. I managed to allow the user to pick up to 10 files from the Desktop and then with a single click of the “Upload these files” button, upload all files direct into the assignment data folder. Not as good as Facebook’s or Dropbox’s multiple file picker in a single dialogue box, but definitely a time-saver.

Now, the moment of truth. I’m going to let you in on the secret. I’m going to tell you how I did it in two parts. In Part I, I will explain in detail how I enabled my Moodle 1.9 site to allow multiple file uploads in a ‘Link to a file or website’ resource. In Part II, I show you how I enabled uploading of multiple files in an assignment. Specifically, the ‘Advanced uploading of files’ assignment activity.

Now let’s get going. The game’s afoot, Watson!


Part I: 
Enabling dual file uploads in a ‘Link to a file or website” resource

 

To save you tears and to make this as painless as I possibly can, I will simplify the learning by showing you how to enable two file uploads instead of 10 file uploads. Once you see how easy it is, you can easily expand the code yourself to enable up to 4, 6 or even 10 number of uploads.

Oh don’t worry, right at the bottom of this post lies the download links to the modified php files that allow you to achieve the same results as I. You might want to go there right now because the next few Page Downs will probably give you a headache or that sinking feeling that “it’s too complicated and I don’t have time to wade through all this geeky stuff!”.

Your choice.

Okay, so still with me? Let’s be brave and proceed with the surgery, erm, I mean the code modification, shall we? This will only hurt just a little bit.

Edit /moodle or public html/files/index.php

Insert code shown in the gray background inside the images shown. The same code is reproduced for your copy-and-paste convenience, at the bottom of each image.

You can refer to the line numbers shown to know the exact location of the code to be inserted. You might also want to make use of the Ctrl-F (Find text) feature of your web browser.

$userfile1= optional_param(‘userfile1’,”,PARAM_FILE);

$um1 = new upload_manager(‘userfile1’,false,false,$course,false,0);if ($um1->process_file_uploads($dir)) { notify(get_string(‘uploadedfile’)); }

upload_print_form_fragment(1,array(‘userfile1’),null,false,null,$upload_max_filesize,0,false);

 

If good English grammar matters to you, then edit /moodle or public_html/lang/en_utf8/moodle.php

Line 1590:
$string[‘uploadafile’] = ‘Upload a file’;
change to
$string[‘uploadafile’] = ‘Upload one or more files‘;

$string[‘uploadafile’

Like to maintain consistently good and correct English grammar on your Moodle site? Then edit /moodle or public_html/lang/en_utf8/moodle.php (the same file!)

Line 1609:
$string[‘uploadthisfile’] = ‘Upload a file’;
change to
$string[‘uploadthisfile’] = ‘Upload these files‘;

$string[‘uploadthisfile’
Now let’s put this thing to the test!

Click the “Choose or upload a file” button

 

So far so good….now for the acid test.

Click the “Upload one or more files” button

Igor!!! Pull the SWITCH!!!

Yes, Master.

Rejoice!

 

You are alive! I created you! Muahahahahahaha.

End of Part I.

 

At this point in this blog post, you just might want to brew some char and sit down in a nice comfortable chair because this is where it gets….complicated.

 

Part II: Enabling multiple file uploads in an ‘Advanced Uploading Of Files” assignment activity

Once again, I will simplify the learning process by showing you how to enable two file uploads instead of ten file uploads. Once you see how easy it is, you can easily expand the code yourself to enable up to N number of uploads. Really! Trust me.

Edit /moodle or public_html/mod/assignment/type/upload/assignment.class.php

As usual, insert the code shown in the gray background.

upload_print_form_fragment(1,array(‘newfile1’),null,false,null,0,$this->assignment->maxbytes,false);

 

Tip:  If you  find these images too small to read, then in Firefox, right-click on an image and select the “View Image” option. An enlarged image will appear in a new Firefox browser tab.

 

$output .= upload_print_form_fragment(1,array(‘newfile1’),null,false,null,0,0,true);

 

$um1 = new upload_manager(‘newfile1’,false,true,$this->course,false,0,true);if (!$um1->process_file_uploads($dir)) { print_header(get_string(‘upload’)); notify(get_string(‘uploaderror’, ‘assignment’)); echo $um1->get_errors(); print_continue($returnurl); print_footer(‘none’); die; }

 

$um1 = new upload_manager(‘newfile1’,false,true,$this->course,false,$this->assignment->maxbytes,true);

 

Just comment off the code in lines 640, 642, 643 and 644.

 

 

to the above section is too large to fit inside this caption. So I have reproduced it below:

	if ($um1->process_file_uploads($dir) and confirm_sesskey()) {
            $submission = $this->get_submission($USER->id, true); //create new submission if needed
            $updated = new object();
            $updated->id           = $submission->id;
            $updated->timemodified = time();
            if (update_record('assignment_submissions', $updated)) {
                add_to_log($this->course->id, 'assignment', 'upload',
                        'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
                $submission = $this->get_submission($USER->id);
                $this->update_grade($submission);
                if (!$this->drafts_tracked()) {
                    $this->email_teachers($submission);
                }
            } else {
                $new_filename = $um1->get_new_filename();
                $this->view_header(get_string('upload'));
                notify(get_string('uploadnotregistered', 'assignment', $new_filename));
                print_continue($returnurl);
                $this->view_footer();
                die;
            }
            redirect('view.php?id='.$this->cm->id);
        }
        $this->view_header(get_string('upload'));
        notify(get_string('uploaderror', 'assignment'));
        echo $um->get_errors();
	echo $um1->get_errors();

 

File downloads

By now most of you, if not all, would have given up following my code insertions. I know. I would too. I mean, who in his or her right mind, has so much time to read all that waffle above and follow every line number to see what code needs to be inserted where?

So, to make it easier for everyone who wants to see this feature on their Moodle 1.9 site, I have, surprise, surprise, included download links below. Please backup your own original files before using my hacked files. And please don’t sue me if my code nukes your site and you receive  a blank screen instead of a webpage. Use at your own peril! Anyway, you know what they say, “No pain, no gain”.

The files below are exactly the same files as on my Moodle 1.9.15 production site at http://www.moodurian.com. Click on each link to download the php file.

Download index.php (modified “Link to file or website” resource)

After downloading, please make sure you rename the downloaded file as index.php

/files/index_10_uploads.php  (upload up to 10 files)

/files/index_2_uploads.php (upload up to 2 files)

/files/index_original.php (original Moodle 1.9.15 index.php file in case you need it)

Download moodle.php (modified labels on the buttons)

/lang/en_utf8/moodle.php

(Sorry, I didn’t make the original moodle.php file available since the changes are minor and non-critical I think)

Download assignment.class.php (“Advanced uploading of files” activity)

After downloading, please make sure you rename the downloaded file as assignment.class.php

mod/assignment/type/upload/assignment.class._allows10filestobeuploaded.php (upload up to 10 files)

mod/assignment/type/upload/assignment.class_allows2filestobeuploaded.php (upload up to 2 files)

mod/assignment/type/upload/assignment.class_original.php (the original Moodle 1.9.15 file in case you need it!)

 

Conclusion

For those still using Moodle 1.9, I hope that this feature will ease your burden of uploading files. If you found it useful, or you think that this is a God-send, please send me an E-mail or comment on this post. I would love to hear from you!

If your teachers, fellow lecturers and/or students are thanking you for saving their fingers from RSI due to the reduced mouse clicks, then I would love to hear from you too.

If you’re using Moodle 2.3, then all this is much ado about nothing, since (I think) the File Picker in M2.3 is light years ahead of M1.9.x. Good on you Mate, but for me, I’m stuck with megabytes and megabytes of M1.9.x data in the moodledata folder which will stay that way for some time. So that’s why this post and many like it exists.

Okay, so if you are still with me, and you’ve tried your darndest to follow my inserted/modified code, then I salute you, Sire or Madam, for your tenacity. For your desire to really get this ‘modified feature’ to work on your Moodle 1.9 site. I hope you succeeded. If you didn’t, then by all means, please email me at [email protected]

Finally, a big THANK YOU for Simon for highlighting what many of us already know about Moodle 1.9.x – the frustration of, at times, too many mouse clicks. I, ahem, I mean, WE (Gustav W Delius, Christian Kloimboeck, Simon Chong and Frankie Kam), helped to make Moodle 1.9.x better.

Necessity is indeed the mother of invention!

Try a Durian fruit today.
Frankie Kam
Melaka, Malaysia