course_description_repository.class.php 7.5 KB

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