Commit ccd47dcb authored by Cecilia Vela Gurovic's avatar Cecilia Vela Gurovic
Browse files

Bug 1886614: framework.json.php refactoring

refactoring the file that saves a framework to DB

- added all code to check and format the input into one function format_input_framework that will:
 get the $_POST parameters needed
 set types to fileds
 get priorities of the standards and elements to know the order they have in the form
 save the DB ids of standards and elements if they are included in the form input data, so later we can check which DB records need to be removed

- addedd a function remove_deleted_items_from_DB to deal with removing records from the DB

behatnotneeded

Change-Id: Ied46c620906e2833df3be21a08f7ffe7e6ac4193
parent 0dab74eb
Loading
Loading
Loading
Loading
+121 −152
Original line number Diff line number Diff line
@@ -9,8 +9,6 @@
 *
 */

//This file is for submitting a framework to the db

define('INTERNAL', 1);
define('JSON',1);

@@ -21,13 +19,77 @@ if (!PluginModuleFramework::is_active()) {
    json_reply(true, get_string('pluginnotactive1', 'error', get_string('frameworknav', 'module.framework')));
}

if (!$_POST) {
    //form not submitted
    json_reply(true, get_string('jsondatanotsubmitted', 'module.framework'));
// if the fw_id is set, we are editing an existing framework
$fw_to_edit = param_variable('fw_id', null);

// save the DB ids if set on the form data, to check if we need to delete any from the DB
$fw_sids = array();
$fw_seids = array();

$form_data = format_input_framework();

if (is_null($form_data)) {
    json_reply(true, get_string('invalidjsonineditor', 'module.framework'));
}
else {
    if (empty($form_data->name) || empty($form_data->standards)) {
        json_reply(true, get_string('jsonmissingvars', 'module.framework'));
    }
    else {
        // format standards structure, set standard elements inside standards
        if (isset($form_data->standardelements)) {
            foreach ($form_data->standards as $key => $standard) {
                if (!isset($form_data->standards[$key]->standardelement)) {
                    $form_data->standards[$key]->standardelement = array();
                }
                foreach ($form_data->standardelements as $element) {
                    if ($standard->standardid === $element->standardid) {
                        $form_data->standards[$key]->standardelement[] = $element;
                    }
                }
            }
            unset($form_data->standardelements);
        }

        //adding extra array required for update
        //@TODO these should really work the same.
        if ($fw_to_edit) {
            $stds = array('standards' => $form_data->standards);
            $form_data->standards = $stds;
        }
        //put ok content into session variable
        $SESSION->set('jsoneditorcontent', $form_data);
    }
}

if ($fw_to_edit) {
    $framework = new Framework($fw_to_edit, $form_data);
    remove_deleted_items_from_db($fw_to_edit);
}
else {
    //populate form_data with keys we expect to see
    $form_data = array_filter($_POST, function ($k) {
    $form_data->active = false;
    $framework = new Framework(null, $form_data);
}

$framework->commit();
if (!$fw_to_edit) {
    $framework->set_config_fields();
}

$data['id'] = $framework->get('id');
$data['institution'] = $form_data->institution;
$data['name'] = $framework->get('name');
// need to also add uid with DB id of standards and elements
// when we are saving for the first time (new framework or copy of existing one)

json_reply(false, (object) array(
    'message' => get_string('successmessage', 'module.framework'),
    'data' => $data)
);

function format_input_framework() {
    global $fw_sids, $fw_seids;
    $form_data = (object) array_filter($_POST, function ($k) {
        $expected_data = [
            'institution',
            'name',
@@ -36,154 +98,78 @@ else {
            'evidencestatuses',
            'standards',
            'standardelements',
            'fw_id',
            'sesskey'
        ];
        if (in_array($k, $expected_data)) {
            return $k;
        }
    }, ARRAY_FILTER_USE_KEY);
}

//if true, we are editing an existing framework
$fw_to_edit = param_variable('fw_id', null);
unset($form_data['fw_id']);

if ($form_data['institution'] != 'all') {
    $institutionname = get_field('institution', 'name', 'displayname', $form_data['institution']);
    $form_data['institution'] = $institutionname;
    if ($form_data->institution != 'all') {
        $form_data->institution = get_field('institution', 'name', 'displayname', $form_data->institution);
    }

 //change $selfassess from true/false string to true/false.
$selfassess = param_variable('selfassess', null);
if ($selfassess === "false") {
    $selfassess = false;
}
else if ($selfassess === "true") {
    $selfassess = true;
}
$form_data['selfassess'] = $selfassess;
    $form_data->selfassess = ($form_data->selfassess === "true" ? true : false);

$evidencestatuses = param_variable('evidencestatuses', null);

//@TODO - this is a mess - refactor!
//need to delete records if edit is true and there is an id missing.
//so get the ids expected from the db.
if ($fw_to_edit) {
    $db_sids = array();
    $db_seids = array();
    $db_stdels = array();

    if ($std_ids = get_records_array('framework_standard', 'framework', $fw_to_edit, '', 'id')) {
        foreach ($std_ids as $sid) {
            array_push($db_sids, $sid->id);
        }
    }

    foreach($db_sids as $sid) {
        $stdel_ids = get_records_array('framework_standard_element', 'standard', $sid, '', 'id');
        array_push($db_seids, $stdel_ids);
    $evidencestatuses = $form_data->evidencestatuses;
    $form_data->evidencestatuses = array();
    foreach ($evidencestatuses as $key => $es) {
        $form_data->evidencestatuses[] = (object) array($key => $es);
    }

    foreach ($db_seids as $seids) {
        if ($seids != null) {
            foreach ($seids as $seid) {
                if (isset($seid->id)) {
                    array_push($db_stdels, $seid->id);
                }
            }
    // calculate priority (order of standards and elements)
    $priority = 0;
    $standards = array();
    foreach ($form_data->standards as $std) {
        $priority ++;
        $std = (object) $std;
        $std->priority = $priority;
        if (isset($std->uid)) {
            $std->id = $std->uid;
            // save to later check if need to delete from DB
            array_push($fw_sids, $std->uid);
            unset($std->uid);
        }
        $standards[] = $std;
    }
    $form_data->standards = $standards;

    $fw_sids = array();
    foreach( $form_data['standards'] as &$std) {
        if (isset($std['uid'])) {
            array_push($fw_sids, $std['uid']);
    $priority = 0;
    $elements = array();
    foreach ($form_data->standardelements as $stdel) {
        $priority ++;
        $stdel = (object) $stdel;
        $stdel->priority = $priority;
        if (isset($stdel->uid)) {
            $stdel->id = $stdel->uid;
            // save to later check if need to delete from DB
            array_push($fw_seids, $stdel->uid);
            unset($stdel->uid);
        }
        $elements[] = $stdel;
    }
    $stds_to_delete = array_values(array_diff($db_sids, $fw_sids));
    $form_data->standardelements = (object) $elements;

    $fw_seids = array();
    foreach ($form_data['standardelements'] as &$se) {
        if (isset($se['uid'])) {
            array_push($fw_seids, $se['uid']);
        }
    }
    $ses_to_delete = array();
    $ses_to_delete = array_values(array_diff($db_stdels, $fw_seids));
    return $form_data;
}

$std_count = 0;
foreach ($form_data['standards'] as &$std) {
    $std_count ++;
    $std['priority'] = $std_count;
    if (isset($std['uid'])) {
        if ($fw_to_edit) {
            $std['id'] = $std['uid'];
        }
        unset($std['uid']);
    }
}
$stdel_count = 0;
foreach ($form_data['standardelements'] as &$stdel) {
    $stdel_count ++;
    $stdel['priority'] = $stdel_count;
    if (isset($stdel['uid'])) {
        if ($fw_to_edit) {
            $stdel['id'] = $stdel['uid'];
        }
        unset($stdel['uid']);
    }
}
function remove_deleted_items_from_db($fw_to_edit) {
    global $fw_sids, $fw_seids;
    //need to delete records if edit is true and there is an id missing.
    //so get the ids expected from the db.
    // get standard ids from the DB
    $db_sids = array();
    $db_sids = get_column('framework_standard', 'id', 'framework', $fw_to_edit);

$matrix = json_encode($form_data);
$content = json_decode($matrix);
    // get standard element ids from the DB
    $db_stdels = array();
    $sql = 'SELECT id FROM {framework_standard_element}
        WHERE standard IN (' . join(',', array_fill(0, count($db_sids), '?')) . ')';
    $db_stdels = get_column_sql($sql, $db_sids);

if (is_null($content)) {
    $ok['error'] = true;
    $ok['message'] = get_string('invalidjsonineditor', 'module.framework');
}
else {
    $content->evidencestatuses = array();
    $count = 0;
    foreach ($evidencestatuses as $key => $es) {
        $obj = new stdClass;
        $obj->$key = $es;
        $content->evidencestatuses[$count] = $obj;
        $count ++;
    }
    if (empty($content->name) || empty($content->standards)) {
        $ok['error'] = true;
        $ok['message'] = get_string('jsonmissingvars', 'module.framework');
    }
    else {
        $ok['content'] = $content;
        if (isset($content->standardelements)) {
            foreach ($content->standards as $key => $standard) {
                foreach ($content->standardelements as $k => $element) {
                    if ($standard->standardid === $element->standardid) {
                        if (!isset($content->standards[$key]->standardelement)) {
                            $content->standards[$key]->standardelement = array();
                        }
                        $content->standards[$key]->standardelement[] = $element;
                    }
                }
            }
            unset($content->standardelements);
        }
        //adding extra array required for update
        //@TODO these should really work the same.
        if ($fw_to_edit) {
            $stds = array('standards' => $content->standards);
            $content->standards = $stds;
        }
        //put ok content into session variable
        $SESSION->set('jsoneditorcontent', $content);
    }
}
    $stds_to_delete = array_values(array_diff($db_sids, $fw_sids));
    $ses_to_delete = array_values(array_diff($db_stdels, $fw_seids));

if ($fw_to_edit) {
    $framework = new Framework($fw_to_edit, $content);
    if ($ses_to_delete) {
        foreach ($ses_to_delete as $se) {
            delete_records('framework_standard_element', 'id', $se);
@@ -197,20 +183,3 @@ if ($fw_to_edit) {
        }
    }
}
 No newline at end of file
else {
    $content->active = false;
    $framework = new Framework(null, $content);
}

$framework->commit();
if (!$fw_to_edit) {
    $framework->set_config_fields();
}

$data['id'] = $framework->get('id');
$data['institution'] = $form_data['institution'];
$data['name'] = $framework->get('name');

$message = get_string('successmessage', 'module.framework');

json_reply(false, (object) array('message' => $message, 'data' => $data));