/**
 * Restore everything from the given backup.
 */
function sloodle_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //Get record from backup_ids
    $data = backup_getid($restore->backup_unique_code, $mod->modtype, $mod->id);
    if ($data) {
        // Get the XML backup data as an array
        $info = $data->info;
        // Build our SLOODLE DB record
        $sloodle = new object();
        $sloodle->course = $restore->course_id;
        $sloodle->type = backup_todb($info['MOD']['#']['SUBTYPE']['0']['#']);
        $sloodle->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $sloodle->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
        $sloodle->timecreated = backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
        $sloodle->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        $newid = insert_record("sloodle", $sloodle);
        // Inform the user what we are restoring
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "sloodle") . " \"" . format_string(stripslashes($sloodle->name), true) . "\"</li>";
        }
        backup_flush(300);
        if ($newid) {
            // We have the newid, update backup_ids
            backup_putid($restore->backup_unique_code, $mod->modtype, $mod->id, $newid);
            // Should we restore userdata?
            $includeuserdata = restore_userdata_selected($restore, 'sloodle', $mod->id);
            // Attempt to get a SloodleModule object for this module sub-type
            $dummysession = new SloodleSession(false);
            // We need to provide this to keep the module happy!
            $moduleobj = sloodle_load_module($sloodle->type, $dummysession);
            if ($moduleobj != false) {
                // Attempt to restore this module's secondary data
                if (!$moduleobj->restore($newid, $info['MOD']['#']['SECONDARYDATA']['0']['#'], $includeuserdata)) {
                    $status = false;
                }
            } else {
                echo "<li>Failed to fully restore SLOODLE module type {$sloodle->type}. This type may not be available on your installation.</li>";
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
/**
 * Backs-up one instance of a SLOODLE module.
 * @param $bf Handle to the file to which backup data should be written.
 * @param $preferences Structure defining preferences which govern the backup.
 * @param object|int $mod If an object, then it is a record from the 'sloodle' db table. If it is a number, then it is the ID of a record in the 'sloodle' db table.
 * @return bool True if successful, or false if not.
 */
function sloodle_backup_one_mod($bf, $preferences, $mod)
{
    // Load a 'sloodle' record if necessary
    if (is_numeric($mod)) {
        $mod = get_record('sloodle', 'id', $mod);
        if ($mod === false) {
            return false;
        }
    }
    $status = true;
    // Attempt to load the course module record from the database
    $cm = get_coursemodule_from_instance('sloodle', $mod->id);
    if ($cm === false) {
        return false;
    }
    // Attempt to get a SloodleModule object for this module sub-type
    $dummysession = new SloodleSession(false);
    // We need to provide this to keep the module happy!
    $moduleobj = sloodle_load_module($mod->type, $dummysession, $cm->id);
    if ($moduleobj == false) {
        return false;
    }
    // Start an element for this module instance, and backup the primary table data
    fwrite($bf, start_tag('MOD', 3, true));
    fwrite($bf, full_tag('ID', 4, false, $mod->id));
    // Instance ID of this SLOODLE module
    fwrite($bf, full_tag('MODTYPE', 4, false, 'sloodle'));
    // Main type of module (always 'sloodle' in this case)
    fwrite($bf, full_tag('SUBTYPE', 4, false, $mod->type));
    // Sub-type of module (e.g. 'controller' or 'presenter')
    fwrite($bf, full_tag('NAME', 4, false, $mod->name));
    fwrite($bf, full_tag('INTRO', 4, false, $mod->intro));
    fwrite($bf, full_tag('TIMECREATED', 4, false, $mod->timecreated));
    fwrite($bf, full_tag('TIMEMODIFIED', 4, false, $mod->timemodified));
    // Backup any secondary data
    fwrite($bf, start_tag('SECONDARYDATA', 4, true));
    if (!$moduleobj->backup($bf, backup_userdata_selected($preferences, 'sloodle', $mod->id))) {
        $status = false;
    }
    fwrite($bf, end_tag('SECONDARYDATA', 4, true));
    // Finish off
    if (!fwrite($bf, end_tag('MOD', 3, true))) {
        $status = false;
    }
    return $status;
}
 /**
  * Constructs and loads the appropriate module part of the session.
  * Note that this function will fail if the current VLE user (in the $user member) does not have permission to access it.
  * @param string $type The expected type of module - function fails if type is not correctly matched
  * @param bool $db If true then the system will also try to load appropriate data from the database, as specified in the module ID request parameter
  * @param bool $require If true, then if something goes wrong, the script will be terminated with an error message
  * @param bool $override_access If true, then access can be gained to a module on a separate course from the current controller
  * @return bool True if successful, or false otherwise. (Note, if parameter $require was true, then the script will terminate before this function returns if something goes wrong)
  */
 function load_module($type, $db, $require = true, $override_access = false)
 {
     // If the database loading is requested, then make sure we have a parameter to load with
     $db_id = null;
     if ($db) {
         $db_id = $this->request->get_module_id($require);
         if ($db_id == null) {
             return false;
         }
         // Is access being overridden?
         if (!$override_access) {
             // No
             // Make sure we have a controller loaded
             if (!$this->course->is_loaded() || !$this->course->controller->is_loaded()) {
                 if ($require) {
                     $this->response->quick_output(-714, 'MODULE_INSTANCE', 'Access has not been authenticated through a Controller. Access prohibited.', false);
                     exit;
                 }
                 return false;
             }
             // Does the specified module instance exist in this course?
             if (!record_exists('course_modules', 'id', $db_id, 'course', $this->course->get_course_id())) {
                 if ($require) {
                     $this->response->quick_output(-714, 'MODULE_INSTANCE', 'Module not found in requested course.', false);
                     exit;
                 }
                 return false;
             }
         }
     }
     // Construct the module
     $this->module = sloodle_load_module($type, $this, $db_id);
     if (!$this->module) {
         if ($require) {
             $this->response->quick_output(-601, 'MODULE', 'Failed to construct module object', false);
             exit;
         }
         return false;
     }
 }
/**
 * Constructs and loads an appropriate SLOODLE module object, based on the course module or course module ID.
 * The appropriate type is detected automatically.
 * The object is provided with a dummy SloodleSession object.
 * @param int|object $cm Either an integer course module ID, or a course module record.
 * @return SloodleModule|bool Returns the constructed module object, or false if it fails.
 */
function sloodle_quick_load_module_from_cm($cm)
{
    // Obtain a course module object if necessary
    if (is_numeric($cm)) {
        $cm = get_coursemodule_from_id('sloodle', $cm);
        if ($cm === false) {
            return false;
        }
    }
    // Get an instance record
    $instance = get_record('sloodle', 'id', (int) $cm->instance);
    if ($instance === false) {
        return false;
    }
    // Attempt to load the module
    $dummysession = new SloodleSession(false);
    return sloodle_load_module($instance->type, $dummysession, (int) $cm->id);
}