Source for file course.php
Documentation is available at course.php
// This file is part of the Sloodle project (www.sloodle.org)
* This file defines a structure for Sloodle data about a particular Moodle course.
* @copyright Copyright (c) 2008 Sloodle (various contributors)
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
* @contributor Peter R. Bloomfield
/** Include the general Sloodle library. */
require_once(SLOODLE_LIBROOT.
'/general.php');
/** Include the Sloodle controller structure. */
require_once(SLOODLE_LIBROOT.
'/controller.php');
/** Include the layout profile management stuff. */
require_once(SLOODLE_LIBROOT.
'/layout_profile.php');
* The Sloodle course data class
* The database object of the course to which this object relates.
* Corresponds to the "course" table in Moodle.
* The Sloodle course data object, if it exists.
* Is null if not yet set.
* The {@link SloodleController} object being used to access this course, if available.
* Determines whether or not course data has been loaded.
* Gets the identifier of the course in the VLE.
* @return mixed Course identifier. Type depends on VLE. (In Moodle, it will be an integer).
* Gets the VLE course object.
* WARNING: this should only be used when ABSOLUTELY necessary.
* The contents are specific to the VLE.
* @return mixed Type and content depends upon VLE. In Moodle, it is an object representing a record from the 'course' table.
* Gets the short name of this course in the VLE.
* @return string Shortname of this course.
* Gets the full name of this course in the VLE.
* @return string Fullname of this course.
* Is auto registration permitted on this site AND course?
* Takes into account the site-wide setting as well.
// Check the site *and* the course value
* Gets the autoregistration value for this course only.
* (Ignores the site setting).
* Enables auto-registration for this course.
* NOTE: it may still be disabled at site-level.
* Disables auto-registration for this course.
* NOTE: does not affect the site setting.
* Is auto enrolment permitted on this site AND course?
* Takes into account the site-wide setting as well.
// Check the site *and* the course value
* Gets the auto enrolment value for this course only.
* (Ignores the site setting).
* Enables auto-enrolment for this course.
* NOTE: it may still be disabled at site-level.
* Disables auto-enrolment for this course.
* NOTE: does not affect the site setting.
* Determines whether or not the course is available.
* Checks that the course has not been disabled or hidden etc..
* @return bool True if the course is available
* Gets the position of the loginzone as a string vector <x,y,z>
* Sets the position of the loginzone as a string vector <x,y,z>
* @param string $pos A string position vector <x,y,z>
* @todo Update to handle arrays as well
* Sets the position of the loginzone as a set of components
* Gets the size of the loginzone as a string vector <x,y,z>
* Sets the size of the loginzone as a string vector <x,y,z>
* @param string $size A string size vector <x,y,z>
* @todo Update to handle arrays as well
* Sets the size of the loginzone as a set of components
* Gets the region of the loginzone
* Sets the region of the loginzone
* @param string $region A string naming a region
* Gets the timestamp of the last time the loginzone was updated
* Sets the timestamp of the last time the loginzone was updated
* @param int $timestamp A unix timestamp. If null, the current timestamp is used
if ($timestamp ==
null) $timestamp =
time();
* Generates a new LoginZone allocation for the specified user.
* @param SloodleUser $user The user for whom this allocation should be made
* @return string|falseA SLurl for the allocation, or false if it was unsuccessful
// Make sure the necessary data is available
// Delete any existing LoginZone allocation for this user
delete_records('sloodle_loginzone_allocation', 'userid', $user->get_user_id());
// We will try up to 10 times to find a new available position
for ($i =
0; $i <
$maxtries &&
$success ==
false; $i++
) {
// Generate a new random position
// Is the position already taken?
if (!get_record('sloodle_loginzone_allocation', 'course', $this->get_course_id(), 'position', $rndpos_str)) {
// Nobody has the position
// Did we succeed in generating it?
if (!$success) return false;
$alloc->userid =
$user->get_user_id();
$alloc->position =
$rndpos_str;
$alloc->timecreated =
time();
// Attempt to insert it into the database
if (!insert_record('sloodle_loginzone_allocation', $alloc)) return false;
* Gets the SLurl for the specified user's loginzone alloation
* @param SloodleUser $user The user whose allocation is to be retrieved
* @return string|boolThe SLurl string if successful, or false if the user has no allocation or the loginzone does not exist
// Make sure the necessary data is available
// Attempt to fetch the data
$alloc =
get_record('sloodle_loginzone_allocation', 'course', $this->get_course_id(), 'userid', $user->get_user_id());
if (!$alloc) return false;
// Calculate the absolute position of the allocation
$abspos =
array('x'=>
$loginzonepos['x'] +
$relpos['x'], 'y'=>
$loginzonepos['y'] +
$relpos['y'], 'z'=>
$loginzonepos['z'] +
$relpos['z']);
// Construct and return the SLurl
return "secondlife://{$this->sloodle_course_data->loginzoneregion}/{
$abspos['x']}/{
$abspos['y']}/{
$abspos['z']}";
* Finds the user identified by LoginZone allocation, and loads it into the given user object.
* Note: does not delete the allocation.
* @param string $pos Absolute position vector (relative to sim, not to LoginZone)
* @param SloodleUser &$user The user object which will be manipulated (by reference)
* @return bool True if successful, or false otherwise
// Calculate the relative position of the allocation
$relpos =
array('x'=>
$abspos['x'] -
$loginzonepos['x'], 'y'=>
$abspos['y'] -
$loginzonepos['y'], 'z'=>
$abspos['z'] -
$loginzonepos['z']);
// Attempt to find a matching LoginZone position in the database
$rec =
get_record('sloodle_loginzone_allocation', 'course', $this->get_course_id(), 'position', $relpos);
return $user->load_user($rec->userid);
* Deletes any loginzone allocations for the given user
* If there are multiple for the same user (which there should never be) it will delete them all.
* @param SloodleUser $user The user whose allocation is to be deleted
delete_records('sloodle_loginzone_allocation', 'userid', $user->get_user_id());
* Determines whether or not LoginZone data exists for this course.
* @return bool True if there is complete data, or false otherwise
* Reads fresh data into the structure from the database.
* Fetches Moodle and Sloodle data about the course specified.
* If necessary, it creates a new Sloodle entry with default settings.
* Returns true if successful, or false on failure.
* @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.
// Check what we are dealing with
// It is a course ID - make sure it's valid
if ($course <=
0) return false;
// It is an object - make sure it has an ID
if (!isset
($course->id)) return false;
// Don't know what it is - do nothing
// Fetch the Sloodle course data
// Did something go wrong?
* Loads course and controller data by the unqiue site-wide identifier of a Sloodle controller.
* @param mixed $controllerid The unique site-wide identifier for a Sloodle Controller. (For Moodle, an integer cmi)
* @return bool True if successful, or false on failure.
// Clear out all our data
// Construct a new controller object, and attempt to load its data
// Now attempt to load all the course data
* Writes current Sloodle course data back to the database.
* Requires that a course structure has already been retrieved.
* @return bool True if successful, or false on failure
// Make sure the course data is valid
// Update the Sloodle data
* Gets an array associating layout ID's to names
// Fetch the layout records
$layouts =
get_records('sloodle_layout', 'course', $this->course_object->id, 'name');
if (!$layouts) return array();
// Construct the array of names
foreach ($layouts as $l) {
$layout_names[$l->id] =
$l->name;
* Gets all the entries in the named layout.
* @param string $name The name of the layout to query
* @return array|boolA numeric array of {@link SloodleLayoutEntry} objects if successful, or false if the layout does not exist
// Attempt to find the relevant layout
$layout =
get_record('sloodle_layout', 'course', $this->course_object->id, 'name', $name);
if (!$layout) return false;
$recs =
get_records('sloodle_layout_entry', 'layout', $layout->id);
if (!$recs) return array();
// Construct the array of SloodleLayoutEntry objects
$entry->position =
$r->position;
$entry->rotation =
$r->rotation;
* Deletes the named layout.
* @param string $name The name of the layout to delete
// Attempt to find the relevant layout
$layout =
get_record('sloodle_layout', 'course', $this->course_object->id, 'name', $name);
// Delete all related entries
delete_records('sloodle_layout_entry', 'layout', $layout->id);
// Delete the layout itself
delete_records('sloodle_layout', 'course', $this->course_object->id, 'name', $name);
* Save the given entries in the named profile.
* @param string $name The name of the layout to query
* @param array $entries A numeric array of {@link SloodleLayoutEntry} objects to store
* @param bool $add (Default: false) If true, then the entries will be added to the layout instead of replacing existing entries
* @return bool True if successful, or false otherwise
// Attempt to find the relevant layout
$layout =
get_record('sloodle_layout', 'course', $this->course_object->id, 'name', $name);
// Does not exist - create it
$layout =
new stdClass();
$layout->timeupdated =
time();
$layout->id =
insert_record('sloodle_layout', $layout);
if (!$layout->id) return false;
// Change the time updated
set_field('sloodle_layout', 'timeupdated', time(), 'course', $this->course_object->id, 'name', $name);
// Delete all existing entries if necessary
if (!$add) delete_records('sloodle_layout_entry', 'layout', $layout->id);
foreach ($entries as $e) {
$rec->layout =
$layout->id;
$rec->position =
$e->position;
$rec->rotation =
$e->rotation;
if (!insert_record('sloodle_layout_entry', $rec)) $success =
false;
* Checks whether or not the CURRENTLY LOGGED-IN user can authorise objects on this course.
* @return bool True if the user has object authorisation permission, or false otherwise.
// Make sure some user data
if (empty($USER) ||
$USER->id ==
0) return FALSE;
return has_capability('mod/sloodle:objectauth', get_context_instance(CONTEXT_COURSE, $this->get_course_id()));
Documentation generated on Mon, 07 Jul 2008 12:32:22 +0100 by phpDocumentor 1.4.0