Skip to content

Commit

Permalink
MDL-47494 ddimageortext: NOBUG continued converting functionality of …
Browse files Browse the repository at this point in the history
…question type so it can be used

with either image or text drag items.
  • Loading branch information
jamiepratt committed Sep 12, 2011
1 parent 42167fc commit c4d8ad0
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 135 deletions.
50 changes: 35 additions & 15 deletions question/type/ddimageortext/edit_ddimagetoimage_form.php
Expand Up @@ -33,8 +33,8 @@
*/
class qtype_ddimagetoimage_edit_form extends question_edit_form {
const MAX_GROUPS = 8;
const START_NUM_IMAGES = 6;
const ADD_NUM_IMAGES = 3;
const START_NUM_ITEMS = 6;
const ADD_NUM_ITEMS = 3;

public function qtype() {
return 'ddimagetoimage';
Expand All @@ -61,14 +61,14 @@ protected function get_drag_image_repeats() {
}
}
if ($this->question->formoptions->repeatelements) {
$imagerepeatsatstart = max(self::START_NUM_IMAGES, $countimages + self::ADD_NUM_IMAGES);
$imagerepeatsatstart = max(self::START_NUM_ITEMS, $countimages + self::ADD_NUM_ITEMS);
} else {
$imagerepeatsatstart = $countimages;
}
$imagerepeats = optional_param('noimages', $imagerepeatsatstart, PARAM_INT);
$addfields = optional_param('addimages', '', PARAM_TEXT);
if (!empty($addfields)) {
$imagerepeats += self::ADD_NUM_IMAGES;
$imagerepeats += self::ADD_NUM_ITEMS;
}
return array($imagerepeatsatstart, $imagerepeats);
}
Expand Down Expand Up @@ -116,15 +116,15 @@ protected function definition_drop_zones($mform, $imagerepeats) {
}
}
if ($this->question->formoptions->repeatelements) {
$dropzonerepeatsatstart = max(self::START_NUM_IMAGES,
$countdropzones + self::ADD_NUM_IMAGES);
$dropzonerepeatsatstart = max(self::START_NUM_ITEMS,
$countdropzones + self::ADD_NUM_ITEMS);
} else {
$dropzonerepeatsatstart = $countdropzones;
}

$this->repeat_elements($this->drop_zone($mform, $imagerepeats), $dropzonerepeatsatstart,
$this->drop_zones_repeated_options(),
'nodropzone', 'adddropzone', self::ADD_NUM_IMAGES,
'nodropzone', 'adddropzone', self::ADD_NUM_ITEMS,
get_string('addmoredropzones', 'qtype_ddimagetoimage'));
}

Expand Down Expand Up @@ -175,7 +175,7 @@ protected function definition_draggable_images($mform, $imagerepeatsatstart) {

$this->repeat_elements($this->draggable_image($mform), $imagerepeatsatstart,
$this->draggable_images_repeated_options(),
'noimages', 'addimages', self::ADD_NUM_IMAGES,
'noitems', 'additems', self::ADD_NUM_ITEMS,
get_string('addmoreimages', 'qtype_ddimagetoimage'));
}

