Skip to content

Commit c3dcaa1

Browse files
committed
Fix sorting issue with quotes
1 parent 1dd2eda commit c3dcaa1

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/classes/PersistCtrl.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ public static function getInstance($mysqlConn = null, $signedUser = null)
5252
}
5353
return self::$instance;
5454
}
55+
56+
public static function compareString($str1, $str2){
57+
// fix sorting issue with quotes in names (for example: N'Gole) by removing it
58+
$str1 = str_replace("'", "", $str1);
59+
$str2 = str_replace("'", "", $str2);
60+
// // Case insensitive string comparisons using a "natural order" algorithm
61+
return strnatcasecmp($str1, $str2);
62+
}
5563

5664
protected function __construct($mysqlConn, $signedUser){
5765
parent::__construct($mysqlConn, $signedUser);
@@ -304,12 +312,10 @@ public function getStudentFollowUp($courseId, $groupId = 0, $cmVisible = 1){
304312
}
305313

306314
$result = array_values($result);
307-
308-
function cmp($a, $b) {
309-
return strnatcasecmp($a->orderby, $b->orderby);
310-
}
311-
312-
usort($result, "recitdashboard\cmp");
315+
316+
usort($result, function($a, $b) {
317+
return PersistCtrl::compareString($a->orderby, $b->orderby);
318+
});
313319

314320
return $result;
315321
}
@@ -398,14 +404,11 @@ public function reportSectionResults($courseId, $groupId, $cmVisible = 1){
398404
$user->grades = array_values(array_merge($cmList, $user->grades));
399405
}
400406

401-
function cmp1($a, $b) {
402-
// Case insensitive string comparisons using a "natural order" algorithm
403-
return strnatcasecmp("{$a->lastName} {$a->firstName}", "{$b->lastName} {$b->firstName}");
404-
}
405-
406407
$result = array_values($result);
407408

408-
usort($result, "recitdashboard\cmp1");
409+
usort($result, function($a, $b) {
410+
return PersistCtrl::compareString("{$a->lastName} {$a->firstName}", "{$b->lastName} {$b->firstName}");
411+
});
409412

410413
return $result;
411414
}
@@ -456,8 +459,7 @@ public function reportActivityCompletion($courseId, $groupId, $cmVisible = 1){
456459
left join {groups_members} t5 on t3.id = t5.userid
457460
left join {course_modules_completion} t2 on t1.id = t2.coursemoduleid and t3.id = t2.userid
458461
where t1.course = :course and st1.shortname in ('student') $whereStmt
459-
group by t1.course, t1.id, t2.id, t3.id, cm_id, t4.sequence
460-
order by ".$this->mysqlConn->sql_concat("t3.lastname", "' '", "t3.firstname") . " asc ";
462+
group by t1.course, t1.id, t2.id, t3.id, cm_id, t4.sequence";
461463

462464
$tmp = $this->getRecordsSQL($query, $vars);
463465

@@ -485,6 +487,10 @@ public function reportActivityCompletion($courseId, $groupId, $cmVisible = 1){
485487
}
486488
}
487489

490+
usort($result, function($a, $b) {
491+
return PersistCtrl::compareString("{$a->lastName} {$a->firstName}", "{$b->lastName} {$b->firstName}");
492+
});
493+
488494
return array_values($result);
489495
}
490496

@@ -654,15 +660,6 @@ public function reportQuiz($courseId, $activityId, $groupId = 0){
654660
$result->questions = array_values($result->questions);
655661
$result->students = array_values($result->students);
656662

657-
658-
function cmp2($a, $b) {
659-
// fix sorting issue with quotes in names (for example: N'Gole)
660-
// this replaces ' with _ then the function strnatcasecmp considers it greater than letters and numbers
661-
$str1 = str_replace("'", "_", "{$a->lastName} {$a->firstName}");
662-
$str2 = str_replace("'", "_", "{$b->lastName} {$b->firstName}");
663-
return strnatcasecmp($str1, $str2);
664-
}
665-
666663
function cmp3($a, $b) {
667664
if($a->slot == $b->slot){ return 0; }
668665
return ($a->slot < $b->slot) ? -1 : 1;
@@ -674,7 +671,10 @@ function cmp4($a, $b) {
674671
}
675672

676673
// sort by student name and question slot
677-
usort($result->students, "recitdashboard\cmp2");
674+
usort($result->students, function($a, $b) {
675+
return PersistCtrl::compareString("{$a->lastName} {$a->firstName}", "{$b->lastName} {$b->firstName}");
676+
});
677+
678678
usort($result->questions, "recitdashboard\cmp3");
679679

680680
foreach($result->students as $student){

0 commit comments

Comments
 (0)