Example #1
0
function voicepresentation_add_grade_column($rid, $course_id, $resource_name, $points_possible, $grades = null)
{
    //check if the resource is already linked to an activity
    $activities = get_records("voicepresentation", "rid", $rid);
    if (!empty($activities)) {
        $keys = array_keys($activities);
        $activity_id = $keys[0];
        //take the first one
    } else {
        srand((double) microtime() * 1000000);
        $activity_id = rand(1000000, 9000000);
        //we generate a big random number
    }
    if ($grades == null) {
        //check if there are some grades associated to the column
        $students = getStudentsEnrolled($course_id);
        $users_key = array_keys($students);
        $gradesData = grade_get_grades($course_id, "mod", "voicepresentation", $activity_id, $users_key);
        if (isset($gradesData->items[0])) {
            $grades = voicepresentation_build_gradeObject_From_ArrayOfGradeInfoObjects($gradesData->items[0]->grades);
        }
    }
    $grade_params = array('itemname' => $resource_name, 'grademax' => $points_possible);
    //we will use this column to get the grade item associated to the resource.
    grade_update("mod/voicepresentation", $course_id, "mod", "voicepresentation", $activity_id, 0, $grades, $grade_params);
    return $activity_id;
}
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;
}