dokeos
[ class tree: dokeos ] [ index: dokeos ] [ all elements ]

Source for file gradebook_data_generator.class.php

Documentation is available at gradebook_data_generator.class.php

  1. <?php
  2.  
  3. /**
  4.  * Class to select, sort and transform object data into array data,
  5.  * used for the general gradebook view
  6.  * @author Bert Stepp�
  7.  */
  8. {
  9.  
  10.     // Sorting types constants
  11.     const GDG_SORT_TYPE 1;
  12.     const GDG_SORT_NAME 2;
  13.     const GDG_SORT_DESCRIPTION 4;
  14.     const GDG_SORT_WEIGHT 8;
  15.     const GDG_SORT_DATE 16;
  16.  
  17.     const GDG_SORT_ASC 32;
  18.     const GDG_SORT_DESC 64;
  19.  
  20.  
  21.     private $items;
  22.  
  23.  
  24.     function GradebookDataGenerator($cats array()$evals array()$links array())
  25.     {
  26.         $allcats (isset($cats$cats array());
  27.         $allevals (isset($evals$evals array());
  28.         $alllinks (isset($links$links array());
  29.  
  30.         // merge categories, evaluations and links
  31.         $this->items = array_merge($allcats$allevals$alllinks);
  32.     }
  33.  
  34.  
  35.     /**
  36.      * Get total number of items (rows)
  37.      */
  38.     public function get_total_items_count()
  39.     {
  40.         return count($this->items);
  41.     }
  42.  
  43.  
  44.  
  45.  
  46.     /**
  47.      * Get actual array data
  48.      * @return array 2-dimensional array - each array contains the elements:
  49.      *  0: cat/eval/link object
  50.      *  1: item name
  51.      *  2: description
  52.      *  3: weight
  53.      *  4: date
  54.      *  5: student's score (if student logged in)
  55.      */
  56.     public function get_data ($sorting 0$start 0$count null$ignore_score_color false)
  57.     {
  58.         
  59.         // do some checks on count, redefine if invalid value
  60.         if (!isset($count))
  61.             $count count ($this->items$start;
  62.         if ($count 0)
  63.             $count 0;
  64.  
  65.         $allitems $this->items;
  66.  
  67.         // sort array
  68.         if ($sorting self :: GDG_SORT_TYPE)
  69.             usort($allitemsarray('GradebookDataGenerator''sort_by_type'));
  70.         elseif ($sorting self :: GDG_SORT_NAME)
  71.             usort($allitemsarray('GradebookDataGenerator''sort_by_name'));
  72.         elseif ($sorting self :: GDG_SORT_DESCRIPTION)
  73.             usort($allitemsarray('GradebookDataGenerator''sort_by_description'));
  74.         elseif ($sorting self :: GDG_SORT_WEIGHT)
  75.             usort($allitemsarray('GradebookDataGenerator''sort_by_weight'));
  76.         elseif ($sorting self :: GDG_SORT_DATE)
  77.             usort($allitemsarray('GradebookDataGenerator''sort_by_date'));
  78.  
  79.         if ($sorting self :: GDG_SORT_DESC)
  80.             $allitems array_reverse($allitems);
  81.  
  82.  
  83.         // get selected items
  84.         $visibleitems array_slice($allitems$start$count);
  85.  
  86.  
  87.         // generate the data to display
  88.         $data array();
  89.         foreach ($visibleitems as $item)
  90.         {
  91.             $row array ();
  92.             $row[$item;
  93.             $row[$item->get_name();
  94.             $row[$item->get_description();
  95.             $row[$item->get_weight();
  96.             $row[$this->build_date_column ($item);
  97.             if (!api_is_allowed_to_create_course())
  98.                 $row[$this->build_result_column ($item$ignore_score_color);
  99.                 $row[$this->get_certificate_link ($item);
  100.             $data[$row;
  101.         }
  102.         
  103.         return $data;
  104.  
  105.     }
  106.  
  107.     /**
  108.      * Returns the link to the certificate generation, if the score is enough, otherwise
  109.      * returns an empty string. This only works with categories.
  110.      * @param    object Item 
  111.      */
  112.     function get_certificate_link($item)
  113.     {
  114.         if(is_a($item'Category'))
  115.         {
  116.             if($item->is_certificate_available(api_get_user_id()))
  117.             {
  118.                 $link '<a href="gradebook.php?export_certificate=1&cat='.$item->get_id().'&user='.api_get_user_id().'">'.get_lang('Certificate').'</a>';
  119.                 return $link;
  120.             }
  121.         }
  122.         return '';
  123.     }
  124.  
  125.  
  126.  
  127.  
  128. // Sort functions
  129. // Make sure to only use functions as defined in the GradebookItem interface !
  130.  
  131.     function sort_by_name($item1$item2)
  132.     {
  133.         if (strtolower($item1->get_name()) == strtolower($item2->get_name()))
  134.             return 0;
  135.         else
  136.             return (strtolower($item1->get_name()) strtolower($item2->get_name()) ? -1);
  137.     }
  138.  
  139.     function sort_by_type($item1$item2)
  140.     {
  141.         if ($item1->get_item_type(== $item2->get_item_type())
  142.             return $this->sort_by_name($item1,$item2);
  143.         else
  144.             return ($item1->get_item_type($item2->get_item_type(? -1);
  145.     }
  146.     
  147.     function sort_by_description($item1$item2)
  148.     {
  149.         if (strtolower($item1->get_description()) == strtolower($item2->get_description()))
  150.             return $this->sort_by_name($item1,$item2);
  151.         else
  152.             return (strtolower($item1->get_description()) strtolower($item2->get_description()) ? -1);
  153.     }
  154.     
  155.     function sort_by_weight($item1$item2)
  156.     {
  157.         if ($item1->get_weight(== $item2->get_weight())
  158.             return $this->sort_by_name($item1,$item2);
  159.         else
  160.             return ($item1->get_weight($item2->get_weight(? -1);
  161.     }
  162.     
  163.     function sort_by_date($item1$item2)
  164.     {
  165.         if ($item1->get_date(== $item2->get_date())
  166.             return $this->sort_by_name($item1,$item2);
  167.         else
  168.             return ($item1->get_date($item2->get_date(? -1);
  169.     }
  170.     
  171.  
  172. // Other functions
  173.  
  174.  
  175.     private function build_result_column ($item$ignore_score_color)
  176.     {
  177.         $scoredisplay ScoreDisplay :: instance();
  178.         $score $item->calc_score(api_get_user_id());
  179.  
  180.         switch ($item->get_item_type())
  181.         {
  182.             // category
  183.             case 'C' :
  184.                 if ($score != null)
  185.                 {
  186.                     $displaytype SCORE_PERCENT;
  187.                     if ($ignore_score_color)
  188.                         $displaytype |= SCORE_IGNORE_SPLIT;
  189.                     return get_lang('Total'' : '
  190.                          . $scoredisplay->display_score($score,$displaytype);
  191.                 }
  192.                 else
  193.                     return '';
  194.  
  195.             // evaluation and link
  196.             case 'E' :
  197.             case 'L' :
  198.                 $displaytype SCORE_DIV_PERCENT;
  199.                 if ($ignore_score_color)
  200.                     $displaytype |= SCORE_IGNORE_SPLIT;
  201.                 return $scoredisplay->display_score($score,$displaytype);
  202.         }
  203.     }
  204.  
  205.     private function build_date_column ($item)
  206.     {
  207.         $date $item->get_date();
  208.         if (!isset($date|| empty($date))
  209.             return '';
  210.         else
  211.             return date("j/n/Y g:i"$date);
  212.     }
  213.  
  214.  
  215. }
  216. ?>

Documentation generated on Thu, 12 Jun 2008 13:34:30 -0500 by phpDocumentor 1.4.1