module_map.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. // This file is part of the Sloodle project (www.sloodle.org)
  3. /**
  4. * This file defines a map resource module for Sloodle.
  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 map resource module class.
  18. * @package sloodle
  19. */
  20. class SloodleModuleMap 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 'mdl_sloodle' table.
  33. * @var object
  34. * @access private
  35. */
  36. var $sloodle_instance = null;
  37. /**
  38. * Instance of the extra Map data from the sloodle_map table.
  39. * @var object
  40. * @access private
  41. */
  42. var $sloodle_map = null;
  43. // FUNCTIONS //
  44. /**
  45. * Constructor
  46. */
  47. function SloodleModuleMap(&$_session)
  48. {
  49. $constructor = get_parent_class($this);
  50. parent::$constructor($_session);
  51. }
  52. /**
  53. * Loads data from the database.
  54. * Note: even if the function fails, it may still have overwritten some or all existing data in the object.
  55. * @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)
  56. * @return bool True if successful, or false otherwise
  57. */
  58. function load($id)
  59. {
  60. // Make sure the ID is valid
  61. $id = (int)$id;
  62. if ($id <= 0) return false;
  63. // Fetch the course module data
  64. if (!($this->cm = get_coursemodule_from_id('sloodle', $id))) {
  65. sloodle_debug("Failed to load course module instance #$id.<br/>");
  66. return false;
  67. }
  68. // Make sure the module is visible
  69. if ($this->cm->visible == 0) {
  70. // Ignore visibility - teachers may want to setup map when it's invisible.
  71. //sloodle_debug("Error: course module instance #$id not visible.<br/>");
  72. //return false;
  73. }
  74. // Load from the primary table: sloodle instance
  75. if (!($this->sloodle_instance = sloodle_get_record('sloodle', 'id', $this->cm->instance))) {
  76. sloodle_debug("Failed to load Sloodle module with instance ID #{$cm->instance}.<br/>");
  77. return false;
  78. }
  79. // Load from the secondary table: sloodle map
  80. if (!($this->sloodle_map = sloodle_get_record('sloodle_map', 'sloodleid', $this->sloodle_instance->id))) {
  81. sloodle_debug("Failed to load Sloodle map with sloodleid #{$this->sloodle_instance->id}.<br/>");
  82. return false;
  83. }
  84. return true;
  85. }
  86. /**
  87. * Gets the initial coordinates of the map.
  88. * @return array Numeric array (or list) containing X and Y floating point components, as global coordinates.
  89. */
  90. function get_initial_coordinates()
  91. {
  92. return array((float)$this->sloodle_map->initialx, (float)$this->sloodle_map->initialy);
  93. }
  94. /**
  95. * Sets the initial coordinates of the map.
  96. * @par int $x The global X coordinate for the map's initial location
  97. * @par int $y The global Y coordinate for the map's initial location
  98. * @return bool True if successful, or false if not.
  99. */
  100. function set_initial_coordinates($x, $y)
  101. {
  102. // Clean the data
  103. $x = (float)$x;
  104. $y = (float)$y;
  105. // Update the data
  106. $this->sloodle_map->initialx = $x;
  107. $this->sloodle_map->initialy = $y;
  108. return sloodle_update_record('sloodle_map', $this->sloodle_map);
  109. }
  110. /**
  111. * Gets the initial zoom factor of the map (1 - 6).
  112. * @return integer
  113. */
  114. function get_initial_zoom()
  115. {
  116. return (int)$this->sloodle_map->initialzoom;
  117. }
  118. /**
  119. * Checks if the pan controls should be visible.
  120. * @return bool
  121. */
  122. function check_pan_controls()
  123. {
  124. return (!empty($this->sloodle_map->showpan));
  125. }
  126. /**
  127. * Checks if map dragging should be enabled.
  128. * @return bool
  129. */
  130. function check_allow_drag()
  131. {
  132. return (!empty($this->sloodle_map->allowdrag));
  133. }
  134. /**
  135. * Checks if the zoom controls should be visible.
  136. * @return bool
  137. */
  138. function check_zoom_controls()
  139. {
  140. return (!empty($this->sloodle_map->showzoom));
  141. }
  142. /**
  143. * Returns a numeric array of locations associated with this map. Sorted by name.
  144. * Each element is an object direct from the sloodle_map_location table.
  145. * @return array
  146. */
  147. function get_locations()
  148. {
  149. $results = sloodle_get_records('sloodle_map_location', 'sloodleid', $this->sloodle_instance->id, 'name');
  150. if (!$results) return array();
  151. return $results;
  152. }
  153. /**
  154. * Adds a new location to this map.
  155. * @par float $globalx Global X coordinate for map location.
  156. * @par float $globaly Global Y coordinate for map location.
  157. * @par string $region Name of the region this location is in.
  158. * @par int $localx Local X coordinate for SLurl (local to region).
  159. * @par int $localy Local Y coordinate for SLurl (local to region).
  160. * @par int $localz Local Z coordinate for SLurl (local to region).
  161. * @par string $name Name of the location.
  162. * @par string $desc Description of the location (optional).
  163. * @return bool True if successful or false if not.
  164. */
  165. function add_location($globalx, $globaly, $region, $localx, $localy, $localz, $name, $desc = '')
  166. {
  167. // Prepare a database record
  168. $rec = new stdClass();
  169. $rec->sloodleid = $this->sloodle_instance->id;
  170. // Clean all the data and add it
  171. $rec->globalx = (float)$globalx;
  172. $rec->globaly = (float)$globaly;
  173. $rec->region = clean_text($region, FORMAT_PLAIN);
  174. $rec->localx = (int)$localx;
  175. $rec->localy = (int)$localy;
  176. $rec->localz = (int)$localz;
  177. $rec->name = clean_text($name, FORMAT_PLAIN);
  178. $rec->description = clean_text($desc, FORMAT_PLAIN);
  179. return sloodle_insert_record('sloodle_map_location', $rec, false);
  180. }
  181. // ACCESSORS //
  182. /**
  183. * Gets the name of this module instance.
  184. * @return string The name of this module
  185. */
  186. function get_name()
  187. {
  188. return $this->sloodle_instance->name;
  189. }
  190. /**
  191. * Gets the intro description of this module instance, if available.
  192. * @return string The intro description of this controller
  193. */
  194. function get_intro()
  195. {
  196. return $this->sloodle_instance->intro;
  197. }
  198. /**
  199. * Gets the identifier of the course this controller belongs to.
  200. * @return mixed Course identifier. Type depends on VLE. (In Moodle, it will be an integer).
  201. */
  202. function get_course_id()
  203. {
  204. return (int)$this->sloodle_instance->course;
  205. }
  206. /**
  207. * Gets the time at which this instance was created, or 0 if unknown.
  208. * @return int Timestamp
  209. */
  210. function get_creation_time()
  211. {
  212. return (int)$this->sloodle_instance->timecreated;
  213. }
  214. /**
  215. * Gets the time at which this instance was last modified, or 0 if unknown.
  216. * @return int Timestamp
  217. */
  218. function get_modification_time()
  219. {
  220. return (int)$this->sloodle_instance->timemodified;
  221. }
  222. /**
  223. * Gets the short type name of this instance.
  224. * @return string
  225. */
  226. function get_type()
  227. {
  228. return SLOODLE_TYPE_MAP;
  229. }
  230. /**
  231. * Gets the full type name of this instance, according to the current language pack, if available.
  232. * Note: should be overridden by sub-classes.
  233. * @return string Full type name if possible, or the short name otherwise.
  234. */
  235. function get_type_full()
  236. {
  237. return get_string('moduletype:'.SLOODLE_TYPE_MAP, 'sloodle');
  238. }
  239. }
  240. ?>