PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/RP_VOTE_915/trunk/system/application/controllers/question.php

https://github.com/holsinger/openfloor
PHP | 300 lines | 214 code | 46 blank | 40 comment | 46 complexity | 96e9114efc3d534ee5f1418b91b637b6 MD5 | raw file
  1. <?php
  2. class Question extends Controller
  3. {
  4. function __construct()
  5. {
  6. parent::Controller();
  7. $this->load->model('Tag_model','tag');
  8. $this->load->model('Question_model','question');
  9. $this->load->model('Event_model','event');
  10. $this->load->model('Vote_model','vote');
  11. $this->load->model('flag_model','flag');
  12. $this->load->library('validation');
  13. $this->load->library('time_lib');
  14. $this->load->library('tag_lib');
  15. $this->load->library('flag_lib');
  16. $this->load->helper('url');//for redirect
  17. $this->load->helper('form');
  18. $this->load->scaffolding('cn_questions');
  19. }
  20. function index () {
  21. $this->add();
  22. }
  23. function add()
  24. {
  25. #check that user is allowed
  26. $this->userauth->check();
  27. //make sure there is an event id
  28. //get the event id
  29. $uri_array = $this->uri->uri_to_assoc(3);
  30. $data['event_url'] = $this->uri->assoc_to_uri(array('event'=>$uri_array['event']));
  31. if (isset($uri_array['sort'])) $data['event_url'] = $this->uri->assoc_to_uri(array('event'=>$uri_array['event'],'sort'=>$uri_array['sort']));
  32. //event
  33. if (isset($uri_array['event']))
  34. {
  35. $data['breadcrumb'] = array('Home'=>$this->config->site_url(),'Events'=>'event/',ucwords(str_replace('_',' ',$uri_array['event']))=>"forums/queue/{$data['event_url']}");
  36. $uri_array = $this->event->get_event(0,$uri_array['event']);
  37. $data['event_id'] = $uri_array['event_id'];
  38. $data['event_name'] = $uri_array['event_name'];
  39. $data['event_type'] = $uri_array['event_type'];
  40. }
  41. else
  42. {
  43. $data['event_id'] = 0;
  44. $data['event_name'] = '';
  45. }
  46. $data['error'] = '';
  47. #FORM VALIDATE
  48. if (isset($_POST['event']) && $_POST['event']=='0') $data['error'] .= 'Please select an Event.<br />';
  49. $rules['event'] = "required";
  50. $rules['question'] = "trim|required|min_length[10]|max_length[150]|xss_clean";
  51. $rules['desc'] = "trim|max_length[255]|xss_clean";
  52. $rules['tags'] = "trim|strtolower|xss_clean";
  53. $this->validation->set_rules($rules);
  54. if ($this->validation->run() == FALSE) {
  55. $data['error'] .= $this->validation->error_string;
  56. } else {
  57. $questionID = $this->addQuestion();
  58. if( is_numeric($questionID) ) {
  59. $this->voteup($questionID);
  60. if(isset($_POST['ajax'])) {
  61. echo 'success'; exit();
  62. } else {
  63. //redirect to question view page
  64. redirect('forums/queue/'.$_POST['event_url']);
  65. ob_clean();
  66. exit();
  67. }
  68. } else {
  69. $data['error'] = 'Error Adding Question';
  70. }
  71. }
  72. //this makes the info sticky
  73. $fields['event'] = ( isset($_POST['event']) ) ? $_POST['event']:"";
  74. $fields['question'] = ( isset($_POST['question']) ) ? $_POST['question']:"";
  75. $fields['desc'] = ( isset($_POST['desc']) ) ? $_POST['desc']:"";
  76. $fields['tags'] = ( isset($_POST['tags']) ) ? $_POST['tags']:"";
  77. if(isset($_POST['ajax'])) foreach($fields as $k => $v) $fields[$k] = '';
  78. $this->validation->set_fields($fields);
  79. #$data['events'] = $this->populateEventsSelect();
  80. if(isset($_POST['ajax'])) {
  81. $data['ajax'] = true;
  82. $this->load->view('question/_submit_question_form', $data);
  83. }
  84. else $this->load->view('question/submit_question', $data);
  85. }
  86. function addQuestion()
  87. {
  88. #check that user is allowed
  89. $this->userauth->check();
  90. $eventID = $_POST['event'];
  91. $userID = $this->userauth->user_id;
  92. $questionName = $_POST['question'];
  93. $questionDesc = $_POST['desc'];
  94. $tags = $_POST['tags'];
  95. /* deal with tags first */
  96. $tags = str_replace(array(/*' ', */"\t", '.', ','), '', $tags);
  97. //make sure we have some tags
  98. if (!empty($tags)) {
  99. $tagsExist = true;
  100. $a = explode(' ',$tags);
  101. $tags = array();
  102. foreach($a as $v) if(!empty($v)) $tags[] = $v;
  103. $query = $this->tag->getTagsInSet($tags);
  104. $existingKs = array();
  105. $existingVs = array();
  106. foreach($query->result_array() as $row)
  107. {
  108. $existingKs[] = $row['tag_id'];
  109. $existingVs[] = $row['value'];
  110. }
  111. $diff = array_diff($tags, $existingVs);
  112. $newKs = array();
  113. if(!empty($diff)) foreach($diff as $v) if($k=$this->tag->insertTag($v)) $newKs[] = $k;
  114. $newKs = array_merge($newKs, $existingKs);
  115. }
  116. /* insert the question*/
  117. $questionID = $this->question->insertQuestion($questionName, $questionDesc, $userID, $eventID,url_title($questionName));
  118. /* insert proper associations */
  119. if(isset($tagsExist)) if(isset($questionID)) foreach($newKs as $v) $this->tag->insertTagAssociation($questionID,0, $v, $userID);
  120. return $questionID;
  121. }
  122. function edit($question_id)
  123. {
  124. $data['error'] = "";
  125. $oldCurrent = 0;
  126. if ($_POST){
  127. // Change status, changed is returned how many rows were affected
  128. $changed = $this->question->updateQuestion($_POST['question_id'], $_POST);
  129. // If changed to current, we have to be sure that there are no other current questions.
  130. // if there is a current questions, then change to 'asked' since this question now replaces it
  131. if ($_POST['question_status'] == 'current') {
  132. $question_id =$_POST['question_id'];
  133. $event_id = $this->event->get_id_from_url($_POST['event_url_name']);
  134. $oldCurrent = $this->question->singleCurrent($event_id, $question_id);
  135. }
  136. if ($changed > 0){
  137. $array = $this->question->get_question($_POST['question_id']);
  138. $data['error'] = "'{$array['question_name']}' changed been updated";
  139. }
  140. }
  141. if($oldCurrent > 0 && $oldCurrent != $_POST['question_id']) {
  142. $question_id = $oldCurrent;
  143. $array = $this->question->get_question($question_id);
  144. $this->question->set_asked_time($question_id);
  145. $data['error'] .= "<br />{$array['question_name']} changed to 'Asked'";
  146. }
  147. $data['question'] = $this->question->get_question($question_id);
  148. $data['event'] = $this->event->get_event($data['question']['fk_event_id']);
  149. $this->load->view('view_edit_question', $data);
  150. }
  151. function populateEventsSelect()
  152. {
  153. $events = $this->event->getEvents();
  154. $output='';
  155. foreach($events as $v) $output .= "<option value=\"{$v['event_id']}\" ". $this->validation->set_select('event', $v['event_id']) .">{$v['event_name']}</option>";
  156. return $output;
  157. }
  158. function view ($event, $question, $sort = 'date')
  159. {
  160. // how to sort comments
  161. switch ($sort) {
  162. case 'votes':
  163. $sort = 'votes';
  164. break;
  165. default:
  166. $sort = 'date';
  167. break;
  168. }
  169. $event_id = $this->event->get_id_from_url($event);
  170. if(!$event_id) redirect();
  171. $this->question->event_id = $event_id;
  172. $question_id = $this->question->get_id_from_url($question);
  173. $this->question->question_status = null;
  174. $this->question->question_id = $question_id;
  175. $result = $this->question->questionQueue();
  176. $data = $result[0];
  177. $data['event_type'] = 'question';
  178. $image_array = unserialize($data['user_avatar']);
  179. if ($image_array) $data['avatar_path'] = "./avatars/".$image_array['file_name'];
  180. else $data['avatar_path'] = "./images/image01.jpg";
  181. // set display name
  182. $data['display_name'] = $this->user->displayName($data['user_name']);
  183. //get time diff
  184. $data['time_diff'] = $this->time_lib->getDecay($data['date']);
  185. //tags
  186. if(!empty($data['tags'])) foreach($data['tags'] as $k1=>$tag) $data['tags'][$k1] = anchor("forums/queue/event/".url_title($data['event_name'])."/tag/".$tag,$tag);
  187. //get voted
  188. if ($this->userauth->isUser()) {
  189. $this->vote->type='question';
  190. $score = $this->vote->votedScore($data['question_id'],$this->userauth->user_id);
  191. if ($score > 0) $data['voted'] = 'up';
  192. else if ($score < 0) $data['voted'] = 'down';
  193. else $data['voted'] = false;
  194. } else $data['voted'] = false;
  195. $this->load->library('comments_library');
  196. $comments_library = new Comments_library();
  197. $comments_library->ajax = isset($_POST['ajax']);
  198. $comments_library->sort = $sort;
  199. $comments_library->type = $data['event_type'];
  200. $data['comments_body'] = $comments_library->createComments($result[0]);
  201. if(isset($_POST['ajax'])) {
  202. $data['ajax'] = true;
  203. $this->load->view('question/_comments', $data);
  204. }
  205. $data['breadcrumb'] = array('Home'=>$this->config->site_url(),'Events'=>'event/',ucwords(str_replace('_',' ',$data['event_name']))=>"forums/queue/event/".url_title($data['event_name']));
  206. $data['rightpods'] = array('dynamic'=>array('event_description'=>$data['event_desc'],'event_location'=>$data['location']));
  207. $data['view_name'] = 'question_view';
  208. $this->load->view('question/question_view.php', $data);
  209. }
  210. /*
  211. function voteup($question_id = 0)
  212. {
  213. $this->vote($question_id, true);
  214. }
  215. function votedown($question_id = 0)
  216. {
  217. $this->vote($question_id, false);
  218. }
  219. */
  220. function vote($event_name, $question_name_or_id, $vote_cast)
  221. {
  222. $this->userauth->check(); // prevents voting unless authorized
  223. // get event id
  224. $event_id = $this->event->get_id_from_url($event_name);
  225. if(!$event_id) return;
  226. $this->question->event_id = $event_id; // Assign to the model
  227. // Question info can be passed by id or name, each type requires a different approach
  228. if (isset($question_name_or_id) && is_numeric($question_name_or_id)) $id = $question_name_or_id;
  229. if (isset($question_name_or_id) && is_string($question_name_or_id)) $id = $this->question->get_id_from_url($question_name_or_id);
  230. $id = ($question_id > 0) ? $question_name_or_id : $id;
  231. #check that user has not voted
  232. // if(!$this->userauth->check() || $this->vote->alreadyVoted($id, $this->userauth->user_id))
  233. // $this->vote->deleteVote($id, $this->userauth->user_id);
  234. #TODO validation and trending need to be considered
  235. $this->vote->vote($this->userauth->user_id, $id, $vote_cast);
  236. }
  237. function getTotalVoteSum($event_name, $question_name, $ajax_on){
  238. // Question info can be passed by id or name, each type requires a different approach
  239. $id = $this->question->get_id_from_url($question_name);
  240. error_log($id);
  241. if($ajax_on == 'true'){
  242. header("Cache-Control: no-cache, must-revalidate"); // Because IE will cache the results otherwise
  243. echo $this->vote->voteSum($id);
  244. }else{
  245. return $this->vote->voteSum($id);
  246. }
  247. }
  248. // Currently used for ajax only
  249. function getVoteCount($question_id){
  250. echo($this->vote->voteCount($question_id));
  251. }
  252. }
  253. ?>