Source for file configure_object.php

Documentation is available at configure_object.php

  1. <?php
  2.     /**
  3.     * Sloodle object configuration/authorization page.
  4.     *
  5.     * Allows users in Moodle to authorize or deny in-world objects' access to Moodle,
  6.     *  and to select the configuration settings.
  7.     *
  8.     * @package sloodleclassroom
  9.     * @copyright Copyright (c) 2007-8 Sloodle (various contributors)
  10.     * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  11.     *
  12.     * @contributor Edmund Edgar
  13.     * @contributor Peter R. Bloomfield
  14.     *
  15.     */
  16.     
  17.     // Initially, the script should be accessed with a single parameter:
  18.     //
  19.     //  sloodleauthid = the integer ID of an (unauthorised) entry in the Sloodle 'active objects' table
  20.     //
  21.     // When the user clicks "yes" or "no" to confirm or deny the object's authorisation,
  22.     //  the following additional parameters will be added:
  23.     //
  24.     //  sloodlecontrollerid = the ID of the controller which is to be used
  25.     //  action = either 'auth' or 'cancel'
  26.     //
  27.     // If the action is 'confirm' then the object is authorised. Otherwise, its entry is deleted.
  28.     //
  29.     // The following parameter will usually also need to be specified:
  30.     //
  31.     //  sloodleobjtype = gives the type identifier of the object (not necessary if type is already registered)
  32.     
  33.     /** Grab the Sloodle/Moodle configuration. */
  34.     require_once('../sl_config.php');
  35.     /** Include the Sloodle PHP API. */
  36.     require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  37.     require_once(SLOODLE_LIBROOT.'/general.php');
  38.     
  39.     // Make sure the user is logged-in
  40.     
  41.     // We need to know which object is being authorised and/or configured
  42.     $sloodleauthid required_param('sloodleauthid'PARAM_INT);
  43.     $auth_obj SloodleController::get_object($sloodleauthid);
  44.     if (!$auth_obj{
  45.         error(get_string('objectauthnotfound','sloodle'));
  46.         exit();
  47.     }
  48.     // Is the object already authorised?
  49.     $object_authorised $auth_obj->course->controller->is_loaded();
  50.     
  51.     // Get the object type (only necessary if it was not already in the database)
  52.     $sloodleobjtype null;
  53.     if (empty($auth_obj->type)) {
  54.         $sloodleobjtype required_param('sloodleobjtype'PARAM_TEXT);
  55.     else {
  56.         $sloodleobjtype optional_param('sloodleobjtype'nullPARAM_TEXT);
  57.     }
  58.     
  59.     // Treat this like a Sloodle session, but do not authenticate anything
  60.     $sloodle new SloodleSession();
  61.     // Was a controller loaded by the request?
  62.     $loaded_controller false;
  63.     if ($sloodle->course->controller->is_loaded()) {
  64.         $loaded_controller true;
  65.     else {
  66.         // If a controller was already specified for the object, then use that
  67.         if ($auth_obj->course->controller->is_loaded()) {
  68.             $loaded_controller true;
  69.             $sloodle->course $auth_obj->course;
  70.         }
  71.     }
  72.     
  73.     // Load the user
  74.     $sloodle->user->load_user($USER->id);
  75.     $sloodle->user->load_linked_avatar();
  76.     
  77.     // Fetch localization strings
  78.     $stryes get_string('yes');
  79.     $strno get_string('no');
  80.     
  81.     // Check if the confirmation action has been specified
  82.     $action optional_param('action'''PARAM_TEXT);
  83.     // If the action was not recognised, then ignore it
  84.     if ($action != 'auth' && $action != 'cancel'$action '';
  85.     
  86.     // Determine the page name - it should be authorisation or configuration
  87.     $pagename get_string('objectauth','sloodle');
  88.     if ($object_authorised == true || $action == 'auth'$pagename get_string('objectconfiguration','sloodle');
  89.     
  90.     // Construct a breadcrumb navigation menu
  91.     $nav get_string('modulename''sloodle').' -> ';
  92.     $nav .= $pagename;
  93.     // Display the page header
  94.     print_header($pagename$pagename$nav''''false);
  95.     
  96.     
  97. //------------------------------------------------------------
  98. // AUTHORISATION STEP
  99.     
  100.     // Was an action specified? (Don't do this if the object was already authorised)
  101.     $action_success false;
  102.     if (!empty($action&& $object_authorised == false{
  103.         // Check if we were instructed to authorise, and also that we have a controller to authorise on
  104.         if ($action == 'auth' && $loaded_controller == true{
  105.             // Make sure the user is allowed to authorise objects
  106.             $module_context get_context_instance(CONTEXT_MODULE$sloodle->course->controller->get_id());
  107.             require_capability('mod/sloodle:objectauth'$module_context);
  108.         
  109.             // Indicate that we are attempting authorisation
  110.             echo '<div style="text-align:center;">';
  111.             echo get_string('authorizingfor''sloodle').$sloodle->course->get_full_name().' &gt; '.$sloodle->course->controller->get_name().'<br><br>';
  112.         
  113.             // Attempt to authorise the entry
  114.             if ($sloodle->course->controller->authorise_object($auth_obj->uuid$sloodle->user$sloodleobjtype)) {
  115.                 echo '<span style="font-weight:bold; color:green;">'.get_string('objectauthsuccessful','sloodle').'</span><br>';
  116.                 $action_success true;
  117.                 $object_authorised true;
  118.                 // Reload the object data
  119.                 $auth_obj SloodleController::get_object($sloodleauthid);
  120.                 
  121.             else {
  122.                 echo '<span style="font-weight:bold; color:red;">'.get_string('objectauthfailed','sloodle').'</span><br>';
  123.             }
  124.             
  125.             echo "</div><br>\n";
  126.             
  127.         else if ($action == 'cancel'{
  128.             // Attempt to delete the entry
  129.             $sloodle->course->controller->remove_object($sloodleauthid);
  130.             // Display the result
  131.             print_box(get_string('objectauthcancelled''sloodle')'generalbox boxaligncenter boxwidthnarrow');
  132.             $action_success true;
  133.         }
  134.     else if ($object_authorised{
  135.         // If the object has already been authorised, then display a message about it
  136.         print_box_start('generalbox boxwidthnarrow boxaligncenter');
  137.         echo '<span style="color:red; font-weight:bold; text-align:center;">'.get_string('objectauthalready','sloodle').'</span>';
  138.         print_box_end();
  139.     }
  140.     
  141. //------------------------------------------------------------
  142. // OBJECT INFO
  143.     
  144.     // Display the information about the object
  145.     print_box_start('generalbox boxaligncenter boxwidthnarrow');
  146.     echo '<div style="text-align:center;">';
  147.     echo '<span style="font-weight:bold; font-size:110%;">'.get_string('objectdetails','sloodle').'</span><br>';
  148.     
  149.     echo get_string('objectname','sloodle').': '.$auth_obj->name.'<br>';
  150.     echo get_string('objectuuid','sloodle').': '.$auth_obj->uuid.'<br>';
  151.     echo get_string('objecttype','sloodle').': ';
  152.     if (!empty($sloodleobjtype)) echo $sloodleobjtype.'<br>';
  153.     else if (!empty($auth_obj->type)) echo $auth_obj->type.'<br>';
  154.     else echo '-<br>';
  155.     
  156.     echo '</div>';
  157.     print_box_end();
  158.     echo '<br>';
  159.     
  160. //------------------------------------------------------------
  161. // AUTHORISATION FORM
  162.     
  163.     // If the action was not performed or successful, then display the authorisation form
  164.     if ($action_success == false && $object_authorised == false{
  165.         print_box_start('generalbox boxaligncenter boxwidthnormal');
  166.         echo '<div style="text-align:center;">';
  167.         echo '<h3>'.get_string('objectauth','sloodle').'</h3>';
  168.  
  169.         // If an authorisation action was attempted but the controller failed to load, then display an error message
  170.         if ($action == 'auth' && $loaded_controller false{
  171.             echo '<span style="font-weight:bold;color:red;">'.get_string('failedauth-trydifferent','sloodle').'</span>';
  172.         }
  173.         
  174.         // Get a list of controllers which the user is permitted to authorise objects on
  175.         // (It will be a 2d array - top level will be course ID's, 2nd level will be coursemodule ID's, containing database objects from the 'sloodle' table)
  176.         $controllers array();
  177.         $recs get_records('sloodle''type'SLOODLE_TYPE_CTRL);
  178.         // Make sure we have at least one controller
  179.         if ($recs == false || count($recs== 0{
  180.             error(get_string('objectauthnocontrollers','sloodle'));
  181.             exit();
  182.         }
  183.         foreach ($recs as $r{
  184.             // Fetch the course module
  185.             $cm get_coursemodule_from_instance('sloodle'$r->id);
  186.             // Check that the person can authorise objects of this module
  187.             if (has_capability('mod/sloodle:objectauth'get_context_instance(CONTEXT_MODULE$cm->id))) {
  188.                 // Store this controller
  189.                 $controllers[$cm->course][$cm->id$r;
  190.             }
  191.         }
  192.         
  193.         // Make sure that permission came through on at least one controller
  194.         if (count($controllers== 0{
  195.             error(get_string('objectauthnopermission','sloodle'));
  196.             exit();
  197.         }
  198.         
  199.         // Construct the list of course names
  200.         $coursenames array();
  201.         foreach ($controllers as $cid => $ctrl{
  202.             $courserec get_record('course''id'$cid'','''','''id,shortname,fullname');
  203.             if (!$courserec$coursenames[$cid"(unknown)";
  204.             else $coursenames[$cid$courserec->fullname;
  205.         }
  206.         
  207.         // Sort the list alphabetically
  208.         natcasesort($coursenames);
  209.         
  210.         // Attempt to get our current controller, if possible, so we can pre-select it in the list
  211.         $cur_controller_id 0;
  212.         if ($loaded_controller{
  213.             $cur_controller_id $sloodle->course->controller->get_id();
  214.         }
  215.         
  216.         // Display an authorisation form
  217.         echo '<form action="" method="GET">';
  218.         if (SLOODLE_DEBUGecho '<input type="hidden" name="sloodledebug" value="true"/>';
  219.         if (!empty($sloodleauthid)) echo '<input type="hidden" name="sloodleauthid" value="'.$sloodleauthid.'"/>';
  220.         if (!empty($sloodleobjtype)) echo '<input type="hidden" name="sloodleobjtype" value="'.$sloodleobjtype.'"/>';
  221.         
  222.         // Ask the user to select a controller to authorise on
  223.         echo get_string('selectcontroller','sloodle').':<br><br>';
  224.         echo '<select name="sloodlecontrollerid" size="6">';
  225.         // Go through each course
  226.         foreach ($coursenames as $cid => $cname// We want to use the alphabetic sorting; get the course ID and name
  227.             // Output a disabled entry to act as a course header
  228.             echo "<option disabled=\"true\" style=\"font-weight:bold; font-style:italic; background-color:#eeeeee;\">{$cid}$cname</option>\n";
  229.             
  230.             // Go through each controller in this course
  231.             foreach ($controllers[$cidas $cmid => $ctrl// Get each controller in the course; get the course module ID and controller data
  232.                 echo "<option ";
  233.                 if ($cmid == $cur_controller_idecho 'selected="true" ';
  234.                 echo "style=\"margin-left:8px;\" value=\"$cmid\">{$ctrl->name}</option>\n";
  235.             }
  236.         }
  237.         echo '</select><br><br>';
  238.         
  239.         // Ask the user if they want to confirm or deny the authorisation
  240.         echo get_string('confirmobjectauth''sloodle').' ';
  241.         echo '<select name="action" size="1">';
  242.         echo '<option style="color:green; font-weight:bold;" value="auth" selected="true">'.$stryes.'</option>';
  243.         echo '<option style="color:red; font-weight:bold;" value="cancel">'.$strno.'</option>';
  244.         echo '</select><br><br>';
  245.         
  246.         echo '<input type="submit" value="'.get_string('submit','sloodle').'"/>';
  247.         echo "</form>\n";
  248.         
  249.         echo '</div>';
  250.         print_box_end();
  251.     }
  252.     
  253.     
  254. //------------------------------------------------------------
  255. // CONFIGURATION FORM
  256.  
  257.     // Get the type
  258.     $usetype "";
  259.     if (!empty($auth_obj->type)) $usetype $auth_obj->type;
  260.     else $usetype $sloodleobjtype;    
  261.     
  262.  
  263.     // If the object is now authorised, we can display its configuration form
  264.     if ($object_authorised && !empty($usetype)) {
  265.         // Make sure the user has permission to manage activities on this course
  266.         $course_context get_context_instance(CONTEXT_COURSE$auth_obj->course->get_course_id());
  267.         require_capability('moodle/course:manageactivities'$course_context);
  268.         
  269.         // Make sure the type value is stored in the database too
  270.         $sloodle->course->controller->update_object_type($auth_obj->uuid$usetype);
  271.     
  272.         // Make sure the type exists
  273.         $objectpath SLOODLE_DIRROOT."/mod/$usetype";
  274.         if (!file_exists($objectpath)) error(get_string('objectnotinstalled','sloodle'));
  275.         // Determine if we have a custom configuration page
  276.         $customconfig $objectpath.'/object_config.php';
  277.         $hascustomconfig file_exists($customconfig);
  278.         
  279.         // Display the configuration section
  280.         print_box_start('generalbox boxwidthnormal boxaligncenter');
  281.         echo '<div style="text-align:center;"><h2>'.get_string('objectconfiguration','sloodle').'</h2>';
  282.         
  283.         // Do we have custom configuration settings?
  284.         if ($hascustomconfig{
  285.             // Display our configuration form
  286.             echo '<form action="'.SLOODLE_WWWROOT.'/classroom/store_object_config.php" method="POST">';
  287.             // Add a hidden field to store the object's type
  288.             echo '<input type="hidden" name="sloodleobjtype" value="'.$usetype.'"/>';
  289.             
  290.             
  291.             // Include the form elements
  292.             require($customconfig);
  293.             
  294.             
  295.             // Add this object's authorisation ID, and a submit button
  296.             echo '<br><input type="hidden" name="sloodleauthid" value="'.$sloodleauthid.'"/>';
  297.             if (SLOODLE_DEBUGecho '<input type="hidden" name="sloodledebug" value="true"/>';
  298.             echo '<input type="submit" value="'.get_string('submit','sloodle').'"/>';
  299.             echo '</form>';
  300.             
  301.         else {
  302.             // No configuration settings for this object
  303.             print_string('noobjectconfig','sloodle');
  304.             
  305.             // Add or udpate a configuration value to store this object's type
  306.             $cfgtype get_record('sloodle_object_config''object'$sloodleauthid'name''sloodleobjtype');
  307.             if (!$cfgtype{
  308.                 $cfgtype new stdClass();
  309.                 $cfgtype->object $sloodleauthid;
  310.                 $cfgtype->name 'sloodleobjtype';
  311.                 $cfgtype->value $usetype;
  312.                 insert_record('sloodle_object_config'$cfgtype);
  313.             else {
  314.                 $cfgtype->value =  $usetype;
  315.                 update_record('sloodle_object_config'$cfgtype);
  316.             }
  317.         }
  318.         
  319.         echo '</div>';
  320.         print_box_end();
  321.     }
  322.     
  323.     
  324.     // Finish
  325.     print_footer();
  326.  
  327. ?>

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