Course likes

parent 20054c40
...@@ -85,4 +85,17 @@ a.ls_block_title { ...@@ -85,4 +85,17 @@ a.ls_block_title {
} }
.ls_block_btn { .ls_block_btn {
margin-top:10px; margin-top:10px;
}
.ls_likes {
margin-bottom:20px;
}
.ls_likes_y_choice {
background: #00CF56;
color: #fff;
text-decoration: none;
}
.ls_likes_n_choice {
background: #F26363;
color: #fff;
text-decoration: none;
} }
\ No newline at end of file
...@@ -100,7 +100,7 @@ $(document).ready(function() { ...@@ -100,7 +100,7 @@ $(document).ready(function() {
$('#login_form').hide(); $('#login_form').hide();
}); });
$('body').on('click', '.call_form_reg', function(){ $('body').on('click', '.call_form_reg', function(){
link = $(this); link = $(this);
title = 'Регистрация на курс <br>'+link.data('title'); title = 'Регистрация на курс <br>'+link.data('title');
spec_propotion = link.data('spec'); spec_propotion = link.data('spec');
...@@ -112,16 +112,37 @@ $(document).ready(function() { ...@@ -112,16 +112,37 @@ $(document).ready(function() {
else { else {
$('.spec_propotition').hide(); $('.spec_propotition').hide();
} }
/*jQuery.ajax({ });
type: 'POST', $('body').on('click', 'a.ls_likes_y', function(){
url: "/school/lessons-admin/update-form-document", link = $(this);
data: {'docId': doc}, courseId = link.data('course');
success: function(data){ if (!link.hasClass("ls_likes_y_choice") ) {
jQuery('#doc-upd').append( jQuery.ajax({
data type: 'POST',
); url: "/school/likes-course/like-course",
} dataType: "html",
});*/ data: {'courseId': courseId},
success: function(data){
jQuery('.ls_likes').html(data);
}
});
}
});
$('body').on('click', 'a.ls_likes_n', function(){
link = $(this);
courseId = link.data('course');
if (!link.hasClass("ls_likes_n_choice")) {
jQuery.ajax({
type: 'POST',
url: "/school/likes-course/unlike-course",
data: {'courseId': courseId},
dataType: "html",
success: function(data){
jQuery('.ls_likes').html(data);
}
});
}
}); });
}); });
......
<?php
namespace common\modules\school\controllers;
use common\components\BaseController;
use common\modules\school\models\LikesCourse;
class LikesCourseController extends BaseController
{
public static function actionsTitles()
{
return [
'Like-course' => 'Положительная оценка курса пользователем',
'Unlike-course' => 'Отрицательная оценка курса пользователем',
];
}
public function actionLikeCourse()
{
if (!\Yii::$app->user->isGuest){
$courseId = $_POST['courseId'];
$userId = \Yii::$app->user->id;
$estimateCourse = LikesCourse::findOne(['user_id' => $userId, 'course_id' => $courseId]);
if (!$estimateCourse){
$estimateCourse = new LikesCourse();
$estimateCourse->estimate = LikesCourse::LIKE;
$estimateCourse->user_id = $userId;
$estimateCourse->course_id = $courseId;
$estimateCourse->save();
}
else{
if ($estimateCourse->estimate == LikesCourse::UNLIKE){
$estimateCourse->estimate = LikesCourse::LIKE;
$estimateCourse->save();
}
}
$gradeUser = LikesCourse::LIKE;
echo $this->renderPartial('block_likes_course', array(
'gradeUser' => $gradeUser, 'courseId' => $courseId
));
}
}
public function actionUnlikeCourse()
{
if (!\Yii::$app->user->isGuest){
$courseId = $_POST['courseId'];
$userId = \Yii::$app->user->id;
$estimateCourse = LikesCourse::findOne(['user_id' => $userId, 'course_id' => $courseId]);
if (!$estimateCourse){
$estimateCourse = new LikesCourse();
$estimateCourse->estimate = LikesCourse::UNLIKE;
$estimateCourse->user_id = $userId;
$estimateCourse->course_id = $courseId;
$estimateCourse->save();
}
else{
if ($estimateCourse->estimate == LikesCourse::LIKE){
$estimateCourse->estimate = LikesCourse::UNLIKE;
$estimateCourse->save();
}
}
$gradeUser = LikesCourse::UNLIKE;
echo $this->renderPartial('block_likes_course', array(
'gradeUser' => $gradeUser, 'courseId' => $courseId
));
}
}
}
...@@ -6,6 +6,7 @@ use Yii; ...@@ -6,6 +6,7 @@ use Yii;
use common\modules\school\models\Lessons; use common\modules\school\models\Lessons;
use common\modules\testings\models\Test; use common\modules\testings\models\Test;
use common\modules\school\models\LikesCourse;
class Courses extends \common\components\ActiveRecordModel class Courses extends \common\components\ActiveRecordModel
{ {
...@@ -135,4 +136,32 @@ class Courses extends \common\components\ActiveRecordModel ...@@ -135,4 +136,32 @@ class Courses extends \common\components\ActiveRecordModel
} }
return $class; return $class;
} }
public function getEstimateCourseLike($courseId)
{
$userId = \Yii::$app->user->id;
$estimateCourse = LikesCourse::findOne(['user_id' => $userId, 'course_id' => $courseId,
'estimate' => LikesCourse::LIKE]);
if($estimateCourse){
return true;
}
else {
return false;
}
}
public function getEstimateCourseUnlike($courseId)
{
$userId = \Yii::$app->user->id;
$estimateCourse = LikesCourse::findOne(['user_id' => $userId, 'course_id' => $courseId,
'estimate' => LikesCourse::UNLIKE]);
if($estimateCourse){
return true;
}
else {
return false;
}
}
} }
<?php
namespace common\modules\school\models;
class LikesCourse extends \common\components\ActiveRecordModel
{
const LIKE = 1;
const UNLIKE = 2;
public static function tableName()
{
return 'likes_course';
}
public function name()
{
return 'Оценка курса';
}
public function attributeLabels()
{
return [
'estimate' => 'Оценка',
'user_id' => 'Пользователь',
'course_id' => 'Курс',
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['estimate', 'user_id', 'course_id'], 'required'],
[['estimate', 'user_id', 'course_id'], 'integer'],
];
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
];
}
}
...@@ -61,15 +61,19 @@ ...@@ -61,15 +61,19 @@
], ],
], ],
] ); ] );
?> ?>
<div class="col-md-3 col-xs-6 col-sm-12"> <div class="clear"></div>
<div class="ls_likes"> <?php if (!Yii::$app->user->isGuest):?>
<div class="ls_likes_title">Нравится курс?</div> <div class="col-md-3 col-xs-6 col-sm-12">
<a href="" class="ls_likes_y">Да</a> <div class="ls_likes">
<a href="" class="ls_likes_n">Нет</a> <div class="ls_likes_title">Нравится курс?</div>
<a href="javascript:void(0)" class="ls_likes_y <?php echo $model->getEstimateCourseLike($model->id) ? 'ls_likes_y_choice':'';?>"
data-course="<?php echo $model->id;?>">Да</a>
<a href="javascript:void(0)" class="ls_likes_n <?php echo $model->getEstimateCourseUnlike($model->id) ? 'ls_likes_n_choice':'';?>"
data-course="<?php echo $model->id;?>">Нет</a>
</div>
</div> </div>
</div> <?php endif;?>
</div> </div>
</section> </section>
<footer> <footer>
......
<?php
use common\modules\school\models\LikesCourse;
?>
<div class="ls_likes_title">Нравится курс?</div>
<a href="javascript:void(0)" class="ls_likes_y <?php echo ($gradeUser == LikesCourse::LIKE) ? 'ls_likes_y_choice':'';?>"
data-course="<?php echo $courseId;?>">Да</a>
<a href="javascript:void(0)" class="ls_likes_n <?php echo ($gradeUser == LikesCourse::UNLIKE) ? 'ls_likes_n_choice':'';?>"
data-course="<?php echo $courseId;?>">Нет</a>
<?php
use yii\db\Schema;
use yii\db\Migration;
class m160308_105319_create_table_likes_course extends Migration
{
public function safeUp()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql')
{
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';
}
// Структура таблицы `lessons`
$this->createTable('likes_course', [
'id' => Schema::TYPE_PK,
'estimate' => Schema::TYPE_INTEGER . '(11) NOT NULL',
'user_id' => Schema::TYPE_INTEGER . '(11) NOT NULL',
'course_id' => Schema::TYPE_INTEGER . '(11) NOT NULL',
], $tableOptions);
$this->createIndex('FK_likes_course_users', 'likes_course', 'user_id');
$this->createIndex('FK_likes_course_courses', 'likes_course', 'course_id');
}
public function safeDown()
{
$this->dropTable('likes_course');
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment