-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArticleManager.php
More file actions
146 lines (134 loc) · 4.47 KB
/
Copy pathArticleManager.php
File metadata and controls
146 lines (134 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* Class ArticleManager provides methods for managing articles.
*/
class ArticleManager
{
/** Adds new article.
*
* @param string $title
* @param string $description
* @param int $file
* @param string $author
*/
public function addArticle($title, $description, $file, $author, $creators)
{
$article = array(
'title' => $title,
'description' => $description,
'approved' => 0,
'file' => $file,
'author' => $author,
'creators' => $creators,
);
try
{
Db::insert('article', $article);
}
catch(PDOException $exception)
{
echo $exception->getMessage();
}
}
/**
* Gets all articles.
*
* @return mixed all articles
*/
public function getAllArticles()
{
return Db::queryAll('
SELECT `article`.`id`, `article`.`title`, `article`.`description`, `article`.`approved`, `article`.`file`, `article`.`creators`, `user`.`name` AS `author`
FROM `article`
INNER JOIN `user` ON `user`.`id` = `article`.`author`');
}
/**
* Gets all articles with its reviews.
*
* @return mixed articles with its revies
*/
public function getAllArticlesWithReviews()
{
return Db::queryAll('SELECT `article`.`id`, `article`.`title`, `user`.`username`, `review`.`id` AS `review`, `review`.`user_id`,
`reviewer`.`username` AS `reviewer`, `review`.`article_id`,
`review`.`rating_originality`, `review`.`rating_theme`, `review`.`rating_language`,
`review`.`rating_awesomeness`, `review`.`rating_style`, `article`.`state`,
FORMAT(`review`.`rating_originality` + `review`.`rating_theme` + `review`.`rating_language` + `review`.`rating_awesomeness` +`review`.`rating_style`, "N2") / 5 AS `rating`,
(SELECT COUNT(*) FROM `review` WHERE `article`.`id` = `review`.`article_id`) AS `count`
FROM `article` LEFT JOIN `review` ON `article`.`id` = `review`.`article_id`
INNER JOIN `user` ON `user`.`id` = `article`.`author`
LEFT JOIN `user` `reviewer` ON `reviewer`.`id` = `review`.`user_id`');
}
/**
* Gets articles of given user
*
* @param int $userID ID of user
* @return mixed articles of given user
*/
public function getArticleByAuthor($userID)
{
return Db::queryAll('
SELECT `article`.`id`, `article`.`title`, `article`.`description`, `article`.`approved`,
`article`.`file`, `article`.`author`, `article`.`creators`, `user`.`username`
FROM `article` INNER JOIN `user` ON `user`.`id` = `article`.`author`
WHERE `article`.`author` = ' . $userID . '');
}
/**
* Gets article by its ID.
*
* @param int $id ID of article
* @return mixed article with given ID
*/
public function getArticleByID($id)
{
return Db::queryOne('
SELECT * FROM article
WHERE id = ?
', array($id));
}
/**
* Gets number of articles.
*
* @return int number of articles
*/
public function getArticleCount()
{
return Db::query('
SELECT * FROM article');
}
/**
* Gets file of article by its ID.
*
* @param int $id ID of article
* @return mixed article with given ID
*/
public function getFileOfArticle($id)
{
return Db::queryOne('
SELECT `file`.`id`, `file`.`filename`, `file`.`filetype`, `file`.`filesize`, `file`.`file` FROM `file`
INNER JOIN `article` ON `file`.`id` = `article`.`file`
WHERE `article`.`id` = ?', array($id));
}
/**
* Updates article with new values.
*
* @param int $id ID of edited article
* @param array $article data of updated article
*/
public function updateArticle($id, $article)
{
Db::update('article', $article, 'WHERE id = ?', array($id));
}
/**
* Deletes articles.
*
* @param int $id ID of deleted article
*/
public function deleteArticle($id)
{
Db::query('
DELETE FROM article
WHERE id = ?
', array($id));
}
}