-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan.php
334 lines (268 loc) · 9.65 KB
/
plan.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
*
* Plan model
*
* @author Lodoss Team
*
*/
class Plan extends Elegant {
public static $timestamps = true;
public function targets()
{
return $this->has_many('Target');
}
protected $aValidationRules = array(
'title' => 'required',
'user_id' => 'required|integer',
);
protected $aInvalidMessages = array(
'title_required' => "Field 'Plan Title' is required.",
'user_id_required' => "Field 'user_id' is required.",
'user_id_integer' => "Field 'user_id' may only contain numbers.",
);
public static function get_allPlans($iLimit = '', $iOffset = 0)
{
$aResult['error'] = '';
$aResult['error_description'] = '';
$aCurrentUser = Session::get( 'userdata.user' );
if($iLimit != 'all' && $iLimit != '') {
$aPlans = DB::table('plans')
->left_join('plan_shares', 'plans.id', '=', 'plan_shares.plan_id')
->left_join('plan_shares as ps', 'ps.plan_id', '=', 'plan_shares.plan_id')
->where('plan_shares.user_id', '=', $aCurrentUser['id'])
->group_by('ps.plan_id')
->take($iLimit)
->skip($iOffset)
->get(array('plans.*', DB::Raw('group_concat(ps.user_id) as users')));
} else {
$aPlans = DB::table('plans')
->left_join('plan_shares', 'plans.id', '=', 'plan_shares.plan_id')
->left_join('plan_shares as ps', 'ps.plan_id', '=', 'plan_shares.plan_id')
// check this condition
->where('plan_shares.user_id', '=', $aCurrentUser['id'])
->group_by('ps.plan_id')
->get(array('plans.*', DB::Raw('group_concat(ps.user_id) as users')));
}
//set sharedWithMe property
foreach ($aPlans as $key => $value) {
$value->sharedWithMe = 0;
if($value->user_id != $aCurrentUser['id']) {
$value->sharedWithMe = 1;
}
}
$aResult['plans'] = $aPlans;
return $aResult;
}
public static function get_byId($id = 0)
{
$aResult['error'] = '';
$aResult['error_description'] = '';
$aResult['plan'] = Plan::find($id)->original;
return $aResult;
}
public static function get_byUserId($iUserId = 0)
{
$aResult['error'] = '';
$aResult['error_description'] = '';
$aResult['plans'] = Plan::where('user_id', '=', $iUserId)->get();
return $aResult;
}
public static function get_countSimilarName()
{
$aResult['error'] = '';
$aResult['error_description'] = '';
$session = Session::get('userdata');
$aResult['count'] = DB::table('plans')->where('title', 'LIKE', Input::get('planname').'%')->where('user_id', '=', $session['user']['id'])->count();
return $aResult;
}
public static function copy_byPlanId($iPlanId = 0) {
$aResult['error'] = '';
$aResult['error_description'] = '';
$aPlanTargets = array();
$aPlanTasts = array();
//get plan related objects
$oPlan = Plan::find($iPlanId);
$aTempTargets = Plan_Target::get_byPlanId($oPlan->id);
$aTempWeeks = Plan_Week::get_byPlanId( $iPlanId );
if($aTempTargets['error'] == '') {
$aPlanTargets = $aTempTargets['targets'];
}
foreach ($aPlanTargets as $key => $oTarget) {
$aTempTasks = Plan_Target_Task::get_byTargetId($oTarget->id);
if($aTempTasks['error'] == '') {
$aPlanTasts[$oTarget->id] = $aTempTasks['tasks'];
}
}
/*save new objects*/
$oPlanNew = new Plan;
$oPlanNew->fill($oPlan->attributes);
$oPlanNew->id = 0;
$input = Input::get();
if(!empty($input['planname'])) {
$oPlanNew->title = $input['planname'];
}
$oPlanNew->user_id = Session::get( 'userdata.user.id' );
$oPlanNew->author = Session::get( 'userdata.user.fullname' );
$oPlanNew->save();
$lastId = DB::Query('SELECT LAST_INSERT_ID() as id');
$oPlanNew->id = $lastId[0]->id;
//create plan owner permission
$oPlanShare = new Plan_Share;
$oPlanShare->user_id = Session::get( 'userdata.user.id' );
$oPlanShare->plan_id = $oPlanNew->id;
$oPlanShare->permission = 3;
$oPlanShare->email = Session::get( 'userdata.user.email' );
$oPlanShare->save();
//copy weekly summative assessment (table - plan_weeks)
if( $aTempWeeks['error'] == '' AND $aTempWeeks['weeks'] ) {
foreach( $aTempWeeks['weeks'] as $oTempWeek ) {
$oPlanWeeks = new Plan_Week;
$oPlanWeeks->fill( (array)$oTempWeek );
$oPlanWeeks->id = 0;
$oPlanWeeks->plan_id = $oPlanNew->id;
$oPlanWeeks->save();
}
}
foreach ($aPlanTargets as $oTarget) {
$aPlanTastsOld = $aPlanTasts[$oTarget->id];
$oTargetNew = new Plan_Target;
$oTargetNew->fill((array)$oTarget);
$oTargetNew->id = 0;
$oTargetNew->plan_id = $oPlanNew->id;
$oTargetNew->save();
$lastId = DB::Query('SELECT LAST_INSERT_ID() as id');
$oTargetNew->id = $lastId[0]->id;
foreach ($aPlanTastsOld as $oTask) {
$oTaskNew = new Plan_Target_Task;
$oTaskNew->fill((array)$oTask);
$oTaskNew->id = 0;
$oTaskNew->target_id = $oTargetNew->id;
$oTaskNew->save();
}
}
return $aResult;
}
public static function createPlan()
{
$aResult['error'] = '';
$aResult['error_description'] = '';
$aSrcPlan = (array)Input::json()->plan;
if(isset($aSrcPlan['sharedWithMe'])) {
unset($aSrcPlan['sharedWithMe']);
}
$aFilteredPlan = $aSrcPlan;
$oPlan = new Plan;
$oPlan->fill( $aFilteredPlan );
$oPlan->save();
$oPlan = Plan::find($oPlan->id);
$aResult['plan'] = $oPlan->to_array();
//create plan owner permission
$oPlanShare = new Plan_Share;
$oPlanShare->user_id = $oPlan->user_id;
$oPlanShare->plan_id = $oPlan->id;
$oPlanShare->permission = 3;
$oPlanShare->email = Session::get( 'userdata.user.email' );
$oPlanShare->save();
for($week = 1; $week < 5; $week++) {
$oPlanWeek = new Plan_Week;
$oPlanWeek->plan_id = $oPlan->id;
$oPlanWeek->week = $week;
$oPlanWeek->save();
}
return $aResult;
}
public static function update_byId($id = 0)
{
$aResult['error'] = '';
$aResult['error_description'] = '';
if($id) {
$aSrcPlan = (array)Input::json()->plan;
//remove flag
if(isset($aSrcPlan['sharedWithMe'])) {
unset($aSrcPlan['sharedWithMe']);
}
if( isset( $aSrcPlan['permission'] ) ) unset( $aSrcPlan['permission'] );
$aFilteredPlan = $aSrcPlan;
$oPlan = Plan::find( $id );
if($oPlan) {
$oPlan->fill( $aFilteredPlan );
if($oPlan->validate( $aFilteredPlan ) ) {
$oPlan->save();
} else {
$aResult['error'] = 'plan_create';
$aResult['error_description'] = (array)$oPlan->errors();
}
} else {
$aResult['error'] = 'plan_update';
$aResult['error_description'] = 'plan do not exists';
}
} else {
$aResult['error'] = 'plan_update';
$aResult['error_description'] = 'please provide plan id';
}
return $aResult;
}
public static function delete_byId($id = 0)
{
$aResult['error'] = '';
$aResult['error_description'] = '';
if($id) {
$oPlan = Plan::find($id);
if($oPlan) {
if(Session::get( 'userdata.user.id' ) == $oPlan->user_id) {
$oPlan->delete();
} else {
$aResult['error'] = 'plan_delete';
$aResult['error_description'] = 'You don’t have permission to do this.';
}
} else {
$aResult['error'] = 'plan_delete';
$aResult['error_description'] = 'plan do not exists';
}
} else {
$aResult['error'] = 'plan_delete';
$aResult['error_description'] = 'please provide plan id';
}
return $aResult;
}
public static function createPdf( $id = 0 )
{
$plan = Plan::find( $id );
//do escape spesial chars
foreach ($plan->attributes as $key => $item) {
$plan->$key = htmlentities($item, ENT_QUOTES, 'UTF-8', false);
}
if( isset( $plan->implementation_date ) AND $plan->implementation_date AND $plan->implementation_date != "0000-00-00 00:00:00" ) {
$plan->implementation_date = date( "n/j/Y", strtotime($plan->implementation_date) );
} else {
$plan->implementation_date = "";
}
$targets = Plan_Target::get_byPlanIdGroupBy( $plan->id , 'week' , 'week' );
//do escape spesial chars for targets
foreach ($targets['targets'] as $target) {
foreach ($target[0]->attributes as $key => $value) {
$target[0]->$key = htmlentities($value, ENT_QUOTES, 'UTF-8', false);
}
}
$aPlanWeeks = Plan_Week::get_byPlanId($plan->id);
$aStandards = explode( "\n", $plan->standard );
$aNewStandards = "<div>" . implode("</div>\n<div>", $aStandards) . "</div>";
$plan->standard = $aNewStandards;
//do escape spesial chars for plans weeks
foreach ($aPlanWeeks['weeks'] as $week) {
$week->description = htmlentities($week->description, ENT_QUOTES, 'UTF-8', false);
}
$path = 'public/files/';
$filename = $id . '_plan';
$pdfContent = View::make( 'plans.pdfplan', array( 'plan' => $plan, 'targets' => $targets['targets'], 'weeks' => $aPlanWeeks['weeks'] ) );
$htmlContent = View::make( 'plans.htmlfileplan', array( 'plan' => $plan, 'targets' => $targets['targets'], 'weeks' => $aPlanWeeks['weeks'] ) );
$html2pdf = new HTML2PDF( 'P', 'A4', 'en', false, 'KOI8-R',0 );
$html2pdf->AddFont('Helvetica','','helvetica.php');
$html2pdf->pdf->SetFont("Helvetica");
$html2pdf->WriteHTML( $pdfContent );
$html2pdf->Output( $path . $filename . '.pdf' , 'F' );
File::put( $path . $filename . '.html' , $htmlContent );
return $path . $filename;
}
}