123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- <?php
- /* For licensing terms, see /license.txt */
- /**
- * Glossay repository class definition
- * @package chamilo.glossary
- */
- /**
- * Init
- */
- namespace Glossary;
- use Database;
- /**
- * Glossary entry repository. Interface with the database.
- *
- * @author Laurent Opprecht <laurent@opprecht.info> for the University of Geneva
- * @licence /license.txt
- */
- class GlossaryRepository
- {
- /**
- * Return the instance of the repository.
- *
- * @return \Glossary\GlossaryRepository
- */
- public static function instance()
- {
- static $result = null;
- if (empty($result)) {
- $result = new self();
- }
- return $result;
- }
- /**
- *
- *
- * @param string $where Where filter to apply
- * @return array
- */
- public function find($where, $orderby = '', $limit = null)
- {
- $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
- $table = Database::get_course_table(TABLE_GLOSSARY);
- $tool = TOOL_GLOSSARY;
- $sql = "SELECT g.*,
- prop.id AS property_id,
- prop.tool,
- prop.insert_user_id,
- prop.insert_date,
- prop.lastedit_date,
- prop.ref,
- prop.lastedit_type,
- prop.lastedit_user_id,
- prop.to_group_id,
- prop.to_user_id,
- prop.visibility,
- prop.start_visible,
- prop.end_visible,
- prop.id_session
- FROM
- $table AS g,
- $table_item_property AS prop
- WHERE
- (g.glossary_id = prop.ref AND
- g.c_id = prop.c_id AND
- prop.tool = '$tool')";
-
- $sql .= $where ? "AND ($where)" : '';
- $sql .= ' ORDER BY ';
- $sql .= $orderby ? $orderby : 'name ASC';
- if($count){
- $from = (int)$limit->from;
- $count = (int)$limit->count;
- $sql .= " LIMIT $from, $count";
- }
- $rs = Database :: query($sql);
- while ($data = Database::fetch_object($rs)) {
- $result[] = Glossary::create($data);
- }
- return $result;
- }
-
- public function count($where)
- {
- $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
- $table = Database::get_course_table(TABLE_GLOSSARY);
- $tool = TOOL_GLOSSARY;
- $sql = "SELECT count(*) AS val
- FROM
- $table AS g,
- $table_item_property AS prop
- WHERE
- (g.glossary_id = prop.ref AND
- g.c_id = prop.c_id AND
- prop.tool = '$tool')";
-
- $sql .= $where ? "AND ($where)" : '';
- $sql .= " ORDER BY name ASC";
- $rs = Database :: query($sql);
- while ($data = Database::fetch_object($rs)) {
- return $data->val;
- }
- return 0;
- }
- /**
- *
- * @param string $where
- * @return \Glossary\Glossary
- */
- public function find_one($where)
- {
- $items = $this->find($where);
- foreach ($items as $item) {
- return $item;
- }
- return null;
- }
- /**
- * Retrieve one course description from its ids.
- *
- * @param int|Course $c_id
- * @param int $id
- * @return \Glossary\Glossary
- */
- public function find_one_by_id($c_id, $id)
- {
- $c_id = is_object($c_id) ? $c_id->get_id() : (int) $c_id;
- return $this->find_one("g.c_id = $c_id AND g.glossary_id = $id");
- }
- /**
- * Retrieve one course description from its ids.
- *
- * @param int|Course $c_id
- * @param string name
- * @return \Glossary\Glossary
- */
- public function find_one_by_course_and_name($c_id, $name)
- {
- $c_id = is_object($c_id) ? $c_id->get_id() : (int) $c_id;
- $name = Database::escape_string($name);
- return $this->find_one("g.c_id = $c_id AND g.name = '$name'");
- }
- /**
- * Returns the list of course descriptions belonging to a specific course and
- * session.
- *
- * @param object $course
- * @return Array
- */
- public function find_by_course($course, $orderby = '', $limit = null)
- {
- $c_id = (int)$course->c_id;
- $session_id = isset($course->session_id) ? (int)$course->session_id : 0;
- if (empty($c_id)) {
- return array();
- }
- $condition_session = api_get_session_condition($session_id, true, true);
- $where = "g.c_id = $c_id $condition_session";
- return $this->find($where, $orderby, $limit);
- }
-
- public function count_by_course($course)
- {
- $c_id = (int)$course->c_id;
- $session_id = isset($course->session_id) ? (int)$course->session_id : 0;
- if (empty($c_id)) {
- return 0;
- }
- $condition_session = api_get_session_condition($session_id, true, true);
- $where = "g.c_id = $c_id $condition_session";
- return $this->count($where);
- }
- /**
- *
- * @param object $glossary
- * @return bool
- */
- public function save($glossary)
- {
- $id = $glossary->id;
- if (empty($id)) {
- return $this->insert($glossary);
- } else {
- return $this->update($glossary);
- }
- }
- function next_display_order($c_id)
- {
- $table = Database :: get_course_table(TABLE_GLOSSARY);
- $sql = "SELECT MAX(display_order) FROM $table WHERE c_id = $c_id ";
- $rs = Database :: query($sql);
- list ($result) = Database :: fetch_row($rs);
- $result = intval($result) + 1;
- return $result;
- }
-
- /**
- *
- * @param \Glossary\Glossary $glossary
- * @return bool
- */
- public function insert($glossary)
- {
- $c_id = (int) $glossary->c_id;
- $name = trim($glossary->name);
- $name = Database::escape_string($name);
- $description = trim($glossary->description);
- $description = Database::escape_string($description);
- $session_id = (int) $glossary->session_id;
- $session_id = $session_id ? $session_id : '0';
- $display_order = $this->next_display_order($c_id);
- $table = Database :: get_course_table(TABLE_GLOSSARY);
- $sql = "INSERT INTO $table
- (c_id, name, description, display_order, session_id)
- VALUES
- ($c_id , '$name', '$description', $display_order, $session_id)";
- $result = (bool) Database :: query($sql);
- if ($result) {
- $id = Database::insert_id();
- $glossary->id = $id;
- $_course = api_get_course_info_by_id($c_id);
- $tool = TOOL_GLOSSARY;
- $user_id = api_get_user_id();
- api_item_property_update($_course, $tool, $id, 'GlossaryAdded', $user_id);
- }
- return $result;
- }
- /**
- *
- * @param \Glossary\Glossary $glossary
- * @return bool
- */
- function update($glossary)
- {
- $c_id = (int) $glossary->c_id;
- $id = (int) $glossary->id;
- $name = trim($glossary->name);
- $name = Database::escape_string($name);
- $description = trim($glossary->description);
- $description = Database::escape_string($description);
- $session_id = (int) $glossary->session_id;
- $session_id = $session_id ? $session_id : '0';
-
- $display_order = (int) $glossary->display_order;
- $table = Database :: get_course_table(TABLE_GLOSSARY);
- $sql = "UPDATE $table SET
- name = '$name',
- description = '$description',
- display_order = $display_order,
- session_id = $session_id
- WHERE
- c_id = $c_id AND
- glossary_id = $id";
- $result = (bool) Database :: query($sql);
- if ($result) {
- $_course = api_get_course_info_by_id($c_id);
- $tool = TOOL_GLOSSARY;
- $user_id = api_get_user_id();
- api_item_property_update($_course, $tool, $id, 'GlossaryUpdated', $user_id);
- }
- return $result;
- }
- /**
- *
- * @param object $glossary
- * @return boolean
- */
- public function remove($glossary)
- {
- $table = Database :: get_course_table(TABLE_GLOSSARY);
- $c_id = (int) $glossary->c_id;
- $id = (int) $glossary->id;
- if (empty($c_id) || empty($id)) {
- return false;
- }
- $sql = "DELETE FROM $table WHERE c_id=$c_id AND glossary_id=$id";
- $result = Database :: query($sql);
- if ($result) {
- $tool = TOOL_GLOSSARY;
- $tbl_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
- $sql = "DELETE FROM $tbl_property WHERE c_id=$c_id AND ref=$id AND tool='$tool'";
- Database :: query($sql);
- }
- return (bool) $result;
- }
- /**
- *
- * @param object $course
- * @return int
- */
- public function remove_by_course($course)
- {
- $items = $this->find_by_course($course);
- foreach ($items as $item) {
- $success = $this->remove($item);
- if ($success) {
- $result++;
- }
- }
- return $result;
- }
- }
|