Skip to content

Commit 8f88808

Browse files
Gradebook: Fix graph generation outside cache folder - refs BT#22658
1 parent 7dbcd74 commit 8f88808

File tree

1 file changed

+54
-60
lines changed

1 file changed

+54
-60
lines changed

public/main/gradebook/lib/fe/gradebooktable.class.php

Lines changed: 54 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,50 +1057,43 @@ public function get_table_data($from = 1, $perPage = null, $column = null, $dire
10571057
*/
10581058
public function getGraph()
10591059
{
1060+
// Retrieve the data needed for the graph (student scores, average, categories)
10601061
$data = $this->getDataForGraph();
1062+
10611063
if (!empty($data) &&
10621064
isset($data['categories']) &&
10631065
isset($data['my_result']) &&
10641066
isset($data['average'])
10651067
) {
1068+
// Prepare the data set with student's result, average, and categories
10661069
$dataSet = new pData();
10671070
$dataSet->addPoints($data['my_result'], get_lang('Me'));
1068-
// In order to generate random values
1069-
// $data['average'] = array(rand(0,50), rand(0,50));
10701071
$dataSet->addPoints($data['average'], get_lang('Average'));
10711072
$dataSet->addPoints($data['categories'], 'categories');
10721073
$dataSet->setAbscissa('categories');
1074+
1075+
// Create the graph image
10731076
$xSize = 700;
10741077
$ySize = 500;
10751078
$pChart = new pImage($xSize, $ySize, $dataSet);
1076-
/* Turn of Antialiasing */
1079+
1080+
// Set visual options: disable antialiasing, draw border and title
10771081
$pChart->Antialias = false;
1082+
$pChart->drawRectangle(0, 0, $xSize - 1, $ySize - 1, ["R" => 0, "G" => 0, "B" => 0]);
1083+
$pChart->drawText(80, 16, get_lang('Results and feedback'), [
1084+
"FontSize" => 11,
1085+
"Align" => TEXT_ALIGN_BOTTOMMIDDLE,
1086+
]);
10781087

1079-
/* Add a border to the picture */
1080-
$pChart->drawRectangle(
1081-
0,
1082-
0,
1083-
$xSize - 1,
1084-
$ySize - 1,
1085-
["R" => 0, "G" => 0, "B" => 0]
1086-
);
1087-
$pChart->drawText(
1088-
80,
1089-
16,
1090-
get_lang('Results and feedback'),
1091-
["FontSize" => 11, "Align" => TEXT_ALIGN_BOTTOMMIDDLE]
1092-
);
1088+
// Define graph area and font properties
10931089
$pChart->setGraphArea(50, 30, $xSize - 50, $ySize - 70);
1094-
$pChart->setFontProperties(
1095-
[
1096-
'FontName' => api_get_path(SYS_FONTS_PATH).'Harmattan/Harmattan-Regular.ttf',
1097-
/*'FontName' => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf',*/
1098-
'FontSize' => 10,
1099-
]
1100-
);
1090+
$pChart->setFontProperties([
1091+
'FontName' => api_get_path(SYS_FONTS_PATH).'Harmattan/Harmattan-Regular.ttf',
1092+
'FontSize' => 10,
1093+
]);
11011094

1102-
/* Draw the scale */
1103-
$scaleSettings = [
1095+
// Draw axes and data
1096+
$pChart->drawScale([
11041097
"XMargin" => AUTO,
11051098
"YMargin" => 10,
11061099
"Floating" => true,
@@ -1110,52 +1103,53 @@ public function getGraph()
11101103
"DrawSubTicks" => true,
11111104
"CycleBackground" => true,
11121105
'LabelRotation' => 10,
1113-
];
1114-
$pChart->drawScale($scaleSettings);
1115-
1116-
/* Draw the line chart */
1106+
]);
11171107
$pChart->drawLineChart();
1118-
$pChart->drawPlotChart(
1119-
[
1120-
"DisplayValues" => true,
1121-
"PlotBorder" => true,
1122-
"BorderSize" => 2,
1123-
"Surrounding" => -60,
1124-
"BorderAlpha" => 80,
1125-
]
1126-
);
1127-
1128-
/* Write the chart legend */
1129-
$pChart->drawLegend(
1130-
$xSize - 180,
1131-
9,
1132-
[
1133-
"Style" => LEGEND_NOBORDER,
1134-
"Mode" => LEGEND_HORIZONTAL,
1135-
"FontR" => 0,
1136-
"FontG" => 0,
1137-
"FontB" => 0,
1138-
]
1139-
);
1108+
$pChart->drawPlotChart([
1109+
"DisplayValues" => true,
1110+
"PlotBorder" => true,
1111+
"BorderSize" => 2,
1112+
"Surrounding" => -60,
1113+
"BorderAlpha" => 80,
1114+
]);
1115+
1116+
// Add a legend to the graph
1117+
$pChart->drawLegend($xSize - 180, 9, [
1118+
"Style" => LEGEND_NOBORDER,
1119+
"Mode" => LEGEND_HORIZONTAL,
1120+
"FontR" => 0,
1121+
"FontG" => 0,
1122+
"FontB" => 0,
1123+
]);
1124+
1125+
// Define a path to store the generated image file
1126+
$cachePath = api_get_path(SYS_ARCHIVE_PATH).'chart/';
1127+
if (!file_exists($cachePath)) {
1128+
mkdir($cachePath, 0755, true);
1129+
}
1130+
if (!is_writable($cachePath)) {
1131+
chmod($cachePath, 0755);
1132+
}
11401133

1141-
$cachePath = api_get_path(SYS_ARCHIVE_PATH);
1142-
$myCache = new pCache(['CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)]);
1134+
// Cache the chart to avoid regenerating the same image
1135+
$myCache = new pCache(['CacheFolder' => rtrim($cachePath, '/')]);
11431136
$chartHash = $myCache->getHash($dataSet);
1137+
$imgSysPath = $cachePath.$chartHash;
11441138

11451139
$myCache->writeToCache($chartHash, $pChart);
1146-
$imgSysPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
11471140
$myCache->saveFromCache($chartHash, $imgSysPath);
1148-
$imgWebPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
11491141

1142+
// Read the image and encode it as base64 to embed directly into HTML
11501143
if (file_exists($imgSysPath)) {
1151-
$result = '<br /><div id="contentArea" style="text-align: center;" >';
1152-
$result .= '<img src="'.$imgWebPath.'" >';
1153-
$result .= '</div>';
1144+
$base64 = base64_encode(file_get_contents($imgSysPath));
11541145

1155-
return $result;
1146+
return '<br /><div id="contentArea" style="text-align: center;">
1147+
<img src="data:image/png;base64,'.$base64.'" />
1148+
</div>';
11561149
}
11571150
}
11581151

1152+
// Return empty if data is missing or graph could not be created
11591153
return '';
11601154
}
11611155

0 commit comments

Comments
 (0)