Skip to content

Commit

Permalink
MDL-47494 ddimageortext: Update OU Qtype ddimageortext for simplified…
Browse files Browse the repository at this point in the history
… forms #6614
  • Loading branch information
Colin Chambers committed May 22, 2013
1 parent a214a28 commit 4139151
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 96 deletions.
66 changes: 30 additions & 36 deletions question/type/ddimageortext/edit_ddimageortext_form.php
Expand Up @@ -52,7 +52,7 @@ public function data_preprocessing($question) {
foreach ($question->options->drags as $drag) {
$dragindex = $drag->no -1;
$question->drags[$dragindex] = array();
$question->drags[$dragindex]['draglabel'] = $drag->label;
$question->draglabel[$dragindex] = $drag->label;
$question->drags[$dragindex]['infinite'] = $drag->infinite;
$question->drags[$dragindex]['draggroup'] = $drag->draggroup;
$dragids[$dragindex] = $drag->id;
Expand Down Expand Up @@ -93,11 +93,11 @@ public function data_preprocessing($question) {
} else {
$fileexists = self::file_uploaded($question->dragitem[$dragindex]);
}
$labelexists = (trim($question->drags[$dragindex]['draglabel']) != '');
$labelexists = (trim($question->draglabel[$dragindex]) != '');
if ($labelexists && !$fileexists) {
$question->dragitemtype[$dragindex] = 'word';
$question->drags[$dragindex]['dragitemtype'] = 'word';
} else {
$question->dragitemtype[$dragindex] = 'image';
$question->drags[$dragindex]['dragitemtype'] = 'image';
}
}
}
Expand All @@ -118,7 +118,7 @@ public function js_call() {
$maxsizes->dragimage->height = QTYPE_DDIMAGEORTEXT_DRAGIMAGE_MAXHEIGHT;

$params = array('maxsizes' => $maxsizes,
'topnode' => 'fieldset#previewareaheader');
'topnode' => 'fieldset#id_previewareaheader');

