Created
September 4, 2017 09:44
-
-
Save SashaDesigN/dc6af048fafddf3d9b15dfb4e0872f8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static function search_users_parameters() { | |
return new external_function_parameters( | |
array( | |
'name' => new external_value(PARAM_RAW, 'users name', VALUE_OPTIONAL), | |
'page' => new external_value(PARAM_RAW, 'pagination', VALUE_OPTIONAL), | |
'faculty' => new external_value(PARAM_RAW, 'Faculty id', VALUE_OPTIONAL), | |
'course' => new external_value(PARAM_INT, 'Course id', VALUE_OPTIONAL) | |
) | |
); | |
} | |
private function _filterByFaculcy(){ | |
return true; | |
} | |
public static function search_users($name, $page, $faculty, $course) { | |
global $CFG, $DB; | |
//Params validation | |
$params = self::validate_parameters( | |
self::search_users_parameters(), | |
array( | |
'name' => $name, | |
'page' => $page, | |
'faculty' => $faculty, | |
'course' => $course | |
) | |
); | |
$where = ''; | |
$join = ''; | |
$faculties = self::getFaculcy(); | |
if(strlen($name) > 0) { | |
$where .= " AND concat(u.firstname, ' ', u.lastname) LIKE '%" . $name . "%' "; | |
} | |
if(strlen($faculty) > 0){ | |
if(isset($faculties[$faculty])){ | |
$join .= "LEFT JOIN {cohort_members} mb ON mb.userid = u.id | |
LEFT JOIN {cohort} c ON c.id = mb.cohortid"; | |
$where .= " AND c.name LIKE '".$faculty."_%' "; | |
} else { | |
echo '{"error":"Faculcy with those id not registered"}'; exit; | |
} | |
} | |
if((int)$course > 0 && (int)$course <= 9){ | |
if(isset($faculties[$faculty])){ | |
if(strlen($faculty) == 0){ | |
$join .= "LEFT JOIN {cohort_members} mb ON mb.userid = u.id | |
LEFT JOIN {cohort} c ON c.id = mb.cohortid"; | |
} | |
$where .= " AND c.name LIKE '%_".$course."' "; | |
} else { | |
echo '{"error":"Wrong course value"}'; exit; | |
} | |
} | |
$page = $page > 0 ? ($page -1) : 0; | |
$pagination = 20 * $page + 20; | |
$sql = "SELECT | |
u.id, | |
u.firstname, | |
u.lastname, | |
u.picture | |
FROM {user} u $join | |
WHERE 1 $where | |
LIMIT $pagination, 20"; | |
$users = $DB->get_records_sql($sql); | |
$res = array(); | |
foreach ($users as $k => $v) { | |
$res[] = self::getUserImage($v); | |
} | |
echo json_encode($res, JSON_UNESCAPED_UNICODE); exit; | |
// return $res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment