123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <?php
- /* For licensing terms, see /license.txt */
- /**
- * Link category repository class definition
- * @package chamilo.link
- */
- /**
- * Init
- */
- namespace Link;
- use Database;
- /**
- * Database interface for link_category
- *
- * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
- * @license /license.txt
- */
- class LinkCategoryRepository
- {
- /**
- *
- * @return \Link\LinkCategoryRepository
- */
- public static function instance()
- {
- static $result = null;
- if (empty($result)) {
- $result = new self();
- }
- return $result;
- }
- public function save($category)
- {
- $id = $category->id;
- if (empty($id)) {
- return $this->insert($category);
- } else {
- return $this->update($category);
- }
- }
- public function insert($category)
- {
- $c_id = (int) $category->c_id;
- $session_id = (int) $category->session_id;
- $session_id = $session_id ? $session_id : '0';
- $category_title = trim($category->category_title);
- $category_title = Database::escape_string($category_title);
- $description = trim($category->description);
- $description = Database::escape_string($description);
- $display_order = $this->next_display_order($c_id);
- $table = Database :: get_course_table(TABLE_LINK_CATEGORY);
- $sql = "INSERT INTO $table
- (c_id, category_title, description, display_order, session_id)
- VALUES
- ($c_id , '$category_title', '$description', $display_order, $session_id)";
- $result = (bool) Database :: query($sql);
- if ($result) {
- $id = Database::insert_id();
- $category->id = $id;
- $category->display_order = $display_order;
- }
- return $result;
- }
- function update($category)
- {
- $c_id = (int) $category->c_id;
- $id = (int) $category->id;
- $session_id = (int) $category->session_id;
- $session_id = $session_id ? $session_id : '0';
- $category_title = trim($category->category_title);
- $category_title = Database::escape_string($category_title);
- $description = trim($category->description);
- $description = Database::escape_string($description);
- $display_order = (int) $category->display_order;
- $table = Database :: get_course_table(TABLE_LINK_CATEGORY);
- $sql = "UPDATE $table SET
- category_title = '$category_title',
- description = '$description',
- display_order = $display_order,
- session_id = $session_id
- WHERE
- c_id = $c_id AND
- id = $id";
- $result = (bool) Database :: query($sql);
- return $result;
- }
- function remove($category)
- {
- $table = Database :: get_course_table(TABLE_LINK_CATEGORY);
- $c_id = (int) $category->c_id;
- $id = (int) $category->id;
- $sql = "DELETE FROM $table WHERE c_id=$c_id AND id=$id";
- $success = (bool) Database :: query($sql);
- if ($success) {
- LinkRepository::instance()->remove_by_category($category);
- }
- return $success;
- }
-
- function remove_by_course($c_id, $session_id = 0)
- {
- $result = true;
- $categories = $this->find_by_course($c_id, $session_id);
- foreach($categories as $category){
- $success = $this->remove($category);
- if(!$success){
- $result = false;
- }
- }
- return $result;
- }
- function next_display_order($c_id)
- {
- $table = Database :: get_course_table(TABLE_LINK_CATEGORY);
- $sql = "SELECT MAX(display_order) FROM $table WHERE c_id = $c_id ";
- $rs = Database :: query($sql);
- list ($result) = Database :: fetch_row($rs);
- $result = $result + 1;
- $result = intval($result);
- return $result;
- }
- /**
- *
- * @param string $where
- * @return array
- */
- public function find($where)
- {
- $result = array();
- $table = Database :: get_course_table(TABLE_LINK_CATEGORY);
- $where = $where ? "WHERE $where" : '';
- $sql = "SELECT * FROM $table $where ORDER BY display_order DESC";
- $rs = Database :: query($sql);
- while ($data = Database::fetch_object($rs)) {
- $result[] = LinkCategory::create($data);
- }
- return $result;
- }
- /**
- *
- * @param string $where
- * @return \Link\LinkCategory
- */
- public function find_one($where)
- {
- $items = $this->find($where);
- foreach ($items as $item) {
- return $item;
- }
- return null;
- }
- /**
- *
- * @param int $c_id
- * @param int $id
- * @return \Link\LinkCategory
- */
- public function find_one_by_id($c_id, $id)
- {
- $where = "c_id = $c_id AND id = $id";
- return $this->find_one($where);
- }
- /**
- *
- * @param int $c_id
- * @param int $session_id
- * @param string $title
- * @return \Link\LinkCategory
- */
- public function find_one_by_course_and_name($c_id, $session_id = 0, $title)
- {
- $c_id = (int) $c_id;
- $session_id = (int) $session_id;
- $title = Database::escape_string($title);
-
- $condition_session = api_get_session_condition($session_id, true, true);
- $where = "c_id = $c_id $condition_session AND category_title = '$title'";
- return $this->find_one($where);
- }
- function find_by_course($c_id, $session_id = 0)
- {
- $c_id = (int) $c_id;
- $session_id = (int) $session_id;
- $condition_session = api_get_session_condition($session_id, true, true);
- $where = "c_id = $c_id $condition_session";
- return $this->find($where);
- }
- /**
- * Ensure $ids are sorted in the order given from greater to lower.
- *
- * Simple algorithm, works only if all ids are given at the same time.
- * This would not work if pagination were used and only a subset were sorted.
- * On the other hand the algorithm is sturdy, it will work even if current
- * weights/display orders are not correctly set up in the db.
- *
- * @param int $c_id
- * @param array $ids
- */
- public function order($c_id, $ids)
- {
- $result = true;
- $ids = array_map('intval', $ids);
- $table = Database::get_course_table(TABLE_LINK_CATEGORY);
- $counter = 0;
- $weight = count($ids) + 1;
- foreach ($ids as $id) {
- $sql = "UPDATE $table SET display_order = $weight WHERE c_id = $c_id AND id = $id";
- $success = Database::query($sql);
- if (!$success) {
- $result = false;
- }
- --$weight;
- }
- return $result;
- }
- }
|