123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- <?php
- /* For licensing terms, see /license.txt */
- /**
- * Class UserDataGenerator
- * Class to select, sort and transform object data into array data,
- * used for a student's general view.
- *
- * @author Bert Steppé
- *
- * @package chamilo.gradebook
- */
- class UserDataGenerator
- {
- // Sorting types constants
- const UDG_SORT_TYPE = 1;
- const UDG_SORT_NAME = 2;
- const UDG_SORT_COURSE = 4;
- const UDG_SORT_CATEGORY = 8;
- const UDG_SORT_AVERAGE = 16;
- const UDG_SORT_SCORE = 32;
- const UDG_SORT_MASK = 64;
- const UDG_SORT_ASC = 128;
- const UDG_SORT_DESC = 256;
- private $items;
- private $userid;
- private $coursecodecache;
- private $categorycache;
- private $scorecache;
- private $avgcache;
- /**
- * UserDataGenerator constructor.
- *
- * @param int $userid
- * @param array $evals
- * @param array $links
- */
- public function __construct($userid, $evals = [], $links = [])
- {
- $this->userid = $userid;
- $result = [];
- foreach ($evals as $eval) {
- $toadd = true;
- $coursecode = $eval->get_course_code();
- if (isset($coursecode)) {
- $result = Result::load(null, $userid, $eval->get_id());
- if (count($result) == 0) {
- $toadd = false;
- }
- }
- if ($toadd) {
- $evals_filtered_copy = $evals;
- }
- }
- if (count($result) == 0) {
- $evals_filtered = $evals;
- } else {
- $evals_filtered = $evals_filtered_copy;
- }
- $this->items = array_merge($evals_filtered, $links);
- $this->coursecodecache = [];
- $this->categorycache = [];
- $this->scorecache = null;
- $this->avgcache = null;
- }
- /**
- * Get total number of items (rows).
- */
- public function get_total_items_count()
- {
- return count($this->items);
- }
- /**
- * Get actual array data.
- *
- * @return array 2-dimensional array - each array contains the elements:
- * 0: eval/link object
- * 1: item name
- * 2: course name
- * 3: category name
- * 4: average score
- * 5: student's score
- * 6: student's score as custom display (only if custom scoring enabled)
- */
- public function get_data(
- $sorting = 0,
- $start = 0,
- $count = null,
- $ignore_score_color = false
- ) {
- // do some checks on count, redefine if invalid value
- if (!isset($count)) {
- $count = count($this->items) - $start;
- }
- if ($count < 0) {
- $count = 0;
- }
- $allitems = $this->items;
- // sort users array
- if ($sorting & self::UDG_SORT_TYPE) {
- usort($allitems, ['UserDataGenerator', 'sort_by_type']);
- } elseif ($sorting & self::UDG_SORT_NAME) {
- usort($allitems, ['UserDataGenerator', 'sort_by_name']);
- } elseif ($sorting & self::UDG_SORT_COURSE) {
- usort($allitems, ['UserDataGenerator', 'sort_by_course']);
- } elseif ($sorting & self::UDG_SORT_CATEGORY) {
- usort($allitems, ['UserDataGenerator', 'sort_by_category']);
- } elseif ($sorting & self::UDG_SORT_AVERAGE) {
- // if user sorts on average scores, first calculate them and cache them
- foreach ($allitems as $item) {
- $this->avgcache[$item->get_item_type().$item->get_id()] = $item->calc_score();
- }
- usort($allitems, ['UserDataGenerator', 'sort_by_average']);
- } elseif ($sorting & self::UDG_SORT_SCORE) {
- // if user sorts on student's scores, first calculate them and cache them
- foreach ($allitems as $item) {
- $this->scorecache[$item->get_item_type().$item->get_id()]
- = $item->calc_score($this->userid);
- }
- usort($allitems, ['UserDataGenerator', 'sort_by_score']);
- } elseif ($sorting & self::UDG_SORT_MASK) {
- // if user sorts on student's masks, first calculate scores and cache them
- foreach ($allitems as $item) {
- $this->scorecache[$item->get_item_type().$item->get_id()]
- = $item->calc_score($this->userid);
- }
- usort($allitems, ['UserDataGenerator', 'sort_by_mask']);
- }
- if ($sorting & self::UDG_SORT_DESC) {
- $allitems = array_reverse($allitems);
- }
- // select the items we have to display
- $visibleitems = array_slice($allitems, $start, $count);
- // fill score cache if not done yet
- if (!isset($this->scorecache)) {
- foreach ($visibleitems as $item) {
- $this->scorecache[$item->get_item_type().$item->get_id()]
- = $item->calc_score($this->userid);
- }
- }
- // generate the data to display
- $scoredisplay = ScoreDisplay::instance();
- $data = [];
- foreach ($visibleitems as $item) {
- $row = [];
- $row[] = $item;
- $row[] = $item->get_name();
- $row[] = $this->build_course_name($item);
- $row[] = $this->build_category_name($item);
- $row[] = $this->build_average_column($item, $ignore_score_color);
- $row[] = $this->build_result_column($item, $ignore_score_color);
- if ($scoredisplay->is_custom()) {
- $row[] = $this->build_mask_column($item, $ignore_score_color);
- }
- $data[] = $row;
- }
- return $data;
- }
- /**
- * @param $item1
- * @param $item2
- *
- * @return int
- */
- public function sort_by_type($item1, $item2)
- {
- if ($item1->get_item_type() == $item2->get_item_type()) {
- return $this->sort_by_name($item1, $item2);
- } else {
- return $item1->get_item_type() < $item2->get_item_type() ? -1 : 1;
- }
- }
- /**
- * @param $item1
- * @param $item2
- *
- * @return int
- */
- public function sort_by_course($item1, $item2)
- {
- $name1 = api_strtolower(
- $this->get_course_name_from_code_cached($item1->get_course_code())
- );
- $name2 = api_strtolower(
- $this->get_course_name_from_code_cached($item2->get_course_code())
- );
- return api_strnatcmp($name1, $name2);
- }
- /**
- * @param $item1
- * @param $item2
- *
- * @return int
- */
- public function sort_by_category($item1, $item2)
- {
- $cat1 = $this->get_category_cached($item1->get_category_id());
- $cat2 = $this->get_category_cached($item2->get_category_id());
- $name1 = api_strtolower($this->get_category_name_to_display($cat1));
- $name2 = api_strtolower($this->get_category_name_to_display($cat2));
- return api_strnatcmp($name1, $name2);
- }
- /**
- * @param $item1
- * @param $item2
- *
- * @return int
- */
- public function sort_by_name($item1, $item2)
- {
- return api_strnatcmp($item1->get_name(), $item2->get_name());
- }
- /**
- * @param $item1
- * @param $item2
- *
- * @return int
- */
- public function sort_by_average($item1, $item2)
- {
- $score1 = $this->avgcache[$item1->get_item_type().$item1->get_id()];
- $score2 = $this->avgcache[$item2->get_item_type().$item2->get_id()];
- return $this->compare_scores($score1, $score2);
- }
- /**
- * @param $item1
- * @param $item2
- *
- * @return int
- */
- public function sort_by_score($item1, $item2)
- {
- $score1 = $this->scorecache[$item1->get_item_type().$item1->get_id()];
- $score2 = $this->scorecache[$item2->get_item_type().$item2->get_id()];
- return $this->compare_scores($score1, $score2);
- }
- /**
- * @param $item1
- * @param $item2
- *
- * @return int
- */
- public function sort_by_mask($item1, $item2)
- {
- $score1 = $this->scorecache[$item1->get_item_type().$item1->get_id()];
- $score2 = $this->scorecache[$item2->get_item_type().$item2->get_id()];
- return ScoreDisplay::compare_scores_by_custom_display($score1, $score2);
- }
- /**
- * @param $score1
- * @param $score2
- *
- * @return int
- */
- public function compare_scores($score1, $score2)
- {
- if (!isset($score1)) {
- return isset($score2) ? 1 : 0;
- } elseif (!isset($score2)) {
- return -1;
- } elseif (($score1[0] / $score1[1]) == ($score2[0] / $score2[1])) {
- return 0;
- } else {
- return ($score1[0] / $score1[1]) < ($score2[0] / $score2[1]) ? -1 : 1;
- }
- }
- /**
- * @param $item
- *
- * @return mixed
- */
- private function build_course_name($item)
- {
- return $this->get_course_name_from_code_cached($item->get_course_code());
- }
- /**
- * @param $item
- *
- * @return string
- */
- private function build_category_name($item)
- {
- $cat = $this->get_category_cached($item->get_category_id());
- return $this->get_category_name_to_display($cat);
- }
- /**
- * @param $item
- * @param $ignore_score_color
- *
- * @return string
- */
- private function build_average_column($item, $ignore_score_color)
- {
- if (isset($this->avgcache)) {
- $avgscore = $this->avgcache[$item->get_item_type().$item->get_id()];
- } else {
- $avgscore = $item->calc_score();
- }
- $scoredisplay = ScoreDisplay::instance();
- $displaytype = SCORE_AVERAGE;
- /*if ($ignore_score_color)
- $displaytype |= SCORE_IGNORE_SPLIT;
- */
- return $scoredisplay->display_score($avgscore, $displaytype);
- }
- /**
- * @param $item
- * @param $ignore_score_color
- *
- * @return string
- */
- private function build_result_column($item, $ignore_score_color)
- {
- $studscore = $this->scorecache[$item->get_item_type().$item->get_id()];
- $scoredisplay = ScoreDisplay::instance();
- $displaytype = SCORE_DIV_PERCENT;
- if ($ignore_score_color) {
- $displaytype |= SCORE_IGNORE_SPLIT;
- }
- return $scoredisplay->display_score(
- $studscore,
- $displaytype,
- SCORE_ONLY_DEFAULT
- );
- }
- /**
- * @param $item
- * @param $ignore_score_color
- *
- * @return string
- */
- private function build_mask_column($item, $ignore_score_color)
- {
- $studscore = $this->scorecache[$item->get_item_type().$item->get_id()];
- $scoredisplay = ScoreDisplay::instance();
- $displaytype = SCORE_DIV_PERCENT;
- if ($ignore_score_color) {
- $displaytype |= SCORE_IGNORE_SPLIT;
- }
- return $scoredisplay->display_score(
- $studscore,
- $displaytype,
- SCORE_ONLY_CUSTOM
- );
- }
- /**
- * @param $coursecode
- *
- * @return mixed
- */
- private function get_course_name_from_code_cached($coursecode)
- {
- if (isset($this->coursecodecache) &&
- isset($this->coursecodecache[$coursecode])
- ) {
- return $this->coursecodecache[$coursecode];
- } else {
- $name = CourseManager::getCourseNameFromCode($coursecode);
- $this->coursecodecache[$coursecode] = $name;
- return $name;
- }
- }
- /**
- * @param $category_id
- */
- private function get_category_cached($category_id)
- {
- if (isset($this->categorycache) &&
- isset($this->categorycache[$category_id])
- ) {
- return $this->categorycache[$category_id];
- } else {
- $cat = Category::load($category_id);
- if (isset($cat)) {
- $this->categorycache[$category_id] = $cat[0];
- return $cat[0];
- } else {
- return null;
- }
- }
- }
- /**
- * @param $cat
- *
- * @return string
- */
- private function get_category_name_to_display($cat)
- {
- if (isset($cat)) {
- if ($cat->get_parent_id() == '0' || $cat->get_parent_id() == null) {
- return '';
- } else {
- return $cat->get_name();
- }
- } else {
- return '';
- }
- }
- }
|