module_controller.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. // This file is part of the Sloodle project (www.sloodle.org)
  3. /**
  4. * This file defines a Controller module for Sloodle.
  5. * It is not currently used in normal SLOODLE operations.
  6. * It is only used for backup and restore purposes.
  7. *
  8. * @package sloodle
  9. * @copyright Copyright (c) 2009 Sloodle (various contributors)
  10. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  11. *
  12. * @contributor Peter R. Bloomfield
  13. */
  14. /** The Sloodle module base. */
  15. require_once(SLOODLE_LIBROOT.'/modules/module_base.php');
  16. /** General Sloodle functions. */
  17. require_once(SLOODLE_LIBROOT.'/general.php');
  18. /**
  19. * The Sloodle Controller module class.
  20. * @package sloodle
  21. */
  22. class SloodleModuleController extends SloodleModule
  23. {
  24. // DATA //
  25. /**
  26. * Internal for Moodle only - course module instance.
  27. * Corresponds to one record from the Moodle 'course_modules' table.
  28. * @var object
  29. * @access private
  30. */
  31. var $cm = null;
  32. /**
  33. * Internal only - Sloodle module instance database object. (Primary table)
  34. * Corresponds to one record from the Moodle 'sloodle' table.
  35. * @var object
  36. * @access private
  37. */
  38. var $sloodle_module_instance = null;
  39. /**
  40. * Internal only - SLOODLE Controller instance object. (Secondary table)
  41. * Corresponds to one record from the Moodle 'sloodle_controller' table.
  42. * @var object
  43. * @access private
  44. */
  45. var $sloodle_controller_instance = null;
  46. // FUNCTIONS //
  47. /**
  48. * Constructor
  49. */
  50. function SloodleModuleController(&$_session)
  51. {
  52. $constructor = get_parent_class($this);
  53. parent::$constructor($_session);
  54. }
  55. /**
  56. * Loads data from the database.
  57. * Note: even if the function fails, it may still have overwritten some or all existing data in the object.
  58. * @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)
  59. * @return bool True if successful, or false otherwise
  60. */
  61. function load($id)
  62. {
  63. // Make sure the ID is valid
  64. if (!is_int($id) || $id <= 0) {
  65. echo "<hr><pre>ID = "; print_r($id); echo "</pre><hr>";
  66. return false;
  67. }
  68. // Fetch the course module data
  69. if (!($this->cm = get_coursemodule_from_id('sloodle', $id))) return false;
  70. // Load from the primary table: Sloodle instance
  71. if (!($this->sloodle_module_instance = sloodle_get_record('sloodle', 'id', $this->cm->instance))) return false;
  72. // Check that it is the correct type
  73. if ($this->sloodle_module_instance->type != SLOODLE_TYPE_CTRL) return false;
  74. // Load from the secondary table: Distributor instance
  75. if (!($this->sloodle_controller_instance = sloodle_get_record('sloodle_controller', 'sloodleid', $this->cm->instance))) return false;
  76. return true;
  77. }
  78. // BACKUP AND RESTORE //
  79. /**
  80. * Backs-up secondary data regarding this module.
  81. * That includes everything except the main 'sloodle' database table for this instance.
  82. * @param $bf Handle to the file which backup data should be written to.
  83. * @param bool $includeuserdata Indicates whether or not to backup 'user' data, i.e. any content. Most SLOODLE tools don't have any user data.
  84. * @return bool True if successful, or false on failure.
  85. */
  86. function backup($bf, $includeuserdata)
  87. {
  88. // Backup the basic secondary data
  89. $enabled = '0';
  90. if (!empty($this->sloodle_controller_instance->enabled) && $this->sloodle_controller_instance->enabled != FALSE) $enabled = '1';
  91. fwrite($bf, full_tag('ID', 5, false, $this->sloodle_controller_instance->id));
  92. fwrite($bf, full_tag('ENABLED', 5, false, $enabled));
  93. fwrite($bf, full_tag('PASSWORD', 5, false, $this->sloodle_controller_instance->password));
  94. // Backup user data
  95. if ($includeuserdata) {
  96. // Layouts and configurations?
  97. // They are technically course-based.
  98. }
  99. return true;
  100. }
  101. /**
  102. * Restore this module's secondary data into the database.
  103. * This ignores any member data, so can be called statically.
  104. * @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)
  105. * @param array $info An associative array representing the XML backup information for the secondary module data
  106. * @param bool $includeuserdata Indicates whether or not to restore user data
  107. * @return bool True if successful, or false on failure.
  108. */
  109. function restore($sloodleid, $info, $includeuserdata)
  110. {
  111. // Construct the database record
  112. $controller = new object();
  113. $controller->sloodleid = $sloodleid;
  114. $controller->enabled = $info['ENABLED']['0']['#'];
  115. $controller->password = $info['PASSWORD']['0']['#'];
  116. $newid = sloodle_insert_record('sloodle_controller', $controller);
  117. return true;
  118. }
  119. /**
  120. * Gets the name of the user data required by this type, or an empty string if none is required.
  121. * For example, a chatroom would use the name "Messages" for user data.
  122. * Note that this should respect current language settings in Moodle.
  123. * @return string Localised name of the user data.
  124. */
  125. function get_user_data_name()
  126. {
  127. return '';
  128. }
  129. /**
  130. * Gets the number of user data records to be backed-up.
  131. * @return int A count of the number of user data records which can be backed-up.
  132. */
  133. function get_user_data_count()
  134. {
  135. return 0;
  136. }
  137. // ACCESSORS //
  138. /**
  139. * Gets the name of this module instance.
  140. * @return string The name of this controller
  141. */
  142. function get_name()
  143. {
  144. return $this->sloodle_module_instance->name;
  145. }
  146. /**
  147. * Gets the intro description of this module instance, if available.
  148. * @return string The intro description of this controller
  149. */
  150. function get_intro()
  151. {
  152. return $this->sloodle_module_instance->intro;
  153. }
  154. /**
  155. * Gets the identifier of the course this controller belongs to.
  156. * @return mixed Course identifier. Type depends on VLE. (In Moodle, it will be an integer).
  157. */
  158. function get_course_id()
  159. {
  160. return (int)$this->sloodle_module_instance->course;
  161. }
  162. /**
  163. * Gets the time at which this instance was created, or 0 if unknown.
  164. * @return int Timestamp
  165. */
  166. function get_creation_time()
  167. {
  168. return $this->sloodle_module_instance->timecreated;
  169. }
  170. /**
  171. * Gets the time at which this instance was last modified, or 0 if unknown.
  172. * @return int Timestamp
  173. */
  174. function get_modification_time()
  175. {
  176. return $this->sloodle_module_instance->timemodified;
  177. }
  178. /**
  179. * Gets the short type name of this instance.
  180. * @return string
  181. */
  182. function get_type()
  183. {
  184. return SLOODLE_TYPE_CTRL;
  185. }
  186. /**
  187. * Gets the full type name of this instance, according to the current language pack, if available.
  188. * Note: should be overridden by sub-classes.
  189. * @return string Full type name if possible, or the short name otherwise.
  190. */
  191. function get_type_full()
  192. {
  193. return get_string('moduletype:'.SLOODLE_TYPE_CTRL, 'sloodle');
  194. }
  195. }
  196. ?>