Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DevMateria util to create a qset yaml from a CSV #34

Open
zachberry opened this issue Jun 19, 2015 · 2 comments
Open

DevMateria util to create a qset yaml from a CSV #34

zachberry opened this issue Jun 19, 2015 · 2 comments

Comments

@zachberry
Copy link
Member

This needs a bit more work, but I basically needed a way to create a qset file from an instructor's document. I decided to create a task to convert a CSV into a qset yaml file, however this will only work for short answer questions, and currently is somewhat hardcoded for Flash Cards

    // Accepts a csv file and turns it into a qset yaml file.
    // Only works for Question-Answer qsets (not multiple choice)
    // Requires CSV in the following format:
    // -------------------------------------
    //  "Q","A"
    //  "Your first question","Your first answer"
    //  "Your second question","Your second answer"
    //  ...
    // -------------------------------------
    // Additionally the file should have UNIX line endings
    // (If using Sublime Text, go to View->Line Endings->Unix)
    // Reading of the CSV file can be touchy, so you may
    // want to strip out non-printable characters in the CSV
    // Sed can do this:
    //      sed 's/[^[:print:]]|[^[\n]]//g' in.csv > out.csv
    public static function convert_csv_to_qset_yaml($csv_file)
    {
        // read in CSV
        $file_area = \File::forge(['basedir' => null]);
        $csv_data = \Format::forge($file_area->read($csv_file, true), 'csv')->to_array();
        // \Cli::write(print_r($csv_data, true));

        // go through each question and convert to a qset structure
        $items = [];
        foreach($csv_data as $q)
        {
            $item = [
                'materiaType' => 'question',
                'type' => 'QA',
                'id' => 0,
                'questions' => [ ['text' => $q['Q']] ],
                'assets' => [],
                'answers' => [[
                    'value' => '100',
                    'text' => $q['A'],
                    'id' => 0
                ]]
            ];

            $item['assets'][] = false;
            $item['assets'][] = false;

            $items[] = $item;

        }

        // create yaml
        // $qset = ['items' => $items ];
        $qset = [
            'rand' => false,
            'items' => [],
            'assets' => [],
            'options' => [],
            'name' => ''
        ];
        $qset['items'][] = ['items' => $items ];
        $yaml = \Format::forge($qset)->to_yaml();

        // write yaml
        $output_file = $csv_file.'_qset.yaml';
        if (file_exists($output_file))
        {
            $output_file = $csv_file.'_'.mt_rand().'_qset.yaml';
        }
        $file_area = \File::forge(['basedir' => null]);
        if ($file_area->create(DOCROOT, $output_file, $yaml))
        {
            \Cli::write($output_file.' created', 'green');
            self::quit();
        }

        \Cli::write(print_r($yaml, true));
    }
@iturgeon
Copy link
Member

This kinda feels like a support tool, not a server task. Maybe part of devMateria

@iturgeon iturgeon changed the title Task to create a qset yaml from a CSV DevMateria util to create a qset yaml from a CSV Jun 30, 2015
@iturgeon iturgeon transferred this issue from ucfopen/Materia Jan 17, 2019
@FrenjaminBanklin
Copy link
Contributor

Not sure about making a tool to generate a qset from a .csv file, but we could potentially build something to generate importable questions from a .csv file.

FrenjaminBanklin added a commit to FrenjaminBanklin/Materia-Widget-Dev-Kit that referenced this issue Mar 27, 2019
…o the output from the preflight checklist. Covered all checks so far.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants