| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Karamelo e-Learning Platform |
|---|
| 4 | * Chipotle Software TM 2002-2008 |
|---|
| 5 | * GPLv3 |
|---|
| 6 | * @author Manuel Montoya manuel<arroba>mononeurona<punto>org |
|---|
| 7 | * @version 0.3 |
|---|
| 8 | * @package Karamelo |
|---|
| 9 | */ |
|---|
| 10 | // file : app/controller/lessons_controller.php |
|---|
| 11 | |
|---|
| 12 | App::import('Sanitize'); |
|---|
| 13 | |
|---|
| 14 | class LessonsController extends AppController |
|---|
| 15 | { |
|---|
| 16 | public $helpers = array('Ajax', 'User', 'Fck', 'Gags', 'Time'); |
|---|
| 17 | |
|---|
| 18 | public $components = array('Edublog', 'Captcha'); |
|---|
| 19 | |
|---|
| 20 | public $paginate = array('limit' => 20, 'page' => 1); |
|---|
| 21 | |
|---|
| 22 | public function beforeFilter() |
|---|
| 23 | { |
|---|
| 24 | parent::beforeFilter(); |
|---|
| 25 | $this->Auth->allow(array('view', 'display', 'annotation', 'captcha', 'contribution')); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | public function annotation() |
|---|
| 29 | { |
|---|
| 30 | if (!empty($this->data['Annotation']) && $this->Auth->user()): |
|---|
| 31 | Sanitize::clean($this->data['Annotation']); |
|---|
| 32 | |
|---|
| 33 | $this->data['Annotation']['user_id'] = $this->Auth->user('id'); |
|---|
| 34 | |
|---|
| 35 | if ($this->Lesson->Annotation->save($this->data)): |
|---|
| 36 | $this->msgFlash(__('Comment saved', true), $this->data['Annotation']['redirect_to'].'/#comments'); |
|---|
| 37 | endif; |
|---|
| 38 | endif; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | public function contribution() |
|---|
| 42 | { |
|---|
| 43 | if ($this->data['Contribution']['captcha'] != $this->Session->read('captcha') || strlen($this->data['Contribution']['captcha']) < 3): |
|---|
| 44 | $this->flash(__('Captcha is wrong', true), $this->data['Contribution']['redirect_to'].'/#comments'); |
|---|
| 45 | $this->Session->delete('captcha'); |
|---|
| 46 | return false; |
|---|
| 47 | endif; |
|---|
| 48 | |
|---|
| 49 | if (!empty($this->data['Contribution'])): |
|---|
| 50 | Sanitize::clean($this->data['Contribution']); |
|---|
| 51 | $this->data['Contribution']['type'] = 2; |
|---|
| 52 | if ($this->Lesson->Contribution->save($this->data)): |
|---|
| 53 | $this->Session->delete('captcha'); |
|---|
| 54 | $this->msgFlash(__('Comment saved', true), $this->data['Contribution']['redirect_to'].'/#comments'); |
|---|
| 55 | endif; |
|---|
| 56 | endif; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | public function view($username, $lesson_id) |
|---|
| 60 | { |
|---|
| 61 | $this->__visits($lesson_id); // sum 1 to visits |
|---|
| 62 | $conditions = array('Lesson.id'=>$lesson_id, 'Lesson.status'=>1); |
|---|
| 63 | $fields = array('Lesson.id', 'Lesson.title', 'Lesson.body', 'Lesson.created', 'Lesson.disc', 'Lesson.modified', 'Lesson.user_id', 'Lesson.visits'); |
|---|
| 64 | $this->Lesson->User->unbindAll(); |
|---|
| 65 | $data = $this->Lesson->find($conditions, $fields, null, 2); |
|---|
| 66 | |
|---|
| 67 | $this->set('data', $data); |
|---|
| 68 | |
|---|
| 69 | $user_id = (int) $this->Lesson->User->field('id', array('User.username'=>$username)); |
|---|
| 70 | |
|---|
| 71 | $this->layout = $this->Edublog->layout($user_id); |
|---|
| 72 | |
|---|
| 73 | $this->Edublog->blog($user_id); // blogger elements |
|---|
| 74 | |
|---|
| 75 | $this->pageTitle = $username .' '. __('Lesson', true); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | public function captcha() |
|---|
| 79 | { |
|---|
| 80 | return $this->Captcha->render(); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | private function __visits($lesson_id) |
|---|
| 84 | { |
|---|
| 85 | if ( $this->Session->read('Lesson.id') == $lesson_id): |
|---|
| 86 | return; |
|---|
| 87 | else: |
|---|
| 88 | $this->Lesson->addVisit($lesson_id); |
|---|
| 89 | $this->Session->write('Lesson.id', $lesson_id); |
|---|
| 90 | endif; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | public function display($username) |
|---|
| 94 | { |
|---|
| 95 | $user_id = (int) $this->Lesson->User->field('id', array('User.username'=>$username)); |
|---|
| 96 | $this->pageTitle = $username . ' '.__('Lessons', true); |
|---|
| 97 | $this->paginate['conditions'] = array('Lesson.status'=>1, 'Lesson.user_id'=>$user_id); |
|---|
| 98 | $this->paginate['fields'] = array('id', 'title', 'created', 'user_id'); |
|---|
| 99 | $this->paginate['order'] = array('Lesson.title' => 'DESC'); |
|---|
| 100 | |
|---|
| 101 | $data = $this->paginate('Lesson'); |
|---|
| 102 | |
|---|
| 103 | $this->set(compact('data')); |
|---|
| 104 | |
|---|
| 105 | $this->layout = $this->Edublog->layout($user_id); |
|---|
| 106 | |
|---|
| 107 | $this->Edublog->blog($user_id); // blogger elements |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /*** === ADMIN METHODS ****/ |
|---|
| 111 | public function admin_listing() |
|---|
| 112 | { |
|---|
| 113 | $this->layout = 'admin'; |
|---|
| 114 | $this->PageTitle = __('Lessons', true); |
|---|
| 115 | $this->paginate['conditions'] = array('Lesson.user_id'=>$this->Auth->user('id')); |
|---|
| 116 | $this->paginate['fields'] = array('id', 'title', 'created', 'user_id', 'status'); |
|---|
| 117 | $this->paginate['order'] = array('Lesson.id' => 'DESC'); |
|---|
| 118 | $data = $this->paginate('Lesson'); |
|---|
| 119 | $this->set(compact('data')); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | public function admin_edit($lesson_id=null) |
|---|
| 123 | { |
|---|
| 124 | $this->layout = 'admin'; |
|---|
| 125 | $this->set('subjects', Set::combine($this->Lesson->Subject->find('all', array('order' => 'title')), "{n}.Subject.id","{n}.Subject.title")); |
|---|
| 126 | if ( !empty( $this->data['Lesson'] ) ): |
|---|
| 127 | Sanitize::paranoid($this->data['Lesson']['title']); |
|---|
| 128 | Sanitize::html($this->data['Lesson']['body']); |
|---|
| 129 | $this->data['Lesson']['user_id'] = (int) $this->Auth->user('id'); |
|---|
| 130 | if ($this->Lesson->save($this->data)): |
|---|
| 131 | if ( $this->data['Lesson']['end'] == 0 && !isset($this->data['Lesson']['id']) ): |
|---|
| 132 | $id = $this->Lesson->getLastInsertID(); |
|---|
| 133 | $this->msgFlash(__('Data saved', true), '/admin/lessons/edit/'.$id); |
|---|
| 134 | elseif ( $this->data['Lesson']['end'] == 0 && isset($this->data['Lesson']['id']) ): |
|---|
| 135 | $this->msgFlash(__('Data saved', true), '/admin/lessons/edit/'.$this->data['Lesson']['id']); |
|---|
| 136 | elseif ( $this->data['Lesson']['end'] == 1 ): |
|---|
| 137 | $this->msgFlash(__('Data saved', true), '/admin/lessons/listing'); |
|---|
| 138 | endif; |
|---|
| 139 | endif; |
|---|
| 140 | elseif( $lesson_id != null && intval($lesson_id)): |
|---|
| 141 | $this->data = $this->Lesson->read(null, $lesson_id); |
|---|
| 142 | endif; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | public function admin_change($lesson_id, $status) |
|---|
| 146 | { |
|---|
| 147 | $this->data['Lesson']['status'] = ($status == 0 ) ? 1 : 0; |
|---|
| 148 | |
|---|
| 149 | $this->data['Lesson']['id'] = (int) $lesson_id; |
|---|
| 150 | |
|---|
| 151 | if ($this->Lesson->save($this->data, false)): |
|---|
| 152 | $this->msgFlash(__('Status modified', true), '/admin/lessons/listing'); |
|---|
| 153 | endif; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | public function admin_delete($lesson_id) |
|---|
| 157 | { |
|---|
| 158 | if ($this->Lesson->del($lesson_id)): |
|---|
| 159 | $this->msgFlash(__('Data removed', true), '/admin/lessons/listing'); |
|---|
| 160 | endif; |
|---|
| 161 | } |
|---|
| 162 | } |
|---|
| 163 | ?> |
|---|