Browse Source

Upgrade pChart 2 for Dashboard plugin, student block, teacher block - refs CT#7408

Daniel Barreto 10 years ago
parent
commit
171bd5c950

+ 17 - 17
main/inc/lib/pchart/pChart.class.php

@@ -196,15 +196,15 @@
    /* This function create the background picture */
    function pChart($XSize,$YSize)
     {
-    	    
+
      $this->XSize   = $XSize;
-     $this->YSize   = $YSize;          
+     $this->YSize   = $YSize;
      $this->Picture = imagecreatetruecolor($XSize,$YSize);
-     $C_White = $this->AllocateColor($this->Picture,255,255,255);          
-     imagefilledrectangle($this->Picture,0,0,$XSize,$YSize,$C_White);     
+     $C_White = $this->AllocateColor($this->Picture,255,255,255);
+     imagefilledrectangle($this->Picture,0,0,$XSize,$YSize,$C_White);
      imagecolortransparent($this->Picture,$C_White);
      $this->setFontProperties("tahoma.ttf",8);
-          
+
     }
 
   /* Set if warnings should be reported */
@@ -2002,18 +2002,18 @@
               }
 
              if ( $Shadow && $Alpha == 100 )
-                          	
+
               $X1 = $YZero;
 			  $Y1 = $XPos+1;
 			  $X2 = $YPos;
 			  $Y2 =	$XPos+$SeriesWidth-1;
-             
+
              $this->drawRectangle($XPos+1,$YZero,$XPos+$SeriesWidth-1,$YPos,25,25,25,TRUE,$Alpha);
              $this->drawFilledRectangle($XPos+1,$YZero,$XPos+$SeriesWidth-1,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE,$Alpha);
              //$this->drawRectangle($X1,$Y1,$X2,$Y2,$YPos,25,25,25,TRUE,$Alpha);
              //$this->drawFilledRectangle($X1,$Y1,$X2,$Y2,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE,$Alpha);
-             
-             
+
+
             }
           }
          $XPos = $XPos + $this->DivisionWidth;
@@ -2021,7 +2021,7 @@
        $SerieID++;
       }
     }
-   
+
 
 
    /* This function draw a bar graph */
@@ -2923,13 +2923,13 @@
 
      $X1=$X1-.2;$Y1=$Y1-.2;
      $X2=$X2+.2;$Y2=$Y2+.2;
-     
+
      $this->drawLine($X1,$Y1,$X2,$Y1,$R,$G,$B);
      $this->drawLine($X2,$Y1,$X2,$Y2,$R,$G,$B);
      $this->drawLine($X2,$Y2,$X1,$Y2,$R,$G,$B);
      $this->drawLine($X1,$Y2,$X1,$Y1,$R,$G,$B);
-     
-     
+
+
     }
 
    /* This function create a filled rectangle with antialias */
@@ -3074,10 +3074,10 @@
      $this->drawLine($X2-$Radius,$Y2,$X1+$Radius,$Y2,$R,$G,$B);
      $this->drawLine($X1,$Y2-$Radius,$X1,$Y1+$Radius,$R,$G,$B);
     }
-    
-    
-    
-    
+
+
+
+
 
    /* This function create a circle with antialias */
    function drawCircle($Xc,$Yc,$Height,$R,$G,$B,$Width=0)

+ 9 - 9
main/inc/lib/pchart/pData.class.php

@@ -122,11 +122,11 @@
      if (is_array($Value) && count($Value) == 1)
       $Value = $Value[0];
 
-     $ID = 0;          
+     $ID = 0;
      for ($i=0;$i<=count($this->Data);$i++) {
-     	if(isset($this->Data[$i][$Serie])) { $ID = $i+1; } 
+     	if(isset($this->Data[$i][$Serie])) { $ID = $i+1; }
      }
-     
+
      if ( count($Value) == 1 ) {
        $this->Data[$ID][$Serie] = $Value;
        if ( $Description != "" ) $this->Data[$ID]["Name"] = $Description;
@@ -139,10 +139,10 @@
          $ID++;
         }
       }
-      
-      
-      
-      
+
+
+
+
     }
 
 
@@ -164,7 +164,7 @@
     }
 
    function AddAllSeries()
-    {    	
+    {
      unset($this->DataDescription["Values"]);
      if ( isset($this->Data[0]) )
       {
@@ -174,7 +174,7 @@
           $this->DataDescription["Values"][] = $Key;
         }
       }
-      
+
     }
 
 

