Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. public function actionEventList($pagin = null)
  2. {
  3. $pagin = isset($_REQUEST['pagin']) ? $_REQUEST['pagin']: null;
  4. /* find all lectors & admins as array id=>fullName*/
  5. $queryLectorAdmin = new Query();
  6. $lectorsRows = $queryLectorAdmin->select(['id','username','secondname','role'])
  7. ->from('cultusers')
  8. ->where(['role'=>[Cultusers::ROLE_LECTOR, Cultusers::ROLE_MODERATOR]])->all();
  9. $lectorsArr = [];
  10. $adminsArr = [];
  11. foreach($lectorsRows as $row) {
  12. $username = array_key_exists('username',$row) ? $row['username'] : null;
  13. $secondname = array_key_exists('secondname',$row) ? $row['secondname'] : null;
  14. $id = (string)$row['_id'];
  15. if($row['role'] == 5){
  16. $lectorsArr[$id] = $username.' '.$secondname;
  17. } elseif($row['role'] == 10){
  18. $adminsArr[$id] = $username.' '.$secondname;
  19. }
  20. }
  21. /* END find all lectors & admins as array id=>fullName*/
  22.  
  23. /* find all courses array id=>courseName */
  24. $queryCourse = new Query();
  25. $courseRows = $queryCourse ->select(['id','course','season.name'])->from('courses')->all();
  26.  
  27. $coursesArr =[];
  28. foreach($courseRows as $courseRow){
  29. $id = (string)$courseRow['_id'];
  30. $courseName = $courseRow['course'].' ('.$courseRow['season']['name'].')';
  31. $coursesArr[$id] = $courseName;
  32. }
  33. /* END find all courses array id=>courseName */
  34. /* find all places array id=>placeName */
  35. $queryPlace = new Query();
  36. $placesRows = $queryPlace->select(['_id','place'])->from('places')->all();
  37. $placesArr = [];
  38. foreach($placesRows as $placeRow){
  39. $id = (string)$placeRow['_id'];
  40. $placeName = $placeRow['place'];
  41. $placesArr[$id] = $placeName;
  42. }
  43. /* END find all places array id=>placeName */
  44.  
  45. $lections = \common\models\Lections::find()->indexBy('_id')
  46. // ->where(
  47. // [
  48. // 'season.name' => [Lections::season(date('Y', time()) - 2009),
  49. // Lections::season(date('Y', time()) - 2010)] // "2014-2015"
  50. // ]
  51. // )
  52. ->orderBy(['dates' => SORT_DESC])
  53. ->limit(40)->offset($pagin)
  54. ->all();
  55. $lectionIds = array_keys($lections);
  56. $lec = '';
  57. foreach($lectionIds as $l) {
  58. $lec .= sprintf("'%s'".', ',$l);
  59. }
  60. $lec = trim($lec, ", ");
  61.  
  62. $db = Yii::$app->db;
  63. $msql = "SELECT lection_id, lection_timestamp, admin_id, COUNT( lection_id ) AS visit_count
  64. FROM `visit`
  65. WHERE lection_id IN($lec)
  66. GROUP BY lection_id";
  67. $command = $db->createCommand($msql);
  68. $result = $command->queryAll();
  69. $command->execute();
  70.  
  71. $lectionAdmins = [];
  72. foreach($result as $rezRow){
  73. $lectionAdmins[$rezRow['lection_id']] =
  74. array_key_exists($rezRow['admin_id'],$adminsArr) ?
  75. $adminsArr[$rezRow['admin_id']] : '';
  76. $lectionAdmins = array_filter($lectionAdmins);
  77. }
  78.  
  79. $lectorIds = array_map(
  80. function ($lection) {
  81. return $lection->lectorId;
  82. },
  83. $lections
  84. );
  85.  
  86. $lectionLectors = [];
  87. foreach ($lectorIds as $lectionId => $lectorId) {
  88. foreach ($lectorId as $val) {
  89. $lectorName = array_key_exists($val, $lectorsArr) ? $lectorsArr[$val] : '' ;
  90. if(array_key_exists($lectionId, $lectionLectors)){
  91. $lectionLectors[$lectionId] = $lectionLectors[$lectionId].' '. $lectorName ;
  92. $lectionLectors = array_filter($lectionLectors);
  93. } else {
  94. $lectionLectors[$lectionId] = $lectorName ;
  95. $lectionLectors = array_filter($lectionLectors);
  96. }
  97.  
  98. }
  99. }
  100.  
  101. $coursesIds = array_map(
  102. function ($lection) {
  103. return $lection->course;
  104. },
  105. $lections
  106. );
  107.  
  108. $lectionCourses = [];
  109. foreach ($coursesIds as $lectionId => $courseId) {
  110. $lectionCourses[$lectionId] = array_key_exists($courseId,$coursesArr) ?
  111. $coursesArr[$courseId] : '';
  112. $lectionCourses = array_filter($lectionCourses);
  113. }
  114.  
  115. $placesIds = array_map(
  116. function ($lection) {
  117. return $lection->place;
  118. },
  119. $lections
  120. );
  121. $placesIds = array_filter($placesIds);
  122. $lectionPlaces = [];
  123. foreach($placesIds as $lectionId => $placeId){
  124. $lectionPlaces[$lectionId] = array_key_exists($placeId, $placesArr) ?
  125. $placesArr[$placeId] : '';
  126. $lectionPlaces = array_filter($lectionPlaces);
  127. }
  128.  
  129.  
  130.  
  131.  
  132. if(\Yii::$app->request->method == 'POST'){
  133.  
  134. $resp = $this->renderPartial("add-evlist",
  135. [
  136. 'result' => $result,
  137. 'lections' => $lections,
  138. 'lectionAdmins' => $lectionAdmins,
  139. 'lectionLectors' => $lectionLectors,
  140. 'lectionCourses' => $lectionCourses,
  141. 'lectionPlaces' => $lectionPlaces,
  142. 'now' => $now = time()
  143. ]);
  144.  
  145. return $resp;
  146. } else {
  147.  
  148. return $this->render(
  149. 'event-list',
  150. [
  151. 'admins' => $adminsArr,
  152. 'places' => $placesArr,
  153. 'courses' => $coursesArr,
  154. 'lectors' => $lectorsArr,
  155. 'result' => $result,
  156. 'lections' => $lections,
  157. 'lectionAdmins' => $lectionAdmins,
  158. 'lectionLectors' => $lectionLectors,
  159. 'lectionCourses' => $lectionCourses,
  160. 'lectionPlaces' => $lectionPlaces,
  161. 'now' => $now = time()
  162. ]
  163. );
  164.  
  165. }
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement