}
     } else {
         if ($params['action'] == "createDefault") {
             echo "error";
             exit;
         }
         wimba_add_log(WIMBA_ERROR, voicepresentation_LOGS, "manageAction :Problem to add the new resource on the vt server");
         redirection($redirectionUrl . '&error=problem_vt');
     }
 } elseif ($params['action'] == 'update') {
     if ($params['type'] == "board") {
         $result = $vtAction->modifyBoard($params["resource_id"]);
         //create the resource on the vt
         if ($result != NULL && $result->error != "error") {
             if ($params['grade'] == 'true') {
                 $params['gradeid'] = voicepresentation_add_grade_column($result->getRid(), $params["enc_course_id"], $result->getTitle(), $params["points_possible"]);
             } else {
                 $resourceDb = get_record("voicepresentation_resources", "rid", $result->getRid());
                 if ($resourceDb->gradeid != -1) {
                     voicepresentation_delete_grade_column($result->getRid(), $params["enc_course_id"]);
                     $params['gradeid'] = -1;
                     //the resource will also be updated
                 }
             }
         }
         $messageAction = "updated";
         $messageProduct = "board";
     } elseif ($params['type'] == "presentation") {
         $result = $vtAction->modifyPresentation($params["resource_id"]);
         $messageAction = "updated";
         $messageProduct = "presentation";
function voicepresentation_delete_instance($id)
{
    /// Given an ID of an instance of this module,
    /// this function will permanently delete the instance
    /// and any data that depends on it.
    $result = true;
    if (!($voicetool = get_record("voicepresentation", "id", $id))) {
        return false;
    }
    # Delete any dependent records here #
    if (!($instanceNumber = delete_records("voicepresentation", "id", $voicetool->id))) {
        wimba_add_log(WIMBA_ERROR, voicepresentation_LOGS, "Problem to delete the instance : " . $voicetool->id);
        $result = false;
    }
    if ("voicepresentation" == "voiceboard") {
        $resource = get_record("voicepresentation_resources", "rid", $voicetool->rid);
        if ($resource->gradeid != -1) {
            $students = getStudentsEnrolled($voicetool->course);
            $users_key = array_keys($students);
            $oldgrade = grade_get_grades($voicetool->course, "mod", "voicepresentation", $resource->gradeid, $users_key);
            if (isset($oldgrade->items[0])) {
                //the activity linked to the grade is no longer available
                //If there is still an activity linked to the resource, we will use this one,
                //else we will use an random numer as instance number(the title of the column will not be a link)
                voicepresentation_delete_grade_column($resource->rid, $voicetool->course);
                $resource->gradeid = voicepresentation_add_grade_column($resource->rid, $voicetool->course, $oldgrade->items[0]->name, $oldgrade->items[0]->grademax, voicepresentation_build_gradeObject_From_ArrayOfGradeInfoObjects($oldgrade->items[0]->grades));
            }
            update_record('voicepresentation_resources', $resource);
        }
    }
    voicepresentation_deleteCalendarEvent($voicetool->id);
    // delete the related calendar event
    return $result;
}
function voicepresentation_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //Get record from backup_ids
    $data = backup_getid($restore->backup_unique_code, $mod->modtype, $mod->id);
    $userdata = restore_userdata_selected($restore, "voicepresentation", $mod->id);
    if ($data) {
        //Now get completed xmlized object
        $info = $data->info;
        $old_rid = $info['MOD']['#']['RID']['0']['#'];
        //add logs
        //Now, build the voicepresentation record structure
        if ("voicepresentation" == "voiceboard") {
            $typeCopy = "0";
            //we copy all the content
        } else {
            $typeCopy = "1";
            //we copy top messages
        }
        if ($userdata === true) {
            $copyOptions = $typeCopy;
            $resourceDbMatched = get_record("voicepresentation_resources", "fromrid", $old_rid, "course", $restore->course_id, "copyoptions", $typeCopy);
            //resource which match the current copy options
            $resourceDbOther = get_record("voicepresentation_resources", "fromrid", $old_rid, "course", $restore->course_id, "copyoptions", "2");
        } else {
            $copyOptions = "2";
            //delete all
            $resourceDbMatched = get_record("voicepresentation_resources", "fromrid", $old_rid, "course", $restore->course_id, "copyoptions", "2");
            $resourceDbOther = get_record("voicepresentation_resources", "fromrid", $old_rid, "course", $restore->course_id, "copyoptions", $typeCopy);
        }
        if (empty($resourceDbMatched)) {
            // the resource of the type needed was not created before
            $newResource = voicetools_api_copy_resource($old_rid, null, $copyOptions);
            if ($newResource === false) {
                return false;
                //error during the copy
            }
            $newResource = voicetools_api_get_resource($newResource->getRid());
            // get all the informations
            if ($newResource === false) {
                return false;
                //error to get the resouce
            }
            $newResourceOptions = $newResource->getOptions();
            $isGradable = $newResourceOptions->getGrade();
            $resourceId = $newResource->getRid();
            if (!empty($resourceDbOther)) {
                //the other type was created, need to update one name
                if ($copyOptions == $typeCopy) {
                    //we have to update the name of the new one
                    $newResource->setTitle($newResource->getTitle() . " with user data");
                    if (voicetools_api_modify_resource($newResource->getResource()) === false) {
                        return false;
                        //error to get the resouce
                    }
                    //save some parameters that we will used to manage the grade column
                    $title = $newResource->getTitle();
                    $ridForGrade = $newResource->getRid();
                    $pointsPossible = $newResourceOptions->getPointsPossible();
                    $actionGradebook = "create";
                    //we will only need to create the grade column with grades for the second resource.
                } else {
                    //we have to update the other which was the one with user data
                    $otherResource = voicetools_api_get_resource($resourceDbOther->rid);
                    if ($otherResource === false) {
                        return false;
                        //error to get the resouce
                    }
                    $otherResource->setTitle($otherResource->getTitle() . " with user data");
                    if (voicetools_api_modify_resource($otherResource->getResource()) === false) {
                        return false;
                        //error to get the resouce
                    }
                    $title = $otherResource->getTitle();
                    $ridForGrade = $otherResource->getRid();
                    $otherResourceOptions = $otherResource->getOptions();
                    $pointsPossible = $otherResourceOptions->getPointsPossible();
                    $actionGradebook = "update";
                    //we will only have to update the name of the grade column and create a new one
                }
                //we store the new resource in the database;
            }
            //update the moodle database
            voicepresentation_createResourceFromResource($old_rid, $resourceId, $restore->course_id, $copyOptions);
        } else {
            //the resource already exist
            $resourceId = $resourceDbMatched->rid;
            $isGradable = false;
        }
        $voicefeature->course = backup_todb($restore->course_id);
        $voicefeature->rid = backup_todb($resourceId);
        $voicefeature->name = str_replace(backup_todb($old_rid), $resourceId, backup_todb($info['MOD']['#']['NAME']['0']['#']));
        $voicefeature->section = backup_todb($info['MOD']['#']['SECTION']['0']['#']);
        $voicefeature->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        $voicefeature->isfirst = 1;
        //The structure is equal to the db, so insert the voicepresentation
        $newActivityId = insert_record("voicepresentation", $voicefeature);
        if ($isGradable == 'true') {
            //the old vb was gradable
            //the activity linked has changed due to the copy, we need to update it to be able to match the good grade column
            $vb = get_record("voicepresentation_resources", "rid", $resourceId);
            $vb->gradeid = $newActivityId;
            update_record("voicepresentation_resources", $vb);
            $oldResourceDb = get_record("voicepresentation_resources", "rid", $old_rid);
            $students = getStudentsEnrolled($oldResourceDb->course);
            $users_key = array_keys($students);
            //get the grade of the initial resource
            $gradesfromInitialResource = grade_get_grades($oldResourceDb->course, "mod", "voicepresentation", $oldResourceDb->gradeid, $users_key);
            $grades = null;
            if (isset($gradesfromInitialResource->items[0])) {
                $grades = voicepresentation_build_gradeObject_From_ArrayOfGradeInfoObjects($gradesfromInitialResource->items[0]->grades);
            }
            if (isset($actionGradebook) && $actionGradebook == "update") {
                //we update the name of the column (add "with user data")
                voicepresentation_delete_grade_column($ridForGrade, $restore->course_id, $newActivityId);
                //delete the one automatically created by moodle
                voicepresentation_add_grade_column($ridForGrade, $restore->course_id, $title, $pointsPossible, $grades);
                //we need to create the grade column with contains no grade( user data was unchecked);
                voicepresentation_add_grade_column($newResource->getRid(), $restore->course_id, $newResource->getTitle(), $newResourceCopyOptions->getPointsPossible());
            } else {
                if (isset($actionGradebook) && $actionGradebook == "create") {
                    voicepresentation_add_grade_column($ridForGrade, $restore->course_id, $title, $pointsPossible, $grades);
                }
            }
        }
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "voicepresentation") . " \"" . format_string(stripslashes($voicefeature->name), true) . "\"</li>";
        }
        backup_flush(300);
        if ($newActivityId) {
            //We have the newid, update backup_ids
            backup_putid($restore->backup_unique_code, $mod->modtype, $mod->id, $newActivityId);
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}