Skip to content

Commit

Permalink
question-export command
Browse files Browse the repository at this point in the history
  • Loading branch information
mi7chal committed Oct 22, 2024
1 parent 079351f commit e9361a4
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
105 changes: 105 additions & 0 deletions Moosh/Command/Moodle41/Question/QuestionExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* moosh - Moodle Shell
*
* @copyright 2012 onwards Tomasz Muras
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace Moosh\Command\Moodle41\Question;

use Moosh\MooshCommand;

/**
* Command exports all question. It may filter them by categories or courses.
*/
class QuestionExport extends MooshCommand
{
public function __construct()
{
parent::__construct('export', 'question');

$this->addOption('c|course:', 'Specifies questions course id.');
$this->addOption('f|filename:', 'Specifies exported file name.', 'moosh-questions-export');
$this->addOption('C|category:', 'Specifies questions category id.');
}

public function execute() {
global $DB, $CFG;

$courseId = $this->expandedOptions['course'];
$categoryId = $this->expandedOptions['category'];
$fileName = $this->expandedOptions['filename'];

// moodle queries questions using similar requests, it is needed due to complicated structure
$sql = "
SELECT q.id AS id, q.name AS name, qc.id AS categoryId, q.questiontext as text
FROM {question} q
JOIN {question_versions} qv ON q.id = qv.questionid
JOIN {question_bank_entries} qbe ON qv.questionbankentryid = qbe.id
JOIN {question_categories} qc ON qbe.questioncategoryid = qc.id
JOIN {context} ctx ON qc.contextid = ctx.id
JOIN {course} c ON ctx.instanceid = c.id
WHERE (qc.id = '$categoryId' or '$categoryId' = '')
and (c.id = '$courseId' or '$courseId' = '')
";

$questions = $DB->get_records_sql($sql);

if(!string_ends_with($fileName, ".json")) {
$fileName = $fileName . ".json";
}

$fullQuestions = $this->loadAnswers($questions);

$json = json_encode($fullQuestions);

file_put_contents($fileName, $json);
}

/**
* Loads answer for question array and returns it.
* @param stdClass[] $questions
* @return stdClass[]
*/
public function loadAnswers($questions) {
global $DB;

$questionsWithAnswers = array();

foreach ($questions as $question) {
$correctAnswer = $DB->get_record('question_answers', array('question' => $question->id, 'fraction' => 1.0));
$question->answer = $correctAnswer->answer;

$questionsWithAnswers[] = $this->formatQuestion($question);
}

return $questionsWithAnswers;
}

/**
* Removes html tags from question text and answer
* @param \stdClass $question
* @return \stdClass
*/
public function formatQuestion($question) {
$question->text = $this->stripAndTrim($question->text);
$question->answer = $this->stripAndTrim($question->answer);

return $question;
}

/**
* Strips html from string and trims it. If null given returns null.
* @param string $string
* @return null|string
*/
public function stripAndTrim($string) {
if(isset($string)) {
$string = trim(strip_tags($string));
}

return $string;
}

}
1 change: 1 addition & 0 deletions moosh-questions-export
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"1":{"id":"1","parent":"0","name":"Question 1","questiontext":"<p>WOOOO?<\/p>","questiontextformat":"1","generalfeedback":"","generalfeedbackformat":"1","defaultmark":"1.0000000","penalty":"1.0000000","qtype":"truefalse","length":"1","stamp":"localhost+240828104454+yH83pv","timecreated":"1724841894","timemodified":"1724841894","createdby":"2","modifiedby":"2"},"2":{"id":"2","parent":"0","name":"My quest","questiontext":"<p>ion hey<\/p>","questiontextformat":"1","generalfeedback":"","generalfeedbackformat":"1","defaultmark":"1.0000000","penalty":"0.3333333","qtype":"multichoice","length":"1","stamp":"localhost+241022094604+DiUHA8","timecreated":"1729590364","timemodified":"1729590364","createdby":"2","modifiedby":"2"}}
1 change: 1 addition & 0 deletions moosh-questions-export.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"id":"1","name":"Question 1","categoryid":"3","text":"WOOOO?","answer":"False"},{"id":"2","name":"My quest","categoryid":"8","text":"ion hey","answer":"My ans"}]
21 changes: 21 additions & 0 deletions www/commands/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2065,6 +2065,27 @@ Example:

moosh plugin-uninstall theme_elegance

question-export
---------------

Exports all or selected questions.

Parameters

| Option | Description |
|----------------|-----------------------------------------------|
| -C, --category | Questions category id. |
| -c, --course | Questions course id. |
| -f, --filename | Exported file name or path without extension. |

Example: export all questions

moosh question-export

Example: export questions with category with id 3 to file my-questions.json

moosh question-export -C 3 -f my-questions

question-import
---------------

Expand Down

0 comments on commit e9361a4

Please sign in to comment.