1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nicklas\Comment\Modules; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A database driven model. |
7
|
|
|
*/ |
8
|
|
|
class Question extends ActiveRecordModelExtender |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var string $tableName name of the database table. |
13
|
|
|
*/ |
14
|
|
|
protected $tableName = "ramverk1_questions"; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Columns in the table. |
18
|
|
|
* |
19
|
|
|
* @var integer $id primary key auto incremented. |
20
|
|
|
*/ |
21
|
|
|
public $id; |
22
|
|
|
public $user; |
23
|
|
|
|
24
|
|
|
public $title; |
25
|
|
|
public $tags; |
26
|
|
|
|
27
|
|
|
public $created; |
28
|
|
|
public $status; # default is active |
29
|
|
|
|
30
|
|
|
public $di; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor injects with DI container. |
35
|
|
|
* |
36
|
|
|
*/ |
37
|
|
|
public function __construct($di = null) |
38
|
|
|
{ |
39
|
|
|
$this->di = $di; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns post with markdown and gravatar |
44
|
|
|
* @param string $sql |
45
|
|
|
* @param array $param |
|
|
|
|
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function getQuestions($sql, $params) |
50
|
|
|
{ |
51
|
|
|
$questions = $this->findAllWhere("$sql", $params); |
52
|
|
|
|
53
|
|
|
return array_map(function ($question) { |
54
|
|
|
|
55
|
|
|
// Find users email |
56
|
|
|
$user = new User(); |
57
|
|
|
$user->setDb($this->di->get("db")); |
58
|
|
|
$user->find("name", $question->user); |
59
|
|
|
|
60
|
|
|
// Start setting attributes |
61
|
|
|
$question->img = $this->gravatar($user->email); |
62
|
|
|
$question->title = $this->getMD($question->title); |
63
|
|
|
$question->tags = explode(',', $question->tags); |
64
|
|
|
|
65
|
|
|
return $question; |
66
|
|
|
}, $questions); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns one question with it's own question text and other answers |
71
|
|
|
* @param int $id |
72
|
|
|
* |
73
|
|
|
* @return object |
74
|
|
|
*/ |
75
|
|
|
public function getQuestion($id) |
76
|
|
|
{ |
77
|
|
|
$question = $this->find("id", $id); |
78
|
|
|
|
79
|
|
|
// Add attributes from Post class |
80
|
|
|
$post = new Posts(); |
81
|
|
|
$post->setDb($this->di->get("db")); |
82
|
|
|
|
83
|
|
|
$question->question = $post->getPost("questionId = ? AND type = ?", [$post->id, "question"]); |
84
|
|
|
$question->answers = $post->getAllPosts("questionId = ? AND type = ?", [$post->id, "answer"]); |
85
|
|
|
$question->answersCount = count($question->answers); |
86
|
|
|
|
87
|
|
|
// Start setting up own attributes |
88
|
|
|
$question->title = $this->getMD($question->title); |
89
|
|
|
$question->tags = explode(',', $question->tags); |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
return $question; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns array of tags, keys are name, value is the integer of how many. |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
public function getPopularTags() |
101
|
|
|
{ |
102
|
|
|
$question = $this->findAll(); |
103
|
|
|
|
104
|
|
|
$tagsMultiArray = array_map(function ($question) { |
105
|
|
|
return explode(',', $question->tags); |
106
|
|
|
}, $question); |
107
|
|
|
|
108
|
|
|
// Merge multi array, count values, sort high to low |
109
|
|
|
$tagArr = array_count_values(call_user_func_array("array_merge", $tagsMultiArray)); |
110
|
|
|
arsort($tagArr); |
111
|
|
|
|
112
|
|
|
if (array_key_exists('', $tagArr)) { |
113
|
|
|
unset($tagArr[""]); |
114
|
|
|
} |
115
|
|
|
return $tagArr; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Check if a post belongs to user |
123
|
|
|
* |
124
|
|
|
* |
125
|
|
|
* @return boolean |
126
|
|
|
*/ |
127
|
|
View Code Duplication |
public function controlAuthority($name) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$user = new User(); |
130
|
|
|
$user->setDb($this->di->get("db")); |
131
|
|
|
$user->find("name", $name); |
132
|
|
|
|
133
|
|
|
// IF AUTHORITY == admin, then continue |
134
|
|
|
if ($user->authority != "admin") { |
135
|
|
|
return ($user->name == $this->user); |
136
|
|
|
} |
137
|
|
|
return true; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.