From 01b7c4f7add9a31a1049b1eae9ac97cd79df7fbe Mon Sep 17 00:00:00 2001 From: Julio Date: Thu, 11 Jan 2018 15:02:13 +0100 Subject: [PATCH] Change queries, use c_id instead of course_code (gradebook tables) --- main/gradebook/lib/GradebookUtils.php | 20 +++-- main/gradebook/lib/be/abstractlink.class.php | 20 +++-- main/gradebook/lib/be/category.class.php | 84 ++++++++++++++----- main/gradebook/lib/be/evaluation.class.php | 78 +++++++++-------- .../gradebook/lib/fe/gradebooktable.class.php | 2 +- main/gradebook/lib/scoredisplay.class.php | 4 +- main/inc/global.inc.php | 7 +- main/inc/lib/course.lib.php | 14 ++-- main/inc/lib/document.lib.php | 11 ++- main/lp/lp_final_item.php | 4 +- 10 files changed, 160 insertions(+), 84 deletions(-) diff --git a/main/gradebook/lib/GradebookUtils.php b/main/gradebook/lib/GradebookUtils.php index 2a892489a1..d67ef7f53e 100644 --- a/main/gradebook/lib/GradebookUtils.php +++ b/main/gradebook/lib/GradebookUtils.php @@ -78,12 +78,13 @@ class GradebookUtils $course_code, $weight ) { - $course_code = Database::escape_string($course_code); - if (!empty($link_id)) { + $courseInfo = api_get_course_info($course_code); + if (!empty($link_id) && !empty($courseInfo)) { $link_id = intval($link_id); + $courseId = $courseInfo['real_id']; $sql = 'UPDATE '.Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK).' SET weight = ' . "'".api_float_val($weight)."'".' - WHERE course_code = "' . $course_code.'" AND id = '.$link_id; + WHERE c_id = "'.$courseId.'" AND id = '.$link_id; Database::query($sql); } @@ -498,10 +499,14 @@ class GradebookUtils $session_id = 0 ) { $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK); - $course_code = Database::escape_string($course_code); + $courseInfo = api_get_course_info($course_code); + if (empty($courseInfo)) { + return []; + } + $sql = "SELECT * FROM $table l WHERE - course_code = '$course_code' AND + c_id = ".$courseInfo['real_id']." AND type = ".(int) $resource_type." AND ref_id = " . (int) $resource_id; $res = Database::query($sql); @@ -546,7 +551,7 @@ class GradebookUtils $tbl_grade_links = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK); $sql = 'SELECT c.id FROM '.$course_table.' c INNER JOIN ' . $tbl_grade_links.' l - ON c.code = l.course_code + ON c.id = l.c_id WHERE l.id=' . intval($id_link).' OR l.category_id='.intval($id_link); $res = Database::query($sql); $array = Database::fetch_array($res, 'ASSOC'); @@ -883,10 +888,11 @@ class GradebookUtils $course_code = api_get_course_id(); } $session_id = api_get_session_id(); + $courseInfo = api_get_course_info($course_code); $t = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); $sql = "SELECT * FROM $t - WHERE course_code = '".Database::escape_string($course_code)."' "; + WHERE course_code = '".$courseInfo['real_id']."' "; if (!empty($session_id)) { $sql .= " AND session_id = ".(int) $session_id; } else { diff --git a/main/gradebook/lib/be/abstractlink.class.php b/main/gradebook/lib/be/abstractlink.class.php index c8564bb176..f86f9bd268 100755 --- a/main/gradebook/lib/be/abstractlink.class.php +++ b/main/gradebook/lib/be/abstractlink.class.php @@ -320,8 +320,11 @@ abstract class AbstractLink implements GradebookItem } else { $sql .= ' WHERE'; } - $sql .= " course_code = '".Database::escape_string($course_code)."'"; - $paramcount++; + $courseInfo = api_get_course_info($course_code); + if ($courseInfo) { + $sql .= " c_id = '".$courseInfo['real_id']."'"; + $paramcount++; + } } if (isset($category_id)) { if ($paramcount != 0) { @@ -360,7 +363,7 @@ abstract class AbstractLink implements GradebookItem $link->set_type($data['type']); $link->set_ref_id($data['ref_id']); $link->set_user_id($data['user_id']); - $link->set_course_code($data['course_code']); + $link->set_course_code(api_get_course_id()); $link->set_category_id($data['category_id']); $link->set_date($data['created_at']); $link->set_weight($data['weight']); @@ -395,8 +398,9 @@ abstract class AbstractLink implements GradebookItem WHERE ref_id = ".$this->get_ref_id()." AND category_id = ".$this->category->get_id()." AND - course_code = '".$this->course_code."' AND - type = ".$this->type; + c_id = '".$this->course_id."' AND + type = ".$this->type." "; + $result = Database::query($sql); $row = Database::fetch_array($result, 'ASSOC'); @@ -405,7 +409,7 @@ abstract class AbstractLink implements GradebookItem 'type' => $this->get_type(), 'ref_id' => $this->get_ref_id(), 'user_id' => $this->get_user_id(), - 'course_code' => $this->get_course_code(), + 'c_id' => $this->course_id, 'category_id' => $this->get_category_id(), 'weight' => $this->get_weight(), 'visible' => $this->is_visible(), @@ -439,11 +443,13 @@ abstract class AbstractLink implements GradebookItem $this->save_linked_data(); + $course = api_get_course_entity($this->getCourseId()); + $link ->setType($this->get_type()) ->setRefId($this->get_ref_id()) ->setUserId($this->get_user_id()) - ->setCourseCode($this->get_course_code()) + ->setCourse($course) ->setCategoryId($this->get_category_id()) ->setWeight($this->get_weight()) ->setVisible($this->is_visible()); diff --git a/main/gradebook/lib/be/category.class.php b/main/gradebook/lib/be/category.class.php index b27df37232..d6861bff95 100755 --- a/main/gradebook/lib/be/category.class.php +++ b/main/gradebook/lib/be/category.class.php @@ -16,6 +16,7 @@ class Category implements GradebookItem private $description; private $user_id; private $course_code; + private $courseId; private $parent; private $weight; private $visible; @@ -43,7 +44,8 @@ class Category implements GradebookItem $this->name = null; $this->description = null; $this->user_id = 0; - $this->course_code = null; + $this->course_code = ''; + $this->courseId = 0; $this->parent = 0; $this->weight = 0; $this->visible = false; @@ -378,14 +380,14 @@ class Category implements GradebookItem $cats[] = self::create_root_category(); return $cats; } - - $courseInfo = api_get_course_info_by_id(api_get_course_int_id()); + $courseId = api_get_course_int_id(); + $courseInfo = api_get_course_info_by_id($courseId); $courseCode = $courseInfo['code']; $session_id = intval($session_id); if (!empty($session_id)) { $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); - $sql = 'SELECT id, course_code + $sql = 'SELECT id, c_id FROM '.$table.' WHERE session_id = '.$session_id; $result_session = Database::query($sql); @@ -393,7 +395,7 @@ class Category implements GradebookItem $categoryList = []; while ($data_session = Database::fetch_array($result_session)) { $parent_id = $data_session['id']; - if ($data_session['course_code'] == $courseCode) { + if ($data_session['c_id'] == $courseId) { $categories = self::load($parent_id); $categoryList = array_merge($categoryList, $categories); } @@ -462,9 +464,12 @@ class Category implements GradebookItem } if ($course_code == '0') { - $sql .= ' course_code is null '; + $sql .= ' c_id is null '; } else { - $sql .= " course_code = '".Database::escape_string($course_code)."'"; + $courseInfo = api_get_course_info($course_code); + if ($courseInfo) { + $sql .= " c_id = '".intval($courseInfo['real_id'])."'"; + } } /*if ($show_session_categories !== true) { @@ -552,7 +557,9 @@ class Category implements GradebookItem $cat->set_name($data['name']); $cat->set_description($data['description']); $cat->set_user_id($data['user_id']); - $cat->set_course_code($data['course_code']); + $courseInfo = api_get_course_info_by_id($data['c_id']); + $cat->set_course_code($courseInfo['code']); + $cat->setCourseId($data['c_id']); $cat->set_parent_id($data['parent_id']); $cat->set_weight($data['weight']); $cat->set_visible($data['visible']); @@ -615,11 +622,14 @@ class Category implements GradebookItem if (isset($this->name) && isset($this->user_id)) { $em = Database::getManager(); + $courseInfo = api_get_course_info($this->course_code); + $course = api_get_course_entity($courseInfo['real_id']); + $category = new GradebookCategory(); $category->setName($this->name); $category->setDescription($this->description); $category->setUserId($this->user_id); - $category->setCourseCode($this->course_code); + $category->setCourse($course); $category->setParentId($this->parent); $category->setWeight($this->weight); $category->setVisible($this->visible); @@ -688,6 +698,7 @@ class Category implements GradebookItem { $em = Database::getManager(); + /** @var GradebookCategory $gradebookCategory */ $gradebookCategory = $em ->getRepository('ChamiloCoreBundle:GradebookCategory') ->find($this->id); @@ -696,10 +707,13 @@ class Category implements GradebookItem return false; } + $course = api_get_user_course_entity(); + $gradebookCategory->setName($this->name); $gradebookCategory->setDescription($this->description); $gradebookCategory->setUserId($this->user_id); - $gradebookCategory->setCourseCode($this->course_code); + $gradebookCategory->setCourse($course); + //$gradebookCategory->setCourseCode($this->course_code); $gradebookCategory->setParentId($this->parent); $gradebookCategory->setWeight($this->weight); $gradebookCategory->setVisible($this->visible); @@ -788,26 +802,39 @@ class Category implements GradebookItem /** * Not delete this category from the database,when visible=3 is category eliminated + * @param int $courseId */ - public function update_category_delete($course_id) + public function update_category_delete($courseId) { - $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); - $sql = 'UPDATE '.$table.' SET - visible = 3 - WHERE course_code ="'.Database::escape_string($course_id).'"'; + $tbl_grade_categories = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); + $sql = 'UPDATE '.$tbl_grade_categories.' SET visible=3 + WHERE c_id ="'.intval($courseId).'"'; + Database::query($sql); + } + + /** + * Delete this category from the database + * @param int $courseId + */ + public static function deleteCategoryFromCourse($courseId) + { + $table = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); + $sql = 'DELETE FROM '.$table.' + WHERE c_id ="'.intval($courseId).'"'; Database::query($sql); } /** - * Show message resource delete + * @param int $course_id + * @return bool|string */ public function show_message_resource_delete($course_id) { - $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); + $tbl_grade_categories = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); $sql = 'SELECT count(*) AS num - FROM '.$table.' + FROM '.$tbl_grade_categories.' WHERE - course_code = "'.Database::escape_string($course_id).'" AND + c_id = "'.Database::escape_string($course_id).'" AND visible = 3'; $res = Database::query($sql); $option = Database::fetch_array($res, 'ASSOC'); @@ -2604,6 +2631,25 @@ class Category implements GradebookItem Session::write('gradebook_dest', $url); } + /** + * @return int + */ + public function getCourseId() + { + return $this->courseId; + } + + /** + * @param int $courseId + * @return Category + */ + public function setCourseId($courseId) + { + $this->courseId = $courseId; + + return $this; + } + /** * @return int */ diff --git a/main/gradebook/lib/be/evaluation.class.php b/main/gradebook/lib/be/evaluation.class.php index 676bc95413..28f6bc416b 100755 --- a/main/gradebook/lib/be/evaluation.class.php +++ b/main/gradebook/lib/be/evaluation.class.php @@ -243,13 +243,16 @@ class Evaluation implements GradebookItem } if (isset($course_code) && $course_code <> '-1') { - if ($paramcount != 0) { - $sql .= ' AND'; - } else { - $sql .= ' WHERE'; + $courseInfo = api_get_course_info($course_code); + if ($courseInfo) { + if ($paramcount != 0) { + $sql .= ' AND'; + } else { + $sql .= ' WHERE'; + } + $sql .= " c_id = '".$courseInfo['real_id']."'"; + $paramcount++; } - $sql .= " course_code = '".Database::escape_string($course_code)."'"; - $paramcount++; } if (isset($category_id)) { @@ -301,7 +304,9 @@ class Evaluation implements GradebookItem $eval->set_name($data['name']); $eval->set_description($data['description']); $eval->set_user_id($data['user_id']); - $eval->set_course_code($data['course_code']); + $eval->setCourseId($data['c_id']); + $courseInfo = api_get_course_info_by_id($data['c_id']); + $eval->set_course_code($courseInfo['course_code']); $eval->set_category_id($data['category_id']); $eval->set_date(api_get_local_time($data['created_at'])); $eval->set_weight($data['weight']); @@ -336,8 +341,8 @@ class Evaluation implements GradebookItem if (isset($this->description)) { $sql .= ',description'; } - if (isset($this->course_code)) { - $sql .= ', course_code'; + if (isset($this->courseId)) { + $sql .= ', c_id'; } if (isset($this->category)) { $sql .= ', category_id'; @@ -348,12 +353,15 @@ class Evaluation implements GradebookItem .','.intval($this->get_user_id()) .','.api_float_val($this->get_weight()) .','.intval($this->get_max()) - .','.intval($this->is_visible()); + .','.intval($this->is_visible()) + .','.intval(0) + + ; if (isset($this->description)) { $sql .= ",'".Database::escape_string($this->get_description())."'"; } - if (isset($this->course_code)) { - $sql .= ",'".Database::escape_string($this->get_course_code())."'"; + if (isset($this->courseId)) { + $sql .= ",'".Database::escape_string($this->getCourseId())."'"; } if (isset($this->category)) { $sql .= ','.intval($this->get_category_id()); @@ -420,9 +428,9 @@ class Evaluation implements GradebookItem $sql .= 'null'; } $sql .= ', user_id = '.intval($this->get_user_id()) - .', course_code = '; - if (isset($this->course_code)) { - $sql .= "'".Database::escape_string($this->get_course_code())."'"; + .', c_id = '; + if (isset($this->courseId)) { + $sql .= "'".Database::escape_string($this->getCourseId())."'"; } else { $sql .= 'null'; } @@ -473,18 +481,15 @@ class Evaluation implements GradebookItem if (api_is_allowed_to_edit()) { $parent = Category::load($parent); - $code = $parent[0]->get_course_code(); - $courseInfo = api_get_course_info($code); - $courseId = $courseInfo['real_id']; - - if (isset($code) && $code != '0') { - $main_course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER); + $courseId = $parent[0]->getCourseId(); + if (isset($courseId) && !empty($courseId)) { + $table = Database :: get_main_table(TABLE_MAIN_COURSE_USER); $sql .= ' AND user_id IN ( - SELECT user_id FROM '.$main_course_user_table.' - WHERE - c_id = '.$courseId.' AND - status = '.COURSEMANAGER.' - )'; + SELECT user_id FROM '.$table.' + WHERE + c_id = '.$courseId.' AND + status = '.COURSEMANAGER.' + )'; } else { $sql .= ' AND user_id = '.api_get_user_id(); } @@ -659,15 +664,11 @@ class Evaluation implements GradebookItem $root = [0, get_lang('RootCat'), $level]; $targets[] = $root; - if (isset($this->course_code) && !empty($this->course_code)) { + if (isset($this->courseId) && !empty($this->courseId)) { $crscats = Category::load(null, null, $this->course_code, 0); foreach ($crscats as $cat) { - $targets[] = [$cat->get_id(), $cat->get_name(), $level + 1]; - $targets = $this->addTargetSubcategories( - $targets, - $level + 1, - $cat->get_id() - ); + $targets[] = [$cat->get_id(), $cat->get_name(), $level+1]; + $targets = $this->addTargetSubcategories($targets, $level+1, $cat->get_id()); } } @@ -846,4 +847,15 @@ class Evaluation implements GradebookItem { $this->studentList = $list; } + + /** + * @param int $courseId + * @return Evaluation + */ + public function setCourseId($courseId) + { + $this->courseId = $courseId; + + return $this; + } } diff --git a/main/gradebook/lib/fe/gradebooktable.class.php b/main/gradebook/lib/fe/gradebooktable.class.php index ebeb590659..09f141a6ae 100755 --- a/main/gradebook/lib/fe/gradebooktable.class.php +++ b/main/gradebook/lib/fe/gradebooktable.class.php @@ -1036,7 +1036,7 @@ class GradebookTable extends SortableTable if (api_is_allowed_to_edit() && $show_message === false) { if ($item->get_type() == 'presence') { return ' ' - .'' + .'' .$item->get_name() .''; } else { diff --git a/main/gradebook/lib/scoredisplay.class.php b/main/gradebook/lib/scoredisplay.class.php index 7d28bb1a9f..57b209ba12 100755 --- a/main/gradebook/lib/scoredisplay.class.php +++ b/main/gradebook/lib/scoredisplay.class.php @@ -174,7 +174,7 @@ class ScoreDisplay private function get_current_gradebook_category_id() { $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); - $curr_course_code = api_get_course_id(); + $courseId = api_get_course_int_id(); $curr_session_id = api_get_session_id(); if (empty($curr_session_id)) { @@ -184,7 +184,7 @@ class ScoreDisplay } $sql = 'SELECT id FROM '.$table.' - WHERE course_code = "'.$curr_course_code.'" '.$session_condition; + WHERE c_id = "'.$courseId.'" '.$session_condition; $rs = Database::query($sql); $category_id = 0; if (Database::num_rows($rs) > 0) { diff --git a/main/inc/global.inc.php b/main/inc/global.inc.php index 8cf9f260cd..6289656a25 100755 --- a/main/inc/global.inc.php +++ b/main/inc/global.inc.php @@ -107,11 +107,16 @@ if (file_exists(api_get_path(SYS_PATH).'.env')) { // Get settings from .env file created when installation chamilo (new Dotenv())->load(api_get_path(SYS_PATH).'.env'); $kernel->boot(); - $doctrine = $kernel->getContainer()->get('doctrine'); + $container = $kernel->getContainer(); + $doctrine = $container->get('doctrine'); $database = new \Database(); $database->setManager($doctrine->getManager()); $database->setConnection($doctrine->getConnection()); + + \CourseManager::setCourseManager( + $container->get('chamilo_core.entity.manager.course_manager') + ); } else { $global_error_code = 3; // The database server is not available or credentials are invalid. diff --git a/main/inc/lib/course.lib.php b/main/inc/lib/course.lib.php index 7327ba862b..6bebc6f909 100755 --- a/main/inc/lib/course.lib.php +++ b/main/inc/lib/course.lib.php @@ -6009,17 +6009,15 @@ class CourseManager /** * Returns course code from a given gradebook category's id * @param int Category ID - * @return string Course code + * @return string int */ public static function get_course_by_category($category_id) { - $category_id = intval($category_id); - $info = Database::fetch_array( - Database::query('SELECT course_code FROM '.Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY).' - WHERE id=' . $category_id), - 'ASSOC' - ); - return $info ? $info['course_code'] : false; + $category_id = (int) $category_id; + $sql = 'SELECT c_id FROM '.Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY).' + WHERE id='.$category_id; + $info = Database::fetch_array(Database::query($sql), 'ASSOC'); + return $info ? $info['c_id'] : false; } /** diff --git a/main/inc/lib/document.lib.php b/main/inc/lib/document.lib.php index 0eb5b2cccb..d2249be9df 100644 --- a/main/inc/lib/document.lib.php +++ b/main/inc/lib/document.lib.php @@ -1696,7 +1696,7 @@ class DocumentManager * @param int $session_id * @return void */ - public static function attach_gradebook_certificate($course_id, $document_id, $session_id = 0) + public static function attach_gradebook_certificate($courseCode, $document_id, $session_id = 0) { $tbl_category = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); $session_id = intval($session_id); @@ -1711,8 +1711,10 @@ class DocumentManager } else { $sql_session = ''; } + $courseInfo = api_get_course_info($courseCode); + $sql = 'UPDATE '.$tbl_category.' SET document_id="'.intval($document_id).'" - WHERE course_code="' . Database::escape_string($course_id).'" '.$sql_session; + WHERE c_id ="' . $courseInfo['real_id'].'" '.$sql_session; Database::query($sql); } @@ -1724,7 +1726,7 @@ class DocumentManager * * @return int The default certificate id */ - public static function get_default_certificate_id($course_id, $session_id = 0) + public static function get_default_certificate_id($courseCode, $session_id = 0) { $tbl_category = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); $session_id = intval($session_id); @@ -1739,8 +1741,9 @@ class DocumentManager } else { $sql_session = ''; } + $courseInfo = api_get_course_info($courseCode); $sql = 'SELECT document_id FROM '.$tbl_category.' - WHERE course_code="' . Database::escape_string($course_id).'" '.$sql_session; + WHERE c_id ="'.$courseInfo['real_id'].'" '.$sql_session; $rs = Database::query($sql); $num = Database::num_rows($rs); diff --git a/main/lp/lp_final_item.php b/main/lp/lp_final_item.php index 04e16bbc98..eb121a69f7 100644 --- a/main/lp/lp_final_item.php +++ b/main/lp/lp_final_item.php @@ -132,8 +132,8 @@ if ($accessGranted == false) { if ($link) { $cat = new Category(); - $catCourseCode = CourseManager::get_course_by_category($categoryId); - $show_message = $cat->show_message_resource_delete($catCourseCode); + $courseId = CourseManager::get_course_by_category($categoryId); + $show_message = $cat->show_message_resource_delete($courseId); if ($show_message == '') { if (!api_is_allowed_to_edit() && !api_is_excluded_user_type()) {