$PAGE->requires->yui_module('moodle-qtype_ddimageortext-form',
'M.qtype_ddimageortext.init_form',
Expand All @@ -128,46 +128,47 @@ public function js_call() {
// Drag items.

protected function definition_draggable_items($mform, $itemrepeatsatstart) {

$mform->addElement('header', 'draggableitemheader',
get_string('draggableitems', 'qtype_ddimageortext'));
$mform->addElement('advcheckbox', 'shuffleanswers', ' ',
get_string('shuffleimages', 'qtype_'.$this->qtype()));
$mform->setDefault('shuffleanswers', 0);
$this->repeat_elements($this->draggable_item($mform), $itemrepeatsatstart,
$this->draggable_items_repeated_options(),
'noitems', 'additems', self::ADD_NUM_ITEMS,
get_string('addmoreimages', 'qtype_ddimageortext'));
get_string('addmoreimages', 'qtype_ddimageortext'), true);
}

protected function draggable_item($mform) {
$draggableimageitem = array();

$draggableimageitem[] = $mform->createElement('header', 'draggableitemheader',
get_string('draggableitemheader', 'qtype_ddimageortext', '{no}'));
$grouparray = array();
$dragitemtypes = array('image' => get_string('draggableimage', 'qtype_ddimageortext'),
'word' => get_string('draggableword', 'qtype_ddimageortext'));
$draggableimageitem[] = $mform->createElement('select', 'dragitemtype',
$grouparray[] = $mform->createElement('select', 'dragitemtype',
get_string('draggableitemtype', 'qtype_ddimageortext'),
$dragitemtypes,
array('class' => 'dragitemtype'));
$draggableimageitem[] = $mform->createElement('filepicker', 'dragitem', '', null,
self::file_picker_options());

$grouparray = array();
$grouparray[] = $mform->createElement('text', 'draglabel',
get_string('label', 'qtype_ddimageortext'),
array('size'=>30, 'class'=>'tweakcss'));
$mform->setType('draglabel', PARAM_NOTAGS);
$options = array();
for ($i = 1; $i <= self::MAX_GROUPS; $i += 1) {
$options[$i] = $i;
}
$grouparray[] = $mform->createElement('static', '', '', ' ' .
get_string('group', 'qtype_gapselect').' ');
$grouparray[] = $mform->createElement('select', 'draggroup',
get_string('group', 'qtype_gapselect'),
$options,
array('class' => 'draggroup'));
$grouparray[] = $mform->createElement('advcheckbox', 'infinite', ' ',
get_string('infinite', 'qtype_ddimageortext'));
$draggableimageitem[] = $mform->createElement('group', 'drags',
get_string('label', 'qtype_ddimageortext'), $grouparray);
get_string('draggableitemheader', 'qtype_ddimageortext', '{no}'), $grouparray);

$draggableimageitem[] = $mform->createElement('filepicker', 'dragitem', '', null,
self::file_picker_options());

$draggableimageitem[] = $mform->createElement('text', 'draglabel',
get_string('label', 'qtype_ddimageortext'),
array('size'=>30, 'class'=>'tweakcss'));
$mform->setType('draglabel', PARAM_NOTAGS);
return $draggableimageitem;
}

Expand All @@ -183,30 +184,20 @@ protected function drop_zone($mform, $imagerepeats) {
$dropzoneitem = array();

$grouparray = array();
$grouparray[] = $mform->createElement('static', 'xleftlabel', '',
' '.get_string('xleft', 'qtype_ddimageortext').' ');
$grouparray[] = $mform->createElement('text', 'xleft',
get_string('xleft', 'qtype_ddimageortext'),
array('size'=>5, 'class'=>'tweakcss'));
$mform->setType('xleft', PARAM_NOTAGS);
$grouparray[] = $mform->createElement('static', 'ytoplabel', '',
' '.get_string('ytop', 'qtype_ddimageortext').' ');
$grouparray[] = $mform->createElement('text', 'ytop',
get_string('ytop', 'qtype_ddimageortext'),
array('size'=>5, 'class'=>'tweakcss'));
$mform->setType('ytop', PARAM_NOTAGS);
$options = array();

$options[0] = '';
for ($i = 1; $i <= $imagerepeats; $i += 1) {
$options[$i] = $i;
}
$grouparray[] = $mform->createElement('static', '', '', ' ' .
get_string('draggableitem', 'qtype_ddimageortext').' ');
$grouparray[] = $mform->createElement('select', 'choice',
get_string('draggableitem', 'qtype_ddimageortext'), $options);
$grouparray[] = $mform->createElement('static', '', '', ' ' .
get_string('label', 'qtype_ddimageortext').' ');
$grouparray[] = $mform->createElement('text', 'droplabel',
get_string('label', 'qtype_ddimageortext'),
array('size'=>10, 'class'=>'tweakcss'));
Expand All @@ -218,8 +209,11 @@ protected function drop_zone($mform, $imagerepeats) {

protected function drop_zones_repeated_options() {
$repeatedoptions = array();
$repeatedoptions['xleft']['type'] = PARAM_INT;
$repeatedoptions['ytop']['type'] = PARAM_INT;
// The next two are PARAM_RAW becuase we need to distinguish 0 and ''.
// We do the necessary validation in the validation method.
$repeatedoptions['drops[xleft]']['type'] = PARAM_RAW;
$repeatedoptions['drops[ytop]']['type'] = PARAM_RAW;
$repeatedoptions['drops[droplabel]']['type'] = PARAM_TEXT;
$repeatedoptions['choice']['default'] = '0';
return $repeatedoptions;
}
Expand Down Expand Up @@ -252,7 +246,7 @@ public function validation($data, $files) {
$errors["drops[$i]"] = get_string('formerror_notintxleft', 'qtype_ddimageortext');
}

if ($data['dragitemtype'][$choice - 1] != 'word' &&
if ($data['drags'][$choice - 1]['dragitemtype'] != 'word' &&
!self::file_uploaded($data['dragitem'][$choice - 1])) {
$errors['dragitem['.($choice - 1).']'] =
get_string('formerror_nofile', 'qtype_ddimageortext', $i);
Expand All @@ -275,8 +269,8 @@ public function validation($data, $files) {
}
}
for ($dragindex=0; $dragindex < $data['noitems']; $dragindex++) {
$label = $data['drags'][$dragindex]['draglabel'];
if ($data['dragitemtype'][$dragindex] == 'word') {
$label = $data['draglabel'][$dragindex];
if ($data['drags'][$dragindex]['dragitemtype'] == 'word') {
$allowedtags = '<br><sub><sup><b><i><strong><em>';
$errormessage = get_string('formerror_disallowedtags', 'qtype_ddimageortext');
} else {
Expand Down
23 changes: 9 additions & 14 deletions question/type/ddimageortext/edit_ddtoimage_form_base.php
Expand Up @@ -63,14 +63,13 @@ protected function definition_inner($mform) {

$mform->registerNoSubmitButton('refresh');
$mform->addElement('submit', 'refresh', get_string('refresh', 'qtype_'.$this->qtype()));
$mform->closeHeaderBefore('refresh');
$mform->addElement('filepicker', 'bgimage', get_string('bgimage', 'qtype_'.$this->qtype()),
null, self::file_picker_options());
$mform->closeHeaderBefore('dropzoneheader');

list($itemrepeatsatstart, $imagerepeats) = $this->get_drag_item_repeats();
$this->definition_drop_zones($mform, $imagerepeats);
$mform->addElement('advcheckbox', 'shuffleanswers', ' ',
get_string('shuffleimages', 'qtype_'.$this->qtype()));
$mform->setDefault('shuffleanswers', 0);
$mform->closeHeaderBefore('shuffleanswers');

// Add the draggable image fields to the form.
$this->definition_draggable_items($mform, $itemrepeatsatstart);

Expand All @@ -82,21 +81,17 @@ protected function definition_drop_zones($mform, $imagerepeats) {
$mform->addElement('header', 'dropzoneheader',
get_string('dropzoneheader', 'qtype_'.$this->qtype()));

$mform->addElement('filepicker', 'bgimage', get_string('bgimage', 'qtype_'.$this->qtype()),
null, self::file_picker_options());

$countdropzones = 0;
if (isset($this->question->id)) {
foreach ($this->question->options->drops as $drop) {
$countdropzones = max($countdropzones, $drop->no);
}
}
if ($this->question->formoptions->repeatelements) {
$dropzonerepeatsatstart = max(self::START_NUM_ITEMS,
$countdropzones + self::ADD_NUM_ITEMS);
} else {
$dropzonerepeatsatstart = $countdropzones;

if (!$countdropzones) {
$countdropzones = self::START_NUM_ITEMS;
}
$dropzonerepeatsatstart = $countdropzones;

$this->repeat_elements($this->drop_zone($mform, $imagerepeats), $dropzonerepeatsatstart,
$this->drop_zones_repeated_options(),
Expand All @@ -107,7 +102,7 @@ abstract protected function drop_zone($mform, $imagerepeats);

abstract protected function drop_zones_repeated_options();

abstract protected function definition_draggable_items($mform, $itemrepeatsatstart);
abstract protected function definition_draggable_items($mform, $itemrepeatsatstart) ;

abstract protected function draggable_item($mform);

Expand Down
5 changes: 3 additions & 2 deletions question/type/ddimageortext/lang/en/qtype_ddimageortext.php
Expand Up @@ -29,6 +29,7 @@
$string['correctansweris'] = 'The correct answer is: {$a}';
$string['draggableimage'] = 'Draggable image';
$string['draggableitem'] = 'Draggable item';
$string['draggableitems'] = 'Draggable items';
$string['draggableitemheader'] = 'Draggable item {$a}';
$string['draggableitemtype'] = 'Type';
$string['draggableword'] = 'Draggable text';
Expand Down Expand Up @@ -57,8 +58,8 @@
$string['pluginnameadding'] = 'Adding drag and drop onto image';
$string['pluginnameediting'] = 'Editing drag and drop onto image';
$string['pluginnamesummary'] = 'Images or text labels are dragged and dropped into drop zones on a background image.';
$string['previewarea'] = 'Preview area -';
$string['previewareaheader'] = 'Preview';
$string['previewarea'] = '';
$string['previewareaheader'] = 'Background image';
$string['previewareamessage'] = 'Select a background image file and select draggable images or just enter text that will be made draggable. Then choose a drag item for each \'drop zone\', and drag the drag item to where the student should drag it to.';
$string['refresh'] = 'Refresh preview';
$string['shuffleimages'] = 'Shuffle drag items each time question is attempted';
Expand Down
13 changes: 7 additions & 6 deletions question/type/ddimageortext/questiontype.php
Expand Up @@ -95,15 +95,15 @@ public function save_question_options($formdata) {
'', 'no, id');
foreach (array_keys($formdata->drags) as $dragno) {
$info = file_get_draft_area_info($formdata->dragitem[$dragno]);
if ($info['filecount'] > 0 || (trim($formdata->drags[$dragno]['draglabel'])!='')) {
if ($info['filecount'] > 0 || (trim($formdata->draglabel[$dragno])!='')) {
$draftitemid = $formdata->dragitem[$dragno];

$drag = new stdClass();
$drag->questionid = $formdata->id;
$drag->no = $dragno + 1;
$drag->draggroup = $formdata->drags[$dragno]['draggroup'];
$drag->infinite = empty($formdata->drags[$dragno]['infinite'])? 0 : 1;
$drag->label = $formdata->drags[$dragno]['draglabel'];
$drag->label = $formdata->draglabel[$dragno];

if (isset($olddragids[$dragno +1])) {
$drag->id = $olddragids[$dragno +1];
Expand All @@ -113,7 +113,7 @@ public function save_question_options($formdata) {
$drag->id = $DB->insert_record('qtype_ddimageortext_drags', $drag);
}

if ($formdata->dragitemtype[$dragno] == 'image') {
if ($formdata->drags[$dragno]['dragitemtype'] == 'image') {
self::constrain_image_size_in_draft_area($draftitemid,
QTYPE_DDIMAGEORTEXT_DRAGIMAGE_MAXWIDTH,
QTYPE_DDIMAGEORTEXT_DRAGIMAGE_MAXHEIGHT);
Expand All @@ -130,6 +130,7 @@ public function save_question_options($formdata) {
}

}

if (!empty($olddragids)) {
list($sql, $params) = $DB->get_in_or_equal(array_values($olddragids));
$DB->delete_records_select('qtype_ddimageortext_drags', "id $sql", $params);
Expand Down Expand Up @@ -243,7 +244,7 @@ public function import_from_xml($data, $question, qformat_xml $format, $extra=nu
$dragno = $format->getpath($dragxml, array('#', 'no', 0, '#'), 0);
$dragindex = $dragno -1;
$question->drags[$dragindex] = array();
$question->drags[$dragindex]['draglabel'] =
$question->draglabel[$dragindex] =
$format->getpath($dragxml, array('#', 'text', 0, '#'), '', true);
$question->drags[$dragindex]['infinite'] = array_key_exists('infinite', $dragxml['#']);
$question->drags[$dragindex]['draggroup'] =
Expand All @@ -252,9 +253,9 @@ public function import_from_xml($data, $question, qformat_xml $format, $extra=nu
$question->dragitem[$dragindex] =
$this->import_files_to_draft_file_area($format, $filexml);
if (count($filexml)) {
$question->dragitemtype[$dragindex] = 'image';
$question->drags[$dragindex]['dragitemtype'] = 'image';
} else {
$question->dragitemtype[$dragindex] = 'word';
$question->drags[$dragindex]['dragitemtype'] = 'word';
}
}

Expand Down

0 comments on commit 4139151

Please sign in to comment.