ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilMStListCourses.php
Go to the documentation of this file.
1 <?php
18 declare(strict_types=1);
19 
21 
24 use ilLPStatus;
27 
33 {
34  protected Container $dic;
35 
40  public function __construct(Container $dic)
41  {
42  $this->dic = $dic;
43  }
44 
45  final public function getData(array $arr_usr_ids = array(), array $options = array()): ListFetcherResult
46  {
47  $users_per_position = ilMyStaffAccess::getInstance()->getUsersForUserPerPosition($this->dic->user()->getId());
48 
49  if (empty($users_per_position)) {
50  return new ListFetcherResult([], 0);
51  }
52 
53  //Permission Filter
55 
56  // permission should not be changed here because learning progress only works in combination with course memberships
57  /*if (isset($options['filters']['lp_status']) && $options['filters']['lp_status'] >= 0) {
58  $operation_access = ilOrgUnitOperation::OP_READ_LEARNING_PROGRESS;
59  }*/
60  /*$tmp_table_user_matrix = ilMyStaffAccess::getInstance()->buildTempTableIlobjectsUserMatrixForUserOperationAndContext($this->dic->user()
61  ->getId(), $operation_access, ilMyStaffAccess::DEFAULT_CONTEXT, ilMyStaffAccess::TMP_DEFAULT_TABLE_NAME_PREFIX_IL_OBJ_USER_MATRIX);*/
62 
63  $_options = array(
64  'filters' => array(),
65  'sort' => array(),
66  'limit' => array(),
67  'count' => false,
68  );
69  $options = array_merge($_options, $options);
70 
71  $query = 'SELECT crs_ref.ref_id AS crs_ref_id, crs.title AS crs_title, reg_status, lp_status, usr_data.usr_id AS usr_id, usr_data.login AS usr_login, usr_data.lastname AS usr_lastname, usr_data.firstname AS usr_firstname, usr_data.email AS usr_email FROM (
72  SELECT reg.obj_id, reg.usr_id, ' . ilMStListCourse::MEMBERSHIP_STATUS_REGISTERED . ' AS reg_status, lp.status AS lp_status FROM obj_members
73  AS reg
74  LEFT JOIN ut_lp_marks AS lp on lp.obj_id = reg.obj_id AND lp.usr_id = reg.usr_id
75  WHERE ' . $this->dic->database()->in('reg.usr_id', $arr_usr_ids, false, 'integer') . ' AND (reg.admin > 0 OR reg.tutor > 0 OR reg.member > 0)
76  UNION
77  SELECT obj_id, usr_id, ' . ilMStListCourse::MEMBERSHIP_STATUS_WAITINGLIST . ' AS reg_status, 0 AS lp_status FROM crs_waiting_list AS waiting
78  WHERE ' . $this->dic->database()->in('waiting.usr_id', $arr_usr_ids, false, 'integer') . '
79  UNION
80  SELECT obj_id, usr_id, ' . ilMStListCourse::MEMBERSHIP_STATUS_REQUESTED . ' AS reg_status, 0 AS lp_status FROM il_subscribers AS requested
81  WHERE ' . $this->dic->database()->in('requested.usr_id', $arr_usr_ids, false, 'integer') . '
82  ) AS memb
83 
84  INNER JOIN object_data AS crs on crs.obj_id = memb.obj_id AND crs.type = ' . $this->dic->database()
85  ->quote(
87  'text'
88  ) . '
89  INNER JOIN object_reference AS crs_ref on crs_ref.obj_id = crs.obj_id AND crs_ref.deleted IS NULL
90  INNER JOIN usr_data on usr_data.usr_id = memb.usr_id';
91 
92  $arr_query = [];
93  foreach ($users_per_position as $position_id => $users) {
94  $obj_ids = ilMyStaffAccess::getInstance()->getIdsForUserAndOperation(
95  $this->dic->user()->getId(),
96  $operation_access
97  );
98  $arr_query[] = $query . " AND " . $this->dic->database()->in(
99  'crs.obj_id',
100  $obj_ids,
101  false,
102  'integer'
103  ) . " AND " . $this->dic->database()->in('usr_data.usr_id', $users, false, 'integer');
104  }
105 
106  $union_query = "SELECT * FROM ((" . implode(') UNION (', $arr_query) . ")) as a_table";
107 
108  $union_query .= static::createWhereStatement($options['filters']);
109 
110  $result = $this->dic->database()->query($union_query);
111  $numRows = $this->dic->database()->numRows($result);
112 
113  if ($options['sort']) {
114  $union_query .= " ORDER BY " . $options['sort']['field'] . " " . $options['sort']['direction'];
115  }
116 
117  if (isset($options['limit']['start']) && isset($options['limit']['end'])) {
118  $union_query .= " LIMIT " . $options['limit']['start'] . "," . $options['limit']['end'];
119  }
120  $result = $this->dic->database()->query($union_query);
121  $crs_data = array();
122 
123  while ($crs = $this->dic->database()->fetchAssoc($result)) {
124  $list_course = new ilMStListCourse();
125  $list_course->setCrsRefId(intval($crs['crs_ref_id']));
126  $list_course->setCrsTitle($crs['crs_title'] ?? "");
127  $list_course->setUsrRegStatus(intval($crs['reg_status']));
128  $list_course->setUsrLpStatus(intval($crs['lp_status']));
129  $list_course->setUsrLogin($crs['usr_login']);
130  $list_course->setUsrLastname($crs['usr_lastname']);
131  $list_course->setUsrFirstname($crs['usr_firstname']);
132  $list_course->setUsrEmail($crs['usr_email'] ?? "");
133  $list_course->setUsrId(intval($crs['usr_id']));
134 
135  $crs_data[] = $list_course;
136  }
137 
138  return new ListFetcherResult($crs_data, $numRows);
139  }
140 
144  protected function createWhereStatement(array $arr_filter): string
145  {
146  $where = array();
147 
148  if (!empty($arr_filter['crs_title'])) {
149  $where[] = '(crs_title LIKE ' . $this->dic->database()->quote(
150  '%' . $arr_filter['crs_title'] . '%',
151  'text'
152  ) . ')';
153  }
154 
155  if ($arr_filter['course'] > 0) {
156  $where[] = '(crs_ref_id = ' . $this->dic->database()->quote($arr_filter['course'], 'integer') . ')';
157  }
158 
159 
160  if (isset($arr_filter['lp_status']) && $arr_filter['lp_status'] >= 0) {
161  switch ($arr_filter['lp_status']) {
163  //if a user has the lp status not attempted it could be, that the user hase no records in table ut_lp_marks
164  $where[] = '(lp_status = ' . $this->dic->database()->quote(
165  $arr_filter['lp_status'],
166  'integer'
167  ) . ' OR lp_status is NULL)';
168  break;
169  default:
170  $where[] = '(lp_status = ' . $this->dic->database()->quote(
171  $arr_filter['lp_status'],
172  'integer'
173  ) . ')';
174  break;
175  }
176  }
177 
178  if (!empty($arr_filter['memb_status'])) {
179  $where[] = '(reg_status = ' . $this->dic->database()->quote($arr_filter['memb_status'], 'integer') . ')';
180  }
181 
182  if (!empty($arr_filter['user'])) {
183  $where[] = "(" . $this->dic->database()->like(
184  "usr_login",
185  "text",
186  "%" . $arr_filter['user'] . "%"
187  ) . " " . "OR " . $this->dic->database()
188  ->like(
189  "usr_firstname",
190  "text",
191  "%" . $arr_filter['user'] . "%"
192  ) . " " . "OR " . $this->dic->database()
193  ->like(
194  "usr_lastname",
195  "text",
196  "%" . $arr_filter['user'] . "%"
197  ) . " " . "OR " . $this->dic->database()
198  ->like(
199  "usr_email",
200  "text",
201  "%" . $arr_filter['user'] . "%"
202  ) . ") ";
203  }
204 
205  if (!empty($arr_filter['org_unit'])) {
206  $where[] = 'usr_id IN (SELECT user_id FROM il_orgu_ua WHERE orgu_id = ' . $this->dic->database()
207  ->quote(
208  $arr_filter['org_unit'],
209  'integer'
210  ) . ')';
211  }
212 
213  if (isset($arr_filter['usr_id']) && is_numeric($arr_filter['usr_id'])) {
214  $where[] = 'usr_id = ' . $this->dic->database()->quote($arr_filter['usr_id'], \ilDBConstants::T_INTEGER);
215  }
216 
217  if (!empty($where)) {
218  return ' WHERE ' . implode(' AND ', $where) . ' ';
219  } else {
220  return '';
221  }
222  }
223 }
__construct(Container $dic)
ilMStListCourses constructor.
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
getData(array $arr_usr_ids=array(), array $options=array())
const LP_STATUS_NOT_ATTEMPTED_NUM
createWhereStatement(array $arr_filter)
Returns the WHERE Part for the Queries using parameter $user_ids AND local variable $filters...