course->controller->is_loaded();
// Get the object type (only necessary if it was not already in the database)
$sloodleobjtype = null;
if (empty($auth_obj->type)) {
    $sloodleobjtype = required_param('sloodleobjtype', PARAM_TEXT);
}
else {
    $sloodleobjtype = optional_param('sloodleobjtype', null, PARAM_TEXT);
}
if ($sloodleobjtype!=null) $urlparams['sloodleobjtype'] = $sloodleobjtype;
// Treat this like a Sloodle session, but do not authenticate anything
$sloodle = new SloodleSession();
// Was a controller loaded by the request?
$loaded_controller = false;
if ($sloodle->course->controller->is_loaded()) {
    $loaded_controller = true;
}
else {
    // If a controller was already specified for the object, then use that
    if ($auth_obj->course->controller->is_loaded()) {
        $loaded_controller = true;
        $sloodle->course = $auth_obj->course;
    }
}
// Load the user
$sloodle->user->load_user($USER->id);
$sloodle->user->load_linked_avatar();
// Fetch localization strings
$stryes = get_string('yes');
$strno = get_string('no');
// Check if the confirmation action has been specified
$action = optional_param('action', '', PARAM_TEXT);
if ($action!='') $urlparams['action'] = $action;
// If the action was not recognised, then ignore it
if ($action != 'auth' && $action != 'cancel') $action = '';
// Determine the page name - it should be authorisation or configuration
$pagename = get_string('objectauth','sloodle');
if ($object_authorised == true || $action == 'auth') $pagename = get_string('objectconfiguration','sloodle');
//
$PAGE->set_url('/mod/sloodle/classroom/configure_object.php', $urlparams);
$PAGE->set_context(context_course::instance(SITEID, IGNORE_MISSING));
// Construct a breadcrumb navigation menu
$nav = get_string('modulename', 'sloodle').' -> ';
$nav .= $pagename;
// Display the page header
sloodle_print_header($pagename, $pagename, $nav, '', '', false);
//------------------------------------------------------------
// AUTHORISATION STEP
// Was an action specified? (Don't do this if the object was already authorised)
$action_success = false;
if (!empty($action) && $object_authorised == false) {
    // Check if we were instructed to authorise, and also that we have a controller to authorise on
    if ($action == 'auth' && $loaded_controller == true) {
        // Make sure the user is allowed to authorise objects
        //$module_context = get_context_instance(CONTEXT_MODULE, $sloodle->course->controller->get_id());
        $module_context = context_module::instance($sloodle->course->controller->get_id());
        require_capability('mod/sloodle:objectauth', $module_context);
    
        // Indicate that we are attempting authorisation
        echo '
';
        echo get_string('authorizingfor', 'sloodle').$sloodle->course->get_full_name().' > '.$sloodle->course->controller->get_name().'
';
    
        // Attempt to authorise the entry
        if ($sloodle->course->controller->authorise_object($auth_obj->uuid, $sloodle->user, $sloodleobjtype)) {
            echo ''.get_string('objectauthsuccessful','sloodle').'
';
            $action_success = true;
            $object_authorised = true;
            // Reload the object data
            $auth_obj = SloodleController::get_object($sloodleauthid);
        }
        else {
            echo ''.get_string('objectauthfailed','sloodle').'
';
        }
        echo "
\n";
    }
    else if ($action == 'cancel') {
        // Attempt to delete the entry
        $sloodle->course->controller->remove_object($sloodleauthid);
        // Display the result
        sloodle_print_box(get_string('objectauthcancelled', 'sloodle'), 'generalbox boxaligncenter boxwidthnarrow');
        $action_success = true;
    }
}
else if ($object_authorised) {
    // If the object has already been authorised, then display a message about it
    sloodle_print_box_start('generalbox boxwidthnarrow boxaligncenter');
    echo ''.get_string('objectauthalready','sloodle').'';
    sloodle_print_box_end();
}
//------------------------------------------------------------
// OBJECT INFO
// Display the information about the object
sloodle_print_box_start('generalbox boxaligncenter boxwidthnarrow');
echo '';
echo ''.get_string('objectdetails','sloodle').'
';
echo get_string('objectname','sloodle').': '.$auth_obj->name.'
';
echo get_string('objectuuid','sloodle').': '.$auth_obj->uuid.'
';
echo get_string('objecttype','sloodle').': ';
if (!empty($sloodleobjtype)) echo $sloodleobjtype.'
';
else if (!empty($auth_obj->type)) echo $auth_obj->type.'
';
else echo '-
';
echo '
';
sloodle_print_box_end();
echo '
';
//------------------------------------------------------------
// AUTHORISATION FORM
// If the action was not performed or successful, then display the authorisation form
if ($action_success == false && $object_authorised == false) {
    sloodle_print_box_start('generalbox boxaligncenter boxwidthnormal');
    echo '';
    echo '
'.get_string('objectauth','sloodle').'
';
    // If an authorisation action was attempted but the controller failed to load, then display an error message
    if ($action == 'auth' && $loaded_controller = false) {
        echo ''.get_string('failedauth-trydifferent','sloodle').'';
    }
    
    // Get a list of controllers which the user is permitted to authorise objects on
    // (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)
    $controllers = array();
    $recs = sloodle_get_records('sloodle', 'type', SLOODLE_TYPE_CTRL);
    // Make sure we have at least one controller
    if ($recs == false || count($recs) == 0) {
        print_error(get_string('objectauthnocontrollers','sloodle'));
        exit();
    }
    foreach ($recs as $r) {
        // Fetch the course module
        $cm = get_coursemodule_from_instance('sloodle', $r->id);
        // Check that the person can authorise objects of this module
        //if (has_capability('mod/sloodle:objectauth', get_context_instance(CONTEXT_MODULE, $cm->id))) {
        if (has_capability('mod/sloodle:objectauth', context_module::instance($cm->id))) {
            // Store this controller
            $controllers[$cm->course][$cm->id] = $r;
        }
    }
    
    // Make sure that permission came through on at least one controller
    if (count($controllers) == 0) {
        print_error(get_string('objectauthnopermission','sloodle'));
        exit();
    }
    
    // Construct the list of course names
    $coursenames = array();
    foreach ($controllers as $cid => $ctrl) {
        $courserec = sloodle_get_record('course', 'id', $cid, '','', '','', 'id,shortname,fullname');
        if (!$courserec) $coursenames[$cid] = "(unknown)";
        else $coursenames[$cid] = $courserec->fullname;
    }
    
    // Sort the list alphabetically
    natcasesort($coursenames);
    
    // Attempt to get our current controller, if possible, so we can pre-select it in the list
    $cur_controller_id = 0;
    if ($loaded_controller) {
        $cur_controller_id = $sloodle->course->controller->get_id();
    }
    
    // Display an authorisation form
    echo '\n";
    
    echo '';
    sloodle_print_box_end();
}
//------------------------------------------------------------
// CONFIGURATION FORM
// Get the type
$usetype = "";
if (!empty($auth_obj->type)) $usetype = $auth_obj->type;
else $usetype = $sloodleobjtype;    
// If the object is now authorised, we can display its configuration form
if ($object_authorised && !empty($usetype)) {
    // Make sure the user has permission to manage activities on this course
    //$course_context = get_context_instance(CONTEXT_COURSE, $auth_obj->course->get_course_id());
    $course_context = context_course::instance($auth_obj->course->get_course_id(), IGNORE_MISSING);
    require_capability('moodle/course:manageactivities', $course_context);
    
    // Make sure the type value is stored in the database too
    $sloodle->course->controller->update_object_type($auth_obj->uuid, $usetype);
    $auth_obj->type = $usetype;
    // Fumi.Hax
    if (preg_match('/^(.*?)\/(.*?)$/', $usetype, $matches) ) {
        $usetype = $matches[1].'/objects/'.$matches[2];
    }
    // Make sure the type exists
    $objectpath = SLOODLE_DIRROOT."/mod/$usetype";
    if (!file_exists($objectpath)) print_error(get_string('objectnotinstalled','sloodle'));
    // Determine if we have a custom configuration page
    $hascustomconfig = $auth_obj->has_custom_config();
    
    // Display the configuration section
    sloodle_print_box_start('generalbox boxwidthnormal boxaligncenter');
    echo ''.get_string('objectconfiguration','sloodle').'
';
    
    // Do we have custom configuration settings?
    if ($hascustomconfig) {
        // Display our configuration form
        echo '';
    }
    else {
        // No configuration settings for this object
        print_string('noobjectconfig','sloodle');
        
        // Add or udpate a configuration value to store this object's type
        $cfgtype = sloodle_get_record('sloodle_object_config', 'object', $sloodleauthid, 'name', 'sloodleobjtype');
        if (!$cfgtype) {
            $cfgtype = new stdClass();
            $cfgtype->object = $sloodleauthid;
            $cfgtype->name = 'sloodleobjtype';
            $cfgtype->value = $usetype;
            sloodle_insert_record('sloodle_object_config', $cfgtype);
        }
        else {
            $cfgtype->value =  $usetype;
            sloodle_update_record('sloodle_object_config', $cfgtype);
        }
    }
    
    echo '';
    sloodle_print_box_end();
}
// Finish
sloodle_print_footer();