Skip to content

Commit

Permalink
GH-220 display scale-score chart
Browse files Browse the repository at this point in the history
  • Loading branch information
eynimeni committed Jan 11, 2024
1 parent 2bf066d commit e5566af
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 49 deletions.
126 changes: 77 additions & 49 deletions classes/teststrategy/feedbackgenerator/personabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,17 @@ private function render_abilityprofile_chart(array $initialcontext, $primarycats
global $OUTPUT, $DB;

$abilitysteps = [];
for ($i = LOCAL_CATQUIZ_PERSONABILITY_LOWER_LIMIT + 0.25; $i <= LOCAL_CATQUIZ_PERSONABILITY_UPPER_LIMIT - 0.25; $i += 0.5) {
$abilitystep = 0.25;
$interval = $abilitystep * 2;
for ($i = LOCAL_CATQUIZ_PERSONABILITY_LOWER_LIMIT + $abilitystep; $i <= LOCAL_CATQUIZ_PERSONABILITY_UPPER_LIMIT - $abilitystep; $i += $interval) {
$abilitysteps[] = $i;
}

$catscale = new catscale($primarycatscale->id);
$items = $catscale->get_testitems($initialcontext['contextid'], true);

// Prepare data for test information line.
$items = $catscale->get_testitems($initialcontext['contextid'], true);
$models = model_strategy::get_installed_models();

$fisherinfos = [];
foreach ($items as $item) {
$key = $item->model;
Expand All @@ -356,64 +358,43 @@ private function render_abilityprofile_chart(array $initialcontext, $primarycats

}

$chart = new chart_bar();
$chartseries = [];
$chartseries['series'] = [];
$chartseries['labels'] = [];

// Testinformation = Summe der Fisherinfos pro Personability
// Mathe-Score = Wieviele Personen haben diese Personability in dieser Skala - jetzt aktuell, also im neuesten Score
// get_abilities_of_scale($catscaleid)
// context auch? default context of scale (parent) : eher nicht, würd ich meinen. aber param mitgeben.
// returns all records for this scale
// abilitycounter wie gehabt.
// foreach record ability runden und vergleichen und zählen.
// Prepare data for scorecounter bars.
$abilityrecords = $DB->get_records('local_catquiz_personparams', ['catscaleid' => $primarycatscale->id]);
$abilityseries = [];
foreach ($abilitysteps as $abilitystep) {
foreach ($abilitysteps as $as) {
$counter = 0;
foreach ($abilityrecords as $record) {
$counter = 0;
$a = floatval($record->ability);
$ability = round($a / 0.05) * 0.05;
if ($ability != $abilitystep) {
$ability = $this->round_to_customsteps($a, $abilitystep, $interval);
if ($ability != $as) {
continue;
} else {
$counter ++;
}
$counter ++;
}
$colorvalue = $this->get_color_for_personability(
$initialcontext['quizsettings'],
$abilitystep,
$as,
intval($primarycatscale->id)
);
$abilityseries['counter'][$abilitystep] = $counter;
$abilityseries['colors'][$abilitystep] = $colorvalue;
}

$aseries = new chart_series('label', $abilityseries['counter']);
$aseries->set_colors($abilityseries['colors']);

// This needs to be done with chart_bar.
// foreach ($fisherinfos as $ability => $fisherinfo) {
// $value = round($fisherinfo, 2);
// $series = new chart_series($ability, [0 => $value]);
// //$series->set_fill(true);
// $series->set_labels([0 => $ability]);

// $colorvalue = $this->get_color_for_personabily(
// $initialcontext['quizsettings'],
// floatval($ability),
// floatval($primarycatscale->id)
// );
// $series->set_colors([0 => $colorvalue]);
// $chart->add_series($series);
// $chart->set_labels([0 => get_string('abilityprofile', 'local_catquiz', $primarycatscale->name)]);
// };

// Could be done with chart_lines to add other line.
$abilitystring = strval($as);
$abilityseries['counter'][$abilitystring] = $counter;
$abilityseries['colors'][$abilitystring] = $colorvalue;
}
// Scale the values of $fisherinfos before creating chart series.
$scaledtiseries = $this->scalevalues(array_values($fisherinfos), array_values($abilityseries['counter']));

$aserieslabel = get_string('scalescorechartlabel', 'local_catquiz', $catscale->catscale->name);
$aseries = new chart_series($aserieslabel, array_values($abilityseries['counter']));
$aseries->set_colors(array_values($abilityseries['colors']));

$testinfolabel = get_string('testinfolabel', 'local_catquiz');
$series = new chart_series($testinfolabel, array_values($fisherinfos));
$series->set_type(\core\chart_series::TYPE_LINE);
$chart->add_series($series);
$tiseries = new chart_series($testinfolabel, $scaledtiseries);
$tiseries->set_type(\core\chart_series::TYPE_LINE);

$chart = new chart_bar();
$chart->add_series($tiseries);
$chart->add_series($aseries);
$chart->set_labels(array_keys($fisherinfos));

$out = $OUTPUT->render($chart);
Expand All @@ -423,6 +404,53 @@ private function render_abilityprofile_chart(array $initialcontext, $primarycats
];
}

/**
* Round float to steps as defined.
*
* @param float $number
* @param float $step
* @param float $interval
*
* @return float
*/
private function round_to_customsteps(float $number, float $step, float $interval):float {
$roundedvalue = round($number / $step) * $step;

// Exclude rounding to steps with 0.5
if ($roundedvalue - floor($roundedvalue) == 0.5) {
$roundedvalue = floor($roundedvalue) + $step;
}

return $roundedvalue;
}

/**
* Scale values of testinfo (sum of fisherinfos) for better display in chart.
*
* @param array $fisherinfos
* @param array $attemptscounter
*
* @return array
*/
private function scalevalues($fisherinfos, $attemptscounter) {
// Find the maximum values in arrays.
$maxattempts = max($attemptscounter);
$maxfisherinfo = max($fisherinfos);

// Avoid division by zero.
if ($maxfisherinfo == 0 || $maxattempts == 0) {
return $fisherinfos;
}

$scalingfactor = $maxattempts / $maxfisherinfo;

// Scale the values in $fisherinfos based on the scaling factor.
foreach ($fisherinfos as &$value) {
$value *= $scalingfactor;
}
return $fisherinfos;
}

/**
* Render chart for personabilities.
*
Expand Down
1 change: 1 addition & 0 deletions lang/de/local_catquiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@
$string['labelforrelativepersonabilitychart'] = 'Relative Fähigkeit';
$string['personabilityrangestring'] = '{$a->rangestart} - {$a->rangeend}';
$string['testinfolabel'] = 'Testinformation';
$string['scalescorechartlabel'] = '{$a}-Score';

// Check display line breaks etc.
$string['choosesubscaleforfeedback_help'] = 'Für die angezeigten Skalen können Sie nun {$a} Feedback-Angaben hinterlegen. Wählen Sie die jeweilige (Sub-)Skala an, um Ihr Feedback einzugeben. Die farbigen Symbole zeigen Ihnen den aktuellen Stand der Bearbeitung an, gemessen an den vor Ihnen hinterlegten Anzahl an Feedback-Optionen:
Expand Down
1 change: 1 addition & 0 deletions lang/en/local_catquiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@
$string['attemptchartstitle'] = 'Number and results of attempts in scale "{$a}"';
$string['personabilityrangestring'] = '{$a->rangestart} - {$a->rangeend}';
$string['testinfolabel'] = 'Test information';
$string['scalescorechartlabel'] = '{$a}-Score';

// Check display line breaks etc.
$string['choosesubscaleforfeedback_help'] = 'You can now store <number of options> feedback informations for the subscales displayed. Select a (sub-)scale to enter your feedback. The colored symbols indicate the current status of processing, measured by the number of feedback options you entered:
Expand Down

0 comments on commit e5566af

Please sign in to comment.