PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/reason_4.0/lib/core/minisite_templates/modules/gallery_vote/module.php

https://github.com/luthercollege/reason_package
PHP | 420 lines | 365 code | 27 blank | 28 comment | 52 complexity | d7ccf18756996718576077ba82dd20ce MD5 | raw file
  1. <?php
  2. /**
  3. * @package reason
  4. * @subpackage minisite_modules
  5. */
  6. /**
  7. * Include the parent class & other dependencies
  8. */
  9. reason_include_once( 'minisite_templates/modules/gallery.php' );
  10. reason_include_once( 'minisite_templates/modules/gallery_vote/vote_form.php' );
  11. reason_include_once( 'function_libraries/admin_actions.php');
  12. reason_include_once( 'classes/csv.php');
  13. /**
  14. * Register the module with Reason
  15. */
  16. $GLOBALS[ '_module_class_names' ][ 'gallery_vote' ] = 'GalleryVoteModule';
  17. /**
  18. * A minisite module that enables voting on the photos for logged-in users
  19. */
  20. class GalleryVoteModule extends GalleryModule
  21. {
  22. var $user_netID; // current user netid
  23. var $csv_auth; // CSV authentication connection
  24. var $csv_data; // CSV data connection
  25. var $rows = 2000; // effectively disables pagination
  26. var $modes = array('gallery', 'vote', 'results');
  27. var $modes_to_nice_names = array('gallery'=>'View Photos', 'vote'=>'Vote', 'results'=>'View Results');
  28. var $mode_checkers = array('vote'=>'voting_is_available', 'results'=>'results_are_viewable');
  29. var $mode_requires_login_checkers = array('vote'=>'voting_requires_login','results'=>'results_require_login');
  30. function get_cleanup_rules()
  31. {
  32. $cr = parent::get_cleanup_rules();
  33. $cr['mode'] = array('function' => 'check_against_array', 'extra_args' => $this->modes );
  34. return $cr;
  35. }
  36. function init( $args = array() )
  37. {
  38. if (empty($this->request['mode'])) $this->request['mode'] = 'gallery'; // default mode is gallery
  39. if(!$this->get_authentication() && $this->mode_requires_login($this->request['mode']))
  40. {
  41. header('Location: '.REASON_LOGIN_URL.'?dest_page='.urlencode(get_current_url()));
  42. die();
  43. }
  44. $this->parent->add_stylesheet(REASON_HTTP_BASE_PATH.'css/image_gallery/gallery_vote.css');
  45. //path to CSV data
  46. if (!defined('REASON_CSV_DIR'))
  47. {
  48. trigger_error('REASON_CSV_DIR path is not defined in settings.php');
  49. }
  50. else
  51. {
  52. //establish csv connection
  53. $this->csv_connect(REASON_CSV_DIR . 'gallery_vote/');
  54. }
  55. if ($this->request['mode'] == 'vote' || $this->request['mode'] == 'results')
  56. {
  57. $es = new entity_selector();
  58. $es->description = 'Selecting images for the gallery';
  59. $es->add_type( id_of('image') );
  60. $es = $this->refine_es( $es );
  61. $this->images = $es->run_one();
  62. //attempt to populate csv_netID
  63. $this->get_authentication();
  64. }
  65. else parent::init($args);
  66. }
  67. function refine_es($es)
  68. {
  69. //if ($this->request['mode'] == 'gallery')
  70. //{
  71. // return parent::refine_es($es);
  72. //}
  73. //else
  74. //{
  75. $es->set_env( 'site' , $this->site_id );
  76. $es->add_right_relationship($this->parent->cur_page->id(), relationship_id_of('minisite_page_to_image'));
  77. $es->set_order( 'RAND()' );
  78. return $es;
  79. //}
  80. }
  81. function csv_connect($path)
  82. {
  83. $auth_filename = $this->parent->cur_page->id() . '_auth.csv';
  84. $data_filename = $this->parent->cur_page->id() . '_data.csv';
  85. $this->csv_auth = new CSV($path . $auth_filename);
  86. $this->csv_data = new CSV($path . $data_filename);
  87. }
  88. function run()
  89. {
  90. echo '<div id="galleryVote">';
  91. echo $this->get_nav();
  92. if ($this->request['mode'] == 'vote' )
  93. {
  94. $this->run_vote_view();
  95. }
  96. elseif( $this->request['mode'] == 'results')
  97. {
  98. $this->run_results_view();
  99. }
  100. else
  101. {
  102. parent::run();
  103. }
  104. echo '</div>';
  105. }
  106. function has_content()
  107. {
  108. if (count($this->images) > 0) return true;
  109. else return false;
  110. }
  111. /**
  112. * Returns the current user's netID, or false if the user is not logged in.
  113. * @return string user's netID
  114. */
  115. function save_vote($ballot, $csvNetIDs)
  116. {
  117. if (in_array($this->user_netID, $csvNetIDs) == false)
  118. {
  119. $auth[] = $this->user_netID;
  120. $data[] = $ballot->get_value('image_choice');
  121. if ($this->csv_auth->appendRow($auth) && $this->csv_data->appendRow($data)) return 'saved';
  122. else
  123. {
  124. trigger_error('error saving vote for user ' . $this->user_netID . ' - gallery_vote module at URL ' . get_current_url());
  125. return 'error';
  126. }
  127. }
  128. else return 'already_voted';
  129. }
  130. function get_authentication()
  131. {
  132. if(empty($this->user_netID))
  133. {
  134. if(!empty($_SERVER['REMOTE_USER']))
  135. {
  136. $this->user_netID = $_SERVER['REMOTE_USER'];
  137. return $this->user_netID;
  138. }
  139. else
  140. {
  141. return $this->get_authentication_from_session();
  142. }
  143. }
  144. else
  145. {
  146. return $this->user_netID;
  147. }
  148. }
  149. function get_authentication_from_session()
  150. {
  151. $this->session =& get_reason_session();
  152. if($this->session->exists())
  153. {
  154. force_secure_if_available();
  155. if( !$this->session->has_started() )
  156. {
  157. $this->session->start();
  158. }
  159. $this->user_netID = $this->session->get( 'username' );
  160. return $this->user_netID;
  161. }
  162. else
  163. {
  164. return false;
  165. }
  166. }
  167. function mode_is_available($mode)
  168. {
  169. if(isset($this->mode_checkers[$mode]))
  170. {
  171. $function = $this->mode_checkers[$mode];
  172. return $this->$function();
  173. }
  174. return true;
  175. }
  176. function mode_requires_login($mode)
  177. {
  178. if(isset($this->mode_requires_login_checkers[$mode]))
  179. {
  180. $function = $this->mode_requires_login_checkers[$mode];
  181. return $this->$function();
  182. }
  183. return false;
  184. }
  185. function voting_is_available()
  186. {
  187. if($this->cur_user_has_voted())
  188. return false;
  189. return true;
  190. }
  191. function voting_requires_login()
  192. {
  193. return true;
  194. }
  195. function results_require_login()
  196. {
  197. return true;
  198. }
  199. function get_nav()
  200. {
  201. $sess_auth = $this->get_authentication_from_session();
  202. $auth = $this->get_authentication();
  203. $ret = '<div id="galleryVoteNav">';
  204. $nav_parts = array();
  205. foreach($this->modes as $mode)
  206. {
  207. if($this->request['mode'] == $mode)
  208. {
  209. $nav_parts[] = '<strong>'.$this->modes_to_nice_names[$mode].'</strong>';
  210. }
  211. elseif($this->mode_is_available($mode))
  212. {
  213. $nav_parts[] = '<a href="'.carl_make_link(array('mode'=>$mode)).'">'.$this->modes_to_nice_names[$mode].'</a>';
  214. }
  215. }
  216. $ret .= implode(' | ',$nav_parts);
  217. $ret .= ' | ' . $this->get_login_logout_link($sess_auth, $auth);
  218. $ret .= '</div>'."\n";
  219. return $ret;
  220. }
  221. function get_login_logout_link($sess_auth = '', $auth = '')
  222. {
  223. if (empty($sess_auth)) $sess_auth = $this->get_authentication_from_session();
  224. if (empty($auth)) $auth = $this->get_authentication();
  225. $ret = '<span class="loginlogout">';
  226. if(!empty($sess_auth))
  227. {
  228. if($this->mode_requires_login($this->request['mode']))
  229. {
  230. $url = carl_make_link( array('mode'=>''), '', '', false );
  231. }
  232. else
  233. {
  234. $url = get_current_url();
  235. }
  236. $ret .= '<a href="'.REASON_LOGIN_URL.'?logout=true&dest_page='.urlencode($url).'">Log Out</a> (Currently Logged In: '.$sess_auth.')';
  237. }
  238. elseif(!empty($auth))
  239. {
  240. $ret .= 'Currently Logged In: '.$auth;
  241. }
  242. else
  243. {
  244. $ret .= '<a href="'.REASON_LOGIN_URL.'">Log In</a>';
  245. }
  246. $ret .= '</span>'."\n";
  247. //return '<p>'.$ret.'</p>';
  248. return $ret;
  249. }
  250. function run_results_view()
  251. {
  252. echo '<div id="galleryVote">';
  253. echo '<h3>Results</h3>'."\n";
  254. $netid = $this->get_authentication();
  255. if(empty($netid))
  256. {
  257. echo '<p>You must be logged in to view results</p>'."\n";
  258. }
  259. else
  260. {
  261. if($this->user_can_view_results($netid))
  262. {
  263. $votes = array();
  264. $num_votes = 0;
  265. foreach($this->csv_data->csv_to_array() as $row)
  266. {
  267. $num_votes++;
  268. if(!isset($votes[$row[0]]))
  269. $votes[$row[0]] = 1;
  270. else
  271. $votes[$row[0]]++;
  272. if(isset($this->images[$row[0]]))
  273. unset($this->images[$row[0]]);
  274. }
  275. foreach($this->images as $id=>$item)
  276. {
  277. $votes[$id] = 0;
  278. }
  279. arsort($votes);
  280. echo '<p><strong>Total votes:</strong> '.$num_votes.'</p>'."\n";
  281. echo '<table style="width:100%">'."\n";
  282. echo '<tr><th>Image</th><th>Votes</th></tr>'."\n";
  283. reset($votes);
  284. $max = current($votes);
  285. foreach($votes as $id=>$num)
  286. {
  287. echo '<tr><td style="width:25%">';
  288. show_image($id);
  289. echo '</td><td>';
  290. if($num == 0 || $max == 0)
  291. {
  292. echo '<strong>0</strong>';
  293. }
  294. else
  295. {
  296. echo '<div style="padding:.5em 0;float:left;background-color:#008;color:#fff;text-align:right;width:' . round($num/$max*100, 2) . '%;"><strong style="padding-right:.5em;">'.$num.'</strong></div>';
  297. }
  298. echo '</td></tr>'."\n";
  299. }
  300. echo '</table>'."\n";
  301. }
  302. else
  303. {
  304. echo '<p>You must do not have permission to view the results</p>'."\n";
  305. }
  306. }
  307. echo '</div>'."\n";
  308. }
  309. function run_vote_view()
  310. {
  311. if (defined('REASON_CSV_DIR'))
  312. {
  313. $csv_netIDs = array_map(create_function('$array', 'return "$array[0]";'), $this->csv_auth->csv_to_array());
  314. if (empty($this->user_netID) && $this->voting_requires_login())
  315. {
  316. echo '<h3>Login Required</h3>';
  317. echo '<p>This page requires you to login using a valid '.SHORT_ORGANIZATION_NAME.' netID.</p>';
  318. }
  319. elseif ($this->cur_user_has_voted())
  320. {
  321. echo '<h3>Sorry</h3><p>You may only vote once and have already voted.</p>';
  322. }
  323. else
  324. {
  325. $ballot = new GalleryVoteForm($this->images);
  326. $ballot->init();
  327. $ballot->run();
  328. if ($ballot->submitted)
  329. {
  330. $result = $this->save_vote($ballot, $csv_netIDs);
  331. switch ($result) {
  332. case 'saved':
  333. echo '<h3>Success</h3><p>Your vote has been recorded - thank you for participating.</p>';
  334. break;
  335. case 'already_voted':
  336. echo '<h3>Error</h3><p>Your vote cannot be saved because you have already voted.</p>';
  337. break;
  338. case 'error':
  339. echo '<h3>Error</h3><p>There was an error saving your vote - the Web Services group has been notified. Please try again later.</p>';
  340. break;
  341. }
  342. }
  343. }
  344. }
  345. else
  346. {
  347. trigger_error('REASON_CSV_DIR must be defined for image voting to work');
  348. echo '<h3>Sorry; Voting is not available.</h3><p>The administrator of this web server needs to turn it on -- please contact the administrator of the web site and tell them the information below:</p>
  349. <p>Image voting needs to have the Reason setting REASON_CSV_DIR defined in order to work. Please go into the Reason settings file and specify the directory for the csv data.</p>';
  350. }
  351. }
  352. function cur_user_has_voted()
  353. {
  354. if(!empty($this->user_netID))
  355. {
  356. $auth_log = $this->csv_auth->csv_to_array();
  357. foreach($auth_log as $row)
  358. {
  359. if($row[0] == $this->user_netID)
  360. return true;
  361. }
  362. }
  363. return false;
  364. }
  365. function results_are_viewable()
  366. {
  367. if($this->user_can_view_results($this->get_authentication()))
  368. {
  369. return true;
  370. }
  371. }
  372. function user_can_view_results($netid)
  373. {
  374. $user_reason_id = get_user_id( $netid );
  375. if(!empty($user_reason_id))
  376. {
  377. $es = new entity_selector();
  378. $es->add_type(id_of('site'));
  379. $es->add_left_relationship($user_reason_id, relationship_id_of('site_to_user'));
  380. $es->set_num(1);
  381. $es->limit_tables();
  382. $es->limit_fields();
  383. $es->add_relation('entity.id = "'.$this->site_id.'"');
  384. $sites = $es->run_one();
  385. if(!empty($sites))
  386. {
  387. return true;
  388. }
  389. }
  390. return false;
  391. }
  392. }
  393. ?>