swad-core/swad_search.c

1177 lines
44 KiB
C
Raw Normal View History

2014-12-01 23:55:08 +01:00
// swad_search.c: search for courses and teachers
/*
SWAD (Shared Workspace At a Distance),
is a web platform developed at the University of Granada (Spain),
and used to support university teaching.
This file is part of SWAD core.
2019-01-07 21:52:19 +01:00
Copyright (C) 1999-2019 Antonio Ca<EFBFBD>as Vargas
2014-12-01 23:55:08 +01:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General 3 License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/*********************************** Headers *********************************/
/*****************************************************************************/
2019-02-18 14:41:46 +01:00
#include <stdio.h> // For fprintf...
2017-01-16 01:51:01 +01:00
#include <string.h> // For string functions...
2014-12-01 23:55:08 +01:00
2017-06-10 21:38:10 +02:00
#include "swad_box.h"
2014-12-01 23:55:08 +01:00
#include "swad_database.h"
2018-11-09 20:47:39 +01:00
#include "swad_form.h"
2014-12-01 23:55:08 +01:00
#include "swad_global.h"
#include "swad_layout.h"
#include "swad_parameter.h"
/*****************************************************************************/
/****************************** Public constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Internal constants ****************************/
/*****************************************************************************/
#define Sch_MIN_LENGTH_LONGEST_WORD 3
2016-12-19 09:39:08 +01:00
#define Sch_MIN_LENGTH_TOTAL 3 // "A An" is not valid; "A An Ann" is valid
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/****************************** Internal types *******************************/
/*****************************************************************************/
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/************************* Internal global variables *************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Internal prototypes ***************************/
/*****************************************************************************/
2019-04-03 20:57:04 +02:00
static void Sch_PutFormToSearchWithWhatToSearchAndScope (Act_Action_t Action,Hie_Level_t DefaultScope);
2014-12-01 23:55:08 +01:00
static bool Sch_CheckIfIHavePermissionToSearch (Sch_WhatToSearch_t WhatToSearch);
2017-03-12 21:15:32 +01:00
static void Sch_GetParamSearch (void);
2014-12-01 23:55:08 +01:00
static void Sch_SearchInDB (void);
2017-02-28 00:59:01 +01:00
static unsigned Sch_SearchCountriesInDB (const char *RangeQuery);
2014-12-01 23:55:08 +01:00
static unsigned Sch_SearchInstitutionsInDB (const char *RangeQuery);
static unsigned Sch_SearchCentresInDB (const char *RangeQuery);
static unsigned Sch_SearchDegreesInDB (const char *RangeQuery);
static unsigned Sch_SearchCoursesInDB (const char *RangeQuery);
static unsigned Sch_SearchUsrsInDB (Rol_Role_t Role);
static unsigned Sch_SearchOpenDocumentsInDB (const char *RangeQuery);
static unsigned Sch_SearchDocumentsInMyCoursesInDB (const char *RangeQuery);
static unsigned Sch_SearchMyDocumentsInDB (const char *RangeQuery);
static void Sch_SaveLastSearchIntoSession (void);
/*****************************************************************************/
/*********************** Request search in system tab ************************/
/*****************************************************************************/
void Sch_ReqSysSearch (void)
{
/***** Search courses, teachers, documents... *****/
2014-12-15 00:05:10 +01:00
Sch_GetParamWhatToSearch ();
2019-04-03 20:57:04 +02:00
Sch_PutFormToSearchWithWhatToSearchAndScope (ActSch,Hie_SYS);
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/****************** Put a form to search, including scope ********************/
/*****************************************************************************/
2019-04-03 20:57:04 +02:00
static void Sch_PutFormToSearchWithWhatToSearchAndScope (Act_Action_t Action,Hie_Level_t DefaultScope)
2014-12-01 23:55:08 +01:00
{
2019-03-26 13:20:21 +01:00
extern const char *Hlp_START_Search;
2019-02-22 21:47:50 +01:00
extern const char *The_ClassFormInBox[The_NUM_THEMES];
2014-12-01 23:55:08 +01:00
extern const char *Txt_Scope;
2016-04-18 10:22:56 +02:00
extern const char *Txt_SEARCH_X_in_Y;
extern const char *Txt_all;
2017-02-28 00:59:01 +01:00
extern const char *Txt_countries;
2016-04-18 10:22:56 +02:00
extern const char *Txt_institutions;
extern const char *Txt_centres;
extern const char *Txt_degrees;
extern const char *Txt_courses;
2016-06-27 12:35:15 +02:00
extern const char *Txt_users[Usr_NUM_SEXS];
2016-04-18 10:22:56 +02:00
extern const char *Txt_ROLES_PLURAL_abc[Rol_NUM_ROLES][Usr_NUM_SEXS];
extern const char *Txt_my_documents;
extern const char *Txt_documents_in_my_courses;
extern const char *Txt_open_documents;
2014-12-01 23:55:08 +01:00
extern const char *Txt_Search;
const char *Titles[Sch_NUM_WHAT_TO_SEARCH] =
{
2017-05-19 10:37:14 +02:00
"", // Sch_SEARCH_UNKNOWN
Txt_all, // Sch_SEARCH_ALL
Txt_countries, // Sch_SEARCH_COUNTRIES
Txt_institutions, // Sch_SEARCH_INSTITS
Txt_centres, // Sch_SEARCH_CENTRES
Txt_degrees, // Sch_SEARCH_DEGREES
Txt_courses, // Sch_SEARCH_COURSES
Txt_users[Usr_SEX_UNKNOWN], // Sch_SEARCH_USERS
2017-05-18 19:13:41 +02:00
Txt_ROLES_PLURAL_abc[Rol_TCH][Usr_SEX_UNKNOWN], // Sch_SEARCH_TEACHERS
Txt_ROLES_PLURAL_abc[Rol_STD][Usr_SEX_UNKNOWN], // Sch_SEARCH_STUDENTS
Txt_ROLES_PLURAL_abc[Rol_GST][Usr_SEX_UNKNOWN], // Sch_SEARCH_GUESTS
2017-05-19 10:37:14 +02:00
Txt_open_documents, // Sch_SEARCH_OPEN_DOCUMENTS
Txt_documents_in_my_courses, // Sch_SEARCH_DOCUM_IN_MY_COURSES
Txt_my_documents, // Sch_SEARCH_MY_DOCUMENTS
2014-12-01 23:55:08 +01:00
};
Sch_WhatToSearch_t WhatToSearch;
2018-10-09 13:41:55 +02:00
/***** Start box *****/
2015-08-24 12:29:00 +02:00
fprintf (Gbl.F.Out,"<div class=\"CENTER_MIDDLE\">");
2018-10-09 13:41:55 +02:00
Box_StartBox (NULL,Txt_Search,Crs_PutIconToSelectMyCourses,
2019-03-26 13:20:21 +01:00
Hlp_START_Search,Box_NOT_CLOSABLE);
2014-12-01 23:55:08 +01:00
2018-10-09 13:41:55 +02:00
/***** Start form *****/
2018-11-09 20:47:39 +01:00
Frm_StartForm (Action);
2018-10-09 13:41:55 +02:00
2016-06-24 20:16:06 +02:00
/***** Scope (whole platform, current country, current institution,
current centre, current degree or current course) *****/
2016-03-27 21:36:52 +02:00
fprintf (Gbl.F.Out,"<div class=\"CENTER_MIDDLE\">"
2016-12-20 02:18:50 +01:00
"<label class=\"%s\">%s:&nbsp;",
2019-02-22 21:47:50 +01:00
The_ClassFormInBox[Gbl.Prefs.Theme],Txt_Scope);
2019-04-03 20:57:04 +02:00
Gbl.Scope.Allowed = 1 << Hie_SYS |
1 << Hie_CTY |
1 << Hie_INS |
1 << Hie_CTR |
1 << Hie_DEG |
1 << Hie_CRS;
2014-12-01 23:55:08 +01:00
Gbl.Scope.Default = DefaultScope;
2016-06-24 20:34:58 +02:00
Sco_GetScope ("ScopeSch");
Sco_PutSelectorScope ("ScopeSch",false);
2016-12-20 02:18:50 +01:00
fprintf (Gbl.F.Out,"</label>"
"</div>");
2014-12-01 23:55:08 +01:00
2016-04-18 10:22:56 +02:00
/***** String to find *****/
Sch_PutInputStringToSearch (NULL);
2014-12-01 23:55:08 +01:00
/***** What to search? *****/
2016-12-25 21:43:59 +01:00
fprintf (Gbl.F.Out,"<label class=\"%s\">"
" %s&nbsp;",
2019-02-22 21:47:50 +01:00
The_ClassFormInBox[Gbl.Prefs.Theme],Txt_SEARCH_X_in_Y);
2015-09-28 18:28:29 +02:00
fprintf (Gbl.F.Out,"<select name=\"WhatToSearch\" style=\"width:186px;\">");
2014-12-01 23:55:08 +01:00
for (WhatToSearch = (Sch_WhatToSearch_t) 0;
WhatToSearch < Sch_NUM_WHAT_TO_SEARCH;
WhatToSearch++)
if (Sch_CheckIfIHavePermissionToSearch (WhatToSearch))
{
fprintf (Gbl.F.Out,"<option value=\"%u\"",
(unsigned) WhatToSearch);
if (Gbl.Search.WhatToSearch == WhatToSearch)
fprintf (Gbl.F.Out," selected=\"selected\"");
fprintf (Gbl.F.Out,">%s</option>",
Titles[WhatToSearch]);
}
2016-12-25 21:43:59 +01:00
fprintf (Gbl.F.Out,"</select>"
"</label>");
2014-12-01 23:55:08 +01:00
2016-04-18 10:22:56 +02:00
/***** Magnifying glass icon *****/
2019-01-08 11:44:01 +01:00
Sch_PutMagnifyingGlassButton ("search.svg");
2014-12-01 23:55:08 +01:00
2018-10-09 13:41:55 +02:00
/***** Send button *****/
Btn_PutButton (Btn_CONFIRM_BUTTON,Txt_Search);
2014-12-01 23:55:08 +01:00
2015-04-11 17:33:14 +02:00
/***** End form *****/
2018-11-09 20:47:39 +01:00
Frm_EndForm ();
2018-10-09 13:41:55 +02:00
/***** End box *****/
Box_EndBox ();
2015-03-13 00:16:02 +01:00
fprintf (Gbl.F.Out,"</div>");
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/************* Check if I have permission to execute an action ***************/
/*****************************************************************************/
static bool Sch_CheckIfIHavePermissionToSearch (Sch_WhatToSearch_t WhatToSearch)
{
unsigned Permissions[Sch_NUM_WHAT_TO_SEARCH] =
{
2017-01-29 12:42:19 +01:00
0x000, // Sch_SEARCH_UNKNOWN
2017-05-31 13:53:17 +02:00
0x3FF, // Sch_SEARCH_ALL
0x3FF, // Sch_SEARCH_COUNTRIES
0x3FF, // Sch_SEARCH_INSTITS
0x3FF, // Sch_SEARCH_CENTRES
0x3FF, // Sch_SEARCH_DEGREES
0x3FF, // Sch_SEARCH_COURSES
0x3FF, // Sch_SEARCH_USERS
0x3FF, // Sch_SEARCH_TEACHERS
0x3FF, // Sch_SEARCH_STUDENTS
0x3FF, // Sch_SEARCH_GUESTS
0x3FF, // Sch_SEARCH_OPEN_DOCUMENTS
0x3FE, // Sch_SEARCH_DOCUM_IN_MY_COURSES Only if I am logged
0x3FE, // Sch_SEARCH_MY_DOCUMENTS Only if I am logged
2014-12-01 23:55:08 +01:00
};
2017-06-04 18:18:54 +02:00
return (Permissions[WhatToSearch] & (1 << Gbl.Usrs.Me.Role.Logged));
2014-12-01 23:55:08 +01:00
}
2016-06-24 20:16:06 +02:00
/*****************************************************************************/
/**************** Put a form to search in page top heading *******************/
/*****************************************************************************/
void Sch_PutFormToSearchInPageTopHeading (void)
{
2017-02-26 23:44:50 +01:00
/***** Put form *****/
fprintf (Gbl.F.Out,"<div id=\"head_row_1_search\">");
2019-02-15 13:39:37 +01:00
Frm_StartForm (ActSch);
2019-04-03 20:57:04 +02:00
Sco_PutParamScope ("ScopeSch",Hie_SYS);
2016-06-24 20:16:06 +02:00
Sch_PutInputStringToSearch ("head_search_text");
2019-01-08 11:44:01 +01:00
Sch_PutMagnifyingGlassButton ("search-white.svg");
2018-11-09 20:47:39 +01:00
Frm_EndForm ();
2016-06-24 20:16:06 +02:00
fprintf (Gbl.F.Out,"</div>"); // head_row_1_search
}
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
2016-04-18 10:22:56 +02:00
/********************* Put string to search inside form **********************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
2016-04-18 10:22:56 +02:00
void Sch_PutInputStringToSearch (const char *IdInputText)
2014-12-01 23:55:08 +01:00
{
extern const char *Txt_Search;
/***** String to find *****/
2016-12-20 10:03:00 +01:00
// Input field not required, because it can be hidden (display:none)
2015-11-27 11:54:56 +01:00
fprintf (Gbl.F.Out,"<input");
if (IdInputText)
fprintf (Gbl.F.Out," id=\"%s\"",IdInputText);
2016-12-19 09:39:08 +01:00
fprintf (Gbl.F.Out," type=\"search\" name=\"Search\""
2016-12-10 18:32:35 +01:00
" size=\"18\" maxlength=\"%u\" value=\"%s\"",
2017-03-07 11:03:05 +01:00
Sch_MAX_CHARS_STRING_TO_FIND,
2014-12-01 23:55:08 +01:00
Gbl.Search.Str);
if (!Gbl.Search.Str[0])
2016-01-15 11:41:45 +01:00
fprintf (Gbl.F.Out," placeholder=\"%s&hellip;\"",
2014-12-01 23:55:08 +01:00
Txt_Search);
2016-12-20 10:03:00 +01:00
fprintf (Gbl.F.Out," style=\"margin:0;\" />");
2016-04-18 10:22:56 +02:00
}
/*****************************************************************************/
/******************* Put magnifying glass button inside form *****************/
/*****************************************************************************/
void Sch_PutMagnifyingGlassButton (const char *Icon)
{
extern const char *Txt_Search;
2014-12-01 23:55:08 +01:00
/***** Send button *****/
2019-01-12 03:00:59 +01:00
Ico_PutIconLink (Icon,Txt_Search);
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/************* Get parameter "what to search" from search form ***************/
/*****************************************************************************/
2014-12-13 21:38:31 +01:00
void Sch_GetParamWhatToSearch (void)
2014-12-01 23:55:08 +01:00
{
2017-01-29 12:42:19 +01:00
Sch_WhatToSearch_t WhatToSearch;
2014-12-01 23:55:08 +01:00
2014-12-15 00:05:10 +01:00
/***** Get what to search from form *****/
2017-01-29 12:42:19 +01:00
WhatToSearch = (Sch_WhatToSearch_t)
2017-01-29 21:41:08 +01:00
Par_GetParToUnsignedLong ("WhatToSearch",
0,
Sch_NUM_WHAT_TO_SEARCH - 1,
(unsigned long) Sch_SEARCH_UNKNOWN);
2014-12-01 23:55:08 +01:00
2019-02-21 20:04:05 +01:00
/***** If parameter WhatToSearch is not present,
use parameter from session *****/
2017-01-29 12:42:19 +01:00
if (WhatToSearch != Sch_SEARCH_UNKNOWN)
Gbl.Search.WhatToSearch = WhatToSearch;
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/*********************** Get string from search form *************************/
/*****************************************************************************/
2017-03-12 21:15:32 +01:00
static void Sch_GetParamSearch (void)
2014-12-01 23:55:08 +01:00
{
/***** Get string to search *****/
2017-03-12 21:15:32 +01:00
Par_GetParToText ("Search",Gbl.Search.Str,Sch_MAX_BYTES_STRING_TO_FIND);
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/************************* Get parameters to search **************************/
/*****************************************************************************/
void Sch_GetParamsSearch (void)
{
/***** What to search? *****/
2014-12-13 21:38:31 +01:00
Sch_GetParamWhatToSearch ();
2014-12-01 23:55:08 +01:00
/***** Get search string *****/
2017-03-12 21:15:32 +01:00
Sch_GetParamSearch ();
2014-12-01 23:55:08 +01:00
/***** Save my search in order to show it in current session *****/
if (Gbl.Usrs.Me.Logged)
Sch_SaveLastSearchIntoSession ();
}
/*****************************************************************************/
/****************** Search courses, teachers, documents... *******************/
/*****************************************************************************/
void Sch_SysSearch (void)
{
if (Gbl.Search.Str[0])
{
/***** Show search form again *****/
2019-04-03 20:57:04 +02:00
Sch_PutFormToSearchWithWhatToSearchAndScope (ActSch,Hie_SYS);
2014-12-01 23:55:08 +01:00
/***** Show results of search *****/
Sch_SearchInDB ();
}
else
/***** Show search form and selectors *****/
Sch_ReqSysSearch ();
}
/*****************************************************************************/
/**** Search institutions, centres, degrees, courses, teachers, documents ****/
/*****************************************************************************/
static void Sch_SearchInDB (void)
{
extern const char *Txt_No_results;
char RangeQuery[256];
unsigned NumResults = 0; // Initialized to avoid warning
2017-02-27 01:41:46 +01:00
Gbl.Search.LogSearch = true;
2014-12-01 23:55:08 +01:00
/***** Select courses in all the degrees or in current degree *****/
switch (Gbl.Scope.Current)
{
2019-04-03 20:57:04 +02:00
case Hie_UNK:
2014-12-01 23:55:08 +01:00
// Not aplicable
2019-04-03 20:57:04 +02:00
case Hie_SYS:
2014-12-01 23:55:08 +01:00
RangeQuery[0] = '\0';
break;
2019-04-03 20:57:04 +02:00
case Hie_CTY:
2017-03-24 01:09:27 +01:00
sprintf (RangeQuery," AND countries.CtyCod=%ld",
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Cty.CtyCod);
2014-12-01 23:55:08 +01:00
break;
2019-04-03 20:57:04 +02:00
case Hie_INS:
2017-03-24 01:09:27 +01:00
sprintf (RangeQuery," AND institutions.InsCod=%ld",
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Ins.InsCod);
2014-12-01 23:55:08 +01:00
break;
2019-04-03 20:57:04 +02:00
case Hie_CTR:
2017-03-24 01:09:27 +01:00
sprintf (RangeQuery," AND centres.CtrCod=%ld",
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Ctr.CtrCod);
2014-12-01 23:55:08 +01:00
break;
2019-04-03 20:57:04 +02:00
case Hie_DEG:
2017-03-24 01:09:27 +01:00
sprintf (RangeQuery," AND degrees.DegCod=%ld",
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Deg.DegCod);
2014-12-01 23:55:08 +01:00
break;
2019-04-03 20:57:04 +02:00
case Hie_CRS:
2017-03-24 01:09:27 +01:00
sprintf (RangeQuery," AND courses.CrsCod=%ld",
2019-04-04 10:45:15 +02:00
Gbl.Hierarchy.Crs.CrsCod);
2014-12-01 23:55:08 +01:00
break;
}
2017-01-29 12:42:19 +01:00
if (Gbl.Search.WhatToSearch == Sch_SEARCH_UNKNOWN)
Gbl.Search.WhatToSearch = Sch_WHAT_TO_SEARCH_DEFAULT;
2014-12-01 23:55:08 +01:00
switch (Gbl.Search.WhatToSearch)
{
case Sch_SEARCH_ALL:
2017-02-28 00:59:01 +01:00
NumResults = Sch_SearchCountriesInDB (RangeQuery);
NumResults += Sch_SearchInstitutionsInDB (RangeQuery);
2014-12-01 23:55:08 +01:00
NumResults += Sch_SearchCentresInDB (RangeQuery);
NumResults += Sch_SearchDegreesInDB (RangeQuery);
NumResults += Sch_SearchCoursesInDB (RangeQuery);
2017-05-22 20:37:46 +02:00
NumResults += Sch_SearchUsrsInDB (Rol_TCH); // Non-editing teachers and teachers
2017-05-18 19:13:41 +02:00
NumResults += Sch_SearchUsrsInDB (Rol_STD);
NumResults += Sch_SearchUsrsInDB (Rol_GST);
2014-12-01 23:55:08 +01:00
NumResults += Sch_SearchOpenDocumentsInDB (RangeQuery);
NumResults += Sch_SearchDocumentsInMyCoursesInDB (RangeQuery);
NumResults += Sch_SearchMyDocumentsInDB (RangeQuery);
break;
2017-02-28 00:59:01 +01:00
case Sch_SEARCH_COUNTRIES:
NumResults = Sch_SearchCountriesInDB (RangeQuery);
break;
2016-10-28 10:03:37 +02:00
case Sch_SEARCH_INSTITS:
2014-12-01 23:55:08 +01:00
NumResults = Sch_SearchInstitutionsInDB (RangeQuery);
break;
case Sch_SEARCH_CENTRES:
NumResults = Sch_SearchCentresInDB (RangeQuery);
break;
case Sch_SEARCH_DEGREES:
NumResults = Sch_SearchDegreesInDB (RangeQuery);
break;
case Sch_SEARCH_COURSES:
NumResults = Sch_SearchCoursesInDB (RangeQuery);
break;
2016-06-27 12:35:15 +02:00
case Sch_SEARCH_USERS:
2017-05-18 19:13:41 +02:00
NumResults = Sch_SearchUsrsInDB (Rol_UNK); // Here Rol_UNK means any user
2016-06-27 12:35:15 +02:00
break;
2014-12-01 23:55:08 +01:00
case Sch_SEARCH_TEACHERS:
2017-05-22 20:37:46 +02:00
NumResults = Sch_SearchUsrsInDB (Rol_TCH); // Non-editing teachers and teachers
2014-12-01 23:55:08 +01:00
break;
case Sch_SEARCH_STUDENTS:
2017-05-18 19:13:41 +02:00
NumResults = Sch_SearchUsrsInDB (Rol_STD);
2014-12-01 23:55:08 +01:00
break;
case Sch_SEARCH_GUESTS:
2017-05-18 19:13:41 +02:00
NumResults = Sch_SearchUsrsInDB (Rol_GST);
2014-12-01 23:55:08 +01:00
break;
case Sch_SEARCH_OPEN_DOCUMENTS:
NumResults = Sch_SearchOpenDocumentsInDB (RangeQuery);
break;
2015-01-26 12:39:48 +01:00
case Sch_SEARCH_DOCUM_IN_MY_COURSES:
2014-12-01 23:55:08 +01:00
NumResults = Sch_SearchDocumentsInMyCoursesInDB (RangeQuery);
break;
case Sch_SEARCH_MY_DOCUMENTS:
NumResults = Sch_SearchMyDocumentsInDB (RangeQuery);
break;
2017-01-29 12:42:19 +01:00
default:
NumResults = 0;
break;
2014-12-01 23:55:08 +01:00
}
if (NumResults == 0)
2019-02-16 18:25:41 +01:00
Ale_ShowAlert (Ale_INFO,Txt_No_results);
2014-12-01 23:55:08 +01:00
}
2017-02-28 00:59:01 +01:00
/*****************************************************************************/
/************************ Search countries in database ***********************/
/*****************************************************************************/
// Returns number of countries found
static unsigned Sch_SearchCountriesInDB (const char *RangeQuery)
{
2018-12-08 16:43:13 +01:00
extern const char *Lan_STR_LANG_ID[1 + Lan_NUM_LANGUAGES];
2017-03-07 11:03:05 +01:00
char SearchQuery[Sch_MAX_BYTES_SEARCH_QUERY + 1];
2017-02-28 00:59:01 +01:00
char FieldName[4+1+2+1]; // Example: Name_en
2018-10-30 14:47:31 +01:00
MYSQL_RES *mysql_res;
unsigned NumCtys;
2017-02-28 00:59:01 +01:00
/***** Check scope *****/
2019-04-03 20:57:04 +02:00
if (Gbl.Scope.Current != Hie_INS &&
Gbl.Scope.Current != Hie_CTR &&
Gbl.Scope.Current != Hie_DEG &&
Gbl.Scope.Current != Hie_CRS)
2017-02-28 00:59:01 +01:00
/***** Check user's permission *****/
if (Sch_CheckIfIHavePermissionToSearch (Sch_SEARCH_COUNTRIES))
{
/***** Split countries string into words *****/
2018-10-18 02:02:32 +02:00
snprintf (FieldName,sizeof (FieldName),
"Name_%s",
2018-12-08 16:43:13 +01:00
Lan_STR_LANG_ID[Gbl.Prefs.Language]);
2017-02-28 00:59:01 +01:00
if (Sch_BuildSearchQuery (SearchQuery,FieldName,NULL,NULL))
{
/***** Query database and list institutions found *****/
2018-10-30 14:47:31 +01:00
NumCtys = (unsigned) DB_QuerySELECT (&mysql_res,"can not get countries",
"SELECT CtyCod"
" FROM countries"
" WHERE %s%s"
" ORDER BY Name_%s",
SearchQuery,RangeQuery,
2018-12-08 16:43:13 +01:00
Lan_STR_LANG_ID[Gbl.Prefs.Language]);
2018-10-30 14:47:31 +01:00
Cty_ListCtysFound (&mysql_res,NumCtys);
return NumCtys;
2017-02-28 00:59:01 +01:00
}
}
return 0;
}
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/********************** Search institutions in database **********************/
/*****************************************************************************/
// Returns number of institutions found
static unsigned Sch_SearchInstitutionsInDB (const char *RangeQuery)
{
2018-12-08 16:43:13 +01:00
extern const char *Lan_STR_LANG_ID[1 + Lan_NUM_LANGUAGES];
2017-03-07 11:03:05 +01:00
char SearchQuery[Sch_MAX_BYTES_SEARCH_QUERY + 1];
2018-10-31 16:16:57 +01:00
MYSQL_RES *mysql_res;
unsigned NumInss;
2014-12-01 23:55:08 +01:00
/***** Check scope *****/
2019-04-03 20:57:04 +02:00
if (Gbl.Scope.Current != Hie_CTR &&
Gbl.Scope.Current != Hie_DEG &&
Gbl.Scope.Current != Hie_CRS)
2014-12-01 23:55:08 +01:00
/***** Check user's permission *****/
2016-10-28 10:03:37 +02:00
if (Sch_CheckIfIHavePermissionToSearch (Sch_SEARCH_INSTITS))
2014-12-01 23:55:08 +01:00
/***** Split institutions string into words *****/
if (Sch_BuildSearchQuery (SearchQuery,"institutions.FullName",NULL,NULL))
{
/***** Query database and list institutions found *****/
2018-10-31 16:16:57 +01:00
NumInss = (unsigned) DB_QuerySELECT (&mysql_res,"can not get institutions",
"SELECT institutions.InsCod"
" FROM institutions,countries"
" WHERE %s"
" AND institutions.CtyCod=countries.CtyCod"
"%s"
" ORDER BY institutions.FullName,countries.Name_%s",
SearchQuery,RangeQuery,
2018-12-08 16:43:13 +01:00
Lan_STR_LANG_ID[Gbl.Prefs.Language]);
2018-10-31 16:16:57 +01:00
Ins_ListInssFound (&mysql_res,NumInss);
return NumInss;
2014-12-01 23:55:08 +01:00
}
return 0;
}
/*****************************************************************************/
/************************* Search centres in database ************************/
/*****************************************************************************/
// Returns number of centres found
static unsigned Sch_SearchCentresInDB (const char *RangeQuery)
{
2017-03-07 11:03:05 +01:00
char SearchQuery[Sch_MAX_BYTES_SEARCH_QUERY + 1];
2018-10-30 13:59:37 +01:00
MYSQL_RES *mysql_res;
unsigned NumCtrs;
2014-12-01 23:55:08 +01:00
/***** Check scope *****/
2019-04-03 20:57:04 +02:00
if (Gbl.Scope.Current != Hie_DEG &&
Gbl.Scope.Current != Hie_CRS)
2014-12-01 23:55:08 +01:00
/***** Check user's permission *****/
if (Sch_CheckIfIHavePermissionToSearch (Sch_SEARCH_CENTRES))
/***** Split centre string into words *****/
if (Sch_BuildSearchQuery (SearchQuery,"centres.FullName",NULL,NULL))
{
/***** Query database and list centres found *****/
2018-10-30 13:59:37 +01:00
NumCtrs = (unsigned) DB_QuerySELECT (&mysql_res,"can not get centres",
"SELECT centres.CtrCod"
" FROM centres,institutions,countries"
" WHERE %s"
" AND centres.InsCod=institutions.InsCod"
" AND institutions.CtyCod=countries.CtyCod"
"%s"
" ORDER BY centres.FullName,institutions.FullName",
SearchQuery,RangeQuery);
Ctr_ListCtrsFound (&mysql_res,NumCtrs);
return NumCtrs;
2014-12-01 23:55:08 +01:00
}
return 0;
}
/*****************************************************************************/
/************************* Search degrees in database ************************/
/*****************************************************************************/
// Returns number of degrees found
static unsigned Sch_SearchDegreesInDB (const char *RangeQuery)
{
2017-03-07 11:03:05 +01:00
char SearchQuery[Sch_MAX_BYTES_SEARCH_QUERY + 1];
2018-10-30 17:47:57 +01:00
MYSQL_RES *mysql_res;
unsigned NumDegs;
2014-12-01 23:55:08 +01:00
/***** Check scope *****/
2019-04-03 20:57:04 +02:00
if (Gbl.Scope.Current != Hie_CRS)
2014-12-01 23:55:08 +01:00
/***** Check user's permission *****/
if (Sch_CheckIfIHavePermissionToSearch (Sch_SEARCH_DEGREES))
/***** Split degree string into words *****/
if (Sch_BuildSearchQuery (SearchQuery,"degrees.FullName",NULL,NULL))
{
/***** Query database and list degrees found *****/
2018-10-30 17:47:57 +01:00
NumDegs = (unsigned) DB_QuerySELECT (&mysql_res,"can not get degrees",
"SELECT degrees.DegCod"
" FROM degrees,centres,institutions,countries"
" WHERE %s"
" AND degrees.CtrCod=centres.CtrCod"
" AND centres.InsCod=institutions.InsCod"
" AND institutions.CtyCod=countries.CtyCod"
"%s"
" ORDER BY degrees.FullName,institutions.FullName",
SearchQuery,RangeQuery);
Deg_ListDegsFound (&mysql_res,NumDegs);
return NumDegs;
2014-12-01 23:55:08 +01:00
}
return 0;
}
/*****************************************************************************/
/************************* Search courses in database ************************/
/*****************************************************************************/
// Returns number of courses found
static unsigned Sch_SearchCoursesInDB (const char *RangeQuery)
{
2017-03-07 11:03:05 +01:00
char SearchQuery[Sch_MAX_BYTES_SEARCH_QUERY + 1];
2018-10-30 14:47:31 +01:00
MYSQL_RES *mysql_res;
unsigned NumCrss;
2014-12-01 23:55:08 +01:00
/***** Check user's permission *****/
if (Sch_CheckIfIHavePermissionToSearch (Sch_SEARCH_COURSES))
/***** Split course string into words *****/
if (Sch_BuildSearchQuery (SearchQuery,"courses.FullName",NULL,NULL))
{
/***** Query database and list courses found *****/
2018-10-30 14:47:31 +01:00
NumCrss = (unsigned) DB_QuerySELECT (&mysql_res,"can not get courses",
"SELECT degrees.DegCod,courses.CrsCod,degrees.ShortName,degrees.FullName,"
"courses.Year,courses.FullName,centres.ShortName"
" FROM courses,degrees,centres,institutions,countries"
" WHERE %s"
" AND courses.DegCod=degrees.DegCod"
" AND degrees.CtrCod=centres.CtrCod"
" AND centres.InsCod=institutions.InsCod"
" AND institutions.CtyCod=countries.CtyCod"
"%s"
" ORDER BY courses.FullName,institutions.FullName,degrees.FullName,courses.Year",
SearchQuery,RangeQuery);
Crs_ListCrssFound (&mysql_res,NumCrss);
return NumCrss;
2014-12-01 23:55:08 +01:00
}
return 0;
}
/*****************************************************************************/
2016-06-29 18:27:49 +02:00
/*************************** Search users in database ************************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
2016-06-29 18:27:49 +02:00
// Returns number of users found
2014-12-01 23:55:08 +01:00
static unsigned Sch_SearchUsrsInDB (Rol_Role_t Role)
{
2016-06-29 18:27:49 +02:00
extern const char *Txt_The_search_text_must_be_longer;
static bool WarningMessageWritten = false;
2017-03-07 11:03:05 +01:00
char SearchQuery[Sch_MAX_BYTES_SEARCH_QUERY + 1];
2014-12-01 23:55:08 +01:00
2016-06-29 18:27:49 +02:00
/***** Split user string into words *****/
if (Sch_BuildSearchQuery (SearchQuery,
"CONCAT_WS(' ',FirstName,Surname1,Surname2)",
NULL,NULL))
/***** Query database and list users found *****/
return Usr_ListUsrsFound (Role,SearchQuery);
else
// Too short
if (!WarningMessageWritten) // To avoid repetitions
{
2019-02-16 18:25:41 +01:00
Ale_ShowAlert (Ale_WARNING,Txt_The_search_text_must_be_longer);
2016-06-29 18:27:49 +02:00
WarningMessageWritten = true;
}
2014-12-01 23:55:08 +01:00
return 0;
}
/*****************************************************************************/
/********************** Search open documents in database ********************/
/*****************************************************************************/
// Returns number of documents found
static unsigned Sch_SearchOpenDocumentsInDB (const char *RangeQuery)
{
2016-04-18 09:46:16 +02:00
extern const char *Txt_open_document;
extern const char *Txt_open_documents;
2017-03-07 11:03:05 +01:00
char SearchQuery[Sch_MAX_BYTES_SEARCH_QUERY + 1];
2018-10-31 00:53:39 +01:00
MYSQL_RES *mysql_res;
unsigned long NumDocs;
2014-12-01 23:55:08 +01:00
/***** Check user's permission *****/
if (Sch_CheckIfIHavePermissionToSearch (Sch_SEARCH_OPEN_DOCUMENTS))
/***** Split document string into words *****/
if (Sch_BuildSearchQuery (SearchQuery,"SUBSTRING_INDEX(files.Path,'/',-1)",
"_latin1 "," COLLATE latin1_general_ci"))
{
2018-10-31 00:53:39 +01:00
/***** Query database *****/
NumDocs = DB_QuerySELECT (&mysql_res,"can not get files",
"SELECT * FROM "
"("
"SELECT files.FilCod," // Institution
"SUBSTRING(files.Path,LOCATE('/',files.Path)) AS PathFromRoot,"
"institutions.InsCod,institutions.ShortName AS InsShortName,"
"-1 AS CtrCod,'' AS CtrShortName,"
"-1 AS DegCod,'' AS DegShortName,"
"-1 AS CrsCod,'' AS CrsShortName,"
"-1 AS GrpCod"
" FROM files,courses,degrees,centres,institutions,countries"
" WHERE files.Public='Y' AND %s"
" AND files.FileBrowser IN (%u,%u)"
" AND files.Cod=institutions.InsCod"
" AND institutions.CtyCod=countries.CtyCod"
"%s"
" UNION "
"SELECT files.FilCod," // Centre
"SUBSTRING(files.Path,LOCATE('/',files.Path)) AS PathFromRoot,"
"institutions.InsCod,institutions.ShortName AS InsShortName,"
"centres.CtrCod,centres.ShortName AS CtrShortName,"
"-1 AS DegCod,'' AS DegShortName,"
"-1 AS CrsCod,'' AS CrsShortName,"
"-1 AS GrpCod"
" FROM files,courses,degrees,centres,institutions,countries"
" WHERE files.Public='Y' AND %s"
" AND files.FileBrowser IN (%u,%u)"
" AND files.Cod=centres.CtrCod"
" AND centres.InsCod=institutions.InsCod"
" AND institutions.CtyCod=countries.CtyCod"
"%s"
" UNION "
"SELECT files.FilCod," // Degree
"SUBSTRING(files.Path,LOCATE('/',files.Path)) AS PathFromRoot,"
"institutions.InsCod,institutions.ShortName AS InsShortName,"
"centres.CtrCod,centres.ShortName AS CtrShortName,"
"degrees.DegCod,degrees.ShortName AS DegShortName,"
"-1,'' AS CrsShortName,"
"-1"
" FROM files,courses,degrees,centres,institutions,countries"
" WHERE files.Public='Y' AND %s"
" AND files.FileBrowser IN (%u,%u)"
" AND files.Cod=degrees.DegCod"
" AND degrees.CtrCod=centres.CtrCod"
" AND centres.InsCod=institutions.InsCod"
" AND institutions.CtyCod=countries.CtyCod"
"%s"
" UNION "
"SELECT files.FilCod," // Course
"SUBSTRING(files.Path,LOCATE('/',files.Path)) AS PathFromRoot,"
"institutions.InsCod,institutions.ShortName AS InsShortName,"
"centres.CtrCod,centres.ShortName AS CtrShortName,"
"degrees.DegCod,degrees.ShortName AS DegShortName,"
"courses.CrsCod,courses.ShortName AS CrsShortName,"
"-1"
" FROM files,courses,degrees,centres,institutions,countries"
" WHERE files.Public='Y' AND %s"
" AND files.FileBrowser IN (%u,%u)"
" AND files.Cod=courses.CrsCod"
" AND courses.DegCod=degrees.DegCod"
" AND degrees.CtrCod=centres.CtrCod"
" AND centres.InsCod=institutions.InsCod"
" AND institutions.CtyCod=countries.CtyCod"
"%s"
") AS selected_files"
" WHERE PathFromRoot<>''"
" ORDER BY InsShortName,CtrShortName,DegShortName,CrsShortName,PathFromRoot",
SearchQuery,
(unsigned) Brw_ADMI_DOC_INS,
(unsigned) Brw_ADMI_SHR_INS,
RangeQuery,
SearchQuery,
(unsigned) Brw_ADMI_DOC_CTR,
(unsigned) Brw_ADMI_SHR_CTR,
RangeQuery,
SearchQuery,
(unsigned) Brw_ADMI_DOC_DEG,
(unsigned) Brw_ADMI_SHR_DEG,
RangeQuery,
SearchQuery,
(unsigned) Brw_ADMI_DOC_CRS,
(unsigned) Brw_ADMI_SHR_CRS,
RangeQuery);
/***** List documents found *****/
Brw_ListDocsFound (&mysql_res,NumDocs,
Txt_open_document,
Txt_open_documents);
return (unsigned) NumDocs;
2014-12-01 23:55:08 +01:00
}
return 0;
}
/*****************************************************************************/
/**************** Search documents in my courses in database *****************/
/*****************************************************************************/
static unsigned Sch_SearchDocumentsInMyCoursesInDB (const char *RangeQuery)
{
2016-04-18 09:46:16 +02:00
extern const char *Txt_document_in_my_courses;
extern const char *Txt_documents_in_my_courses;
2018-10-25 22:10:24 +02:00
char SearchQuery[Sch_MAX_BYTES_SEARCH_QUERY + 1];
2018-10-31 00:53:39 +01:00
MYSQL_RES *mysql_res;
unsigned long NumDocs;
2014-12-01 23:55:08 +01:00
/***** Check user's permission *****/
2015-01-26 12:39:48 +01:00
if (Sch_CheckIfIHavePermissionToSearch (Sch_SEARCH_DOCUM_IN_MY_COURSES))
2014-12-01 23:55:08 +01:00
/***** Split document string into words *****/
if (Sch_BuildSearchQuery (SearchQuery,"SUBSTRING_INDEX(files.Path,'/',-1)",
"_latin1 "," COLLATE latin1_general_ci"))
{
/***** Create temporary table with codes of files in documents and shared areas accessible by me.
It is necessary to speed up the second query *****/
2018-11-02 22:41:02 +01:00
DB_Query ("can not remove temporary table",
"DROP TEMPORARY TABLE IF EXISTS my_files_crs,my_files_grp");
DB_Query ("can not create temporary table",
"CREATE TEMPORARY TABLE my_files_crs"
" (FilCod INT NOT NULL,UNIQUE INDEX(FilCod))"
" ENGINE=MEMORY"
" SELECT files.FilCod FROM crs_usr,files"
" WHERE crs_usr.UsrCod=%ld"
" AND crs_usr.CrsCod=files.Cod"
" AND files.FileBrowser IN (%u,%u,%u,%u)",
Gbl.Usrs.Me.UsrDat.UsrCod,
(unsigned) Brw_ADMI_DOC_CRS,
(unsigned) Brw_ADMI_TCH_CRS,
(unsigned) Brw_ADMI_SHR_CRS,
(unsigned) Brw_ADMI_MRK_CRS);
DB_Query ("can not create temporary table",
"CREATE TEMPORARY TABLE my_files_grp"
" (FilCod INT NOT NULL,UNIQUE INDEX(FilCod))"
" ENGINE=MEMORY"
" SELECT files.FilCod FROM crs_grp_usr,files"
" WHERE crs_grp_usr.UsrCod=%ld"
" AND crs_grp_usr.GrpCod=files.Cod"
" AND files.FileBrowser IN (%u,%u,%u,%u)",
Gbl.Usrs.Me.UsrDat.UsrCod,
(unsigned) Brw_ADMI_DOC_GRP,
(unsigned) Brw_ADMI_TCH_GRP,
(unsigned) Brw_ADMI_SHR_GRP,
(unsigned) Brw_ADMI_MRK_GRP);
2014-12-01 23:55:08 +01:00
/***** Build the query *****/
2018-10-31 00:53:39 +01:00
NumDocs = DB_QuerySELECT (&mysql_res,"can not get files",
"SELECT * FROM "
"("
"SELECT files.FilCod,"
"SUBSTRING(files.Path,LOCATE('/',files.Path)) AS PathFromRoot,"
"institutions.InsCod,institutions.ShortName AS InsShortName,"
"centres.CtrCod,centres.ShortName AS CtrShortName,"
"degrees.DegCod,degrees.ShortName AS DegShortName,"
"courses.CrsCod,courses.ShortName AS CrsShortName,"
"-1 AS GrpCod"
" FROM files,courses,degrees,centres,institutions,countries"
" WHERE files.FilCod IN (SELECT FilCod FROM my_files_crs) AND %s"
" AND files.FileBrowser IN (%u,%u,%u,%u)"
" AND files.Cod=courses.CrsCod"
" AND courses.DegCod=degrees.DegCod"
" AND degrees.CtrCod=centres.CtrCod"
" AND centres.InsCod=institutions.InsCod"
" AND institutions.CtyCod=countries.CtyCod"
"%s"
" UNION "
"SELECT files.FilCod,"
"SUBSTRING(files.Path,LOCATE('/',files.Path)) AS PathFromRoot,"
"institutions.InsCod,institutions.ShortName AS InsShortName,"
"centres.CtrCod,centres.ShortName AS CtrShortName,"
"degrees.DegCod,degrees.ShortName AS DegShortName,"
"courses.CrsCod,courses.ShortName AS CrsShortName,"
"crs_grp.GrpCod"
" FROM files,crs_grp,crs_grp_types,courses,degrees,centres,institutions,countries"
" WHERE files.FilCod IN (SELECT FilCod FROM my_files_grp) AND %s"
" AND files.FileBrowser IN (%u,%u,%u,%u)"
" AND files.Cod=crs_grp.GrpCod"
" AND crs_grp.GrpTypCod=crs_grp_types.GrpTypCod"
" AND crs_grp_types.CrsCod=courses.CrsCod"
" AND courses.DegCod=degrees.DegCod"
" AND degrees.CtrCod=centres.CtrCod"
" AND centres.InsCod=institutions.InsCod"
" AND institutions.CtyCod=countries.CtyCod"
"%s"
") AS selected_files"
" WHERE PathFromRoot<>''"
" ORDER BY InsShortName,CtrShortName,DegShortName,CrsShortName,PathFromRoot",
SearchQuery,
(unsigned) Brw_ADMI_DOC_CRS,
(unsigned) Brw_ADMI_TCH_CRS,
(unsigned) Brw_ADMI_SHR_CRS,
(unsigned) Brw_ADMI_MRK_CRS,
RangeQuery,
SearchQuery,
(unsigned) Brw_ADMI_DOC_GRP,
(unsigned) Brw_ADMI_TCH_GRP,
(unsigned) Brw_ADMI_SHR_GRP,
(unsigned) Brw_ADMI_MRK_GRP,
RangeQuery);
/***** List documents found *****/
Brw_ListDocsFound (&mysql_res,NumDocs,
Txt_document_in_my_courses,
Txt_documents_in_my_courses);
2014-12-01 23:55:08 +01:00
/***** Drop temporary table *****/
2018-11-02 22:41:02 +01:00
DB_Query ("can not remove temporary table",
"DROP TEMPORARY TABLE IF EXISTS my_files_crs,my_files_grp");
2014-12-01 23:55:08 +01:00
2018-10-31 00:53:39 +01:00
return (unsigned) NumDocs;
2014-12-01 23:55:08 +01:00
}
return 0;
}
/*****************************************************************************/
/********************** Search my documents in database **********************/
/*****************************************************************************/
static unsigned Sch_SearchMyDocumentsInDB (const char *RangeQuery)
{
2016-04-18 09:46:16 +02:00
extern const char *Txt_document_from_me;
extern const char *Txt_documents_from_me;
2017-03-07 11:03:05 +01:00
char SearchQuery[Sch_MAX_BYTES_SEARCH_QUERY + 1];
2018-10-31 00:53:39 +01:00
MYSQL_RES *mysql_res;
unsigned long NumDocs;
2014-12-01 23:55:08 +01:00
/***** Check user's permission *****/
if (Sch_CheckIfIHavePermissionToSearch (Sch_SEARCH_MY_DOCUMENTS))
/***** Split document string into words *****/
if (Sch_BuildSearchQuery (SearchQuery,"SUBSTRING_INDEX(files.Path,'/',-1)",
"_latin1 "," COLLATE latin1_general_ci"))
{
/***** Build the query *****/
2018-10-31 00:53:39 +01:00
NumDocs = DB_QuerySELECT (&mysql_res,"can not get files",
"SELECT * FROM "
"("
"SELECT files.FilCod," // Institution
"SUBSTRING(files.Path,LOCATE('/',files.Path)) AS PathFromRoot,"
"institutions.InsCod,institutions.ShortName AS InsShortName,"
"-1 AS CtrCod,'' AS CtrShortName,"
"-1 AS DegCod,'' AS DegShortName,"
"-1 AS CrsCod,'' AS CrsShortName,"
"-1 AS GrpCod"
" FROM files,courses,degrees,centres,institutions,countries"
" WHERE files.PublisherUsrCod=%ld AND %s"
" AND files.FileBrowser IN (%u,%u)"
" AND files.Cod=institutions.InsCod"
" AND institutions.CtyCod=countries.CtyCod"
"%s"
" UNION "
"SELECT files.FilCod," // Centre
"SUBSTRING(files.Path,LOCATE('/',files.Path)) AS PathFromRoot,"
"institutions.InsCod,institutions.ShortName AS InsShortName,"
"centres.CtrCod,centres.ShortName AS CtrShortName,"
"-1 AS DegCod,'' AS DegShortName,"
"-1 AS CrsCod,'' AS CrsShortName,"
"-1 AS GrpCod"
" FROM files,courses,degrees,centres,institutions,countries"
" WHERE files.PublisherUsrCod=%ld AND %s"
" AND files.FileBrowser IN (%u,%u)"
" AND files.Cod=centres.CtrCod"
" AND centres.InsCod=institutions.InsCod"
" AND institutions.CtyCod=countries.CtyCod"
"%s"
" UNION "
"SELECT files.FilCod," // Degree
"SUBSTRING(files.Path,LOCATE('/',files.Path)) AS PathFromRoot,"
"institutions.InsCod,institutions.ShortName AS InsShortName,"
"centres.CtrCod,centres.ShortName AS CtrShortName,"
"degrees.DegCod,degrees.ShortName AS DegShortName,"
"-1 AS CrsCod,'' AS CrsShortName,"
"-1 AS GrpCod"
" FROM files,courses,degrees,centres,institutions,countries"
" WHERE files.PublisherUsrCod=%ld AND %s"
" AND files.FileBrowser IN (%u,%u)"
" AND files.Cod=degrees.DegCod"
" AND degrees.CtrCod=centres.CtrCod"
" AND centres.InsCod=institutions.InsCod"
" AND institutions.CtyCod=countries.CtyCod"
"%s"
" UNION "
"SELECT files.FilCod," // Course
"SUBSTRING(files.Path,LOCATE('/',files.Path)) AS PathFromRoot,"
"institutions.InsCod,institutions.ShortName AS InsShortName,"
"centres.CtrCod,centres.ShortName AS CtrShortName,"
"degrees.DegCod,degrees.ShortName AS DegShortName,"
"courses.CrsCod,courses.ShortName AS CrsShortName,"
"-1 AS GrpCod"
" FROM files,courses,degrees,centres,institutions,countries"
" WHERE files.PublisherUsrCod=%ld AND %s"
" AND files.FileBrowser IN (%u,%u,%u,%u)"
" AND files.Cod=courses.CrsCod"
" AND courses.DegCod=degrees.DegCod"
" AND degrees.CtrCod=centres.CtrCod"
" AND centres.InsCod=institutions.InsCod"
" AND institutions.CtyCod=countries.CtyCod"
"%s"
" UNION "
"SELECT files.FilCod," // Group
"SUBSTRING(files.Path,LOCATE('/',files.Path)) AS PathFromRoot,"
"institutions.InsCod,institutions.ShortName AS InsShortName,"
"centres.CtrCod,centres.ShortName AS CtrShortName,"
"degrees.DegCod,degrees.ShortName AS DegShortName,"
"courses.CrsCod,courses.ShortName AS CrsShortName,"
"crs_grp.GrpCod"
" FROM files,crs_grp,crs_grp_types,courses,degrees,centres,institutions,countries"
" WHERE files.PublisherUsrCod=%ld AND %s"
" AND files.FileBrowser IN (%u,%u,%u,%u)"
" AND files.Cod=crs_grp.GrpCod"
" AND crs_grp.GrpTypCod=crs_grp_types.GrpTypCod"
" AND crs_grp_types.CrsCod=courses.CrsCod"
" AND courses.DegCod=degrees.DegCod"
" AND degrees.CtrCod=centres.CtrCod"
" AND centres.InsCod=institutions.InsCod"
" AND institutions.CtyCod=countries.CtyCod"
"%s"
" UNION "
"SELECT files.FilCod," // Briefcase
"SUBSTRING(files.Path,LOCATE('/',files.Path)) AS PathFromRoot,"
"-1 AS InsCod,'' AS InsShortName,"
"-1 AS CtrCod,'' AS CtrShortName,"
"-1 AS DegCod,'' AS DegShortName,"
"-1 AS CrsCod,'' AS CrsShortName,"
"-1 AS GrpCod"
" FROM files"
" WHERE files.PublisherUsrCod=%ld AND %s"
" AND files.FileBrowser=%u"
") AS selected_files"
" WHERE PathFromRoot<>''"
" ORDER BY InsShortName,CtrShortName,DegShortName,CrsShortName,PathFromRoot",
Gbl.Usrs.Me.UsrDat.UsrCod,SearchQuery,
(unsigned) Brw_ADMI_DOC_INS,
(unsigned) Brw_ADMI_SHR_INS,
RangeQuery,
Gbl.Usrs.Me.UsrDat.UsrCod,SearchQuery,
(unsigned) Brw_ADMI_DOC_CTR,
(unsigned) Brw_ADMI_SHR_CTR,
RangeQuery,
Gbl.Usrs.Me.UsrDat.UsrCod,SearchQuery,
(unsigned) Brw_ADMI_DOC_DEG,
(unsigned) Brw_ADMI_SHR_DEG,
RangeQuery,
Gbl.Usrs.Me.UsrDat.UsrCod,SearchQuery,
(unsigned) Brw_ADMI_DOC_CRS,
(unsigned) Brw_ADMI_TCH_CRS,
(unsigned) Brw_ADMI_SHR_CRS,
(unsigned) Brw_ADMI_MRK_CRS,
RangeQuery,
Gbl.Usrs.Me.UsrDat.UsrCod,SearchQuery,
(unsigned) Brw_ADMI_DOC_GRP,
(unsigned) Brw_ADMI_TCH_GRP,
(unsigned) Brw_ADMI_SHR_GRP,
(unsigned) Brw_ADMI_MRK_GRP,
RangeQuery,
Gbl.Usrs.Me.UsrDat.UsrCod,SearchQuery,
(unsigned) Brw_ADMI_BRF_USR);
/***** List documents found *****/
Brw_ListDocsFound (&mysql_res,NumDocs,
Txt_document_from_me,
Txt_documents_from_me);
return (unsigned) NumDocs;
2014-12-01 23:55:08 +01:00
}
return 0;
}
/*****************************************************************************/
/****** Build a search query by splitting a string to search into words ******/
/*****************************************************************************/
2016-06-23 19:02:57 +02:00
// Returns true if a valid search query is built
2014-12-01 23:55:08 +01:00
// Returns false when no valid search query
2017-03-07 11:03:05 +01:00
bool Sch_BuildSearchQuery (char SearchQuery[Sch_MAX_BYTES_SEARCH_QUERY + 1],
2017-01-16 01:51:01 +01:00
const char *FieldName,
2016-06-29 18:27:49 +02:00
const char *CharSet,const char *Collate)
2014-12-01 23:55:08 +01:00
{
const char *Ptr;
unsigned NumWords;
2016-06-27 18:51:09 +02:00
unsigned NumWord;
2014-12-01 23:55:08 +01:00
size_t LengthWord;
2016-06-27 18:51:09 +02:00
size_t LengthTotal = 0;
2014-12-01 23:55:08 +01:00
size_t MaxLengthWord = 0;
2017-03-07 11:03:05 +01:00
char SearchWords[Sch_MAX_WORDS_IN_SEARCH][Sch_MAX_BYTES_SEARCH_WORD + 1];
2014-12-01 23:55:08 +01:00
bool SearchWordIsValid = true;
2016-06-29 18:27:49 +02:00
if (Gbl.Search.Str[0])
2014-12-01 23:55:08 +01:00
{
2016-06-29 18:27:49 +02:00
SearchQuery[0] = '\0';
Ptr = Gbl.Search.Str;
for (NumWords = 0;
NumWords < Sch_MAX_WORDS_IN_SEARCH && *Ptr;
NumWords++)
{
/* Get next word */
2017-03-07 11:03:05 +01:00
Str_GetNextStringUntilSpace (&Ptr,SearchWords[NumWords],Sch_MAX_BYTES_SEARCH_WORD);
2016-06-29 18:27:49 +02:00
/* Is this word valid? */
switch (Gbl.Search.WhatToSearch)
{
case Sch_SEARCH_OPEN_DOCUMENTS:
case Sch_SEARCH_DOCUM_IN_MY_COURSES:
case Sch_SEARCH_MY_DOCUMENTS:
SearchWordIsValid = Str_ConvertFilFolLnkNameToValid (SearchWords[NumWords]);
break;
default:
SearchWordIsValid = true;
break;
}
2014-12-01 23:55:08 +01:00
2016-06-29 18:27:49 +02:00
/* Check if this word is repeated (case insensitive) */
for (NumWord = 0;
SearchWordIsValid && NumWord < NumWords;
NumWord++)
if (!strcasecmp (SearchWords[NumWord],SearchWords[NumWords]))
SearchWordIsValid = false;
2016-06-27 18:51:09 +02:00
2016-06-29 18:27:49 +02:00
/* Concatenate word to search string */
if (SearchWordIsValid)
{
LengthWord = strlen (SearchWords[NumWords]);
LengthTotal += LengthWord;
if (LengthWord > MaxLengthWord)
MaxLengthWord = LengthWord;
2017-03-12 21:15:32 +01:00
if (strlen (SearchQuery) + 128 + LengthWord >
2017-03-07 11:03:05 +01:00
Sch_MAX_BYTES_SEARCH_QUERY) // Prevent string overflow
2016-06-29 18:27:49 +02:00
break;
if (NumWords)
2017-01-17 03:33:05 +01:00
Str_Concat (SearchQuery," AND ",
2017-03-07 11:03:05 +01:00
Sch_MAX_BYTES_SEARCH_QUERY);
2017-01-17 03:33:05 +01:00
Str_Concat (SearchQuery,FieldName,
2017-03-07 11:03:05 +01:00
Sch_MAX_BYTES_SEARCH_QUERY);
2017-01-17 03:33:05 +01:00
Str_Concat (SearchQuery," LIKE ",
2017-03-07 11:03:05 +01:00
Sch_MAX_BYTES_SEARCH_QUERY);
2016-06-29 18:27:49 +02:00
if (CharSet)
if (CharSet[0])
2017-01-17 03:33:05 +01:00
Str_Concat (SearchQuery,CharSet,
2017-03-07 11:03:05 +01:00
Sch_MAX_BYTES_SEARCH_QUERY);
2017-01-17 03:33:05 +01:00
Str_Concat (SearchQuery,"'%",
2017-03-07 11:03:05 +01:00
Sch_MAX_BYTES_SEARCH_QUERY);
2017-01-17 03:33:05 +01:00
Str_Concat (SearchQuery,SearchWords[NumWords],
2017-03-07 11:03:05 +01:00
Sch_MAX_BYTES_SEARCH_QUERY);
2017-01-17 03:33:05 +01:00
Str_Concat (SearchQuery,"%'",
2017-03-07 11:03:05 +01:00
Sch_MAX_BYTES_SEARCH_QUERY);
2016-06-29 18:27:49 +02:00
if (Collate)
if (Collate[0])
2017-01-17 03:33:05 +01:00
Str_Concat (SearchQuery,Collate,
2017-03-07 11:03:05 +01:00
Sch_MAX_BYTES_SEARCH_QUERY);
2016-06-29 18:27:49 +02:00
}
2014-12-01 23:55:08 +01:00
}
2016-06-29 18:27:49 +02:00
/***** If search string valid? *****/
if (LengthTotal < Sch_MIN_LENGTH_TOTAL ||
MaxLengthWord < Sch_MIN_LENGTH_LONGEST_WORD)
return false;
2016-06-27 18:51:09 +02:00
return true;
2016-06-29 18:27:49 +02:00
}
return false;
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/********************** Save last search into session ************************/
/*****************************************************************************/
static void Sch_SaveLastSearchIntoSession (void)
{
if (Gbl.Usrs.Me.Logged)
{
2017-01-29 12:42:19 +01:00
if (Gbl.Search.WhatToSearch == Sch_SEARCH_UNKNOWN)
Gbl.Search.WhatToSearch = Sch_WHAT_TO_SEARCH_DEFAULT;
2014-12-01 23:55:08 +01:00
/***** Save last search in session *****/
2018-11-03 12:16:40 +01:00
DB_QueryUPDATE ("can not update last search in session",
"UPDATE sessions SET WhatToSearch=%u,SearchStr='%s'"
" WHERE SessionId='%s'",
(unsigned) Gbl.Search.WhatToSearch,
Gbl.Search.Str,
Gbl.Session.Id);
2014-12-01 23:55:08 +01:00
/***** Update my last type of search *****/
// WhatToSearch is stored in usr_last for next time I log in
// In other existing sessions distinct to this, WhatToSearch will remain unchanged
2018-11-03 12:16:40 +01:00
DB_QueryUPDATE ("can not update type of search in user's last data",
2019-04-01 11:15:38 +02:00
"UPDATE usr_last SET WhatToSearch=%u"
" WHERE UsrCod=%ld",
2018-11-03 12:16:40 +01:00
(unsigned) Gbl.Search.WhatToSearch,
Gbl.Usrs.Me.UsrDat.UsrCod);
2014-12-01 23:55:08 +01:00
}
}