course.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. <?php
  2. // This file is part of the Sloodle project (www.sloodle.org)
  3. /**
  4. * This file defines a structure for Sloodle data about a particular Moodle course.
  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. /** Include the general Sloodle library. */
  13. require_once(SLOODLE_LIBROOT.'/general.php');
  14. /** Include the Sloodle controller structure. */
  15. require_once(SLOODLE_LIBROOT.'/controller.php');
  16. /** Include the layout profile management stuff. */
  17. require_once(SLOODLE_LIBROOT.'/layout_profile.php');
  18. /**
  19. * The Sloodle course data class
  20. * @package sloodle
  21. */
  22. class SloodleCourse
  23. {
  24. // DATA //
  25. /**
  26. * The database object of the course to which this object relates.
  27. * Corresponds to the "course" table in Moodle.
  28. * Is null if not yet set
  29. * @var object
  30. * @access private
  31. */
  32. var $course_object = null;
  33. /**
  34. * The Sloodle course data object, if it exists.
  35. * Is null if not yet set.
  36. * @var object
  37. * @var private
  38. */
  39. var $sloodle_course_data = null;
  40. /**
  41. * The {@link SloodleController} object being used to access this course, if available.
  42. * @var SloodleController
  43. * @var public
  44. */
  45. var $controller = null;
  46. // FUNCTIONS //
  47. /**
  48. * Constructor
  49. */
  50. function SloodleCourse()
  51. {
  52. $this->controller = new SloodleController();
  53. }
  54. /**
  55. * Determines whether or not course data has been loaded.
  56. * @return bool
  57. */
  58. function is_loaded()
  59. {
  60. if (empty($this->course_object) || empty($this->sloodle_course_data)) return false;
  61. return true;
  62. }
  63. /**
  64. * Gets the identifier of the course in the VLE.
  65. * @return mixed Course identifier. Type depends on VLE. (In Moodle, it will be an integer).
  66. */
  67. function get_course_id()
  68. {
  69. return (int)$this->course_object->id;
  70. }
  71. /**
  72. * Gets the VLE course object.
  73. * WARNING: this should only be used when ABSOLUTELY necessary.
  74. * The contents are specific to the VLE.
  75. * @return mixed Type and content depends upon VLE. In Moodle, it is an object representing a record from the 'course' table.
  76. */
  77. function get_course_object()
  78. {
  79. return $this->course_object;
  80. }
  81. /**
  82. * Gets the short name of this course in the VLE.
  83. * @return string Shortname of this course.
  84. */
  85. function get_short_name()
  86. {
  87. return $this->course_object->shortname;
  88. }
  89. /**
  90. * Gets the full name of this course in the VLE.
  91. * @return string Fullname of this course.
  92. */
  93. function get_full_name()
  94. {
  95. return $this->course_object->fullname;
  96. }
  97. /**
  98. * Is auto registration permitted on this site AND course?
  99. * Takes into account the site-wide setting as well.
  100. * @return bool
  101. */
  102. function check_autoreg()
  103. {
  104. // Check the site *and* the course value
  105. return ((bool)sloodle_autoreg_enabled_site() && (bool)$this->get_autoreg());
  106. }
  107. /**
  108. * Gets the autoregistration value for this course only.
  109. * (Ignores the site setting).
  110. * @return bool
  111. */
  112. function get_autoreg()
  113. {
  114. return (!empty($this->sloodle_course_data->autoreg));
  115. }
  116. /**
  117. * Enables auto-registration for this course.
  118. * NOTE: it may still be disabled at site-level.
  119. * @return void
  120. */
  121. function enable_autoreg()
  122. {
  123. $this->sloodle_course_data->autoreg = 1;
  124. }
  125. /**
  126. * Disables auto-registration for this course.
  127. * NOTE: does not affect the site setting.
  128. * @return void
  129. */
  130. function disable_autoreg()
  131. {
  132. $this->sloodle_course_data->autoreg = 0;
  133. }
  134. /**
  135. * Is auto enrolment permitted on this site AND course?
  136. * Takes into account the site-wide setting as well.
  137. * @return bool
  138. */
  139. function check_autoenrol()
  140. {
  141. // Check the site *and* the course value
  142. return ((bool)sloodle_autoenrol_enabled_site() && $this->get_autoenrol());
  143. }
  144. /**
  145. * Gets the auto enrolment value for this course only.
  146. * (Ignores the site setting).
  147. * @return bool
  148. */
  149. function get_autoenrol()
  150. {
  151. return (!empty($this->sloodle_course_data->autoenrol));
  152. }
  153. /**
  154. * Enables auto-enrolment for this course.
  155. * NOTE: it may still be disabled at site-level.
  156. * @return void
  157. */
  158. function enable_autoenrol()
  159. {
  160. $this->sloodle_course_data->autoenrol = 1;
  161. }
  162. /**
  163. * Disables auto-enrolment for this course.
  164. * NOTE: does not affect the site setting.
  165. * @return void
  166. */
  167. function disable_autoenrol()
  168. {
  169. $this->sloodle_course_data->autoenrol = 0;
  170. }
  171. /**
  172. * Determines whether or not the course is available.
  173. * Checks that the course has not been disabled or hidden etc..
  174. * @return bool True if the course is available
  175. */
  176. function is_available()
  177. {
  178. // Check visbility
  179. if (empty($this->course_object->visible)) return false;
  180. return true;
  181. }
  182. /**
  183. * Gets the position of the loginzone as a string vector <x,y,z>
  184. * @return string
  185. */
  186. function get_loginzone_position()
  187. {
  188. if (isset($this->sloodle_course_data->loginzonepos)) return $this->sloodle_course_data->loginzonepos;
  189. return '';
  190. }
  191. /**
  192. * Sets the position of the loginzone as a string vector <x,y,z>
  193. * @param string $pos A string position vector <x,y,z>
  194. * @return void
  195. * @todo Update to handle arrays as well
  196. */
  197. function set_loginzone_position($pos)
  198. {
  199. $this->sloodle_course_data->loginzonepos = $pos;
  200. }
  201. /**
  202. * Sets the position of the loginzone as a set of components
  203. * @param float $x
  204. * @param float $y
  205. * @param float $z
  206. * @return void
  207. */
  208. function set_loginzone_position_xyz($x, $y, $z)
  209. {
  210. $this->sloodle_course_data->loginzonepos = "<$x,$y,$z>";
  211. }
  212. /**
  213. * Gets the size of the loginzone as a string vector <x,y,z>
  214. * @return string
  215. */
  216. function get_loginzone_size()
  217. {
  218. if (isset($this->sloodle_course_data->loginzonesize)) return $this->sloodle_course_data->loginzonesize;
  219. return '';
  220. }
  221. /**
  222. * Sets the size of the loginzone as a string vector <x,y,z>
  223. * @param string $size A string size vector <x,y,z>
  224. * @return void
  225. * @todo Update to handle arrays as well
  226. */
  227. function set_loginzone_size($size)
  228. {
  229. $this->sloodle_course_data->loginzonesize = $size;
  230. }
  231. /**
  232. * Sets the size of the loginzone as a set of components
  233. * @param float $x
  234. * @param float $y
  235. * @param float $z
  236. * @return void
  237. */
  238. function set_loginzone_size_xyz($x, $y, $z)
  239. {
  240. $this->sloodle_course_data->loginzonesize = "<$x,$y,$z>";
  241. }
  242. /**
  243. * Gets the region of the loginzone
  244. * @return string
  245. */
  246. function get_loginzone_region()
  247. {
  248. if (isset($this->sloodle_course_data->loginzoneregion)) return $this->sloodle_course_data->loginzoneregion;
  249. return '';
  250. }
  251. /**
  252. * Sets the region of the loginzone
  253. * @param string $region A string naming a region
  254. * @return void
  255. */
  256. function set_loginzone_region($region)
  257. {
  258. $this->sloodle_course_data->loginzoneregion = $region;
  259. }
  260. /**
  261. * Gets the timestamp of the last time the loginzone was updated
  262. * @return int
  263. */
  264. function get_loginzone_time_updated()
  265. {
  266. if (isset($this->sloodle_course_data->loginzoneupdated)) return $this->sloodle_course_data->loginzoneupdated;
  267. return 0;
  268. }
  269. /**
  270. * Sets the timestamp of the last time the loginzone was updated
  271. * @param int $timestamp A unix timestamp. If null, the current timestamp is used
  272. * @return void
  273. */
  274. function set_loginzone_time_updated($timestamp = null)
  275. {
  276. if ($timestamp == null) $timestamp = time();
  277. $this->sloodle_course_data->loginzoneupdated = $timestamp;
  278. }
  279. /**
  280. * Generates a new LoginZone allocation for the specified user.
  281. * @param SloodleUser $user The user for whom this allocation should be made
  282. * @return string|false A SLurl for the allocation, or false if it was unsuccessful
  283. */
  284. function generate_loginzone_allocation($user)
  285. {
  286. // Make sure the necessary data is available
  287. if (!(isset($this->sloodle_course_data->loginzonepos) && isset($this->sloodle_course_data->loginzonesize) && isset($this->sloodle_course_data->loginzoneregion))) return false;
  288. // Delete any existing LoginZone allocation for this user
  289. sloodle_delete_records('sloodle_loginzone_allocation', 'userid', $user->get_user_id());
  290. // We will try up to 10 times to find a new available position
  291. $loginzonesize = sloodle_vector_to_array($this->sloodle_course_data->loginzonesize);
  292. $maxtries = 10;
  293. $success = false;
  294. for ($i = 0; $i < $maxtries && $success == false; $i++) {
  295. // Generate a new random position
  296. $rndpos_arr = sloodle_random_position_in_zone($loginzonesize);
  297. // Make sure the Z parameter is even - round it if not
  298. if ($rndpos_arr['z'] % 2 != 0) $rndpos_arr['z'] += 1;
  299. $rndpos_str = sloodle_array_to_vector($rndpos_arr);
  300. // Is the position already taken?
  301. if (!sloodle_get_record('sloodle_loginzone_allocation', 'course', $this->get_course_id(), 'position', $rndpos_str)) {
  302. // Nobody has the position
  303. $success = true;
  304. }
  305. }
  306. // Did we succeed in generating it?
  307. if (!$success) return false;
  308. // Create a new one
  309. $alloc = new stdClass();
  310. $alloc->course = $this->get_course_id();
  311. $alloc->userid = $user->get_user_id();
  312. $alloc->position = $rndpos_str;
  313. $alloc->timecreated = time();
  314. // Attempt to insert it into the database
  315. if (!sloodle_insert_record('sloodle_loginzone_allocation', $alloc)) return false;
  316. return true;
  317. }
  318. /**
  319. * Gets the SLurl for the specified user's loginzone alloation
  320. * @param SloodleUser $user The user whose allocation is to be retrieved
  321. * @return string|bool The SLurl string if successful, or false if the user has no allocation or the loginzone does not exist
  322. */
  323. function get_loginzone_allocation($user)
  324. {
  325. // Make sure the necessary data is available
  326. if (!(isset($this->sloodle_course_data->loginzonepos) && isset($this->sloodle_course_data->loginzonesize) && isset($this->sloodle_course_data->loginzoneregion))) return false;
  327. // Attempt to fetch the data
  328. $alloc = sloodle_get_record('sloodle_loginzone_allocation', 'course', $this->get_course_id(), 'userid', $user->get_user_id());
  329. if (!$alloc) return false;
  330. $relpos = sloodle_vector_to_array($alloc->position);
  331. // Calculate the absolute position of the allocation
  332. $loginzonepos= sloodle_vector_to_array($this->sloodle_course_data->loginzonepos);
  333. $abspos = array('x'=>$loginzonepos['x'] + $relpos['x'], 'y'=>$loginzonepos['y'] + $relpos['y'], 'z'=>$loginzonepos['z'] + $relpos['z']);
  334. // Construct and return the SLurl
  335. return "secondlife://{$this->sloodle_course_data->loginzoneregion}/{$abspos['x']}/{$abspos['y']}/{$abspos['z']}";
  336. }
  337. /**
  338. * Finds the user identified by LoginZone allocation, and loads it into the given user object.
  339. * Note: does not delete the allocation.
  340. * @param string $pos Absolute position vector (relative to sim, not to LoginZone)
  341. * @param SloodleUser &$user The user object which will be manipulated (by reference)
  342. * @return bool True if successful, or false otherwise
  343. */
  344. function load_user_by_loginzone($pos, &$user)
  345. {
  346. // Calculate the relative position of the allocation
  347. $abspos = sloodle_vector_to_array($pos);
  348. $loginzonepos= sloodle_vector_to_array($this->sloodle_course_data->loginzonepos);
  349. $relpos = array('x'=>$abspos['x'] - $loginzonepos['x'], 'y'=>$abspos['y'] - $loginzonepos['y'], 'z'=>$abspos['z'] - $loginzonepos['z']);
  350. $relposstr = sloodle_array_to_vector($relpos);
  351. // Generate an alternate LoginZone position 1 unit downwards, in case the SL teleport glitch has happened.
  352. // (Note that LoginZone positions are generate on even Z units only, so an offset of 1 shouldn't cause a problem)
  353. $altrelpos = array('x'=>$relpos['x'], 'y'=>$relpos['y'], 'z'=>$relpos['z'] - 1);
  354. $altrelposstr = sloodle_array_to_vector($altrelpos);
  355. // Attempt to find a matching LoginZone position in the database
  356. $rec = sloodle_get_record('sloodle_loginzone_allocation', 'course', $this->get_course_id(), 'position', $relposstr);
  357. if (!$rec)
  358. {
  359. $rec = sloodle_get_record('sloodle_loginzone_allocation', 'course', $this->get_course_id(), 'position', $altrelposstr);
  360. }
  361. if (!$rec) return false;
  362. // Load the user
  363. return $user->load_user($rec->userid);
  364. }
  365. /**
  366. * Deletes any loginzone allocations for the given user
  367. * If there are multiple for the same user (which there should never be) it will delete them all.
  368. * @param SloodleUser $user The user whose allocation is to be deleted
  369. * @return void
  370. */
  371. function delete_loginzone_allocation($user)
  372. {
  373. sloodle_delete_records('sloodle_loginzone_allocation', 'userid', $user->get_user_id());
  374. }
  375. /**
  376. * Determines whether or not LoginZone data exists for this course.
  377. * @return bool True if there is complete data, or false otherwise
  378. */
  379. function has_loginzone_data()
  380. {
  381. return (!(empty($this->sloodle_course_data->loginzonepos) || empty($this->sloodle_course_data->loginzonesize) || empty($this->sloodle_course_data->loginzoneregion)));
  382. }
  383. /**
  384. * Reads fresh data into the structure from the database.
  385. * Fetches Moodle and Sloodle data about the course specified.
  386. * If necessary, it creates a new Sloodle entry with default settings.
  387. * Returns true if successful, or false on failure.
  388. * @param mixed $course Either a unique course ID, or a course data object. If the former, then VLE course data is read from the database. Otherwise, the data object is used as-is.
  389. * @return bool
  390. */
  391. function load($course)
  392. {
  393. // Reset everything
  394. $this->course_object = null;
  395. $this->sloodle_course_data = null;
  396. // Check what we are dealing with
  397. if (is_int($course)) {
  398. // It is a course ID - make sure it's valid
  399. if ($course <= 0) return false;
  400. // Load the course data
  401. $this->course_object = sloodle_get_record('course', 'id', $course);
  402. if (!$this->course_object) {
  403. $this->course_object = null;
  404. return false;
  405. }
  406. } else if (is_object($course)) {
  407. // It is an object - make sure it has an ID
  408. if (!isset($course->id)) return false;
  409. $this->course_object = $course;
  410. } else {
  411. // Don't know what it is - do nothing
  412. return false;
  413. }
  414. // Fetch the Sloodle course data
  415. $this->sloodle_course_data = sloodle_get_record('sloodle_course', 'course', $this->course_object->id);
  416. // Did it fail?
  417. if (!$this->sloodle_course_data) {
  418. // Create the new entry
  419. $this->sloodle_course_data = new stdClass();
  420. $this->sloodle_course_data->course = $this->course_object->id;
  421. $this->sloodle_course_data->autoreg = 0;
  422. $this->sloodle_course_data->loginzonepos = '';
  423. $this->sloodle_course_data->loginzonesize = '';
  424. $this->sloodle_course_data->loginzoneregion = '';
  425. $this->sloodle_course_data->id = sloodle_insert_record('sloodle_course', $this->sloodle_course_data);
  426. // Did something go wrong?
  427. if (!$this->sloodle_course_data->id) {
  428. $this->course_object = null;
  429. $this->sloodle_course_data = null;
  430. return false;
  431. }
  432. }
  433. return true;
  434. }
  435. /**
  436. * Loads course and controller data by the unqiue site-wide identifier of a Sloodle controller.
  437. * @param mixed $controllerid The unique site-wide identifier for a Sloodle Controller. (For Moodle, an integer cmi)
  438. * @return bool True if successful, or false on failure.
  439. */
  440. function load_by_controller($controllerid)
  441. {
  442. // Clear out all our data
  443. $this->course_object = null;
  444. $this->sloodle_course_data = null;
  445. // Construct a new controller object, and attempt to load its data
  446. $this->controller = new SloodleController();
  447. if (!$this->controller->load($controllerid)) {
  448. sloodle_debug("Failed to load controller.<br>");
  449. return false;
  450. }
  451. // Now attempt to load all the course data
  452. if (!$this->load($this->controller->get_course_id())) {
  453. sloodle_debug("Failed to load course data.<br>");
  454. return false;
  455. }
  456. return true;
  457. }
  458. /**
  459. * Writes current Sloodle course data back to the database.
  460. * Requires that a course structure has already been retrieved.
  461. * @return bool True if successful, or false on failure
  462. */
  463. function write()
  464. {
  465. // Make sure the course data is valid
  466. if (empty($this->course_object) || $this->course_object->id <= 0) return false;
  467. if (empty($this->sloodle_course_data) || $this->sloodle_course_data->id <= 0) return false;
  468. // Update the Sloodle data
  469. return sloodle_update_record('sloodle_course', $this->sloodle_course_data);
  470. }
  471. /**
  472. * Gets an array associating layout ID's to names
  473. * @return array
  474. */
  475. function get_layout_names()
  476. {
  477. // Fetch the layout records
  478. $layouts = sloodle_get_records('sloodle_layout', 'course', $this->course_object->id, 'name');
  479. if (!$layouts) return array();
  480. // Construct the array of names
  481. $layout_names = array();
  482. foreach ($layouts as $l) {
  483. $layout_names[$l->id] = $l->name;
  484. }
  485. return $layout_names;
  486. }
  487. /**
  488. * Gets all the entries in the named layout.
  489. * @param string $name The name of the layout to query
  490. * @return array|bool A numeric array of {@link SloodleLayoutEntry} objects if successful, or false if the layout does not exist
  491. */
  492. function get_layout_entries($name)
  493. {
  494. // Attempt to find the relevant layout
  495. $layout = sloodle_get_record('sloodle_layout', 'course', $this->course_object->id, 'name', $name);
  496. if (!$layout) return false;
  497. return $this->get_layout_entries_for_layout_id($layout->id);
  498. }
  499. /**
  500. * Gets all the entries in the named layout.
  501. * @param string $name The name of the layout to query
  502. * @return array|bool A numeric array of {@link SloodleLayoutEntry} objects if successful, or false if the layout does not exist
  503. */
  504. function get_layout_entries_for_layout_id($id)
  505. {
  506. // Fetch all entries
  507. $recs = sloodle_get_records('sloodle_layout_entry', 'layout', $id);
  508. if (!$recs) return array();
  509. // Construct the array of SloodleLayoutEntry objects
  510. $entries = array();
  511. foreach ($recs as $r) {
  512. $entry = new SloodleLayoutEntry($r);
  513. $entries[] = $entry;
  514. }
  515. return $entries;
  516. }
  517. /**
  518. * Gets SloodleLayout objects for all the layouts in the course.
  519. */
  520. function get_layouts($controllerid) {
  521. // Fetch the layout records
  522. $layoutrecs = sloodle_get_records('sloodle_layout', 'course', $this->course_object->id, 'name');
  523. $layouts = array();
  524. if (!$layoutrecs) return array();
  525. foreach ($layoutrecs as $r) {
  526. if ($r->controllerid != $controllerid) {
  527. continue;
  528. }
  529. $layouts[] = new SloodleLayout($r);
  530. }
  531. return $layouts;
  532. }
  533. /**
  534. * Deletes the named layout.
  535. * @param string $name The name of the layout to delete
  536. * @return void
  537. */
  538. function delete_layout($name)
  539. {
  540. // Attempt to find the relevant layout
  541. $layout = sloodle_get_record('sloodle_layout', 'course', $this->course_object->id, 'name', $name);
  542. if (!$layout) return;
  543. // Delete all related entries
  544. sloodle_delete_records('sloodle_layout_entry', 'layout', $layout->id);
  545. // Delete the layout itself
  546. sloodle_delete_records('sloodle_layout', 'course', $this->course_object->id, 'name', $name);
  547. }
  548. /**
  549. * Save the given entries in the named profile.
  550. * @param string $name The name of the layout to query
  551. * @param array $entries A numeric array of {@link SloodleLayoutEntry} objects to store
  552. * @param bool $add (Default: false) If true, then the entries will be added to the layout instead of replacing existing entries
  553. * @return bool True if successful, or false otherwise
  554. */
  555. function save_layout($name, $entries, $add = false)
  556. {
  557. // Attempt to find the relevant layout
  558. $layout = sloodle_get_record('sloodle_layout', 'course', $this->course_object->id, 'name', $name);
  559. $lid = 0;
  560. if ($layout) {
  561. $lid = $layout->id;
  562. }
  563. return $this->save_layout_by_id($lid, $name, $entries, $add);
  564. }
  565. /**
  566. * Save the given entries in the profile specified by id.
  567. * @param string $id The id of the layout to query. 0 to add a new layout.
  568. * @param string $name The new name of the layout.
  569. * @param array $entries A numeric array of {@link SloodleLayoutEntry} objects to store
  570. * @param bool $add (Default: false) If true, then the entries will be added to the layout instead of replacing existing entries
  571. * @return bool True if successful, or false otherwise
  572. */
  573. function save_layout_by_id($id, $name, $entries, $add = false)
  574. {
  575. // Attempt to find the relevant layout
  576. if ($id > 0) {
  577. /*
  578. // Delete all existing entries if necessary
  579. // This will happen when we save
  580. // TODO: make add-only functionality for backwards compatibility
  581. if (!$add) {
  582. sloodle_delete_records('sloodle_layout_entry', 'layout', $layout->id);
  583. }
  584. */
  585. $layout = $this->get_layout($id);
  586. $layout->name = $name;
  587. $layout->timeupdated = time();
  588. $layout->entries = $entries;
  589. $layout->populate_entries_from_active_objects(); // where the records have objectuuids set, copy their settings
  590. if (!$layout->update()) {
  591. return false;
  592. }
  593. $this->layout = $layout;
  594. } else {
  595. $layout = new SloodleLayout();
  596. $layout->name = $name;
  597. $layout->course = $this->course_object->id;
  598. $layout->timeupdated = time();
  599. $layout->entries = $entries;
  600. $layout->populate_entries_from_active_objects();
  601. $layout->id = $layout->insert(); #sloodle_insert_record('sloodle_layout', $layout);
  602. if (!$layout->id) return false;
  603. $this->layout = $layout;
  604. }
  605. /*
  606. // This should have been done by the layout
  607. // Insert each new entry
  608. $success = true;
  609. foreach ($entries as $e) {
  610. $rec = new stdClass();
  611. $rec->layout = $layout->id;
  612. $rec->name = $e->name;
  613. $rec->position = $e->position;
  614. $rec->rotation = $e->rotation;
  615. // TODO EDE: If there's an objectuuid for the entry, copy the entries from the active object table to the layout config table
  616. if ($objectuuid != '') {
  617. $rec->copy_active_object_with_uuid($e->objectuuid);
  618. }
  619. $entry_id = sloodle_insert_record('sloodle_layout_entry', $rec);
  620. }
  621. */
  622. return $layout->id;
  623. }
  624. function get_enrolled_users(){
  625. global $CFG;
  626. $contextid = get_context_instance(CONTEXT_COURSE,$this->course_object->id);
  627. $cid=$contextid->id;
  628. $sql= "SELECT distinct sl.*,sl.uuid as avuuid,u.id, u.firstname, u.lastname FROM {$CFG->prefix}user u INNER JOIN {$CFG->prefix}sloodle_users sl ON sl.userid=u.id INNER JOIN {$CFG->prefix}role_assignments ra ON ra.userid=u.id AND ra.contextid=?";
  629. $enrolledUsers = sloodle_get_records_sql_params($sql, array($cid));
  630. //return $enrolledUsers
  631. return $enrolledUsers;
  632. }
  633. function get_layout($layoutid) {
  634. $rec = sloodle_get_record('sloodle_layout', 'course', $this->course_object->id, 'id', $layoutid);
  635. return new SloodleLayout($rec);
  636. }
  637. // Make sure the course has at least one layout, creating one if necessary using the prefix $nameprefix
  638. // Returns true on success, false on failure
  639. function ensure_at_least_one_layout($nameprefix, $controllerid) {
  640. if ( count($this->get_layouts($controllerid)) > 0 ) {
  641. return true;
  642. }
  643. if (!$layoutname = SloodleLayout::UniqueName($nameprefix)) {
  644. return false;
  645. }
  646. $l = new SloodleLayout();
  647. $l->name = $layoutname;
  648. $l->controllerid = $controllerid;
  649. $l->course = $this->course_object->id;
  650. return $l->insert();
  651. }
  652. /**
  653. * Checks whether or not the CURRENTLY LOGGED-IN user can authorise objects on this course.
  654. * @return bool True if the user has object authorisation permission, or false otherwise.
  655. */
  656. function can_user_authorise_objects()
  657. {
  658. global $USER;
  659. // Make sure some user data
  660. if (empty($USER) || $USER->id == 0) return FALSE;
  661. // Check the capability
  662. return has_capability('mod/sloodle:objectauth', get_context_instance(CONTEXT_COURSE, $this->get_course_id()));
  663. }
  664. }
  665. ?>