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

Source for file abstractlink.class.php

Documentation is available at abstractlink.class.php

  1. <?php
  2.  
  3.  
  4. /**
  5.  * Defines a gradebook AbstractLink object.
  6.  * To implement specific links,
  7.  * extend this class and define a type in LinkFactory.
  8.  * Use the methods in LinkFactory to create link objects.
  9.  * @author Bert Stepp�
  10.  * @package dokeos.gradebook
  11.  */
  12. abstract class AbstractLink implements GradebookItem
  13. {
  14.  
  15. // PROPERTIES
  16.  
  17.     protected $id;
  18.     protected $type;
  19.     protected $ref_id;
  20.     protected $user_id;
  21.     protected $course_code;
  22.     protected $category;
  23.     protected $link_date;
  24.     protected $weight;
  25.     protected $visible;
  26.  
  27. // CONSTRUCTORS
  28.  
  29.     function AbstractLink()
  30.     {
  31.     }
  32.  
  33. // GETTERS AND SETTERS
  34.  
  35.        public function get_id()
  36.     {
  37.         return $this->id;
  38.     }
  39.  
  40.     public function get_type()
  41.     {
  42.         return $this->type;
  43.     }
  44.  
  45.     public function get_ref_id()
  46.     {
  47.         return $this->ref_id;
  48.     }
  49.  
  50.     public function get_user_id()
  51.     {
  52.         return $this->user_id;
  53.     }
  54.  
  55.     public function get_course_code()
  56.     {
  57.         return $this->course_code;
  58.     }
  59.  
  60.     public function get_category_id()
  61.     {
  62.         return $this->category;
  63.     }
  64.  
  65.     public function get_date()
  66.     {
  67.         return $this->link_date;
  68.     }
  69.  
  70.     public function get_weight()
  71.     {
  72.         return $this->weight;
  73.     }
  74.  
  75.     public function is_visible()
  76.     {
  77.         return $this->visible;
  78.     }
  79.  
  80.  
  81.  
  82.     public function set_id ($id)
  83.     {
  84.         $this->id = $id;
  85.     }
  86.     
  87.     public function set_type ($type)
  88.     {
  89.         $this->type = $type;
  90.     }
  91.     
  92.     public function set_ref_id ($ref_id)
  93.     {
  94.         $this->ref_id = $ref_id;
  95.     }
  96.     
  97.     public function set_user_id ($user_id)
  98.     {
  99.         $this->user_id = $user_id;
  100.     }
  101.     
  102.     public function set_course_code ($course_code)
  103.     {
  104.         $this->course_code = $course_code;
  105.     }
  106.     
  107.     public function set_category_id ($category_id)
  108.     {
  109.         $this->category = $category_id;
  110.     }
  111.     
  112.     public function set_date ($date)
  113.     {
  114.         $this->link_date = $date;
  115.     }
  116.     
  117.     public function set_weight ($weight)
  118.     {
  119.         $this->weight = $weight;
  120.     }
  121.     
  122.     public function set_visible ($visible)
  123.     {
  124.         $this->visible = $visible;
  125.     }
  126.     
  127.     
  128. // CRUD FUNCTIONS
  129.  
  130.     /**
  131.      * Retrieve links and return them as an array of extensions of AbstractLink.
  132.      * To keep consistency, do not call this method but LinkFactory::load instead.
  133.      */
  134.     public function load ($id null$type null$ref_id null$user_id null$course_code null$category_id null$visible null)
  135.     {
  136.         $tbl_grade_links Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  137.         $sql='SELECT id,type,ref_id,user_id,course_code,category_id,date,weight,visible FROM '.$tbl_grade_links;
  138.         $paramcount 0;
  139.         if (isset ($id))
  140.         {
  141.             $sql.= ' WHERE id = '.$id;
  142.             $paramcount ++;
  143.         }
  144.         if (isset ($type))
  145.         {
  146.             if ($paramcount != 0$sql .= ' AND';
  147.             else $sql .= ' WHERE';
  148.             $sql .= ' type = '.$type;
  149.             $paramcount ++;
  150.         }
  151.         if (isset ($ref_id))
  152.         {
  153.             if ($paramcount != 0$sql .= ' AND';
  154.             else $sql .= ' WHERE';
  155.             $sql .= ' ref_id = '.$ref_id;
  156.             $paramcount ++;
  157.         }
  158.         if (isset ($user_id))
  159.         {
  160.             if ($paramcount != 0$sql .= ' AND';
  161.             else $sql .= ' WHERE';
  162.             $sql .= ' user_id = '.$user_id;
  163.             $paramcount ++;
  164.         }
  165.         if (isset ($course_code))
  166.         {
  167.             if ($paramcount != 0$sql .= ' AND';
  168.             else $sql .= ' WHERE';
  169.             $sql .= " course_code = '".$course_code."'";
  170.             $paramcount ++;
  171.         }
  172.         if (isset ($category_id))
  173.         {
  174.             if ($paramcount != 0$sql .= ' AND';
  175.             else $sql .= ' WHERE';
  176.             $sql .= ' category_id = '.$category_id;
  177.             $paramcount ++;
  178.         }
  179.         if (isset ($visible))
  180.         {
  181.             if ($paramcount != 0$sql .= ' AND';
  182.             else $sql .= ' WHERE';
  183.             $sql .= ' visible = '.$visible;
  184.             $paramcount ++;
  185.         }
  186.  
  187.         $result api_sql_query($sql__FILE____LINE__);
  188.         $links AbstractLink::create_objects_from_mysql_result($result);
  189.         return $links;
  190.     }
  191.     
  192.     private function create_objects_from_mysql_result($result)
  193.     {
  194.         $links=array();
  195.         while ($data=mysql_fetch_array($result))
  196.         {
  197.             $link LinkFactory::create(intval($data['type']));
  198.             $link->set_id($data['id']);
  199.             $link->set_type($data['type']);
  200.             $link->set_ref_id($data['ref_id']);
  201.             $link->set_user_id($data['user_id']);
  202.             $link->set_course_code($data['course_code']);
  203.             $link->set_category_id($data['category_id']);
  204.             $link->set_date($data['date']);
  205.             $link->set_weight($data['weight']);
  206.             $link->set_visible($data['visible']);
  207.             $links[]=$link;
  208.         }
  209.         return $links;
  210.     }
  211.     
  212.     /**
  213.      * Insert this link into the database
  214.      */
  215.     public function add()
  216.     {
  217.         $this->add_linked_data();
  218.  
  219.         if (isset($this->type&& isset($this->ref_id&& isset($this->user_id&& isset($this->course_code)
  220.          && isset($this->category&& isset($this->weight&& isset($this->visible))
  221.         {
  222.             $tbl_grade_links Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  223.             
  224.             $sql 'INSERT INTO '.$tbl_grade_links
  225.                     .' (type,ref_id,user_id,course_code,category_id,weight,visible';
  226.             if (isset($this->link_date)) $sql .= ',date';
  227.             $sql .= ') VALUES ('
  228.                     .$this->get_type()
  229.                     .','.$this->get_ref_id()
  230.                     .','.$this->get_user_id()
  231.                     .",'".$this->get_course_code()."'"
  232.                     .','.$this->get_category_id()
  233.                     .','.$this->get_weight()
  234.                     .','.$this->is_visible();
  235.             if (isset($this->link_date)) $sql .= ','.$this->get_date();
  236.             $sql .= ")";
  237.  
  238.             api_sql_query($sql__FILE____LINE__);
  239.             $this->set_id(mysql_insert_id());
  240.         }
  241.         else
  242.             die('Error in AbstractLink add: required field empty');
  243.     }
  244.     
  245.     /**
  246.      * Update the properties of this link in the database
  247.      */
  248.     public function save()
  249.     {
  250.         $this->save_linked_data();
  251.  
  252.         $tbl_grade_links Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  253.         $sql 'UPDATE '.$tbl_grade_links
  254.                 .' SET type = '.$this->get_type()
  255.                 .', ref_id = '.$this->get_ref_id()
  256.                 .', user_id = '.$this->get_user_id()
  257.                 .", course_code = '".$this->get_course_code()."'"
  258.                 .', category_id = '.$this->get_category_id()
  259.                 .', date = ';
  260.         if (isset($this->link_date))
  261.             $sql .= $this->get_date();
  262.         else
  263.             $sql .= 'null';
  264.  
  265.         $sql .= ', weight = '.$this->get_weight()
  266.                 .', visible = '.$this->is_visible()
  267.                 .' WHERE id = '.$this->id;
  268.         api_sql_query($sql__FILE____LINE__);
  269.     }
  270.     
  271.     /**
  272.      * Delete this link from the database
  273.      */
  274.     public function delete()
  275.     {
  276.         $this->delete_linked_data();
  277.  
  278.         $tbl_grade_links Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  279.         $sql 'DELETE FROM '.$tbl_grade_links.' WHERE id = '.$this->id;
  280.         api_sql_query($sql__FILE____LINE__);
  281.     }
  282.     
  283.     
  284. // OTHER FUNCTIONS
  285.  
  286.     /**
  287.      * Generate an array of possible categories where this link can be moved to.
  288.      * Notice: its own parent will be included in the list: it's up to the frontend
  289.      * to disable this element.
  290.      * @return array 2-dimensional array - every element contains 3 subelements (id, name, level)
  291.      */
  292.     public function get_target_categories()
  293.     {
  294.         // links can only be moved to categories inside this course
  295.  
  296.         $targets array();
  297.         $level 0;
  298.  
  299.         $crscats Category::load(null,null,$this->get_course_code(),0);
  300.         foreach ($crscats as $cat)
  301.         {
  302.             $targets[array ($cat->get_id()$cat->get_name()$level+1);
  303.             $targets $this->add_target_subcategories($targets$level+1$cat->get_id());
  304.         }
  305.  
  306.         return $targets;
  307.     }
  308.  
  309.     /**
  310.      * Internal function used by get_target_categories()
  311.      */
  312.     private function add_target_subcategories($targets$level$catid)
  313.     {
  314.         $subcats Category::load(null,null,null,$catid);
  315.         foreach ($subcats as $cat)
  316.         {
  317.             $targets[array ($cat->get_id()$cat->get_name()$level+1);
  318.             $targets $this->add_target_subcategories($targets$level+1$cat->get_id());
  319.         }
  320.         return $targets;
  321.     }
  322.  
  323.     /**
  324.      * Move this link to the given category.
  325.      * If this link moves to outside a course, delete it.
  326.      */
  327.     public function move_to_cat($cat)
  328.     {
  329.         if ($this->get_course_code(!= $cat->get_course_code())
  330.         {
  331.             $this->delete();
  332.         }
  333.         else
  334.         {
  335.             $this->set_category_id($cat->get_id());
  336.             $this->save();
  337.         }
  338.     }
  339.  
  340.  
  341.     /**
  342.      * Find links by name
  343.      * To keep consistency, do not call this method but LinkFactory::find_links instead.
  344.      * @todo can be written more efficiently using a new (but very complex) sql query
  345.      */
  346.     public function find_links ($name_mask,$selectcat)
  347.     {
  348.         $rootcat Category::load($selectcat)
  349.         $links $rootcat[0]->get_links((api_is_allowed_to_create_course(null api_get_user_id())true);
  350.         $foundlinks array();
  351.         foreach ($links as $link)
  352.         {
  353.             if (!(strpos(strtolower($link->get_name())strtolower($name_mask)) === false))
  354.                 $foundlinks[$link;
  355.         }
  356.         return $foundlinks;
  357.     }
  358.  
  359.  
  360. // Other methods implementing GradebookItem
  361.  
  362.     public function get_item_type()
  363.     {
  364.         return 'L';
  365.     }
  366.  
  367.     public function get_icon_name()
  368.     {
  369.         return 'link';
  370.     }
  371.  
  372.  
  373. // ABSTRACT FUNCTIONS - to be implemented by subclass
  374.     
  375.     abstract function has_results();
  376.     abstract function get_link();
  377.     abstract function is_valid_link();
  378.     abstract function get_type_name();
  379.  
  380.     // The following methods are already defined in GradebookItem,
  381.     // and must be implemented by the subclass as well !
  382. //    abstract function get_name();
  383. //    abstract function get_description();
  384. //  abstract function calc_score($stud_id = null);
  385.  
  386.     abstract function needs_name_and_description();
  387.     abstract function needs_max();
  388.     abstract function needs_results();
  389.     abstract function is_allowed_to_change_name();
  390.  
  391.  
  392. // TRIVIAL FUNCTIONS - to be overwritten by subclass if needed
  393.  
  394.     public function get_not_created_links()
  395.     {
  396.         return null;
  397.     }
  398.     public function get_all_links()
  399.     {
  400.         return null;
  401.     }
  402.  
  403.     public function add_linked_data()
  404.     {
  405.     }
  406.     
  407.     public function save_linked_data()
  408.     {
  409.     }
  410.     
  411.     public function delete_linked_data()
  412.     {
  413.     }
  414.  
  415.  
  416.     public function set_name ($name)
  417.     {
  418.     }
  419.     
  420.     public function set_description ($description)
  421.     {
  422.     }
  423.     
  424.     public function set_max ($max)
  425.     {
  426.     }
  427.     
  428.     public function get_view_url ($stud_id)
  429.     {
  430.         return null;
  431.     }
  432.  
  433. }
  434. ?>

Documentation generated on Thu, 12 Jun 2008 12:53:17 -0500 by phpDocumentor 1.4.1