diff --git a/ajax.php b/ajax.php index 15319e9..416a4c5 100755 --- a/ajax.php +++ b/ajax.php @@ -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' diff --git a/backup/moodle2/backup_jazzquiz_activity_task.class.php b/backup/moodle2/backup_jazzquiz_activity_task.class.php index f3846df..11107dd 100644 --- a/backup/moodle2/backup_jazzquiz_activity_task.class.php +++ b/backup/moodle2/backup_jazzquiz_activity_task.class.php @@ -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. diff --git a/backup/moodle2/restore_jazzquiz_activity_task.class.php b/backup/moodle2/restore_jazzquiz_activity_task.class.php index 9d57a39..1d82d86 100644 --- a/backup/moodle2/restore_jazzquiz_activity_task.class.php +++ b/backup/moodle2/restore_jazzquiz_activity_task.class.php @@ -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']) ]; @@ -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') @@ -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 []; } diff --git a/classes/improviser.php b/classes/improviser.php index 962097b..cab8960 100755 --- a/classes/improviser.php +++ b/classes/improviser.php @@ -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 @@ -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 @@ -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 = ' '; @@ -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; @@ -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); @@ -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; } /** @@ -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); @@ -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); diff --git a/reports.php b/reports.php index 6c76726..79e25a2 100755 --- a/reports.php +++ b/reports.php @@ -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);