|
@@ -44,21 +44,21 @@ class ScoreDisplay
|
|
|
/**
|
|
|
* Get the instance of this class
|
|
|
*/
|
|
|
- public static function instance($category_id = 0) {
|
|
|
+ public static function instance($category_id = 0)
|
|
|
+ {
|
|
|
static $instance;
|
|
|
if (!isset ($instance)) {
|
|
|
$instance = new ScoreDisplay($category_id);
|
|
|
}
|
|
|
+
|
|
|
return $instance;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- // Static methods
|
|
|
-
|
|
|
/**
|
|
|
* Compare the custom display of 2 scores, can be useful in sorting
|
|
|
*/
|
|
|
- public static function compare_scores_by_custom_display ($score1, $score2) {
|
|
|
+ public static function compare_scores_by_custom_display ($score1, $score2)
|
|
|
+ {
|
|
|
if (!isset($score1)) {
|
|
|
return (isset($score2) ? 1 : 0);
|
|
|
} elseif (!isset($score2)) {
|
|
@@ -86,7 +86,8 @@ class ScoreDisplay
|
|
|
/**
|
|
|
* Protected constructor - call instance() to instantiate
|
|
|
*/
|
|
|
- protected function ScoreDisplay($category_id = 0) {
|
|
|
+ protected function ScoreDisplay($category_id = 0)
|
|
|
+ {
|
|
|
if (!empty($category_id)) {
|
|
|
$this->category_id = $category_id;
|
|
|
}
|
|
@@ -154,7 +155,8 @@ class ScoreDisplay
|
|
|
/**
|
|
|
* Is coloring enabled ?
|
|
|
*/
|
|
|
- public function is_coloring_enabled () {
|
|
|
+ public function is_coloring_enabled ()
|
|
|
+ {
|
|
|
return $this->coloring_enabled;
|
|
|
}
|
|
|
|
|
@@ -168,7 +170,8 @@ class ScoreDisplay
|
|
|
/**
|
|
|
* Is upperlimit included ?
|
|
|
*/
|
|
|
- public function is_upperlimit_included () {
|
|
|
+ public function is_upperlimit_included ()
|
|
|
+ {
|
|
|
return $this->upperlimit_included;
|
|
|
}
|
|
|
|
|
@@ -177,7 +180,8 @@ class ScoreDisplay
|
|
|
* See also update_custom_score_display_settings
|
|
|
* @return array current settings (or null if feature not enabled)
|
|
|
*/
|
|
|
- public function get_custom_score_display_settings() {
|
|
|
+ public function get_custom_score_display_settings()
|
|
|
+ {
|
|
|
return $this->custom_display;
|
|
|
}
|
|
|
|
|
@@ -185,7 +189,8 @@ class ScoreDisplay
|
|
|
* If coloring is enabled, scores below this value will be displayed in red.
|
|
|
* @return int color split value, in percent (or null if feature not enabled)
|
|
|
*/
|
|
|
- public function get_color_split_value() {
|
|
|
+ public function get_color_split_value()
|
|
|
+ {
|
|
|
return $this->color_split_value;
|
|
|
}
|
|
|
|
|
@@ -193,13 +198,12 @@ class ScoreDisplay
|
|
|
* Get current gradebook category id
|
|
|
* @return int Category id
|
|
|
*/
|
|
|
- private function get_current_gradebook_category_id() {
|
|
|
-
|
|
|
+ private function get_current_gradebook_category_id()
|
|
|
+ {
|
|
|
$tbl_gradebook_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
|
|
|
$curr_course_code = api_get_course_id();
|
|
|
$curr_session_id = api_get_session_id();
|
|
|
|
|
|
- $session_condition = '';
|
|
|
if (empty($curr_session_id)) {
|
|
|
$session_condition = ' AND session_id is null ';
|
|
|
} else {
|
|
@@ -218,11 +222,12 @@ class ScoreDisplay
|
|
|
|
|
|
/**
|
|
|
* Update custom score display settings
|
|
|
- * @param array $displays 2-dimensional array - every subarray must have keys (score, display)
|
|
|
- * @param int score color percent (optional)
|
|
|
- * @param int gradebook category id (optional)
|
|
|
+ * @param array $displays 2-dimensional array - every sub array must have keys (score, display)
|
|
|
+ * @param int score color percent (optional)
|
|
|
+ * @param int gradebook category id (optional)
|
|
|
*/
|
|
|
- public function update_custom_score_display_settings ($displays, $scorecolpercent = 0, $category_id = null) {
|
|
|
+ public function update_custom_score_display_settings ($displays, $scorecolpercent = 0, $category_id = null)
|
|
|
+ {
|
|
|
$this->custom_display = $displays;
|
|
|
$this->custom_display_conv = $this->convert_displays($this->custom_display);
|
|
|
|
|
@@ -250,7 +255,8 @@ class ScoreDisplay
|
|
|
Database::query($sql);
|
|
|
}
|
|
|
|
|
|
- public function insert_defaults($category_id) {
|
|
|
+ public function insert_defaults($category_id)
|
|
|
+ {
|
|
|
if (empty($category_id)) {
|
|
|
return false;
|
|
|
}
|
|
@@ -275,10 +281,10 @@ class ScoreDisplay
|
|
|
);
|
|
|
Database::insert($tbl_display, $params);
|
|
|
}
|
|
|
- Database::query($sql);
|
|
|
}
|
|
|
|
|
|
- public function get_number_decimals() {
|
|
|
+ public function get_number_decimals()
|
|
|
+ {
|
|
|
$number_decimals = api_get_setting('gradebook_number_decimals');
|
|
|
if (!isset($number_decimals)) {
|
|
|
$number_decimals = 0;
|
|
@@ -289,20 +295,28 @@ class ScoreDisplay
|
|
|
/**
|
|
|
* Formats a number depending of the number of decimals
|
|
|
*
|
|
|
- * @param float a score
|
|
|
+ * @param float $score
|
|
|
* @return float the score formatted
|
|
|
*/
|
|
|
- public function format_score($score) {
|
|
|
+ public function format_score($score)
|
|
|
+ {
|
|
|
return floatval(number_format($score, $this->get_number_decimals()));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Display a score according to the current settings
|
|
|
- * @param array $score score data structure, as returned by the calc_score functions
|
|
|
- * @param int $type one of the following constants: SCORE_DIV, SCORE_PERCENT, SCORE_DIV_PERCENT, SCORE_AVERAGE (ignored for student's view if custom score display is enabled)
|
|
|
- * @param int $what one of the following constants: SCORE_BOTH, SCORE_ONLY_DEFAULT, SCORE_ONLY_CUSTOM (default: SCORE_BOTH) (only taken into account if custom score display is enabled and for course/platform admin)
|
|
|
+ * @param array $score data structure, as returned by the calc_score functions
|
|
|
+ * @param int $type one of the following constants:
|
|
|
+ * SCORE_DIV, SCORE_PERCENT, SCORE_DIV_PERCENT, SCORE_AVERAGE
|
|
|
+ * (ignored for student's view if custom score display is enabled)
|
|
|
+ * @param int $what one of the following constants:
|
|
|
+ * SCORE_BOTH, SCORE_ONLY_DEFAULT, SCORE_ONLY_CUSTOM (default: SCORE_BOTH)
|
|
|
+ * (only taken into account if custom score display is enabled and for course/platform admin)
|
|
|
+ *
|
|
|
+ * @return string
|
|
|
*/
|
|
|
- public function display_score($score, $type = SCORE_DIV_PERCENT, $what = SCORE_BOTH, $no_color = false) {
|
|
|
+ public function display_score($score, $type = SCORE_DIV_PERCENT, $what = SCORE_BOTH, $no_color = false)
|
|
|
+ {
|
|
|
$my_score = $score == 0 ? 1 : $score;
|
|
|
|
|
|
if ($type == SCORE_BAR) {
|
|
@@ -332,8 +346,13 @@ class ScoreDisplay
|
|
|
return $display;
|
|
|
}
|
|
|
|
|
|
- // Internal functions
|
|
|
- private function display_default ($score, $type) {
|
|
|
+ /**
|
|
|
+ * @param $score
|
|
|
+ * @param $type
|
|
|
+ * @return float|string
|
|
|
+ */
|
|
|
+ private function display_default ($score, $type)
|
|
|
+ {
|
|
|
switch ($type) {
|
|
|
case SCORE_DIV : // X / Y
|
|
|
return $this->display_as_div($score);
|
|
@@ -381,7 +400,12 @@ class ScoreDisplay
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private function display_simple_score($score) {
|
|
|
+ /**
|
|
|
+ * @param array $score
|
|
|
+ * @return float|string
|
|
|
+ */
|
|
|
+ private function display_simple_score($score)
|
|
|
+ {
|
|
|
if (isset($score[0])) {
|
|
|
return $this->format_score($score[0]);
|
|
|
}
|
|
@@ -390,8 +414,11 @@ class ScoreDisplay
|
|
|
|
|
|
/**
|
|
|
* Returns "1" for array("100", "100");
|
|
|
+ * @param array $score
|
|
|
+ * @return float
|
|
|
*/
|
|
|
- private function display_as_decimal($score) {
|
|
|
+ private function display_as_decimal($score)
|
|
|
+ {
|
|
|
$score_denom = ($score[1]==0) ? 1 : $score[1];
|
|
|
return $this->format_score($score[0]/$score_denom);
|
|
|
}
|
|
@@ -399,7 +426,8 @@ class ScoreDisplay
|
|
|
/**
|
|
|
* Returns "100 %" for array("100", "100");
|
|
|
*/
|
|
|
- private function display_as_percent($score) {
|
|
|
+ private function display_as_percent($score)
|
|
|
+ {
|
|
|
$score_denom = ($score[1]==0) ? 1 : $score[1];
|
|
|
return $this->format_score($score[0]/$score_denom*100) . ' %';
|
|
|
}
|
|
@@ -409,7 +437,8 @@ class ScoreDisplay
|
|
|
* Returns 10.00 / 10.00 for array("100", "100");
|
|
|
* @param array $score
|
|
|
*/
|
|
|
- private function display_as_div($score) {
|
|
|
+ private function display_as_div($score)
|
|
|
+ {
|
|
|
if ($score == 1) {
|
|
|
return '0/0';
|
|
|
} else {
|
|
@@ -423,7 +452,8 @@ class ScoreDisplay
|
|
|
* Depends on the teacher's configuration of thresholds. i.e. [0 50] "Bad", [50:100] "Good"
|
|
|
* @param array $score
|
|
|
*/
|
|
|
- private function display_custom ($score) {
|
|
|
+ private function display_custom ($score)
|
|
|
+ {
|
|
|
$my_score_denom= ($score[1]==0) ? 1 : $score[1];
|
|
|
$scaledscore = $score[0] / $my_score_denom;
|
|
|
if ($this->upperlimit_included) {
|
|
@@ -443,13 +473,13 @@ class ScoreDisplay
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* Get score color percent by category
|
|
|
* @param int Gradebook category id
|
|
|
* @return int Score
|
|
|
*/
|
|
|
- private function get_score_color_percent($category_id = null) {
|
|
|
+ private function get_score_color_percent($category_id = null)
|
|
|
+ {
|
|
|
$tbl_display = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_SCORE_DISPLAY);
|
|
|
if (isset($category_id)) {
|
|
|
$category_id = intval($category_id);
|
|
@@ -472,7 +502,8 @@ class ScoreDisplay
|
|
|
* @param int Gradebook category id
|
|
|
* @return array 2-dimensional array - every element contains 3 subelements (id, score, display)
|
|
|
*/
|
|
|
- private function get_custom_displays($category_id = null) {
|
|
|
+ private function get_custom_displays($category_id = null)
|
|
|
+ {
|
|
|
$tbl_display = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_SCORE_DISPLAY);
|
|
|
if (isset($category_id)) {
|
|
|
$category_id = intval($category_id);
|
|
@@ -484,11 +515,11 @@ class ScoreDisplay
|
|
|
return Database::store_result($result,'ASSOC');
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* Convert display settings to internally used values
|
|
|
*/
|
|
|
- private function convert_displays($custom_display) {
|
|
|
+ private function convert_displays($custom_display)
|
|
|
+ {
|
|
|
if (isset($custom_display)) {
|
|
|
// get highest score entry, and copy each element to a new array
|
|
|
$converted = array();
|
|
@@ -522,7 +553,13 @@ class ScoreDisplay
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private function sort_display($item1, $item2) {
|
|
|
+ /**
|
|
|
+ * @param array $item1
|
|
|
+ * @param array $item2
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
+ private function sort_display($item1, $item2)
|
|
|
+ {
|
|
|
if ($item1['score'] == $item2['score']) {
|
|
|
return 0;
|
|
|
} else {
|