-
Notifications
You must be signed in to change notification settings - Fork 7
/
locallib.php
250 lines (227 loc) · 8.36 KB
/
locallib.php
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Internal library of functions for module hotquestion
*
* All the hotquestion specific functions, needed to implement the module
* logic, should go here. Never include this file from your lib.php!
*
* @package mod_hotquestion
* @copyright 2011 Sun Zhigang
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
class mod_hotquestion {
public $instance;
public $cm;
public $course;
protected $current_round;
protected $prev_round;
protected $next_round;
public function __construct($cmid, $roundid = -1) {
global $DB;
$this->cm = get_coursemodule_from_id('hotquestion', $cmid, 0, false, MUST_EXIST);
$this->course = $DB->get_record('course', array('id' => $this->cm->course), '*', MUST_EXIST);
$this->instance = $DB->get_record('hotquestion', array('id' => $this->cm->instance), '*', MUST_EXIST);
$this->set_current_round($roundid);
}
/**
* Return whether the user has voted on specified question
*
* @param int $question question id
* @param int $user user id. -1 means current user
* @return boolean
*/
public function has_voted($question, $user = -1) {
global $USER, $DB;
if ($user == -1) {
$user = $USER->id;
}
return $DB->record_exists('hotquestion_votes', array('question'=>$question, 'voter'=>$user));
}
/**
* Add a new question to current round
*
* @global object
* @global object
* @global object
* @param object $fromform from ask form
*/
public function add_new_question($fromform) {
global $USER, $CFG, $DB;
$data->hotquestion = $this->instance->id;
$data->content = trim($fromform->question);
$data->userid = $USER->id;
$data->time = time();
if (isset($fromform->anonymous) && $fromform->anonymous && $this->instance->anonymouspost) {
$data->anonymous = $fromform->anonymous;
// Assume this user is guest
$data->userid = $CFG->siteguest;
}
if (!empty($data->content)) {
$DB->insert_record('hotquestion_questions', $data);
add_to_log($this->course->id, "hotquestion", "add question", "view.php?id={$this->cm->id}", $data->content, $this->cm->id);
return true;
} else {
return false;
}
}
/**
* Vote on question
*
* @global object
* @global object
* @param int $question the question id
*/
public function vote_on($question) {
global $DB, $USER;
$question = $DB->get_record('hotquestion_questions', array('id'=>$question));
if ($question && $this->can_vote_on($question)) {
add_to_log($this->course->id, 'hotquestion', 'update vote', "view.php?id={$this->cm->id}", $question->id, $this->cm->id);
if (!$this->has_voted($question->id)) {
$votes->question = $question->id;
$votes->voter = $USER->id;
$DB->insert_record('hotquestion_votes', $votes);
} else {
$DB->delete_records('hotquestion_votes', array('question'=> $question->id, 'voter'=>$USER->id));
}
}
}
/**
* Whether can vote on the question
*
* @param object or int $question
* @param object $user null means current user
*/
public function can_vote_on($question, $user = null) {
global $USER, $DB;
if (is_int($question)) {
$question = $DB->get_record('hotquestion_questions', array('id'=>$question));
}
if (empty($user)) {
$user = $USER;
}
// Is this question in last round?
$rounds = $DB->get_records('hotquestion_rounds', array('hotquestion' => $this->instance->id), 'id DESC', '*', 0, 1);
$lastround = reset($rounds);
$in_last_round = $question->time >= $lastround->starttime;
return $question->userid != $user->id && $in_last_round;
}
/**
* Open a new round and close the old one
*
* @global object
*/
public function add_new_round() {
global $DB;
// Close the latest round
$old = array_pop($DB->get_records('hotquestion_rounds', array('hotquestion'=>$this->instance->id), 'id DESC', '*', 0, 1));
$old->endtime = time();
$DB->update_record('hotquestion_rounds', $old);
// Open a new round
$new->hotquestion = $this->instance->id;
$new->starttime = time();
$new->endtime = 0;
$rid = $DB->insert_record('hotquestion_rounds', $new);
add_to_log($this->course->id, 'hotquestion', 'add round', "view.php?id={$this->cm->id}&round=$rid", $rid, $this->cm->id);
}
/**
* Set current round to show
*
* @global object
* @param int $roundid
*/
public function set_current_round($roundid = -1) {
global $DB;
$rounds = $DB->get_records('hotquestion_rounds', array('hotquestion' => $this->instance->id), 'id ASC');
if (empty($rounds)) {
// Create the first round
$round->starttime = time();
$round->endtime = 0;
$round->hotquestion = $this->instance->id;
$round->id = $DB->insert_record('hotquestion_rounds', $round);
$rounds[] = $round;
}
if ($roundid != -1 && array_key_exists($roundid, $rounds)) {
$this->current_round = $rounds[$roundid];
$ids = array_keys($rounds);
// Search previous round
$current_key = array_search($roundid, $ids);
if (array_key_exists($current_key - 1, $ids)) {
$this->prev_round = $rounds[$ids[$current_key - 1]];
} else {
$this->prev_round = null;
}
// Search next round
if (array_key_exists($current_key + 1, $ids)) {
$this->next_round = $rounds[$ids[$current_key + 1]];
} else {
$this->next_round = null;
}
} else {
// Use the last round
$this->current_round = array_pop($rounds);
$this->prev_round = array_pop($rounds);
$this->next_round = null;
}
}
/**
* Return current round
*
* @return object
*/
public function get_current_round() {
return $this->current_round;
}
/**
* Return previous round
*
* @return object
*/
public function get_prev_round() {
return $this->prev_round;
}
/**
* Return next round
*
* @return object
*/
public function get_next_round() {
return $this->next_round;
}
/**
* Return questions according to $current_round
*
* @global object
* @return all questions with vote count in current round
*/
public function get_questions() {
global $DB;
if ($this->current_round->endtime == 0) {
$this->current_round->endtime = 0xFFFFFFFF; //Hack
}
$params = array($this->instance->id, $this->current_round->starttime, $this->current_round->endtime);
return $DB->get_records_sql('SELECT q.*, count(v.voter) as votecount
FROM {hotquestion_questions} q
LEFT JOIN {hotquestion_votes} v
ON v.question = q.id
WHERE q.hotquestion = ?
AND q.time >= ?
AND q.time <= ?
GROUP BY q.id
ORDER BY votecount DESC, q.time DESC', $params);
}
}