Skip to content

Commit

Permalink
sorting out some refactoring anc chnges for preview #520123
Browse files Browse the repository at this point in the history
  • Loading branch information
mkassaei committed Sep 4, 2024
1 parent 9fb7876 commit 568a1d6
Show file tree
Hide file tree
Showing 11 changed files with 577 additions and 160 deletions.
108 changes: 108 additions & 0 deletions classes/line.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace qtype_drawlines;
use qtype_drawlines\dragitem;
use qtype_drawlines\dropzone;

/**
* Represents a line objet of drawlines question.
Expand Down Expand Up @@ -116,6 +118,66 @@ public static function get_line_types(): array {
];
}

public static function is_dragitem_in_the_right_place($dragcoord, $dropcood): bool {
[$cx, $cy, $r] = self::parse_into_cx_cy_with_or_without_radius($dropcood, true);
[$rescx, $rescy] = self::parse_into_cx_cy_with_or_without_radius($dragcoord);

$xcoord = false;
if ((($cx - $r) <= $rescx) && ($rescx <= ($cx + $r))) {
$xcoord = true;
}
$ycoord = false;
if ((($cy - $r) <= $rescy) && ($rescy <= ($cy + $r))) {
$ycoord = true;
}
if ($xcoord && $ycoord) {
return true;
}
return false;
}

/**
* Parse the input and return the parts in a list of 'cx', 'cy' with or whothout 'r'.
*
* @param string $dropzone, the string in a given format with or whithout radius
* @param bool $radius, if set to true, return the list with radius, otherwise with radius
* @return int[], a list of 'cx', 'cy' with or whothout 'r'.
*/
public static function parse_into_cx_cy_with_or_without_radius(string $dropzone, bool $radius = false): array {
if ($radius === true) {
$dropzonecontent = explode(';', $dropzone);
$coordinates = explode(',', $dropzonecontent[0]);

// Return the parsts in a list of 'cx', 'cy' and 'r' in numbers.
return [(int)$coordinates[0], (int)$coordinates[1], (int)$dropzonecontent[1]];
}
$coordinates = explode(',', $dropzone);
// Return the parsts in a list of 'cx'and 'cy'in numbers.
return [(int)$coordinates[0], (int)$coordinates[1]];
}

/**
* Return the coordinates for a given dropzone (strat or end of the line).
*
* @param string $zone
* @return string
*/
public static function get_coordinates(string $zone): string {
$zonestart = explode(';', $zone);
return $zonestart[0];
}

/**
* Return the radius for a given dropzone circle (strat or end of the line).
*
* @param string $zone
* @return int
*/
public static function get_radius(string $zone): int {
$zonestart = explode(';', $zone);
return (int)$zonestart[1];
}

/**
* Validate the zone coordinates for Start or End zone of a line.
* The correct format is x,y;r where x,y are the coordinates of the centre of a circle and r is the radius.
Expand All @@ -140,4 +202,50 @@ public static function is_zone_coordinates_valid(string $zone): bool {
// Match found.
return true;
}

public static function make_drop_zone(int $linenumber, string $label, string $zone): drop_zone {
[$xleft, $ytop] = self::parse_into_cx_cy_with_or_without_radius($zone);
return new drop_zone($linenumber, $label, $xleft, $ytop);
}

}

/**
* Represents a drop-zone objet for start or end of a line.
*
* @package qtype_drawlines
* @copyright 2024 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class drop_zone {
/** @var int The number of the line */
public $linenumber;

/** @var string Alt text for the drop zone item */
public $label;

/** @var array X and Y location of the drop zone */
public $xy;

/**
* Create a drop zone object.
*
* @param int $linenumber Which line the drop zone belong to
* @param string $alttextlabel The alt text of the drop zone
* @param int $x X location
* @param int $y Y location
*/
public function __construct($linenumber, $label, $x, $y) {
$this->linenumber = $linenumber;
$this->label = $label;
$this->xy = [$x, $y];
}

/**
* Returns the line number for this droo zone.
*
*/
public function get_linenumber(): int {
return $this->linenumber;
}
}
6 changes: 6 additions & 0 deletions edit_drawlines_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ protected function definition_inner($mform): void {

$this->set_current_settings();

// Add hidden form elements, for choices.
for ($number = 1; $number <= ($this->numberoflines * 2); $number++) {
$mform->addElement('hidden', 'c' . $number);
$mform->setType('c' . $number, PARAM_RAW);
}

$grademethodmenu = [
'partial' => get_string('gradepartialcredit', 'qtype_' . $this->qtype()),
'allnone' => get_string('gradeallornothing', 'qtype_drawlines'),
Expand Down
10 changes: 10 additions & 0 deletions lang/en/qtype_drawlines.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
$string['lineinfinite'] = 'Infinite line --o--o--';
$string['linexheader'] = 'Line {no}';

$string['pleasedragalllines'] = 'Your answer is not complete; you must place all lines on the image.';
$string['pluginname'] = 'DrawLines';
$string['pluginname_help'] = 'DrawLines require the respondent to position the lines on a background image.';
$string['pluginname_link'] = 'question/type/drawlines';
Expand All @@ -67,13 +68,22 @@
$string['privacy:preference:defaultmark'] = 'The default mark set for a given question.';
$string['privacy:preference:grademethod'] = 'The penalty for each incorrect try when questions are run using the \'Interactive with multiple tries\' or \'Adaptive mode\' behaviour.';
$string['privacy:preference:penalty'] = 'The penalty for each incorrect try when questions are run using the \'Interactive with multiple tries\' or \'Adaptive mode\' behaviour.';

$string['refresh'] = 'Refresh preview';

$string['showmisplaced'] = 'State which zones are incorrectly placed';
$string['summarisechoice'] = '{$a->no}. {$a->text}';
$string['summariseplace'] = '{$a->no}. {$a->text}';
$string['summarisechoiceno'] = 'Item {$a}';
$string['summariseplaceno'] = 'Drop zone {$a}';

$string['type'] = 'Type';
$string['type_help'] = 'You can choose whether the line doesn’t have a beginning or end (line), has one or more ends (right, left, and double arrows), or it only matters that the line intersects specific points on the graph (intersect points).';

$string['xleft'] = 'Left';

$string['ytop'] = 'Top';

$string['zonecoordinates'] = 'x,y;r (where x,y are the coordinates of the centre of the circle and r is the radius)';
$string['zonestart'] = 'Start zone coordinates';
$string['zoneend'] = 'End zone coordinates';
Loading

0 comments on commit 568a1d6

Please sign in to comment.