Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. <?php
  2.  
  3. namespace common\models;
  4. use yii\mongodb\ActiveRecord;
  5. use yii\web\IdentityInterface;
  6. use common\models\Discipline;
  7. use common\models\Lections;
  8. use common\models\Cultusers;
  9. use yii\helpers;
  10. use Yii;
  11.  
  12.  
  13.  
  14.  
  15. class Programs extends ActiveRecord
  16. {
  17.  
  18. public $file;
  19.  
  20. public function attributes()
  21. {
  22.  
  23. return ['_id' ,
  24. 'program',
  25. 'icon',
  26. 'subtitle',
  27. 'enddate',
  28.  
  29.  
  30. 'about',
  31. 'bibliography',
  32. 'file'
  33.  
  34. ];
  35.  
  36. }
  37.  
  38. public function rules()
  39. {
  40. return [
  41. [['program'], 'required'],
  42. [[ 'subtitle', 'enddate', 'about', 'bibliography', ],'safe'],
  43. [['file'], 'file','extensions' => 'gif, jpg, png, jpeg',]
  44. ];
  45. }
  46.  
  47.  
  48. public static function findIdentity($id)
  49. {
  50. if(!($id instanceof \MongoId)){
  51. $id = new \MongoId($id);
  52. }
  53. return static::findOne(['_id'=>$id]);
  54. }
  55.  
  56. public function getDisciplines()
  57. {
  58. $disciolines = Discipline::find()
  59. ->where(['program' => $this->program])->all();
  60.  
  61. return $disciolines;
  62. }
  63.  
  64. public function getLections()
  65. {
  66. $lections = Lections::find()->where(['program' => $this->program])->all();
  67. return $lections;
  68. }
  69.  
  70. public function getLecionsDates()
  71. {
  72. $lectionsDates=array();
  73. foreach($this->getLections() as $value) {
  74. $lectionsDates[] = $value->begindate;}
  75. return $lectionsDates;
  76. }
  77.  
  78. public function getBeginDate()
  79. {
  80.  
  81. $beginDate = min($this->getLecionsDates())->sec;
  82. return $beginDate;
  83. }
  84.  
  85. public function dateToView($sec)
  86. {
  87. return date('d-m-Y', $sec);
  88. }
  89. public function getLastLecDate()
  90. {
  91. $lastLecDate = array_filter($this->getLecionsDates(),
  92. function($var)
  93. {
  94. $now = new \DateTime();
  95. if($var->sec < $now->getTimestamp()){
  96. return true;
  97. }
  98. }) ;
  99. return max($lastLecDate);
  100. }
  101.  
  102. public function getNextLecDate()
  103. {
  104. $nextLecDate = array_filter($this->getLecionsDates(),
  105. function($var)
  106. {
  107. $now = new \DateTime();
  108. if($var->sec > $now->getTimestamp()){
  109. return true;
  110. }
  111. }) ;
  112. if($nextLecDate != false) {
  113. return min($nextLecDate);
  114. }else{
  115.  
  116. return $this->getLastLecDate();
  117. }
  118. }
  119.  
  120. public function getLastLec()
  121. {
  122. $getLastLec = null;
  123. foreach($this->getLections() as $lection) {
  124. if($lection->begindate->sec === $this->getLastLecDate()->sec){
  125. $getLastLec = $lection;
  126. }
  127. }
  128. return $getLastLec;
  129. }
  130. public function getNextLec()
  131. {
  132. $getLastLec = null;
  133. foreach($this->getLections() as $lection) {
  134. if($lection->begindate->sec === $this->getNextLecDate()->sec){
  135. $getLastLec = $lection;
  136. }
  137. }
  138. return $getLastLec;
  139. }
  140.  
  141.  
  142. public function getLectors()
  143. {
  144. $lections = $this->getLections();
  145. $lectorIds = array_map(function($lection){
  146. return $lection->lectorId;
  147. }, $lections);
  148. $lectors = Cultusers::find()->where(['role'=>Cultusers::ROLE_LECTOR, '_id' => $lectorIds])->all();
  149. return $lectors;
  150.  
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement