-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathswad_question_database.c
1511 lines (1382 loc) · 58.6 KB
/
swad_question_database.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// swad_question_database.c: test/exam/game questions, operations with database
/*
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.
Copyright (C) 1999-2025 Antonio Cañas Vargas
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public 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 *********************************/
/*****************************************************************************/
#include <string.h> // For string functions
#include "swad_alert.h"
#include "swad_database.h"
#include "swad_error.h"
#include "swad_global.h"
#include "swad_parameter.h"
#include "swad_question.h"
#include "swad_test_print.h"
/*****************************************************************************/
/***************************** Public constants ******************************/
/*****************************************************************************/
const char *Qst_DB_StrAnswerTypes[Qst_NUM_ANS_TYPES] =
{
[Qst_ANS_INT ] = "int",
[Qst_ANS_FLOAT ] = "float",
[Qst_ANS_TRUE_FALSE ] = "true_false",
[Qst_ANS_UNIQUE_CHOICE ] = "unique_choice",
[Qst_ANS_MULTIPLE_CHOICE] = "multiple_choice",
[Qst_ANS_TEXT ] = "text",
};
/*****************************************************************************/
/**************************** Private constants ******************************/
/*****************************************************************************/
#define Qst_MAX_BYTES_QUERY_QUESTIONS (16 * 1024 - 1)
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/*********** Insert or update question in the table of questions *************/
/*****************************************************************************/
long Qst_DB_CreateQst (const struct Qst_Question *Question)
{
return
DB_QueryINSERTandReturnCode ("can not create question",
"INSERT INTO tst_questions"
" (CrsCod,"
"EditTime,"
"AnsType,"
"Shuffle,"
"Stem,"
"Feedback,"
"MedCod,"
"NumHits,"
"Score)"
" VALUES"
" (%ld," // CrsCod
"NOW()," // EditTime
"'%s'," // AnsType
"'%c'," // Shuffle
"'%s'," // Stem
"'%s'," // Feedback
"%ld," // MedCod
"0," // NumHits
"0)", // Score
Gbl.Hierarchy.Node[Hie_CRS].HieCod,
Qst_DB_StrAnswerTypes[Question->Answer.Type],
Question->Answer.Shuffle ? 'Y' :
'N',
Question->Stem,
Question->Feedback ? Question->Feedback :
"",
Question->Media.MedCod);
}
/*****************************************************************************/
/************ Update existing question in the table of questions *************/
/*****************************************************************************/
void Qst_DB_UpdateQst (const struct Qst_Question *Question)
{
DB_QueryUPDATE ("can not update question",
"UPDATE tst_questions"
" SET EditTime=NOW(),"
"AnsType='%s',"
"Shuffle='%c',"
"Stem='%s',"
"Feedback='%s',"
"MedCod=%ld"
" WHERE QstCod=%ld"
" AND CrsCod=%ld", // Extra check
Qst_DB_StrAnswerTypes[Question->Answer.Type],
Question->Answer.Shuffle ? 'Y' :
'N',
Question->Stem,
Question->Feedback ? Question->Feedback :
"",
Question->Media.MedCod,
Question->QstCod,
Gbl.Hierarchy.Node[Hie_CRS].HieCod);
}
/*****************************************************************************/
/*********************** Update the score of a question **********************/
/*****************************************************************************/
void Qst_DB_UpdateQstScore (long QstCod,bool AnswerIsNotBlank,double Score)
{
Str_SetDecimalPointToUS (); // To print the floating point as a dot
if (AnswerIsNotBlank) // User's answer is not blank
DB_QueryUPDATE ("can not update the score of a question",
"UPDATE tst_questions"
" SET NumHits=NumHits+1,"
"NumHitsNotBlank=NumHitsNotBlank+1,"
"Score=Score+(%.15lg)"
" WHERE QstCod=%ld",
Score,
QstCod);
else // User's answer is blank
DB_QueryUPDATE ("can not update the score of a question",
"UPDATE tst_questions"
" SET NumHits=NumHits+1"
" WHERE QstCod=%ld",
QstCod);
Str_SetDecimalPointToLocal (); // Return to local system
}
/*****************************************************************************/
/*********************** Change the shuffle of a question ********************/
/*****************************************************************************/
void Qst_DB_UpdateQstShuffle (long QstCod,bool Shuffle)
{
DB_QueryUPDATE ("can not update the shuffle type of a question",
"UPDATE tst_questions"
" SET Shuffle='%c'"
" WHERE QstCod=%ld"
" AND CrsCod=%ld", // Extra check
Shuffle ? 'Y' :
'N',
QstCod,
Gbl.Hierarchy.Node[Hie_CRS].HieCod);
}
/*****************************************************************************/
/*************************** Create integer answer ***************************/
/*****************************************************************************/
void Qst_DB_CreateIntAnswer (const struct Qst_Question *Question)
{
DB_QueryINSERT ("can not create answer",
"INSERT INTO tst_answers"
" (QstCod,AnsInd,Answer,Feedback,MedCod,Correct)"
" VALUES"
" (%ld,0,%ld,'',-1,'Y')",
Question->QstCod,
Question->Answer.Integer);
}
/*****************************************************************************/
/**************************** Create float answer ****************************/
/*****************************************************************************/
void Qst_DB_CreateFltAnswer (const struct Qst_Question *Question)
{
unsigned i;
Str_SetDecimalPointToUS (); // To print the floating point as a dot
for (i = 0;
i < 2;
i++)
DB_QueryINSERT ("can not create answer",
"INSERT INTO tst_answers"
" (QstCod,AnsInd,Answer,Feedback,MedCod,Correct)"
" VALUES"
" (%ld,%u,'%.15lg','',-1,'Y')",
Question->QstCod,
i,
Question->Answer.FloatingPoint[i]);
Str_SetDecimalPointToLocal (); // Return to local system
}
/*****************************************************************************/
/***************************** Create T/F answer *****************************/
/*****************************************************************************/
void Qst_DB_CreateTF_Answer (const struct Qst_Question *Question)
{
DB_QueryINSERT ("can not create answer",
"INSERT INTO tst_answers"
" (QstCod,AnsInd,Answer,Feedback,MedCod,Correct)"
" VALUES"
" (%ld,0,'%c','',-1,'Y')",
Question->QstCod,
Question->Answer.TF);
}
/*****************************************************************************/
/***************************** Create T/F answer *****************************/
/*****************************************************************************/
void Qst_DB_CreateChoAnswer (struct Qst_Question *Question)
{
unsigned NumOpt;
for (NumOpt = 0;
NumOpt < Question->Answer.NumOptions;
NumOpt++)
if (Question->Answer.Options[NumOpt].Text[0] || // Text
Question->Answer.Options[NumOpt].Media.Type != Med_TYPE_NONE) // or media
{
DB_QueryINSERT ("can not create answer",
"INSERT INTO tst_answers"
" (QstCod,AnsInd,Answer,Feedback,MedCod,Correct)"
" VALUES"
" (%ld,%u,'%s','%s',%ld,'%c')",
Question->QstCod,NumOpt,
Question->Answer.Options[NumOpt].Text,
Question->Answer.Options[NumOpt].Feedback ? Question->Answer.Options[NumOpt].Feedback :
"",
Question->Answer.Options[NumOpt].Media.MedCod,
Question->Answer.Options[NumOpt].Correct ? 'Y' :
'N');
/* Update image status */
if (Question->Answer.Options[NumOpt].Media.Type != Med_TYPE_NONE)
Question->Answer.Options[NumOpt].Media.Status = Med_STORED_IN_DB;
}
}
/*****************************************************************************/
/***************** Get several test questions from database ******************/
/*****************************************************************************/
unsigned Qst_DB_GetQsts (MYSQL_RES **mysql_res,
const struct Qst_Questions *Questions)
{
extern const char *Qst_DB_StrAnswerTypes[Qst_NUM_ANS_TYPES];
extern const char *Txt_No_questions_found_matching_your_search_criteria;
char *Query = NULL;
long LengthQuery;
unsigned NumItemInList;
unsigned NumSelTag;
const char *Ptr;
char LongStr[Cns_MAX_DIGITS_LONG + 1];
char UnsignedStr[Cns_MAX_DIGITS_UINT + 1];
Qst_AnswerType_t AnsType;
char CrsCodStr[Cns_MAX_DIGITS_LONG + 1];
unsigned NumQsts;
/***** Allocate space for query *****/
if ((Query = malloc (Qst_MAX_BYTES_QUERY_QUESTIONS + 1)) == NULL)
Err_NotEnoughMemoryExit ();
/***** Select questions *****/
/* Begin query */
Str_Copy (Query,"SELECT tst_questions.QstCod" // row[0]
" FROM tst_questions",Qst_MAX_BYTES_QUERY_QUESTIONS);
if (!Questions->Tags.All)
Str_Concat (Query,",tst_question_tags",Qst_MAX_BYTES_QUERY_QUESTIONS);
Str_Concat (Query," WHERE tst_questions.CrsCod=",
Qst_MAX_BYTES_QUERY_QUESTIONS);
snprintf (CrsCodStr,sizeof (CrsCodStr),"%ld",
Gbl.Hierarchy.Node[Hie_CRS].HieCod);
Str_Concat (Query,CrsCodStr,Qst_MAX_BYTES_QUERY_QUESTIONS);
Str_Concat (Query," AND tst_questions.EditTime>=FROM_UNIXTIME('",
Qst_MAX_BYTES_QUERY_QUESTIONS);
snprintf (LongStr,sizeof (LongStr),"%ld",
(long) Dat_GetRangeTimeUTC (Dat_STR_TIME));
Str_Concat (Query,LongStr,Qst_MAX_BYTES_QUERY_QUESTIONS);
Str_Concat (Query,"') AND tst_questions.EditTime<=FROM_UNIXTIME('",
Qst_MAX_BYTES_QUERY_QUESTIONS);
snprintf (LongStr,sizeof (LongStr),"%ld",
(long) Dat_GetRangeTimeUTC (Dat_END_TIME));
Str_Concat (Query,LongStr,Qst_MAX_BYTES_QUERY_QUESTIONS);
Str_Concat (Query,"')",Qst_MAX_BYTES_QUERY_QUESTIONS);
/* Add the tags selected */
if (!Questions->Tags.All)
{
Str_Concat (Query," AND tst_questions.QstCod=tst_question_tags.QstCod",
Qst_MAX_BYTES_QUERY_QUESTIONS);
for (NumSelTag = 0, LengthQuery = strlen (Query);
NumSelTag < Questions->Tags.NumSelected;
NumSelTag++)
{
snprintf (LongStr,sizeof (LongStr),"%ld",
Questions->Tags.ListSelectedTagCods[NumSelTag]);
LengthQuery += 35 + strlen (LongStr) + 1;
if (LengthQuery > Qst_MAX_BYTES_QUERY_QUESTIONS - 256)
Err_QuerySizeExceededExit ();
Str_Concat (Query,NumSelTag ? " OR tst_question_tags.TagCod=" :
" AND (tst_question_tags.TagCod=",
Qst_MAX_BYTES_QUERY_QUESTIONS);
Str_Concat (Query,LongStr,Qst_MAX_BYTES_QUERY_QUESTIONS);
}
Str_Concat (Query,")",Qst_MAX_BYTES_QUERY_QUESTIONS);
}
/* Add the types of answer selected */
if (!Questions->AnswerTypes.All)
{
for (Ptr = Questions->AnswerTypes.List, NumItemInList = 0, LengthQuery = strlen (Query);
*Ptr;
NumItemInList++)
{
Par_GetNextStrUntilSeparParMult (&Ptr,UnsignedStr,Tag_MAX_BYTES_TAG);
AnsType = Qst_ConvertFromUnsignedStrToAnsTyp (UnsignedStr);
LengthQuery += 35 + strlen (Qst_DB_StrAnswerTypes[AnsType]) + 1;
if (LengthQuery > Qst_MAX_BYTES_QUERY_QUESTIONS - 256)
Err_QuerySizeExceededExit ();
Str_Concat (Query,NumItemInList ? " OR tst_questions.AnsType='" :
" AND (tst_questions.AnsType='",
Qst_MAX_BYTES_QUERY_QUESTIONS);
Str_Concat (Query,Qst_DB_StrAnswerTypes[AnsType],
Qst_MAX_BYTES_QUERY_QUESTIONS);
Str_Concat (Query,"'",Qst_MAX_BYTES_QUERY_QUESTIONS);
}
Str_Concat (Query,")",Qst_MAX_BYTES_QUERY_QUESTIONS);
}
/* End the query */
Str_Concat (Query," GROUP BY tst_questions.QstCod",Qst_MAX_BYTES_QUERY_QUESTIONS);
switch (Questions->SelectedOrder)
{
case Qst_ORDER_STEM:
Str_Concat (Query," ORDER BY tst_questions.Stem",
Qst_MAX_BYTES_QUERY_QUESTIONS);
break;
case Qst_ORDER_NUM_HITS:
Str_Concat (Query," ORDER BY tst_questions.NumHits DESC,"
"tst_questions.Stem",
Qst_MAX_BYTES_QUERY_QUESTIONS);
break;
case Qst_ORDER_AVERAGE_SCORE:
Str_Concat (Query," ORDER BY tst_questions.Score/tst_questions.NumHits DESC,"
"tst_questions.NumHits DESC,"
"tst_questions.Stem",
Qst_MAX_BYTES_QUERY_QUESTIONS);
break;
case Qst_ORDER_NUM_HITS_NOT_BLANK:
Str_Concat (Query," ORDER BY tst_questions.NumHitsNotBlank DESC,"
"tst_questions.Stem",
Qst_MAX_BYTES_QUERY_QUESTIONS);
break;
case Qst_ORDER_AVERAGE_SCORE_NOT_BLANK:
Str_Concat (Query," ORDER BY tst_questions.Score/tst_questions.NumHitsNotBlank DESC,"
"tst_questions.NumHitsNotBlank DESC,"
"tst_questions.Stem",
Qst_MAX_BYTES_QUERY_QUESTIONS);
break;
}
/* Make the query */
if ((NumQsts = (unsigned)
DB_QuerySELECT (mysql_res,"can not get questions",
"%s",
Query)) == 0)
Ale_ShowAlert (Ale_INFO,Txt_No_questions_found_matching_your_search_criteria);
return NumQsts;
}
/*****************************************************************************/
/******************* Get questions for a new test print **********************/
/*****************************************************************************/
unsigned Qst_DB_GetQstsForNewTestPrint (MYSQL_RES **mysql_res,
const struct Qst_Questions *Questions)
{
extern const char *Qst_DB_StrAnswerTypes[Qst_NUM_ANS_TYPES];
char *Query = NULL;
long LengthQuery;
unsigned NumSelTag;
unsigned NumItemInList;
const char *Ptr;
char LongStr[Cns_MAX_DIGITS_LONG + 1];
char UnsignedStr[Cns_MAX_DIGITS_UINT + 1];
Qst_AnswerType_t AnswerType;
char StrNumQsts[Cns_MAX_DIGITS_UINT + 1];
/***** Allocate space for query *****/
if ((Query = malloc (Qst_MAX_BYTES_QUERY_QUESTIONS + 1)) == NULL)
Err_NotEnoughMemoryExit ();
/***** Select questions without hidden tags *****/
/* Begin query */
// Reject questions with any tag hidden
// Select only questions with tags
// DISTINCT is necessary to not repeat questions
snprintf (Query,Qst_MAX_BYTES_QUERY_QUESTIONS + 1,
"SELECT DISTINCT "
"tst_questions.QstCod," // row[0]
"tst_questions.AnsType," // row[1]
"tst_questions.Shuffle" // row[2]
" FROM tst_questions,tst_question_tags"
" WHERE tst_questions.CrsCod=%ld"
" AND tst_questions.QstCod NOT IN"
" (SELECT tst_question_tags.QstCod"
" FROM tst_tags,tst_question_tags"
" WHERE tst_tags.CrsCod=%ld"
" AND tst_tags.TagHidden='Y'"
" AND tst_tags.TagCod=tst_question_tags.TagCod)"
" AND tst_questions.QstCod=tst_question_tags.QstCod",
Gbl.Hierarchy.Node[Hie_CRS].HieCod,
Gbl.Hierarchy.Node[Hie_CRS].HieCod);
/* Add selected tags */
if (Questions->Tags.PreselectedTagCod > 0) // Only one preselected tag
{
Str_Concat (Query," AND tst_question_tags.TagCod=",
Qst_MAX_BYTES_QUERY_QUESTIONS);
snprintf (LongStr,sizeof (LongStr),"%ld",
Questions->Tags.PreselectedTagCod);
Str_Concat (Query,LongStr,Qst_MAX_BYTES_QUERY_QUESTIONS);
}
else if (!Questions->Tags.All) // User has selected some tags, but not all
{
LengthQuery = strlen (Query);
for (NumSelTag = 0;
NumSelTag < Questions->Tags.NumSelected;
NumSelTag++)
{
snprintf (LongStr,sizeof (LongStr),"%ld",
Questions->Tags.ListSelectedTagCods[NumSelTag]);
LengthQuery += 35 + strlen (LongStr) + 1;
if (LengthQuery > Qst_MAX_BYTES_QUERY_QUESTIONS - 128)
Err_QuerySizeExceededExit ();
Str_Concat (Query,NumSelTag ? " OR tst_question_tags.TagCod=" :
" AND (tst_question_tags.TagCod=",
Qst_MAX_BYTES_QUERY_QUESTIONS);
Str_Concat (Query,LongStr,Qst_MAX_BYTES_QUERY_QUESTIONS);
}
Str_Concat (Query,")",Qst_MAX_BYTES_QUERY_QUESTIONS);
}
/* Add answer types selected */
if (!Questions->AnswerTypes.All)
{
for (Ptr = Questions->AnswerTypes.List, NumItemInList = 0, LengthQuery = strlen (Query);
*Ptr;
NumItemInList++)
{
Par_GetNextStrUntilSeparParMult (&Ptr,UnsignedStr,Tag_MAX_BYTES_TAG);
AnswerType = Qst_ConvertFromUnsignedStrToAnsTyp (UnsignedStr);
LengthQuery += 35 + strlen (Qst_DB_StrAnswerTypes[AnswerType]) + 1;
if (LengthQuery > Qst_MAX_BYTES_QUERY_QUESTIONS - 128)
Err_QuerySizeExceededExit ();
Str_Concat (Query,NumItemInList ? " OR tst_questions.AnsType='" :
" AND (tst_questions.AnsType='",
Qst_MAX_BYTES_QUERY_QUESTIONS);
Str_Concat (Query,Qst_DB_StrAnswerTypes[AnswerType],Qst_MAX_BYTES_QUERY_QUESTIONS);
Str_Concat (Query,"'",Qst_MAX_BYTES_QUERY_QUESTIONS);
}
Str_Concat (Query,")",Qst_MAX_BYTES_QUERY_QUESTIONS);
}
/* End query */
Str_Concat (Query," ORDER BY RAND() LIMIT ",Qst_MAX_BYTES_QUERY_QUESTIONS);
snprintf (StrNumQsts,sizeof (StrNumQsts),"%u",Questions->NumQsts);
Str_Concat (Query,StrNumQsts,Qst_MAX_BYTES_QUERY_QUESTIONS);
/*
if (Gbl.Usrs.Me.Roles.LoggedRole == Rol_SYS_ADM)
Lay_ShowAlert (Lay_INFO,Query);
*/
/* Make the query */
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get questions",
"%s",
Query);
}
/*****************************************************************************/
/******************* Get one question for trivial game **********************/
/*****************************************************************************/
unsigned Qst_DB_GetTrivialQst (MYSQL_RES **mysql_res,
char DegreesStr[API_MAX_BYTES_DEGREES_STR + 1],
float lowerScore,float upperScore)
{
unsigned NumQsts;
Str_SetDecimalPointToUS (); // To print the floating point as a dot
NumQsts = (unsigned)
DB_QuerySELECT (mysql_res,"can not get test questions",
"SELECT DISTINCT "
"tst_questions.QstCod," // row[0]
"tst_questions.AnsType," // row[1]
"tst_questions.Shuffle," // row[2]
"tst_questions.Stem," // row[3]
"tst_questions.Feedback," // row[4]
"tst_questions.Score/tst_questions.NumHits AS S" // row[5]
" FROM crs_courses,"
"tst_questions"
" WHERE crs_courses.DegCod IN (%s)"
" AND crs_courses.CrsCod=tst_questions.CrsCod"
" AND tst_questions.AnsType='unique_choice'"
" AND tst_questions.NumHits>0"
" AND tst_questions.QstCod NOT IN"
" (SELECT tst_question_tags.QstCod"
" FROM crs_courses,"
"tst_tags,"
"tst_question_tags"
" WHERE crs_courses.DegCod IN (%s)"
" AND crs_courses.CrsCod=tst_tags.CrsCod"
" AND tst_tags.TagHidden='Y'"
" AND tst_tags.TagCod=tst_question_tags.TagCod)"
" HAVING S>='%f'"
" AND S<='%f'"
" ORDER BY RAND()"
" LIMIT 1",
DegreesStr,
DegreesStr,
lowerScore,
upperScore);
Str_SetDecimalPointToLocal (); // Return to local system
return NumQsts;
}
/*****************************************************************************/
/** Get number of visible test questions from database giving a course code **/
/*****************************************************************************/
unsigned Qst_DB_GetNumQstsInCrs (long CrsCod)
{
/***** Get number of questions *****/
// Reject questions with any tag hidden
// Select only questions with tags
return (unsigned)
DB_QueryCOUNT ("can not get number of test questions",
"SELECT COUNT(*)"
" FROM tst_questions,"
"tst_question_tags,"
"tst_tags"
" WHERE tst_questions.CrsCod=%ld"
" AND tst_questions.QstCod NOT IN"
" (SELECT tst_question_tags.QstCod"
" FROM tst_tags,"
"tst_question_tags"
" WHERE tst_tags.CrsCod=%ld"
" AND tst_tags.TagHidden='Y'"
" AND tst_tags.TagCod=tst_question_tags.TagCod)"
" AND tst_questions.QstCod=tst_question_tags.QstCod"
" AND tst_question_tags.TagCod=tst_tags.TagCod"
" AND tst_tags.CrsCod=%ld",
CrsCod,
CrsCod,
CrsCod);
}
/*****************************************************************************/
/*********************** Get number of test questions ************************/
/*****************************************************************************/
// Returns the number of test questions
// in this location (all the platform, current degree or current course)
unsigned Qst_DB_GetNumQsts (MYSQL_RES **mysql_res,
Hie_Level_t Level,Qst_AnswerType_t AnsType)
{
switch (Level)
{
case Hie_SYS:
if (AnsType == Qst_ANS_UNKNOWN) // Any type
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get number of test questions",
"SELECT COUNT(*)," // row[0]
"SUM(NumHits)," // row[1]
"SUM(Score)" // row[2]
" FROM tst_questions");
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get number of test questions",
"SELECT COUNT(*)," // row[0]
"SUM(NumHits)," // row[1]
"SUM(Score)" // row[2]
" FROM tst_questions"
" WHERE AnsType='%s'",
Qst_DB_StrAnswerTypes[AnsType]);
case Hie_CTY:
if (AnsType == Qst_ANS_UNKNOWN) // Any type
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get number of test questions",
"SELECT COUNT(*)," // row[0]
"SUM(NumHits)," // row[1]
"SUM(Score)" // row[2]
" FROM ins_instits,"
"ctr_centers,"
"deg_degrees,"
"crs_courses,"
"tst_questions"
" WHERE ins_instits.CtyCod=%ld"
" AND ins_instits.InsCod=ctr_centers.InsCod"
" AND ctr_centers.CtrCod=deg_degrees.CtrCod"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=tst_questions.CrsCod",
Gbl.Hierarchy.Node[Hie_CTY].HieCod);
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get number of test questions",
"SELECT COUNT(*)," // row[0]
"SUM(NumHits)," // row[1]
"SUM(Score)" // row[2]
" FROM ins_instits,"
"ctr_centers,"
"deg_degrees,"
"crs_courses,"
"tst_questions"
" WHERE ins_instits.CtyCod=%ld"
" AND ins_instits.InsCod=ctr_centers.InsCod"
" AND ctr_centers.CtrCod=deg_degrees.CtrCod"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=tst_questions.CrsCod"
" AND tst_questions.AnsType='%s'",
Gbl.Hierarchy.Node[Hie_CTY].HieCod,
Qst_DB_StrAnswerTypes[AnsType]);
case Hie_INS:
if (AnsType == Qst_ANS_UNKNOWN) // Any type
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get number of test questions",
"SELECT COUNT(*)," // row[0]
"SUM(NumHits)," // row[1]
"SUM(Score)" // row[2]
" FROM ctr_centers,"
"deg_degrees,"
"crs_courses,"
"tst_questions"
" WHERE ctr_centers.InsCod=%ld"
" AND ctr_centers.CtrCod=deg_degrees.CtrCod"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=tst_questions.CrsCod",
Gbl.Hierarchy.Node[Hie_INS].HieCod);
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get number of test questions",
"SELECT COUNT(*)," // row[0]
"SUM(NumHits)," // row[1]
"SUM(Score)" // row[2]
" FROM ctr_centers,"
"deg_degrees,"
"crs_courses,"
"tst_questions"
" WHERE ctr_centers.InsCod=%ld"
" AND ctr_centers.CtrCod=deg_degrees.CtrCod"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=tst_questions.CrsCod"
" AND tst_questions.AnsType='%s'",
Gbl.Hierarchy.Node[Hie_INS].HieCod,
Qst_DB_StrAnswerTypes[AnsType]);
case Hie_CTR:
if (AnsType == Qst_ANS_UNKNOWN) // Any type
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get number of test questions",
"SELECT COUNT(*)," // row[0]
"SUM(NumHits)," // row[1]
"SUM(Score)" // row[2]
" FROM deg_degrees,"
"crs_courses,"
"tst_questions"
" WHERE deg_degrees.CtrCod=%ld"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=tst_questions.CrsCod",
Gbl.Hierarchy.Node[Hie_CTR].HieCod);
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get number of test questions",
"SELECT COUNT(*)," // row[0]
"SUM(NumHits)," // row[1]
"SUM(Score)" // row[2]
" FROM deg_degrees,"
"crs_courses,"
"tst_questions"
" WHERE deg_degrees.CtrCod=%ld"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=tst_questions.CrsCod"
" AND tst_questions.AnsType='%s'",
Gbl.Hierarchy.Node[Hie_CTR].HieCod,
Qst_DB_StrAnswerTypes[AnsType]);
case Hie_DEG:
if (AnsType == Qst_ANS_UNKNOWN) // Any type
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get number of test questions",
"SELECT COUNT(*)," // row[0]
"SUM(NumHits)," // row[1]
"SUM(Score)" // row[2]
" FROM crs_courses,"
"tst_questions"
" WHERE crs_courses.DegCod=%ld"
" AND crs_courses.CrsCod=tst_questions.CrsCod",
Gbl.Hierarchy.Node[Hie_DEG].HieCod);
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get number of test questions",
"SELECT COUNT(*)," // row[0]
"SUM(NumHits)," // row[1]
"SUM(Score)" // row[2]
" FROM crs_courses,"
"tst_questions"
" WHERE crs_courses.DegCod=%ld"
" AND crs_courses.CrsCod=tst_questions.CrsCod"
" AND tst_questions.AnsType='%s'",
Gbl.Hierarchy.Node[Hie_DEG].HieCod,
Qst_DB_StrAnswerTypes[AnsType]);
case Hie_CRS:
if (AnsType == Qst_ANS_UNKNOWN) // Any type
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get number of test questions",
"SELECT COUNT(*)," // row[0]
"SUM(NumHits)," // row[1]
"SUM(Score)" // row[2]
" FROM tst_questions"
" WHERE CrsCod=%ld",
Gbl.Hierarchy.Node[Hie_CRS].HieCod);
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get number of test questions",
"SELECT COUNT(*)," // row[0]
"SUM(NumHits)," // row[1]
"SUM(Score)" // row[2]
" FROM tst_questions"
" WHERE CrsCod=%ld"
" AND AnsType='%s'",
Gbl.Hierarchy.Node[Hie_CRS].HieCod,
Qst_DB_StrAnswerTypes[AnsType]);
default:
Err_WrongHierarchyLevelExit ();
return 0; // Not reached
}
}
/*****************************************************************************/
/**************** Get number of courses with test questions ******************/
/*****************************************************************************/
// Returns the number of courses with test questions
// in this location (all the platform, current degree or current course)
unsigned Qst_DB_GetNumCrssWithQsts (Hie_Level_t Level,
Qst_AnswerType_t AnsType)
{
extern const char *Qst_DB_StrAnswerTypes[Qst_NUM_ANS_TYPES];
/***** Get number of courses with test questions from database *****/
switch (Level)
{
case Hie_SYS:
if (AnsType == Qst_ANS_UNKNOWN) // Any type
return (unsigned)
DB_QueryCOUNT ("can not get number of courses with test questions",
"SELECT COUNT(DISTINCT CrsCod)"
" FROM tst_questions");
return (unsigned)
DB_QueryCOUNT ("can not get number of courses with test questions",
"SELECT COUNT(DISTINCT CrsCod)"
" FROM tst_questions"
" WHERE AnsType='%s'",
Qst_DB_StrAnswerTypes[AnsType]);
case Hie_CTY:
if (AnsType == Qst_ANS_UNKNOWN) // Any type
return (unsigned)
DB_QueryCOUNT ("can not get number of courses with test questions",
"SELECT COUNT(DISTINCT tst_questions.CrsCod)"
" FROM ins_instits,"
"ctr_centers,"
"deg_degrees,"
"crs_courses,"
"tst_questions"
" WHERE ins_instits.CtyCod=%ld"
" AND ins_instits.InsCod=ctr_centers.InsCod"
" AND ctr_centers.CtrCod=deg_degrees.CtrCod"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=tst_questions.CrsCod",
Gbl.Hierarchy.Node[Hie_CTY].HieCod);
return (unsigned)
DB_QueryCOUNT ("can not get number of courses with test questions",
"SELECT COUNT(DISTINCT tst_questions.CrsCod)"
" FROM ins_instits,"
"ctr_centers,"
"deg_degrees,"
"crs_courses,"
"tst_questions"
" WHERE ins_instits.CtyCod=%ld"
" AND ins_instits.InsCod=ctr_centers.InsCod"
" AND ctr_centers.CtrCod=deg_degrees.CtrCod"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=tst_questions.CrsCod"
" AND tst_questions.AnsType='%s'",
Gbl.Hierarchy.Node[Hie_CTY].HieCod,
Qst_DB_StrAnswerTypes[AnsType]);
case Hie_INS:
if (AnsType == Qst_ANS_UNKNOWN) // Any type
return (unsigned)
DB_QueryCOUNT ("can not get number of courses with test questions",
"SELECT COUNT(DISTINCT tst_questions.CrsCod)"
" FROM ctr_centers,"
"deg_degrees,"
"crs_courses,"
"tst_questions"
" WHERE ctr_centers.InsCod=%ld"
" AND ctr_centers.CtrCod=deg_degrees.CtrCod"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=tst_questions.CrsCod",
Gbl.Hierarchy.Node[Hie_INS].HieCod);
return (unsigned)
DB_QueryCOUNT ("can not get number of courses with test questions",
"SELECT COUNT(DISTINCT tst_questions.CrsCod)"
" FROM ctr_centers,"
"deg_degrees,"
"crs_courses,"
"tst_questions"
" WHERE ctr_centers.InsCod=%ld"
" AND ctr_centers.CtrCod=deg_degrees.CtrCod"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=tst_questions.CrsCod"
" AND tst_questions.AnsType='%s'",
Gbl.Hierarchy.Node[Hie_INS].HieCod,
Qst_DB_StrAnswerTypes[AnsType]);
case Hie_CTR:
if (AnsType == Qst_ANS_UNKNOWN) // Any type
return (unsigned)
DB_QueryCOUNT ("can not get number of courses with test questions",
"SELECT COUNT(DISTINCT tst_questions.CrsCod)"
" FROM deg_degrees,"
"crs_courses,"
"tst_questions"
" WHERE deg_degrees.CtrCod=%ld"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=tst_questions.CrsCod",
Gbl.Hierarchy.Node[Hie_CTR].HieCod);
return (unsigned)
DB_QueryCOUNT ("can not get number of courses with test questions",
"SELECT COUNT(DISTINCT tst_questions.CrsCod)"
" FROM deg_degrees,"
"crs_courses,"
"tst_questions"
" WHERE deg_degrees.CtrCod=%ld"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=tst_questions.CrsCod"
" AND tst_questions.AnsType='%s'",
Gbl.Hierarchy.Node[Hie_CTR].HieCod,
Qst_DB_StrAnswerTypes[AnsType]);
case Hie_DEG:
if (AnsType == Qst_ANS_UNKNOWN) // Any type
return (unsigned)
DB_QueryCOUNT ("can not get number of courses with test questions",
"SELECT COUNTDISTINCT (tst_questions.CrsCod)"
" FROM crs_courses,"
"tst_questions"
" WHERE crs_courses.DegCod=%ld"
" AND crs_courses.CrsCod=tst_questions.CrsCod",
Gbl.Hierarchy.Node[Hie_DEG].HieCod);
return (unsigned)
DB_QueryCOUNT ("can not get number of courses with test questions",
"SELECT COUNT(DISTINCT tst_questions.CrsCod)"
" FROM crs_courses,"
"tst_questions"
" WHERE crs_courses.DegCod=%ld"
" AND crs_courses.CrsCod=tst_questions.CrsCod"
" AND tst_questions.AnsType='%s'",
Gbl.Hierarchy.Node[Hie_DEG].HieCod,
Qst_DB_StrAnswerTypes[AnsType]);
case Hie_CRS:
if (AnsType == Qst_ANS_UNKNOWN) // Any type
return (unsigned)
DB_QueryCOUNT ("can not get number of courses with test questions",
"SELECT COUNT(DISTINCT CrsCod)"
" FROM tst_questions"
" WHERE CrsCod=%ld",
Gbl.Hierarchy.Node[Hie_CRS].HieCod);
return (unsigned)
DB_QueryCOUNT ("can not get number of courses with test questions",
"SELECT COUNT(DISTINCT CrsCod)"
" FROM tst_questions"
" WHERE CrsCod=%ld"
" AND AnsType='%s'",
Gbl.Hierarchy.Node[Hie_CRS].HieCod,
Qst_DB_StrAnswerTypes[AnsType]);
default:
Err_WrongHierarchyLevelExit ();
return 0; // Not reached
}
}
/*****************************************************************************/
/*********** Get number of courses with pluggable test questions *************/
/*****************************************************************************/
// Returns the number of courses with pluggable test questions
// in this location (all the platform, current degree or current course)
unsigned Qst_DB_GetNumCrssWithPluggableQsts (Hie_Level_t Level,
Qst_AnswerType_t AnsType)
{
extern const char *Qst_DB_StrAnswerTypes[Qst_NUM_ANS_TYPES];
extern const char *Tst_DB_Pluggable[TstCfg_NUM_OPTIONS_PLUGGABLE];
/***** Get number of courses with test questions from database *****/
switch (Level)
{
case Hie_SYS:
if (AnsType == Qst_ANS_UNKNOWN) // Any type
return (unsigned)
DB_QueryCOUNT ("can not get number of courses with pluggable test questions",
"SELECT COUNT(DISTINCT tst_questions.CrsCod)"
" FROM tst_questions,"
"tst_config"
" WHERE tst_questions.CrsCod=tst_config.CrsCod"
" AND tst_config.pluggable='%s'",
Tst_DB_Pluggable[TstCfg_PLUGGABLE_YES]);
return (unsigned)
DB_QueryCOUNT ("can not get number of courses with pluggable test questions",
"SELECT COUNT(DISTINCT tst_questions.CrsCod)"
" FROM tst_questions,"
"tst_config"
" WHERE tst_questions.AnsType='%s'"
" AND tst_questions.CrsCod=tst_config.CrsCod"
" AND tst_config.pluggable='%s'",
Qst_DB_StrAnswerTypes[AnsType],
Tst_DB_Pluggable[TstCfg_PLUGGABLE_YES]);
case Hie_CTY:
if (AnsType == Qst_ANS_UNKNOWN) // Any type
return (unsigned)
DB_QueryCOUNT ("can not get number of courses with pluggable test questions",
"SELECT COUNT(DISTINCT tst_questions.CrsCod)"
" FROM ins_instits,"
"ctr_centers,"
"deg_degrees,"
"crs_courses,"
"tst_questions,"
"tst_config"
" WHERE ins_instits.CtyCod=%ld"
" AND ins_instits.InsCod=ctr_centers.InsCod"
" AND ctr_centers.CtrCod=deg_degrees.CtrCod"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=tst_questions.CrsCod"
" AND tst_questions.CrsCod=tst_config.CrsCod"
" AND tst_config.pluggable='%s'",
Gbl.Hierarchy.Node[Hie_CTY].HieCod,
Tst_DB_Pluggable[TstCfg_PLUGGABLE_YES]);
return (unsigned)
DB_QueryCOUNT ("can not get number of courses with pluggable test questions",
"SELECT COUNT(DISTINCT tst_questions.CrsCod)"
" FROM ins_instits,"
"ctr_centers,"
"deg_degrees,"
"crs_courses,"
"tst_questions,"
"tst_config"
" WHERE ins_instits.CtyCod=%ld"
" AND ins_instits.InsCod=ctr_centers.InsCod"
" AND ctr_centers.CtrCod=deg_degrees.CtrCod"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=tst_questions.CrsCod"
" AND tst_questions.AnsType='%s'"
" AND tst_questions.CrsCod=tst_config.CrsCod"
" AND tst_config.pluggable='%s'",
Gbl.Hierarchy.Node[Hie_CTY].HieCod,
Qst_DB_StrAnswerTypes[AnsType],
Tst_DB_Pluggable[TstCfg_PLUGGABLE_YES]);
case Hie_INS:
if (AnsType == Qst_ANS_UNKNOWN) // Any type
return (unsigned)
DB_QueryCOUNT ("can not get number of courses with pluggable test questions",
"SELECT COUNT(DISTINCT tst_questions.CrsCod)"