course_notice_query.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * Returns tool notifications for a specific user. I.e. course activity for courses to
  4. * which the user is registered.
  5. *
  6. * @license see /license.txt
  7. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  8. */
  9. class CourseNoticeQuery implements IteratorAggregate
  10. {
  11. /**
  12. *
  13. * @param int $user_id
  14. * @param int $limit
  15. * @return CourseNotices
  16. */
  17. public static function create($user_id = null, $limit = 20)
  18. {
  19. return new self($user_id, $limit);
  20. }
  21. protected $user_id;
  22. protected $limit;
  23. function __construct($user_id = null, $limit = 20)
  24. {
  25. if (empty($user_id))
  26. {
  27. global $_user;
  28. $user_id = $_user['user_id'];
  29. }
  30. $this->user_id = (int) $user_id;
  31. $this->limit = $limit;
  32. }
  33. public function get_limit()
  34. {
  35. return $this->limit;
  36. }
  37. public function get_user_id()
  38. {
  39. return $this->user_id;
  40. }
  41. function get_tools()
  42. {
  43. return array(
  44. array('name' => TOOL_ANNOUNCEMENT, 'table' => TABLE_ANNOUNCEMENT, 'filter' => ''),
  45. array('name' => TOOL_DOCUMENT, 'table' => TABLE_DOCUMENT, 'filter' => "tool.filetype = 'file'"),
  46. array('name' => TOOL_CALENDAR_EVENT, 'table' => TABLE_AGENDA, 'filter' => ''),
  47. array('name' => TOOL_LINK, 'table' => TABLE_LINK, 'filter' => '')
  48. );
  49. }
  50. public function getIterator()
  51. {
  52. return new ArrayIterator($this->get_items());
  53. }
  54. private $_get_user_courses = null;
  55. function get_user_courses()
  56. {
  57. if (!is_null($this->_get_user_courses))
  58. {
  59. return $this->_get_user_courses;
  60. }
  61. $user_id = $this->user_id;
  62. return $this->_get_user_courses = CourseManager::get_courses_list_by_user_id($user_id);
  63. }
  64. function get_user_groups()
  65. {
  66. $result = array();
  67. $user_id = $this->user_id;
  68. $tbl_group = Database::get_course_table(TABLE_GROUP_USER);
  69. $tbl_group_tutor = Database::get_course_table(TABLE_GROUP_TUTOR);
  70. $sql = "(SELECT c_id, group_id FROM $tbl_group WHERE user_id = $user_id)
  71. UNION DISTINCT
  72. (SELECT c_id, group_id FROM $tbl_group_tutor WHERE user_id = $user_id)";
  73. $rs = Database::query($sql);
  74. while ($row = Database::fetch_array($rs))
  75. {
  76. $result[] = $row;
  77. }
  78. return $result;
  79. }
  80. function get_items()
  81. {
  82. $result = array();
  83. $tools = $this->get_tools();
  84. foreach ($tools as $tool)
  85. {
  86. $tool_name = $tool['name'];
  87. $tool_table = $tool['table'];
  88. $tool_filter = $tool['filter'];
  89. $items = $this->get_tool_items($tool_name, $tool_table, $tool_filter);
  90. $result = array_merge($result, $items);
  91. }
  92. usort($result, array($this, 'sort_item'));
  93. return $result;
  94. }
  95. protected function sort_item($left, $right)
  96. {
  97. if ($left->lastedit_date == $right->lastedit_date)
  98. {
  99. return 0;
  100. }
  101. return ($left->lastedit_date <= $right->lastedit_date) ? 1 : -1;
  102. }
  103. function get_tool_items($tool_name, $tool_table, $tool_filter = '')
  104. {
  105. $item_property_table = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  106. $course_description = Database :: get_course_table(TABLE_COURSE_DESCRIPTION);
  107. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  108. $tool_table = Database :: get_course_table($tool_table);
  109. $user_id = $this->user_id;
  110. //courses
  111. $course_ids = array();
  112. $user_courses = $this->get_user_courses();
  113. foreach ($user_courses as $course)
  114. {
  115. $course_ids[] = $course['real_id'];
  116. }
  117. $course_ids = implode(',', $course_ids);
  118. //groups
  119. $group_filter = array();
  120. $user_groups = $this->get_user_groups();
  121. foreach ($user_groups as $group)
  122. {
  123. $group_id = $group['group_id'];
  124. $course_id = $group['c_id'];
  125. $group_filter[] = "(prop.to_group_id = $group_id AND prop.c_id = $course_id)";
  126. }
  127. $group_filter = implode(' OR ', $user_groups);
  128. $group_filter = $group_filter ? ' OR ' . $group_filter : '';
  129. //AND prop.lastedit_date > '" . $access_date . "'
  130. //doc.filetype = 'file'AND
  131. //$access_date = $this->get_last_access_date($course->code, TOOL_DOCUMENT);
  132. $sql = "SELECT tool.*,
  133. prop.tool, prop.insert_user_id, prop.insert_date, prop.lastedit_date,
  134. prop.ref, prop.lastedit_type, prop.lastedit_user_id, prop.to_group_id,
  135. prop.to_user_id, prop.visibility, prop.start_visible, prop.end_visible, prop.id_session,
  136. course.code, course.title AS course_title, des.content AS course_description
  137. FROM $item_property_table prop, $tool_table tool, $course_table course, $course_description des
  138. WHERE (
  139. course.id = prop.c_id AND
  140. des.c_id = course.id AND
  141. des.id = 1 AND
  142. prop.tool = '$tool_name' AND
  143. tool.id = prop.ref AND
  144. tool.c_id = prop.c_id AND
  145. prop.c_id IN ($course_ids) AND
  146. prop.visibility != 2 AND
  147. ((prop.to_user_id IS NULL AND prop.to_group_id = 0) OR (prop.to_user_id = $user_id) $group_filter)
  148. )";
  149. $sql = $tool_filter ? "$sql AND ($tool_filter)" : $sql;
  150. $sql .= 'ORDER BY lastedit_date DESC';
  151. $sql .= ' LIMIT ' . $this->limit;
  152. $rs = Database::query($sql);
  153. $result = array();
  154. while ($data = Database::fetch_array($rs, 'ASSOC'))
  155. {
  156. $result[] = $this->format_item($data);
  157. }
  158. return $result;
  159. }
  160. protected function format($items)
  161. {
  162. $result = array();
  163. foreach ($items as $item)
  164. {
  165. $result[] = $this->format_item($item);
  166. }
  167. return $result;
  168. }
  169. protected function format_item($item)
  170. {
  171. $result = (object) $item;
  172. $item = (object) $item;
  173. if (!isset($result->title))
  174. {
  175. if (isset($item->name))
  176. {
  177. $result->title = $item->name;
  178. }
  179. }
  180. if (!isset($result->description))
  181. {
  182. if (isset($item->content))
  183. {
  184. $result->description = $item->content;
  185. }
  186. else if (isset($item->comment))
  187. {
  188. $result->description = $item->comment;
  189. }
  190. }
  191. $result->course_code = $item->code;
  192. $result->course_id = $item->c_id;
  193. return $result;
  194. }
  195. }