Proyectos de Subversion LeadersLinked - Services

Rev

Rev 302 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
283 www 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Mapper;
5
 
6
use LeadersLinked\Mapper\Common\MapperCommon;
7
use Laminas\Db\Adapter\AdapterInterface;
8
use LeadersLinked\Model\MicrolearningCapsuleComment;
9
use Laminas\Db\Sql\Expression;
10
use Laminas\Paginator\Paginator;
11
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
12
use Laminas\Db\ResultSet\HydratingResultSet;
13
use Laminas\Paginator\Adapter\DbSelect;
14
 
15
 
16
 
17
class MicrolearningCapsuleCommentMapper extends MapperCommon
18
{
19
    const _TABLE = 'tbl_microlearning_capsule_comments';
20
 
21
    /**
22
     *
23
     * @var MicrolearningCapsuleCommentMapper
24
     */
25
    private static $_instance;
26
 
27
    /**
28
     *
29
     * @param AdapterInterface $adapter
30
     */
31
    private function __construct($adapter)
32
    {
33
        parent::__construct($adapter);
34
    }
35
 
36
    /**
37
     *
38
     * @param AdapterInterface $adapter
39
     * @return MicrolearningCapsuleCommentMapper
40
     */
41
    public static function getInstance($adapter)
42
    {
43
        if(self::$_instance == null) {
44
            self::$_instance = new MicrolearningCapsuleCommentMapper($adapter);
45
        }
46
        return self::$_instance;
47
    }
48
 
49
    /**
50
     *
51
     * @param int $company_id
52
     * @param int $topic_id
53
     * @param int $capsule_id
54
     * return int
55
     */
302 www 56
    public function fetchCountByCompanyIdAndCapsuleId($company_id, $capsule_id)
283 www 57
    {
58
 
59
        $select = $this->sql->select();
60
        $select->columns(['total' => new Expression('COUNT(*)')]);
61
        $select->from(self::_TABLE);
62
        $select->where->equalTo('company_id', $company_id);
63
        $select->where->equalTo('capsule_id', $capsule_id);
64
 
65
        $record = $this->executeFetchOneArray($select);
66
        return $record['total'];
67
    }
68
 
69
 
70
 
71
   /**
72
    *
73
 
74
    * @param int $capsule_id
75
    * @param int $user_id
76
    * return MicrolearningCapsuleComment[]
77
    */
78
   public function fetchAllByCapsuleIdAndUserId($capsule_id, $user_id)
79
   {
80
       $prototype = new MicrolearningCapsuleComment();
81
 
82
       $select = $this->sql->select();
83
       $select->from(self::_TABLE);
84
       $select->where->equalTo('capsule_id', $capsule_id);
85
       $select->where->equalTo('user_id', $user_id);
86
       $select->order('added_on DESC');
87
 
88
       return $this->executeFetchAllObject($select, $prototype);
89
   }
90
 
91
   /*
92
   * @param int $capsule_id
93
   * return MicrolearningCapsuleComment[]
94
   */
95
   public function fetchAllByCapsuleId($capsule_id)
96
   {
97
       $prototype = new MicrolearningCapsuleComment();
98
 
99
       $select = $this->sql->select();
100
       $select->from(self::_TABLE);
101
       $select->where->equalTo('capsule_id', $capsule_id);
102
       $select->order('added_on DESC');
103
 
104
       return $this->executeFetchAllObject($select, $prototype);
105
   }
106
 
107
 
108
   /**
109
    *
110
    * @param int $company_id
111
    * @param int $capsule_id
112
    * @param string $search
113
    * @param int $page
114
    * @param int $records_per_page
115
    * @param string $order_field
116
    * @param string $order_direction
117
    * @return Paginator
118
    */
302 www 119
   public function fetchAllDataTableByCompanyIdAndCapsuleId($company_id, $capsule_id, $search, $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
283 www 120
   {
121
       $prototype = new MicrolearningCapsuleComment();
122
       $select = $this->sql->select(self::_TABLE);
123
       $select->where->equalTo('company_id', $company_id);
124
       $select->where->equalTo('capsule_id', $capsule_id);
125
 
126
       if($search) {
127
           $select->where->like('name', '%' . $search . '%');
128
       }
129
       $select->order($order_field . ' ' . $order_direction);
130
 
131
       //echo $select->getSqlString($this->adapter->platform); exit;
132
 
133
       $hydrator   = new ObjectPropertyHydrator();
134
       $resultset  = new HydratingResultSet($hydrator, $prototype);
135
 
136
       $adapter = new DbSelect($select, $this->sql, $resultset);
137
       $paginator = new Paginator($adapter);
138
       $paginator->setItemCountPerPage($records_per_page);
139
       $paginator->setCurrentPageNumber($page);
140
 
141
 
142
       return $paginator;
143
   }
144
 
145
   /**
146
    *
147
    * @param int $company_id
148
    * @param int $capsule_id
149
    * @param int $user_id
150
    * @return void|MicrolearningCapsuleComment
151
    */
302 www 152
   public function fetchOneByCompanyIdAndCapsuleIdAndUserId($company_id, $capsule_id, $user_id)
283 www 153
   {
154
       $select = $this->sql->select();
155
       $select->from(self::_TABLE);
156
       $select->where->equalTo('company_id', $company_id);
157
       $select->where->equalTo('capsule_id', $capsule_id);
158
       $select->where->equalTo('user_id', $user_id);
159
 
160
       $prototype = new MicrolearningCapsuleComment();
161
       return $this->executeFetchOneObject($select, $prototype);
162
   }
163
 
164
   /**
165
    *
166
    * @param int $company_id
167
    * @param int $topic_id
168
    * @param int $capsule_id
169
    * @return number[]
170
    */
302 www 171
   public function fetchCountAndRatingAverage($company_id, $capsule_id)
283 www 172
   {
173
       $select = $this->sql->select();
174
       $select->columns([
175
           'total_comments' => new Expression('COUNT(*)'),
176
           'total_rating'   => new Expression('COALESCE(AVG(rating), 0)')
177
       ]);
178
 
179
       $select->from(self::_TABLE);
180
       $select->where->equalTo('company_id', $company_id);
181
       $select->where->equalTo('capsule_id', $capsule_id);
182
 
183
       $record = $this->executeFetchOneArray($select);
184
 
185
       return  [
186
           'total_comments' => $record['total_comments'],
187
           'total_rating'   => $record['total_rating'],
188
       ];
189
 
190
   }
191
 
192
 
193
    /**
194
     *
195
     * @param int $id
196
     * return MicrolearningCapsuleComment
197
     */
198
    public function fetchOne($id)
199
    {
200
        $prototype = new MicrolearningCapsuleComment();
201
        $select = $this->sql->select();
202
        $select->from(self::_TABLE);
203
        $select->where->equalTo('id', $id);
204
 
205
        return $this->executeFetchOneObject($select, $prototype);
206
    }
207
 
208
 
209
 
210
    /**
211
     *
212
     * @param string $uuid
213
     * return MicrolearningCapsuleComment
214
     */
215
    public function fetchOneByUuid($uuid)
216
    {
217
        $prototype = new MicrolearningCapsuleComment();
218
        $select = $this->sql->select();
219
        $select->from(self::_TABLE);
220
        $select->where->equalTo('uuid', $uuid);
221
 
222
        return $this->executeFetchOneObject($select, $prototype);
223
    }
224
 
225
    /**
226
     *
227
     * @param MicrolearningCapsuleComment $microlearningCapsuleComment
228
     * @return boolean
229
     */
230
    public function insert($microlearningCapsuleComment)
231
    {
232
        $hydrator = new ObjectPropertyHydrator();
233
        $values = $hydrator->extract($microlearningCapsuleComment);
234
        $values = $this->removeEmpty($values);
235
 
236
        $insert = $this->sql->insert(self::_TABLE);
237
        $insert->values($values);
238
 
239
        $response = $this->executeInsert($insert);
240
        if($response) {
241
            $microlearningCapsuleComment->id = $this->lastInsertId;
242
        }
243
 
244
        return $response;
245
    }
246
 
247
 
248
 
249
    /**
250
     *
251
     * @param int $id
252
     * @return boolean
253
     */
254
    public function delete($id)
255
    {
256
        $delete = $this->sql->delete(self::_TABLE);
257
        $delete->where->equalTo('id', $id);
258
 
259
        return $this->executeDelete($delete);
260
    }
261
 
632 stevensc 262
    /**
263
     * Deletes all comments by capsule id
264
     * @param int $capsule_id
265
     * @return int
266
     */
267
    public function deleteAllByCapsuleId($capsule_id)
268
    {
269
        $delete = $this->sql->delete(self::_TABLE);
270
        $delete->where->equalTo('capsule_id', $capsule_id);
271
        return $this->executeDelete($delete);
272
    }
273
 
283 www 274
}