ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCourseUserData.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=0);
4 
26 {
27  private int $user_id;
28  private int $field_id;
29  private string $value;
30 
31  protected ilDBInterface $db;
32 
33  public function __construct(int $a_user_id, int $a_field_id = 0)
34  {
35  global $DIC;
36 
37  $this->db = $DIC->database();
38  $this->user_id = $a_user_id;
39  $this->field_id = $a_field_id;
40  if ($this->field_id) {
41  $this->read();
42  }
43  }
44 
45  public static function _getValuesByObjId(int $a_obj_id): array
46  {
47  global $DIC;
48 
49  $ilDB = $DIC->database();
50  $field_ids = ilCourseDefinedFieldDefinition::_getFieldIds($a_obj_id);
51  if ($field_ids === []) {
52  return array();
53  }
54  $where = "WHERE " . $ilDB->in('field_id', $field_ids, false, 'integer');
55  $query = "SELECT * FROM crs_user_data " .
56  $where;
57 
58  $res = $ilDB->query($query);
59  $user_data = [];
60  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
61  $user_data[(int) $row->usr_id][(int) $row->field_id] = $row->value;
62  }
63  return $user_data;
64  }
65 
66  public static function _checkRequired(int $a_usr_id, int $a_obj_id): bool
67  {
68  global $DIC;
69 
70  $ilDB = $DIC->database();
72  if ($required === []) {
73  return true;
74  }
75 
76  //$and = ("AND field_id IN (".implode(",",ilUtil::quoteArray($required)).")");
77  $and = "AND " . $ilDB->in('field_id', $required, false, 'integer');
78 
79  $query = "SELECT COUNT(*) num_entries FROM crs_user_data " .
80  "WHERE usr_id = " . $ilDB->quote($a_usr_id, 'integer') . " " .
81  "AND value != '' AND value IS NOT NULL " .
82  $and . " " .
83  " ";
84  $res = $ilDB->query($query);
85  $row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT);
86  return $row->num_entries == count($required);
87  }
88 
89  public static function _deleteByUser(int $a_user_id): void
90  {
91  global $DIC;
92 
93  $ilDB = $DIC->database();
94  $query = "DELETE FROM crs_user_data " .
95  "WHERE usr_id = " . $ilDB->quote($a_user_id, 'integer');
96  $res = $ilDB->manipulate($query);
97  }
98 
99  public static function _deleteByField(int $a_field_id): void
100  {
101  global $DIC;
102 
103  $ilDB = $DIC['ilDB'];
104 
105  $query = "DELETE FROM crs_user_data " .
106  "WHERE field_id = " . $ilDB->quote($a_field_id, 'integer');
107  $res = $ilDB->manipulate($query);
108  }
109 
110  public function setValue(string $a_value): void
111  {
112  $this->value = $a_value;
113  }
114 
115  public function getValue(): string
116  {
117  return $this->value;
118  }
119 
120  public function update(): void
121  {
122  $this->delete();
123  $this->create();
124  }
125 
126  public function delete(): void
127  {
128  $query = "DELETE FROM crs_user_data " .
129  "WHERE usr_id = " . $this->db->quote($this->user_id, 'integer') . " " .
130  "AND field_id = " . $this->db->quote($this->field_id, 'integer');
131  $res = $this->db->manipulate($query);
132  }
133 
134  public function create(): void
135  {
136  $query = "INSERT INTO crs_user_data (value,usr_id,field_id) " .
137  "VALUES( " .
138  $this->db->quote($this->getValue(), 'text') . ", " .
139  $this->db->quote($this->user_id, 'integer') . ", " .
140  $this->db->quote($this->field_id, 'integer') . " " .
141  ")";
142 
143  $res = $this->db->manipulate($query);
144  }
145 
146  private function read(): void
147  {
148  $query = "SELECT * FROM crs_user_data " .
149  "WHERE usr_id = " . $this->db->quote($this->user_id, 'integer') . " " .
150  "AND field_id = " . $this->db->quote($this->field_id, 'integer');
151  $res = $this->db->query($query);
152  $this->setValue('');
153  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
154  $this->setValue((string) $row->value);
155  }
156  }
157 }
$res
Definition: ltiservices.php:69
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setValue(string $a_value)
static _getFieldIds(int $a_container_id, string $a_sort=self::IL_CDF_SORT_ID)
static _getValuesByObjId(int $a_obj_id)
__construct(int $a_user_id, int $a_field_id=0)
global $DIC
Definition: feed.php:28
static _checkRequired(int $a_usr_id, int $a_obj_id)
$query
static _deleteByField(int $a_field_id)
static _deleteByUser(int $a_user_id)
static _getRequiredFieldIds(int $a_obj_id)
Get required filed id&#39;s.