module_presenter.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. <?php
  2. // This file is part of the Sloodle project (www.sloodle.org)
  3. /**
  4. * This file defines a Presenter 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 presenter module class.
  18. * @package sloodle
  19. */
  20. class SloodleModulePresenter 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. * Internal only - a database objects representing the Presenter itself.
  39. * Corresponds to one record from the Moodle 'mdl_sloodle_presenter' table.
  40. * @var object
  41. * @access private
  42. */
  43. var $presenter = null;
  44. // FUNCTIONS //
  45. /**
  46. * Constructor
  47. */
  48. function SloodleModulePresenter(&$_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. $id = (int)$id;
  63. if ($id <= 0) return false;
  64. // Fetch the course module data
  65. if (!($this->cm = get_coursemodule_from_id('sloodle', $id))) {
  66. sloodle_debug("Failed to load course module instance #$id.<br/>");
  67. return false;
  68. }
  69. // Make sure the module is visible
  70. if ($this->cm->visible == 0) {
  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_presenter
  80. if (!($this->presenter = sloodle_get_record('sloodle_presenter', 'sloodleid', $this->cm->instance))) {
  81. sloodle_debug("Failed to load secondary module table with instance ID #{$this->cm->instance}.<br/>");
  82. return false;
  83. }
  84. return true;
  85. }
  86. /**
  87. * Gets an array of absolute URLs to images in this slideshow, all correctly ordered.
  88. * @return 2d numeric array, each element associates an entry ID to a numeric array of URL string, the name of the source type, and the name of the slide.
  89. */
  90. function get_entry_urls()
  91. {
  92. // Search the database for entries
  93. $recs = sloodle_get_records_select_params('sloodle_presenter_entry', "sloodleid = ? ", array($this->sloodle_instance->id), 'ordering');
  94. if (!$recs) return array();
  95. // Format it all nicely into a simple array
  96. $output = array();
  97. foreach ($recs as $r) {
  98. // Substitute the source data for the name if no name is given.
  99. $name = '';
  100. if (isset($r->name)){
  101. $name = $r->name;
  102. }
  103. if (empty($name)) $name = $r->source;
  104. $output[$r->id] = array($r->source, $r->type, $name);
  105. }
  106. return $output;
  107. }
  108. /**
  109. * Gets a specific slide from the presentation, identified by its database ID.
  110. * Note that the slide position is NOT determined by this function so it will be given as -1.
  111. * Use "get_slides()" to fetch an array with valid slide positions.
  112. * @param int $id The record ID of the sldie in the database.
  113. * @return SloodlePresenterSlide|bool A slide object if successful, or false if it could not be found.
  114. */
  115. function get_slide($id)
  116. {
  117. // Sanitize the data
  118. $id = (int)$id;
  119. // Fetch the requested slide
  120. $rec = sloodle_get_record('sloodle_presenter_entry', 'id', $id);
  121. if (!$rec) return false;
  122. // Substitute the source data for the name if no name is given.
  123. $name = '';
  124. if (isset($rec->name)){
  125. $name = $rec->name;
  126. }
  127. if (empty($name)) $name = $rec->source;
  128. // Convert plugin class names back to legacy slide types.
  129. // (The class names were used temporarily, but deemed unnecessary.)
  130. $type = strtolower($rec->type);
  131. switch ($type)
  132. {
  133. case 'sloodlepluginpresenterslideimage': case 'presenterslideimage': $type = 'image'; break;
  134. case 'sloodlepluginpresenterslideweb': case 'presenterslideweb': $type = 'web'; break;
  135. case 'sloodlepluginpresenterslidevideo': case 'presenterslidevideo': $type = 'video'; break;
  136. }
  137. return new SloodlePresenterSlide($rec->id, $this, $name, $rec->source, $type, $rec->ordering, -1);
  138. }
  139. /**
  140. * Gets an ordered associative array of slides in presentation order.
  141. * @return Array associating slide IDs to SloodlePresenterSlide objects if successful, or false if not.
  142. */
  143. function get_slides()
  144. {
  145. // Make sure we have valid ordering
  146. $this->validate_ordering();
  147. // Fetch the database records
  148. $recs = sloodle_get_records_select_params('sloodle_presenter_entry', "sloodleid = ? ", array($this->sloodle_instance->id), 'ordering');
  149. if (!$recs) return array();
  150. // Construct the array of objects
  151. $output = array();
  152. $slideposition = 1;
  153. foreach ($recs as $r) {
  154. // Substitute the source data for the name if no name is given.
  155. $name = '';
  156. if (isset($r->name)){
  157. $name = $r->name;
  158. }
  159. if (empty($name)) $name = $r->source;
  160. // Convert plugin class names back to legacy slide types.
  161. // (The class names were used temporarily, but deemed unnecessary.)
  162. $type = strtolower($r->type);
  163. switch ($r->type)
  164. {
  165. case 'sloodlepluginpresenterslideimage': case 'presenterslideimage': $type = 'image'; break;
  166. case 'sloodlepluginpresenterslideweb': case 'presenterslideweb': $type = 'web'; break;
  167. case 'sloodlepluginpresenterslidevideo': case 'presenterslidevideo': $type = 'video'; break;
  168. }
  169. // Add the slide to our list
  170. $output[$r->id] = new SloodlePresenterSlide($r->id, $this, $name, $r->source, $type, $r->ordering, $slideposition);
  171. $slideposition++;
  172. }
  173. return $output;
  174. }
  175. /**
  176. * Adds a new entry to the presentation.
  177. * @param string $source A string containing the source address -- must start with http for absolute URLs
  178. * @param string $type Name of the type of source, e.g. "web", "image", or "video"
  179. * @param string $name Name of the slide
  180. * @param integer $position Integer indicating the position of the new entry. If negative, then it is placed last in the presentation.
  181. * @return True if successful, or false on failure.
  182. */
  183. function add_entry($source, $type, $name, $position = -1)
  184. {
  185. // Make sure our entry ordering is valid before we start
  186. $this->validate_ordering();
  187. // Construct and attempt to insert the new record
  188. $rec = new stdClass();
  189. $rec->sloodleid = $this->sloodle_instance->id;
  190. $rec->source = $source;
  191. $rec->name = $name;
  192. $rec->type = $type;
  193. if ($position < 0) {
  194. $num = sloodle_count_records('sloodle_presenter_entry', 'sloodleid', $this->sloodle_instance->id);
  195. $rec->ordering = ((int)$num + 1) * 10;
  196. } else {
  197. $rec->ordering = ($position * 10) - 1; // Ordering works in multiples of 10, starting at 10.
  198. }
  199. $result = (bool)sloodle_insert_record('sloodle_presenter_entry', $rec, false);
  200. // Make sure our entry ordering is valid again now
  201. $this->validate_ordering();
  202. return $result;
  203. }
  204. /**
  205. * Edits an existing entry in the presentation.
  206. * @param int $id The ID of the entry in the database.
  207. * @param string $source A string containing the source address -- must start with http for absolute URLs
  208. * @param string $type Name of the type of source, e.g. "web", "image", or "video"
  209. * @param string $name Name of the slide
  210. * @param integer $position Integer indicating the desired position of the entry. If negative, then its position is left unchanged.
  211. * @return True if successful, or false on failure.
  212. */
  213. function edit_entry($id, $source, $type, $name, $position = -1)
  214. {
  215. // Ensure we have valid ordering to begin with
  216. $this->validate_ordering();
  217. // Attempt to fetch the existing entry from the database
  218. $id = (int)$id;
  219. $rec = sloodle_get_record('sloodle_presenter_entry', 'id', $id, 'sloodleid', $this->sloodle_instance->id);
  220. if (!$rec) return false;
  221. // Apply the changes to the record
  222. $rec->source = $source;
  223. $rec->name = $name;
  224. $rec->type = $type;
  225. if ($position > 0) {
  226. $rec->ordering = ($position * 10) - 1; // Ordering works in multiples of 10, starting at 10.
  227. }
  228. // Update the database
  229. $result = (bool)sloodle_update_record('sloodle_presenter_entry', $rec);
  230. // Make sure our entry ordering is valid
  231. $this->validate_ordering();
  232. return $result;
  233. }
  234. /**
  235. * Deletes the identified entry by ID.
  236. * Only works if the entry is part of this presentation.
  237. * @param int $id The ID of an entry record to delete
  238. * @return bool True if successful or false otherwise.
  239. */
  240. function delete_entry($id)
  241. {
  242. $result = sloodle_delete_records('sloodle_presenter_entry', 'sloodleid', $this->sloodle_instance->id, 'id', $id);
  243. // Fix the ordering
  244. $this->validate_ordering();
  245. if ($result === false) return false;
  246. return true;
  247. }
  248. /**
  249. * Moves the ID'd entry forward or back in the presentation ordering.
  250. * Only works if the entry is part of this presentation.
  251. * @param int $id The ID of the entry to move.
  252. * @param bool $forward TRUE to move the entry forward (closer to the beginning) or FALSE to push it back (closer to the end)
  253. * @return void
  254. */
  255. function move_entry($id, $forward)
  256. {
  257. // Start by ensuring uniform ordering, starting at 10.
  258. $this->validate_ordering();
  259. // Attempt to move the specified entry in the appropriate direction
  260. $entry = sloodle_get_record('sloodle_presenter_entry', 'sloodleid', $this->sloodle_instance->id, 'id', $id);
  261. if (!$entry) return;
  262. if ($forward) {
  263. // Avoid a negative ordering value.
  264. if ($entry->ordering >= 20) $entry->ordering -= 15;
  265. } else {
  266. $entry->ordering += 15;
  267. }
  268. sloodle_update_record('sloodle_presenter_entry', $entry);
  269. // Re-validate the entry ordering
  270. $this->validate_ordering();
  271. }
  272. /**
  273. * Relocates the identified entry to the specified position in the presentation.
  274. * Positions count from 1 upwards. If an entry already exists in that location, then the existing entry is pushed to the next position.
  275. * @param int $id The ID of the entry to relocate
  276. * @param int $pos The position in the presentation to move the slide to
  277. * @return void
  278. */
  279. function relocate_entry($id, $pos)
  280. {
  281. // Start by ensuring uniform ordering, starting at 10.
  282. $this->validate_ordering();
  283. // Calculate the ordering value for our entry.
  284. // The first entry in a presentation has ordering 10, and subsequent entries increment by 10.
  285. // Therefore, we can insert an entry before an existing slot by moving it to one BEFORE the appropriate multiple of 10.
  286. $newordering = ($pos * 10) - 1;
  287. // Write the new ordering to the database, and re-validate the order
  288. $entry = sloodle_get_record('sloodle_presenter_entry', 'sloodleid', $this->sloodle_instance->id, 'id', $id);
  289. if (!$entry) return;
  290. $entry->ordering = $newordering;
  291. sloodle_update_record('sloodle_presenter_entry', $entry);
  292. $this->validate_ordering();
  293. }
  294. /**
  295. * Validates the ordering value of all entries in the presenter.
  296. * Gives each record an ordering value from 10 upwards, incrementing by 10 each time.
  297. * @return void
  298. */
  299. function validate_ordering()
  300. {
  301. // Get all entries in this presentation
  302. $entries = sloodle_get_records('sloodle_presenter_entry', 'sloodleid', $this->sloodle_instance->id, 'ordering');
  303. if (!$entries || count($entries) <= 1) return;
  304. // Go through each entry in our array, and give it a valid ordering value.
  305. $ordering = 10;
  306. foreach ($entries as $entry) {
  307. $entry->ordering = $ordering;
  308. sloodle_update_record('sloodle_presenter_entry', $entry);
  309. $ordering += 10;
  310. }
  311. }
  312. // BACKUP AND RESTORE //
  313. /**
  314. * Backs-up secondary data regarding this module.
  315. * That includes everything except the main 'sloodle' database table for this instance.
  316. * @param $bf Handle to the file which backup data should be written to.
  317. * @param bool $includeuserdata Indicates whether or not to backup 'user' data, i.e. any content. Most SLOODLE tools don't have any user data.
  318. * @return bool True if successful, or false on failure.
  319. */
  320. function backup($bf, $includeuserdata)
  321. {
  322. // Data about the Presenter itself
  323. fwrite($bf, full_tag('ID', 5, false, $this->presenter->id));
  324. fwrite($bf, full_tag('FRAMEWIDTH', 5, false, $this->presenter->framewidth));
  325. fwrite($bf, full_tag('FRAMEHEIGHT', 5, false, $this->presenter->frameheight));
  326. // Attempt to fetch all the slides in the presentation
  327. $slides = $this->get_slides();
  328. if (!$slides) return false;
  329. // Data about the slides in the presenter.
  330. // Currently this will only backup the raw URLs, and won't transfer any files.
  331. // In future, it should backup any files which are on the same server.
  332. fwrite($bf, start_tag('SLIDES', 5, true));
  333. foreach ($slides as $slide) {
  334. fwrite($bf, start_tag('SLIDE', 6, true));
  335. // Convert plugin class names back to simple slide types
  336. switch ($slide->type) {
  337. case 'SloodlePluginPresenterSlideImage': case 'PresenterSlideImage': $slide->type = 'image'; break;
  338. case 'SloodlePluginPresenterSlideVideo': case 'PresenterSlideVideo': $slide->type = 'video'; break;
  339. case 'SloodlePluginPresenterSlideWeb': case 'PresenterSlideWeb': $slide->type = 'web'; break;
  340. }
  341. fwrite($bf, full_tag('ID', 7, false, $slide->id));
  342. fwrite($bf, full_tag('NAME', 7, false, $slide->name));
  343. fwrite($bf, full_tag('SOURCE', 7, false, $slide->source));
  344. fwrite($bf, full_tag('TYPE', 7, false, $slide->type));
  345. fwrite($bf, full_tag('ORDERING', 7, false, $slide->ordering));
  346. fwrite($bf, end_tag('SLIDE', 6, true));
  347. }
  348. fwrite($bf, end_tag('SLIDES', 5, true));
  349. return true;
  350. }
  351. /**
  352. * Restore this module's secondary data into the database.
  353. * This ignores any member data, so can be called statically.
  354. * @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)
  355. * @param array $info An associative array representing the XML backup information for the secondary module data
  356. * @param bool $includeuserdata Indicates whether or not to restore user data
  357. * @return bool True if successful, or false on failure.
  358. */
  359. function restore($sloodleid, $info, $includeuserdata)
  360. {
  361. // Construct the database record for the Presenter itself
  362. $presenter = new object();
  363. $presenter->sloodleid = $sloodleid;
  364. $presenter->framewidth = $info['FRAMEWIDTH']['0']['#'];
  365. $presenter->frameheight = $info['FRAMEHEIGHT']['0']['#'];
  366. $presenter->id = sloodle_insert_record('sloodle_presenter', $presenter);
  367. // Go through each slide in the presenter backup
  368. $numslides = count($info['SLIDES']['0']['#']['SLIDE']);
  369. $curslide = null;
  370. for ($slidenum = 0; $slidenum < $numslides; $slidenum++) {
  371. // Get the current slide data
  372. $curslide = $info['SLIDES']['0']['#']['SLIDE'][$slidenum]['#'];
  373. // Construct a new Presenter slide database object
  374. $slide = new object();
  375. $slide->sloodleid = $sloodleid;
  376. $slide->name = $curslide['NAME']['0']['#'];
  377. $slide->source = $curslide['SOURCE']['0']['#'];
  378. $slide->type = $curslide['TYPE']['0']['#'];
  379. $slide->ordering = $curslide['ORDERING']['0']['#'];
  380. $slide->id = sloodle_insert_record('sloodle_presenter_entry', $slide);
  381. }
  382. return true;
  383. }
  384. /**
  385. * Gets the name of the user data required by this type, or an empty string if none is required.
  386. * For example, a chatroom would use the name "Messages" for user data.
  387. * Note that this should respect current language settings in Moodle.
  388. * @return string Localised name of the user data.
  389. */
  390. function get_user_data_name()
  391. {
  392. return '';
  393. }
  394. /**
  395. * Gets the number of user data records to be backed-up.
  396. * @return int A count of the number of user data records which can be backed-up.
  397. */
  398. function get_user_data_count()
  399. {
  400. return 0;
  401. }
  402. // ACCESSORS //
  403. /**
  404. * Gets the width of the Presenter frame (for viewing in Moodle).
  405. * @return int Width of the Presenter frame.
  406. */
  407. function get_frame_width()
  408. {
  409. return (int)$this->presenter->framewidth;
  410. }
  411. /**
  412. * Gets the height of the Presenter frame (for viewing in Moodle).
  413. * @return int Height of the Presenter frame.
  414. */
  415. function get_frame_height()
  416. {
  417. return (int)$this->presenter->frameheight;
  418. }
  419. /**
  420. * Gets the name of this module instance.
  421. * @return string The name of this module
  422. */
  423. function get_name()
  424. {
  425. return $this->sloodle_instance->name;
  426. }
  427. /**
  428. * Gets the intro description of this module instance, if available.
  429. * @return string The intro description of this controller
  430. */
  431. function get_intro()
  432. {
  433. return $this->sloodle_instance->intro;
  434. }
  435. /**
  436. * Gets the identifier of the course this controller belongs to.
  437. * @return mixed Course identifier. Type depends on VLE. (In Moodle, it will be an integer).
  438. */
  439. function get_course_id()
  440. {
  441. return (int)$this->sloodle_instance->course;
  442. }
  443. /**
  444. * Gets the time at which this instance was created, or 0 if unknown.
  445. * @return int Timestamp
  446. */
  447. function get_creation_time()
  448. {
  449. return (int)$this->sloodle_instance->timecreated;
  450. }
  451. /**
  452. * Gets the time at which this instance was last modified, or 0 if unknown.
  453. * @return int Timestamp
  454. */
  455. function get_modification_time()
  456. {
  457. return (int)$this->sloodle_instance->timemodified;
  458. }
  459. /**
  460. * Gets the short type name of this instance.
  461. * @return string
  462. */
  463. function get_type()
  464. {
  465. return SLOODLE_TYPE_PRESENTER;
  466. }
  467. /**
  468. * Gets the full type name of this instance, according to the current language pack, if available.
  469. * Note: should be overridden by sub-classes.
  470. * @return string Full type name if possible, or the short name otherwise.
  471. */
  472. function get_type_full()
  473. {
  474. return get_string('moduletype:'.SLOODLE_TYPE_PRESENTER, 'sloodle');
  475. }
  476. }
  477. /**
  478. * Defines a single slide from a presentation, containing raw data.
  479. * The data will usually need to interpreted by a slide plugin.
  480. * @package sloodle
  481. */
  482. class SloodlePresenterSlide
  483. {
  484. // FUNCTIONS //
  485. // Constructor
  486. function SloodlePresenterSlide($id=0, $presenter=null, $name='', $source='', $type='', $ordering=0, $slideposition=0)
  487. {
  488. $this->id = $id;
  489. $this->presenter = $presenter;
  490. $this->name = $name;
  491. $this->source = $source;
  492. $this->type = $type;
  493. $this->ordering = $ordering;
  494. $this->slideposition = $slideposition;
  495. }
  496. // DATA //
  497. /**
  498. * The ID of this slide in the DB table of slides.
  499. * @access public
  500. * @var int
  501. */
  502. var $id = 0;
  503. /**
  504. * The SloodleModulePresenter object relating the presentation this slide is in
  505. * @access public
  506. * @var SloodleModulePresenter
  507. */
  508. var $presenter = null;
  509. /**
  510. * The name of this slide
  511. * @access public
  512. * @var string
  513. */
  514. var $name = '';
  515. /**
  516. * The source data for this slide. Normally this would be an absolute url (starting with a protocol specifier like HTTP),
  517. * or a relative path, in which case it is treated as an internal Moodle file.
  518. * Plugins may alternatively use this to store data which is to be rendered.
  519. * @access public
  520. * @var string
  521. */
  522. var $source = '';
  523. /**
  524. * The type of this slide. This will be the ID of a plugin in the "presenter-slide" category, such as "web", "image", or "video".
  525. * @access public
  526. * @var string
  527. */
  528. var $type = '';
  529. /**
  530. * The ordering value for this slide. This should not generally be used. Refer to 'slideposition' instead.
  531. * @access public
  532. * @var integer
  533. */
  534. var $ordering = 0;
  535. /**
  536. * The position of this slide in the presentation. This is a 1-based count.
  537. * @access public
  538. * @var integer
  539. */
  540. var $slideposition = 0;
  541. }
  542. ?>