/**
  * 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;
     // determine sorting type
     $col_adjust = api_is_allowed_to_edit() ? 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;
         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 = api_get_user_id();
     $course_code = api_get_course_id();
     $session_id = api_get_session_id();
     $status_user = api_get_status_of_user_in_course($user_id, $course_code);
     $data_array = $this->datagen->get_data($sorting, $from, $this->per_page);
     // generate the data to display
     $sortable_data = array();
     $weight_total_links = 0;
     $main_categories = array();
     $main_cat = Category::load(null, null, $course_code, null, null, $session_id, false);
     $total_categories_weight = 0;
     $scoredisplay = ScoreDisplay::instance();
     // Categories.
     foreach ($data_array as $data) {
         // list of items inside the gradebook (exercises, lps, forums, etc)
         $row = array();
         /** @var AbstractLink $item */
         $item = $item_category = $data[0];
         //if the item is invisible, wrap it in a span with class invisible
         $invisibility_span_open = api_is_allowed_to_edit() && $item->is_visible() == '0' ? '<span class="invisible">' : '';
         $invisibility_span_close = api_is_allowed_to_edit() && $item->is_visible() == '0' ? '</span>' : '';
         // Id
         if (api_is_allowed_to_edit(null, true)) {
             $row[] = $this->build_id_column($item);
         }
         // Type.
         $row[] = $this->build_type_column($item);
         // Name.
         if (get_class($item) == 'Category') {
             $row[] = $invisibility_span_open . '<h3>' . $item->get_name() . '</h3>' . $invisibility_span_close;
             $main_categories[$item->get_id()]['name'] = $item->get_name();
         } else {
             $row[] = $invisibility_span_open . $this->build_name_link($item) . $invisibility_span_close;
             $main_categories[$item->get_id()]['name'] = $this->build_name_link($item);
         }
         $main_categories[$item->get_id()]['weight'] = $item->get_weight();
         $total_categories_weight += $item->get_weight();
         // Description.
         $row[] = $invisibility_span_open . $data[2] . $invisibility_span_close;
         // Weight.
         $average = $scoredisplay->display_score(array($data['3'], $this->currentcat->get_weight()), SCORE_SIMPLE, SCORE_BOTH, true);
         // Weight
         if (api_is_allowed_to_edit(null, true)) {
             $row[] = $invisibility_span_open . Display::tag('h4', $average) . $invisibility_span_close;
         } else {
             $row[] = $invisibility_span_open . $average . $invisibility_span_close;
         }
         $category_weight = $item->get_weight();
         if (api_is_allowed_to_edit(null, true)) {
             $weight_total_links += $data[3];
         } else {
             $cattotal = Category::load($_GET['selectcat']);
             $scoretotal = $cattotal[0]->calc_score(api_get_user_id());
             $item_value = $scoredisplay->display_score($scoretotal, SCORE_SIMPLE);
         }
         // Edit (for admins).
         if (api_is_allowed_to_edit(null, true)) {
             $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 {
             //students get the results and certificates columns
             if (count($this->evals_links) > 0 && $status_user != 1) {
                 $value_data = isset($data[4]) ? $data[4] : null;
                 if (!is_null($value_data)) {
                     $row[] = Display::tag('h4', $value_data);
                 } else {
                     $row[] = $this->build_edit_column($item);
                 }
             } else {
                 $score = $item->calc_score(api_get_user_id());
                 if (!empty($score[1])) {
                     $complete_score = $scoredisplay->display_score($score, SCORE_DIV_PERCENT);
                     $score = $score[0] / $score[1] * $item->get_weight();
                     $score = $scoredisplay->display_score(array($score, null), SCORE_SIMPLE);
                     $row[] = Display::tip($score, $complete_score);
                 } else {
                     $row[] = '-';
                 }
                 if (!empty($this->cats)) {
                     $row[] = $this->build_edit_column($item);
                 }
             }
         }
         // Category added.
         $sortable_data[] = $row;
         // Loading children
         if (get_class($item) == 'Category') {
             $stud_id = api_get_user_id();
             $course_code = api_get_course_id();
             $session_id = api_get_session_id();
             $parent_id = $item->get_id();
             $cats = Category::load($parent_id, null, null, null, null, null);
             if (isset($cats[0])) {
                 $allcat = $cats[0]->get_subcategories($stud_id, $course_code, $session_id);
                 $alleval = $cats[0]->get_evaluations($stud_id);
                 $alllink = $cats[0]->get_links($stud_id);
                 $sub_cat_info = new GradebookDataGenerator($allcat, $alleval, $alllink);
                 $data_array = $sub_cat_info->get_data($sorting, $from, $this->per_page);
                 $total_weight = 0;
                 // Links.
                 foreach ($data_array as $data) {
                     $row = array();
                     $item = $data[0];
                     //if the item is invisible, wrap it in a span with class invisible
                     $invisibility_span_open = api_is_allowed_to_edit() && $item->is_visible() == '0' ? '<span class="invisible">' : '';
                     $invisibility_span_close = api_is_allowed_to_edit() && $item->is_visible() == '0' ? '</span>' : '';
                     $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 (api_is_allowed_to_edit(null, true)) {
                         $row[] = $this->build_id_column($item);
                     }
                     $row[] = $this->build_type_column($item, array('style' => 'padding-left:5px'));
                     // Name.
                     $row[] = $invisibility_span_open . "&nbsp;&nbsp;&nbsp;  " . $this->build_name_link($item) . $invisibility_span_close;
                     // Description.
                     $row[] = $invisibility_span_open . $data[2] . $invisibility_span_close;
                     $weight = $data[3];
                     $total_weight += $weight;
                     $row[] = $invisibility_span_open . $weight . $invisibility_span_close;
                     if (api_is_allowed_to_edit(null, true)) {
                         //$weight_total_links += intval($data[3]);
                     } else {
                         $cattotal = Category::load($_GET['selectcat']);
                         $scoretotal = $cattotal[0]->calc_score(api_get_user_id());
                         $item_value = $scoretotal[0];
                     }
                     // Admins get an edit column.
                     if (api_is_allowed_to_edit(null, true)) {
                         $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 {
                         //students get the results and certificates columns
                         $eval_n_links = array_merge($alleval, $alllink);
                         if (count($eval_n_links) > 0 && $status_user != 1) {
                             $value_data = isset($data[4]) ? $data[4] : null;
                             if (!is_null($value_data)) {
                                 $score = $item->calc_score(api_get_user_id());
                                 $new_score = $data[3] * $score[0] / $score[1];
                                 $new_score = floatval(number_format($new_score, api_get_setting('gradebook_number_decimals')));
                                 $row[] = Display::tip($new_score, $data[4]);
                             }
                         }
                         if (!empty($cats)) {
                             $row[] = null;
                         }
                     }
                     $row['child_of'] = $parent_id;
                     $sortable_data[] = $row;
                 }
                 // "Warning row"
                 if (!empty($data_array)) {
                     if (api_is_allowed_to_edit()) {
                         // Compare the category weight to the sum of all weights inside the category
                         if (intval($total_weight) == $category_weight) {
                             $label = null;
                             $total = score_badges(array($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 = array(null, null, "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<h5>" . get_lang('SubTotal') . '</h5>', null, $total . ' ' . $label, 'child_of' => $parent_id);
                         $sortable_data[] = $row;
                     }
                 }
             }
         }
     }
     //end looping categories
     if (api_is_allowed_to_edit()) {
         if (count($main_cat) > 1) {
             $main_weight = intval($main_cat[0]->get_weight());
             if (intval($total_categories_weight) == $main_weight) {
                 $total = score_badges(array($total_categories_weight . ' / ' . $main_weight, '100'));
             } else {
                 $total = Display::badge($total_categories_weight . ' / ' . $main_weight, 'warning');
             }
             $row = array(null, null, '<h3>' . get_lang('Total') . '</h3>', null, $total);
             $sortable_data[] = $row;
         }
     }
     // warning messages
     $view = isset($_GET['view']) ? $_GET['view'] : null;
     if (api_is_allowed_to_edit()) {
         if (isset($_GET['selectcat']) && $_GET['selectcat'] > 0 && $view != 'presence') {
             $id_cat = intval($_GET['selectcat']);
             $category = Category::load($id_cat);
             $weight_category = intval($this->build_weight($category[0]));
             $course_code = $this->build_course_code($category[0]);
             $weight_total_links = round($weight_total_links);
             if ($weight_total_links > $weight_category || $weight_total_links < $weight_category || $weight_total_links > $weight_category) {
                 $warning_message = sprintf(get_lang('TotalWeightMustBeX'), $weight_category);
                 $modify_icons = '<a class="right_link" href="gradebook_edit_cat.php?editcat=' . $id_cat . '&cidReq=' . $course_code . '&id_session=' . api_get_session_id() . '">' . Display::return_icon('edit.png', $warning_message, array(), ICON_SIZE_SMALL) . '</a>';
                 $warning_message .= $modify_icons;
                 Display::display_warning_message($warning_message, false);
             }
             $content_html = DocumentManager::replace_user_info_into_html(api_get_user_id(), $course_code);
             if (!empty($content_html)) {
                 $new_content = explode('</head>', $content_html['content']);
             }
             if (empty($new_content[0])) {
                 // Set default certificate
                 $courseData = api_get_course_info($course_code);
                 DocumentManager::generateDefaultCertificate($courseData);
             }
         }
         if (empty($_GET['selectcat'])) {
             $categories = Category::load();
             $weight_categories = $certificate_min_scores = $course_codes = array();
             foreach ($categories as $category) {
                 $course_code_category = $this->build_course_code($category);
                 if (!empty($course_code)) {
                     if ($course_code_category == $course_code) {
                         $weight_categories[] = intval($this->build_weight($category));
                         $certificate_min_scores[] = intval($this->build_certificate_min_score($category));
                         $course_codes[] = $course_code;
                         break;
                     }
                 } else {
                     $weight_categories[] = intval($this->build_weight($category));
                     $certificate_min_scores[] = intval($this->build_certificate_min_score($category));
                     $course_codes[] = $course_code_category;
                 }
             }
             if (is_array($weight_categories) && is_array($certificate_min_scores) && is_array($course_codes)) {
                 $warning_message = '';
                 for ($x = 0; $x < count($weight_categories); $x++) {
                     $weight_category = intval($weight_categories[$x]);
                     $certificate_min_score = intval($certificate_min_scores[$x]);
                     $course_code = $course_codes[$x];
                     if (empty($certificate_min_score) || $certificate_min_score > $weight_category) {
                         $warning_message .= $course_code . '&nbsp;-&nbsp;' . get_lang('CertificateMinimunScoreIsRequiredAndMustNotBeMoreThan') . '&nbsp;' . $weight_category . '<br />';
                     }
                 }
                 if (!empty($warning_message)) {
                     Display::display_warning_message($warning_message, false);
                 }
             }
         }
     }
     return $sortable_data;
 }
예제 #2
0
 /**
  * Subscribes courses to the given session and optionally (default)
  * unsubscribes previous users
  * @author Carlos Vargas from existing code
  * @param	int		$sessionId
  * @param	array	$courseList List of courses int ids
  * @param	bool	$removeExistingCoursesWithUsers Whether to unsubscribe
  * existing courses and users (true, default) or not (false)
  * @param $copyEvaluation from base course to session course
  * @return	void	Nothing, or false on error
  * */
 public static function add_courses_to_session($sessionId, $courseList, $removeExistingCoursesWithUsers = true, $copyEvaluation = false)
 {
     $sessionId = intval($sessionId);
     if (empty($sessionId) || empty($courseList)) {
         return false;
     }
     $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
     $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
     $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
     $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
     // Get list of courses subscribed to this session
     $sql = "SELECT c_id\n                FROM {$tbl_session_rel_course}\n                WHERE session_id = {$sessionId}";
     $rs = Database::query($sql);
     $existingCourses = Database::store_result($rs);
     $nbr_courses = count($existingCourses);
     // Get list of users subscribed to this session
     $sql = "SELECT user_id\n                FROM {$tbl_session_rel_user}\n                WHERE\n                    session_id = {$sessionId} AND\n                    relation_type<>" . SESSION_RELATION_TYPE_RRHH . "";
     $result = Database::query($sql);
     $user_list = Database::store_result($result);
     // Remove existing courses from the session.
     if ($removeExistingCoursesWithUsers === true && !empty($existingCourses)) {
         foreach ($existingCourses as $existingCourse) {
             if (!in_array($existingCourse['c_id'], $courseList)) {
                 $sql = "DELETE FROM {$tbl_session_rel_course}\n                            WHERE\n                                c_id = " . $existingCourse['c_id'] . " AND\n                                session_id = {$sessionId}";
                 Database::query($sql);
                 $sql = "DELETE FROM {$tbl_session_rel_course_rel_user}\n                            WHERE\n                                c_id = " . $existingCourse['c_id'] . " AND\n                                session_id = {$sessionId}";
                 Database::query($sql);
                 Event::addEvent(LOG_SESSION_DELETE_COURSE, LOG_COURSE_ID, $existingCourse['c_id'], api_get_utc_datetime(), api_get_user_id(), $existingCourse['c_id'], $sessionId);
                 CourseManager::remove_course_ranking($existingCourse['c_id'], $sessionId);
                 $nbr_courses--;
             }
         }
     }
     // Pass through the courses list we want to add to the session
     foreach ($courseList as $courseId) {
         $courseInfo = api_get_course_info_by_id($courseId);
         // If course doesn't exists continue!
         if (empty($courseInfo)) {
             continue;
         }
         $exists = false;
         // check if the course we want to add is already subscribed
         foreach ($existingCourses as $existingCourse) {
             if ($courseId == $existingCourse['c_id']) {
                 $exists = true;
             }
         }
         if (!$exists) {
             // Copy gradebook categories and links (from base course)
             // to the new course session
             if ($copyEvaluation) {
                 $cats = Category::load(null, null, $courseInfo['code']);
                 if (!empty($cats)) {
                     $categoryIdList = [];
                     /** @var Category $cat */
                     foreach ($cats as $cat) {
                         $categoryIdList[$cat->get_id()] = $cat->get_id();
                     }
                     $newCategoryIdList = [];
                     foreach ($cats as $cat) {
                         $links = $cat->get_links(null, false, $courseInfo['code'], 0);
                         $cat->set_session_id($sessionId);
                         $oldCategoryId = $cat->get_id();
                         $newId = $cat->add();
                         $newCategoryIdList[$oldCategoryId] = $newId;
                         $parentId = $cat->get_parent_id();
                         if (!empty($parentId)) {
                             $newParentId = $newCategoryIdList[$parentId];
                             $cat->set_parent_id($newParentId);
                             $cat->save();
                         }
                         /** @var AbstractLink $link */
                         foreach ($links as $link) {
                             $newCategoryId = $newCategoryIdList[$link->get_category_id()];
                             $link->set_category_id($newCategoryId);
                             $link->add();
                         }
                     }
                     // Create
                     DocumentManager::generateDefaultCertificate($courseInfo, true, $sessionId);
                 }
             }
             // If the course isn't subscribed yet
             $sql = "INSERT INTO {$tbl_session_rel_course} (session_id, c_id)\n                        VALUES ({$sessionId}, {$courseId})";
             Database::query($sql);
             Event::addEvent(LOG_SESSION_ADD_COURSE, LOG_COURSE_ID, $courseId, api_get_utc_datetime(), api_get_user_id(), $courseId, $sessionId);
             // We add the current course in the existing courses array,
             // to avoid adding another time the current course
             $existingCourses[] = array('c_id' => $courseId);
             $nbr_courses++;
             // subscribe all the users from the session to this course inside the session
             $nbr_users = 0;
             foreach ($user_list as $enreg_user) {
                 $enreg_user_id = intval($enreg_user['user_id']);
                 $sql = "INSERT IGNORE INTO {$tbl_session_rel_course_rel_user} (session_id, c_id, user_id)\n                            VALUES ({$sessionId}, {$courseId}, {$enreg_user_id})";
                 $result = Database::query($sql);
                 Event::addEvent(LOG_SESSION_ADD_USER_COURSE, LOG_USER_ID, $enreg_user_id, api_get_utc_datetime(), api_get_user_id(), $courseId, $sessionId);
                 if (Database::affected_rows($result)) {
                     $nbr_users++;
                 }
             }
             $sql = "UPDATE {$tbl_session_rel_course}\n                        SET nbr_users = {$nbr_users}\n                        WHERE session_id = {$sessionId} AND c_id = {$courseId}";
             Database::query($sql);
         }
     }
     $sql = "UPDATE {$tbl_session}\n                SET nbr_courses = {$nbr_courses}\n                WHERE id = {$sessionId}";
     Database::query($sql);
 }