teacherView = is_null($showTeacherView) ? api_is_allowed_to_edit(null, true) : $showTeacherView; $this->userId = is_null($userId) ? api_get_user_id() : $userId; $this->exportToPdf = $exportToPdf; $this->studentList = $studentList; parent::__construct( 'gradebooklist', null, null, api_is_allowed_to_edit() ? 1 : 0, 20, 'ASC', 'gradebook_list' ); $this->evals_links = array_merge($evals, $links); $this->currentcat = $currentcat; $this->cats = $cats; $this->loadStats = $loadStats; $this->datagen = new GradebookDataGenerator($cats, $evals, $links); $this->datagen->preLoadDataKey = $this->getPreloadDataKey(); $this->datagen->hidePercentage = api_get_configuration_value('hide_gradebook_percentage_user_result'); if (!empty($userId)) { $this->datagen->userId = $userId; } if (isset($addparams)) { $this->set_additional_parameters($addparams); } $column = 0; if ($this->teacherView) { if ($this->exportToPdf == false) { $this->set_header($column++, '', '', 'width="25px"'); } } $this->set_header($column++, get_lang('Type'), '', 'width="35px"'); $this->set_header($column++, get_lang('Name'), false); if ($this->exportToPdf == false) { $this->set_header($column++, get_lang('Description'), false); } $model = ExerciseLib::getCourseScoreModel(); $this->set_header( $column++, get_lang('Weight'), '', 'width="100px"' ); if (!$this->teacherView) { $this->set_header($column++, get_lang('Result'), false); } if (empty($model)) { if (in_array(1, $this->loadStats)) { $this->set_header($column++, get_lang('Ranking'), false); } if (in_array(2, $this->loadStats)) { $this->set_header($column++, get_lang('BestScore'), false); } if (in_array(3, $this->loadStats)) { $this->set_header($column++, get_lang('Average'), false); } } if ($this->teacherView) { } else { if (!empty($cats)) { if ($this->exportToPdf == false) { $this->set_header($column++, get_lang('Actions'), false); } } } // Deactivates the odd/even alt rows in order that the +/- buttons work see #4047 $this->odd_even_rows_enabled = false; // Admins get an edit column. if ($this->teacherView) { $this->set_header($column++, get_lang('Modify'), false, 'width="195px"'); // Actions on multiple selected documents. $this->set_form_actions( [ 'setvisible' => get_lang('SetVisible'), 'setinvisible' => get_lang('SetInvisible'), 'deleted' => get_lang('DeleteSelected'), ] ); } else { if (empty($_GET['selectcat']) && !$this->teacherView) { if ($this->exportToPdf == false) { $this->set_header( $column++, get_lang('Certificates'), false ); } } } } /** * @return GradebookDataGenerator */ public function get_data() { return $this->datagen; } /** * Function used by SortableTable to get total number of items in the table. * * @return int */ public function get_total_number_of_items() { return $this->datagen->get_total_items_count(); } /** * @return string */ public function getPreloadDataKey() { return 'default_data_'.api_get_course_id().'_'.api_get_session_id(); } public function preloadData() { $allitems = $this->datagen->items; usort($allitems, ['GradebookDataGenerator', 'sort_by_name']); $visibleItems = array_merge($this->datagen->items, $this->evals_links); $defaultDataFromSession = Session::read($this->getPreloadDataKey()); if (empty($defaultDataFromSession)) { $defaultData = []; /** @var GradebookItem $item */ foreach ($visibleItems as $item) { $item->setStudentList($this->studentList); $itemType = get_class($item); switch ($itemType) { case 'Evaluation': // Best $best = $this->datagen->buildBestResultColumn($item); $defaultData[$item->get_id()]['best'] = $best; // Average $average = $this->datagen->buildAverageResultColumn($item); $defaultData[$item->get_id()]['average'] = $average; break; case 'ExerciseLink': /** @var ExerciseLink $item */ // Best $best = $this->datagen->buildBestResultColumn($item); $defaultData[$item->get_id()]['best'] = $best; // Average $average = $this->datagen->buildAverageResultColumn($item); $defaultData[$item->get_id()]['average'] = $average; // Ranking /*if (!empty($this->studentList)) { $invalidateRanking = true; foreach ($this->studentList as $user) { $score = $this->datagen->build_result_column( $user['user_id'], $item, false, true ); if (!empty($score['score'])) { $invalidateRanking = false; } $rankingStudentList[$user['user_id']] = $score['score'][0]; $defaultData[$item->get_id()]['ranking'] = $rankingStudentList; $defaultData[$item->get_id()]['ranking_invalidate'] = $invalidateRanking; } }*/ break; default: // Best $best = $this->datagen->buildBestResultColumn($item); $defaultData[$item->get_id()]['best'] = $best; // Average $average = $this->datagen->buildAverageResultColumn($item); $defaultData[$item->get_id()]['average'] = $average; // Ranking if (!empty($this->studentList)) { $invalidateRanking = true; foreach ($this->studentList as $user) { $score = $this->datagen->build_result_column( $user['user_id'], $item, false, true ); if (!empty($score['score'])) { $invalidateRanking = false; } $rankingStudentList[$user['user_id']] = $score['score'][0]; $defaultData[$item->get_id()]['ranking'] = $rankingStudentList; $defaultData[$item->get_id()]['ranking_invalidate'] = $invalidateRanking; } //exit; } break; } } Session::write($this->getPreloadDataKey(), $defaultData); } else { $defaultData = $defaultDataFromSession; } return $defaultData; } /** * Function used by SortableTable to generate the data to display. * * @param int $from * @param int $per_page * @param int $column * @param string $direction * @param int $sort * * @return array|mixed */ public function get_table_data($from = 1, $per_page = null, $column = null, $direction = null, $sort = null) { //variables load in index.php global $certificate_min_score; $isAllowedToEdit = api_is_allowed_to_edit(); // determine sorting type $col_adjust = $isAllowedToEdit ? 1 : 0; // By id $this->column = 5; switch ($this->column) { // Type case 0 + $col_adjust: $sorting = GradebookDataGenerator::GDG_SORT_TYPE; break; case 1 + $col_adjust: $sorting = GradebookDataGenerator::GDG_SORT_NAME; break; case 2 + $col_adjust: $sorting = GradebookDataGenerator::GDG_SORT_DESCRIPTION; break; case 3 + $col_adjust: $sorting = GradebookDataGenerator::GDG_SORT_WEIGHT; break; case 4 + $col_adjust: $sorting = GradebookDataGenerator::GDG_SORT_DATE; break; case 5 + $col_adjust: $sorting = GradebookDataGenerator::GDG_SORT_ID; break; } if ($this->direction == 'DESC') { $sorting |= GradebookDataGenerator::GDG_SORT_DESC; } else { $sorting |= GradebookDataGenerator::GDG_SORT_ASC; } // Status of user in course. $user_id = $this->userId; $course_code = api_get_course_id(); $session_id = api_get_session_id(); $statusToFilter = 0; if (empty($session_id)) { $statusToFilter = STUDENT; } if (empty($this->studentList) && $this->loadStats) { $studentList = CourseManager::get_user_list_from_course_code( $course_code, $session_id, null, null, $statusToFilter ); $this->studentList = $studentList; } $this->datagen->userId = $this->userId; $data_array = $this->datagen->get_data( $sorting, $from, $this->per_page, false, $this->studentList, $this->loadStats ); // generate the data to display $sortable_data = []; $weight_total_links = 0; $main_cat = Category::load( null, null, $course_code, null, null, $session_id, 'ORDER BY id' ); $total_categories_weight = 0; $scoredisplay = ScoreDisplay::instance(); $totalUserResult = [0, 0]; $totalBest = [0, 0]; $totalAverage = [0, 0]; $type = 'detail'; if ($this->exportToPdf) { $type = 'simple'; } $model = ExerciseLib::getCourseScoreModel(); $userExerciseScoreInCategory = api_get_configuration_value( 'gradebook_use_exercise_score_settings_in_categories' ); $course_code = api_get_course_id(); $session_id = api_get_session_id(); $defaultData = Session::read($this->getPreloadDataKey()); // Categories. if (!empty($data_array)) { foreach ($data_array as $data) { // list of items inside the gradebook (exercises, lps, forums, etc) $row = []; /** @var AbstractLink $item */ $item = $data[0]; // If the item is invisible, wrap it in a span with class invisible $invisibility_span_open = $isAllowedToEdit && $item->is_visible() == '0' ? '' : ''; $invisibility_span_close = $isAllowedToEdit && $item->is_visible() == '0' ? '' : ''; // Id if ($this->teacherView) { if ($this->exportToPdf == false) { $row[] = $this->build_id_column($item); } } // Type. $row[] = $this->build_type_column($item); // Name. if (get_class($item) === 'Category') { $row[] = $invisibility_span_open.''.$item->get_name().''.$invisibility_span_close; $main_categories[$item->get_id()]['name'] = $item->get_name(); } else { $name = $this->build_name_link($item, $type); $row[] = $invisibility_span_open.$name.$invisibility_span_close; $main_categories[$item->get_id()]['name'] = $name; } $this->dataForGraph['categories'][] = $item->get_name(); $main_categories[$item->get_id()]['weight'] = $item->get_weight(); $total_categories_weight += $item->get_weight(); // Description. if ($this->exportToPdf == false) { $row[] = $invisibility_span_open.$data[2].$invisibility_span_close; } // Weight. $weight = $scoredisplay->display_score( [ $data['3'], $this->currentcat->get_weight(), ], SCORE_SIMPLE, SCORE_BOTH, true ); if ($this->teacherView) { $row[] = $invisibility_span_open. Display::tag('p', $weight, ['class' => 'score']). $invisibility_span_close; } else { $row[] = $invisibility_span_open.$weight.$invisibility_span_close; } $category_weight = $item->get_weight(); if ($this->teacherView) { $weight_total_links += $data[3]; } // Edit (for admins). if ($this->teacherView) { $cat = new Category(); $show_message = $cat->show_message_resource_delete($item->get_course_code()); if ($show_message === false) { $row[] = $this->build_edit_column($item); } } else { $score = $item->calc_score($this->userId); if (!empty($score[1])) { $completeScore = $scoredisplay->display_score($score, SCORE_DIV_PERCENT); $score = $score[0] / $score[1] * $item->get_weight(); $score = $scoredisplay->display_score([$score, null], SCORE_SIMPLE); } else { $categoryScore = null; } // Students get the results and certificates columns $value_data = isset($data[4]) ? $data[4] : null; $best = isset($data['best']) ? $data['best'] : null; $average = isset($data['average']) ? $data['average'] : null; $ranking = isset($data['ranking']) ? $data['ranking'] : null; $totalResult = [ $data['result_score'][0], $data['result_score'][1], ]; if (empty($model)) { $totalBest = [ $scoredisplay->format_score($totalBest[0] + $data['best_score'][0]), $scoredisplay->format_score($totalBest[1] + $data['best_score'][1]), ]; $totalAverage = [ $data['average_score'][0], $data['average_score'][1], ]; } // Student result if (empty($model)) { $row[] = $value_data; } else { $row[] = ExerciseLib::show_score( $data['result_score'][0], $data['result_score'][1] ); } $mode = SCORE_AVERAGE; if ($userExerciseScoreInCategory) { $mode = SCORE_SIMPLE; $result = ExerciseLib::convertScoreToPlatformSetting($totalAverage[0], $totalAverage[1]); $totalAverage[0] = $result['score']; $totalAverage[1] = $result['weight']; $result = ExerciseLib::convertScoreToPlatformSetting($totalResult[0], $totalResult[1]); $totalResult[0] = $result['score']; $totalResult[1] = $result['weight']; $result = ExerciseLib::convertScoreToPlatformSetting( $data['result_score'][0], $data['result_score'][1] ); $data['my_result_no_float'][0] = $result['score']; } $totalResultAverageValue = strip_tags($scoredisplay->display_score($totalResult, $mode)); $totalAverageValue = strip_tags($scoredisplay->display_score($totalAverage, $mode)); $this->dataForGraph['my_result'][] = floatval($totalResultAverageValue); $this->dataForGraph['average'][] = floatval($totalAverageValue); $this->dataForGraph['my_result_no_float'][] = $data['result_score'][0]; if (empty($model)) { // Ranking if (in_array(1, $this->loadStats)) { $row[] = $ranking; } // Best if (in_array(2, $this->loadStats)) { $row[] = $best; } // Average if (in_array(3, $this->loadStats)) { $row[] = $average; } } if (get_class($item) === 'Category') { if ($this->exportToPdf == false) { $row[] = $this->build_edit_column($item); } } } // Category added. $sortable_data[] = $row; // Loading children if (get_class($item) === 'Category') { $parent_id = $item->get_id(); $cats = Category::load( $parent_id, null, null, null, null, null ); if (isset($cats[0])) { /** @var Category $subCategory */ $subCategory = $cats[0]; $allcat = $subCategory->get_subcategories($this->userId, $course_code, $session_id); $alleval = $subCategory->get_evaluations($this->userId); $alllink = $subCategory->get_links($this->userId); $sub_cat_info = new GradebookDataGenerator($allcat, $alleval, $alllink); $sub_cat_info->preLoadDataKey = $this->getPreloadDataKey(); $sub_cat_info->userId = $user_id; $data_array2 = $sub_cat_info->get_data( $sorting, $from, $this->per_page, false, $this->studentList ); $total_weight = 0; // Links. foreach ($data_array2 as $data) { $row = []; $item = $data[0]; //if the item is invisible, wrap it in a span with class invisible $invisibility_span_open = $isAllowedToEdit && $item->is_visible() == '0' ? '' : ''; $invisibility_span_close = $isAllowedToEdit && $item->is_visible() == '0' ? '' : ''; if (isset($item)) { $main_categories[$parent_id]['children'][$item->get_id()]['name'] = $item->get_name(); $main_categories[$parent_id]['children'][$item->get_id()]['weight'] = $item->get_weight(); } if ($this->teacherView) { if ($this->exportToPdf == false) { $row[] = $this->build_id_column($item); } } // Type $row[] = $this->build_type_column($item, ['style' => 'padding-left:5px']); // Name. $row[] = $invisibility_span_open.' '. $this->build_name_link($item, $type).$invisibility_span_close; // Description. if ($this->exportToPdf == false) { $row[] = $invisibility_span_open.$data[2].$invisibility_span_close; } $weight = $data[3]; $total_weight += $weight; // Weight $row[] = $invisibility_span_open.$weight.$invisibility_span_close; // Admins get an edit column. if (api_is_allowed_to_edit(null, true) && isset($_GET['user_id']) == false && (isset($_GET['action']) && $_GET['action'] != 'export_all' || !isset($_GET['action'])) ) { $cat = new Category(); $show_message = $cat->show_message_resource_delete($item->get_course_code()); if ($show_message === false) { if ($this->exportToPdf == false) { $row[] = $this->build_edit_column($item); } } } else { // Students get the results and certificates columns $eval_n_links = array_merge($alleval, $alllink); if (count($eval_n_links) > 0) { $value_data = isset($data[4]) ? $data[4] : null; if (!is_null($value_data)) { // Result $row[] = $value_data; $best = isset($data['best']) ? $data['best'] : null; $average = isset($data['average']) ? $data['average'] : null; $ranking = isset($data['ranking']) ? $data['ranking'] : null; if (empty($model)) { if (in_array(1, $this->loadStats)) { // Ranking $row[] = $ranking; } if (in_array(2, $this->loadStats)) { // Best $row[] = $best; } // Average if (in_array(3, $this->loadStats)) { $row[] = $average; } } } } if (!empty($cats)) { if ($this->exportToPdf == false) { $row[] = null; } } } if ($this->exportToPdf == false) { $row['child_of'] = $parent_id; } $sortable_data[] = $row; } // "Warning row" if (!empty($data_array)) { if ($this->teacherView) { // Compare the category weight to the sum of all weights inside the category if (intval($total_weight) == $category_weight) { $label = null; $total = GradebookUtils::score_badges( [ $total_weight.' / '.$category_weight, '100', ] ); } else { $label = Display::return_icon( 'warning.png', sprintf(get_lang('TotalWeightMustBeX'), $category_weight) ); $total = Display::badge($total_weight.' / '.$category_weight, 'warning'); } $row = [ null, null, "