Skip to content

Commit 7420b34

Browse files
committed
reduce php mess in ratings class
1 parent 6c3be91 commit 7420b34

File tree

4 files changed

+108
-109
lines changed

4 files changed

+108
-109
lines changed

classes/ratings.php

Lines changed: 63 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,18 @@ class ratings {
4242
* @param int $postid
4343
* @param int $rating
4444
* @param object $cm
45-
* @param null $userid
45+
* @param int $userid
4646
*
4747
* @return bool|int
4848
*/
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;
5651

5752
// Is the submitted rating valid?
5853
$possibleratings = [RATING_NEUTRAL, RATING_DOWNVOTE, RATING_UPVOTE, RATING_SOLVED,
5954
RATING_HELPFUL, RATING_REMOVE_DOWNVOTE, RATING_REMOVE_UPVOTE,
6055
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');
6457

6558
// Get the related post.
6659
$post = moodleoverflow_get_record_or_exception('moodleoverflow_posts', ['id' => $postid], 'invalidparentpostid');
@@ -85,26 +78,17 @@ public static function moodleoverflow_add_rating($moodleoverflow, $postid, $rati
8578
if (!self::moodleoverflow_user_can_rate($post, $modulecontext, $userid)) {
8679

8780
// 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);
9683

9784
// Notify the user, that he can not post a new discussion.
9885
throw new moodle_exception('noratemoodleoverflow', 'moodleoverflow');
9986
}
10087

10188
// 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');
10892

10993
// Check if we are removing a mark.
11094
if (in_array($rating / 10, $possibleratings)) {
@@ -121,15 +105,13 @@ public static function moodleoverflow_add_rating($moodleoverflow, $postid, $rati
121105
// Mark a post as solution or as helpful.
122106
if ($rating == RATING_SOLVED || $rating == RATING_HELPFUL) {
123107

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');
128111

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');
133115

134116
// Check if multiple marks are not enabled.
135117
if (!$multiplemarks) {
@@ -145,22 +127,18 @@ public static function moodleoverflow_add_rating($moodleoverflow, $postid, $rati
145127
return self::moodleoverflow_update_rating_record($post->id, $rating, $userid, $otherrating->id, $modulecontext);
146128

147129
} 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,
151131
$rating, $userid, $modulecontext);
152132
}
153-
154133
} else {
155134
// 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);
158137
}
159138
}
160139

161140
// Update an rating record.
162141
if ($oldrating['normal']) {
163-
164142
moodleoverflow_get_config_or_exception('moodleoverflow', 'allowratingchange',
165143
'noratingchangeallowed', 'moodleoverflow');
166144

@@ -174,35 +152,25 @@ public static function moodleoverflow_add_rating($moodleoverflow, $postid, $rati
174152
}
175153

176154
// 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);
181157
}
182158

183159
/**
184160
* Get the reputation of a user.
185161
* Whether within a course or an instance is decided by the settings.
186162
*
187163
* @param int $moodleoverflowid
188-
* @param null $userid
164+
* @param int $userid
189165
* @param bool $forcesinglerating If true you only get the reputation for the given $moodleoverflowid,
190166
* even if coursewidereputation = true
191167
*
192168
* @return int
193169
*/
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) {
202171
// 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');
206174

207175
// Check whether the reputation can be summed over the whole course.
208176
if ($moodleoverflow->coursewidereputation && !$forcesinglerating) {
@@ -417,39 +385,38 @@ public static function moodleoverflow_get_reputation_instance($moodleoverflowid,
417385
// Initiate a variable.
418386
$reputation = 0;
419387

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 <> ?";
427395

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+
}
431399

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;
453420
}
454421
}
455422

@@ -527,33 +494,29 @@ private static function moodleoverflow_check_old_rating($postid, $userid, $oldra
527494
// Initiate the array.
528495
$rating = [];
529496

530-
// Get the normal rating.
531497
$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 ]);
535502

536503
// Return the rating if it is requested.
537504
if ($oldrating == RATING_DOWNVOTE || $oldrating == RATING_UPVOTE) {
538505
return $rating['normal'];
539506
}
540507

541508
// 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 ]);
546511

547512
// Return the rating if it is requested.
548513
if ($oldrating == RATING_SOLVED) {
549514
return $rating['solved'];
550515
}
551516

552517
// 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 ]);
557520

558521
// Return the rating if it is requested.
559522
if ($oldrating == RATING_HELPFUL) {
@@ -585,7 +548,6 @@ private static function moodleoverflow_can_be_changed($postid, $rating, $userid)
585548

586549
/**
587550
* Removes a rating record.
588-
*
589551
* @param int $postid
590552
* @param int $rating
591553
* @param int $userid
@@ -605,11 +567,7 @@ private static function moodleoverflow_remove_rating($postid, $rating, $userid,
605567
$oldrecord = self::moodleoverflow_check_old_rating($postid, $userid, $rating);
606568

607569
// 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]);
613571
$event->add_record_snapshot('moodleoverflow_ratings', $oldrecord);
614572
$event->trigger();
615573

discussion.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
* @copyright 2017 Kennet Winter <[email protected]>
2222
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2323
*/
24+
defined('MOODLE_INTERNAL') || die();
25+
26+
global $CFG, $PAGE, $USER, $SESSION, $OUTPUT;
2427

2528
// Include config and locallib.
2629
require_once('../../config.php');
@@ -83,7 +86,7 @@
8386

8487
if (in_array($ratingid, [RATING_SOLVED, RATING_REMOVE_SOLVED, RATING_HELPFUL, RATING_REMOVE_HELPFUL])) {
8588
// Rate the post.
86-
if (!\mod_moodleoverflow\ratings::moodleoverflow_add_rating($moodleoverflow, $ratedpost, $ratingid, $cm)) {
89+
if (!\mod_moodleoverflow\ratings::moodleoverflow_add_rating($moodleoverflow, $ratedpost, $ratingid, $cm, $USER->id)) {
8790
throw new moodle_exception('ratingfailed', 'moodleoverflow');
8891
}
8992

externallib.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public static function record_vote($postid, $ratingid) {
114114

115115
// Rate the post.
116116
if (!\mod_moodleoverflow\ratings::moodleoverflow_add_rating($moodleoverflow,
117-
$params['postid'], $params['ratingid'], $cm)) {
117+
$params['postid'], $params['ratingid'], $cm, $USER->id)) {
118118
throw new moodle_exception('ratingfailed', 'moodleoverflow');
119119
}
120120

locallib.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,14 +2188,14 @@ function moodleoverflow_quick_array_sort(&$array, $low, $high, $key, $order) {
21882188
* @param array $options Conditions for the record
21892189
* @param string $exceptionstring Name of the moodleoverflow exception that should be thrown in case there is no record.
21902190
* @param string $fields Optional fields that are retrieved from the found record.
2191-
* @param bool $coreexception Optional param if exception from the core exceptions.
2191+
* @param bool $coreexception Optional param if exception is from the core exceptions.
21922192
* @return mixed $record The found record
21932193
*/
21942194
function moodleoverflow_get_record_or_exception($table, $options, $exceptionstring, $fields = '*', $coreexception = false) {
21952195
global $DB;
21962196
if (!$record = $DB->get_record($table, $options, $fields)) {
21972197
if ($coreexception) {
2198-
throw new moodle_exception('invalidcourseid');
2198+
throw new moodle_exception($exceptionstring);
21992199
} else {
22002200
throw new moodle_exception($exceptionstring, 'moodleoverflow');
22012201
}
@@ -2217,3 +2217,41 @@ function moodleoverflow_get_config_or_exception($plugin, $configname, $errorcode
22172217
}
22182218
return $config;
22192219
}
2220+
2221+
/**
2222+
* Function that throws an exception if a given check is true.
2223+
* @param bool $check The result of a boolean check.
2224+
* @param string $errorcode Error code/name of the exception
2225+
* @param string $coreexception Optional param if exception is from the core exceptions and not moodleoverflow.
2226+
* @return void
2227+
*/
2228+
function moodleoverflow_throw_exception_with_check($check, $errorcode, $coreexception = false) {
2229+
if ($check) {
2230+
if ($coreexception) {
2231+
throw new moodle_exception($errorcode);
2232+
} else {
2233+
throw new moodle_exception($errorcode, 'moodleoverflow');
2234+
}
2235+
}
2236+
}
2237+
2238+
/**
2239+
* Function that catches unenrolled users and redirects them to the enrolment page.
2240+
* @param context $coursecontext The context of the course.
2241+
* @param int $courseid Id of the course that the user needs to enrol.
2242+
* @param string $returnurl The url to return to after the user has been enrolled.
2243+
* @return void
2244+
*/
2245+
function moodleoverflow_catch_unenrolled_user($coursecontext, $courseid, $returnurl) {
2246+
global $SESSION;
2247+
if (!isguestuser() && !is_enrolled($coursecontext)) {
2248+
if (enrol_selfenrol_available($courseid)) {
2249+
$SESSION->wantsurl = qualified_me();
2250+
$SESSION->enrolcancel = get_local_referer(false);
2251+
redirect(new \moodle_url('/enrol/index.php', [
2252+
'id' => $courseid,
2253+
'returnurl' => $returnurl,
2254+
]), get_string('youneedtoenrol'));
2255+
}
2256+
}
2257+
}

0 commit comments

Comments
 (0)