How do I extract the actual filename of $mod->url inside /course/renderer.php ? On the verge of a paradigm shift...or at least I think so

How do I extract the actual filename of $mod->url inside /course/renderer.php ? On the verge of a paradigm shift...or at least I think so

by Frankie Kam -
Number of replies: 1
Picture of Plugin developers

Hi there smile

I am on the verge of a earth-shattering paradigm change in my Moodle 2.9 course page. Serious. The  'paradigm change' is that when I click on a PDF/ODT/TXT/PNG/JPG/GIF file that has been dragged and dropped onto my coursepage, the clicked link will result in a JQuery pop-up window displaying the contents of the file in a lightbox-like format. Like so:


The norm is the clicked link will result in the file being downloaded into my Windows7 "C:\Users\Frankie\Downloads" folder.

A working demo is found directly below. Just login as Guest and try clicking on the files.
http://www.cambridgekids.org/m29/course/view.php?id=3 

Note: if you see a new web browser tab open up instead of the pop-up, try refreshing (F5) the course page again.



I am using ViewerJS and Fancybox JQuery libraries to achieve the pop-up effects. I've got this working for PDF and ODT (Ms Word file saved as Open Document) files. The seback is that it doesn't quite work with PNG/GIF/JPG and TXT files. 

Therefore to solve this setback, I have a need to process a file depending on its file extension. So I need a way, using code, to extract the full filename, including its extension. The file /course/renderer.php has this function named:

  public function course_section_cm_name(cm_info $mod, $displayoptions = array()) {

     ...

        // Display link itself.

        $activitylink = html_writer::empty_tag('img', array('src' => $mod->get_icon_url(),

                'class' => 'iconlarge activityicon', 'alt' => ' ', 'role' => 'presentation')) . $accesstext .

                html_writer::tag('span', $instancename . $altname, array('class' => 'instancename'));

        if ($mod->uservisible) {

            $output .= html_writer::link($url, $activitylink, array('class' => $linkclasses, 'onclick' => $onclick)) .

                    $groupinglabel;

            ,,,

  }

The boldfaced code displays the actual link. For example, the image below is shows a Ms Word document file named "Book2.docx" that has been dragged and dropped onto my course page.



My question is this: how do I get the actual name of the file (Book2.docx)? The closest I can get this is to use the code $mod->url, but this just gives me the output: http://www.cambridgekids.org/m29/mod/resource/view.php?id=22 

Is there any code, e.g.,  get_fullname_of_file($mod->url) that can retrieve the full name plus extension name of the file which is "Book2.docx" ?

If I can distinguish a file resource link based on its file extension, then I can handle a text and png file differently from a PDF file.

Cheers,
Frankie Kam

P.S., this work begs the question: is it safe to open all types of file as a pop-up instead of the safer download? Is there any security risk to the Moodle site all file resource links opened as a pop-up? After all, someone could unknowingly drag and drop a file named "test.docx" when it actually contains malicious code.


Average of ratings: Useful (1)
In reply to Frankie Kam

Re: How do I extract the actual filename of $mod->url inside /course/renderer.php ? On the verge of a paradigm shift...or at least I think so

by Frankie Kam -
Picture of Plugin developers

I'll need to call $fs = get_file_storage()

Then I'll have to call $files = $fs->get_area_files($modulecontextid, 'mod_resource', 'the name of the filearea that mod_resource uses internally')

Then I need to look through the file(s) returned and get the filename.

Well, I've got the gist of it from the maker of "drag and drop file upload" in Moodle, but if anyone has the working code to do this, please let me know. Thanks.