forked from kodeplay/kodelearn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.php
192 lines (159 loc) · 5.39 KB
/
feed.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php defined('SYSPATH') or die('No direct script access.');
abstract class Feed {
/**
* The feed model object
*/
protected $_feed;
protected $id;
protected $type;
protected $action;
protected $respective_id;
protected $course_id = 0;
protected $actor_id;
protected $time;
/**
* Array of Feed stream object
* Depending upon whether a stream exists, a new streams will be created
* to which this feed will be associated
* All users in these streams will then see this feed
* There can be multiple feed streams associated with a feed
*/
protected $_streams = array();
public function get_time(){
return $this->time;
}
public function set_time($value){
$this->time = $value;
}
public function get_actor_id(){
return $this->actor_id;
}
public function set_actor_id($value){
$this->actor_id = $value;
}
public function get_course_id(){
return $this->course_id;
}
public function set_course_id($value){
$this->course_id = $value;
}
public function get_respective_id(){
return $this->respective_id;
}
public function set_respective_id($value){
$this->respective_id = $value;
}
public function get_action(){
return $this->action;
}
public function set_action($value){
$this->action = $value;
}
public function get_type(){
return $this->type;
}
public function set_type($value){
$this->type = $value;
}
public function get_id(){
return $this->id;
}
public function set_id($value){
$this->id = $value;
}
public static function factory ($type, $id = NULL) {
$file = MODPATH . $type . '/classes/feed/' . $type . '.php';
$file_feed = MODPATH . 'feed/classes/feed/' . $type . '.php';
if(file_exists($file) || file_exists($file_feed)){
$class = 'Feed_' . $type;
return new $class($id);
} else {
throw new Exception('Class Feed_ ' . $type . ' not found');
}
}
/**
* If an id is passed while instantiating this class,
* load the feed corresponding to this id and populate the variables
* otherwise just create a new instance of the Feed model and keep
* it assigned to the $_feed instance variable
*/
public function __construct($id = null) {
if($id !== null){
$this->_feed = $this->load($id);
} else {
$this->_feed = ORM::factory('feed');
}
}
public function load($id) {
$feed = ORM::factory('feed', $id);
$this->_feed = $feed;
$this->id = $id;
$this->type = $feed->type;
$this->action = $feed->action;
$this->respective_id = $feed->respective_id;
$this->actor_id = $feed->actor_id;
$this->course_id = $feed->course_id;
$this->time = $feed->time;
$this->_streams = $feed->feedstreams->find_all()->as_array();
return $this->_feed;
}
public function render(){
return $this->type . ' ' . $this->action;
}
public function save() {
$this->_feed->type = $this->type;
$this->_feed->action = $this->action;
$this->_feed->respective_id = $this->respective_id;
$this->_feed->actor_id = $this->actor_id;
$this->_feed->course_id = $this->course_id;
$this->_feed->time = time();
$this->_feed->save();
$this->update_streams();
$this->load($this->_feed->id);
}
/**
* Method to save the streams to be associated to the feed
* @param array $data
*/
public function streams($data) {
if (!empty($data['user_id']) && is_array($data['user_id'])) { // multiple user ids passed
$users = $data['user_id'];
foreach ($users as $user_id) {
$this->_streams[] = Model_Feedstream::create_if_not_exists(array('user_id' => $user_id));
}
}
elseif (!empty($data['role_id']) && is_array($data['role_id'])) { // if multiple role_ids passed
$roles = $data['role_id'];
foreach ($roles as $role_id) {
$this->_streams[] = Model_Feedstream::create_if_not_exists(array_merge($data, array(
'role_id' => $role_id
)));
}
} else { // create only one stream with whatever data passed including user_id and role_id
$this->_streams[] = Model_Feedstream::create_if_not_exists($data);
}
return $this;
}
/**
* Update the feed streams-feed association by adding the feedstream objects to feed
*/
public function update_streams() {
foreach ($this->_streams as $stream) {
if ($this->_feed->has('feedstreams', $stream)) {
continue;
}
$this->_feed->add('feedstreams', $stream);
}
}
public function check_deleted($object){
if($object->id === NULL){
$this->delete();
return true;
} else {
return false;
}
}
public function delete(){
ORM::factory('feed', $this->id)->delete();
}
}