Commit c3e6bd18 authored by Tim Hunt's avatar Tim Hunt
Browse files

MDL-20636 work-in-progress converting the ddwtos and gapselect qtypes.

parent 6e34cd29
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="question/type/ddwtos/db" VERSION="20080909" COMMENT="XMLDB file for Moodle question/type/ddwtos"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="question_ddwtos" COMMENT="Defines drag and drop (words into sentences) questions">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" ENUM="false" NEXT="questionid"/>
<FIELD NAME="questionid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="shuffleanswers"/>
<FIELD NAME="shuffleanswers" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="1" SEQUENCE="false" ENUM="false" PREVIOUS="questionid" NEXT="correctfeedback"/>
<FIELD NAME="correctfeedback" TYPE="text" LENGTH="small" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="shuffleanswers" NEXT="partiallycorrectfeedback"/>
<FIELD NAME="partiallycorrectfeedback" TYPE="text" LENGTH="small" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="correctfeedback" NEXT="incorrectfeedback"/>
<FIELD NAME="incorrectfeedback" TYPE="text" LENGTH="small" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="partiallycorrectfeedback" NEXT="shownumcorrect"/>
<FIELD NAME="shownumcorrect" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="incorrectfeedback"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="questionid"/>
<KEY NAME="questionid" TYPE="foreign" FIELDS="questionid" REFTABLE="questions" REFFIELDS="id" PREVIOUS="primary"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
\ No newline at end of file
<?php // $Id$
// This file keeps track of upgrades to
// the ddwtos qtype plugin
//
// Sometimes, changes between versions involve
// alterations to database structures and other
// major things that may break installations.
//
// The upgrade function in this file will attempt
// to perform all the necessary actions to upgrade
// your older installtion to the current version.
//
// If there's something it cannot do itself, it
// will tell you what you need to do.
//
// The commands in here will all be database-neutral,
// using the functions defined in lib/ddllib.php
function xmldb_qtype_ddwtos_upgrade($oldversion=0) {
global $CFG, $THEME, $db;
$result = true;
// Rename question_ddwtos table to question_ddwtos
// as it is too long for Oracle
if ($result && $oldversion < 2008121900) {
/// Define table course_extended_meta to be created
$table = new XMLDBTable('question_ddwordsintosentences');
/// Do nothing if table already exists (probably created from xml file)
if (table_exists($table)) {
/// Define table question_ddwtos to be renamed to NEWNAMEGOESHERE
$table = new XMLDBTable('question_ddwordsintosentences');
/// Launch rename table for question_ddwtos
$result = $result && rename_table($table, 'question_ddwtos');
}
}
if ($oldversion < 2009052000) {
$table = new XMLDBTable('question_ddanswers');
if (table_exists($table) && ($ddanswers = get_records('question_ddanswers'))) {
foreach($ddanswers as $ddanswer){
$answer = new stdClass;
$answer->question = $ddanswer->questionid;
$answer->answer = addslashes($ddanswer->answer);
$answer->fraction = 1;
$feedback = new stdClass;
$feedback->draggroup = $ddanswer->draggroup;
$feedback->infinite = $ddanswer->infinite;
$answer->feedback = serialize($feedback);
if(!insert_record('question_answers', $answer)){
notify('move_question_ddanswers_to_question_answers(): cannot insert row into question_answer table.');
return false;
}
}
}
// Drop table
$result = $result && drop_table($table);
}
if ($result && $oldversion < 2010042800) {
/// Rename field correctresponsesfeedback on table question_ddwtos to shownumcorrect
$table = new XMLDBTable('question_ddwtos');
$field = new XMLDBField('correctresponsesfeedback');
$field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'incorrectfeedback');
/// Launch rename field correctresponsesfeedback
$result = $result && rename_field($table, $field, 'shownumcorrect');
}
return $result;
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Defines the editing form for the drag-and-drop words into sentences question type.
*
* @package qtype_ddwtos
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot . '/question/type/sddl/edit_form_base.php');
/**
* Drag-and-drop words into sentences editing form definition.
*
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_ddwtos_edit_form extends qtype_gapselect_edit_form_base {
function qtype() {
return 'ddwtos';
}
protected function default_values_from_feedback_field($feedback, $key){
$feedback = unserialize($feedback);
$draggroup = $feedback->draggroup;
$infinite = $feedback->infinite;
$default_values = array();
$default_values['choices['.$key.'][draggroup]'] = $draggroup;
$default_values['choices['.$key.'][infinite]'] = $infinite;
return $default_values;
}
protected function repeated_options(){
$repeatedoptions = array();
$repeatedoptions['draggroup']['default'] = '1';
$repeatedoptions['infinite']['default'] = 0;
return $repeatedoptions;
}
protected function choice_group(&$mform, $grouparray){
$options = array();
for ($i = 1; $i <= 8; $i += 1) {
$options[$i] = $i;
}
$grouparray[] =& $mform->createElement('select', 'draggroup', get_string('group', 'qtype_ddwtos'), $options);
$grouparray[] =& $mform->createElement('checkbox', 'infinite', ' ', get_string('infinite', 'qtype_ddwtos'), null, array('size'=>1, 'class'=>'tweakcss'));
return $grouparray;
}
}
<?php
$string['addingddwtos'] = 'Adding drag and drop: words in text';
$string['addmorechoiceblanks'] = 'Blanks for {no} More Choices';
$string['answer'] = 'Answer';
$string['correctansweris'] = 'The correct answer is: $a';
$string['ddwtos'] = 'Drag and drop: words in text';
$string['ddwtos_help'] = 'Type in some question text like "The [[1]] jumped over the [[2]]", then enter the possible words to go in gaps 1 and 2 underneath.';
$string['ddwtossummary'] = 'Missing words in some text are filled in using drag-and-drop.';
$string['editingddwtos'] = 'Editing drag and drop: words in text';
$string['infinite'] = 'Infinite';
$string['pleaseputananswerineachbox'] = 'Please put an answer in each box.';
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Drag-and-drop words into sentences question definition class.
*
* @package qtype_ddwtos
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot . '/question/type/gapselect/questionbase.php');
/**
* Represents a drag-and-drop words into sentences question.
*
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_ddwtos_question extends qtype_gapselect_question_base {
//is actually exactly the same.
}
/**
* Represents one of the choices (draggable boxes).
*
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_ddwtos_choice {
public $text;
public $draggroup;
public $isinfinite;
public function __construct($text, $draggroup = 1, $isinfinite = false) {
$this->text = $text;
$this->draggroup = $draggroup;
$this->isinfinite = $isinfinite;
}
public function choice_group(){
return $this->draggroup;
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Question type class for the drag-and-drop words into sentences question type.
*
* @package qtype_ddwtos
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->libdir . '/questionlib.php');
require_once($CFG->dirroot . '/question/engine/lib.php');
require_once($CFG->dirroot . '/question/format/xml/format.php');
require_once($CFG->dirroot . '/question/type/gapselect/questiontypebase.php');
/**
* The drag-and-drop words into sentences question type class.
*
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_ddwtos extends qtype_gapselect_base {
protected function feedback_serialization($choice){
$output = new stdClass;
$output->draggroup = $choice['draggroup'];
$output->infinite = !empty($choice['infinite']);
return serialize($output);
}
protected function feedback_unserialization($choicedata){
$options = unserialize($choicedata->feedback);
return new qtype_ddwtos_choice($choicedata->answer, $options->draggroup, $options->infinite);
}
protected function feedback_unserialize_as_array($feedback){
$feedbackobj = unserialize($feedback);
return array('draggroup'=> $feedbackobj->draggroup, 'infinite'=> $feedbackobj->infinite);
}
protected function choice_group_key(){
return 'draggroup';
}
public function requires_qtypes() {
return array('sddl');
}
public function import_from_xml($data, $question, $format, $extra=null) {
if (!isset($data['@']['type']) || $data['@']['type'] != 'ddwtos') {
return false;
}
$question = $format->import_headers($data);
$question->qtype = 'ddwtos';
$question->shuffleanswers = $format->trans_single(
$format->getpath($data, array('#', 'shuffleanswers', 0, '#'), 1));
if (!empty($data['#']['dragbox'])) {
// Modern XML format.
$dragboxes = $data['#']['dragbox'];
$question->answer = array();
$question->draggroup = array();
$question->infinite = array();
foreach ($data['#']['dragbox'] as $dragboxxml) {
$question->choices[] = array(
'answer' => $format->getpath($dragboxxml, array('#', 'text', 0, '#'), '', true),
'draggroup' => $format->getpath($dragboxxml, array('#', 'group', 0, '#'), 1),
'infinite' => array_key_exists('infinite', $dragboxxml['#']),
);
}
} else {
// Legacy format containing PHP serialisation.
foreach ($data['#']['answer'] as $answerxml) {
$ans = $format->import_answer($answerxml);
$options = unserialize(stripslashes($ans->feedback));
$question->choices[] = array(
'answer' => $ans->answer,
'draggroup' => $options->draggroup,
'infinite' => $options->infinite,
);
}
}
$format->import_combined_feedback($question, $data, true);
$format->import_hints($question, $data, true);
return $question;
}
function export_to_xml($question, $format, $extra = null) {
$output = '';
$output .= ' <shuffleanswers>' . $question->options->shuffleanswers . "</shuffleanswers>\n";
$output .= $format->write_combined_feedback($question->options);
foreach ($question->options->answers as $answer) {
$options = unserialize($answer->feedback);
$output .= " <dragbox>\n";
$output .= $format->writetext($answer->answer, 3);
$output .= " <group>{$options->draggroup}</group>\n";
if ($options->infinite) {
$output .= " <infinite/>\n";
}
$output .= " </dragbox>\n";
}
return $output;
}
/*
* Backup the data in the question
*
* This is used in question/backuplib.php
*/
public function backup($bf, $preferences, $question, $level = 6) {
$status = true;
$ddwtos = get_records("question_ddwtos", "questionid", $question, "id");
//If there are ddwtos
if ($ddwtos) {
//Iterate over each ddwtos
foreach ($ddwtos as $ddws) {
$status = fwrite ($bf,start_tag("DDWORDSSENTENCES",$level,true));
//Print oumultiresponse contents
fwrite ($bf,full_tag("SHUFFLEANSWERS",$level+1,false,$ddws->shuffleanswers));
fwrite ($bf,full_tag("CORRECTFEEDBACK",$level+1,false,$ddws->correctfeedback));
fwrite ($bf,full_tag("PARTIALLYCORRECTFEEDBACK",$level+1,false,$ddws->partiallycorrectfeedback));
fwrite ($bf,full_tag("INCORRECTFEEDBACK",$level+1,false,$ddws->incorrectfeedback));
fwrite ($bf,full_tag("SHOWNUMCORRECT",$level+1,false,$ddws->shownumcorrect));
$status = fwrite ($bf,end_tag("DDWORDSSENTENCES",$level,true));
}
//Now print question_answers
$status = question_backup_answers($bf,$preferences,$question);
}
return $status;
}
/**
* Restores the data in the question (This is used in question/restorelib.php)
*
*/
public function restore($old_question_id,$new_question_id,$info,$restore) {
$status = true;
//Get the ddwtos array
$ddwtos = $info['#']['DDWORDSSENTENCES'];
//Iterate over oumultiresponses
for($i = 0; $i < sizeof($ddwtos); $i++) {
$mul_info = $ddwtos[$i];
//Now, build the question_ddwtos record structure
$ddwtos = new stdClass;
$ddwtos->questionid = $new_question_id;
$ddwtos->shuffleanswers = isset($mul_info['#']['SHUFFLEANSWERS']['0']['#'])?backup_todb($mul_info['#']['SHUFFLEANSWERS']['0']['#']):'';
if (array_key_exists("CORRECTFEEDBACK", $mul_info['#'])) {
$ddwtos->correctfeedback = backup_todb($mul_info['#']['CORRECTFEEDBACK']['0']['#']);
} else {
$ddwtos->correctfeedback = '';
}
if (array_key_exists("PARTIALLYCORRECTFEEDBACK", $mul_info['#'])) {
$ddwtos->partiallycorrectfeedback = backup_todb($mul_info['#']['PARTIALLYCORRECTFEEDBACK']['0']['#']);
} else {
$ddwtos->partiallycorrectfeedback = '';
}
if (array_key_exists("INCORRECTFEEDBACK", $mul_info['#'])) {
$ddwtos->incorrectfeedback = backup_todb($mul_info['#']['INCORRECTFEEDBACK']['0']['#']);
} else {
$ddwtos->incorrectfeedback = '';
}
if (array_key_exists('SHOWNUMCORRECT', $mul_info['#'])) {
$ddwtos->shownumcorrect = backup_todb($mul_info['#']['SHOWNUMCORRECT']['0']['#']);
} else if (array_key_exists('CORRECTRESPONSESFEEDBACK', $mul_info['#'])) {
$ddwtos->shownumcorrect = backup_todb($mul_info['#']['CORRECTRESPONSESFEEDBACK']['0']['#']);
} else {
$ddwtos->shownumcorrect = 0;
}
$newid = insert_record ("question_ddwtos",$ddwtos);
//Do some output
if (($i+1) % 50 == 0) {
if (!defined('RESTORE_SILENTLY')) {
echo ".";
if (($i+1) % 1000 == 0) {
echo "<br />";
}
}
backup_flush(300);
}
if (!$newid) {
$status = false;
}
}
return $status;
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Drag-and-drop words into sentences question renderer class.
*
* @package qtype_ddwtos
* @copyright 2010 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot . '/question/type/gapselect/rendererbase.php');
/**
* Generates the output for drag-and-drop words into sentences questions.
*
* @copyright 2010 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_ddwtos_renderer extends qtype_elements_embedded_in_question_text_renderer {
protected function qtext_classname(){
return 'qtext ddwtos_questionid_for_javascript';
}
protected function post_qtext_elements(question_attempt $qa, question_display_options $options){
$result = '';
$question = $qa->get_question();
$dragboxs = '';
foreach ($question->choices as $group => $choices) {
$dragboxs .= $this->drag_boxes($qa, $group,
$question->get_ordered_choices($group), $options);
}
$result .= html_writer::tag('div', $dragboxs,
array('class' => 'answercontainer'));
// We abuse the clear_wrong method to output the hidden form fields we
// want irrespective of whether we are actually clearing the wrong
// bits of the response.
if (!$options->clearwrong) {
$result .= $this->clear_wrong($qa, false);
}
return $result;
}
/**
* Modify the contents of a drag/drop box to fix some IE-related problem.
* Unfortunately I don't have more details than that.
* @param string $string the box contents.
* @return string the box contents modified.
*/
protected function dodgy_ie_fix($string) {
return '<sub>&#160;</sub>' . $string . '<sup>&#160;</sup>';
}
protected function embedded_element(question_attempt $qa, $place, question_display_options $options) {
$question = $qa->get_question();
$group = $question->places[$place];
$boxcontents = $this->dodgy_ie_fix('&#160;');
$value = $qa->get_last_qt_var($question->field($place));
$attributes = array(
'id' => $this->box_id($qa, 'p' . $place, $group),
'class' => 'slot group' . $group
);
if ($options->readonly) {
$attributes['class'] .= ' readonly';
} else {
$attributes['tabindex'] = '0';
}
$feedbackimage = '';
if ($options->correctness) {
$response = $qa->get_last_qt_data();
$fieldname = $question->field($place);
if (array_key_exists($fieldname, $response)) {
$fraction = (int) ($response[$fieldname] == $question->get_right_choice_for($place));
$attributes['class'] .= ' ' . $this->feedback_class($fraction);
$feedbackimage = $this->feedback_image($fraction);
}
}
return html_writer::tag('span', $boxcontents, $attributes) . ' ' . $feedbackimage;
}
protected function drag_boxes($qa, $group, $choices, question_display_options $options) {
$readonly = '';
if ($options->readonly) {
$readonly = ' readonly';
}
$boxes = '';
foreach ($choices as $key => $choice) {
//Bug 8632 - long text entry causes bug in drag and drop field in IE
$content = str_replace('-', '&#x2011;', $choice->text);
$content = $this->dodgy_ie_fix(str_replace(' ', '&#160;', $content));
$infinite = '';
if ($choice->isinfinite) {
$infinite = ' infinite';
}
$boxes .= html_writer::tag('span', $content, array(
'id' => $this->box_id($qa, $key, $choice->draggroup),
'class' => 'player group' . $choice->draggroup . $infinite . $readonly)) . ' ';
}
return html_writer::nonempty_tag('div', $boxes, array('class' => 'answertext'));