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

Add before_file_created hook to file API

XMLWordPrintable

    • MOODLE_34_STABLE
    • MOODLE_34_STABLE
    • MDL-59700-master
    • 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

      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).

            cameron1729 cameron1729
            cameron1729 cameron1729
            Mathieu Petit-Clair Mathieu Petit-Clair
            Andrew Lyons Andrew Lyons
            John Okely John Okely
            Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:

                Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.