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

Documentation generated on Mon, 07 Jul 2008 12:32:18 +0100 by phpDocumentor 1.4.0