Skip to content

Gradebook: Fix graph generation outside cache folder - refs BT#22658 #6324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 54 additions & 60 deletions public/main/gradebook/lib/fe/gradebooktable.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1057,50 +1057,43 @@ public function get_table_data($from = 1, $perPage = null, $column = null, $dire
*/
public function getGraph()
{
// Retrieve the data needed for the graph (student scores, average, categories)
$data = $this->getDataForGraph();

if (!empty($data) &&
isset($data['categories']) &&
isset($data['my_result']) &&
isset($data['average'])
) {
// Prepare the data set with student's result, average, and categories
$dataSet = new pData();
$dataSet->addPoints($data['my_result'], get_lang('Me'));
// In order to generate random values
// $data['average'] = array(rand(0,50), rand(0,50));
$dataSet->addPoints($data['average'], get_lang('Average'));
$dataSet->addPoints($data['categories'], 'categories');
$dataSet->setAbscissa('categories');

// Create the graph image
$xSize = 700;
$ySize = 500;
$pChart = new pImage($xSize, $ySize, $dataSet);
/* Turn of Antialiasing */

// Set visual options: disable antialiasing, draw border and title
$pChart->Antialias = false;
$pChart->drawRectangle(0, 0, $xSize - 1, $ySize - 1, ["R" => 0, "G" => 0, "B" => 0]);
$pChart->drawText(80, 16, get_lang('Results and feedback'), [
"FontSize" => 11,
"Align" => TEXT_ALIGN_BOTTOMMIDDLE,
]);

/* Add a border to the picture */
$pChart->drawRectangle(
0,
0,
$xSize - 1,
$ySize - 1,
["R" => 0, "G" => 0, "B" => 0]
);
$pChart->drawText(
80,
16,
get_lang('Results and feedback'),
["FontSize" => 11, "Align" => TEXT_ALIGN_BOTTOMMIDDLE]
);
// Define graph area and font properties
$pChart->setGraphArea(50, 30, $xSize - 50, $ySize - 70);
$pChart->setFontProperties(
[
'FontName' => api_get_path(SYS_FONTS_PATH).'Harmattan/Harmattan-Regular.ttf',
/*'FontName' => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf',*/
'FontSize' => 10,
]
);
$pChart->setFontProperties([
'FontName' => api_get_path(SYS_FONTS_PATH).'Harmattan/Harmattan-Regular.ttf',
'FontSize' => 10,
]);

/* Draw the scale */
$scaleSettings = [
// Draw axes and data
$pChart->drawScale([
"XMargin" => AUTO,
"YMargin" => 10,
"Floating" => true,
Expand All @@ -1110,52 +1103,53 @@ public function getGraph()
"DrawSubTicks" => true,
"CycleBackground" => true,
'LabelRotation' => 10,
];
$pChart->drawScale($scaleSettings);

/* Draw the line chart */
]);
$pChart->drawLineChart();
$pChart->drawPlotChart(
[
"DisplayValues" => true,
"PlotBorder" => true,
"BorderSize" => 2,
"Surrounding" => -60,
"BorderAlpha" => 80,
]
);

/* Write the chart legend */
$pChart->drawLegend(
$xSize - 180,
9,
[
"Style" => LEGEND_NOBORDER,
"Mode" => LEGEND_HORIZONTAL,
"FontR" => 0,
"FontG" => 0,
"FontB" => 0,
]
);
$pChart->drawPlotChart([
"DisplayValues" => true,
"PlotBorder" => true,
"BorderSize" => 2,
"Surrounding" => -60,
"BorderAlpha" => 80,
]);

// Add a legend to the graph
$pChart->drawLegend($xSize - 180, 9, [
"Style" => LEGEND_NOBORDER,
"Mode" => LEGEND_HORIZONTAL,
"FontR" => 0,
"FontG" => 0,
"FontB" => 0,
]);

// Define a path to store the generated image file
$cachePath = api_get_path(SYS_ARCHIVE_PATH).'chart/';
if (!file_exists($cachePath)) {
mkdir($cachePath, 0755, true);
}
if (!is_writable($cachePath)) {
chmod($cachePath, 0755);
}

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

$myCache->writeToCache($chartHash, $pChart);
$imgSysPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
$myCache->saveFromCache($chartHash, $imgSysPath);
$imgWebPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;

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

return $result;
return '<br /><div id="contentArea" style="text-align: center;">
<img src="data:image/png;base64,'.$base64.'" />
</div>';
}
}

// Return empty if data is missing or graph could not be created
return '';
}

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy