コード例 #1
0
 /**
  * Generates a big graph with the number of best results
  * @param	array
  */
 static function generate_exercise_result_graph($attempts)
 {
     $exercise_title = strip_tags($attempts['title']);
     $attempts = $attempts['data'];
     $my_exercise_result_array = $exercise_result = array();
     if (empty($attempts)) {
         return null;
     }
     foreach ($attempts as $attempt) {
         if (api_get_user_id() == $attempt['exe_user_id']) {
             if ($attempt['exe_weighting'] != 0) {
                 $my_exercise_result_array[] = $attempt['exe_result'] / $attempt['exe_weighting'];
             }
         } else {
             if ($attempt['exe_weighting'] != 0) {
                 $exercise_result[] = $attempt['exe_result'] / $attempt['exe_weighting'];
             }
         }
     }
     //Getting best result
     rsort($my_exercise_result_array);
     $my_exercise_result = 0;
     if (isset($my_exercise_result_array[0])) {
         $my_exercise_result = $my_exercise_result_array[0] * 100;
     }
     $max = 100;
     $pieces = 5;
     $part = round($max / $pieces);
     $x_axis = array();
     $final_array = array();
     $my_final_array = array();
     for ($i = 1; $i <= $pieces; $i++) {
         $sum = 1;
         if ($i == 1) {
             $sum = 0;
         }
         $min = ($i - 1) * $part + $sum;
         $max = $i * $part;
         $x_axis[] = $min . " - " . $max;
         $count = 0;
         foreach ($exercise_result as $result) {
             $percentage = $result * 100;
             if ($percentage >= $min && $percentage <= $max) {
                 $count++;
             }
         }
         $final_array[] = $count;
         if ($my_exercise_result >= $min && $my_exercise_result <= $max) {
             $my_final_array[] = 1;
         } else {
             $my_final_array[] = 0;
         }
     }
     //Fix to remove the data of the user with my data
     for ($i = 0; $i <= count($my_final_array); $i++) {
         if (!empty($my_final_array[$i])) {
             $my_final_array[$i] = $final_array[$i] + 1;
             //Add my result
             $final_array[$i] = 0;
         }
     }
     // Dataset definition
     $dataSet = new pData();
     $dataSet->addPoints($final_array, 'Serie1');
     $dataSet->addPoints($my_final_array, 'Serie2');
     $dataSet->addPoints($x_axis, 'Serie3');
     $dataSet->setSerieDescription('Serie1', get_lang('Score'));
     $dataSet->setSerieDescription('Serie2', get_lang('MyResults'));
     $dataSet->setAbscissa('Serie3');
     $dataSet->setXAxisName(get_lang('Score'));
     $dataSet->normalize(100, "%");
     $dataSet->loadPalette(api_get_path(SYS_CODE_PATH) . 'palettes/pchart/default.color', true);
     // Cache definition
     $cachePath = api_get_path(SYS_ARCHIVE_PATH);
     $myCache = new pCache(array('CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)));
     $chartHash = $myCache->getHash($dataSet);
     if ($myCache->isInCache($chartHash)) {
         $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
         $myCache->saveFromCache($chartHash, $imgPath);
         $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
     } else {
         /* Create the pChart object */
         $widthSize = 480;
         $heightSize = 250;
         $fontSize = 8;
         $myPicture = new pImage($widthSize, $heightSize, $dataSet);
         /* Turn of Antialiasing */
         $myPicture->Antialias = false;
         /* Add a border to the picture */
         $myPicture->drawRectangle(0, 0, $widthSize - 1, $heightSize - 1, array('R' => 0, 'G' => 0, 'B' => 0));
         /* Set the default font */
         $myPicture->setFontProperties(array('FontName' => api_get_path(SYS_FONTS_PATH) . 'opensans/OpenSans-Regular.ttf', 'FontSize' => 10));
         /* Write the chart title */
         $myPicture->drawText(250, 20, $exercise_title, array('FontSize' => 12, 'Align' => TEXT_ALIGN_BOTTOMMIDDLE));
         /* Define the chart area */
         $myPicture->setGraphArea(50, 50, $widthSize - 20, $heightSize - 30);
         /* Draw the scale */
         $scaleSettings = array('GridR' => 200, 'GridG' => 200, 'GridB' => 200, 'DrawSubTicks' => true, 'CycleBackground' => true, 'Mode' => SCALE_MODE_MANUAL, 'ManualScale' => array('0' => array('Min' => 0, 'Max' => 100)));
         $myPicture->drawScale($scaleSettings);
         /* Turn on shadow computing */
         $myPicture->setShadow(true, array('X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10));
         /* Draw the chart */
         $myPicture->setShadow(true, array('X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10));
         $settings = array('DisplayValues' => true, 'DisplaySize' => $fontSize, 'DisplayR' => 0, 'DisplayG' => 0, 'DisplayB' => 0, 'DisplayOrientation' => ORIENTATION_HORIZONTAL, 'Gradient' => false, 'Surrounding' => 30, 'InnerSurrounding' => 25);
         $myPicture->drawStackedBarChart($settings);
         $legendSettings = array('Mode' => LEGEND_HORIZONTAL, 'Style' => LEGEND_NOBORDER);
         $myPicture->drawLegend($widthSize / 2, 30, $legendSettings);
         /* Write and save into cache */
         $myCache->writeToCache($chartHash, $myPicture);
         $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
         $myCache->saveFromCache($chartHash, $imgPath);
         $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
     }
     return $imgPath;
 }