/** * This is a cron that sets the next subject to be showed * and the next subject to be cached(subject_id and subject_id_2) * */ public function actionSetNextSubject() { if ($_SERVER['REMOTE_ADDR'] != '127.0.0.1') { die; } //Only allow to run this locally $command = Yii::app()->db->createCommand(); //If the table its empty by any reason(initial import), insert something to make the UPDATE work if (!$command->select('count(*) as num')->from('live_subject')->queryScalar()) { $command->insert('live_subject', array('subject_id' => 0, 'subject_id_2' => 0)); } //Position all subs on its time Subject::reschedule_positions(); $round_utc_time = SiteLibrary::utc_time_interval(); //Remote case: This update is just in case cron didn't run in x times of interva(s) //This frees up subs that never were used because they were fixed position but cron failed to run and time passed by Subject::model()->updateAll(array('position' => '0', 'user_position' => '0', 'manager_position' => '0'), 'position < ' . $round_utc_time . ' AND user_position < ' . $round_utc_time . ' AND manager_position < ' . $round_utc_time); $subject = Subject::model()->find(array('condition' => 'position >= ' . $round_utc_time . ' AND content_type_id <> 2 AND approved=1 AND authorized=1 AND disabled=0 AND deleted=0', 'order' => 'position ASC')); $live_subject = Yii::app()->db->createCommand()->select('*')->from('live_subject')->queryRow(); $command->delete('live_comment'); $command->update('live_subject', array('comment_id' => 0, 'comment_number' => 0)); //TEMPORAL:Refill the live_comments table with old comments about this subject if this subject is repeated $past_comments = Yii::app()->db->createCommand()->select('t1.id,code,time,comment,comment_number,username,likes,dislikes')->from('comment t1')->where('subject_id =' . $subject->id)->leftJoin('country t2', 'country_id=t2.id')->leftJoin('user t3', 'user_id=t3.id')->order('time ASC')->queryAll(); echo "<br>gggg"; print_r($past_comments); $i = 0; foreach ($past_comments as $past_comment) { $i++; $country_code = $past_comment['code'] ? $past_comment['code'] : "WW"; $command->insert('live_comment', array('comment_id' => $past_comment['id'], 'username' => $past_comment['username'], 'subject_id' => $subject->id, 'comment_country' => $country_code, 'comment_time' => $past_comment['time'], 'comment_text' => $past_comment['comment'], 'comment_number' => $i, 'likes' => $past_comment['likes'], 'dislikes' => $past_comment['dislikes'])); //we neet to use our own sequence because there might be repeated numbers $comment_id = $past_comment['id']; } if ($i > 0) { $command->update('live_subject', array('comment_id' => $comment_id, 'comment_number' => $i)); } $command->update('live_subject', array('subject_id' => $subject->id, 'scheduled_time' => SiteLibrary::utc_time_interval(), 'subject_data' => serialize($subject))); //Reset position as subject is going to live now Subject::model()->updateByPk($subject->id, array('show_time' => SiteLibrary::utc_time(), 'user_position' => 0, 'manager_position' => 0)); //Notify subject owner via email that his subject its gonna get LIVE $user = User::model()->findByPk($subject->user_id); if ($user->id != 1 and $user->notify_subject_live == 1) { $mail_message = Yii::t('subject', "Hi {username}, \nWe are writing to notify you that your subject got approved and that it is\ngoing to be placed in the live stream(Homepage) in the next 5 minutes.\nDetails\nSubject Title: {title}\nUploaded time: {uploaded_time} UTC\nCurrent time: {current_time} UTC (time of this message)\nEstimated time: {estimated_time} UTC (about 5 minutes)\nIt is even more cool if you chat with your friends about your upcomming subject.\nSo, invite them to go to samesub.com now, you still have 4 minutes.\nIf you do not want to receive this type of notification you can update the settings in\nyour user profile anytime you want.", array('{username}' => $user->username, '{title}' => $subject->title, '{uploaded_time}' => date("Y/m/d H:i", $subject->time_submitted), '{current_time}' => date("Y/m/d H:i", SiteLibrary::utc_time()), '{estimated_time}' => date("Y/m/d H:i", SiteLibrary::utc_time() + 300))); $mail_message .= "\n\n"; $mail_message .= Yii::t('site', "Thanks\nSincerely\nSamesub Team\nwww.samesub.com"); if (SiteLibrary::send_email($user->email, "Your subject is going LIVE", $mail_message)) { echo "An email has been sent."; } else { echo "Email could not be sent."; } } echo 'Done setting next subject_id_2 : ' . $subject->id; //There are some pages that need to be refreshed from the cache such as /subject/index, so that it content reflects the updated data. $optional_prefix = "index.php"; $cmd = Yii::app()->params['cache_refresher'] . ' "' . $optional_prefix . '/subject/index' . '"'; if (Yii::app()->params['cache_refresher']) { exec($cmd); } }
/** * Move forward the position of a subject in the timeboard schedule * This function automatically moves forward all subjects ahead of the subject in question if its needed or if they must be moved * @param integer $id The id of the subject to move * @param boolean $reschedule wether to reschedule positions or not(this is to add a little overload protection, ie:if calling this function subsequently we just need to reschedule once) * @return boolean true on success false on failure */ public function move_position_forward($id, $reschedule = true) { if (!($move_sub = Subject::model()->findByPk($id))) { return false; } //If next position is occupied by a fixed sub(user_position OR manager_position) then move that one, iterate again until a no fixed position is found then loop is finished. //Then update the position of the received sub id //Then do a positions reschedule $timed_subs = Subject::model()->findAll(array('condition' => 'position > ' . $move_sub->position, 'order' => 'position ASC')); $next_pos = $move_sub->position + Yii::app()->params['subject_interval'] * 60; foreach ($timed_subs as $timed_sub) { if (($timed_sub->user_position or $timed_sub->manager_position) and $timed_sub->position == $next_pos) { Subject::model()->updateByPk($timed_sub->id, array('position' => $next_pos + Yii::app()->params['subject_interval'] * 60)); $next_pos = $next_pos + Yii::app()->params['subject_interval'] * 60; continue; } else { Subject::model()->updateByPk($move_sub->id, array('position' => $move_sub->position + Yii::app()->params['subject_interval'] * 60)); break; //We found a hole ,end the loop } } if ($reschedule) { Subject::reschedule_positions(); } return true; /* //Move all subs with user/manager position UNset //Note: user/manager UNset are always together and always in the most beginin possible of the current clock time Subject::model()->updateAll(array('position'=>new CDbExpression('position + '.(Yii::app()->params['subject_interval'] * 60) )), 'position >= '.$move_sub->position.' AND user_position = 0 AND manager_position = 0'); */ }