MDL-43739 block_course_overview: allow resorting of big number of courses

previously an exception was thrown if the user preference exceeds 1333 chars
This commit is contained in:
Marina Glancy 2014-08-08 14:32:41 +08:00
parent 457f818026
commit 1302bb08f6

View File

@ -59,10 +59,34 @@ function block_course_overview_update_mynumber($number) {
/**
* Sets user course sorting preference in course_overview block
*
* @param array $sortorder sort order of course
* @param array $sortorder list of course ids
*/
function block_course_overview_update_myorder($sortorder) {
set_user_preference('course_overview_course_order', serialize($sortorder));
$value = implode(',', $sortorder);
if (core_text::strlen($value) > 1333) {
// The value won't fit into the user preference. Remove courses in the end of the list (mostly likely user won't even notice).
$value = preg_replace('/,[\d]*$/', '', core_text::substr($value, 0, 1334));
}
set_user_preference('course_overview_course_sortorder', $value);
}
/**
* Gets user course sorting preference in course_overview block
*
* @return array list of course ids
*/
function block_course_overview_get_myorder() {
if ($value = get_user_preferences('course_overview_course_sortorder')) {
return explode(',', $value);
}
// If preference was not found, look in the old location and convert if found.
$order = array();
if ($value = get_user_preferences('course_overview_course_order')) {
$order = unserialize($value);
block_course_overview_update_myorder($order);
unset_user_preference('course_overview_course_order');
}
return $order;
}
/**
@ -167,10 +191,7 @@ function block_course_overview_get_sorted_courses($showallcourses = false) {
$courses[$remoteid] = $val;
}
$order = array();
if (!is_null($usersortorder = get_user_preferences('course_overview_course_order'))) {
$order = unserialize($usersortorder);
}
$order = block_course_overview_get_myorder();
$sortedcourses = array();
$counter = 0;