/** * This private method is used by the usort function in the * orderSentWork and orderReceivedWork methods. * It compares 2 work-objects by 1 of the properties of that object, dictated by the * private property _orderBy * * @param unknown_type $a * @param unknown_type $b * @return -1, 0 or 1 dependent of the result of the comparison. */ function _cmpWork($a, $b) { $sort = $this->_orderBy; $aval = $a->{$sort}; $bval = $b->{$sort}; if ($sort == 'recipients') { // The recipients property is an array so we do the comparison based on the first item of the recipients array $aval = $aval[0]['name']; $bval = $bval[0]['name']; } if ($sort == 'filesize') { // Filesize is not a string, so we use other comparison technique return $aval < $bval ? -1 : 1; } elseif ($sort == 'title') { // Natural order for sorting titles is more "human-friendly" return api_strnatcmp($aval, $bval); } else { return api_strcasecmp($aval, $bval); } }
/** * Sort courses for a specific user ?? * @param int User ID * @param string Course code * @return int Minimum course order * @todo Review documentation */ public static function userCourseSort($user_id, $course_code) { if ($user_id != strval(intval($user_id))) { return false; } $course_code = Database::escape_string($course_code); $TABLECOURSE = Database::get_main_table(TABLE_MAIN_COURSE); $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER); $course_title = Database::result(Database::query('SELECT title FROM ' . $TABLECOURSE . ' WHERE code="' . $course_code . '"'), 0, 0); $sql = 'SELECT course.code as code, course.title as title, cu.sort as sort FROM ' . $TABLECOURSUSER . ' as cu, ' . $TABLECOURSE . ' as course WHERE course.id = cu.c_id AND user_id = "' . $user_id . '" AND cu.relation_type<>' . COURSE_RELATION_TYPE_RRHH . ' AND user_course_cat = 0 ORDER BY cu.sort'; $result = Database::query($sql); $course_title_precedent = ''; $counter = 0; $course_found = false; $course_sort = 1; if (Database::num_rows($result) > 0) { while ($courses = Database::fetch_array($result)) { if ($course_title_precedent == '') { $course_title_precedent = $courses['title']; } if (api_strcasecmp($course_title_precedent, $course_title) < 0) { $course_found = true; $course_sort = $courses['sort']; if ($counter == 0) { $sql = 'UPDATE ' . $TABLECOURSUSER . ' SET sort = sort+1 WHERE user_id= "' . $user_id . '" AND relation_type<>' . COURSE_RELATION_TYPE_RRHH . ' AND user_course_cat="0" AND sort > "' . $course_sort . '"'; $course_sort++; } else { $sql = 'UPDATE ' . $TABLECOURSUSER . ' SET sort = sort+1 WHERE user_id= "' . $user_id . '" AND relation_type<>' . COURSE_RELATION_TYPE_RRHH . ' AND user_course_cat="0" AND sort >= "' . $course_sort . '"'; } Database::query($sql); break; } else { $course_title_precedent = $courses['title']; } $counter++; } // We must register the course in the beginning of the list if (!$course_found) { $course_sort = Database::result(Database::query('SELECT min(sort) as min_sort FROM ' . $TABLECOURSUSER . ' WHERE user_id="' . $user_id . '" AND user_course_cat="0"'), 0, 0); Database::query('UPDATE ' . $TABLECOURSUSER . ' SET sort = sort+1 WHERE user_id= "' . $user_id . '" AND user_course_cat="0"'); } } return $course_sort; }