Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-59700

Add before_file_created hook to file API

    XMLWordPrintable

    Details

    • Testing Instructions:
      Hide
      1. Add the following to mod/forum/lib.php:

        function forum_before_file_created($newfile, $fileinfo) {
            error_log("== START BEFORE FILE CREATE HOOK TRIGGER WITH STRING ==");
            error_log('forum_before_file_created');
            error_log(var_export(($newfile), true);
            error_log(var_export(($fileinfo), true);
            error_log("== END BEFORE FILE CREATE HOOK TRIGGER WITH STRING ==");
        }
        

      2. Download the attached hooks_test.php file and place it in your moodle root
      3. Tail your web server's error log
      4. Run hooks_test.php from the CLI: {php hooks_test.php}
      5. Verify you see the output similar to:

        == START BEFORE FILE CREATE HOOK TRIGGER WITH STRING ==
        forum_before_file_created
        stdClass Object
        (
            [contextid] => 2
            [component] => course
            [filearea] => test
            [itemid] => 0
            [filepath] => /
            [filename] => test.txt
            [timecreated] => 1501815056
            [timemodified] => 1501815056
            [userid] =>
            [source] =>
            [author] =>
            [license] =>
            [status] => 0
            [sortorder] => 0
        )
         
        Array(
            [pathname] =>
            [content] => foo
        )
         
        == END BEFORE FILE CREATE HOOK TRIGGER WITH STRING ==
         
         
        == START BEFORE FILE CREATE HOOK TRIGGER WITH PATHNAME ==
        forum_before_file_created
        stdClass Object(
            [contextid] => 2
            [component] => course
            [filearea] => test2
            [itemid] => 0
            [filepath] => /
            [filename] => test2.txt
            [timecreated] => 1501815056
            [timemodified] => 1501815056
            [mimetype] => text/plain
            [userid] =>
            [source] =>
            [author] =>
            [license] =>
            [status] => 0
            [sortorder] => 0
        )
         
        Array(
            [pathname] => ./index.php
            [content] =>
        )
         
        == END BEFORE FILE CREATE HOOK TRIGGER WITH PATHNAME ==
        

      6. Setup the filesystem, and dropbox repositories
      7. Try linking files from those repositories as a reference file
      Show
      Add the following to mod/forum/lib.php : function forum_before_file_created($newfile, $fileinfo) { error_log("== START BEFORE FILE CREATE HOOK TRIGGER WITH STRING ==");     error_log('forum_before_file_created');     error_log(var_export(($newfile), true);     error_log(var_export(($fileinfo), true); error_log("== END BEFORE FILE CREATE HOOK TRIGGER WITH STRING =="); } Download the attached hooks_test.php file and place it in your moodle root Tail your web server's error log Run hooks_test.php from the CLI: {php hooks_test.php} Verify you see the output similar to: == START BEFORE FILE CREATE HOOK TRIGGER WITH STRING == forum_before_file_created stdClass Object (     [contextid] => 2     [component] => course     [filearea] => test     [itemid] => 0     [filepath] => /     [filename] => test.txt     [timecreated] => 1501815056     [timemodified] => 1501815056     [userid] =>    [source] =>     [author] =>     [license] =>     [status] => 0     [sortorder] => 0 )   Array(     [pathname] =>     [content] => foo )   == END BEFORE FILE CREATE HOOK TRIGGER WITH STRING ==     == START BEFORE FILE CREATE HOOK TRIGGER WITH PATHNAME == forum_before_file_created stdClass Object(     [contextid] => 2     [component] => course     [filearea] => test2     [itemid] => 0     [filepath] => /     [filename] => test2.txt     [timecreated] => 1501815056     [timemodified] => 1501815056     [mimetype] => text/plain     [userid] =>     [source] =>    [author] =>    [license] =>    [status] => 0     [sortorder] => 0 )   Array(     [pathname] => ./index.php     [content] => )   == END BEFORE FILE CREATE HOOK TRIGGER WITH PATHNAME == Setup the filesystem, and dropbox repositories Try linking files from those repositories as a reference file
    • Affected Branches:
      MOODLE_34_STABLE
    • Fixed Branches:
      MOODLE_34_STABLE
    • Pull Master Branch:
      MDL-59700-master

      Description

      MDL-57476 added a hook for just after file creation; it would be useful to also have a hook for before the file is created (one usecase for such a hook would be a file quota system).

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                cameron1729 cameron1729
                Reporter:
                cameron1729 cameron1729
                Peer reviewer:
                Mathieu Petit-Clair
                Integrator:
                Andrew Nicols
                Tester:
                John Okely
                Participants:
                Component watchers:
                Matteo Scaramuccia, Jake Dallimore, Jun Pataleta, Ryan Wyllie
              • Votes:
                2 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Fix Release Date:
                  13/Nov/17