@@ -42,25 +42,18 @@ class ratings {
42
42
* @param int $postid
43
43
* @param int $rating
44
44
* @param object $cm
45
- * @param null $userid
45
+ * @param int $userid
46
46
*
47
47
* @return bool|int
48
48
*/
49
- public static function moodleoverflow_add_rating ($ moodleoverflow , $ postid , $ rating , $ cm , $ userid = null ) {
50
- global $ DB , $ USER , $ SESSION ;
51
-
52
- // Has a user been submitted?
53
- if (!isset ($ userid )) {
54
- $ userid = $ USER ->id ;
55
- }
49
+ public static function moodleoverflow_add_rating ($ moodleoverflow , $ postid , $ rating , $ cm , $ userid ) {
50
+ global $ DB ;
56
51
57
52
// Is the submitted rating valid?
58
53
$ possibleratings = [RATING_NEUTRAL , RATING_DOWNVOTE , RATING_UPVOTE , RATING_SOLVED ,
59
54
RATING_HELPFUL , RATING_REMOVE_DOWNVOTE , RATING_REMOVE_UPVOTE ,
60
55
RATING_REMOVE_SOLVED , RATING_REMOVE_HELPFUL , ];
61
- if (!in_array ($ rating , $ possibleratings )) {
62
- throw new moodle_exception ('invalidratingid ' , 'moodleoverflow ' );
63
- }
56
+ moodleoverflow_throw_exception_with_check (!in_array ($ rating , $ possibleratings ), 'invalidratingid ' );
64
57
65
58
// Get the related post.
66
59
$ post = moodleoverflow_get_record_or_exception ('moodleoverflow_posts ' , ['id ' => $ postid ], 'invalidparentpostid ' );
@@ -85,26 +78,17 @@ public static function moodleoverflow_add_rating($moodleoverflow, $postid, $rati
85
78
if (!self ::moodleoverflow_user_can_rate ($ post , $ modulecontext , $ userid )) {
86
79
87
80
// Catch unenrolled users.
88
- if (!isguestuser () && !is_enrolled ($ coursecontext )) {
89
- $ SESSION ->wantsurl = qualified_me ();
90
- $ SESSION ->enrolcancel = get_local_referer (false );
91
- redirect (new \moodle_url ('/enrol/index.php ' , [
92
- 'id ' => $ course ->id ,
93
- 'returnurl ' => '/mod/moodleoverflow/view.php?m ' . $ moodleoverflow ->id ,
94
- ]), get_string ('youneedtoenrol ' ));
95
- }
81
+ $ returnurl = '/mod/moodleoverflow/view.php?m ' . $ moodleoverflow ->id ;
82
+ moodleoverflow_catch_unenrolled_user ($ coursecontext , $ course ->id , $ returnurl );
96
83
97
84
// Notify the user, that he can not post a new discussion.
98
85
throw new moodle_exception ('noratemoodleoverflow ' , 'moodleoverflow ' );
99
86
}
100
87
101
88
// Make sure post author != current user, unless they have permission.
102
- if (($ post ->userid == $ userid ) && !
103
- (($ rating == RATING_SOLVED || $ rating == RATING_REMOVE_SOLVED ) &&
104
- has_capability ('mod/moodleoverflow:marksolved ' , $ modulecontext ))
105
- ) {
106
- throw new moodle_exception ('rateownpost ' , 'moodleoverflow ' );
107
- }
89
+ $ authorcheck = ($ post ->userid == $ userid ) && ! (($ rating == RATING_SOLVED || $ rating == RATING_REMOVE_SOLVED ) &&
90
+ has_capability ('mod/moodleoverflow:marksolved ' , $ modulecontext ));
91
+ moodleoverflow_throw_exception_with_check ($ authorcheck , 'rateownpost ' );
108
92
109
93
// Check if we are removing a mark.
110
94
if (in_array ($ rating / 10 , $ possibleratings )) {
@@ -121,15 +105,13 @@ public static function moodleoverflow_add_rating($moodleoverflow, $postid, $rati
121
105
// Mark a post as solution or as helpful.
122
106
if ($ rating == RATING_SOLVED || $ rating == RATING_HELPFUL ) {
123
107
124
- // Check if the current user is the startuser.
125
- if ($ rating == RATING_HELPFUL && $ userid != $ discussion ->userid ) {
126
- throw new moodle_exception ('notstartuser ' , 'moodleoverflow ' );
127
- }
108
+ // Make sure that a helpful mark is made by the user who started the discussion.
109
+ $ isnotstartuser = $ rating == RATING_HELPFUL && $ userid != $ discussion ->userid ;
110
+ moodleoverflow_throw_exception_with_check ($ isnotstartuser , 'nostartuser ' );
128
111
129
- // Check if the current user is a teacher.
130
- if ($ rating == RATING_SOLVED && !has_capability ('mod/moodleoverflow:marksolved ' , $ modulecontext )) {
131
- throw new moodle_exception ('notteacher ' , 'moodleoverflow ' );
132
- }
112
+ // Make sure that a solution mark is made by a teacher (or someone with the right capability).
113
+ $ isnotteacher = $ rating == RATING_SOLVED && !has_capability ('mod/moodleoverflow:marksolved ' , $ modulecontext );
114
+ moodleoverflow_throw_exception_with_check ($ isnotteacher , 'notteacher ' );
133
115
134
116
// Check if multiple marks are not enabled.
135
117
if (!$ multiplemarks ) {
@@ -145,22 +127,18 @@ public static function moodleoverflow_add_rating($moodleoverflow, $postid, $rati
145
127
return self ::moodleoverflow_update_rating_record ($ post ->id , $ rating , $ userid , $ otherrating ->id , $ modulecontext );
146
128
147
129
} else {
148
- $ mid = $ moodleoverflow ->id ;
149
-
150
- return self ::moodleoverflow_add_rating_record ($ mid , $ discussion ->id , $ post ->id ,
130
+ return self ::moodleoverflow_add_rating_record ($ moodleoverflow ->id , $ discussion ->id , $ post ->id ,
151
131
$ rating , $ userid , $ modulecontext );
152
132
}
153
-
154
133
} else {
155
134
// If multiplemarks are allowed, only create a new rating.
156
- $ mid = $ moodleoverflow ->id ;
157
- return self :: moodleoverflow_add_rating_record ( $ mid , $ discussion -> id , $ post -> id , $ rating , $ userid , $ modulecontext );
135
+ return self :: moodleoverflow_add_rating_record ( $ moodleoverflow -> id , $ discussion -> id , $ post ->id ,
136
+ $ rating , $ userid , $ modulecontext );
158
137
}
159
138
}
160
139
161
140
// Update an rating record.
162
141
if ($ oldrating ['normal ' ]) {
163
-
164
142
moodleoverflow_get_config_or_exception ('moodleoverflow ' , 'allowratingchange ' ,
165
143
'noratingchangeallowed ' , 'moodleoverflow ' );
166
144
@@ -174,35 +152,25 @@ public static function moodleoverflow_add_rating($moodleoverflow, $postid, $rati
174
152
}
175
153
176
154
// Create a new rating record.
177
- $ mid = $ moodleoverflow ->id ;
178
- $ did = $ post ->discussion ;
179
-
180
- return self ::moodleoverflow_add_rating_record ($ mid , $ did , $ postid , $ rating , $ userid , $ modulecontext );
155
+ return self ::moodleoverflow_add_rating_record ($ moodleoverflow ->id , $ post ->discussion , $ postid ,
156
+ $ rating , $ userid , $ modulecontext );
181
157
}
182
158
183
159
/**
184
160
* Get the reputation of a user.
185
161
* Whether within a course or an instance is decided by the settings.
186
162
*
187
163
* @param int $moodleoverflowid
188
- * @param null $userid
164
+ * @param int $userid
189
165
* @param bool $forcesinglerating If true you only get the reputation for the given $moodleoverflowid,
190
166
* even if coursewidereputation = true
191
167
*
192
168
* @return int
193
169
*/
194
- public static function moodleoverflow_get_reputation ($ moodleoverflowid , $ userid = null , $ forcesinglerating = false ) {
195
- global $ DB , $ USER ;
196
-
197
- // Get the user id.
198
- if (!isset ($ userid )) {
199
- $ userid = $ USER ->id ;
200
- }
201
-
170
+ public static function moodleoverflow_get_reputation ($ moodleoverflowid , $ userid , $ forcesinglerating = false ) {
202
171
// Check the moodleoverflow instance.
203
- if (!$ moodleoverflow = $ DB ->get_record ('moodleoverflow ' , ['id ' => $ moodleoverflowid ])) {
204
- throw new moodle_exception ('invalidmoodleoverflowid ' , 'moodleoverflow ' );
205
- }
172
+ $ moodleoverflow = moodleoverflow_get_record_or_exception ('moodleoverflow ' , ['id ' => $ moodleoverflowid ],
173
+ 'invalidmoodleoverflowid ' );
206
174
207
175
// Check whether the reputation can be summed over the whole course.
208
176
if ($ moodleoverflow ->coursewidereputation && !$ forcesinglerating ) {
@@ -417,39 +385,38 @@ public static function moodleoverflow_get_reputation_instance($moodleoverflowid,
417
385
// Initiate a variable.
418
386
$ reputation = 0 ;
419
387
420
- if ( $ moodleoverflow -> anonymous != anonymous:: EVERYTHING_ANONYMOUS ) {
421
- // Get all posts of this user in this module .
422
- // Do not count votes for own posts.
423
- $ sql = " SELECT r.id, r.postid as post, r.rating
424
- FROM {moodleoverflow_posts} p
425
- JOIN {moodleoverflow_ratings} r ON p.id = r.postid
426
- WHERE p.userid = ? AND NOT r.userid = ? AND r.moodleoverflowid = ? " ;
388
+ // Get all posts of this user in this module.
389
+ // Do not count votes for own posts .
390
+ $ sql = " SELECT r.id, r.postid as post, r.rating
391
+ FROM {moodleoverflow_posts} p
392
+ JOIN {moodleoverflow_ratings} r ON p.id = r.postid
393
+ JOIN {moodleoverflow} m ON r.moodleoverflowid = m.id
394
+ WHERE p.userid = ? AND NOT r.userid = ? AND r.moodleoverflowid = ? AND m.anonymous <> ? " ;
427
395
428
- if ($ moodleoverflow ->anonymous == anonymous::QUESTION_ANONYMOUS ) {
429
- $ sql .= " AND p.parent <> 0 " ;
430
- }
396
+ if ($ moodleoverflow ->anonymous == anonymous::QUESTION_ANONYMOUS ) {
397
+ $ sql .= " AND p.parent <> 0 " ;
398
+ }
431
399
432
- $ sql .= "ORDER BY r.postid ASC " ;
433
-
434
- $ params = [$ userid , $ userid , $ moodleoverflowid ];
435
- $ records = $ DB ->get_records_sql ($ sql , $ params );
436
-
437
- // Iterate through all ratings.
438
- foreach ($ records as $ record ) {
439
- switch ($ record ->rating ) {
440
- case RATING_DOWNVOTE :
441
- $ reputation += get_config ('moodleoverflow ' , 'votescaledownvote ' );
442
- break ;
443
- case RATING_UPVOTE :
444
- $ reputation += get_config ('moodleoverflow ' , 'votescaleupvote ' );
445
- break ;
446
- case RATING_HELPFUL :
447
- $ reputation += get_config ('moodleoverflow ' , 'votescalehelpful ' );
448
- break ;
449
- case RATING_SOLVED :
450
- $ reputation += get_config ('moodleoverflow ' , 'votescalesolved ' );
451
- break ;
452
- }
400
+ $ sql .= "ORDER BY r.postid ASC " ;
401
+
402
+ $ params = [$ userid , $ userid , $ moodleoverflowid , anonymous::EVERYTHING_ANONYMOUS ];
403
+ $ records = $ DB ->get_records_sql ($ sql , $ params );
404
+
405
+ // Iterate through all ratings.
406
+ foreach ($ records as $ record ) {
407
+ switch ($ record ->rating ) {
408
+ case RATING_DOWNVOTE :
409
+ $ reputation += get_config ('moodleoverflow ' , 'votescaledownvote ' );
410
+ break ;
411
+ case RATING_UPVOTE :
412
+ $ reputation += get_config ('moodleoverflow ' , 'votescaleupvote ' );
413
+ break ;
414
+ case RATING_HELPFUL :
415
+ $ reputation += get_config ('moodleoverflow ' , 'votescalehelpful ' );
416
+ break ;
417
+ case RATING_SOLVED :
418
+ $ reputation += get_config ('moodleoverflow ' , 'votescalesolved ' );
419
+ break ;
453
420
}
454
421
}
455
422
@@ -527,33 +494,29 @@ private static function moodleoverflow_check_old_rating($postid, $userid, $oldra
527
494
// Initiate the array.
528
495
$ rating = [];
529
496
530
- // Get the normal rating.
531
497
$ sql = "SELECT *
532
- FROM {moodleoverflow_ratings}
533
- WHERE userid = ? AND postid = ? AND (rating = 1 OR rating = 2) " ;
534
- $ rating ['normal ' ] = $ DB ->get_record_sql ($ sql , [ $ userid , $ postid ]);
498
+ FROM {moodleoverflow_ratings} " ;
499
+ // Get the normal rating.
500
+ $ condition = "WHERE userid = ? AND postid = ? AND (rating = 1 OR rating = 2) " ;
501
+ $ rating ['normal ' ] = $ DB ->get_record_sql ($ sql . $ condition , [ $ userid , $ postid ]);
535
502
536
503
// Return the rating if it is requested.
537
504
if ($ oldrating == RATING_DOWNVOTE || $ oldrating == RATING_UPVOTE ) {
538
505
return $ rating ['normal ' ];
539
506
}
540
507
541
508
// Get the solved rating.
542
- $ sql = "SELECT *
543
- FROM {moodleoverflow_ratings}
544
- WHERE postid = ? AND rating = 3 " ;
545
- $ rating ['solved ' ] = $ DB ->get_record_sql ($ sql , [ $ postid ]);
509
+ $ condition = "WHERE postid = ? AND rating = 3 " ;
510
+ $ rating ['solved ' ] = $ DB ->get_record_sql ($ sql . $ condition , [ $ postid ]);
546
511
547
512
// Return the rating if it is requested.
548
513
if ($ oldrating == RATING_SOLVED ) {
549
514
return $ rating ['solved ' ];
550
515
}
551
516
552
517
// Get the helpful rating.
553
- $ sql = "SELECT *
554
- FROM {moodleoverflow_ratings}
555
- WHERE postid = ? AND rating = 4 " ;
556
- $ rating ['helpful ' ] = $ DB ->get_record_sql ($ sql , [ $ postid ]);
518
+ $ condition = "WHERE postid = ? AND rating = 4 " ;
519
+ $ rating ['helpful ' ] = $ DB ->get_record_sql ($ sql . $ condition , [ $ postid ]);
557
520
558
521
// Return the rating if it is requested.
559
522
if ($ oldrating == RATING_HELPFUL ) {
@@ -585,7 +548,6 @@ private static function moodleoverflow_can_be_changed($postid, $rating, $userid)
585
548
586
549
/**
587
550
* Removes a rating record.
588
- *
589
551
* @param int $postid
590
552
* @param int $rating
591
553
* @param int $userid
@@ -605,11 +567,7 @@ private static function moodleoverflow_remove_rating($postid, $rating, $userid,
605
567
$ oldrecord = self ::moodleoverflow_check_old_rating ($ postid , $ userid , $ rating );
606
568
607
569
// Trigger an event.
608
- $ params = [
609
- 'objectid ' => $ oldrecord ->id ,
610
- 'context ' => $ modulecontext ,
611
- ];
612
- $ event = \mod_moodleoverflow \event \rating_deleted::create ($ params );
570
+ $ event = \mod_moodleoverflow \event \rating_deleted::create (['objectid ' => $ oldrecord ->id , 'context ' => $ modulecontext ]);
613
571
$ event ->add_record_snapshot ('moodleoverflow_ratings ' , $ oldrecord );
614
572
$ event ->trigger ();
615
573
0 commit comments