modules.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. // This file is part of the Sloodle project (www.sloodle.org)
  3. /**
  4. * This file brings all of the Sloodle module classes together.
  5. * It also provides functionality to create and load different module types.
  6. *
  7. * @package sloodle
  8. * @copyright Copyright (c) 2008 Sloodle (various contributors)
  9. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  10. *
  11. * @contributor Peter R. Bloomfield
  12. */
  13. /** Base module. */
  14. require_once(SLOODLE_LIBROOT.'/modules/module_base.php');
  15. require_once(SLOODLE_LIBROOT.'/general.php');
  16. // Now we will go through every file in the "lib/modules" folder which starts "module_", and include it.
  17. $MODULESPATH = SLOODLE_LIBROOT.'/modules';
  18. $modulefiles = sloodle_get_files($MODULESPATH, true);
  19. // Go through each file
  20. if (is_array($modulefiles)) {
  21. foreach ($modulefiles as $mf) {
  22. // Does this filename start with "module_" and end with ".php"?
  23. if (strcasecmp(substr($mf, 0, 7), 'module_') == 0 && strcasecmp(substr($mf, -4), '.php') == 0) {
  24. // Yes - include it
  25. include_once($MODULESPATH.'/'.$mf);
  26. }
  27. }
  28. }
  29. // We will store an associative array of module types to module class names
  30. // e.g. { 'chat'=>'SloodleModuleChat', 'distributor'=>'SloodleModuleDistributor', ...}
  31. global $SLOODLE_MODULE_CLASS;
  32. $SLOODLE_MODULE_CLASS = array();
  33. // Go through each declared class
  34. $allclasses = get_declared_classes();
  35. foreach ($allclasses as $c) {
  36. // Is this class a subclass of the Sloodle Module type?
  37. if (strcasecmp(get_parent_class($c), 'SloodleModule') == 0) {
  38. // Fetch the type name
  39. $curtype = call_user_func(array($c, 'get_type'));
  40. if (!empty($curtype)) {
  41. // Add it to our array with its type
  42. $SLOODLE_MODULE_CLASS[$curtype] = $c;
  43. }
  44. }
  45. }
  46. /*
  47. Static method to return a list of available module types => module names.
  48. Avoids having to use the global variable elsewhere.
  49. */
  50. function sloodle_available_modules() {
  51. global $SLOODLE_MODULE_CLASS;
  52. return $SLOODLE_MODULE_CLASS;
  53. }
  54. /**
  55. * Constructs a appropriate Sloodle module object based on the named type.
  56. * @param string $type The type of module to construct - typically a short name, such as 'chat' or 'blog'
  57. * @param SloodleSession &$_session The {@link SloodleSession} object to pass to the module on construction, or just null
  58. * @param mixed $id The identifier of the module instance to load from the database (or null if there is no module data)
  59. * @return SloodleModule|bool Returns the cosntructed module object, or false if it fails
  60. */
  61. function sloodle_load_module($type, &$_session, $id = null)
  62. {
  63. global $SLOODLE_MODULE_CLASS;
  64. // Abort if the type is not recognised
  65. if (!array_key_exists($type, $SLOODLE_MODULE_CLASS)) {
  66. sloodle_debug("Module load failed - type \"$type\" not recognised.<br/>");
  67. return false;
  68. }
  69. // Construct the object, based on the class name in our array
  70. $module = new $SLOODLE_MODULE_CLASS[$type]($_session);
  71. // Load the data from the database, if necessary
  72. if ($id != null) {
  73. if ($module->load((int)$id)) return $module;
  74. sloodle_debug("Failed to load module data from database with ID $id.<br/>");
  75. return false;
  76. }
  77. // Everything seems OK
  78. return $module;
  79. }
  80. /**
  81. * Constructs and loads an appropriate SLOODLE module object, based on the 'sloodle' record (that is, the instance or instance ID).
  82. * The appropriate type is detected from the instance data.
  83. * The object is provided with a dummy SloodleSession object.
  84. * @param int|object $instance Either an integer instance ID, or a record from the 'sloodle' table.
  85. * @return SloodleModule|bool Returns the constructed module object, or false if it fails.
  86. */
  87. function sloodle_quick_load_module_from_instance($instance)
  88. {
  89. // Get an instance record if necessary
  90. if (is_numeric($instance)) {
  91. $instance = sloodle_get_record('sloodle', 'id', (int)$instance);
  92. if ($instance === false) return false;
  93. }
  94. // Attempt to get the course module ID
  95. $cm = get_coursemodule_from_instance('sloodle', $instance->id);
  96. if ($cm === false) return false;
  97. // Attempt to load the module
  98. $dummysession = new SloodleSession(false);
  99. return sloodle_load_module($instance->type, $dummysession, (int)$cm->id);
  100. }
  101. /**
  102. * Constructs and loads an appropriate SLOODLE module object, based on the course module or course module ID.
  103. * The appropriate type is detected automatically.
  104. * The object is provided with a dummy SloodleSession object.
  105. * @param int|object $cm Either an integer course module ID, or a course module record.
  106. * @return SloodleModule|bool Returns the constructed module object, or false if it fails.
  107. */
  108. function sloodle_quick_load_module_from_cm($cm)
  109. {
  110. // Obtain a course module object if necessary
  111. if (is_numeric($cm)) {
  112. $cm = get_coursemodule_from_id('sloodle', $cm);
  113. if ($cm === false) return false;
  114. }
  115. // Get an instance record
  116. $instance = sloodle_get_record('sloodle', 'id', (int)$cm->instance);
  117. if ($instance === false) return false;
  118. // Attempt to load the module
  119. $dummysession = new SloodleSession(false);
  120. return sloodle_load_module($instance->type, $dummysession, (int)$cm->id);
  121. }
  122. ?>