Source for file lib.php

Documentation is available at lib.php

  1. <?php                          
  2.  
  3.     /**
  4.     * Sloodle module core library functionality.
  5.     *
  6.     * This script is required by Moodle to contain certain key functionality for the module.
  7.     *
  8.     * @package sloodle
  9.     * @copyright Copyright (c) 2007 Sloodle (various contributors)
  10.     * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  11.     *
  12.     * @contributor Peter R. Bloomfield
  13.     *
  14.     */                   
  15.     
  16.     require_once($CFG->dirroot.'/mod/sloodle/sl_config.php');
  17.     require_once(SLOODLE_LIBROOT.'/general.php');
  18.     
  19.  
  20.     /**
  21.     * Processes the module configuration options prior to saving them.
  22.     * Only used when the "config.html" file is used for module configuration.
  23.     * Moodle 1.9 now prefers the "settings.php" file.
  24.     *
  25.     * @param object &$config Reference to an object containing the configuration settings (can be edited)
  26.     * @return void 
  27.     */
  28.     function sloodle_process_options(&$config)
  29.     {
  30.     }
  31.     
  32.  
  33.     /**
  34.      * Given an object containing all the necessary data,
  35.      * (defined by the form in mod.html) this function
  36.      * will create a new instance and return the id number
  37.      * of the new instance.
  38.      *
  39.      * @param object $instance An object from the form in mod.html
  40.      * @return int The id of the newly inserted sloodle record
  41.      ***/
  42.     function sloodle_add_instance($sloodle{
  43.         global $CFG;
  44.  
  45.         // Set the creation and modification times
  46.         $sloodle->timecreated time();
  47.         $sloodle->timemodified time();
  48.         
  49.         // Prefix the full type name on the instance name
  50.         $sloodle->name get_string("moduletype:{$sloodle->type}"'sloodle'': ' $sloodle->name;
  51.             
  52.         // Attempt to insert the new Sloodle record
  53.         if (!$sloodle->id insert_record('sloodle'$sloodle)) {
  54.             error(get_string('failedaddinstance''sloodle'));
  55.         }
  56.         
  57.         // We need to create a new secondary table for this module
  58.         $sec_table new stdClass();
  59.         $sec_table->sloodleid $sloodle->id;
  60.         
  61.         // Check the type of this module
  62.         $result FALSE;
  63.         $errormsg '';
  64.         switch ($sloodle->type{
  65.         case SLOODLE_TYPE_CTRL:
  66.             // Create a new controller record
  67.             if (isset($sloodle->controller_enabled&& $sloodle->controller_enabled$sec_table->enabled 1;
  68.             else $sec_table->enabled 0;
  69.             $sec_table->password $sloodle->controller_password;
  70.             
  71.             // Attempt to add it to the database
  72.             if (!insert_record('sloodle_controller'$sec_table)) {
  73.                 $errormsg get_string('failedaddsecondarytable''sloodle');
  74.             else {
  75.                 $result TRUE;
  76.             }
  77.             break;
  78.  
  79.         case SLOODLE_TYPE_IBANK:
  80.             // Create the secondary table for this ibank
  81.             $sec_table->amount = (int)$sloodle->stipendgiver_amount;
  82.             $sec_table->icurrency = (string)$sloodle->icurrency;            
  83.             if (!insert_record('sloodle_stipendgiver'$sec_table)) {
  84.                 $errormsg get_string('failedaddsecondarytable''sloodle');
  85.             else {
  86.                 $result TRUE;
  87.             }
  88.  
  89.             break;
  90.                 
  91.         case SLOODLE_TYPE_DISTRIB:
  92.             // Add in a default blank channel number and update time
  93.             $sec_table->channel '';
  94.             $sec_table->timeupdated 0;
  95.         
  96.             // Attempt to add it to the database
  97.             if (!insert_record('sloodle_distributor'$sec_table)) {
  98.                 $errormsg get_string('failedaddsecondarytable''sloodle');
  99.             else {
  100.                 $result TRUE;
  101.             }        
  102.             break;
  103.             
  104.         case SLOODLE_TYPE_PRESENTER:
  105.             // Add in the dimensions of the frame
  106.             $sec_table->framewidth = (int)$sloodle->presenter_framewidth;
  107.             $sec_table->frameheight = (int)$sloodle->presenter_frameheight;
  108.             // Attempt to add it to the database
  109.             if (!insert_record('sloodle_presenter'$sec_table)) {
  110.                 $errormsg get_string('failedaddsecondarytable''sloodle');
  111.             else {
  112.                 $result TRUE;
  113.             }
  114.             break;
  115.  
  116.         case SLOODLE_TYPE_MAP:
  117.             // Create a new map record from the form data
  118. /*            $sec_table->initialx = 1000.0;
  119.             $sec_table->initialy = 1000.0;
  120.             $sec_table->initialzoom = 2;
  121.             $sec_table->showpan = 1;
  122.             $sec_table->allowdrag = 1;
  123.             $sec_table->showzoom = 1;*/
  124.             
  125.             $sec_table->initialx = (float)$sloodle->map_initialx;
  126.             $sec_table->initialy = (float)$sloodle->map_initialy;
  127.             $sec_table->initialzoom = (int)$sloodle->map_initialzoom;
  128.             if (empty($sloodle->map_showpan)) $sec_table->showpan 0else $sec_table->showpan 1;
  129.             if (empty($sloodle->map_allowdrag)) $sec_table->allowdrag 0else $sec_table->allowdrag 1;
  130.             if (empty($sloodle->map_showzoom)) $sec_table->showzoom 0else $sec_table->showzoom 1;
  131.             
  132.             // Add it to the database
  133.             if (!insert_record('sloodle_map'$sec_table)) {
  134.                 $errormsg get_string('failedaddsecondarytable''sloodle');
  135.             else {
  136.                 $result TRUE;
  137.             }
  138.             break;
  139.             
  140.         // ADD FURTHER MODULE TYPES HERE!
  141.             
  142.         default:
  143.             // Type not recognised - this really shouldn't kill the script though... makes development very hard
  144.             //$errormsg = error(get_string('moduletypeunknown', 'sloodle'));
  145.             $result TRUE;
  146.             break;
  147.         }
  148.         
  149.         // Was there a problem?
  150.         if (!$result{
  151.             // Yes
  152.             // Delete the Sloodle instance
  153.             delete_records('sloodle''id'$sloodle->id);
  154.             
  155.             // Show the error message (if there was one)
  156.             if (!empty($errormsg)) error($errormsg);
  157.             return FALSE;
  158.         }
  159.         
  160.         // Success!
  161.         return $sloodle->id;
  162.     }
  163.  
  164.     /**
  165.      * Given an object containing all the necessary data,
  166.      * (defined by the form in mod.html) this function
  167.      * will update an existing instance with new data.
  168.      *
  169.      * @param object $instance An object from the form in mod.html
  170.      * @return boolean Success/Fail
  171.      ***/
  172.     function sloodle_update_instance($sloodle{
  173.         global $CFG;
  174.  
  175.         // Update the modification time
  176.         $sloodle->timemodified time();
  177.         // Make sure the ID is correct for update
  178.         $sloodle->id $sloodle->instance;
  179.         
  180.         // Make sure the type is the same as the existing record
  181.         $existing_record get_record('sloodle''id'$sloodle->id);
  182.         if (!$existing_recorderror(get_string('modulenotfound''sloodle'));
  183.         if ($existing_record->type != $sloodle->typeerror(get_string('moduletypemismatch''sloodle'));
  184.         
  185.         // Check the type of this module
  186.         switch ($sloodle->type{
  187.         case SLOODLE_TYPE_CTRL:
  188.             // Attempt to fetch the controller record
  189.             $ctrl get_record('sloodle_controller''sloodleid'$sloodle->id);
  190.             if (!$ctrlerror(get_string('secondarytablenotfound''sloodle'));
  191.             
  192.             // Add the updated 'enabled' value
  193.             if (isset($sloodle->controller_enabled&& $sloodle->controller_enabled$ctrl->enabled 1;
  194.             else $ctrl->enabled 0;
  195.             // Add the updated password
  196.             $ctrl->password $sloodle->controller_password;
  197.             
  198.             
  199.             // Update the database
  200.             update_record('sloodle_controller'$ctrl);
  201.             
  202.             break;
  203.             
  204.         case SLOODLE_TYPE_DISTRIB:
  205.             // Attempt to fetch the distributor record
  206.             $distrib get_record('sloodle_distributor''sloodleid'$sloodle->id);
  207.             if (!$distriberror(get_string('secondarytablenotfound''sloodle'));
  208.             
  209.             // Has a reset been requested?
  210.             if ($sloodle->distributor_reset{
  211.                 // Yes - clear the distributor channel
  212.                 $distrib->channel '';
  213.                 
  214.                 // Delete all objects associated with the Distributor
  215.                 delete_records('sloodle_distributor_entry''distributorid'$distrib->id);
  216.             }
  217.             
  218.             
  219.             // Update the database
  220.             update_record('sloodle_distributor'$distrib);
  221.             
  222.             break;
  223.             
  224.         case SLOODLE_TYPE_PRESENTER:
  225.             // Attempt to fetch the Presenter record
  226.             $presenter get_record('sloodle_presenter''sloodleid'$sloodle->id);
  227.             if (!$presentererror(get_string('secondarytablenotfound''sloodle'));
  228.  
  229.             // Add the updated frame dimensions
  230.             $presenter->framewidth = (int)$sloodle->presenter_framewidth;
  231.             $presenter->frameheight = (int)$sloodle->presenter_frameheight;
  232.  
  233.             // Update the database
  234.             update_record('sloodle_presenter'$presenter)
  235.  
  236.             break;
  237.  
  238.         case SLOODLE_TYPE_IBANK:
  239.             // Attempt to fetch the stipend giver record
  240.             $stipendgiver get_record('sloodle_stipendgiver''sloodleid'$sloodle->id);
  241.             if (!$stipendgivererror(get_string('secondarytablenotfound''sloodle'));
  242.             // Add the updates values from the form
  243.             $stipendgiver->amount = (int)$sloodle->stipendgiver_amount;
  244.              $stipendgiver->icurrency = (string)$sloodle->icurrency;       
  245.  
  246.             // Update the database
  247.             update_record('sloodle_stipendgiver'$stipendgiver);
  248.         break;
  249.  
  250.         case SLOODLE_TYPE_MAP;
  251.             // Attempt to fetch the map record
  252.             $map get_record('sloodle_map''sloodleid'$sloodle->id);
  253.             if (!$maperror(get_string('secondarytablenotfound''sloodle'));
  254.             
  255.             // Check for the settings updates
  256.             $map->initialx = (float)$sloodle->map_initialx;
  257.             $map->initialy = (float)$sloodle->map_initialy;
  258.             $map->initialzoom = (int)$sloodle->map_initialzoom;
  259.             if (empty($sloodle->map_showpan)) $map->showpan 0else $map->showpan 1;
  260.             if (empty($sloodle->map_allowdrag)) $map->allowdrag 0else $map->allowdrag 1;
  261.             if (empty($sloodle->map_showzoom)) $map->showzoom 0else $map->showzoom 1;
  262.             
  263.             // Update the database
  264.             update_record('sloodle_map'$map);
  265.             
  266.             break;
  267.             
  268.         // ADD FURTHER MODULE TYPES HERE!
  269.             
  270.         default:
  271.             // Type not recognised
  272.             //error(get_string('moduletypeunknown', 'sloodle'));
  273.             break;
  274.         }
  275.  
  276.         // Attempt the update
  277.         return update_record('sloodle'$sloodle);
  278.     }
  279.  
  280.     /**
  281.      * Given an ID of an instance of this module,
  282.      * this function will permanently delete the instance
  283.      * and any data that depends on it.
  284.      *
  285.      * @param int $id Id of the module instance
  286.      * @return boolean Success/Failure
  287.      ***/
  288.     function sloodle_delete_instance($id{
  289.  
  290.         // Determine our success or otherwise
  291.         $result true;
  292.  
  293.         // Attempt to delete the main Sloodle instance
  294.         if (!delete_records('sloodle''id'$id)) {
  295.             $result false;
  296.         }
  297.         
  298.         // Delete any secondary controller tables
  299.         delete_records('sloodle_controller''sloodleid'$id);
  300.         
  301.         // Attempt to get any secondary distributor tables
  302.         $distribs get_records('sloodle_distributor''sloodleid'$id);
  303.         if (is_array($distribs&& count($distribs0{
  304.             // Go through each distributor
  305.             foreach ($distribs as $d{
  306.                 // Delete any related distributor entries
  307.                 delete_records('sloodle_distributor_entry''distributorid'$d->id);
  308.             }
  309.         }
  310.         // Delete all the distributors
  311.         delete_records('sloodle_distributor''sloodleid'$id);
  312.  
  313.         // Delete any presenter entries
  314.         delete_records('sloodle_presenter_entry''sloodleid'$id);
  315.  
  316.         // Delete any map entries
  317.         delete_records('sloodle_map_location''sloodleid'$id);
  318.         
  319.         
  320.         // ADD FURTHER MODULE TYPES HERE!
  321.         delete_records('sloodle_stipendgiver''sloodleid'$id);  
  322.         delete_records('sloodle_iPoint_trans''stipendgiverid'$id);
  323.         return $result;
  324.     }
  325.  
  326.     /**
  327.      * Return a small object with summary information about what a
  328.      * user has done with a given particular instance of this module
  329.      * Used for user activity reports.
  330.      * $return->time = the time they did it
  331.      * $return->info = a short text description
  332.      *
  333.      * @return null 
  334.      * @todo Do we need this function to do anything?
  335.      ***/
  336.     function sloodle_user_outline($course$user$mod$sloodle{
  337.         return NULL;
  338.     }
  339.  
  340.     /**
  341.      * Print a detailed representation of what a user has done with
  342.      * a given particular instance of this module, for user activity reports.
  343.      *
  344.      * @return boolean 
  345.      * @todo Do we need this function to do anything?
  346.      ***/
  347.     function sloodle_user_complete($course$user$mod$sloodle{
  348.         return true;
  349.     }
  350.  
  351.     /**
  352.      * Given a course and a time, this module should find recent activity
  353.      * that has occurred in sloodle activities and print it out.
  354.      * Return true if there was output, or false is there was none.
  355.      *
  356.      * @uses $CFG
  357.      * @return boolean 
  358.      * @todo Figure out if we need this function to do anything!
  359.      ***/
  360.     function sloodle_print_recent_activity($course$isteacher$timestart{
  361.         global $CFG;
  362.  
  363.         return false;  //  True if anything was printed, otherwise false 
  364.     }
  365.  
  366.     /**
  367.      * Function to be run periodically according to the Moodle cron.
  368.      * Clears out expired pending user entries, session keys, etc.
  369.      *
  370.      * @return boolean 
  371.      * @todo Delete expired user entries
  372.      * @todo Use a custom delay for expiries of users/objects
  373.      ***/
  374.     function sloodle_cron ({
  375.         echo "\nProcessing Sloodle cron tasks:\n";
  376.         
  377.         // Delete any pending user entries which have expired (more than 30 minutes old)
  378.         $expirytime time(1800;
  379.         echo "Removing expired pending avatar entries...\n";
  380.         delete_records_select('sloodle_pending_avatars'"timeupdated < $expirytime");
  381.         
  382.         // Delete any LoginZone allocations which have expired (more than 15 minutes old)
  383.         $expirytime time(900;
  384.         echo "Removing expired LoginZone allocations...\n";
  385.         delete_records_select('sloodle_loginzone_allocation'"timecreated < $expirytime");
  386.         
  387.         // Fetch our configuration settings, if they exist
  388.         $active_object_days 7// Give active objects a week by default
  389.         if (!empty($CFG->sloodle_active_object_lifetime)) $active_object_days $CFG->sloodle_active_object_lifetime;
  390.         $user_object_days 21// Give user objects 3 weeks by default
  391.         if (!empty($CFG->sloodle_user_object_lifetime)) $user_object_days $CFG->sloodle_user_object_lifetime;
  392.         
  393.         // Delete any active objects and session keys which have expired
  394.         // Deletes any authorised objects which have not checked-in for more than X days (where X is specified in module config)
  395.         // Deletes any unauthorised objects which have not checked-in for more than 1 hour
  396.         $expirytime_auth time((86400 $active_object_days);
  397.         $expirytime_unauth time(3600;
  398.         echo "Searching for expired active objects...\n";
  399.         $recs get_records_select('sloodle_active_object'"(((controllerid = 0 AND userid = 0) OR type = '') AND timeupdated < $expirytime_unauth) OR timeupdated < $expirytime_auth");
  400.         if ($recs{
  401.             // Go through each object
  402.             echo "Removing " count($recs" expired active objects and associated configuration settings...\n";
  403.             foreach ($recs as $r{
  404.                 delete_records('sloodle_object_config''object'$r->id);
  405.                 delete_records('sloodle_active_object''id'$r->id);
  406.             }            
  407.         }
  408.         
  409.         // Delete any user-authorised objects which have expired
  410.         // (will eventually use custom expiry times, chosen in the configuration)
  411.         // Deletes any authorised objects which have not checked-in for more than X days (where X is specified in module config)
  412.         // Deletes any unauthorised objects which have not checked-in for more than 1 hour
  413.         $expirytime_auth time((86400 $user_object_days);
  414.         $expirytime_unauth time(3600;
  415.         echo "Deleting expired user objects...\n";
  416.         delete_records_select('sloodle_user_object'"(authorised = 0 AND timeupdated < $expirytime_unauth) OR timeupdated < $expirytime_auth");
  417.         
  418.         // More stuff?
  419.         //...
  420.         
  421.         // Email login details to auto-registered avatars in-world
  422.         echo "Sending out login notifications...\n";
  423.  
  424.         echo "Done processing Sloodle cron tasks.\n\n";
  425.         return true;
  426.     }
  427.  
  428.     /**
  429.      * Must return an array of grades for a given instance of this module,
  430.      * indexed by user.  It also returns a maximum allowed grade.
  431.      * 
  432.      * Example:
  433.      *    $return->grades = array of grades;
  434.      *    $return->maxgrade = maximum allowed grade;
  435.      *
  436.      *    return $return;
  437.      *
  438.      * @param int $sloodleid ID of an instance of this module
  439.      * @return mixed Null or object with an array of grades and with the maximum grade
  440.      ***/
  441.     function sloodle_grades($sloodleid{
  442.        return NULL;
  443.     }
  444.  
  445.     /**
  446.      * Must return an array of user records (all data) who are participants
  447.      * for a given instance of sloodle. Must include every user involved
  448.      * in the instance, independient of his role (student, teacher, admin...)
  449.      * See other modules as example.
  450.      *
  451.      * @param int $sloodleid ID of an instance of this module
  452.      * @return mixed boolean/array of students
  453.      * @todo Possibly make it return a list of users registered via the Sloodle instance?
  454.      ***/
  455.     function sloodle_get_participants($sloodleid{
  456.         return false;
  457.     }
  458.  
  459.     /**
  460.      * This function returns if a scale is being used by one sloodle
  461.      * it it has support for grading and scales. Commented code should be
  462.      * modified if necessary. See forum, glossary or journal modules
  463.      * as reference.
  464.      *
  465.      * @param int $sloodleid ID of an instance of this module
  466.      * @return mixed 
  467.      ***/
  468.     function sloodle_scale_used ($sloodleid,$scaleid{
  469.         $return false;
  470.        
  471.         return $return;
  472.     }
  473.  
  474.  
  475.     /**
  476.     * Gets the different sub-types of Sloodle module available as a list for the "Add Activity..." menu.
  477.     * Also adds the 'Sloodle Map' resource type.
  478.     *
  479.     * $return array Entries for the "Add Activity..." and "Add Resource..." menus.
  480.     */
  481.  
  482.     function sloodle_get_types({
  483.         global $CFG$SLOODLE_TYPES;
  484.         $types array();
  485.  
  486.         // Start the group of activities
  487.         $type new object();
  488.         $type->modclass MOD_CLASS_ACTIVITY;
  489.         $type->type "sloodle_group_start";
  490.         $type->typestr '--'.get_string('modulenameplural''sloodle');
  491.         $types[$type;
  492.          
  493.         // Go through each Sloodle module type, and add it
  494.         foreach ($SLOODLE_TYPES as $st{
  495.             $type new object();
  496.             $type->modclass MOD_CLASS_ACTIVITY;
  497.             $type->type "sloodle&amp;type=$st";
  498.             $type->typestr get_string("moduletype:$st"'sloodle');
  499.             $types[$type;
  500.         }
  501.  
  502.         // End the group of activities
  503.         $type new object();
  504.         $type->modclass MOD_CLASS_ACTIVITY;
  505.         $type->type "sloodle_group_end";
  506.         $type->typestr '--';
  507.         $types[$type;
  508.  
  509.         // Add the resource
  510.         $type new object();
  511.         $type->modclass MOD_CLASS_RESOURCE;
  512.         $type->type "sloodle&amp;type=map";
  513.         $type->typestr get_string("moduleaction:map""sloodle");
  514.         $types[$type;
  515.  
  516.         return $types;
  517.     }
  518.  
  519.  
  520.  
  521. ?>

Documentation generated on Fri, 17 Jul 2009 11:01:35 +0100 by phpDocumentor 1.4.0