+ 102 - 64
plugin/dashboard/block_student_graph/block_student_graph.class.php

@@ -14,10 +14,9 @@
  */
 
 require_once api_get_path(LIBRARY_PATH).'attendance.lib.php';
-require_once api_get_path(LIBRARY_PATH).'pchart/pData.class.php';
-require_once api_get_path(LIBRARY_PATH).'pchart/pChart.class.php';
-require_once api_get_path(LIBRARY_PATH).'pchart/pCache.class.php';
-require_once api_get_path(LIBRARY_PATH).'pchart/MyHorBar.class.php';
+require_once api_get_path(LIBRARY_PATH).'pChart2/class/pData.class.php';
+require_once api_get_path(LIBRARY_PATH).'pChart2/class/pDraw.class.php';
+require_once api_get_path(LIBRARY_PATH).'pChart2/class/pCache.class.php';
 require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/gradebookitem.class.php';
 require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/evaluation.class.php';
 require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/result.class.php';
@@ -139,69 +138,108 @@ class BlockStudentGraph extends Block
 
 		if (is_array($usernames) && count($usernames) > 0) {
 
-			// Defining data
-			$data_set = new pData;
-
-			$data_set->AddPoint($faults,"Promedio");
-			$data_set->AddPoint($usernames,"Usuario");
-			$data_set->AddAllSeries();
-			$data_set->SetAbsciseLabelSerie("Usuario");
-
-			// prepare cache for saving image
-			$graph_id = $this->user_id.'StudentEvaluationGraph';  	// the graph id
-			$cache = new pCache();
-
-			$data = $data_set->GetData();	// return $this->DataDescription
-
-			if ($cache->IsInCache($graph_id, $data_set->GetData())) {
-			//if (0) {
-				//if we already created the img
-				$img_file = $cache->GetHash($graph_id, $data_set->GetData());  // image file with hash
-			} else {
-
-				if (count($usernames) < 5) {
-					$height = 200;
-				} else {
-					$height = (count($usernames)*40);
-				}
-
-				// Initialise the graph
-				$test = new MyHorBar(400,($height+30));
-				$test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf', 8);
-				$test->setGraphArea(100,30,370,$height);
-
-				$test->drawFilledRoundedRectangle(7,7,393,$height,5,240,240,240);
-				$test->drawRoundedRectangle(5,5,395,$height,5,230,230,230);
-				$test->drawGraphArea(255,255,255,TRUE);
-
-				//X axis
-				$test->setFixedScale(0,100,10);
-				//var_dump($data_set->GetDataDescription());
-				$test->drawHorScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_ADDALL,150,150,150,TRUE,0,0,TRUE);
-				$test->setColorPalette(0,255,0,0);
-				$test->drawHorGrid(10,TRUE,230,230,230,50);
-
-				// Draw the 0 line
-				$test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf', 6);
-				$test->drawTreshold(0,143,55,72,TRUE,TRUE);
-
-				// Draw the bar graph
-				$test->drawHorBarGraph($data_set->GetData(),$data_set->GetDataDescription(),TRUE,50);
-
-				$cache->WriteToCache($graph_id, $data_set->GetData(), $test);
-
-				ob_start();
-				$test->Stroke();
-				ob_end_clean();
-
-				$img_file = $cache->GetHash($graph_id, $data_set->GetData());
-			}
-			if (!empty($img_file)) {
-				$graph = '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'">';
-			}
+            // Defining data
+            $dataSet = new pData();
+            $dataSet->addPoints($faults, 'Serie1');
+            $dataSet->addPoints($usernames, 'Labels');
+            $dataSet->SetSerieDescription('Series1', get_lang('Average'));
+            $dataSet->SetSerieDescription('Labels', get_lang('User'));
+            $dataSet->setAbscissa('Labels');
+            $dataSet->setAbscissaName(get_lang('User'));
+            $dataSet->SetAxisName(0, get_lang('Attendance'));
+            $palette = array(
+                '0' => array('R' => 188, 'G' => 224, 'B' => 46, 'Alpha' => 100),
+                '1' => array('R' => 224, 'G' => 100, 'B' => 46, 'Alpha' => 100),
+                '2' => array('R' => 224, 'G' => 214, 'B' => 46, 'Alpha' => 100),
+                '3' => array('R' => 46, 'G' => 151, 'B' => 224, 'Alpha' => 100),
+                '4' => array('R' => 176, 'G' => 46, 'B' => 224, 'Alpha' => 100),
+                '5' => array('R' => 224, 'G' => 46, 'B' => 117, 'Alpha' => 100),
+                '6' => array('R' => 92, 'G' => 224, 'B' => 46, 'Alpha' => 100),
+                '7' => array('R' => 224, 'G' => 176, 'B' => 46, 'Alpha' => 100)
+            );
+            // 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 {
+
+                $maxCounts = max(count($usernames), count($faults));
+                if ($maxCounts < 5) {
+                    $heightSize = 200;
+                } else {
+                    $heightSize = $maxCounts * 40;
+                }
+
+                /* Create the pChart object */
+                $widthSize = 480;
+                $angle = 40;
+
+                $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(LIBRARY_PATH) . 'pChart2/fonts/verdana.ttf', 'FontSize' => 10));
+
+                /* Do NOT Write the chart title */
+
+                /* Define the chart area */
+                $myPicture->setGraphArea(80, 40, $widthSize - 20, $heightSize - 40);
+
+                /* Draw the scale */
+                $scaleSettings = array(
+                    'GridR' => 200,
+                    'GridG' => 200,
+                    'GridB' => 200,
+                    'DrawSubTicks' => true,
+                    'CycleBackground' => true,
+                    'Mode' => SCALE_MODE_ADDALL_START0,
+                    'Pos' => SCALE_POS_TOPBOTTOM,
+                    'DrawXLines' => false,
+                    'LabelRotation' => $angle,
+                );
+
+                $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(
+                    'OverrideColors' => $palette,
+                    'Gradient' => false,
+                    'GradientMode' => GRADIENT_SIMPLE,
+                    'DisplayPos' => LABEL_POS_TOP,
+                    'DisplayValues' => true,
+                    'DisplayR' => 0,
+                    'DisplayG' => 0,
+                    'DisplayB' => 0,
+                    'DisplayShadow' => true,
+                    'Surrounding' => 10,
+                );
+                $myPicture->drawBarChart($settings);
+
+                /* Render the picture (choose the best way) */
+
+                $myCache->writeToCache($chartHash, $myPicture);
+                $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
+                $myCache->saveFromCache($chartHash, $imgPath);
+                $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
+            }
+            $graph = '<img src="' . $imgPath . '" >';
 		} else {
 			$graph = '<p>'.api_convert_encoding(get_lang('GraphicNotAvailable'),'UTF-8').'</p>';
 		}
