| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Karamelo E-Learning Platform |
|---|
| 4 | * Manuel Montoya 2002-2008 |
|---|
| 5 | * GPLv3 manuel<at>mononeurona<punto>org |
|---|
| 6 | * @version 0.3 |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | App::import('Sanitize'); |
|---|
| 10 | |
|---|
| 11 | class TopicsController extends AppController { |
|---|
| 12 | |
|---|
| 13 | public $helpers = array('Ajax', 'Time', 'Fck'); |
|---|
| 14 | |
|---|
| 15 | public $components = array('Edublog'); |
|---|
| 16 | |
|---|
| 17 | public function beforeFilter() |
|---|
| 18 | { |
|---|
| 19 | parent::beforeFilter(); |
|---|
| 20 | |
|---|
| 21 | if ( $this->Auth->user() ): |
|---|
| 22 | $this->Auth->allow(array('display', 'add', 'reply')); |
|---|
| 23 | endif; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | public function display($user_id, $forum_id, $topic_id) |
|---|
| 27 | { |
|---|
| 28 | $this->pageTitle = __('Discussion', true); |
|---|
| 29 | |
|---|
| 30 | $this->layout = $this->Edublog->layout($user_id); |
|---|
| 31 | |
|---|
| 32 | $this->Edublog->blog($user_id); |
|---|
| 33 | |
|---|
| 34 | $vclassroom_id = $this->Topic->Forum->field('vclassroom_id', array('Forum.id'=>$forum_id)); |
|---|
| 35 | // student belongst to this class? |
|---|
| 36 | if ( $this->Auth->user() ): |
|---|
| 37 | $this->set('belongs', $this->Topic->Forum->Vclassroom->belongs($this->Auth->user('id'), $vclassroom_id)); |
|---|
| 38 | else: |
|---|
| 39 | $this->set('belongs', false); |
|---|
| 40 | endif; |
|---|
| 41 | |
|---|
| 42 | $conditions = array("Topic.status"=>1, "Topic.id"=>$topic_id); |
|---|
| 43 | $fields = null; |
|---|
| 44 | $order = "Topic.created DESC"; |
|---|
| 45 | |
|---|
| 46 | $this->Topic->User->unbindModel(array('hasMany'=>array('Category', 'Faq', 'Lesson', 'Entry', 'Acquaintance', 'Vclassroom'))); |
|---|
| 47 | $this->Topic->Forum->unbindModel(array('hasMany'=>array('Topic'))); |
|---|
| 48 | $data = $this->Topic->find($conditions, $fields, $order, 2); |
|---|
| 49 | |
|---|
| 50 | $this->set('data',$data); |
|---|
| 51 | |
|---|
| 52 | if ( !$this->Session->check('topic'.$topic_id) ): // add 1 to visit |
|---|
| 53 | $this->Topic->addVisitor($topic_id, $this->Auth->user('id')); |
|---|
| 54 | $this->Session->write('topic'.$topic_id, $topic_id); //set session, only one visit per session |
|---|
| 55 | endif; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | public function add($vclassroom_id = null, $forum_id = null) |
|---|
| 59 | { |
|---|
| 60 | $this->layout='ajax'; |
|---|
| 61 | |
|---|
| 62 | if (!empty($this->data['Topic'])): |
|---|
| 63 | |
|---|
| 64 | Sanitize::clean($this->data['Topic']); |
|---|
| 65 | |
|---|
| 66 | $this->data['Topic']['user_id'] = $this->Auth->user('id'); |
|---|
| 67 | |
|---|
| 68 | if ($this->Topic->save($this->data)): |
|---|
| 69 | $this->msgFlash('Topic added', '/forums/display/'.$this->data['Topic']['forum_id'].'/'.$this->data['Topic']['vclassroom_id']); |
|---|
| 70 | endif; |
|---|
| 71 | else: |
|---|
| 72 | $this->set('forum_id', $forum_id); |
|---|
| 73 | $this->set('vclassroom_id', $vclassroom_id); |
|---|
| 74 | endif; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | public function reply($vclassroom_id, $forum_id, $topic_id, $blogger_id) |
|---|
| 78 | { |
|---|
| 79 | // adds new reply to topic |
|---|
| 80 | |
|---|
| 81 | $this->layout = 'ajax'; |
|---|
| 82 | |
|---|
| 83 | $this->set('forum_id', $forum_id); |
|---|
| 84 | $this->set('topic_id', $topic_id); |
|---|
| 85 | $this->set('blogger_id', $blogger_id); |
|---|
| 86 | $this->set('vclassroom_id', $vclassroom_id); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /*** === ADMIN METHODS ===********/ |
|---|
| 90 | public function admin_listing($topic_id) |
|---|
| 91 | { |
|---|
| 92 | $this->layout = 'admin'; |
|---|
| 93 | |
|---|
| 94 | $this->pageTitle = 'Topics'; |
|---|
| 95 | |
|---|
| 96 | $conditions = array("Topic.user_id"=>$this->Auth->user('id'), "Topic.id"=>$topic_id); |
|---|
| 97 | |
|---|
| 98 | $fields = array("id", "subject", "message", "status", "created"); |
|---|
| 99 | |
|---|
| 100 | $this->set('data', $this->Topic->find($conditions, $fields, null, 2)); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | public function admin_edit($topic_id = null) |
|---|
| 104 | { |
|---|
| 105 | if (empty($this->data['Topic'])): |
|---|
| 106 | $this->data = $this->Topic->read(null, $topic_id); |
|---|
| 107 | else: |
|---|
| 108 | if ($this->Topic->save($this->data['Topic'])): |
|---|
| 109 | $this->msgFlash('Your virtual classroom has been updated.','/vclassrooms/'); |
|---|
| 110 | endif; |
|---|
| 111 | endif; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | // change status published/draft |
|---|
| 115 | public function admin_change($id, $status, $forum_id) |
|---|
| 116 | { |
|---|
| 117 | if ( !intval($id) || !is_numeric($status) || !intval($forum_id) ) |
|---|
| 118 | { |
|---|
| 119 | $this->redirect('/'); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | $this->data['Topic']['status'] = ($status == 0 ) ? 1 : 0; |
|---|
| 123 | |
|---|
| 124 | $this->data['Topic']['id'] = $id; |
|---|
| 125 | |
|---|
| 126 | if ($this->Topic->save($this->data['Topic'], array('validate'=>false))): |
|---|
| 127 | $this->msgFlash(__('Status modified', true), '/admin/forums/topics/'.$forum_id); |
|---|
| 128 | endif; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | public function admin_reply($reply_id) |
|---|
| 132 | { |
|---|
| 133 | $this->layout = 'popup'; |
|---|
| 134 | |
|---|
| 135 | $conditions = array('Reply.id'=>$reply_id); |
|---|
| 136 | |
|---|
| 137 | $this->set('data', $this->Topic->Reply->find($conditions)); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | public function admin_delete($topic_id, $forum_id) |
|---|
| 141 | { |
|---|
| 142 | if ( $this->Topic->del($topic_id) ): |
|---|
| 143 | $this->msgFlash('Topic deleted','/admin/forums/topics/'.$forum_id); |
|---|
| 144 | endif; |
|---|
| 145 | } |
|---|
| 146 | } |
|---|