-
Notifications
You must be signed in to change notification settings - Fork 15
/
locallib.php
975 lines (890 loc) · 38 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
<?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
* @copyright 2016 onwards AL Rachels [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_hotquestion\event\remove_vote;
use mod_hotquestion\event\update_vote;
use mod_hotquestion\event\add_question;
use mod_hotquestion\event\add_round;
use mod_hotquestion\event\remove_question;
use mod_hotquestion\event\remove_round;
use mod_hotquestion\event\download_questions;
use mod_hotquestion\local\results;
defined('MOODLE_INTERNAL') || die(); // @codingStandardsIgnoreLine
define('HOTQUESTION_EVENT_TYPE_OPEN', 'open');
define('HOTQUESTION_EVENT_TYPE_CLOSE', 'close');
/**
* Standard base class for mod_hotquestion.
*
* @package mod_hotquestion
* @copyright 2011 Sun Zhigang
* @copyright 2016 onwards AL Rachels [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_hotquestion {
/** @var int callback arg - the instance of the current hotquestion activity */
public $instance;
/** @var int callback arg - the instance of the current hotquestion module */
public $cm;
/** @var int callback arg - the instance of the current hotquestion course */
public $course;
/** @var int callback arg - the id of current round of questions */
protected $currentround;
/** @var int callback arg - the id of previous round of questions */
protected $prevround;
/** @var int callback arg - the id of next round of questions */
protected $nextround;
/** @var int callback arg - the total round count in this hot question */
protected $roundcount;
/** @var int callback arg - the round being looked at in this hot question */
protected $currentroundx;
/**
* Constructor for the base hotquestion class.
*
* Note: For $coursemodule you can supply a stdclass if you like, but it
* will be more efficient to supply a cm_info object.
*
* @param mixed $cmid
* @param mixed $roundid
*/
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', ['id' => $this->cm->course], '*', MUST_EXIST);
$this->instance = $DB->get_record('hotquestion', ['id' => $this->cm->instance], '*', MUST_EXIST);
$this->set_currentround($roundid);
// Contrib by ecastro ULPGC, for grades callbacks.
$this->instance->cmid = $cmid;
$this->instance->cmidumber = $this->cm->idnumber;
}
/**
* Return whether the user has voted on specified question.
*
* Called from function vote_on($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', ['question' => $question, 'voter' => $user]);
}
/**
* Vote on question.
*
* Called from view.php.
* @param int $question The question id.
*/
public function vote_on($question) {
global $CFG, $DB, $USER;
$votes = new StdClass();
$context = context_module::instance($this->cm->id);
$question = $DB->get_record('hotquestion_questions', ['id' => $question]);
if ($question && $this->can_vote_on($question)) {
// Trigger and log a vote event.
$params = [
'objectid' => $this->cm->id,
'context' => $context,
];
$event = update_vote::create($params);
$event->trigger();
if (!$this->has_voted($question->id)) {
$votes->question = $question->id;
$votes->voter = $USER->id;
$DB->insert_record('hotquestion_votes', $votes);
}
// Update viewed completion state for current user.
$this->update_completion_state();
}
// Contrib by ecastro ULPGC, update grades for questions author and voters.
// 20220623 Moved so entering viewgrades.php page always updates to the latest grade.
$this->update_users_grades([$question->userid, $USER->id]);
}
/**
* Remove vote on question.
*
* Called from view.php.
* @param int $question The question id.
*/
public function remove_vote($question) {
global $CFG, $DB, $USER;
$votes = new StdClass();
$context = context_module::instance($this->cm->id);
$question = $DB->get_record('hotquestion_questions', ['id' => $question]);
if ($question && $this->can_vote_on($question)) {
// Trigger and log a remove_vote event.
$params = [
'objectid' => $this->cm->id,
'context' => $context,
];
$event = remove_vote::create($params);
$event->trigger();
if ($this->has_voted($question->id)) {
$DB->delete_records('hotquestion_votes', ['question' => $question->id, 'voter' => $USER->id]);
}
// Update viewed completion state for current user.
$this->update_completion_state();
// Contrib by ecastro ULPGC, update grades for question author and voters.
$this->update_users_grades([$question->userid, $USER->id]);
}
}
/**
* Whether can vote on the question.
*
* @param int $question
* @param stdClass $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', ['id' => $question]);
}
if (empty($user)) {
$user = $USER;
}
// Is this question in last round?
$rounds = $DB->get_records('hotquestion_rounds', ['hotquestion' => $this->instance->id], 'id DESC', '*', 0, 1);
$lastround = reset($rounds);
$inlastround = $question->time >= $lastround->starttime;
return $question->userid != $user->id && $inlastround;
}
/**
* Calculates number of remaining votes (heat) available to this user in the current round.
*
* Added 20200529.
* @param int $hq
* @param int $user
*/
public function heat_tally($hq, $user = null) {
global $USER, $CFG, $DB;
$params = [
$hq->currentround->id,
$hq->currentround->hotquestion,
$USER->id,
];
$sql = "SELECT hqq.id AS questionid,
COUNT(hqv.voter) AS heat,
hqq.hotquestion AS hotquestionid,
hqr.id AS round,
hqq.content AS content,
hqq.userid AS userid,
hqv.voter AS voter,
hqq.time AS time,
hqq.anonymous AS anonymous,
hqq.tpriority AS tpriority,
hqq.approved AS approved
FROM {hotquestion_rounds} hqr
LEFT JOIN {hotquestion_questions} hqq ON hqr.hotquestion=hqq.hotquestion
LEFT JOIN {hotquestion_votes} hqv ON hqv.question=hqq.id
JOIN {user} u ON u.id = hqq.userid
WHERE hqr.id = ?
AND hqr.hotquestion = ?
AND hqq.time > hqr.starttime
AND hqr.endtime = 0
AND hqv.voter = ?
GROUP BY hqq.id, hqr.id, hqv.voter, hqq.hotquestion, hqq.content,
hqq.userid, hqv.voter, hqq.time, hqq.anonymous, hqq.tpriority,
hqq.approved
ORDER BY hqq.hotquestion ASC, tpriority DESC, heat DESC";
$tally = count($DB->get_records_sql($sql, $params));
$results = ($hq->instance->heatlimit - $tally);
return $results;
}
/**
* Open a new round and close the old one.
*/
public function add_new_round() {
global $USER, $CFG, $DB;
// Close the latest round.
$rounds = $DB->get_records('hotquestion_rounds', ['hotquestion' => $this->instance->id], 'id DESC', '*', 0, 1);
$old = array_pop($rounds);
$old->endtime = time();
$DB->update_record('hotquestion_rounds', $old);
// Open a new round.
$new = new StdClass();
$new->hotquestion = $this->instance->id;
$new->starttime = time();
$new->endtime = 0;
$context = context_module::instance($this->cm->id);
$rid = $DB->insert_record('hotquestion_rounds', $new);
// Trigger add new round event.
$params = [
'objectid' => $this->cm->id,
'context' => $context,
];
$event = add_round::create($params);
$event->trigger();
}
/**
* Set current round to show.
* @param int $roundid
*/
public function set_currentround($roundid = -1) {
global $DB;
// Get all the rounds for this Hot Question.
$rounds = $DB->get_records('hotquestion_rounds', ['hotquestion' => $this->instance->id], 'id ASC');
// 20210214 Get total number of rounds for the current Hot Question activity.
$this->roundcount = (count($rounds));
// If there are no rounds, it is a new Hot Question activity and we need to create the first round for it.
if (empty($rounds)) {
// Create the first round.
$round = new StdClass();
$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->currentround = $rounds[$roundid];
$ids = array_keys($rounds);
// Search previous round.
$currentkey = array_search($roundid, $ids);
// 20210215 $currentkey contains the virtual number - 1 of the current round being looked at.
// Correct the virtual number by adding 1.
$this->currentroundx = $currentkey + 1;
if (array_key_exists($currentkey - 1, $ids)) {
$this->prevround = $rounds[$ids[$currentkey - 1]];
} else {
$this->prevround = null;
}
// Search next round.
if (array_key_exists($currentkey + 1, $ids)) {
$this->nextround = $rounds[$ids[$currentkey + 1]];
} else {
$this->nextround = null;
}
} else {
// Use the last round.
$this->currentround = array_pop($rounds);
$this->prevround = array_pop($rounds);
$this->nextround = null;
}
return $roundid;
}
/**
* Return current round.
*
* @return object
*/
public function get_currentround() {
return $this->currentround;
}
/**
* Return previous round.
*
* @return object
*/
public function get_prevround() {
return $this->prevround;
}
/**
* Return next round.
*
* @return object
*/
public function get_nextround() {
return $this->nextround;
}
/**
* Return next round.
*
* @return object
*/
public function get_roundcount() {
return $this->roundcount;
}
/**
* Return next round.
*
* @return object
*/
public function get_currentroundx() {
return $this->currentroundx;
}
/**
* Return questions according to $currentround.
*
* Sort order is priority descending, votecount descending,
* and time descending from most recent to oldest.
* @return all questions with vote count in current round.
*/
public function get_questions() {
global $DB;
if ($this->currentround->endtime == 0) {
$this->currentround->endtime = 0xFFFFFFFF; // Hack.
}
$params = [
$this->instance->id,
$this->currentround->starttime,
$this->currentround->endtime,
];
// 20210306 Added format to the selection.
return $DB->get_records_sql('SELECT q.id, q.hotquestion, q.content, q.format, q.userid, q.time,
q.anonymous, q.approved, q.tpriority, 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, q.hotquestion, q.content, q.userid, q.time,
q.anonymous, q.approved, q.tpriority
ORDER BY q.approved DESC, tpriority DESC, votecount DESC, q.time DESC', $params);
}
/**
* Remove selected question and any votes that it might have.
*
* @return object
*/
public function remove_question() {
global $CFG, $DB;
$data = new StdClass();
$data->hotquestion = $this->instance->id;
$context = context_module::instance($this->cm->id);
// Trigger remove_question event.
$params = [
'objectid' => $this->cm->id,
'context' => $context,
];
$event = remove_question::create($params);
$event->trigger();
if (null !== (required_param('q', PARAM_INT))) {
$questionid = required_param('q', PARAM_INT);
$itemid = required_param('q', PARAM_INT);
$dbquestion = $DB->get_record('hotquestion_questions', ['id' => $questionid]);
// Contrib by ecastro ULPGC.
$users = $this->get_question_voters($questionid);
$users[] = $dbquestion->userid;
// Contrib by ecastro ULPGC.
$DB->delete_records('hotquestion_questions', ['id' => $dbquestion->id]);
// 20220510 Deleted $dbvote line of code.
// Delete all votes on the question that was just deleted.
$DB->delete_records('hotquestion_votes', ['question' => $dbquestion->id]);
// 20220510 Delete all comments on the question that was just deleted.
$DB->delete_records('comments', ['itemid' => $itemid, 'component' => 'mod_hotquestion']);
// Update completion state for question creator.
$this->update_completion_state($dbquestion->userid);
// Update completion state for voters.
foreach ($users as $user) {
$this->update_completion_state($user);
}
// Contrib by ecastro ULPGC, update grades for question author and voters.
$this->update_users_grades($users);
}
return $this->currentround;
}
/**
* If the currently being viewed round is empty, delete it.
* Otherwise, remove any questions in the round currently being viewed,
* remove any votes for each question being removed,
* then remove the currently being viewed round.
* @return nothing
*/
public function remove_round() {
global $CFG, $DB;
$data = new StdClass();
$data->hotquestion = $this->instance->id;
$context = context_module::instance($this->cm->id);
// Trigger remove_question event.
$params = [
'objectid' => $this->cm->id,
'context' => $context,
];
$event = remove_round::create($params);
$event->trigger();
$roundid = required_param('round', PARAM_INT);
if ($this->currentround->endtime == 0) {
$this->currentround->endtime = 0xFFFFFFFF; // Hack.
}
$params = [
$this->instance->id,
$this->currentround->starttime,
$this->currentround->endtime,
];
$questions = $DB->get_records_sql('SELECT q.id, q.hotquestion, q.content, q.userid, q.time,
q.anonymous, q.approved, q.tpriority, 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, q.hotquestion, q.content, q.userid, q.time, q.anonymous,
q.approved, q.tpriority
ORDER BY votecount DESC, q.time DESC', $params);
if ($questions) {
foreach ($questions as $q) {
$questionid = $q->id; // Get id of first question on the page to delete.
$dbquestion = $DB->get_record('hotquestion_questions', ['id' => $questionid]);
// Contrib by ecastro ULPGC.
$users = $this->get_question_voters($questionid);
$users[] = $dbquestion->userid;
// Contrib by ecastro ULPGC.
$DB->delete_records('hotquestion_questions', ['id' => $dbquestion->id]);
// Get an array of all votes on the question that was just deleted, then delete them.
$dbvote = $DB->get_records('hotquestion_votes', ['question' => $questionid]);
$DB->delete_records('hotquestion_votes', ['question' => $dbquestion->id]);
// Update completion state for question creator.
$this->update_completion_state($dbquestion->userid);
// Update completion state for voters.
foreach ($users as $user) {
$this->update_completion_state($user);
}
// Contrib by ecastro ULPGC, update grades for question author and voters.
$this->update_users_grades($users);
}
// Now that all questions and votes are gone, remove the round.
$dbround = $DB->get_record('hotquestion_rounds', ['id' => $roundid]);
$DB->delete_records('hotquestion_rounds', ['id' => $dbround->id]);
} else {
// This round is empty so delete without having to remove questions and votes.
$dbround = $DB->get_record('hotquestion_rounds', ['id' => $roundid]);
$DB->delete_records('hotquestion_rounds', ['id' => $dbround->id]);
}
// Now we need to see if we need a new round or have one we can still use.
$rounds = $DB->get_records('hotquestion_rounds', ['hotquestion' => $this->instance->id], 'id DESC');
foreach ($rounds as $rnd) {
if ($rnd->endtime == 0) {
// Deleted a closed round so just return.
return;
} else {
// Deleted our open round so create a new round.
$round = new StdClass();
$round->starttime = time();
$round->endtime = 0;
$round->hotquestion = $this->instance->id;
$round->id = $DB->insert_record('hotquestion_rounds', $round);
$rounds[] = $round;
return;
}
}
return $this->currentround;
}
/**
* Download questions.
* @param array $chq
* @param string $filename - The filename to use.
* @param string $delimiter - The character to use as a delimiter.
* @return nothing
*/
public function download_questions($chq, $filename = "export.csv", $delimiter=";") {
global $CFG, $DB, $USER;
require_once($CFG->libdir.'/csvlib.class.php');
$context = context_module::instance($this->cm->id);
// Trigger download_questions event.
$params = [
'objectid' => $this->cm->id,
'context' => $context,
];
$event = download_questions::create($params);
$event->trigger();
// Construct sql query and filename based on admin or teacher/manager.
// Add filename details based on course and HQ activity name.
$csv = new csv_export_writer();
$strhotquestion = get_string('hotquestion', 'hotquestion');
$fields = [];
if (is_siteadmin($USER->id)) {
// For an admin, we want every hotquestion activity.
$whichhqs = ('AND hq.hotquestion IS NOT NULL');
$csv->filename = clean_filename(get_string('exportfilenamep1', 'hotquestion'));
} else {
// For a teacher, we want only the current hotquestion activity.
$whichhqs = ('AND hq.hotquestion = ');
$whichhqs .= (':thisinstid');
$csv->filename = clean_filename(($this->course->shortname).'_');
$csv->filename .= clean_filename(($this->instance->name));
}
// Add fields with the column labels for ONLY the current HQ activity.
$fields = [
get_string('firstname'),
get_string('lastname'),
get_string('userid', 'hotquestion'),
get_string('hotquestion', 'hotquestion').' ID',
get_string('question', 'hotquestion').' ID',
get_string('time', 'hotquestion'),
get_string('anonymous', 'hotquestion'),
$this->instance->teacherprioritylabel,
$this->instance->heatlabel,
$this->instance->approvallabel,
$this->instance->questionlabel,
get_string('comments'),
];
$csv->filename .= clean_filename(get_string('exportfilenamep2', 'hotquestion').gmdate("Ymd_Hi").'GMT.csv');
// Now add this instance id that's needed in the sql for teachers and managers downloads.
$fields = [$fields, 'thisinstid' => $this->instance->id];
if ($CFG->dbtype == 'pgsql') {
$sql = "SELECT hq.id AS question,
CASE
WHEN u.firstname = 'Guest user'
THEN u.lastname || 'Anonymous'
ELSE u.firstname
END AS firstname,
u.lastname AS lastname,
hq.hotquestion AS hotquestion,
hq.content AS content,
hq.userid AS userid,
to_char(to_timestamp(hq.time), 'YYYY-MM-DD HH24:MI:SS') AS time,
hq.anonymous AS anonymous,
hq.tpriority AS tpriority,
COUNT(hv.voter) AS heat,
hq.approved AS approved,
h.course AS course,
h.teacherprioritylabel AS teacherprioritylabel,
h.heatlabel AS heatlabel,
h.approvallabel AS approvallabel,
h.questionlabel AS questionlabel
FROM {hotquestion_questions} hq
LEFT JOIN {hotquestion_votes} hv ON hv.question=hq.id
JOIN {hotquestion} h ON h.id = hq.hotquestion
JOIN {user} u ON u.id = hq.userid
WHERE hq.userid > 0 ";
} else {
$sql = "SELECT hq.id AS question,
CASE
WHEN u.firstname = 'Guest user'
THEN CONCAT(u.lastname, 'Anonymous')
ELSE u.firstname
END AS 'firstname',
u.lastname AS 'lastname',
hq.hotquestion AS hotquestion,
hq.content AS content,
hq.userid AS userid,
FROM_UNIXTIME(hq.time) AS TIME,
hq.anonymous AS anonymous,
hq.tpriority AS tpriority,
COUNT(hv.voter) AS heat,
hq.approved AS approved,
h.course AS course,
h.teacherprioritylabel AS teacherprioritylabel,
h.heatlabel AS heatlabel,
h.approvallabel AS approvallabel,
h.questionlabel AS questionlabel
FROM {hotquestion_questions} hq
LEFT JOIN {hotquestion_votes} hv ON hv.question = hq.id
JOIN {hotquestion} h ON h.id = hq.hotquestion
JOIN {user} u ON u.id = hq.userid
WHERE hq.userid > 0 ";
}
$sql .= ($whichhqs);
$sql .= " GROUP BY u.lastname, u.firstname, hq.hotquestion, hq.id, hq.content,
hq.userid, hq.time, hq.anonymous, hq.tpriority, hq.approved,
h.course, h.teacherprioritylabel, h.heatlabel, h.approvallabel,h.questionlabel
ORDER BY hq.hotquestion ASC, u.lastname ASC, u.firstname ASC, hq.id ASC, tpriority DESC, heat";
// Add the list of users and HotQuestions to our data array.
if ($hqs = $DB->get_records_sql($sql, $fields)) {
$firstrowflag = 1;
if (is_siteadmin($USER->id)) {
$currenthqhotquestion = !empty($hqs[1]->hotquestion) ? $hqs[1]->hotquestion : 'Hotquestion';
} else {
$currenthqhotquestion = '';
}
foreach ($hqs as $q) {
$fields2 = [
get_string('firstname'),
get_string('lastname'),
get_string('userid', 'hotquestion'),
get_string('hotquestion', 'hotquestion').' ID',
get_string('question', 'hotquestion').' ID',
get_string('time', 'hotquestion'),
get_string('anonymous', 'hotquestion'),
$q->teacherprioritylabel,
$q->heatlabel,
$q->approvallabel,
$q->questionlabel,
get_string('comments'),
];
// 20220818 Initialize variable for any comments for the next question.
$comment = '';
// 20220818 If there are any, get the comments for each question to add in the export file.
// 20221124 Modified to include the component.
if ($cmts = $DB->get_records('comments', [
'itemid' => $q->question,
'component' => 'mod_hotquestion',
],
'userid,
content,
timecreated'
)) {
$temp = count($cmts);
$comment .= '('.$temp.' '.get_string('comments').') ';
foreach ($cmts as $cmt) {
$comment .= get_string('user').' '.$cmt->userid.' commented: '.$cmt->content.' | ';
}
}
// 20220819 Split admins output into sections by HotQuestions activities.
if ((($currenthqhotquestion <> $q->hotquestion) && (is_siteadmin($USER->id))) || ($firstrowflag)) {
$currenthqhotquestion = $q->hotquestion;
// 20220819 Add the course shortname and the HQ activity name to our data array.
$currentcrsname = $DB->get_record('course', ['id' => $q->course], 'shortname');
$currenthqname = $DB->get_record('hotquestion', ['id' => $q->hotquestion], 'name');
$blankrow = [' ', null];
// 20220820 Only include filename, date, and URL only on the first row of the export.
// 20220820 Add a blank line before each HQ activity output, except for the first HQ activity.
if (!$firstrowflag) {
$csv->add_data($blankrow);
$activityinfo = [
get_string('course').': '.$currentcrsname->shortname,
get_string('activity').': '.$currenthqname->name,
];
} else {
$activityinfo = [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
get_string('exportfilenamep2', 'hotquestion').
gmdate("Ymd_Hi").get_string('for', 'hotquestion').
$CFG->wwwroot,
];
$csv->add_data($activityinfo);
$activityinfo = [
get_string('course').': '.$currentcrsname->shortname,
get_string('activity').': '.$currenthqname->name,
];
}
$csv->add_data($activityinfo);
$csv->add_data($fields2);
$firstrowflag = 0;
}
// 20220821 Cleaning the content to remove all the paragraph tags to make things easier to read.
$cleanedcontent = format_string($q->content,
$striplinks = true,
$options = null);
$output = [
$q->firstname,
$q->lastname,
$q->userid,
$q->hotquestion,
$q->question,
$q->time,
$q->anonymous,
$q->tpriority,
$q->heat,
$q->approved,
$cleanedcontent,
$comment,
];
$csv->add_data($output);
}
}
// Download the completed array of questions and comments.
$csv->download_file();
exit;
}
/**
* Toggle approval go/stop of current question in current round.
*
* @param var $question
* @return nothing
*/
public function approve_question($question) {
global $CFG, $DB, $USER;
$context = context_module::instance($this->cm->id);
$question = $DB->get_record('hotquestion_questions', ['id' => $question]);
if ($question->approved) {
// If currently approved, toggle to disapproved.
$question->approved = '0';
$DB->update_record('hotquestion_questions', $question);
} else {
// If currently disapproved, toggle to approved.
$question->approved = '1';
$DB->update_record('hotquestion_questions', $question);
}
$this->update_users_grades([$question->userid, $USER->id]);
return;
}
/**
* Set teacher priority of current question in current round.
*
* @param int $u The priority up(1) or down(0) flag.
* @param int $question The question id to change the teacher priority for.
*/
public function tpriority_change($u, $question) {
global $CFG, $DB, $USER;
$context = context_module::instance($this->cm->id);
$question = $DB->get_record('hotquestion_questions', ['id' => $question]);
if ($u) {
// If priority flag is 1, increase priority by 1.
$question->tpriority = ++$question->tpriority;
$DB->update_record('hotquestion_questions', $question);
} else {
// If priority flag is 0, decrease priority by 1.
$question->tpriority = --$question->tpriority;
$DB->update_record('hotquestion_questions', $question);
}
$this->update_users_grades([$question->userid, $USER->id]);
}
// Contrib by ecastro ULPGC.
/**
* Get the user rating in this activity, by posts and heat/votes.
*
* Function is called ONLY when a user is on view.php but via renderer.php page.
*
* @param int $userid The single user to calculate the rating for.
* @return float $rating number
*/
public function calculate_user_ratings($userid = null): float {
global $DB, $USER;
if (!$userid) {
$userid = $USER->id;
}
// Get any questions by this user and any votes if they have some.
$sql = "SELECT q.id, q.approved, q.tpriority, count(v.voter) as votes
FROM {hotquestion_questions} q
LEFT JOIN {hotquestion_votes} v ON v.question = q.id
WHERE q.hotquestion = ? AND q.userid = ? AND q.anonymous = 0
GROUP BY q.id ";
$params = [$this->instance->id, $userid];
$questions = $DB->get_records_sql($sql, $params);
$grade = 0;
// If the user added any questions, add to the current user rating.
foreach ($questions as $question) {
// 20220623 Add to the current user rating only if the question is approved.
if ($question->approved) {
// Add/subtract to the current user rating based on teacher priority.
$qrate = ($question->tpriority) ? $question->tpriority : $this->instance->factorpriority / 100;
// Add/subtract to the current user rating based on teacher priority and heat given.
$grade += $qrate + $question->votes * $this->instance->factorheat / 100;
}
}
// Get any votes made by this user.
$sql = "SELECT COUNT(v.id)
FROM {hotquestion_votes} v
JOIN {hotquestion_questions} q ON v.question = q.id
WHERE q.hotquestion = ? AND v.voter = ? ";
$params = [$this->instance->id, $userid];
$votes = $DB->count_records_sql($sql, $params);
// If the user voted, add to the current user rating.
if ($votes > 0) {
// Add/subtract to the current user rating based on heat received.
$grade += $votes * $this->instance->factorvote / 100;
}
return $grade;
}
/**
* Gets the users that had voted a given question.
*
* @param int $questionid The question id.
* @return array Array of int userids or empty if none.
*/
public function get_question_voters(int $questionid): array {
global $DB;
$voters = $DB->get_records_menu('hotquestion_votes', ['question' => $questionid], '', 'id, voter');
if ($voters) {
return array_values($voters);
}
return [];
}
/**
* Recalculates ratings and grades for users related to a question.
* The author of the question and the voters.
*
* @param array $users The userids of users to update.
*/
public function update_users_grades(array $users) {
global $DB;
if (empty($users)) {
return false;
}
list($insql, $params) = $DB->get_in_or_equal($users);
$select = "userid $insql AND hotquestion = ? ";
$params[] = $this->instance->id;
$grades = $DB->get_records_select('hotquestion_grades',
$select,
$params,
'',
'userid,
id,
hotquestion,
rawrating,
timemodified');
$now = time();
$newgrade = new stdClass();
$newgrade->hotquestion = $this->instance->id;
$newgrade->timemodified = $now;
foreach ($users as $userid) {
$rating = $this->calculate_user_ratings($userid);
if (isset($grades[$userid])) {
// Existing user grade, update if changed.
$grade = $grades[$userid];
if ($rating != $grade->rawrating) {
$grade->rawrating = $rating;
$grade->timemodified = $now;
$DB->update_record('hotquestion_grades', $grade);
}
} else {
// New user grade, create.
$newgrade->rawrating = $rating;
$newgrade->userid = $userid;
$DB->insert_record('hotquestion_grades', $newgrade);
}
// Calling the function in lib.php at about line 807.
hotquestion_update_grades($this->instance, $userid);
}
}
/**
* Update completion state.
*
* @param int $user
* @return void
*/
public function update_completion_state($user = null) {
$completion = new completion_info($this->course);
if (!$completion->is_enabled($this->cm)) {
return;
}
$completion->set_module_viewed($this->cm);
// 20240706 Added to update completion state after a user adds heat or teacher adds to a students priority/grade.
$cm = $this->cm;
$ci = new completion_info($this->course);
if ($cm->completion == COMPLETION_TRACKING_AUTOMATIC) {
$ci->update_state($cm, COMPLETION_UNKNOWN, null);
}
}
/**
* Finds all hotquestion notifications that have yet to be mailed out, and mails them.
*
* Cron function to be run periodically according to the moodle cron.
*
* @return bool
*/
public function cron() {
$completion = new completion_info($this->course);
if (!$completion->is_enabled($this->cm)) {
return;
}
$completion->set_module_viewed($this->cm);
}
}