+
  		return $graph;
  	}
 

+ 71 - 64
plugin/dashboard/block_teacher_graph/block_teacher_graph.class.php

@@ -12,9 +12,9 @@
 require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
 require_once api_get_path(LIBRARY_PATH).'course.lib.php';
 require_once api_get_path(LIBRARY_PATH).'tracking.lib.php';
-require_once api_get_path(LIBRARY_PATH).'pchart/pData.class.php';
-require_once api_get_path(LIBRARY_PATH).'pchart/pChart.class.php';
-require_once api_get_path(LIBRARY_PATH).'pchart/pCache.class.php';
+require_once api_get_path(LIBRARY_PATH) . 'pChart2/class/pData.class.php';
+require_once api_get_path(LIBRARY_PATH) . 'pChart2/class/pDraw.class.php';
+require_once api_get_path(LIBRARY_PATH) . 'pChart2/class/pCache.class.php';
 
 /**
  * This class is used like controller for teacher graph block plugin,
@@ -100,7 +100,7 @@ class BlockTeacherGraph extends Block
  		$a_last_week = get_last_week();
 
 		if (is_array($user_ids) && count($user_ids) > 0) {
-			$data_set = new pData;
+			$dataSet = new pData;
 			foreach ($user_ids as $user_id) {
 				$teacher_info = api_get_user_info($user_id);
 				$username = $teacher_info['username'];
@@ -115,8 +115,7 @@ class BlockTeacherGraph extends Block
 					$min = floor(($time_on_platform_by_day - ($hours * 3600)) / 60);
 					$time_by_days[] = $min;
 				}
-				$data_set->AddPoint($time_by_days,$username);
-				$data_set->AddSerie($username);
+				$dataSet->addPoints($time_by_days, $username);
 			}
 
 			$last_week 	 = date('Y-m-d',$a_last_week[0]).' '.get_lang('To').' '.date('Y-m-d', $a_last_week[6]);
@@ -125,68 +124,76 @@ class BlockTeacherGraph extends Block
 				$days_on_week[] = date('d/m',$weekday);
 			}
 
-			$data_set->AddPoint($days_on_week,"Days");
-			$data_set->SetXAxisName($last_week);
-			$data_set->SetYAxisName(get_lang('Minutes'));
-
-			$data_set->SetAbsciseLabelSerie("Days");
-			$graph_id = $this->user_id.'TeacherConnectionsGraph';
-
-			$cache = new pCache();
-			// the graph id
-			$data = $data_set->GetData();
-
-			if ($cache->IsInCache($graph_id, $data_set->GetData())) {
-				//if we already created the img
-				$img_file = $cache->GetHash($graph_id, $data_set->GetData());
-			} else {
-
-				// Initializing the graph
-				$bg_width = 440;
-				$bg_height = 350;
-                $angle = -30;
-				$test = new pChart($bg_width+10,$bg_height+20);
-				$test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
-                $test = $test->fixHeightByRotation($data_set->GetData(), $data_set->GetDataDescription(), $angle);
-				$test->setGraphArea(65,30,$bg_width-70,$bg_height-50);
-				$test->drawFilledRoundedRectangle(7,7,$bg_width,$bg_height,5,240,240,240);
-				$test->drawRoundedRectangle(5,5,$bg_width+2,$bg_height+2,5,230,230,230);
-				$test->drawGraphArea(255,255,255,TRUE);
-                $test->drawScale(
-                    $data_set->GetData(),
-                    $data_set->GetDataDescription(),
-                    SCALE_NORMAL,
-                    150,
-                    150,
-                    150,
-                    TRUE,
-                    $angle,
-                    2,
-                    TRUE
+			$dataSet->addPoints($days_on_week, 'Days');
+			$dataSet->setAbscissaName($last_week);
+			$dataSet->setAxisName(0, get_lang('Minutes'));
+            $dataSet->setAbscissa('Days');
+
+            // 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 = 440;
+                $heightSize = 350;
+                $angle = 50;
+
+                $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(LIBRARY_PATH) . 'pChart2/fonts/verdana.ttf', 'FontSize' => 10));
+
+                /* Do NOT Write the chart title */
+
+                /* Define the chart area */
+                $myPicture->setGraphArea(40, 40, $widthSize - 20, $heightSize - 80);
+
+                /* Draw the scale */
+                $scaleSettings = array(
+                    'GridR' => 200,
+                    'GridG' => 200,
+                    'GridB' => 200,
+                    'DrawSubTicks' => true,
+                    'CycleBackground' => true,
+                    'Mode' => SCALE_MODE_ADDALL_START0,
+                    'LabelRotation' => $angle,
                 );
