Skip to content

Commit

Permalink
Merge pull request #93 from KQMATH/bug-92-no-improv-questions
Browse files Browse the repository at this point in the history
Bug 92 no improv questions
  • Loading branch information
hgeorgsch authored Sep 30, 2022
2 parents f453aae + deb3758 commit 94c531f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 15 deletions.
2 changes: 2 additions & 0 deletions ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
function show_all_improvise_questions(jazzquiz $jazzquiz) {
$improviser = new improviser($jazzquiz);
$questionrecords = $improviser->get_all_improvised_question_definitions();
debugging( 'show'.$questionrecords );
if (!$questionrecords) {
debugging( 'No improvisation questions' );
return [
'status' => 'error',
'message' => 'No improvisation questions'
Expand Down
2 changes: 1 addition & 1 deletion backup/moodle2/backup_jazzquiz_activity_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function define_my_steps() {
* @param string $content
* @return string of content with the URLs encoded
*/
static public function encode_content_links($content) {
public static function encode_content_links($content) {
global $CFG;
$base = preg_quote($CFG->wwwroot, '/');
// Link to the list of JazzQuizes.
Expand Down
8 changes: 4 additions & 4 deletions backup/moodle2/restore_jazzquiz_activity_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function define_my_steps() {
/**
* @return restore_decode_content[]
*/
static public function define_decode_contents() {
public static function define_decode_contents() {
return [
new restore_decode_content('jazzquiz', ['intro'])
];
Expand All @@ -50,7 +50,7 @@ static public function define_decode_contents() {
/**
* @return restore_decode_rule[]
*/
static public function define_decode_rules() {
public static function define_decode_rules() {
return [
new restore_decode_rule('JAZZQUIZVIEWBYID', '/mod/jazzquiz/view.php?id=$1', 'course_module'),
new restore_decode_rule('JAZZQUIZINDEX', '/mod/jazzquiz/index.php?id=$1', 'course')
Expand All @@ -60,14 +60,14 @@ static public function define_decode_rules() {
/**
* @return restore_log_rule[]
*/
static public function define_restore_log_rules() {
public static function define_restore_log_rules() {
return [];
}

/**
* @return restore_log_rule[]
*/
static public function define_restore_log_rules_for_course() {
public static function define_restore_log_rules_for_course() {
return [];
}

Expand Down
77 changes: 68 additions & 9 deletions classes/improviser.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,19 @@ public function get_all_improvised_question_definitions() {
$questions = [];
$context = \context_module::instance($this->jazzquiz->cm->id);
$parts = explode('/', $context->path);
debugging( "get_all_improvised: ".$parts ) ;
foreach ($parts as $part) {
// Selecting name first, to prevent duplicate improvise questions.
debugging( "get_all_improvised: ".$part ) ;
$sql = "SELECT q.name,
q.id
FROM {question} q
JOIN {question_versions} qv
ON q.id = qv.questionid
JOIN {question_bank_entries} qbe
ON qbe.id = qv.questionbankentryid
JOIN {question_categories} qc
ON qc.id = q.category
ON qc.id = qbe.questioncategoryid
JOIN {context} ctx
ON ctx.id = qc.contextid
AND ctx.path LIKE :path
Expand Down Expand Up @@ -120,12 +126,30 @@ private function get_default_question_category() {
$context = \context_module::instance($this->jazzquiz->cm->id);
$category = question_get_default_category($context->id);
if (!$category) {
$contexts = new \question_edit_contexts($context);
$contexts = new \core_question\local\bank\question_edit_contexts($context);
$category = question_make_default_categories($contexts->all());
}
return $category;
}

/**
* Create a question bank entry for the database.
* @return \stdClass | null
*/
private function make_generic_question_bank_entry() {
$category = $this->get_default_question_category();
if (!$category) {
return null;
}

/* debugging( "Making generic question bank entry." ) ; */
$question = new \stdClass();
$question->questioncategoryid = $category->id;
$question->ownerid = null;
$question->idnumber = null;
return $question;
}

/**
* Create a question database object.
* @param string $qtype What question type to create
Expand All @@ -134,18 +158,15 @@ private function get_default_question_category() {
*/
private function make_generic_question_definition(string $qtype, string $name) {
if (!$this->question_type_exists($qtype)) {
/* debugging( "make_generic_question_definition: question type does not exist" ) ; */
return null;
}
$existing = $this->get_improvised_question_definition($name);
if ($existing !== false) {
return null;
}
$category = $this->get_default_question_category();
if (!$category) {
/* debugging( "make_generic_question_definition: existing: ".$name ) ; */
return null;
}
$question = new \stdClass();
$question->category = $category->id;
$question->parent = 0;
$question->name = '{IMPROV}' . $name;
$question->questiontext = ' ';
Expand All @@ -157,8 +178,6 @@ private function make_generic_question_definition(string $qtype, string $name) {
$question->qtype = $qtype;
$question->length = 1;
$question->stamp = '';
$question->version = '';
$question->hidden = 0;
$question->timecreated = time();
$question->timemodified = $question->timecreated;
$question->createdby = null;
Expand Down Expand Up @@ -225,11 +244,19 @@ private function make_generic_question_answer($questionid, string $format, strin
*/
private function insert_multichoice_question_definition(string $name, array $answers) {
global $DB;
/* debugging("insert_multichoice_question_definition: ".$name ) ; */
$question = $this->make_generic_question_definition('multichoice', $name);
if (!$question) {
return;
}
/* debugging("insert_multichoice_question_definition 2") ; */
$qbankentry = $this->make_generic_question_bank_entry();
if (!$qbankentry) {
return;
}
$question->id = $DB->insert_record('question', $question);
$qbankentry->id = $DB->insert_record('question_bank_entries', $qbankentry);
$this->insert_question_version( $qbankentry->id, $question->id ) ;
// Add options.
$options = $this->make_multichoice_options($question->id);
$DB->insert_record('qtype_multichoice_options', $options);
Expand All @@ -238,6 +265,23 @@ private function insert_multichoice_question_definition(string $name, array $ans
$qanswer = $this->make_generic_question_answer($question->id, 1, $answer);
$DB->insert_record('question_answers', $qanswer);
}
/* debugging( "Question ID ".$question->id ); */
}

/* Create a question version database object, linking the given question
* and question bank entry.
* @param int $qbankid The ID of the question banke entry
* @param int $qid The ID of the question
* @return \stdClass
*/
private function insert_question_version(int $qbankid, int $qid) {
global $DB;
$qv = new \stdClass();
$qv->questionbankentryid = $qbankid ;
$qv->version = 1 ;
$qv->questionid = $qid ;
$qv->id = $DB->insert_record('question_versions', $qv);
return $qv;
}

/**
Expand All @@ -246,11 +290,19 @@ private function insert_multichoice_question_definition(string $name, array $ans
*/
private function insert_shortanswer_question_definition(string $name) {
global $DB;
/* debugging("insert_shortanswer_question_definition: ".$name ) ; */
$question = $this->make_generic_question_definition('shortanswer', $name);
if (!$question) {
return;
}
$qbankentry = $this->make_generic_question_bank_entry();
if (!$qbankentry) {
return;
}
$question->id = $DB->insert_record('question', $question);
$qbankentry->id = $DB->insert_record('question_bank_entries', $qbankentry);
$this->insert_question_version( $qbankentry->id, $question->id ) ;

// Add options.
$options = $this->make_shortanswer_options($question->id);
$DB->insert_record('qtype_shortanswer_options', $options);
Expand All @@ -261,11 +313,18 @@ private function insert_shortanswer_question_definition(string $name) {

private function insert_shortmath_question_definition(string $name) {
global $DB;
/* debugging("insert_shortmath_question_definition: ".$name ) ; */
$question = $this->make_generic_question_definition('shortmath', $name);
if (!$question) {
return;
}
$qbankentry = $this->make_generic_question_bank_entry();
if (!$qbankentry) {
return;
}
$question->id = $DB->insert_record('question', $question);
$qbankentry->id = $DB->insert_record('question_bank_entries', $qbankentry);
$this->insert_question_version( $qbankentry->id, $question->id ) ;
// Add options. Important: If shortmath changes options table in the future, this must be changed too.
$options = $this->make_shortanswer_options($question->id);
$DB->insert_record('qtype_shortmath_options', $options);
Expand Down
2 changes: 1 addition & 1 deletion reports.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function jazzquiz_reports() {
if (!$isdownload) {
$jazzquiz->renderer->header($jazzquiz, 'reports');
}
$sessionid = optional_param('sessionid', 0, PARAM_INT);
$sessionid = optional_param('sessionid', 0, PARAM_INT);
switch ($action) {
case 'view':
jazzquiz_view_session_report($jazzquiz, $url, $sessionid);
Expand Down

0 comments on commit 94c531f

Please sign in to comment.