ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
class.ilMStListCourses.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use ilLPStatus;
28
34{
35 protected Container $dic;
36
41 public function __construct(Container $dic)
42 {
43 $this->dic = $dic;
44 }
45
46 final public function getData(array $arr_usr_ids = array(), array $options = array()): ListFetcherResult
47 {
48 $users_per_position = ilMyStaffAccess::getInstance()->getUsersForUserPerPosition($this->dic->user()->getId());
49
50 if (empty($users_per_position)) {
51 return new ListFetcherResult([], 0);
52 }
53
54 //Permission Filter
56
57 // permission should not be changed here because learning progress only works in combination with course memberships
58 /*if (isset($options['filters']['lp_status']) && $options['filters']['lp_status'] >= 0) {
59 $operation_access = ilOrgUnitOperation::OP_READ_LEARNING_PROGRESS;
60 }*/
61 /*$tmp_table_user_matrix = ilMyStaffAccess::getInstance()->buildTempTableIlobjectsUserMatrixForUserOperationAndContext($this->dic->user()
62 ->getId(), $operation_access, ilMyStaffAccess::DEFAULT_CONTEXT, ilMyStaffAccess::TMP_DEFAULT_TABLE_NAME_PREFIX_IL_OBJ_USER_MATRIX);*/
63
64 $_options = array(
65 'filters' => array(),
66 'sort' => array(),
67 'limit' => array(),
68 'count' => false,
69 );
70 $options = array_merge($_options, $options);
71
72 $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 (
73 SELECT reg.obj_id, reg.usr_id, ' . ilMStListCourse::MEMBERSHIP_STATUS_REGISTERED . ' AS reg_status, lp.status AS lp_status FROM obj_members
74 AS reg
75 LEFT JOIN ut_lp_marks AS lp on lp.obj_id = reg.obj_id AND lp.usr_id = reg.usr_id
76 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)
77 UNION
78 SELECT obj_id, usr_id, ' . ilMStListCourse::MEMBERSHIP_STATUS_WAITINGLIST . ' AS reg_status, 0 AS lp_status FROM crs_waiting_list AS waiting
79 WHERE ' . $this->dic->database()->in('waiting.usr_id', $arr_usr_ids, false, 'integer') . '
80 UNION
81 SELECT obj_id, usr_id, ' . ilMStListCourse::MEMBERSHIP_STATUS_REQUESTED . ' AS reg_status, 0 AS lp_status FROM il_subscribers AS requested
82 WHERE ' . $this->dic->database()->in('requested.usr_id', $arr_usr_ids, false, 'integer') . '
83 ) AS memb
84
85 INNER JOIN object_data AS crs on crs.obj_id = memb.obj_id AND crs.type = ' . $this->dic->database()
86 ->quote(
88 'text'
89 ) . '
90 INNER JOIN object_reference AS crs_ref on crs_ref.obj_id = crs.obj_id AND crs_ref.deleted IS NULL
91 LEFT JOIN orgu_obj_pos_settings AS oupos_set on oupos_set.obj_id = crs.obj_id
92 INNER JOIN orgu_obj_type_settings AS outype_set ON outype_set.obj_type = "crs"
93 INNER JOIN usr_data on usr_data.usr_id = memb.usr_id';
94
95 $arr_query = [];
96
97 // reflect course settings or org unit settings
98 $query .= " AND (oupos_set.active = 1 OR outype_set.activation_default = 1 OR outype_set.changeable = 0)";
99
100 foreach ($users_per_position as $position_id => $users) {
101 $obj_ids = ilMyStaffAccess::getInstance()->getIdsForUserAndOperation(
102 $this->dic->user()->getId(),
103 $operation_access
104 );
105 $arr_query[] = $query . " AND " . $this->dic->database()->in(
106 'crs.obj_id',
107 $obj_ids,
108 false,
109 'integer'
110 ) . " AND " . $this->dic->database()->in('usr_data.usr_id', $users, false, 'integer');
111 }
112
113 $union_query = "SELECT * FROM ((" . implode(') UNION (', $arr_query) . ")) as a_table";
114
115 $union_query .= static::createWhereStatement($options['filters']);
116
117 $result = $this->dic->database()->query($union_query);
118 $numRows = $this->dic->database()->numRows($result);
119
120 if ($options['sort']) {
121 /*
122 * Sort field is validated in ilMStListCoursesTableGUI::getSafeOrderField,
123 * the available columns depend on user administration settings.
124 */
125 $sort_field = (string) ($options['sort']['field'] ?? '');
126 $sort_direction = $this->getSafeSortDirection((string) ($options['sort']['direction'] ?? ''));
127 $union_query .= " ORDER BY " . $this->dic->database()->quoteIdentifier($sort_field) . " " . $sort_direction;
128 }
129
130 if (isset($options['limit']['start']) && isset($options['limit']['end'])) {
131 $union_query .= " LIMIT " . (int) $options['limit']['start'] . "," . (int) $options['limit']['end'];
132 }
133 $result = $this->dic->database()->query($union_query);
134 $crs_data = array();
135
136 while ($crs = $this->dic->database()->fetchAssoc($result)) {
137 $list_course = new ilMStListCourse();
138 $list_course->setCrsRefId(intval($crs['crs_ref_id']));
139 $list_course->setCrsTitle($crs['crs_title'] ?? "");
140 $list_course->setUsrRegStatus(intval($crs['reg_status']));
141 $list_course->setUsrLpStatus(intval($crs['lp_status']));
142 $list_course->setUsrLogin($crs['usr_login']);
143 $list_course->setUsrLastname($crs['usr_lastname']);
144 $list_course->setUsrFirstname($crs['usr_firstname']);
145 $list_course->setUsrEmail($crs['usr_email'] ?? "");
146 $list_course->setUsrId(intval($crs['usr_id']));
147
148 $crs_data[] = $list_course;
149 }
150
151 return new ListFetcherResult($crs_data, $numRows);
152 }
153
157 protected function createWhereStatement(array $arr_filter): string
158 {
159 $where = array();
160
161 if (!empty($arr_filter['crs_title'])) {
162 $where[] = '(crs_title LIKE ' . $this->dic->database()->quote(
163 '%' . $arr_filter['crs_title'] . '%',
164 'text'
165 ) . ')';
166 }
167
168 if ($arr_filter['course'] > 0) {
169 $where[] = '(crs_ref_id = ' . $this->dic->database()->quote($arr_filter['course'], 'integer') . ')';
170 }
171
172
173 if (isset($arr_filter['lp_status']) && $arr_filter['lp_status'] >= 0) {
174 switch ($arr_filter['lp_status']) {
176 //if a user has the lp status not attempted it could be, that the user hase no records in table ut_lp_marks
177 $where[] = '(lp_status = ' . $this->dic->database()->quote(
178 $arr_filter['lp_status'],
179 'integer'
180 ) . ' OR lp_status is NULL)';
181 break;
182 default:
183 $where[] = '(lp_status = ' . $this->dic->database()->quote(
184 $arr_filter['lp_status'],
185 'integer'
186 ) . ')';
187 break;
188 }
189 }
190
191 if (!empty($arr_filter['memb_status'])) {
192 $where[] = '(reg_status = ' . $this->dic->database()->quote($arr_filter['memb_status'], 'integer') . ')';
193 }
194
195 if (!empty($arr_filter['user'])) {
196 $where[] = "(" . $this->dic->database()->like(
197 "usr_login",
198 "text",
199 "%" . $arr_filter['user'] . "%"
200 ) . " " . "OR " . $this->dic->database()
201 ->like(
202 "usr_firstname",
203 "text",
204 "%" . $arr_filter['user'] . "%"
205 ) . " " . "OR " . $this->dic->database()
206 ->like(
207 "usr_lastname",
208 "text",
209 "%" . $arr_filter['user'] . "%"
210 ) . " " . "OR " . $this->dic->database()
211 ->like(
212 "usr_email",
213 "text",
214 "%" . $arr_filter['user'] . "%"
215 ) . ") ";
216 }
217
218 if (!empty($arr_filter['org_unit'])) {
219 $where[] = 'usr_id IN (SELECT user_id FROM il_orgu_ua WHERE orgu_id = ' . $this->dic->database()
220 ->quote(
221 $arr_filter['org_unit'],
222 'integer'
223 ) . ')';
224 }
225
226 if (isset($arr_filter['usr_id']) && is_numeric($arr_filter['usr_id'])) {
227 $where[] = 'usr_id = ' . $this->dic->database()->quote($arr_filter['usr_id'], \ilDBConstants::T_INTEGER);
228 }
229
230 if (!empty($where)) {
231 return ' WHERE ' . implode(' AND ', $where) . ' ';
232 } else {
233 return '';
234 }
235 }
236
237 private function getSafeSortDirection(string $sort_direction): string
238 {
239 return strtolower($sort_direction) === 'asc'
240 ? 'asc'
241 : 'desc';
242 }
243}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
getData(array $arr_usr_ids=array(), array $options=array())
createWhereStatement(array $arr_filter)
Returns the WHERE Part for the Queries using parameter $user_ids AND local variable $filters.
__construct(Container $dic)
ilMStListCourses constructor.
Abstract class ilLPStatus for all learning progress modes E.g ilLPStatusManual, ilLPStatusObjectives ...
const LP_STATUS_NOT_ATTEMPTED_NUM
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...