/*********************************** get a value for editing ************************/
if ($action == 'edit') {
    $valueid = required_param('valueid', PARAM_INT);
    $data = $DB->get_record($CFG->classification_value_table, array('id' => $valueid));
}
/*********************************** moves up ************************/
if ($action == 'up') {
    $id = required_param('valueid', PARAM_INT);
    classification_tree_up($id, $type);
    classification_tree_updateordering(0, $type);
}
/*********************************** moves down ************************/
if ($action == 'down') {
    $id = required_param('valueid', PARAM_INT);
    classification_tree_down($id, $type);
    classification_tree_updateordering(0, $type);
}
/************************************* Delete safe (only if not used) ******************/
if ($action == 'delete') {
    // todo check if there is no instances working with such type.
    // if not, bounce to forcedelete
    $action = 'forcedelete';
}
/************************************* Delete unsafe ******************/
if ($action == 'forcedelete') {
    $id = required_param('valueid', PARAM_INT);
    $value = $DB->get_record($CFG->classification_value_table, array('id' => $id));
    if ($value) {
        // delete related constraints
        $DB->delete_records($CFG->classification_constraint_table, array('value1' => $id));
        $DB->delete_records($CFG->classification_constraint_table, array('value2' => $id));
    $id = required_param('typeid', PARAM_INT);
    $typeorder = $DB->get_field($CFG->classification_type_table, 'sortorder', array('id' => $id));
    if (!$DB->delete_records($CFG->classification_type_table, array('id' => $id))) {
        print_error('could not delete classifier');
    }
    // clear all sub values
    if ($valueids = $DB->get_records_menu($CFG->classification_type_table, array('type' => $id), 'id,id')) {
        $DB->delete_records($CFG->classification_value_table, array($CFG->classification_value_type_key => $id));
        // clear constraint records
        $valueidslist = implode("','", array_keys($valueids));
        $DB->delete_records_select($CFG->classification_constraint_table, "value1 IN ('{$valueidslist}') ");
        $DB->delete_records_select($CFG->classification_constraint_table, "value2 IN ('{$valueidslist}') ");
        // clear course assignations
        $DB->delete_records_select($CFG->course_metadata_table, " {$CFG->course_metadata_value_key} IN ('{$valueidslist}') ");
    }
    classification_tree_updateordering($typeorder);
}
/**
* updates ordering of a tree branch from a specific node, reordering 
* all subsequent siblings. 
* @param id the node from where to reorder
* @param table the table-tree
*/
function classification_tree_updateordering($id)
{
    // getting ordering type of the current node
    global $CFG;
    $res = $DB->get_record($CFG->classification_type_table, array('id' => $id));
    if (!$res) {
        // fallback : we give the ordering
        $res->sortorder = $id;