-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters