Note: You are currently viewing documentation for Moodle 4.0. Up-to-date documentation for the latest stable version of Moodle may be available here: Developing plugins for Moodle Workplace.

Developing plugins for Moodle Workplace: Difference between revisions

From MoodleDocs
(Created page with "Third party plugins may be developed specifically for Moodle Workplace or they can be developed so they can work on both Moodle LMS and Moodle Workplace. If a plugin is devel...")
 
No edit summary
Line 21: Line 21:
     }
     }
For multi-tenancy callbacks it is convenient to use the function '''component_class_callback()''' - this function will return default value if the class or method is not found. This is the best way to filter users list based on multi-tenancy. Quick example:
For multi-tenancy callbacks it is convenient to use the function '''component_class_callback()''' - this function will return default value if the class or method is not found. This is the best way to filter users list based on multi-tenancy. Quick example:
    // Normal query:
 
Instead of direct query that looks like that:


     $sql = "SELECT u.* FROM {user} u WHERE u.deleted = 0";
     $sql = "SELECT u.* FROM {user} u WHERE u.deleted = 0";


    // Modified query:
Use the following code:


    // Add tenant condition.
    /** @uses \tool_tenant\tenancy::get_users_subquery */
     $tenantcondition = component_class_callback('tool_tenant\\tenancy', 'get_users_subquery',
     $tenantcondition = component_class_callback('tool_tenant\\tenancy', 'get_users_subquery',
         [true, true, 'u.id'], '');''
         [true, true, 'u.id'], '');''
     $sql = "SELECT u.* FROM {user} u WHERE $tenantcondition u.deleted = 0"
     $sql = "SELECT u.* FROM {user} u WHERE $tenantcondition u.deleted = 0"
When executed on Moodle LMS (without tool_tenant) this will give exactly the same result as the original query. When executed on Workplace a condition will be added to the query filtering users not hidden by multi-tenancy.
Find more examples and explanations in the '''admin/tool/tenant/README.md'''
Find more examples and explanations in the '''admin/tool/tenant/README.md'''

Revision as of 11:31, 18 May 2022

Third party plugins may be developed specifically for Moodle Workplace or they can be developed so they can work on both Moodle LMS and Moodle Workplace.

If a plugin is developed to work on Moodle Workplace only, include in version.php a strict dependency to the Workplace plugin that you need to use in your plugin, for example:

   $plugin->dependencies = array(
       'tool_program' => 2022031610, 
   );

If you want to include optional code that should be executed only on Moodle Workplace, for example, first you need to check that the relevant Workplace plugin exists, here are some examples of how to do it:

   // Check that specific class exists:
   if (class_exists('tool_program\api')) {
       // ....
   }
   // Check if the specific plugin is installed:
   if (\core_component::get_component_directory('tool_program')) {
       // ....
   }
   // Check that specific plugin is installed and the version is bigger than:
   if (\core_component::get_component_directory('tool_program') && get_config('version', 'tool_plugin') >= 2022031610) {
       // ....
   }

For multi-tenancy callbacks it is convenient to use the function component_class_callback() - this function will return default value if the class or method is not found. This is the best way to filter users list based on multi-tenancy. Quick example:

Instead of direct query that looks like that:

   $sql = "SELECT u.* FROM {user} u WHERE u.deleted = 0";

Use the following code:

   $tenantcondition = component_class_callback('tool_tenant\\tenancy', 'get_users_subquery',
       [true, true, 'u.id'], );
   $sql = "SELECT u.* FROM {user} u WHERE $tenantcondition u.deleted = 0"

When executed on Moodle LMS (without tool_tenant) this will give exactly the same result as the original query. When executed on Workplace a condition will be added to the query filtering users not hidden by multi-tenancy.

Find more examples and explanations in the admin/tool/tenant/README.md