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

Source for file evaluation.class.php

Documentation is available at evaluation.class.php

  1. <?php
  2.  
  3.  
  4. /**
  5.  * Defines a gradebook Evaluation object
  6.  * @author Bert Stepp�, Stijn Konings
  7.  * @package dokeos.gradebook
  8.  */
  9. class Evaluation implements GradebookItem
  10. {
  11.  
  12. // PROPERTIES
  13.  
  14.     private $id;
  15.     private $name;
  16.     private $description;
  17.     private $user_id;
  18.     private $course_code;
  19.     private $category;
  20.     private $eval_date;
  21.     private $weight;
  22.     private $eval_max;
  23.     private $visible;
  24.  
  25. // CONSTRUCTORS
  26.  
  27.     function Evaluation()
  28.     {
  29.     }
  30.  
  31. // GETTERS AND SETTERS
  32.  
  33.        public function get_id()
  34.     {
  35.         return $this->id;
  36.     }
  37.  
  38.     public function get_name()
  39.     {
  40.         return $this->name;
  41.     }
  42.  
  43.     public function get_description()
  44.     {
  45.         return $this->description;
  46.     }
  47.  
  48.     public function get_user_id()
  49.     {
  50.         return $this->user_id;
  51.     }
  52.  
  53.     public function get_course_code()
  54.     {
  55.         return $this->course_code;
  56.     }
  57.  
  58.     public function get_category_id()
  59.     {
  60.         return $this->category;
  61.     }
  62.  
  63.     public function get_date()
  64.     {
  65.         return $this->eval_date;
  66.     }
  67.  
  68.     public function get_weight()
  69.     {
  70.         return $this->weight;
  71.     }
  72.  
  73.     public function get_max()
  74.     {
  75.         return $this->eval_max;
  76.     }
  77.  
  78.     public function is_visible()
  79.     {
  80.         return $this->visible;
  81.     }
  82.  
  83.     public function set_id ($id)
  84.     {
  85.         $this->id = $id;
  86.     }
  87.     
  88.     public function set_name ($name)
  89.     {
  90.         $this->name = $name;
  91.     }
  92.     
  93.     public function set_description ($description)
  94.     {
  95.         $this->description = $description;
  96.     }
  97.     
  98.     public function set_user_id ($user_id)
  99.     {
  100.         $this->user_id = $user_id;
  101.     }
  102.     
  103.     public function set_course_code ($course_code)
  104.     {
  105.         $this->course_code = $course_code;
  106.     }
  107.     
  108.     public function set_category_id ($category_id)
  109.     {
  110.         $this->category = $category_id;
  111.     }
  112.     
  113.     public function set_date ($date)
  114.     {
  115.         $this->eval_date = $date;
  116.     }
  117.     
  118.     public function set_weight ($weight)
  119.     {
  120.         $this->weight = $weight;
  121.     }
  122.     
  123.     public function set_max ($max)
  124.     {
  125.         $this->eval_max = $max;
  126.     }
  127.     
  128.     public function set_visible ($visible)
  129.     {
  130.         $this->visible = $visible;
  131.     }
  132.     
  133.     
  134. // CRUD FUNCTIONS
  135.  
  136.     /**
  137.      * Retrieve evaluations and return them as an array of Evaluation objects
  138.      * @param $id evaluation id
  139.      * @param $user_id user id (evaluation owner)
  140.      * @param $course_code course code
  141.      * @param $category_id parent category
  142.      * @param $visible visible
  143.      */
  144.     public function load ($id null$user_id null$course_code null$category_id null$visible null)
  145.     {
  146.         $tbl_grade_evaluations Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  147.         $sql='SELECT id,name,description,user_id,course_code,category_id,date,weight,max,visible FROM '.$tbl_grade_evaluations;
  148.         $paramcount 0;
  149.         if (isset ($id))
  150.         {
  151.             $sql.= ' WHERE id = '.$id;
  152.             $paramcount ++;
  153.         }
  154.         if (isset ($user_id))
  155.         {
  156.             if ($paramcount != 0$sql .= ' AND';
  157.             else $sql .= ' WHERE';
  158.             $sql .= ' user_id = '.$user_id;
  159.             $paramcount ++;
  160.         }
  161.         if (isset ($course_code))
  162.         {
  163.             if ($paramcount != 0$sql .= ' AND';
  164.             else $sql .= ' WHERE';
  165.             $sql .= " course_code = '".$course_code."'";
  166.             $paramcount ++;
  167.         }
  168.         if (isset ($category_id))
  169.         {
  170.             if ($paramcount != 0$sql .= ' AND';
  171.             else $sql .= ' WHERE';
  172.             $sql .= ' category_id = '.$category_id;
  173.             $paramcount ++;
  174.         }
  175.         if (isset ($visible))
  176.         {
  177.             if ($paramcount != 0$sql .= ' AND';
  178.             else $sql .= ' WHERE';
  179.             $sql .= ' visible = '.$visible;
  180.             $paramcount ++;
  181.         }
  182.  
  183.         $result api_sql_query($sql__FILE____LINE__);
  184.         $alleval Evaluation::create_evaluation_objects_from_mysql_result($result);
  185.         return $alleval;
  186.     }
  187.     
  188.     private function create_evaluation_objects_from_mysql_result($result)
  189.     {
  190.         $alleval=array();
  191.         while ($data=mysql_fetch_array($result))
  192.         {    
  193.             $evalnew Evaluation();
  194.             $eval->set_id($data['id']);
  195.             $eval->set_name($data['name']);
  196.             $eval->set_description($data['description']);
  197.             $eval->set_user_id($data['user_id']);
  198.             $eval->set_course_code($data['course_code']);
  199.             $eval->set_category_id($data['category_id']);
  200.             $eval->set_date($data['date']);
  201.             $eval->set_weight($data['weight']);
  202.             $eval->set_max($data['max']);
  203.             $eval->set_visible($data['visible']);
  204.             $alleval[]=$eval;
  205.         }
  206.         return $alleval;
  207.     }
  208.     
  209.     /**
  210.      * Insert this evaluation into the database
  211.      */
  212.     public function add()
  213.     {
  214.         if (isset($this->name&& isset($this->user_id&& isset($this->weight&& isset ($this->eval_max&& isset($this->visible))
  215.         {
  216.             $tbl_grade_evaluations Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  217.             
  218.             $sql 'INSERT INTO '.$tbl_grade_evaluations
  219.                     .' (name,user_id,weight,max,visible';
  220.             if (isset($this->description)) $sql .= ',description';
  221.             if (isset($this->course_code)) $sql .= ',course_code';
  222.             if (isset($this->category)) $sql .= ',category_id';
  223.             if (isset($this->eval_date)) $sql .= ',date';
  224.             $sql .= ") VALUES ('".mysql_real_escape_string($this->get_name())."'"
  225.                     .','.$this->get_user_id()
  226.                     .','.$this->get_weight()
  227.                     .','.$this->get_max()
  228.                     .','.$this->is_visible();
  229.             if (isset($this->description)) $sql .= ",'".mysql_real_escape_string($this->get_description())."'";
  230.             if (isset($this->course_code)) $sql .= ",'".$this->get_course_code()."'";
  231.             if (isset($this->category)) $sql .= ','.$this->get_category_id();
  232.             if (isset($this->eval_date)) $sql .= ','.$this->get_date();
  233.             $sql .= ")";
  234.  
  235.             api_sql_query($sql__FILE____LINE__);
  236.             $this->set_id(mysql_insert_id());
  237.         }
  238.         else
  239.             die('Error in Evaluation add: required field empty');
  240.     }
  241.     
  242.     /**
  243.      * Update the properties of this evaluation in the database
  244.      */
  245.     public function save()
  246.     {
  247.         $tbl_grade_evaluations Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  248.         $sql 'UPDATE '.$tbl_grade_evaluations
  249.             ." SET name = '".mysql_real_escape_string($this->get_name())."'"
  250.             .', description = ';
  251.         if (isset($this->description))
  252.             $sql .= "'".mysql_real_escape_string($this->get_description())."'";
  253.         else
  254.             $sql .= 'null';
  255.  
  256.         $sql .= ', user_id = '.$this->get_user_id()
  257.                 .', course_code = ';
  258.         if (isset($this->course_code))
  259.             $sql .= "'".$this->get_course_code()."'";
  260.         else
  261.             $sql .= 'null';
  262.         
  263.         $sql .= ', category_id = ';
  264.         if (isset($this->category))
  265.             $sql .= $this->get_category_id();
  266.         else
  267.             $sql .= 'null';
  268.  
  269.         $sql .= ', date = ';
  270.         if (isset($this->eval_date))
  271.             $sql .= $this->get_date();
  272.         else
  273.             $sql .= 'null';
  274.  
  275.         $sql .= ', weight = '.$this->get_weight()
  276.                 .', max = '.$this->get_max()
  277.                 .', visible = '.$this->is_visible()
  278.                 .' WHERE id = '.$this->id;
  279.         api_sql_query($sql__FILE____LINE__);
  280.     }
  281.     
  282.     /**
  283.      * Delete this evaluation from the database
  284.      */
  285.     public function delete()
  286.     {
  287.         $tbl_grade_evaluations Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  288.         $sql 'DELETE FROM '.$tbl_grade_evaluations.' WHERE id = '.$this->id;
  289.         api_sql_query($sql__FILE____LINE__);
  290.     }
  291.  
  292. // OTHER FUNCTIONS
  293.  
  294.     /**
  295.      * Check if an evaluation name (with the same parent category) already exists
  296.      * @param $name name to check (if not given, the name property of this object will be checked)
  297.      * @param $parent parent category
  298.      */
  299.     public function does_name_exist($name$parent)
  300.     {
  301.         if (!isset ($name))
  302.         {
  303.             $name $this->name;
  304.             $parent $this->category;
  305.         }
  306.         $tbl_grade_evaluations Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  307.         $sql 'SELECT count(id) AS number'
  308.              .' FROM '.$tbl_grade_evaluations
  309.              ." WHERE name = '".$name."'";
  310.              
  311.         if (api_is_allowed_to_create_course()) // && !api_is_platform_admin())
  312.         {
  313.             $parent Category::load($parent);
  314.             $code $parent[0]->get_course_code();
  315.             if (isset($code&& $code != '0')
  316.             {
  317.                 $main_course_user_table Database :: get_main_table(TABLE_MAIN_COURSE_USER);            
  318.                 $sql .= ' AND user_id IN ('
  319.                         .' SELECT user_id FROM '.$main_course_user_table
  320.                         ." WHERE course_code = '".$code."'"
  321.                         .' AND status = '.COURSEMANAGER
  322.                         .')';
  323.             }
  324.             else
  325.                 $sql .= ' AND user_id = '.api_get_user_id();
  326.         }
  327.         else
  328.             $sql .= ' AND user_id = '.api_get_user_id();
  329.              
  330.         if (!isset ($parent))
  331.             $sql.= ' AND category_id is null';
  332.         else
  333.             $sql.= ' AND category_id = '.$parent;
  334.  
  335.         $result api_sql_query($sql__FILE____LINE__);
  336.         $number=mysql_fetch_row($result);
  337.         return ($number[0!= 0);
  338.     }
  339.  
  340.     /**
  341.      * Are there any results for this evaluation yet ?
  342.      * The 'max' property should not be changed then.
  343.      */
  344.     public function has_results()
  345.     {
  346.         $tbl_grade_results Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  347.         $sql='SELECT count(id) AS number FROM '.$tbl_grade_results
  348.             .' WHERE evaluation_id = '.$this->id;
  349.         $result api_sql_query($sql__FILE____LINE__);
  350.         $number=mysql_fetch_row($result);
  351.  
  352.         return ($number[0!= 0);
  353.     }
  354.     
  355.     
  356.     /**
  357.      * Does this evaluation have any results for a student ?
  358.      */
  359.      /* - not used anywhere (yet ?)
  360.     public function has_results_for_student($stud_id)
  361.     {
  362.         $tbl_grade_results = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  363.         $sql="SELECT count(id) AS number FROM ".$tbl_grade_results
  364.             ." WHERE evaluation_id = ".$this->id." AND user_id = ".$stud_id;
  365.         $result = api_sql_query($sql, __FILE__, __LINE__);
  366.         $number=mysql_fetch_row($result);
  367.         return ($number[0] != 0);
  368.     }
  369.     */
  370.  
  371.     
  372.     /**
  373.      * Delete all results for this evaluation
  374.      */
  375.     public function delete_results()
  376.     {
  377.         $tbl_grade_results Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  378.         $sql 'DELETE FROM '.$tbl_grade_results.' WHERE evaluation_id = '.$this->id;
  379.         api_sql_query($sql__FILE____LINE__);
  380.     }
  381.     
  382.     
  383.     /**
  384.      * Delete this evaluation and all underlying results.
  385.      */
  386.     public function delete_with_results()
  387.     {
  388.         $this->delete_results();
  389.         $this->delete();
  390.     }
  391.     
  392.     
  393.     /**
  394.      * Check if the given score is possible for this evaluation
  395.      */
  396.     public function is_valid_score ($score)
  397.     {
  398.         return (is_numeric($score&& $score >= && $score <= $this->eval_max);
  399.     }
  400.     
  401.     
  402.     /**
  403.      * Calculate the score of this evaluation
  404.      * @param $stud_id student id (default: all students who have results for this eval - then the average is returned)
  405.      * @return    array (score, max) if student is given
  406.      *              array (sum of scores, number of scores) otherwise
  407.      *              or null if no scores available
  408.      */
  409.     public function calc_score ($stud_id null)
  410.     {
  411.         $results Result::load(null,$stud_id,$this->id);
  412.         
  413.         $rescount 0;
  414.         $sum 0;
  415.         foreach ($results as $res)
  416.         {
  417.             $score $res->get_score();
  418.             if ((!empty ($score)) || ($score == '0'))
  419.             {
  420.                 $rescount++;
  421.                 $sum += ($score $this->get_max());
  422.             }
  423.         }
  424.         
  425.         if ($rescount == 0)
  426.             return null;
  427.         else if (isset($stud_id))
  428.             return array ($score$this->get_max());
  429.         else
  430.             return array ($sum$rescount);
  431.     }
  432.     
  433.     
  434.     
  435.     /**
  436.      * Generate an array of possible categories where this evaluation can be moved to.
  437.      * Notice: its own parent will be included in the list: it's up to the frontend
  438.      * to disable this element.
  439.      * @return array 2-dimensional array - every element contains 3 subelements (id, name, level)
  440.      */
  441.     public function get_target_categories()
  442.     {
  443.         // - course independent evaluation
  444.         //   -> movable to root or other course independent categories
  445.         // - evaluation inside a course
  446.         //   -> movable to root, independent categories or categories inside the course
  447.  
  448.         $user (api_is_platform_admin(null api_get_user_id());
  449.         $targets array();
  450.         $level 0;
  451.  
  452.         $root array(0get_lang('RootCat')$level);
  453.         $targets[$root;
  454.  
  455.         if (isset($this->course_code&& !empty($this->course_code))
  456.         {
  457.             $crscats Category::load(null,null,$this->course_code,0);
  458.             foreach ($crscats as $cat)
  459.             {
  460.                 $targets[array ($cat->get_id()$cat->get_name()$level+1);
  461.                 $targets $this->add_target_subcategories($targets$level+1$cat->get_id());
  462.             }
  463.         }
  464.  
  465.         $indcats Category::load(null,$user,0,0);
  466.         foreach ($indcats as $cat)
  467.         {
  468.             $targets[array ($cat->get_id()$cat->get_name()$level+1);
  469.             $targets $this->add_target_subcategories($targets$level+1$cat->get_id());
  470.         }
  471.             
  472.         return $targets;
  473.     }
  474.     
  475.     /**
  476.      * Internal function used by get_target_categories()
  477.      */
  478.     private function add_target_subcategories($targets$level$catid)
  479.     {
  480.         $subcats Category::load(null,null,null,$catid);
  481.         foreach ($subcats as $cat)
  482.         {
  483.             $targets[array ($cat->get_id()$cat->get_name()$level+1);
  484.             $targets $this->add_target_subcategories($targets$level+1$cat->get_id());
  485.         }
  486.         return $targets;
  487.     }
  488.     
  489.     
  490.     /**
  491.      * Move this evaluation to the given category.
  492.      * If this evaluation moves from inside a course to outside,
  493.      * its course code is also changed.
  494.      */
  495.     public function move_to_cat ($cat)
  496.     {
  497.         $this->set_category_id($cat->get_id());
  498.         if ($this->get_course_code(!= $cat->get_course_code())
  499.         {
  500.             $this->set_course_code($cat->get_course_code());
  501.         }
  502.         $this->save();
  503.     }
  504.     
  505.     
  506.     
  507.     /**
  508.      * Retrieve evaluations where a student has results for
  509.      * and return them as an array of Evaluation objects
  510.      * @param $cat_id parent category (use 'null' to retrieve them in all categories)
  511.      * @param $stud_id student id
  512.      */
  513.     public function get_evaluations_with_result_for_student ($cat_id null$stud_id)
  514.     {
  515.         $tbl_grade_evaluations Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  516.         $tbl_grade_results Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  517.  
  518.         $sql 'SELECT * FROM '.$tbl_grade_evaluations
  519.                 .' WHERE id IN'
  520.                 .'(SELECT evaluation_id FROM '.$tbl_grade_results
  521.                 .' WHERE user_id = '.$stud_id.' AND score IS NOT NULL)';
  522.         if (!api_is_allowed_to_create_course())
  523.             $sql .= ' AND visible = 1';
  524.         if (isset($cat_id))
  525.             $sql .= ' AND category_id = '.$cat_id;
  526.         else
  527.             $sql .= ' AND category_id >= 0';
  528.  
  529.  
  530.         $result api_sql_query($sql__FILE____LINE__);
  531.         $alleval Evaluation::create_evaluation_objects_from_mysql_result($result);
  532.         
  533.         return $alleval;
  534.     }
  535.     
  536.     
  537.     
  538.     /**
  539.      * Get a list of students that do not have a result record for this evaluation
  540.      */
  541.     public function get_not_subscribed_students ($first_letter_user '')
  542.     {
  543.         $tbl_user Database :: get_main_table(TABLE_MAIN_USER);
  544.         $tbl_grade_results Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  545.  
  546.         $sql 'SELECT user_id,lastname,firstname,username FROM '.$tbl_user
  547.                 ." WHERE lastname LIKE '".$first_letter_user."%'"
  548.                 .' AND status = '.STUDENT
  549.                 .' AND user_id NOT IN'
  550.                 .' (SELECT user_id FROM '.$tbl_grade_results
  551.                 .' WHERE evaluation_id = '.$this->id
  552.                 .' )'
  553.                 .' ORDER BY lastname';
  554.  
  555.         $result api_sql_query($sql__FILE____LINE__);
  556.         $db_users api_store_result($result);
  557.         return $db_users;
  558.     }
  559.     
  560.     
  561.     /**
  562.      * Find evaluations by name
  563.      * @param string $name_mask search string
  564.      * @return array evaluation objects matching the search criterium
  565.      * @todo can be written more efficiently using a new (but very complex) sql query
  566.      */
  567.     public function find_evaluations ($name_mask,$selectcat)
  568.     {
  569.         $rootcat Category::load($selectcat)
  570.         $evals $rootcat[0]->get_evaluations((api_is_allowed_to_create_course(null api_get_user_id())true);
  571.         $foundevals array();
  572.         foreach ($evals as $eval)
  573.         {
  574.             if (!(strpos(strtolower($eval->get_name())strtolower($name_mask)) === false))
  575.                 $foundevals[$eval;
  576.         }
  577.         return $foundevals;
  578.     }
  579.     
  580.     
  581.     
  582. // Other methods implementing GradebookItem
  583.  
  584.     public function get_item_type()
  585.     {
  586.         return 'E';
  587.     }
  588.     
  589.     public function get_icon_name()
  590.     {
  591.         return $this->has_results('evalnotempty' 'evalempty';
  592.     }
  593.     
  594. }
  595. ?>

Documentation generated on Thu, 12 Jun 2008 13:23:38 -0500 by phpDocumentor 1.4.1