source: iPeer/app/controllers/events_controller.php @ 6602514

3.1.xdevhotfixpagodaboxticket463
Last change on this file since 6602514 was 6602514, checked in by Compass <pan.luo@…>, 6 years ago

Change the styles for evaluation result pages

  • Property mode set to 100644
File size: 16.5 KB
Line 
1<?php
2/**
3 * EventsController
4 *
5 * @uses AppController
6 * @package   CTLT.iPeer
7 * @author    Pan Luo <pan.luo@ubc.ca>
8 * @copyright 2012 All rights reserved.
9 * @license   MIT {@link http://www.opensource.org/licenses/MIT}
10 */
11class EventsController extends AppController
12{
13    public $name = 'Events';
14    public $helpers = array('Html', 'Ajax', 'Javascript', 'Time');
15    public $uses = array('GroupEvent', 'User', 'Group', 'Course', 'Event', 'EventTemplateType', 'SimpleEvaluation', 'Rubric', 'Mixeval', 'Personalize', 'GroupsMembers', 'Penalty');
16    public $components = array("AjaxList", "Session");
17
18    /**
19     * __construct
20     *
21     * @access protected
22     * @return void
23     */
24    function __construct()
25    {
26        $this->Sanitize = new Sanitize;
27        $this->set('title_for_layout', __('Events', true));
28        parent::__construct();
29    }
30
31    /**
32     * postProcessData Post Process Data : add released column
33     *
34     * @param mixed $data
35     *
36     * @access public
37     * @return void
38     */
39    function postProcessData($data)
40    {
41        // Check the release dates, and match them up with present date
42        if (empty($data)) {
43            return $data;
44        }
45        // loop through each data point, and display it.
46        foreach ($data as $i => $entry) {
47            $releaseDate = strtotime($entry["Event"]["release_date_begin"]);
48            $endDate = strtotime($entry["Event"]["release_date_end"]);
49            $timeNow = time();
50
51            if (!$releaseDate) {
52                $releaseDate = 0;
53            }
54            if (!$endDate) {
55                $endDate = 0;
56            }
57
58            $isReleased = "";
59            if ($timeNow < $releaseDate) {
60                $isReleased = __("Not Yet Open", true);
61            } else if ($timeNow > $endDate) {
62                $isReleased = __("Already Closed", true);
63            } else {
64                $isReleased = __("Open Now", true);
65            }
66
67            // Set the is released string
68            $entry['!Custom']['isReleased'] = $isReleased;
69
70            // Set the view results column
71            $entry['!Custom']['results'] = __("Results", true);
72
73            // Write the entry back
74            $data[$i] = $entry;
75        }
76
77        // Return the modified data
78        return $data;
79    }
80
81
82    /**
83     * setUpAjaxList
84     *
85     * @param mixed $courseId
86     *
87     * @access public
88     * @return void
89     */
90    function setUpAjaxList($courseId = null)
91    {
92        // Grab the course list
93        $coursesList = $this->Course->getAccessibleCourses(User::get('id'), User::getCourseFilterPermission(), 'list');
94
95        // Set up Columns
96        $columns = array(
97            array("Event.id",             "",            "",     "hidden"),
98            array("Course.id",            "",            "",     "hidden"),
99            array("Course.course",        __("Course", true),      "9em", "action", "View Course"),
100            array("Event.Title",          __("Title", true),       "auto", "action", "View Event"),
101            array("!Custom.results",       __("View", true),       "4em", "action", "View Results"),
102            array("Event.event_template_type_id", __("Type", true), "", "map",
103            array("1" => __("Simple", true), "2" => __("Rubric", true), "4" => __("Mixed", true))),
104            array("Event.due_date",       __("Due Date", true),    "10em", "date"),
105            array("!Custom.isReleased",    __("Released ?", true), "8em", "string"),
106            array("Event.self_eval",       __("Self Eval", true),   "6em", "map",
107            array("0" => __("Disabled", true), "1" => __("Enabled", true))),
108            array("Event.com_req",        __("Comment", true),      "6em", "map",
109            array("0" => __("Optional", true), "1" => __("Required", true))),
110
111            // Release window
112            array("Event.release_date_begin", "", "",    "hidden"),
113            array("Event.release_date_end",   "", "",    "hidden"),
114        );
115
116        // put all the joins together
117        // shows all events of courses the user (not student) has access to
118        if ($courseId == null) {
119            $joinTables =  array( array (
120                // GUI aspects
121                "id" => "course_id",
122                "description" => __("for Course:", true),
123                // The choice and default values
124                "list" => $coursesList,
125            ));
126        // shows only the events from the current selected course (parameter)
127        // non-numeric and invalid ids will result in "No Results"
128        } else {
129            $joinTables =  array( array (
130                // GUI aspects
131                "id" => "course_id",
132                "description" => __("for Course:", true),
133                // The choice and default values
134                "list" => $coursesList,
135                "default" => $courseId,
136            ));
137        }
138
139        // super admins
140        if (User::hasPermission('functions/superadmin')) {
141            $extraFilters = "";
142        // faculty admins or instructors - $coursesList from above
143        } else {
144            $extraFilters = " ( ";
145            $courseIds = array_keys($coursesList);
146            foreach ($courseIds as $id) {
147                $extraFilters .= "course_id=$id or ";
148            }
149            $extraFilters .= "1=0 ) ";
150        }
151
152        // Leave the survey types out, always
153        $extraFilters .= !empty($extraFilters) ? "and " : "";
154        $extraFilters .= "Event.event_template_type_id<>3";
155
156
157        // Set up actions
158        $warning = __("Are you sure you want to delete this event permanently?", true);
159        $actions = array(
160            array("View Results", "", "", "evaluations", "view", "Event.id"),
161            array("View Event", "", "", "", "view", "Event.id"),
162            array("Edit Event", "", "", "", "edit", "Event.id"),
163            array("View Course", "", "", "courses", "view", "Course.id"),
164            array("Export Results", "", "", "evaluations", "export/event", "Event.id"),
165            array("Delete Event", $warning, "", "", "delete", "Event.id"));
166
167        $recursive = 0;
168
169        $this->AjaxList->setUp($this->Event, $columns, $actions,
170            "Course.course", "Course.course", $joinTables, $extraFilters,
171            $recursive, "postProcessData");
172    }
173
174    /**
175     * index
176     *
177     * @param mixed $courseId
178     * @param string $message
179     *
180     * @access public
181     * @return void
182     */
183    function index($courseId = null, $message = '')
184    {
185        // check for permission to course ($courseId) only when $courseId is provided
186        if (!is_null($courseId)) {
187            $course = $this->Course->getAccessibleCourseById($courseId, User::get('id'), User::getCourseFilterPermission());
188            if (!$course) {
189                $this->Session->setFlash(__('Error: Course does not exist or you do not have permission to view this course.', true));
190                $this->redirect('index');
191            }
192            $this->breadcrumb->push(array('course' => $course['Course']));
193        }
194
195        $this->set('message', $message);
196
197        // We need to change the session state to point to this
198        // course:
199        // Initialize a basic non-funcional AjaxList
200        $this->AjaxList->quickSetUp();
201        // Clear the state first, we don't want any previous searches/selections.
202        $this->AjaxList->clearState();
203        // Set and update session state Variable
204        $joinFilterSelections->course_id = $courseId;
205        $this->AjaxList->setStateVariable("joinFilterSelections", $joinFilterSelections);
206
207        // Set up the basic static ajax list variables
208        $this->Session->write('eventsControllerCourseId', $courseId);
209        $this->setUpAjaxList($courseId);
210        // Set the display list
211        $this->set('paramsForList', $this->AjaxList->getParamsForList());
212        $this->set('courseId', $courseId);
213        $this->set('breadcrumb', $this->breadcrumb->push(__('Events', true)));
214    }
215
216
217    /**
218     * ajaxList
219     *
220     * @access public
221     * @return void
222     */
223    function ajaxList()
224    {
225        // Set up the list
226        $courseId = $this->Session->read('eventsControllerCourseId');
227        $this->setUpAjaxList($courseId);
228        // Process the request for data
229        $this->AjaxList->asyncGet();
230    }
231
232
233    /**
234     * view
235     *
236     * @param mixed $id
237     *
238     * @access public
239     * @return void
240     */
241    function view ($eventId)
242    {
243        if (!($event = $this->Event->getAccessibleEventById($eventId, User::get('id'), User::getCourseFilterPermission(), array('Course', 'Group.Member', 'EventTemplateType', 'Penalty' => array('order' => array('days_late ASC')))))) {
244            $this->Session->setFlash(__('Error: That event does not exist or you dont have access to it', true));
245            $this->redirect('index');
246            return;
247        } else if ($event['Event']['event_template_type_id'] == '3') {
248            // can't view survey event from this view
249            $this->Session->setFlash(__('Invalid Id', true));
250            $this->redirect('index');
251        }
252
253        switch ($event['Event']['event_template_type_id']) {
254        case 1:
255            $modelName = 'SimpleEvaluation';
256            break;
257        case 2:
258            $modelName = 'Rubric';
259            break;
260        case 4:
261            $modelName = 'Mixeval';
262        }
263
264        // find the related evaluation
265        $model = ClassRegistry::init($modelName);
266        $evaluation = $model->find('first', array(
267            'conditions' => array('id' => $event['Event']['template_id']),
268            'contain' => false
269        ));
270
271        //merge into event
272        $event = array_merge($event, $evaluation);
273
274        //Get all display event types
275        $this->set('breadcrumb', $this->breadcrumb->push(array('course' => $event['Course']))->push(array('event' => $event['Event']))->push(__('View', true)));
276        $this->set('event', $event);
277        $this->set('modelName', $modelName);
278    }
279
280    /**
281     * Add an event
282     *
283     * @param mixed $courseId
284     *
285     * @access public
286     * @return void
287     */
288    function add($courseId)
289    {
290        $course = $this->Course->getAccessibleCourseById($courseId, User::get('id'), User::getCourseFilterPermission());
291        if (!$course) {
292            $this->Session->setFlash(__('Error: Course does not exist or you do not have permission to view this course.', true));
293            $this->redirect('/home');
294            return;
295        }
296
297        // Init form variables needed for display
298        $this->set('breadcrumb', $this->breadcrumb->push(array('course' => $course['Course']))->push(__('Add Event', true)));
299        $this->set('groups', $this->Group->getGroupsByCourseId($courseId));
300        $courseList = $this->Course->getAccessibleCourses(User::get('id'), User::getCourseFilterPermission(), 'list');
301        $this->set('courses', $courseList);
302        $this->set(
303            'eventTemplateTypes',
304            $this->EventTemplateType->getEventTemplateTypeList(true)
305        );
306        $this->set(
307            'mixevals',
308            $this->Mixeval->getBelongingOrPublic($this->Auth->user('id'))
309        );
310        $this->set(
311            'simpleEvaluations',
312            $this->SimpleEvaluation->getBelongingOrPublic($this->Auth->user('id'))
313        );
314        $this->set(
315            'rubrics',
316            $this->Rubric->getBelongingOrPublic($this->Auth->user('id'))
317        );
318        $this->set('course_id', $courseId);
319
320        // Try to save the data
321        if (!empty($this->data)) {
322            // need to set the template_id based on the event_template_type_id
323            $typeId = $this->data['Event']['event_template_type_id'];
324            if ($typeId == 1) {
325                $this->data['Event']['template_id'] =
326                    $this->data['Event']['SimpleEvaluation'];
327            } else if ($typeId == 2) {
328                $this->data['Event']['template_id'] =
329                    $this->data['Event']['Rubric'];
330            } else if ($typeId == 4) {
331                $this->data['Event']['template_id'] =
332                    $this->data['Event']['Mixeval'];
333            }
334
335            if ($this->Event->saveAll($this->data)) {
336                $this->Session->setFlash("Add event successful!", 'good');
337            } else {
338                $this->Session->setFlash("Add event failed.");
339            }
340            $this->redirect('index/'.$courseId);
341        }
342    }
343
344    /**
345     * edit
346     *
347     * @param mixed $id
348     *
349     * @access public
350     * @return void
351     */
352    function edit($eventId)
353    {
354        // Check whether the course exists
355        if (!($event = $this->Event->getAccessibleEventById($eventId, User::get('id'), User::getCourseFilterPermission(), array('Course', 'Group', 'Penalty')))) {
356            $this->Session->setFlash(__('Error: That event does not exist or you dont have access to it', true));
357            $this->redirect('index');
358            return;
359        } else if ($event['Event']['event_template_type_id'] == '3') {
360            // can't edit survey event from this view
361            $this->redirect('/surveys/edit/'.$eventId);
362            return;
363        }
364
365        if (!empty($this->data)) {
366            // need to set the template_id based on the event_template_type_id
367            $typeId = $this->data['Event']['event_template_type_id'];
368            if ($typeId == 1) {
369                $this->data['Event']['template_id'] =
370                    $this->data['Event']['SimpleEvaluation'];
371            } else if ($typeId == 2) {
372                $this->data['Event']['template_id'] =
373                    $this->data['Event']['Rubric'];
374            } else if ($typeId == 4) {
375                $this->data['Event']['template_id'] =
376                    $this->data['Event']['Mixeval'];
377            }
378
379            $penaltyData = $this->Penalty->find('all', array('conditions' => array('event_id' => $eventId), 'contain' => false));
380            $penalties = array();
381            foreach ($penaltyData as $tmp) {
382                array_splice($tmp['Penalty'], 1, -2);
383                $penalties[] = $tmp['Penalty'];
384            }
385
386            isset($this->data['Penalty']) ? $formPenalty = $this->data['Penalty'] : $formPenalty = array();
387            // check differences (table vs form data), delete what's missing in form data from db
388            foreach ($penalties as $pTmp) {
389                if (!in_array($pTmp, $formPenalty)) {
390                    $this->Penalty->delete($pTmp['id']);
391                }
392            }
393            if ($this->Event->saveAll($this->data)) {
394                $this->Session->setFlash("Edit event successful!", 'good');
395                $this->redirect('index/'.$event['Event']['course_id']);
396                return;
397            } else {
398                $this->Session->setFlash("Edit event failed.");
399            }
400        }
401
402        // Sets up the already assigned groups
403
404        $this->set('groups', $this->Group->getGroupsByCourseId($event['Event']['course_id']));
405        $this->set(
406            'mixevals',
407            $this->Mixeval->getBelongingOrPublic(User::get('id'))
408        );
409        $this->set(
410            'simpleEvaluations',
411            $this->SimpleEvaluation->getBelongingOrPublic(User::get('id'))
412        );
413        $this->set(
414            'rubrics',
415            $this->Rubric->getBelongingOrPublic(User::get('id'))
416        );
417
418        $this->set('event', $event);
419        $this->set('eventTemplateTypes', $this->EventTemplateType->find('list', array('conditions' => array('NOT' => array('id' => 3)))));
420        $this->set('breadcrumb', $this->breadcrumb->push(array('course' => $event['Course']))->push(array('event' => $event['Event']))->push(__('Edit', true)));
421
422        $this->data = $event;
423    }
424
425    /**
426     * delete
427     *
428     * @param int $eventId
429     *
430     * @access public
431     * @return void
432     */
433    function delete ($eventId)
434    {
435        // Check whether the course exists
436        if (!($event = $this->Event->getAccessibleEventById($eventId, User::get('id'), User::getCourseFilterPermission(), array('Course', 'Group', 'Penalty')))) {
437            $this->Session->setFlash(__('Error: That event does not exist or you dont have access to it', true));
438            $this->redirect('/home');
439            return;
440        }
441
442        // what's this????
443        if (isset($this->params['form']['id'])) {
444            $eventId = intval(substr($this->params['form']['id'], 5));
445
446        }
447
448        if ($this->Event->delete($eventId)) {
449            $this->Session->setFlash(__('The event has been deleted successfully.', true), 'good');
450            $this->redirect('index/'.$event['Event']['course_id']);
451            return;
452        } else {
453            $this->Session->setFlash(__('Failed to delete the event', true));
454            $this->redirect('index/'.$event['Event']['course_id']);
455            return;
456        }
457    }
458}
Note: See TracBrowser for help on using the repository browser.