course_description_repository.class.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Course description repository class definition
  5. * @package chamilo.course_description
  6. */
  7. /**
  8. * Init
  9. */
  10. namespace CourseDescription;
  11. use Database;
  12. /**
  13. * Description of course_description_controller
  14. *
  15. * @author Laurent Opprecht <laurent@opprecht.info> for the University of Geneva
  16. * @licence /license.txt
  17. */
  18. class CourseDescriptionRepository
  19. {
  20. /**
  21. * Return the instance of the repository.
  22. *
  23. * @return \CourseDescription\CourseDescriptionRepository
  24. */
  25. public static function instance()
  26. {
  27. static $result = null;
  28. if (empty($result)) {
  29. $result = new self();
  30. }
  31. return $result;
  32. }
  33. /**
  34. *
  35. *
  36. * @param string $where Where filter to apply
  37. * @return array
  38. */
  39. public function find($where)
  40. {
  41. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  42. $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  43. $tool = TOOL_COURSE_DESCRIPTION;
  44. $sql = "SELECT des.*,
  45. prop.id AS property_id,
  46. prop.tool,
  47. prop.insert_user_id,
  48. prop.insert_date,
  49. prop.lastedit_date,
  50. prop.ref,
  51. prop.lastedit_type,
  52. prop.lastedit_user_id,
  53. prop.to_group_id,
  54. prop.to_user_id,
  55. prop.visibility,
  56. prop.start_visible,
  57. prop.end_visible,
  58. prop.id_session
  59. FROM
  60. $table AS des,
  61. $table_item_property AS prop
  62. WHERE
  63. (des.id = prop.ref AND
  64. des.c_id = prop.c_id AND
  65. prop.tool = '$tool')";
  66. $sql .= $where ? "AND ($where)" : '';
  67. $rs = Database :: query($sql);
  68. while ($data = Database::fetch_object($rs)) {
  69. $result[] = CourseDescription::create($data);
  70. }
  71. return $result;
  72. //$result = new ResultSet($sql);
  73. //return $result->return_type(__CLASS__);
  74. }
  75. /**
  76. *
  77. * @param string $where
  78. * @return \CourseDescription\CourseDescription
  79. */
  80. public function find_one($where)
  81. {
  82. $items = $this->find($where);
  83. foreach ($items as $item) {
  84. return $item;
  85. }
  86. return null;
  87. }
  88. /**
  89. * Retrieve one course description from its ids.
  90. *
  91. * @param int|Course $c_id
  92. * @param int $id
  93. * @return \CourseDescription\CourseDescription
  94. */
  95. public function find_one_by_id($c_id, $id)
  96. {
  97. $c_id = is_object($c_id) ? $c_id->get_id() : (int) $c_id;
  98. return $this->find_one("des.c_id = $c_id AND des.id = $id");
  99. }
  100. /**
  101. * Returns the list of course descriptions belonging to a specific course and
  102. * session.
  103. *
  104. * @param object $course
  105. * @return Array
  106. */
  107. public function find_by_course($course)
  108. {
  109. $c_id = (int)$course->c_id;
  110. $session_id = isset($course->session_id) ? (int)$course->session_id : 0;
  111. if (empty($c_id)) {
  112. return array();
  113. }
  114. $condition_session = api_get_session_condition($session_id, true, true);
  115. $where = "des.c_id = $c_id $condition_session";
  116. return $this->find($where);
  117. }
  118. /**
  119. *
  120. * @param object $description
  121. * @return bool
  122. */
  123. public function save($description)
  124. {
  125. $id = $description->id;
  126. if (empty($id)) {
  127. return $this->insert($description);
  128. } else {
  129. return $this->update($description);
  130. }
  131. }
  132. /**
  133. *
  134. * @param \CourseDescription\CourseDescription $description
  135. * @return bool
  136. */
  137. public function insert($description)
  138. {
  139. $c_id = (int) $description->c_id;
  140. $session_id = (int) $description->session_id;
  141. $session_id = $session_id ? $session_id : '0';
  142. $title = trim($description->title);
  143. $title = Database::escape_string($title);
  144. $content = trim($description->content);
  145. $content = Database::escape_string($content);
  146. $description_type = (int) $description->description_type;
  147. $progress = (int) $description->progress;
  148. $table = Database :: get_course_table(TABLE_COURSE_DESCRIPTION);
  149. $sql = "INSERT INTO $table
  150. (c_id, title, content, session_id, description_type, progress)
  151. VALUES
  152. ($c_id , '$title', '$content', $session_id, $description_type, $progress)";
  153. $result = (bool) Database :: query($sql);
  154. if ($result) {
  155. $id = Database::insert_id();
  156. $description->id = $id;
  157. $_course = api_get_course_info_by_id($c_id);
  158. $tool = TOOL_COURSE_DESCRIPTION;
  159. $user_id = api_get_user_id();
  160. api_item_property_update($_course, $tool, $id, 'CourseDescriptionAdded', $user_id);
  161. }
  162. return $result;
  163. }
  164. /**
  165. *
  166. * @param \CourseDescription\CourseDescription $description
  167. * @return bool
  168. */
  169. function update($description)
  170. {
  171. $c_id = (int) $description->c_id;
  172. $id = (int) $description->id;
  173. $session_id = (int) $description->session_id;
  174. $session_id = $session_id ? $session_id : '0';
  175. $title = trim($description->title);
  176. $title = Database::escape_string($title);
  177. $content = trim($description->content);
  178. $content = Database::escape_string($content);
  179. $description_type = (int) $description->description_type;
  180. $progress = (int) $description->progress;
  181. $table = Database :: get_course_table(TABLE_COURSE_DESCRIPTION);
  182. $sql = "UPDATE $table SET
  183. title = '$title',
  184. content = '$content',
  185. session_id = $session_id,
  186. description_type = $description_type,
  187. progress = $progress
  188. WHERE
  189. c_id = $c_id AND
  190. id = $id";
  191. $result = (bool) Database :: query($sql);
  192. if ($result) {
  193. $_course = api_get_course_info_by_id($c_id);
  194. $tool = TOOL_COURSE_DESCRIPTION;
  195. $user_id = api_get_user_id();
  196. api_item_property_update($_course, $tool, $id, 'CourseDescriptionUpdated', $user_id);
  197. }
  198. return $result;
  199. }
  200. /**
  201. *
  202. * @param object $description
  203. * @return boolean
  204. */
  205. public function remove($description)
  206. {
  207. $table = Database :: get_course_table(TABLE_COURSE_DESCRIPTION);
  208. $c_id = (int) $description->c_id;
  209. $id = (int) $description->id;
  210. if (empty($c_id) || empty($id)) {
  211. return false;
  212. }
  213. $sql = "DELETE FROM $table WHERE c_id=$c_id AND id=$id";
  214. $result = Database :: query($sql);
  215. if ($result) {
  216. $tool = TOOL_COURSE_DESCRIPTION;
  217. $tbl_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  218. $sql = "DELETE FROM $tbl_property WHERE c_id=$c_id AND ref=$id AND tool='$tool'";
  219. Database :: query($sql);
  220. }
  221. return (bool) $result;
  222. }
  223. /**
  224. *
  225. * @param object $course
  226. * @return int
  227. */
  228. public function remove_by_course($course)
  229. {
  230. $items = $this->find_by_course($course);
  231. foreach ($items as $item) {
  232. $success = $this->remove($item);
  233. if ($success) {
  234. $result++;
  235. }
  236. }
  237. return $result;
  238. }
  239. }