module_distributor.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. // This file is part of the Sloodle project (www.sloodle.org)
  3. /**
  4. * This file defines the Sloodle Distributor module.
  5. *
  6. * @package sloodle
  7. * @copyright Copyright (c) 2008 Sloodle (various contributors)
  8. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  9. *
  10. * @contributor Peter R. Bloomfield
  11. */
  12. /** The Sloodle module base. */
  13. require_once(SLOODLE_LIBROOT.'/modules/module_base.php');
  14. /** General Sloodle functions. */
  15. require_once(SLOODLE_LIBROOT.'/general.php');
  16. /**
  17. * The Sloodle Distributor module class.
  18. * @package sloodle
  19. */
  20. class SloodleModuleDistributor extends SloodleModule
  21. {
  22. // DATA //
  23. /**
  24. * Internal for Moodle only - course module instance.
  25. * Corresponds to one record from the Moodle 'course_modules' table.
  26. * @var object
  27. * @access private
  28. */
  29. var $cm = null;
  30. /**
  31. * Internal only - Sloodle module instance database object.
  32. * Corresponds to one record from the Moodle 'sloodle' table.
  33. * @var object
  34. * @access private
  35. */
  36. var $sloodle_module_instance = null;
  37. /**
  38. * Internal only - Sloodle Distributor instance database object.
  39. * Corresponds to one record from the Moodle 'sloodle_distributor' table.
  40. * @var object
  41. * @access private
  42. */
  43. var $sloodle_distributor_instance = null;
  44. // FUNCTIONS //
  45. /**
  46. * Constructor
  47. */
  48. function SloodleModuleDistributor(&$_session)
  49. {
  50. $constructor = get_parent_class($this);
  51. parent::$constructor($_session);
  52. }
  53. /**
  54. * Loads data from the database.
  55. * Note: even if the function fails, it may still have overwritten some or all existing data in the object.
  56. * @param mixed $id The site-wide unique identifier for all modules. Type depends on VLE. On Moodle, it is an integer course module identifier ('id' field of 'course_modules' table)
  57. * @return bool True if successful, or false otherwise
  58. */
  59. function load($id)
  60. {
  61. // Make sure the ID is valid
  62. if (!is_int($id) || $id <= 0) return false;
  63. // Fetch the course module data
  64. if (!($this->cm = get_coursemodule_from_id('sloodle', $id))) return false;
  65. // Load from the primary table: Sloodle instance
  66. if (!($this->sloodle_module_instance = sloodle_get_record('sloodle', 'id', $this->cm->instance))) return false;
  67. // Check that it is the correct type
  68. if ($this->sloodle_module_instance->type != SLOODLE_TYPE_DISTRIB) return false;
  69. // Load from the secondary table: Distributor instance
  70. if (!($this->sloodle_distributor_instance = sloodle_get_record('sloodle_distributor', 'sloodleid', $this->cm->instance))) return false;
  71. return true;
  72. }
  73. /**
  74. * Gets a list of all objects for this Distributor.
  75. * @return array An array of strings, each string containing the name of an object in this Distributor.
  76. */
  77. function get_objects()
  78. {
  79. // Get all distributor record entries for this distributor, sorted alphabetically
  80. $recs = sloodle_get_records('sloodle_distributor_entry', 'distributorid', $this->sloodle_distributor_instance->id, 'name');
  81. if (!$recs) return array();
  82. // Convert it to an array of strings
  83. $entries = array();
  84. foreach ($recs as $r) {
  85. $entries[] = $recs->name;
  86. }
  87. return $entries;
  88. }
  89. /**
  90. * Sets the list of objects in this Distributor
  91. * @param array $objects An array of strings, each string containing the name of an object in the Distributor.
  92. * @return bool True if successful, or false if not
  93. */
  94. function set_objects($objects)
  95. {
  96. // Delete all existing records for this Distributor
  97. sloodle_delete_records('sloodle_distributor_entry', 'distributorid', $this->sloodle_distributor_instance->id);
  98. // Go through each new entry
  99. $result = true;
  100. foreach ($objects as $o) {
  101. // Construct the new record
  102. $rec = new stdClass();
  103. $rec->distributorid = $this->sloodle_distributor_instance->id;
  104. $rec->name = sloodle_clean_for_db($o);
  105. // Insert it
  106. if (!sloodle_insert_record('sloodle_distributor_entry', $rec)) $result = false;
  107. }
  108. return $result;
  109. }
  110. /**
  111. * Sets the UUID of the XMLRPC channel used for requests.
  112. * @param string $uuid The UUID of an XMLRPC channel
  113. * @return bool True if successful, or false if not
  114. */
  115. function set_channel($uuid)
  116. {
  117. // Update the values
  118. $this->sloodle_distributor_instance->channel = $uuid;
  119. $this->sloodle_distributor_instance->timeupdated = time();
  120. // Update the database
  121. return sloodle_update_record('sloodle_distributor', $this->sloodle_distributor_instance);
  122. }
  123. /**
  124. * Request that the specified object be sent to the specified avatar.
  125. * @param string $objname Name of the object to send
  126. * @param string $uuid UUID of the avatar to send the object to
  127. * @return bool True if successful, or false if not.
  128. */
  129. function send_object($objname, $uuid)
  130. {
  131. // Check that the object exists in this distributor
  132. if (!sloodle_record_exists('sloodle_distributor_entry', 'distributorid', $this->distrib_id, 'name', $objname)) return false;
  133. // Send the XMLRPC request
  134. return sloodle_send_xmlrpc_message($this->sloodle_distributor_instance->channel, 0, "1|OK\\nSENDOBJECT|$uuid|$objname");
  135. }
  136. // BACKUP AND RESTORE //
  137. /**
  138. * Backs-up secondary data regarding this module.
  139. * That includes everything except the main 'sloodle' database table for this instance.
  140. * @param $bf Handle to the file which backup data should be written to.
  141. * @param bool $includeuserdata Indicates whether or not to backup 'user' data, i.e. any content. Most SLOODLE tools don't have any user data.
  142. * @return bool True if successful, or false on failure.
  143. */
  144. function backup($bf, $includeuserdata)
  145. {
  146. // The only thing we can backup is the ID.
  147. // Everything depends on a Vending Machine in-world connecting to the site.
  148. fwrite($bf, full_tag('ID', 5, false, $this->sloodle_distributor_instance->id));
  149. return true;
  150. }
  151. /**
  152. * Restore this module's secondary data into the database.
  153. * This ignores any member data, so can be called statically.
  154. * @param int $sloodleid The ID of the primary SLOODLE entry this restore belongs to (i.e. the ID of the record in the "sloodle" table)
  155. * @param array $info An associative array representing the XML backup information for the secondary module data
  156. * @param bool $includeuserdata Indicates whether or not to restore user data
  157. * @return bool True if successful, or false on failure.
  158. */
  159. function restore($sloodleid, $info, $includeuserdata)
  160. {
  161. // Construct the database record
  162. $distributor = new object();
  163. $distributor->sloodleid = $sloodleid;
  164. $distributor->channel = '';
  165. $distributor->timeupdated = 0;
  166. $newid = sloodle_insert_record('sloodle_distributor', $distributor);
  167. return true;
  168. }
  169. /**
  170. * Gets the name of the user data required by this type, or an empty string if none is required.
  171. * For example, a chatroom would use the name "Messages" for user data.
  172. * Note that this should respect current language settings in Moodle.
  173. * @return string Localised name of the user data.
  174. */
  175. function get_user_data_name()
  176. {
  177. return '';
  178. }
  179. /**
  180. * Gets the number of user data records to be backed-up.
  181. * @return int A count of the number of user data records which can be backed-up.
  182. */
  183. function get_user_data_count()
  184. {
  185. return 0;
  186. }
  187. // ACCESSORS //
  188. /**
  189. * Gets the name of this module instance.
  190. * @return string The name of this controller
  191. */
  192. function get_name()
  193. {
  194. return $this->sloodle_module_instance->name;
  195. }
  196. /**
  197. * Gets the intro description of this module instance, if available.
  198. * @return string The intro description of this controller
  199. */
  200. function get_intro()
  201. {
  202. return $this->sloodle_module_instance->intro;
  203. }
  204. /**
  205. * Gets the identifier of the course this controller belongs to.
  206. * @return mixed Course identifier. Type depends on VLE. (In Moodle, it will be an integer).
  207. */
  208. function get_course_id()
  209. {
  210. return (int)$this->sloodle_module_instance->course;
  211. }
  212. /**
  213. * Gets the time at which this instance was created, or 0 if unknown.
  214. * @return int Timestamp
  215. */
  216. function get_creation_time()
  217. {
  218. return $this->sloodle_module_instance->timecreated;
  219. }
  220. /**
  221. * Gets the time at which this instance was last modified, or 0 if unknown.
  222. * @return int Timestamp
  223. */
  224. function get_modification_time()
  225. {
  226. return $this->sloodle_module_instance->timemodified;
  227. }
  228. /**
  229. * Gets the short type name of this instance.
  230. * @return string
  231. */
  232. function get_type()
  233. {
  234. return SLOODLE_TYPE_DISTRIB;
  235. }
  236. /**
  237. * Gets the full type name of this instance, according to the current language pack, if available.
  238. * Note: should be overridden by sub-classes.
  239. * @return string Full type name if possible, or the short name otherwise.
  240. */
  241. function get_type_full()
  242. {
  243. return get_string('moduletype:'.SLOODLE_TYPE_DISTRIB, 'sloodle');
  244. }
  245. }
  246. ?>