From a79d43fe9c21d7a3f1a4589627c91c27a53a3118 Mon Sep 17 00:00:00 2001 From: Jamie Pratt Date: Sun, 1 Apr 2012 17:30:42 +0700 Subject: [PATCH 1/3] MDL-47494 ddmarker: NOBUG adding a condition to catch if there are no questions to convert --- .../type/ddmarker/imagetargetconverter.php | 69 ++++++++++--------- .../type/ddmarker/lang/en/qtype_ddmarker.php | 1 + 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/question/type/ddmarker/imagetargetconverter.php b/question/type/ddmarker/imagetargetconverter.php index 19667a43c34..364d6451381 100644 --- a/question/type/ddmarker/imagetargetconverter.php +++ b/question/type/ddmarker/imagetargetconverter.php @@ -162,41 +162,44 @@ $sql = 'SELECT q.*, cat.contextid '.$from.$where.'ORDER BY cat.id, q.name'; $questions = $DB->get_records_sql($sql, $params); -$contextids = array(); -foreach ($questions as $question) { - $contextids[] = $question->contextid; -} - -$contextlist = new qtype_ddmarker_context_list(array_unique($contextids)); -$categorylist = new qtype_ddmarker_category_list($contextids, $contextlist); -$questionlist = new qtype_ddmarker_question_converter_list($questions, $categorylist); - -foreach ($questions as $question) { - $questionlist->leaf_node($question->id, 1); -} -$questionsselected = (bool) ($categoryid || $qcontextid || $questionid); -if ($questionid) { - $top = $questionlist->get_instance($questionid); -} else if ($categoryid) { - $top = $categorylist->get_instance($categoryid); -} else if ($qcontextid) { - $top = $contextlist->get_instance($qcontextid); +if (!count($questions)) { + echo html_writer::tag('div', get_string('noquestionsfound', 'qtype_ddmarker')); } else { - $top = $contextlist->root_node(); -} -if (!$confirm) { - if ($questionsselected) { - echo $contextlist->render('listitemaction', false, $top); - $cofirmedurl = new moodle_url($PAGE->url, compact('categoryid', 'contextid', 'questionid') + array('confirm'=>1)); - $cancelurl = new moodle_url($PAGE->url); - echo $OUTPUT->confirm(get_string('confirmimagetargetconversion', 'qtype_ddmarker'), $cofirmedurl, $cancelurl); - } else { - echo $contextlist->render('listitemlist', true, $top); + $contextids = array(); + foreach ($questions as $question) { + $contextids[] = $question->contextid; } -} else if (confirm_sesskey()) { - $questionlist->prepare_for_processing($top); - $top->process(); -} + $contextlist = new qtype_ddmarker_context_list(array_unique($contextids)); + $categorylist = new qtype_ddmarker_category_list($contextids, $contextlist); + $questionlist = new qtype_ddmarker_question_converter_list($questions, $categorylist); + + foreach ($questions as $question) { + $questionlist->leaf_node($question->id, 1); + } + $questionsselected = (bool) ($categoryid || $qcontextid || $questionid); + if ($questionid) { + $top = $questionlist->get_instance($questionid); + } else if ($categoryid) { + $top = $categorylist->get_instance($categoryid); + } else if ($qcontextid) { + $top = $contextlist->get_instance($qcontextid); + } else { + $top = $contextlist->root_node(); + } + if (!$confirm) { + if ($questionsselected) { + echo $contextlist->render('listitemaction', false, $top); + $cofirmedurl = new moodle_url($PAGE->url, compact('categoryid', 'contextid', 'questionid') + array('confirm'=>1)); + $cancelurl = new moodle_url($PAGE->url); + echo $OUTPUT->confirm(get_string('confirmimagetargetconversion', 'qtype_ddmarker'), $cofirmedurl, $cancelurl); + } else { + echo $contextlist->render('listitemlist', true, $top); + } + } else if (confirm_sesskey()) { + $questionlist->prepare_for_processing($top); + $top->process(); + } +} // Footer. echo $OUTPUT->footer(); diff --git a/question/type/ddmarker/lang/en/qtype_ddmarker.php b/question/type/ddmarker/lang/en/qtype_ddmarker.php index 41211b816c1..aa4cc9263b9 100644 --- a/question/type/ddmarker/lang/en/qtype_ddmarker.php +++ b/question/type/ddmarker/lang/en/qtype_ddmarker.php @@ -69,6 +69,7 @@ $string['marker'] = 'Marker'; $string['marker_n'] = 'Marker {no}'; $string['markers'] = 'Markers'; $string['nolabel'] = 'No label text'; +$string['noquestionsfound'] = 'No questions found to convert here.'; $string['pleasedragatleastonemarker'] = 'Your answer is not complete, you must place at least one marker on the image.'; $string['pluginname'] = 'Drag and drop markers'; $string['previewarea'] = 'Preview area -'; From 35c7dea6d201de9fd72ba5920119ffe09257e3af Mon Sep 17 00:00:00 2001 From: Jamie Pratt Date: Mon, 2 Apr 2012 16:33:09 +0700 Subject: [PATCH 2/3] MDL-47494 ddmarker: NOBUG added script needed for upgrading attempt data from imagetarget qs --- question/type/ddmarker/db/upgradelib.php | 85 ++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 question/type/ddmarker/db/upgradelib.php diff --git a/question/type/ddmarker/db/upgradelib.php b/question/type/ddmarker/db/upgradelib.php new file mode 100644 index 00000000000..2ab9118ddb7 --- /dev/null +++ b/question/type/ddmarker/db/upgradelib.php @@ -0,0 +1,85 @@ +. + +/** + * Upgrade library code for the ddmarker question type. This will only get triggered by the code + * to convert imagetarget questions to ddmarker. + * + * @package qtype + * @subpackage ddmarker + * @copyright 2012 Jamie Pratt + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + + +defined('MOODLE_INTERNAL') || die(); + + +/** + * Class for converting attempt data from imagetarget questions when converting + * attempts to the new question engine. + * + * This class is used by the code in question/engine/upgrade/upgradelib.php. + * + * @copyright 2010 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class qtype_ddmarker_qe2_attempt_updater extends question_qtype_attempt_updater { + public function right_answer() { + $drag = reset($this->question->options->drags); + return '{'.get_string('dropzone', 'qtype_ddmarker', '1')." -> ".$drag->label.'}'; + } + + public function was_answered($state) { + return !empty($state->answer); + } + + public function response_summary($state) { + if (!empty($state->answer)) { + $drag = reset($this->question->options->drags); + foreach ($this->question->options->drops as $drop) { + list($xy, $wh) = explode(';', $drop->coords); + list($x, $y) = explode(',', $xy); + list($w, $h) = explode(',', $wh); + list($answerx, $answery) = explode(',', $state->answer); + if (($answerx >= $x && $answerx <= ($x + $w)) && ($answery >= $y && $answery <= ($y + $h))) { + return '{'.get_string('dropzone', 'qtype_ddmarker', $drop->no)." -> ".$drag->label.'}'; + } + } + return ''; + } else { + return null; + } + } + + public function question_summary() { + $drag = reset($this->question->options->drags); + return parent::question_summary().'[['.get_string('dropzone', 'qtype_ddmarker', '1')."]] -> {".$drag->label.'}'; + } + + public function set_first_step_data_elements($state, &$data) { + $data['_choiceorder1'] = '1'; + } + + public function supply_missing_first_step_data(&$data) { + } + + public function set_data_elements_for_step($state, &$data) { + if (!empty($state->answer)) { + $data['c1'] = $state->answer; + } + } +} From 5153be2b26f32dfd245e71ecbda2360e39dc80f3 Mon Sep 17 00:00:00 2001 From: Jamie Pratt Date: Mon, 2 Apr 2012 17:20:12 +0700 Subject: [PATCH 3/3] MDL-47494 ddmarker: NOBUG not using list progress trace any more as it is has a bug --- .../type/ddmarker/imagetargetconverter.php | 2 ++ question/type/ddmarker/questionlists.php | 35 +++++++++++-------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/question/type/ddmarker/imagetargetconverter.php b/question/type/ddmarker/imagetargetconverter.php index 364d6451381..6533917d38b 100644 --- a/question/type/ddmarker/imagetargetconverter.php +++ b/question/type/ddmarker/imagetargetconverter.php @@ -198,7 +198,9 @@ if (!count($questions)) { } } else if (confirm_sesskey()) { $questionlist->prepare_for_processing($top); + echo '
    '; $top->process(); + echo '
'; } } // Footer. diff --git a/question/type/ddmarker/questionlists.php b/question/type/ddmarker/questionlists.php index af4668d8a3b..9f8a0ba9f4d 100644 --- a/question/type/ddmarker/questionlists.php +++ b/question/type/ddmarker/questionlists.php @@ -46,7 +46,11 @@ abstract class qtype_ddmarker_list_item { abstract protected function parent_node (); - abstract public function render($stringidentifier, $link); + public function render($stringidentifier, $link) { + return $this->render_item($stringidentifier, $link).$this->render_children($stringidentifier, $link); + } + + abstract protected function render_item($stringidentifier, $link); public function leaf_to_root($qcount) { $this->qcount += $qcount; @@ -57,19 +61,20 @@ abstract class qtype_ddmarker_list_item { } } - public function process($progresstrace = null, $depth = 0) { - if (null === $progresstrace) { - $progresstrace = new html_list_progress_trace(); - } - $progresstrace->output($this->render('listitemprocessing', false), $depth); - $this->process_children($progresstrace, $depth); + public function process() { + echo '
  • '; + echo $this->render_item('listitemprocessing', false); + $this->process_children(); + echo '
  • '; + flush(); } - protected function process_children($progresstrace, $depth) { - $children = array(); + protected function process_children() { + echo '
      '; foreach ($this->children as $child) { - $child->process($progresstrace, $depth + 1); + $child->process(); } + echo '
    '; } public function question_ids() { @@ -120,7 +125,7 @@ class qtype_ddmarker_category_list_item extends qtype_ddmarker_list_item { } } - public function render ($stringidentifier, $link) { + protected function render_item ($stringidentifier, $link) { global $PAGE; $a = new stdClass(); $a->qcount = $this->qcount; @@ -131,7 +136,7 @@ class qtype_ddmarker_category_list_item extends qtype_ddmarker_list_item { $thisitem = html_writer::tag('a', $thisitem, array('href' => $actionurl)); } - return $thisitem.$this->render_children($stringidentifier, $link); + return $thisitem; } } class qtype_ddmarker_question_list_item extends qtype_ddmarker_list_item { @@ -146,7 +151,7 @@ class qtype_ddmarker_question_list_item extends qtype_ddmarker_list_item { return $this->parentlist->get_instance($this->record->category); } - public function render ($stringidentifier, $link) { + protected function render_item ($stringidentifier, $link) { global $PAGE; $a = new stdClass(); $a->name = $this->record->name; @@ -180,7 +185,7 @@ class qtype_ddmarker_context_list_item extends qtype_ddmarker_list_item { } } - public function render ($stringidentifier, $link) { + protected function render_item ($stringidentifier, $link) { global $PAGE; $a = new stdClass(); $a->qcount = $this->qcount; @@ -190,7 +195,7 @@ class qtype_ddmarker_context_list_item extends qtype_ddmarker_list_item { $actionurl = new moodle_url($PAGE->url, array('contextid'=> $this->record->id)); $thisitem = html_writer::tag('a', $thisitem, array('href' => $actionurl)); } - return $thisitem.$this->render_children($stringidentifier, $link); + return $thisitem; } public function course_context_id() {