/
events_controller.php
763 lines (696 loc) · 29.2 KB
/
events_controller.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
<?php
/**
* EventsController
*
* @uses AppController
* @package CTLT.iPeer
* @author Pan Luo <pan.luo@ubc.ca>
* @copyright 2012 All rights reserved.
* @license MIT {@link http://www.opensource.org/licenses/MIT}
*/
class EventsController extends AppController
{
public $name = 'Events';
public $helpers = array('Html', 'Ajax', 'Javascript', 'Time');
public $uses = array('GroupEvent', 'User', 'Group', 'Course', 'Event', 'EventTemplateType',
'SimpleEvaluation', 'Rubric', 'Mixeval', 'Personalize', 'GroupsMembers', 'Penalty', 'Survey','EmailSchedule',
'EvaluationSubmission', 'EmailTemplate', 'EvaluationRubric', 'EvaluationSimple', 'EvaluationMixeval');
public $components = array("AjaxList", "Session", "RequestHandler","Email", "Evaluation");
/**
* __construct
*
* @access protected
* @return void
*/
function __construct()
{
$this->Sanitize = new Sanitize;
$this->set('title_for_layout', __('Events', true));
parent::__construct();
}
/**
* postProcessData Post Process Data : add released column
*
* @param mixed $data
*
* @access public
* @return void
*/
function postProcessData($data)
{
// Check the release dates, and match them up with present date
if (empty($data)) {
return $data;
}
// loop through each data point, and display it.
foreach ($data as $i => $entry) {
$releaseDate = strtotime($entry["Event"]["release_date_begin"]);
$endDate = strtotime($entry["Event"]["release_date_end"]);
$resultStart = strtotime($entry['Event']['result_release_date_begin']);
$resultEnd = strtotime($entry['Event']['result_release_date_end']);
$timeNow = time();
if (!$releaseDate) {
$releaseDate = 0;
}
if (!$endDate) {
$endDate = 0;
}
$isReleased = "";
if ($timeNow < $releaseDate) {
$isReleased = __("Not Yet Open", true);
} else if ($timeNow > $endDate) {
$isReleased = __("Already Closed", true);
} else {
$isReleased = __("Open Now", true);
}
// Set the is released string
$entry['!Custom']['isReleased'] = $isReleased;
// Set the view results column
$entry['!Custom']['results'] = __("Results", true);
if ($entry['Event']['event_template_type_id'] == 3) {
$entry['!Custom']['resultReleased'] = 'N/A';
} else if ($timeNow < $resultStart) {
$entry['!Custom']['resultReleased'] = 'Not Yet';
} else if ($timeNow >= $resultEnd) {
$entry['!Custom']['resultReleased'] = 'Closed';
} else if ($entry['Event']['auto_release']){
$entry['!Custom']['resultReleased'] = 'Auto';
} else {
$entry['!Custom']['resultReleased'] = 'Manual';
}
// group count
if ($entry['Event']['event_template_type_id'] == 3) {
$entry['!Custom']['group_count'] = 'N/A';
} else if ($entry['Event']['group_count'] > 0) {
$entry['!Custom']['group_count'] = $entry['Event']['group_count'];
} else {
// evaluation event without any groups - warning
$entry['!Custom']['group_count'] = "<font color='red'>0</font>";
}
// Write the entry back
$data[$i] = $entry;
}
// Return the modified data
return $data;
}
/**
* setUpAjaxList
*
* @param mixed $courseId
*
* @access public
* @return void
*/
function setUpAjaxList($courseId = null)
{
// Grab the course list
$coursesList = $this->Course->getAccessibleCourses(User::get('id'), User::getCourseFilterPermission(), 'list');
// Set up Columns
$columns = array(
array("Event.id", "", "", "hidden"),
array("Course.id", "", "", "hidden"),
array("Event.group_count", "", "", "hidden"),
array("Course.course", __("Course", true), "9em", "action", "View Course"),
array("Event.Title", __("Title", true), "auto", "action", "View Event"),
array("!Custom.results", __("View", true), "4em", "action", "View Results"),
array("!Custom.group_count", __("Groups", true), "2em", "string"),
array("Event.event_template_type_id", __("Type", true), "", "map",
array(
"1" => __("Simple", true),
"2" => __("Rubric", true),
"3" => __("Survey", true),
"4" => __("Mixed", true))),
array("Event.due_date", __("Due Date", true), "10em", "date"),
array("!Custom.isReleased", __("Released ?", true), "8em", "string"),
array("!Custom.resultReleased", __("Result Released", true), "6em", "string"),
// Release window
array("Event.release_date_begin", "", "", "hidden"),
array("Event.release_date_end", "", "", "hidden"),
array("Event.result_release_date_begin", "", "", "hidden"),
array("Event.result_release_date_end", "", "", "hidden"),
array("Event.auto_release", "", "", "hidden"),
);
// put all the joins together
// shows all events of courses the user (not student) has access to
if ($courseId == null) {
$joinTables = array( array (
// GUI aspects
"id" => "course_id",
"description" => __("for Course:", true),
// The choice and default values
"list" => $coursesList,
));
// shows only the events from the current selected course (parameter)
// non-numeric and invalid ids will result in "No Results"
} else {
$joinTables = array( array (
// GUI aspects
"id" => "course_id",
"description" => __("for Course:", true),
// The choice and default values
"list" => $coursesList,
"default" => $courseId,
));
}
// super admins
if (User::hasPermission('functions/superadmin')) {
$extraFilters = "";
// faculty admins or instructors - $coursesList from above
} else {
$extraFilters = " ( ";
$courseIds = array_keys($coursesList);
foreach ($courseIds as $id) {
$extraFilters .= "course_id=$id or ";
}
$extraFilters .= "1=0 ) ";
}
// Surveys cannot be exported
$restrictions['Event.event_template_type_id'] = array(1 => true, 2 => true,
4 => true, "!default" => false);
// Set up actions
$warning = __("Are you sure you want to delete this event permanently?", true);
$actions = array(
array("View Results", "", "", "evaluations", "view", "Event.id"),
array("View Event", "", "", "", "view", "Event.id"),
array("Edit Event", "", "", "", "edit", "Event.id"),
array("View Course", "", "", "courses", "view", "Course.id"),
array("Export Results", "", $restrictions, "evaluations", "export/event", "Event.id"),
array("Delete Event", $warning, "", "", "delete", "Event.id"));
$recursive = 0;
$this->AjaxList->setUp($this->Event, $columns, $actions,
"Course.course", "Course.course", $joinTables, $extraFilters,
$recursive, "postProcessData");
}
/**
* index
*
* @param mixed $courseId
*
* @access public
* @return void
*/
function index($courseId = null)
{
$course = array('Course' => array('id' => $courseId));
// check for permission to course ($courseId) only when $courseId is provided
if (!is_null($courseId)) {
$course = $this->Course->getAccessibleCourseById($courseId, User::get('id'), User::getCourseFilterPermission());
if (!$course) {
$this->Session->setFlash(__('Error: Course does not exist or you do not have permission to view this course.', true));
$this->redirect('index');
return;
}
$this->breadcrumb->push(array('course' => $course['Course']));
}
// We need to change the session state to point to this
// course:
// Initialize a basic non-functional AjaxList
$this->AjaxList->quickSetUp();
// Clear the state first, we don't want any previous searches/selections
$this->AjaxList->clearState();
// Set and update session state Variable
$joinFilterSelections = new Object();
$joinFilterSelections->course_id = $courseId;
$this->AjaxList->setStateVariable("joinFilterSelections", $joinFilterSelections);
// Set up the basic static ajax list variables
$this->Session->write('eventsControllerCourseId', $courseId);
$this->setUpAjaxList($courseId);
// Set the display list
$this->set('course', $course);
$this->set('paramsForList', $this->AjaxList->getParamsForList());
$this->set('breadcrumb', $this->breadcrumb->push(__('Events', true)));
}
/**
* ajaxList
*
* @access public
* @return void
*/
function ajaxList()
{
// Set up the list
$courseId = $this->Session->read('eventsControllerCourseId');
$this->setUpAjaxList($courseId);
// Process the request for data
$this->AjaxList->asyncGet();
}
/**
* view
*
* @param mixed $eventId
*
* @access public
* @return void
*/
function view ($eventId)
{
if (!($event = $this->Event->getAccessibleEventById($eventId, User::get('id'), User::getCourseFilterPermission(), array('Course', 'Group.Member', 'EventTemplateType', 'Penalty' => array('order' => array('days_late ASC')))))) {
$this->Session->setFlash(__('Error: That event does not exist or you dont have access to it', true));
$this->redirect('index');
return;
}
switch ($event['Event']['event_template_type_id']) {
case 1:
$modelName = 'SimpleEvaluation';
break;
case 2:
$modelName = 'Rubric';
break;
case 3:
$modelName = 'Survey';
break;
case 4:
$modelName = 'Mixeval';
}
// find the related evaluation
$evaluation = $this->$modelName->find('first', array(
'conditions' => array('id' => $event['Event']['template_id']),
'recursive' => -1
));
//merge into event
$event = array_merge($event, $evaluation);
//Get all display event types
$this->set('breadcrumb', $this->breadcrumb->push(array('course' => $event['Course']))->push(array('event' => $event['Event']))->push(__('View', true)));
$this->set('event', $event);
$this->set('modelName', $modelName);
}
/**
* Add an event
*
* @param mixed $courseId
*
* @access public
* @return void
*/
function add($courseId)
{
$course = $this->Course->getAccessibleCourseById($courseId, User::get('id'), User::getCourseFilterPermission());
if (!$course) {
$this->Session->setFlash(__('Error: Course does not exist or you do not have permission to view this course.', true));
$this->redirect('/home');
return;
}
// Init form variables needed for display
$this->set('breadcrumb', $this->breadcrumb->push(array('course' => $course['Course']))->push(__('Add Event', true)));
$this->set('groups', $this->Group->getGroupsByCourseId($courseId));
$courseList = $this->Course->getAccessibleCourses(User::get('id'), User::getCourseFilterPermission(), 'list');
$this->set('courses', $courseList);
$this->set(
'eventTemplateTypes',
$this->EventTemplateType->getEventTemplateTypeList(true)
);
$this->set(
'mixevals',
$this->Mixeval->getBelongingOrPublic($this->Auth->user('id'))
);
$this->set(
'simpleEvaluations',
$this->SimpleEvaluation->getBelongingOrPublic($this->Auth->user('id'))
);
$this->set(
'surveys',
$this->Survey->getBelongingOrPublic($this->Auth->user('id'))
);
$this->set(
'rubrics',
$this->Rubric->getBelongingOrPublic($this->Auth->user('id'))
);
$emailReminders = array('0'=> 'Disable', '1' => '1 Day', '2'=>'2 Days','3'=>'3 Days','4'=>'4 Days','5'=>'5 Days','6'=>'6 Days','7'=>'7 Days');
$this->set('emailTemplates', $this->EmailTemplate->getPermittedEmailTemplate(User::get('id'), 'list'));
$this->set('emailSchedules', $emailReminders);
$this->set('course_id', $courseId);
// Try to save the data
if (!empty($this->data)) {
// need to set the template_id based on the event_template_type_id
$typeId = $this->data['Event']['event_template_type_id'];
if ($typeId == 1) {
$this->data['Event']['template_id'] =
$this->data['Event']['SimpleEvaluation'];
} else if ($typeId == 2) {
$this->data['Event']['template_id'] =
$this->data['Event']['Rubric'];
} else if ($typeId == 3) {
$this->data['Event']['template_id'] =
$this->data['Event']['Survey'];
} else if ($typeId == 4) {
$this->data['Event']['template_id'] =
$this->data['Event']['Mixeval'];
}
$this->data = $this->_multiMap($this->data);
if ($this->Event->saveAll($this->data)) {
$this->Session->setFlash("Add event successful!", 'good');
//Call the setSchedule function to Schedule reminder emails
$this->setSchedule($this->Event->id, $this->data);
// set release status for group event
$groupEvents = $this->GroupEvent->find('list',
array('conditions' => array('event_id'=>$this->Event->id)));
if (isset($this->data['Event']['auto_release']) && $this->data['Event']['auto_release']) {
$this->Evaluation->setGroupEventsReleaseStatus($groupEvents, 'Auto');
} else {
$this->Evaluation->setGroupEventsReleaseStatus($groupEvents, 'None');
}
$this->redirect('index/'.$courseId);
return;
} else {
$this->Session->setFlash("Add event failed.");
}
}
}
/**
* setSchedule
*
* @param mixed $eventId
* @param mixed $eventData
*
* @access public
* @return void
*/
function setSchedule($eventId, $eventData)
{
$emailFreq = $eventData['Event']['email_schedule'];
$to = $this->getGroupMembers($eventId); // get members
if ($emailFreq == 0 || empty($to)) {
return;
}
$emailTemp = $eventData['Event']['EmailTemplate'];
$template = $this->EmailTemplate->findById($emailTemp);
$subject = $template['EmailTemplate']['subject'];
$courseId = $this->Event->getCourseByEventId($eventId);
//Get the startdate, duedate and frequency of emails
$startDate = $eventData['Event']['release_date_begin'];
$startDate = strtotime($eventData['Event']['release_date_begin']) > time() ?
$eventData['Event']['release_date_begin'] : date('Y-m-j H:i:s');
$dueDate = $eventData['Event']['due_date'];
$emailFreq = '+' . $emailFreq . ' day';
$courseName = $this->Course->getCourseName($courseId);
//Prepare the data for pushing to the email_schedules table
$data = array();
$data['course_id']= $courseId;
$data['event_id'] = $eventId;
$data['from'] = $this->Auth->user('id');
$data['subject'] = $subject;
$data['content'] = $emailTemp; //saving email template id
$data['to'] = 'save_reminder;'.implode(';', $to);
while (strtotime($startDate) < strtotime($dueDate)) {
$data['date'] = $startDate;
$startDate = strtotime($emailFreq, strtotime($startDate));
$startDate = date('Y-m-j H:i:s', $startDate);
$data = $this->_multiMap($data);
$this->EmailSchedule->saveAll($data);
}
return;
}
/**
* getGroupMembers
*
* @param mixed $eventId
*
* @access public
* @return void
*/
function getGroupMembers($eventId)
{
$event = $this->Event->findById($eventId);
if ($event['Event']['event_template_type_id'] == 3) {
$class = $this->User->getEnrolledStudents($event['Event']['course_id']);
return Set::extract('/User/id', $class);
} else {
//Get the groups by the eventId
$groups = $this->GroupEvent->findAllByEventId($eventId);
//Get the assigned groupids and their resepctive members for the event
$groupIds = Set::extract('/GroupEvent/group_id', $groups);
$members = $this->GroupsMembers->findAllByGroupId($groupIds);
return Set::extract('/GroupsMembers/user_id', $members);
}
}
/**
* edit
*
* @param mixed $eventId
*
* @access public
* @return void
*/
function edit($eventId)
{
// Check whether the course exists
if (!($event = $this->Event->getAccessibleEventById($eventId, User::get('id'), User::getCourseFilterPermission(), array('Course', 'Group', 'Penalty')))) {
$this->Session->setFlash(__('Error: That event does not exist or you dont have access to it', true));
$this->redirect('index');
return;
}
$orig_email_frequency = $this->calculateFrequency($eventId);
$this->set('email_schedule', $orig_email_frequency);
$emailTemp = $this->EmailSchedule->find('first', array(
'conditions' => array('EmailSchedule.sent' => 0, 'EmailSchedule.event_id' => $eventId)
));
$this->set('emailId', $emailTemp['EmailSchedule']['content']);
$event = $this->Event->getEventById($eventId);
if (!empty($this->data) && array_key_exists('formLoaded', $this->data)) {
if (!array_key_exists('formLoaded', $this->data)) {
$this->Session->setFlash("Edit event failed because the form hasn't finished loading yet.");
return;
}
// need to set the template_id based on the event_template_type_id
$typeId = $this->data['Event']['event_template_type_id'];
if ($typeId == 1) {
$this->data['Event']['template_id'] =
$this->data['Event']['SimpleEvaluation'];
} else if ($typeId == 2) {
$this->data['Event']['template_id'] =
$this->data['Event']['Rubric'];
} else if ($typeId == 3) {
$this->data['Event']['template_id'] =
$this->data['Event']['Survey'];
} else if ($typeId == 4) {
$this->data['Event']['template_id'] =
$this->data['Event']['Mixeval'];
}
// if all groups unselected - delete groupEvents
if (empty($this->data['Group']['Group'])) {
$this->GroupEvent->deleteAll(array('GroupEvent.event_id' => $eventId));
}
// update submitted evaluations release status if auto-release status has been changed
$groupEvents = $this->GroupEvent->find('list',
array('conditions' => array('event_id'=>$eventId)));
if ($this->data['Event']['auto_release'] != $event['Event']['auto_release']) {
$model = null;
switch ($event['Event']['event_template_type_id']) {
case 1://simple
//$model = 'EvaluationSimple';
$this->EvaluationSimple->setAllEventCommentRelease($eventId, $this->Auth->user('id'), $this->data['Event']['auto_release']);
$this->EvaluationSimple->setAllEventGradeRelease($eventId, $this->data['Event']['auto_release']);
break;
case 2://rubric
$this->Evaluation->changeRubricEvalCommentRelease($this->data['Event']['auto_release'], $groupEvents);
$this->EvaluationRubric->setAllEventGradeRelease($eventId, $this->data['Event']['auto_release']);
break;
case 4:
$this->Evaluation->changeMixedEvalCommentRelease($this->data['Event']['auto_release'], $groupEvents);
$this->EvaluationMixeval->setAllEventGradeRelease($eventId, $this->data['Event']['auto_release']);
break;
}
if ($this->data['Event']['auto_release']) {
$this->Evaluation->setGroupEventsReleaseStatus($groupEvents, 'Auto');
} else {
$this->Evaluation->setGroupEventsReleaseStatus($groupEvents, 'None');
}
}
$penaltyData = $this->Penalty->find('all', array('conditions' => array('event_id' => $eventId), 'contain' => false));
$penalties = array();
foreach ($penaltyData as $tmp) {
array_splice($tmp['Penalty'], 1, -2);
$penalties[] = $tmp['Penalty'];
}
isset($this->data['Penalty']) ? $formPenalty = $this->data['Penalty'] : $formPenalty = array();
// check differences (table vs form data), delete what's missing in form data from db
foreach ($penalties as $pTmp) {
if (!in_array($pTmp, $formPenalty)) {
$this->Penalty->delete($pTmp['id']);
}
}
$this->data = $this->_multiMap($this->data);
if ($this->Event->saveAll($this->data)) {
$this->Session->setFlash("Edit event successful!", 'good');
if ($this->checkIfChanged($event, $this->data, $orig_email_frequency, $emailTemp)) {
// only delete emails that haven't been sent
$this->EmailSchedule->deleteAll(array('event_id' => $eventId, 'sent' => 0), false);
$this->setSchedule($eventId, $this->data);
}
$this->redirect('index/'.$event['Event']['course_id']);
return;
} else {
$this->Session->setFlash("Edit event failed.");
}
} else if (!empty($this->data)) {
$this->Session->setFlash("Edit event failed because the form hasn't finished loading yet.");
}
// Sets up the already assigned groups
$this->set('groups', $this->Group->getGroupsByCourseId($event['Event']['course_id']));
// Populate the template selections
$this->set(
'mixevals',
$this->Mixeval->getBelongingOrPublic($this->Auth->user('id'))
);
$this->set(
'simpleEvaluations',
$this->SimpleEvaluation->getBelongingOrPublic($this->Auth->user('id'))
);
$this->set(
'surveys',
$this->Survey->getBelongingOrPublic($this->Auth->user('id'))
);
$this->set(
'rubrics',
$this->Rubric->getBelongingOrPublic($this->Auth->user('id'))
);
$this->set(
'eventTemplateTypes',
$this->EventTemplateType->getEventTemplateTypeList(true)
);
$this->set('simpleSelected', '');
$this->set('rubricSelected', '');
$this->set('surveySelected', '');
$this->set('mixevalSelected', '');
$typeId = $event['Event']['event_template_type_id'];
if ($typeId == 1) {
$this->set('simpleSelected', $event['Event']['template_id']);
} else if ($typeId == 2) {
$this->set('rubricSelected', $event['Event']['template_id']);
} else if ($typeId == 3) {
$this->set('surveySelected', $event['Event']['template_id']);
} else if ($typeId == 4) {
$this->set('mixevalSelected', $event['Event']['template_id']);
}
$emailReminders = array('0'=> 'Disable', '1' => '1 Day', '2'=>'2 Days','3'=>'3 Days','4'=>'4 Days','5'=>'5 Days','6'=>'6 Days','7'=>'7 Days');
$this->set('emailTemplates', $this->EmailTemplate->getPermittedEmailTemplate(User::get('id'), 'list'));
$this->set('emailSchedules', $emailReminders);
$this->set('event', $event);
$this->set('breadcrumb', $this->breadcrumb->push(array('course' => $event['Course']))->push(array('event' => $event['Event']))->push(__('Edit', true)));
$this->data = $event;
}
/**
* checkIfChanged
*
* @param mixed $event
* @param mixed $data
* @param int $email_frequency
* @param int $originalTemp original template id
*
* @access public
* @return bool - return 1 if the data has been modified else returns 0
**/
function checkIfChanged($event, $data, $email_frequency, $originalTemp)
{
$orig_release_date_begin = $event['Event']['release_date_begin'];
$orig_due_date = $event['Event']['due_date'];
$new_release_date_begin = $data['Event']['release_date_begin'];
$new_due_date = $data['Event']['due_date'];
$orig_frequency = $email_frequency;
$new_frequency = $data['Event']['email_schedule'];
$new_template = $data['Event']['EmailTemplate'];
if ($orig_release_date_begin != $new_release_date_begin || $orig_due_date != $new_due_date
|| $orig_frequency != $new_frequency || $originalTemp != $new_template) {
return 1;
} else {
return 0;
}
}
/**
* calculateFrequency
*
* @param int $eventId
*
* @access public
* @return int - email_frequency
**/
function calculateFrequency($eventId)
{
$schedule = $this->EmailSchedule->find('all', array(
'conditions' => array('event_id' => $eventId),
'order' => 'date DESC'
));
$count = count($schedule);
if ($count <= 0) {
$email_frequency = 0;
} else if ($count == 1) {
$email_frequency = 7; // set to 7 days
} else {
// last two emails
$last = strtotime($schedule[0]['EmailSchedule']['date']);
$second = strtotime($schedule[1]['EmailSchedule']['date']);
$diff = $last - $second;
$email_frequency = ceil($diff/(60*60*24));
}
return $email_frequency;
}
/**
* delete
*
* @param int $eventId
*
* @access public
* @return void
*/
function delete ($eventId)
{
// Check whether the course exists
if (!($event = $this->Event->getAccessibleEventById($eventId, User::get('id'), User::getCourseFilterPermission(), array('Course', 'Group', 'Penalty')))) {
$this->Session->setFlash(__('Error: That event does not exist or you dont have access to it', true));
$this->redirect('/home');
return;
}
// what's this????
if (isset($this->params['form']['id'])) {
$eventId = intval(substr($this->params['form']['id'], 5));
}
if ($this->Event->delete($eventId)) {
$this->Session->setFlash(__('The event has been deleted successfully.', true), 'good');
$this->redirect('index/'.$event['Event']['course_id']);
return;
} else {
$this->Session->setFlash(__('Failed to delete the event', true));
$this->redirect('index/'.$event['Event']['course_id']);
return;
}
}
/**
* checkDuplicateName
*
* @param mixed $courseId course id
* @param mixed $eventId event id
*
* @access public
* @return void
*/
function checkDuplicateName($courseId, $eventId=null)
{
if (!$this->RequestHandler->isAjax()) {
$this->cakeError('error404');
}
$this->layout = 'ajax';
$this->autoRender = false;
$conditions = array('Event.title' => $this->data['Event']['title'], 'Event.course_id' => $courseId);
if (!is_null($eventId)) {
$conditions = $conditions + array('not' => array('Event.id' => $eventId));
}
$sFound = $this->Event->find('all', array('conditions' => $conditions));
return ($sFound) ? __('Event title "', true).$this->data['Event']['title'].__('" already exists in this course.', true) : '';
}
/**
* _multiMap
*
* @param mixed $data data
*
* @access private
* @return void
*/
function _multiMap($data)
{
$ret = array();
foreach($data as $key => $value) {
$ret[$key] = is_array($value) ? $this->_multiMap($value) : trim($value);
}
return $ret;
}
}