-				$test->drawGrid(4,TRUE,230,230,230,50);
 
-				// Drawing lines
-				//$test->drawLineGraph($data_set->GetData(),$data_set->GetDataDescription());
-				$test->drawFilledCubicCurve($data_set->GetData(),$data_set->GetDataDescription(),.1,30);
-				//$test->drawPlotGraph($data_set->GetData(),$data_set->GetDataDescription(),3,2,255,255,255);
+                $myPicture->drawScale($scaleSettings);
 
-				// Drawing Legend
-				$test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
-				$test->drawLegend($bg_width-80,20,$data_set->GetDataDescription(),204,204,255);
+                /* Turn on shadow computing */
+                $myPicture->setShadow(true, array('X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10));
 
-				$test->writeValues($data_set->GetData(),$data_set->GetDataDescription(),array("Days"));
-
-				$cache->WriteToCache($graph_id, $data_set->GetData(), $test);
-				ob_start();
-				$test->Stroke();
-				ob_end_clean();
-				$img_file = $cache->GetHash($graph_id, $data_set->GetData());
-
-			}
-			if (!empty($img_file)) {
-				$graph = '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'">';
-			}
+                /* Draw the chart */
+                $myPicture->setShadow(true, array('X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10));
+                $settings = array(
+                    'DisplayValues' => true,
+                    'DisplayR' => 0,
+                    'DisplayG' => 0,
+                    'DisplayB' => 0,
+                );
+                $myPicture->drawFilledSplineChart($settings);
+                $myPicture->drawLegend(40, 20, array('Mode' => LEGEND_HORIZONTAL));
+
+                /* Render the picture (choose the best way) */
+                $myCache->writeToCache($chartHash, $myPicture);
+                $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
+                $myCache->saveFromCache($chartHash, $imgPath);
+                $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
+            }
+            $graph = '<img src="' . $imgPath . '" >';
 		} else {
 			$graph = '<p>'.api_convert_encoding(get_lang('GraphicNotAvailable'),'UTF-8').'</p>';
 		}