Minor - format code, skip file in codeclimate, scrutinizer

pull/2487/head
jmontoyaa 8 years ago
parent 483e46fc8b
commit 55883ca7b3
  1. 1
      .codeclimate.yml
  2. 1
      .scrutinizer.yml
  3. 65
      main/inc/lib/groupmanager.lib.php
  4. 107
      main/inc/lib/text.lib.php

@ -31,6 +31,7 @@ exclude_paths:
- app/cache/
- app/logs/
- app/config/
- app/SymfonyRequirements.php
- app/Migrations/
- app/Resources/
- main/admin/db.php

@ -130,6 +130,7 @@ filter:
- 'app/cache/*'
- 'app/logs/*'
- 'app/config/*'
- 'app/SymfonyRequirements.php'
- 'documentation/*'
- 'app/Migrations/*'
- 'app/Resources/*'

@ -157,6 +157,8 @@ class GroupManager
* @param int $category_id
* @param int $tutor The user-id of the group's tutor
* @param int $places How many people can subscribe to the new group
*
* @return int
*/
public static function create_group($name, $category_id, $tutor, $places)
{
@ -473,7 +475,6 @@ class GroupManager
$db_object = Database::fetch_object($db_result);
$result = array();
if ($db_object) {
$result['id'] = $db_object->id;
$result['iid'] = $db_object->iid;
@ -575,10 +576,10 @@ class GroupManager
* @param int Forum tool's visibility (0=none,1=private,2=public)
* @param int Wiki tool's visibility (0=none,1=private,2=public)
* @param int Chat tool's visibility (0=none,1=private,2=public)
* @param bool Whether self registration is allowed or not
* @param bool Whether self unregistration is allowed or not
* @param int $categoryId
* @return bool TRUE if properties are successfully changed, false otherwise
* @param bool Whether self registration is allowed or not
* @param bool Whether self unregistration is allowed or not
* @param int $categoryId
* @return bool TRUE if properties are successfully changed, false otherwise
*/
public static function set_group_properties(
$group_id,
@ -733,7 +734,7 @@ class GroupManager
* current course)
* @return array The category
*/
public static function get_category_from_group($group_id, $course_code = null)
public static function get_category_from_group($group_id, $course_code = '')
{
$table_group = Database::get_course_table(TABLE_GROUP);
$table_group_cat = Database::get_course_table(TABLE_GROUP_CATEGORY);
@ -771,10 +772,14 @@ class GroupManager
* @param int $cat_id The id of the category to delete
* @param string $course_code The code in which the category should be
* deleted (default = current course)
* @return bool
*/
public static function delete_category($cat_id, $course_code = null)
public static function delete_category($cat_id, $course_code = '')
{
$course_info = api_get_course_info($course_code);
if (empty($course_info)) {
return false;
}
$course_id = $course_info['real_id'];
$table_group = Database::get_course_table(TABLE_GROUP);
@ -792,6 +797,8 @@ class GroupManager
$sql = "DELETE FROM $table_group_cat
WHERE c_id = $course_id AND id='".$cat_id."'";
Database::query($sql);
return true;
}
/**
@ -829,11 +836,11 @@ class GroupManager
if (empty($title)) {
return false;
}
$table_group_category = Database::get_course_table(TABLE_GROUP_CATEGORY);
$table = Database::get_course_table(TABLE_GROUP_CATEGORY);
$course_id = api_get_course_int_id();
$sql = "SELECT MAX(display_order)+1 as new_order
FROM $table_group_category
FROM $table
WHERE c_id = $course_id ";
$res = Database::query($sql);
$obj = Database::fetch_object($res);
@ -859,9 +866,9 @@ class GroupManager
'max_student' => $maximum_number_of_students
];
$categoryId = Database::insert($table_group_category, $params);
$categoryId = Database::insert($table, $params);
if ($categoryId) {
$sql = "UPDATE $table_group_category SET id = iid
$sql = "UPDATE $table SET id = iid
WHERE iid = $categoryId";
Database::query($sql);
@ -1181,7 +1188,7 @@ class GroupManager
* @param array $groups list of group ids
* @return array list of user ids
*/
public static function get_groups_users($groups = array())
public static function get_groups_users($groups = [])
{
$result = array();
$tbl_group_user = Database::get_course_table(TABLE_GROUP_USER);
@ -1355,7 +1362,8 @@ class GroupManager
$table_group = Database::get_course_table(TABLE_GROUP);
$group_id = intval($group_id);
$course_id = api_get_course_int_id();
$sql = "SELECT max_student FROM $table_group WHERE c_id = $course_id AND iid = $group_id";
$sql = "SELECT max_student FROM $table_group
WHERE c_id = $course_id AND iid = $group_id";
$db_result = Database::query($sql);
$db_object = Database::fetch_object($db_result);
if ($db_object->max_student == 0) {
@ -1490,8 +1498,11 @@ class GroupManager
*
* @return bool TRUE if given user can be subscribed in given group
*/
public static function can_user_subscribe($user_id, $groupInfo, $checkMaxNumberStudents = true)
{
public static function can_user_subscribe(
$user_id,
$groupInfo,
$checkMaxNumberStudents = true
) {
$group_id = $groupInfo['id'];
$groupIid = $groupInfo['iid'];
if ($checkMaxNumberStudents) {
@ -1658,14 +1669,14 @@ class GroupManager
/**
* Subscribe tutor(s) to a specified group in current course
* @param mixed $user_ids Can be an array with user-id's or a single user-id
* @param int $group_id id
* @param array $groupInfo
* @param int $course_id
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @see subscribe_users. This function is almost an exact copy of that function.
* @return bool TRUE if successful
*/
public static function subscribe_tutors($user_ids, $groupInfo, $course_id = null)
public static function subscribe_tutors($user_ids, $groupInfo, $course_id = 0)
{
$user_ids = is_array($user_ids) ? $user_ids : array($user_ids);
$result = true;
@ -2014,8 +2025,7 @@ class GroupManager
return false;
}
if (
self::user_has_access($userId, $groupId, self::GROUP_TOOL_FORUM) ||
if (self::user_has_access($userId, $groupId, self::GROUP_TOOL_FORUM) ||
self::user_has_access($userId, $groupId, self::GROUP_TOOL_DOCUMENTS) ||
self::user_has_access($userId, $groupId, self::GROUP_TOOL_CALENDAR) ||
self::user_has_access($userId, $groupId, self::GROUP_TOOL_ANNOUNCEMENT) ||
@ -2023,7 +2033,6 @@ class GroupManager
self::user_has_access($userId, $groupId, self::GROUP_TOOL_WIKI) ||
self::user_has_access($userId, $groupId, self::GROUP_TOOL_CHAT)
) {
return true;
}
@ -2144,10 +2153,20 @@ class GroupManager
$complete_user_list[$i]['number_groups_left'] = $number_groups_per_user - $number_of_groups;
}
//first sort by user_id to filter out duplicates
$complete_user_list = TableSort::sort_table($complete_user_list, 'user_id');
$complete_user_list = self::filter_duplicates($complete_user_list, 'user_id');
$complete_user_list = TableSort::sort_table(
$complete_user_list,
'user_id'
);
$complete_user_list = self::filter_duplicates(
$complete_user_list,
'user_id'
);
//now sort by # of group left
$complete_user_list = TableSort::sort_table($complete_user_list, 'number_groups_left', SORT_DESC);
$complete_user_list = TableSort::sort_table(
$complete_user_list,
'number_groups_left',
SORT_DESC
);
return $complete_user_list;
}

@ -1,5 +1,6 @@
<?php
/* For licensing terms, see /license.txt */
/**
* This is the text library for Chamilo.
* It is loaded during the global initialization,
@ -108,7 +109,8 @@ define('_PCRE_XML_ENCODING', '/<\?xml.*encoding=[\'"](.*?)[\'"].*\?>/m');
* @return string Returns the detected encoding.
* @todo The second parameter is to be eliminated. See api_detect_encoding_html().
*/
function api_detect_encoding_xml($string, $default_encoding = null) {
function api_detect_encoding_xml($string, $default_encoding = null)
{
if (preg_match(_PCRE_XML_ENCODING, $string, $matches)) {
return api_refine_encoding_id($matches[1]);
}
@ -129,7 +131,8 @@ function api_detect_encoding_xml($string, $default_encoding = null) {
* @param string $from_encoding (optional) The encoding that text is being converted from. If it is omited, it is tried to be detected then.
* @return string Returns the converted xml-text.
*/
function api_convert_encoding_xml($string, $to_encoding, $from_encoding = null) {
function api_convert_encoding_xml($string, $to_encoding, $from_encoding = null)
{
return _api_convert_encoding_xml($string, $to_encoding, $from_encoding);
}
@ -139,7 +142,8 @@ function api_convert_encoding_xml($string, $to_encoding, $from_encoding = null)
* @param string $from_encoding (optional) The encoding that text is being converted from. If it is omited, it is tried to be detected then.
* @return string Returns the converted xml-text.
*/
function api_utf8_encode_xml($string, $from_encoding = null) {
function api_utf8_encode_xml($string, $from_encoding = null)
{
return _api_convert_encoding_xml($string, 'UTF-8', $from_encoding);
}
@ -149,7 +153,8 @@ function api_utf8_encode_xml($string, $from_encoding = null) {
* @param string $to_encoding (optional) The encoding that text is being converted to. If it is omited, the platform character set is assumed.
* @return string Returns the converted xml-text.
*/
function api_utf8_decode_xml($string, $to_encoding = 'UTF-8') {
function api_utf8_decode_xml($string, $to_encoding = 'UTF-8')
{
return _api_convert_encoding_xml($string, $to_encoding, 'UTF-8');
}
@ -160,7 +165,8 @@ function api_utf8_decode_xml($string, $to_encoding = 'UTF-8') {
* @param string $from_encoding (optional) The encoding that text is being converted from. If the value is empty, it is tried to be detected then.
* @return string Returns the converted xml-text.
*/
function _api_convert_encoding_xml(&$string, $to_encoding, $from_encoding) {
function _api_convert_encoding_xml(&$string, $to_encoding, $from_encoding)
{
if (empty($from_encoding)) {
$from_encoding = api_detect_encoding_xml($string);
}
@ -179,7 +185,16 @@ function _api_convert_encoding_xml(&$string, $to_encoding, $from_encoding) {
}
global $_api_encoding;
$_api_encoding = api_refine_encoding_id($to_encoding);
return api_convert_encoding(preg_replace_callback(_PCRE_XML_ENCODING, '_api_convert_encoding_xml_callback', $string), $to_encoding, $from_encoding);
return api_convert_encoding(
preg_replace_callback(
_PCRE_XML_ENCODING,
'_api_convert_encoding_xml_callback',
$string
),
$to_encoding,
$from_encoding
);
}
/**
@ -187,7 +202,8 @@ function _api_convert_encoding_xml(&$string, $to_encoding, $from_encoding) {
* @param array $matches Input array of matches corresponding to the xml-declaration.
* @return string Returns the xml-declaration with modified encoding.
*/
function _api_convert_encoding_xml_callback($matches) {
function _api_convert_encoding_xml_callback($matches)
{
global $_api_encoding;
return str_replace($matches[1], $_api_encoding, $matches[0]);
}
@ -199,7 +215,8 @@ function _api_convert_encoding_xml_callback($matches) {
* @param string $html The input html text.
* @return bool Returns TRUE when there is a formula found or FALSE otherwise.
*/
function api_contains_asciimathml($html) {
function api_contains_asciimathml($html)
{
if (!preg_match_all('/<span[^>]*class\s*=\s*[\'"](.*?)[\'"][^>]*>/mi', $html, $matches)) {
return false;
}
@ -236,8 +253,8 @@ function api_contains_asciisvg($html)
/**
* Convers a string from camel case into underscore.
* Works correctly with ASCII strings only, implementation for human-language strings is not necessary.
* @param string $string The input string (ASCII)
* @return string The converted result string
* @param string $string The input string (ASCII)
* @return string The converted result string
*/
function api_camel_case_to_underscore($string)
{
@ -247,9 +264,9 @@ function api_camel_case_to_underscore($string)
/**
* Converts a string with underscores into camel case.
* Works correctly with ASCII strings only, implementation for human-language strings is not necessary.
* @param string $string The input string (ASCII)
* @param string $string The input string (ASCII)
* @param bool $capitalise_first_char (optional) If true (default), the function capitalises the first char in the result string.
* @return string The converted result string
* @return string The converted result string
*/
function api_underscore_to_camel_case($string, $capitalise_first_char = true)
{
@ -296,13 +313,14 @@ function api_trunc_str($text, $length = 30, $suffix = '...', $middle = false, $e
*
* @author Denes Nagy
* @param string variable - the variable to be revised
* @return string
*/
function domesticate($input)
{
$input = stripslashes($input);
$input = str_replace("'", "''", $input);
$input = str_replace('"', "''", $input);
return ($input);
return $input;
}
/**
@ -341,7 +359,8 @@ function domesticate($input)
* @param array $matches Single Regex Match.
* @return string HTML A element with URI address.
*/
function _make_url_clickable_cb($matches) {
function _make_url_clickable_cb($matches)
{
$url = $matches[2];
if (')' == $matches[3] && strpos($url, '(')) {
@ -360,8 +379,9 @@ function _make_url_clickable_cb($matches) {
}
$url = esc_url($url);
if (empty($url))
if (empty($url)) {
return $matches[0];
}
return $matches[1]."<a href=\"$url\" rel=\"nofollow\">$url</a>".$suffix;
}
@ -375,7 +395,7 @@ function _make_url_clickable_cb($matches) {
*
* @since wordpress 2.8.0
* @uses wp_kses_bad_protocol() To only permit protocols in the URL set
* via $protocols or the common ones set in the function.
* via $protocols or the common ones set in the function.
*
* @param string $url The URL to be cleaned.
* @param array $protocols Optional. An array of acceptable protocols.
@ -383,11 +403,12 @@ function _make_url_clickable_cb($matches) {
* @param string $_context Private. Use esc_url_raw() for database usage.
* @return string The cleaned $url after the 'clean_url' filter is applied.
*/
function esc_url($url, $protocols = null, $_context = 'display') {
function esc_url($url, $protocols = null, $_context = 'display')
{
//$original_url = $url;
if ('' == $url)
if ('' == $url) {
return $url;
}
$url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url);
$strip = array('%0d', '%0a', '%0D', '%0A');
$url = _deep_replace($strip, $url);
@ -446,7 +467,8 @@ function esc_url($url, $protocols = null, $_context = 'display') {
* @param string $subject The string being searched and replaced on, otherwise known as the haystack.
* @return string The string with the replaced svalues.
*/
function _deep_replace($search, $subject) {
function _deep_replace($search, $subject)
{
$subject = (string) $subject;
$count = 1;
@ -457,7 +479,6 @@ function _deep_replace($search, $subject) {
return $subject;
}
/**
* Callback to convert URL match to HTML A element.
*
@ -470,13 +491,15 @@ function _deep_replace($search, $subject) {
* @param array $matches Single Regex Match.
* @return string HTML A element with URL address.
*/
function _make_web_ftp_clickable_cb($matches) {
function _make_web_ftp_clickable_cb($matches)
{
$ret = '';
$dest = $matches[2];
$dest = 'http://'.$dest;
$dest = esc_url($dest);
if (empty($dest))
if (empty($dest)) {
return $matches[0];
}
// removed trailing [.,;:)] from URL
if (in_array(substr($dest, -1), array('.', ',', ';', ':', ')')) === true) {
@ -498,7 +521,8 @@ function _make_web_ftp_clickable_cb($matches) {
* @param array $matches Single Regex Match.
* @return string HTML A element with email address.
*/
function _make_email_clickable_cb($matches) {
function _make_email_clickable_cb($matches)
{
$email = $matches[2].'@'.$matches[3];
return $matches[1]."<a href=\"mailto:$email\">$email</a>";
}
@ -514,16 +538,17 @@ function _make_email_clickable_cb($matches) {
* @param string $text Content to convert URIs.
* @return string Content with converted URIs.
*/
function make_clickable($text) {
function make_clickable($text)
{
$r = '';
$textarr = preg_split('/(<[^<>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE); // split out HTML tags
$nested_code_pre = 0; // Keep track of how many levels link is nested inside <pre> or <code>
foreach ($textarr as $piece) {
if (preg_match('|^<code[\s>]|i', $piece) || preg_match('|^<pre[\s>]|i', $piece))
if (preg_match('|^<code[\s>]|i', $piece) || preg_match('|^<pre[\s>]|i', $piece)) {
$nested_code_pre++;
elseif (('</code>' === strtolower($piece) || '</pre>' === strtolower($piece)) && $nested_code_pre)
} elseif (('</code>' === strtolower($piece) || '</pre>' === strtolower($piece)) && $nested_code_pre) {
$nested_code_pre--;
}
if ($nested_code_pre || empty($piece) || ($piece[0] === '<' && !preg_match('|^<\s*[\w]{1,20}+://|', $piece))) {
$r .= $piece;
@ -559,7 +584,6 @@ function make_clickable($text) {
// Tell PCRE to spend more time optimizing since, when used on a page load, it will probably be used several times.
$ret = preg_replace_callback($url_clickable, '_make_url_clickable_cb', $ret);
$ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret);
$ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret);
@ -604,11 +628,10 @@ function make_clickable($text) {
* @param int $goal The desired chunk length.
* @return array Numeric array of chunks.
*/
function _split_str_by_whitespace($string, $goal) {
function _split_str_by_whitespace($string, $goal)
{
$chunks = array();
$string_nullspace = strtr($string, "\r\n\t\v\f ", "\000\000\000\000\000\000");
while ($goal < strlen($string_nullspace)) {
$pos = strrpos(substr($string_nullspace, 0, $goal + 1), "\000");
@ -639,7 +662,8 @@ function _split_str_by_whitespace($string, $goal) {
* @param bool Whether to embed in a <span title="...">...</span>
* @return string
* */
function cut($text, $maxchar, $embed = false) {
function cut($text, $maxchar, $embed = false)
{
if (api_strlen($text) > $maxchar) {
if ($embed) {
return '<p title="'.$text.'">'.api_substr($text, 0, $maxchar).'...</p>';
@ -656,12 +680,12 @@ function cut($text, $maxchar, $embed = false) {
* @param int Decimal points 0=never, 1=if needed, 2=always
* @return mixed An integer or a float depends on the parameter
*/
function float_format($number, $flag = 1) {
function float_format($number, $flag = 1)
{
if (is_numeric($number)) {
if (!$number) {
$result = ($flag == 2 ? '0.'.str_repeat('0', EXERCISE_NUMBER_OF_DECIMALS) : '0');
} else {
if (floor($number) == $number) {
$result = number_format($number, ($flag == 2 ? EXERCISE_NUMBER_OF_DECIMALS : 0));
} else {
@ -677,7 +701,8 @@ function float_format($number, $flag = 1) {
* Function to obtain last week timestamps
* @return array Times for every day inside week
*/
function get_last_week() {
function get_last_week()
{
$week = date('W');
$year = date('Y');
@ -700,10 +725,12 @@ function get_last_week() {
* @param string Date in UTC (2010-01-01 12:12:12)
* @return int Returns an integer with the week number of the year
*/
function get_week_from_day($date) {
function get_week_from_day($date)
{
if (!empty($date)) {
$time = api_strtotime($date, 'UTC');
return date('W', $time);
$time = api_strtotime($date, 'UTC');
return date('W', $time);
} else {
return date('W');
}
@ -782,7 +809,7 @@ function format_file_size($file_size)
function return_datetime_from_array($array)
{
$year = '0000';
$year = '0000';
$month = $day = $hours = $minutes = $seconds = '00';
if (isset($array['Y']) && (isset($array['F']) || isset($array['M'])) && isset($array['d']) && isset($array['H']) && isset($array['i'])) {
$year = $array['Y'];

Loading…
Cancel
Save