Expand All @@ -184,8 +184,8 @@ protected function draggable_image($mform) {

$draggableimageitem[] = $mform->createElement('header', 'draggableitemheader',
get_string('draggableitemheader', 'qtype_ddimagetoimage', '{no}'));
$dragitemtypes = array(0 => get_string('draggableimage', 'qtype_ddimagetoimage'),
1 => get_string('draggableword', 'qtype_ddimagetoimage'));
$dragitemtypes = array('image' => get_string('draggableimage', 'qtype_ddimagetoimage'),
'word' => get_string('draggableword', 'qtype_ddimagetoimage'));
$draggableimageitem[] = $mform->createElement('select', 'dragitemtype',
get_string('draggableitemtype', 'qtype_ddimagetoimage'),
$dragitemtypes,
Expand Down Expand Up @@ -271,16 +271,16 @@ public function data_preprocessing($question) {
if (!empty($question->options)) {
foreach ($question->options->drags as $drag) {
$dragindex = $drag->no -1;
if (!isset($draftitemids[$dragindex])) {
if (!isset($question->dragitem[$dragindex])) {
$fileexists = false;
} else {
$fileexists = self::file_uploaded($draftitemids[$dragindex]);
$fileexists = self::file_uploaded($question->dragitem[$dragindex]);
}
$labelexists = $question->drags[$dragindex]['draglabel'];
if ($labelexists && !$fileexists) {
$question->dragitemtype[$dragindex] = 1;
$question->dragitemtype[$dragindex] = 'word';
} else {
$question->dragitemtype[$dragindex] = 0;
$question->dragitemtype[$dragindex] = 'image';
}
}
}
Expand Down Expand Up @@ -315,7 +315,6 @@ public static function file_uploaded($draftitemid) {
return true;
}


public function validation($data, $files) {
$errors = parent::validation($data, $files);
if (!self::file_uploaded($data['bgimage'])) {
Expand All @@ -340,6 +339,13 @@ public function validation($data, $files) {
get_string('formerror_noxleft', 'qtype_ddimagetoimage');
}

if ($data['dragitemtype'][$choice - 1] != 'word' &&
!self::file_uploaded($data['dragitem'][$choice - 1])) {
$errors['dragitem['.($choice - 1).']'] =
get_string('formerror_nofile', 'qtype_ddimagetoimage', $i);
}


if (isset($allchoices[$choice]) && !$data['drags'][$choice-1]['infinite']) {
$errors["drops[$i]"] =
get_string('formerror_multipledraginstance', 'qtype_ddimagetoimage', $choice);
Expand All @@ -356,6 +362,20 @@ public function validation($data, $files) {
}
}
}
for ($dragindex=0; $dragindex < $data['noitems']; $dragindex++) {
$label = $data['drags'][$dragindex]['draglabel'];
if ($data['dragitemtype'][$dragindex] == 'word') {
$allowedtags = '<br><sub><sup><b><i><strong><em>';
$errormessage = get_string('formerror_disallowedtags', 'qtype_ddimagetoimage');
} else {
$allowedtags = '';
$errormessage = get_string('formerror_noallowedtags', 'qtype_ddimagetoimage');
}
if ($label != strip_tags($label, $allowedtags)){
$errors["drags[{$dragindex}]"] = $errormessage;
}

}
return $errors;
}

Expand Down
9 changes: 5 additions & 4 deletions question/type/ddimageortext/lang/en/qtype_ddimagetoimage.php
Expand Up @@ -35,21 +35,22 @@
$string['draggableitem'] = 'Draggable item';
$string['draggableitemheader'] = 'Draggable item {$a}';
$string['draggableitemtype'] = 'Type';
$string['draggableword'] = 'Draggable word';
$string['draggableword'] = 'Draggable text';
$string['dropzone'] = 'Drop zone {$a}';
$string['dropzoneheader'] = 'Drop zones';
$string['editingddimagetoimage'] = 'Editing drag and drop: images onto image';
$string['formerror_disallowedtags'] = 'You have used html tags here that are not allowed in a draggable text drag item type.';
$string['formerror_noallowedtags'] = 'No html tags are allowed in this text which is the alt text for a draggable image';
$string['formerror_noytop'] = 'You must provide a value for the y coords for the top left corner of this drop area. You can drag and drop the drop area above to set the coordinates or enter them manually here.';
$string['formerror_noxleft'] = 'You must provide a value for the y coords for the top left corner of this drop area. You can drag and drop the drop area above to set the coordinates or enter them manually here.';
$string['formerror_nofile'] = 'Although you selected drag image {$a} as the correct choice for this drop zone you have not selected an image for drag image {$a}.';
$string['formerror_nofile2'] = 'You need to select an image file here, this drag item is referred to as the correct answer for drop zone {$a}.';
$string['formerror_nofile'] = 'You need to upload or select a file to use here.';
$string['formerror_nofile3'] = 'You need to select an image file here, or delete the associated label and uncheck the infinite checkbox.';
$string['formerror_multipledraginstance'] = 'You have selected this image {$a} more than once as the correct choice for a drop zone but it is not marked as being an infinite drag item.';
$string['formerror_multipledraginstance2'] = 'You have selected this image more than once as the correct choice for a drop zone but it is not marked as being an infinite drag item.';
$string['formerror_noimageselected'] = 'You need to select a drag item to be the correct choice for this drop zone.';
$string['formerror_nobgimage'] = 'You need to select an image to use as the background for the drag and drop area.';
$string['infinite'] = 'Infinite';
$string['label'] = 'Label';
$string['label'] = 'Text';
$string['nolabel'] = 'No label text';
$string['pleasedraganimagetoeachdropregion'] = 'Your answer is not complete, please drag an image to each drop region.';
$string['previewarea'] = 'Preview area -';
Expand Down
16 changes: 13 additions & 3 deletions question/type/ddimageortext/questiontype.php
Expand Up @@ -155,7 +155,7 @@ 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 || !empty($formdata->drags[$dragno]['draglabel'])) {
if ($info['filecount'] > 1 || !empty($formdata->drags[$dragno]['draglabel'])) {
$draftitemid = $formdata->dragitem[$dragno];

$drag = new stdClass();
Expand All @@ -173,13 +173,22 @@ public function save_question_options($formdata) {
$drag->id = $DB->insert_record('qtype_ddimagetoimage_drags', $drag);
}

self::constrain_image_size_in_draft_area($draftitemid,
if ($formdata->dragitemtype[$dragno] == 'image') {
self::constrain_image_size_in_draft_area($draftitemid,
QTYPE_DDIMAGETOIMAGE_DRAGIMAGE_MAXWIDTH,
QTYPE_DDIMAGETOIMAGE_DRAGIMAGE_MAXHEIGHT);
file_save_draft_area_files($draftitemid, $formdata->context->id,
file_save_draft_area_files($draftitemid, $formdata->context->id,
'qtype_ddimagetoimage', 'dragimage', $drag->id,
array('subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 1));
} else {
//delete any existing files for draggable text item type
$fs = get_file_storage();
$fs->delete_area_files($formdata->context->id, 'qtype_ddimagetoimage',
'dragimage', $drag->id);
}

}

}
if (!empty($olddragids)) {
list($sql, $params) = $DB->get_in_or_equal(array_values($olddragids));
Expand All @@ -194,6 +203,7 @@ public function save_question_options($formdata) {
array('subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 1));
}


public static function constrain_image_size_in_draft_area($draftitemid, $maxwidth, $maxheight) {
global $USER;
$usercontext = get_context_instance(CONTEXT_USER, $USER->id);
Expand Down
2 changes: 1 addition & 1 deletion question/type/ddimageortext/renderer.php
Expand Up @@ -84,7 +84,7 @@ public function formulation_and_controls(question_attempt $qa,
$dragimageurl = self::get_url_for_image($qa, 'dragimage', $dragimage->id);
$classes = array("group{$groupno}",
'draghome',
"dragimagehomes{$dragimage->no}",
"dragitemhomes{$dragimage->no}",
"choice{$choiceno}");
if ($dragimage->isinfinite) {
$classes[] = 'infinite';
Expand Down

0 comments on commit c4d8ad0

Please sign in to comment.