<?php //$Id: backuplib.php,v 1.2 2007-07-27 10:59:27 nicus Exp $
    //This php script contains all the stuff to backup/restore
    //register mods

    function register_backup_mods($bf,$preferences) {
        
        global $CFG;

        $status = true;

        //Iterate over register table
        $registers = get_records ("register","course",$preferences->backup_course,"id");
        if ($registers) {
            foreach ($registers as $register) {
                if (backup_mod_selected($preferences,'register',$register->id)) {
                    $status = register_backup_one_mod($bf,$preferences,$register);
                }
            }
        }
        return $status;
    }

    function register_backup_one_mod($bf,$preferences,$register) {

        global $CFG;

        if (is_numeric($register)) {
            $register = get_record('register','id',$register);
        }
    
        $status = true;

        //Start mod
        fwrite ($bf,start_tag("MOD",3,true));
        //Print register data
        fwrite ($bf,full_tag("ID",4,false,$register->id));
        fwrite ($bf,full_tag("MODTYPE",4,false,"register"));
        fwrite ($bf,full_tag("NAME",4,false,$register->name));
        fwrite ($bf,full_tag("TYPE",4,false,$register->type));
        fwrite ($bf,full_tag("OFFLINESESSIONS",4,false,$register->offlinesessions));
        fwrite ($bf,full_tag("LASTLOGSCALC",4,false,$register->lastlogscalc));
        fwrite ($bf,full_tag("TOLERANCEEVALUATION",4,false,$register->toleranceevaluation));
        fwrite ($bf,full_tag("SESSIONTIMEOUT",4,false,$register->sessiontimeout));
        fwrite ($bf,full_tag("DAYSCERTIFICABLE",4,false,$register->dayscertificable));
        fwrite ($bf,full_tag("OFFLINECOMMENTS",4,false,$register->offlinecomments));
        fwrite ($bf,full_tag("MANDATORYOFFLINECOMMENTS",4,false,$register->mandatoryofflinecomm));
        fwrite ($bf,full_tag("OFFLINESPECIFYCOURSE",4,false,$register->offlinespecifycourse));
        fwrite ($bf,full_tag("MANDATORYOFFLINESPECIFYCOURSE",4,false,$register->mandofflspeccourse));
        fwrite ($bf,full_tag("SHOWTOLERANCE",4,false,$register->showtolerance));

        //if we've selected to backup users info, then execute backup_offline sessions
        if (backup_userdata_selected($preferences,'register',$register->id)) {
            $status = backup_register_sessions($bf,$preferences,$register->id);
        }
        //End mod
        $status =fwrite ($bf,end_tag("MOD",3,true));

        return $status;
    }

    //backup register_offline sessions contents (executed from register_backup_mods)
    function backup_register_sessions ($bf,$preferences,$register) {

        global $CFG;

        $status = true;

        $register_sessions = get_records("register_logs","register",$register,"id");
        //If there are sessions
        if ($register_sessions) {            
            //Write start tag
            $status =fwrite ($bf,start_tag("SESSIONS",4,true));
            //Iterate over each answer
            foreach ($register_sessions as $reg_ses) {
                //Start option
                if ($reg_ses->online != false) continue;
                $status =fwrite ($bf,start_tag("OFFLINESESSION",5,true));
                if (!empty($reg_ses->refcourse)) $refcourse = get_record ('course','id',$reg_ses->refcourse);
                //Print option contents
                fwrite ($bf,full_tag("ID",6,false,$reg_ses->id));
                fwrite ($bf,full_tag("LOGIN",6,false,$reg_ses->login));
                fwrite ($bf,full_tag("LOGOUT",6,false,$reg_ses->logout));
                fwrite ($bf,full_tag("TOLERANCE",6,false,$reg_ses->tolerance));
                fwrite ($bf,full_tag("USERID",6,false,$reg_ses->userid));
                fwrite ($bf,full_tag("REF_COURSE",6,false,$reg_ses->refcourse));
                if (!empty($reg_ses->refcourse)) {
                    fwrite ($bf,full_tag("REF_COURSE_SHORTNAME",6,false,$refcourse->shortname));
                    fwrite ($bf,full_tag("REF_COURSE_FULLNAME",6,false,$refcourse->fullname));
                }
                fwrite ($bf,full_tag("COMMENTS",6,false,$reg_ses->comments));
                //End answer
                $status =fwrite ($bf,end_tag("OFFLINESESSION",5,true));
            }
            //Write end tag
            $status =fwrite ($bf,end_tag("SESSIONS",4,true));
        }
        return $status;
    }
   
   ////Return an array of info (name,value)
   function register_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {

        if (!empty($instances) && is_array($instances) && count($instances)) {
            $info = array();
            foreach ($instances as $id => $instance) {
                $info += register_check_backup_mods_instances($instance,$backup_unique_code);
            }
            return $info;
        }
        //First the course data
        $info[0][0] = get_string("modulenameplural","register");
        if ($ids = register_ids ($course)) {
            $info[0][1] = count($ids);
        } else {
            $info[0][1] = 0;
        }

        //Now, if requested, the user_data
        if ($user_data) {
            $info[1][0] = get_string("sessions","register");
            if ($ids = register_session_ids_by_course ($course)) {
                $info[1][1] = count($ids);
            } else {
                $info[1][1] = 0;
            }
        }
        return $info;
    }

   ////Return an array of info (name,value)
   function register_check_backup_mods_instances($instance,$backup_unique_code) {
        //First the course data
        $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
        $info[$instance->id.'0'][1] = '';

        //Now, if requested, the user_data
        if (!empty($instance->userdata)) {
            $info[$instance->id.'1'][0] = get_string("sessions","register");
            if ($ids = register_session_ids_by_instance ($instance->id)) {
                $info[$instance->id.'1'][1] = count($ids);
            } else {
                $info[$instance->id.'1'][1] = 0;
            }
        }
        return $info;
    }

    //Return a content encoded to support interactivities linking. Every module
    //should have its own. They are called automatically from the backup procedure.
    function register_encode_content_links ($content,$preferences) {

        global $CFG;

        $base = preg_quote($CFG->wwwroot,"/");

        //Link to the list of registers
        $buscar="/(".$base."\/mod\/register\/index.php\?id\=)([0-9]+)/";
        $result= preg_replace($buscar,'$@REGISTERINDEX*$2@$',$content);

        //Link to register view by moduleid
        $buscar="/(".$base."\/mod\/register\/view.php\?id\=)([0-9]+)/";
        $result= preg_replace($buscar,'$@REGISTERVIEWBYID*$2@$',$result);

        return $result;
    }

    // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE

    //Returns an array of registers id
    function register_ids ($course) {

        global $CFG;

        return get_records_sql ("SELECT a.id, a.course
                                 FROM {$CFG->prefix}register a
                                 WHERE a.course = '$course'");
    }
   
    //Returns an array of register_answers id
    function register_session_ids_by_course ($course) {

        global $CFG;

        return get_records_sql ("SELECT l.id , l.register
                                 FROM {$CFG->prefix}register_logs l,
                                      {$CFG->prefix}register a
                                 WHERE a.course = '$course' AND
                                       l.register = a.id AND l.online=0");
    }

    //Returns an array of register_answers id
    function register_session_ids_by_instance ($instanceid) {

        global $CFG;

        return get_records_sql ("SELECT s.id , s.register
                                 FROM {$CFG->prefix}register_logs s
                                 WHERE s.register = $instanceid AND s.online=0");
    }
?>
