Source for file lib.php
Documentation is available at lib.php
* Sloodle module core library functionality.
* This script is required by Moodle to contain certain key functionality for the module.
* @copyright Copyright (c) 2007 Sloodle (various contributors)
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
* @contributor Peter R. Bloomfield
require_once($CFG->dirroot.
'/mod/sloodle/sl_config.php');
require_once(SLOODLE_LIBROOT.
'/general.php');
* Processes the module configuration options prior to saving them.
* Only used when the "config.html" file is used for module configuration.
* Moodle 1.9 now prefers the "settings.php" file.
* @param object &$config Reference to an object containing the configuration settings (can be edited)
* Given an object containing all the necessary data,
* (defined by the form in mod.html) this function
* will create a new instance and return the id number
* @param object $instance An object from the form in mod.html
* @return int The id of the newly inserted sloodle record
// Set the creation and modification times
$sloodle->timecreated =
time();
$sloodle->timemodified =
time();
// Prefix the full type name on the instance name
$sloodle->name =
get_string("moduletype:{$sloodle->type}", 'sloodle') .
': ' .
$sloodle->name;
// Attempt to insert the new Sloodle record
if (!$sloodle->id =
insert_record('sloodle', $sloodle)) {
error(get_string('failedaddinstance', 'sloodle'));
// We need to create a new secondary table for this module
$sec_table =
new stdClass();
$sec_table->sloodleid =
$sloodle->id;
// Check the type of this module
switch ($sloodle->type) {
// Create a new controller record
if (isset
($sloodle->controller_enabled) &&
$sloodle->controller_enabled) $sec_table->enabled =
1;
else $sec_table->enabled =
0;
$sec_table->password =
$sloodle->controller_password;
// Attempt to add it to the database
if (!insert_record('sloodle_controller', $sec_table)) {
$errormsg =
get_string('failedaddsecondarytable', 'sloodle');
// Add in a default blank channel number and update time
$sec_table->channel =
'';
$sec_table->timeupdated =
0;
// Attempt to add it to the database
if (!insert_record('sloodle_distributor', $sec_table)) {
$errormsg =
get_string('failedaddsecondarytable', 'sloodle');
// ADD FURTHER MODULE TYPES HERE!
$errormsg =
error(get_string('moduletypeunknown', 'sloodle'));
// Delete the Sloodle instance
delete_records('sloodle', 'id', $sloodle->id);
// Show the error message (if there was one)
if (!empty($errormsg)) error($errormsg);
* Given an object containing all the necessary data,
* (defined by the form in mod.html) this function
* will update an existing instance with new data.
* @param object $instance An object from the form in mod.html
* @return boolean Success/Fail
// Update the modification time
$sloodle->timemodified =
time();
// Make sure the ID is correct for update
$sloodle->id =
$sloodle->instance;
// Make sure the type is the same as the existing record
$existing_record =
get_record('sloodle', 'id', $sloodle->id);
if (!$existing_record) error(get_string('modulenotfound', 'sloodle'));
if ($existing_record->type !=
$sloodle->type) error(get_string('moduletypemismatch', 'sloodle'));
// Check the type of this module
switch ($sloodle->type) {
// Attempt to fetch the controller record
$ctrl =
get_record('sloodle_controller', 'sloodleid', $sloodle->id);
if (!$ctrl) error(get_string('secondarytablenotfound', 'sloodle'));
// Add the updated 'enabled' value
if (isset
($sloodle->controller_enabled) &&
$sloodle->controller_enabled) $ctrl->enabled =
1;
// Add the updated password
$ctrl->password =
$sloodle->controller_password;
update_record('sloodle_controller', $ctrl);
// Attempt to fetch the distributor record
$distrib =
get_record('sloodle_distributor', 'sloodleid', $sloodle->id);
if (!$distrib) error(get_string('secondarytablenotfound', 'sloodle'));
// Has a reset been requested?
if ($sloodle->distributor_reset) {
// Yes - clear the distributor channel
// Delete all objects associated with the Distributor
delete_records('sloodle_distributor_entry', 'distributorid', $distrib->id);
update_record('sloodle_distributor', $distrib);
// ADD FURTHER MODULE TYPES HERE!
error(get_string('moduletypeunknown', 'sloodle'));
return update_record('sloodle', $sloodle);
* Given an ID of an instance of this module,
* this function will permanently delete the instance
* and any data that depends on it.
* @param int $id Id of the module instance
* @return boolean Success/Failure
// Determine our success or otherwise
// Attempt to delete the main Sloodle instance
if (!delete_records('sloodle', 'id', $id)) {
// Delete any secondary controller tables
delete_records('sloodle_controller', 'sloodleid', $id);
// Attempt to get any secondary distributor tables
$distribs =
get_records('sloodle_distributor', 'sloodleid', $id);
// Go through each distributor
foreach ($distribs as $d) {
// Delete any related distributor entries
delete_records('sloodle_distributor_entry', 'distributorid', $d->id);
// Delete all the distributors
delete_records('sloodle_distributor', 'sloodleid', $id);
// ADD FURTHER MODULE TYPES HERE!
* Return a small object with summary information about what a
* user has done with a given particular instance of this module
* Used for user activity reports.
* $return->time = the time they did it
* $return->info = a short text description
* @todo Do we need this function to do anything?
* Print a detailed representation of what a user has done with
* a given particular instance of this module, for user activity reports.
* @todo Do we need this function to do anything?
* Given a course and a time, this module should find recent activity
* that has occurred in sloodle activities and print it out.
* Return true if there was output, or false is there was none.
* @todo Figure out if we need this function to do anything!
return false; // True if anything was printed, otherwise false
* Function to be run periodically according to the Moodle cron.
* Clears out expired pending user entries, session keys, etc.
* @todo Delete expired user entries
* @todo Use a custom delay for expiries of users/objects
echo
"\nProcessing Sloodle cron tasks:\n";
// Delete any pending user entries which have expired (more than 30 minutes old)
$expirytime =
time() -
1800;
echo
"Removing expired pending avatar entries...\n";
delete_records_select('sloodle_pending_avatars', "timeupdated < $expirytime");
// Delete any LoginZone allocations which have expired (more than 15 minutes old)
$expirytime =
time() -
900;
echo
"Removing expired LoginZone allocations...\n";
delete_records_select('sloodle_loginzone_allocation', "timecreated < $expirytime");
// Fetch our configuration settings, if they exist
$active_object_days =
7; // Give active objects a week by default
if (!empty($CFG->sloodle_active_object_lifetime)) $active_object_days =
$CFG->sloodle_active_object_lifetime;
$user_object_days =
21; // Give user objects 3 weeks by default
if (!empty($CFG->sloodle_user_object_lifetime)) $user_object_days =
$CFG->sloodle_user_object_lifetime;
// Delete any active objects and session keys which have expired
// Deletes any authorised objects which have not checked-in for more than X days (where X is specified in module config)
// Deletes any unauthorised objects which have not checked-in for more than 1 hour
$expirytime_auth =
time() -
(86400 *
$active_object_days);
$expirytime_unauth =
time() -
3600;
echo
"Searching for expired active objects...\n";
$recs =
get_records_select('sloodle_active_object', "(((controllerid = 0 AND userid = 0) OR type = '') AND timeupdated < $expirytime_unauth) OR timeupdated < $expirytime_auth");
// Go through each object
echo
"Removing " .
count($recs) .
" expired active objects and associated configuration settings...\n";
delete_records('sloodle_object_config', 'object', $r->id);
delete_records('sloodle_active_object', 'id', $r->id);
// Delete any user-authorised objects which have expired
// (will eventually use custom expiry times, chosen in the configuration)
// Deletes any authorised objects which have not checked-in for more than X days (where X is specified in module config)
// Deletes any unauthorised objects which have not checked-in for more than 1 hour
$expirytime_auth =
time() -
(86400 *
$user_object_days);
$expirytime_unauth =
time() -
3600;
echo
"Deleting expired user objects...\n";
delete_records_select('sloodle_user_object', "(authorised = 0 AND timeupdated < $expirytime_unauth) OR timeupdated < $expirytime_auth");
// Email login details to auto-registered avatars in-world
echo
"Sending out login notifications...\n";
echo
"Done processing Sloodle cron tasks.\n\n";
* Must return an array of grades for a given instance of this module,
* indexed by user. It also returns a maximum allowed grade.
* $return->grades = array of grades;
* $return->maxgrade = maximum allowed grade;
* @param int $sloodleid ID of an instance of this module
* @return mixed Null or object with an array of grades and with the maximum grade
* Must return an array of user records (all data) who are participants
* for a given instance of sloodle. Must include every user involved
* in the instance, independient of his role (student, teacher, admin...)
* See other modules as example.
* @param int $sloodleid ID of an instance of this module
* @return mixed boolean/array of students
* @todo Possibly make it return a list of users registered via the Sloodle instance?
* This function returns if a scale is being used by one sloodle
* it it has support for grading and scales. Commented code should be
* modified if necessary. See forum, glossary or journal modules
* @param int $sloodleid ID of an instance of this module
* Gets the different sub-types of Sloodle module available as a list for the "Add Activity..." menu.
* $return array Entries for the "Add Activity..." sub-menu for Sloodle
global $CFG, $SLOODLE_TYPES;
$type->modclass =
MOD_CLASS_ACTIVITY;
$type->type =
"sloodle_group_start";
$type->typestr =
'--'.
get_string('modulenameplural', 'sloodle');
// Go through each Sloodle module type, and add it
foreach ($SLOODLE_TYPES as $st) {
$type->modclass =
MOD_CLASS_ACTIVITY;
$type->type =
"sloodle&type=$st";
$type->typestr =
get_string("moduletype:$st", 'sloodle');
$type->modclass =
MOD_CLASS_ACTIVITY;
$type->type =
"sloodle_group_end";
Documentation generated on Mon, 07 Jul 2008 12:32:36 +0100 by phpDocumentor 1.4.0