exercise.lib.php 162 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Class ExerciseLib
  6. * shows a question and its answers
  7. * @author Olivier Brouckaert <oli.brouckaert@skynet.be>
  8. * @author Hubert Borderiou 2011-10-21
  9. * @author ivantcholakov2009-07-20
  10. *
  11. */
  12. class ExerciseLib
  13. {
  14. /**
  15. * Shows a question
  16. *
  17. * @param int $questionId $questionId question id
  18. * @param bool $only_questions if true only show the questions, no exercise title
  19. * @param bool $origin i.e = learnpath
  20. * @param string $current_item current item from the list of questions
  21. * @param bool $show_title
  22. * @param bool $freeze
  23. * @param array $user_choice
  24. * @param bool $show_comment
  25. * @param null $exercise_feedback
  26. * @param bool $show_answers
  27. * @return bool|int
  28. */
  29. public static function showQuestion(
  30. $questionId,
  31. $only_questions = false,
  32. $origin = false,
  33. $current_item = '',
  34. $show_title = true,
  35. $freeze = false,
  36. $user_choice = array(),
  37. $show_comment = false,
  38. $exercise_feedback = null,
  39. $show_answers = false,
  40. $show_icon = false
  41. ) {
  42. $course_id = api_get_course_int_id();
  43. $course = api_get_course_info_by_id($course_id);
  44. // Change false to true in the following line to enable answer hinting
  45. $debug_mark_answer = $show_answers;
  46. // Reads question information
  47. if (!$objQuestionTmp = Question::read($questionId)) {
  48. // Question not found
  49. return false;
  50. }
  51. if ($exercise_feedback != EXERCISE_FEEDBACK_TYPE_END) {
  52. $show_comment = false;
  53. }
  54. $answerType = $objQuestionTmp->selectType();
  55. $pictureName = $objQuestionTmp->getPictureFilename();
  56. $s = '';
  57. if ($answerType != HOT_SPOT && $answerType != HOT_SPOT_DELINEATION && $answerType != ANNOTATION) {
  58. // Question is not a hotspot
  59. if (!$only_questions) {
  60. $questionDescription = $objQuestionTmp->selectDescription();
  61. if ($show_title) {
  62. TestCategory::displayCategoryAndTitle($objQuestionTmp->id);
  63. $titleToDisplay = null;
  64. if ($answerType == READING_COMPREHENSION) {
  65. // In READING_COMPREHENSION, the title of the question
  66. // contains the question itself, which can only be
  67. // shown at the end of the given time, so hide for now
  68. $titleToDisplay = Display::div(
  69. $current_item.'. '.get_lang('ReadingComprehension'),
  70. ['class' => 'question_title']
  71. );
  72. } else {
  73. $titleToDisplay = $objQuestionTmp->getTitleToDisplay($current_item);
  74. }
  75. echo $titleToDisplay;
  76. }
  77. if (!empty($questionDescription) && $answerType != READING_COMPREHENSION) {
  78. echo Display::div(
  79. $questionDescription,
  80. array('class' => 'question_description')
  81. );
  82. }
  83. }
  84. if (in_array($answerType, array(FREE_ANSWER, ORAL_EXPRESSION)) &&
  85. $freeze
  86. ) {
  87. return '';
  88. }
  89. echo '<div class="question_options">';
  90. // construction of the Answer object (also gets all answers details)
  91. $objAnswerTmp = new Answer($questionId);
  92. $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
  93. $quiz_question_options = Question::readQuestionOption(
  94. $questionId,
  95. $course_id
  96. );
  97. // For "matching" type here, we need something a little bit special
  98. // because the match between the suggestions and the answers cannot be
  99. // done easily (suggestions and answers are in the same table), so we
  100. // have to go through answers first (elems with "correct" value to 0).
  101. $select_items = array();
  102. //This will contain the number of answers on the left side. We call them
  103. // suggestions here, for the sake of comprehensions, while the ones
  104. // on the right side are called answers
  105. $num_suggestions = 0;
  106. if (in_array($answerType, [MATCHING, DRAGGABLE, MATCHING_DRAGGABLE])) {
  107. if ($answerType == DRAGGABLE) {
  108. $isVertical = $objQuestionTmp->extra == 'v';
  109. $s .= '
  110. <div class="col-md-12 ui-widget ui-helper-clearfix">
  111. <div class="clearfix">
  112. <ul class="exercise-draggable-answer '.($isVertical ? '' : 'list-inline').'"
  113. id="question-'.$questionId.'" data-question="'.$questionId.'">
  114. ';
  115. } else {
  116. $s .= '<div id="drag'.$questionId.'_question" class="drag_question">
  117. <table class="data_table">';
  118. }
  119. // Iterate through answers
  120. $x = 1;
  121. //mark letters for each answer
  122. $letter = 'A';
  123. $answer_matching = array();
  124. $cpt1 = array();
  125. for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
  126. $answerCorrect = $objAnswerTmp->isCorrect($answerId);
  127. $numAnswer = $objAnswerTmp->selectAutoId($answerId);
  128. if ($answerCorrect == 0) {
  129. // options (A, B, C, ...) that will be put into the list-box
  130. // have the "correct" field set to 0 because they are answer
  131. $cpt1[$x] = $letter;
  132. $answer_matching[$x] = $objAnswerTmp->selectAnswerByAutoId(
  133. $numAnswer
  134. );
  135. $x++;
  136. $letter++;
  137. }
  138. }
  139. $i = 1;
  140. $select_items[0]['id'] = 0;
  141. $select_items[0]['letter'] = '--';
  142. $select_items[0]['answer'] = '';
  143. foreach ($answer_matching as $id => $value) {
  144. $select_items[$i]['id'] = $value['id_auto'];
  145. $select_items[$i]['letter'] = $cpt1[$id];
  146. $select_items[$i]['answer'] = $value['answer'];
  147. $i++;
  148. }
  149. $user_choice_array_position = array();
  150. if (!empty($user_choice)) {
  151. foreach ($user_choice as $item) {
  152. $user_choice_array_position[$item['position']] = $item['answer'];
  153. }
  154. }
  155. $num_suggestions = ($nbrAnswers - $x) + 1;
  156. } elseif ($answerType == FREE_ANSWER) {
  157. $fck_content = isset($user_choice[0]) && !empty($user_choice[0]['answer']) ? $user_choice[0]['answer'] : null;
  158. $form = new FormValidator('free_choice_'.$questionId);
  159. $config = array(
  160. 'ToolbarSet' => 'TestFreeAnswer'
  161. );
  162. $form->addHtmlEditor(
  163. "choice[".$questionId."]",
  164. null,
  165. false,
  166. false,
  167. $config
  168. );
  169. $form->setDefaults(
  170. array("choice[".$questionId."]" => $fck_content)
  171. );
  172. $s .= $form->returnForm();
  173. } elseif ($answerType == ORAL_EXPRESSION) {
  174. // Add nanog
  175. if (api_get_setting('enable_record_audio') == 'true') {
  176. //@todo pass this as a parameter
  177. global $exercise_stat_info, $exerciseId, $exe_id;
  178. if (!empty($exercise_stat_info)) {
  179. $objQuestionTmp->initFile(
  180. api_get_session_id(),
  181. api_get_user_id(),
  182. $exercise_stat_info['exe_exo_id'],
  183. $exercise_stat_info['exe_id']
  184. );
  185. } else {
  186. $objQuestionTmp->initFile(
  187. api_get_session_id(),
  188. api_get_user_id(),
  189. $exerciseId,
  190. 'temp_exe'
  191. );
  192. }
  193. echo $objQuestionTmp->returnRecorder();
  194. }
  195. $form = new FormValidator('free_choice_'.$questionId);
  196. $config = array(
  197. 'ToolbarSet' => 'TestFreeAnswer'
  198. );
  199. $form->addHtmlEditor(
  200. "choice[".$questionId."]",
  201. null,
  202. false,
  203. false,
  204. $config
  205. );
  206. $s .= $form->returnForm();
  207. }
  208. // Now navigate through the possible answers, using the max number of
  209. // answers for the question as a limiter
  210. $lines_count = 1; // a counter for matching-type answers
  211. if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE ||
  212. $answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE
  213. ) {
  214. $header = Display::tag('th', get_lang('Options'));
  215. foreach ($objQuestionTmp->options as $item) {
  216. if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
  217. if (in_array($item, $objQuestionTmp->options)) {
  218. $header .= Display::tag('th', get_lang($item));
  219. } else {
  220. $header .= Display::tag('th', $item);
  221. }
  222. } else {
  223. $header .= Display::tag('th', $item);
  224. }
  225. }
  226. if ($show_comment) {
  227. $header .= Display::tag('th', get_lang('Feedback'));
  228. }
  229. $s .= '<table class="table table-hover table-striped">';
  230. $s .= Display::tag(
  231. 'tr',
  232. $header,
  233. array('style' => 'text-align:left;')
  234. );
  235. }
  236. if ($show_comment) {
  237. if (
  238. in_array(
  239. $answerType,
  240. array(
  241. MULTIPLE_ANSWER,
  242. MULTIPLE_ANSWER_COMBINATION,
  243. UNIQUE_ANSWER,
  244. UNIQUE_ANSWER_IMAGE,
  245. UNIQUE_ANSWER_NO_OPTION,
  246. GLOBAL_MULTIPLE_ANSWER
  247. )
  248. )
  249. ) {
  250. $header = Display::tag('th', get_lang('Options'));
  251. if ($exercise_feedback == EXERCISE_FEEDBACK_TYPE_END) {
  252. $header .= Display::tag('th', get_lang('Feedback'));
  253. }
  254. $s .= '<table class="table table-hover table-striped">';
  255. $s .= Display::tag(
  256. 'tr',
  257. $header,
  258. array('style' => 'text-align:left;')
  259. );
  260. }
  261. }
  262. $matching_correct_answer = 0;
  263. $user_choice_array = array();
  264. if (!empty($user_choice)) {
  265. foreach ($user_choice as $item) {
  266. $user_choice_array[] = $item['answer'];
  267. }
  268. }
  269. $hidingClass = '';
  270. if ($answerType == READING_COMPREHENSION) {
  271. $objQuestionTmp->processText(
  272. $objQuestionTmp->selectDescription()
  273. );
  274. $hidingClass = 'hide-reading-answers';
  275. }
  276. if ($answerType == READING_COMPREHENSION) {
  277. $s .= Display::div(
  278. $objQuestionTmp->selectTitle(),
  279. ['class' => 'question_title '.$hidingClass]
  280. );
  281. }
  282. for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
  283. $answer = $objAnswerTmp->selectAnswer($answerId);
  284. $answerCorrect = $objAnswerTmp->isCorrect($answerId);
  285. $numAnswer = $objAnswerTmp->selectAutoId($answerId);
  286. $comment = $objAnswerTmp->selectComment($answerId);
  287. $attributes = array();
  288. switch ($answerType) {
  289. case UNIQUE_ANSWER:
  290. //no break
  291. case UNIQUE_ANSWER_NO_OPTION:
  292. //no break
  293. case UNIQUE_ANSWER_IMAGE:
  294. //no break
  295. case READING_COMPREHENSION:
  296. $input_id = 'choice-'.$questionId.'-'.$answerId;
  297. if (isset($user_choice[0]['answer']) && $user_choice[0]['answer'] == $numAnswer) {
  298. $attributes = array(
  299. 'id' => $input_id,
  300. 'checked' => 1,
  301. 'selected' => 1
  302. );
  303. } else {
  304. $attributes = array('id' => $input_id);
  305. }
  306. if ($debug_mark_answer) {
  307. if ($answerCorrect) {
  308. $attributes['checked'] = 1;
  309. $attributes['selected'] = 1;
  310. }
  311. }
  312. if ($show_comment) {
  313. $s .= '<tr><td>';
  314. }
  315. if ($answerType == UNIQUE_ANSWER_IMAGE) {
  316. if ($show_comment) {
  317. if (empty($comment)) {
  318. $s .= '<div id="answer'.$questionId.$numAnswer.'" '
  319. . 'class="exercise-unique-answer-image" style="text-align: center">';
  320. } else {
  321. $s .= '<div id="answer'.$questionId.$numAnswer.'" '
  322. . 'class="exercise-unique-answer-image col-xs-6 col-sm-12" style="text-align: center">';
  323. }
  324. } else {
  325. $s .= '<div id="answer'.$questionId.$numAnswer.'" '
  326. . 'class="exercise-unique-answer-image col-xs-6 col-md-3" style="text-align: center">';
  327. }
  328. }
  329. $answer = Security::remove_XSS($answer, STUDENT);
  330. $s .= Display::input(
  331. 'hidden',
  332. 'choice2['.$questionId.']',
  333. '0'
  334. );
  335. $answer_input = null;
  336. if ($answerType == UNIQUE_ANSWER_IMAGE) {
  337. $attributes['style'] = 'display: none;';
  338. $answer = '<div class="thumbnail">'.$answer.'</div>';
  339. }
  340. $answer_input .= '<label class="radio '.$hidingClass.'">';
  341. $answer_input .= Display::input(
  342. 'radio',
  343. 'choice['.$questionId.']',
  344. $numAnswer,
  345. $attributes
  346. );
  347. $answer_input .= $answer;
  348. $answer_input .= '</label>';
  349. if ($answerType == UNIQUE_ANSWER_IMAGE) {
  350. $answer_input .= "</div>";
  351. }
  352. if ($show_comment) {
  353. $s .= $answer_input;
  354. $s .= '</td>';
  355. $s .= '<td>';
  356. $s .= $comment;
  357. $s .= '</td>';
  358. $s .= '</tr>';
  359. } else {
  360. $s .= $answer_input;
  361. }
  362. break;
  363. case MULTIPLE_ANSWER:
  364. //no break
  365. case MULTIPLE_ANSWER_TRUE_FALSE:
  366. //no break
  367. case GLOBAL_MULTIPLE_ANSWER:
  368. $input_id = 'choice-'.$questionId.'-'.$answerId;
  369. $answer = Security::remove_XSS($answer, STUDENT);
  370. if (in_array($numAnswer, $user_choice_array)) {
  371. $attributes = array(
  372. 'id' => $input_id,
  373. 'checked' => 1,
  374. 'selected' => 1
  375. );
  376. } else {
  377. $attributes = array('id' => $input_id);
  378. }
  379. if ($debug_mark_answer) {
  380. if ($answerCorrect) {
  381. $attributes['checked'] = 1;
  382. $attributes['selected'] = 1;
  383. }
  384. }
  385. if ($answerType == MULTIPLE_ANSWER || $answerType == GLOBAL_MULTIPLE_ANSWER) {
  386. $s .= '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
  387. $answer_input = '<label class="checkbox">';
  388. $answer_input .= Display::input(
  389. 'checkbox',
  390. 'choice['.$questionId.']['.$numAnswer.']',
  391. $numAnswer,
  392. $attributes
  393. );
  394. $answer_input .= $answer;
  395. $answer_input .= '</label>';
  396. if ($show_comment) {
  397. $s .= '<tr><td>';
  398. $s .= $answer_input;
  399. $s .= '</td>';
  400. $s .= '<td>';
  401. $s .= $comment;
  402. $s .= '</td>';
  403. $s .= '</tr>';
  404. } else {
  405. $s .= $answer_input;
  406. }
  407. } elseif ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
  408. $my_choice = array();
  409. if (!empty($user_choice_array)) {
  410. foreach ($user_choice_array as $item) {
  411. $item = explode(':', $item);
  412. $my_choice[$item[0]] = $item[1];
  413. }
  414. }
  415. $s .= '<tr>';
  416. $s .= Display::tag('td', $answer);
  417. if (!empty($quiz_question_options)) {
  418. foreach ($quiz_question_options as $id => $item) {
  419. if (isset($my_choice[$numAnswer]) && $id == $my_choice[$numAnswer]) {
  420. $attributes = array(
  421. 'checked' => 1,
  422. 'selected' => 1
  423. );
  424. } else {
  425. $attributes = array();
  426. }
  427. if ($debug_mark_answer) {
  428. if ($id == $answerCorrect) {
  429. $attributes['checked'] = 1;
  430. $attributes['selected'] = 1;
  431. }
  432. }
  433. $s .= Display::tag(
  434. 'td',
  435. Display::input(
  436. 'radio',
  437. 'choice['.$questionId.']['.$numAnswer.']',
  438. $id,
  439. $attributes
  440. ),
  441. array('style' => '')
  442. );
  443. }
  444. }
  445. if ($show_comment) {
  446. $s .= '<td>';
  447. $s .= $comment;
  448. $s .= '</td>';
  449. }
  450. $s .= '</tr>';
  451. }
  452. break;
  453. case MULTIPLE_ANSWER_COMBINATION:
  454. // multiple answers
  455. $input_id = 'choice-'.$questionId.'-'.$answerId;
  456. if (in_array($numAnswer, $user_choice_array)) {
  457. $attributes = array(
  458. 'id' => $input_id,
  459. 'checked' => 1,
  460. 'selected' => 1
  461. );
  462. } else {
  463. $attributes = array('id' => $input_id);
  464. }
  465. if ($debug_mark_answer) {
  466. if ($answerCorrect) {
  467. $attributes['checked'] = 1;
  468. $attributes['selected'] = 1;
  469. }
  470. }
  471. $answer = Security::remove_XSS($answer, STUDENT);
  472. $answer_input = '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
  473. $answer_input .= '<label class="checkbox">';
  474. $answer_input .= Display::input(
  475. 'checkbox',
  476. 'choice['.$questionId.']['.$numAnswer.']',
  477. 1,
  478. $attributes
  479. );
  480. $answer_input .= $answer;
  481. $answer_input .= '</label>';
  482. if ($show_comment) {
  483. $s .= '<tr>';
  484. $s .= '<td>';
  485. $s .= $answer_input;
  486. $s .= '</td>';
  487. $s .= '<td>';
  488. $s .= $comment;
  489. $s .= '</td>';
  490. $s .= '</tr>';
  491. } else {
  492. $s .= $answer_input;
  493. }
  494. break;
  495. case MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE:
  496. $s .= '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
  497. $my_choice = array();
  498. if (!empty($user_choice_array)) {
  499. foreach ($user_choice_array as $item) {
  500. $item = explode(':', $item);
  501. if (isset($item[1]) && isset($item[0])) {
  502. $my_choice[$item[0]] = $item[1];
  503. }
  504. }
  505. }
  506. $answer = Security::remove_XSS($answer, STUDENT);
  507. $s .= '<tr>';
  508. $s .= Display::tag('td', $answer);
  509. foreach ($objQuestionTmp->options as $key => $item) {
  510. if (isset($my_choice[$numAnswer]) && $key == $my_choice[$numAnswer]) {
  511. $attributes = array(
  512. 'checked' => 1,
  513. 'selected' => 1
  514. );
  515. } else {
  516. $attributes = array();
  517. }
  518. if ($debug_mark_answer) {
  519. if ($key == $answerCorrect) {
  520. $attributes['checked'] = 1;
  521. $attributes['selected'] = 1;
  522. }
  523. }
  524. $s .= Display::tag(
  525. 'td',
  526. Display::input(
  527. 'radio',
  528. 'choice['.$questionId.']['.$numAnswer.']',
  529. $key,
  530. $attributes
  531. )
  532. );
  533. }
  534. if ($show_comment) {
  535. $s .= '<td>';
  536. $s .= $comment;
  537. $s .= '</td>';
  538. }
  539. $s .= '</tr>';
  540. break;
  541. case FILL_IN_BLANKS:
  542. // display the question, with field empty, for student to fill it,
  543. // or filled to display the answer in the Question preview of the exercise/admin.php page
  544. $displayForStudent = true;
  545. $listAnswerInfo = FillBlanks::getAnswerInfo($answer);
  546. list($answer) = explode('::', $answer);
  547. // Correct answers
  548. $correctAnswerList = $listAnswerInfo['tabwords'];
  549. // Student's answer
  550. $studentAnswerList = array();
  551. if (isset($user_choice[0]['answer'])) {
  552. $arrayStudentAnswer = FillBlanks::getAnswerInfo($user_choice[0]['answer'], true);
  553. $studentAnswerList = $arrayStudentAnswer['studentanswer'];
  554. }
  555. // If the question must be shown with the answer (in page exercise/admin.php) for teacher preview
  556. // set the student-answer to the correct answer
  557. if ($debug_mark_answer) {
  558. $studentAnswerList = $correctAnswerList;
  559. $displayForStudent = false;
  560. }
  561. if (!empty($correctAnswerList) && !empty($studentAnswerList)) {
  562. $answer = '';
  563. for ($i = 0; $i < count($listAnswerInfo['commonwords']) - 1; $i++) {
  564. // display the common word
  565. $answer .= $listAnswerInfo['commonwords'][$i];
  566. // display the blank word
  567. $correctItem = $listAnswerInfo['tabwords'][$i];
  568. if (isset($studentAnswerList[$i])) {
  569. // If student already started this test and answered this question,
  570. // fill the blank with his previous answers
  571. // may be "" if student viewed the question, but did not fill the blanks
  572. $correctItem = $studentAnswerList[$i];
  573. }
  574. $attributes['style'] = "width:".$listAnswerInfo['tabinputsize'][$i]."px";
  575. $answer .= FillBlanks::getFillTheBlankHtml(
  576. $current_item,
  577. $questionId,
  578. $correctItem,
  579. $attributes,
  580. $answer,
  581. $listAnswerInfo,
  582. $displayForStudent,
  583. $i
  584. );
  585. }
  586. // display the last common word
  587. $answer .= $listAnswerInfo['commonwords'][$i];
  588. } else {
  589. // display empty [input] with the right width for student to fill it
  590. $answer = '';
  591. for ($i = 0; $i < count($listAnswerInfo['commonwords']) - 1; $i++) {
  592. // display the common words
  593. $answer .= $listAnswerInfo['commonwords'][$i];
  594. // display the blank word
  595. $attributes["style"] = "width:".$listAnswerInfo['tabinputsize'][$i]."px";
  596. $answer .= FillBlanks::getFillTheBlankHtml(
  597. $current_item,
  598. $questionId,
  599. '',
  600. $attributes,
  601. $answer,
  602. $listAnswerInfo,
  603. $displayForStudent,
  604. $i
  605. );
  606. }
  607. // display the last common word
  608. $answer .= $listAnswerInfo['commonwords'][$i];
  609. }
  610. $s .= $answer;
  611. break;
  612. case CALCULATED_ANSWER:
  613. /*
  614. * In the CALCULATED_ANSWER test
  615. * you mustn't have [ and ] in the textarea
  616. * you mustn't have @@ in the textarea
  617. * the text to find mustn't be empty or contains only spaces
  618. * the text to find mustn't contains HTML tags
  619. * the text to find mustn't contains char "
  620. */
  621. if ($origin !== null) {
  622. global $exe_id;
  623. $trackAttempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  624. $sql = 'SELECT answer FROM '.$trackAttempts.'
  625. WHERE exe_id=' . $exe_id.' AND question_id='.$questionId;
  626. $rsLastAttempt = Database::query($sql);
  627. $rowLastAttempt = Database::fetch_array($rsLastAttempt);
  628. $answer = $rowLastAttempt['answer'];
  629. if (empty($answer)) {
  630. $_SESSION['calculatedAnswerId'][$questionId] = mt_rand(
  631. 1,
  632. $nbrAnswers
  633. );
  634. $answer = $objAnswerTmp->selectAnswer(
  635. $_SESSION['calculatedAnswerId'][$questionId]
  636. );
  637. }
  638. }
  639. list($answer) = explode('@@', $answer);
  640. // $correctAnswerList array of array with correct anwsers 0=> [0=>[\p] 1=>[plop]]
  641. api_preg_match_all(
  642. '/\[[^]]+\]/',
  643. $answer,
  644. $correctAnswerList
  645. );
  646. // get student answer to display it if student go back to previous calculated answer question in a test
  647. if (isset($user_choice[0]['answer'])) {
  648. api_preg_match_all(
  649. '/\[[^]]+\]/',
  650. $answer,
  651. $studentAnswerList
  652. );
  653. $studentAnswerListTobecleaned = $studentAnswerList[0];
  654. $studentAnswerList = array();
  655. for ($i = 0; $i < count(
  656. $studentAnswerListTobecleaned
  657. ); $i++) {
  658. $answerCorrected = $studentAnswerListTobecleaned[$i];
  659. $answerCorrected = api_preg_replace(
  660. '| / <font color="green"><b>.*$|',
  661. '',
  662. $answerCorrected
  663. );
  664. $answerCorrected = api_preg_replace(
  665. '/^\[/',
  666. '',
  667. $answerCorrected
  668. );
  669. $answerCorrected = api_preg_replace(
  670. '|^<font color="red"><s>|',
  671. '',
  672. $answerCorrected
  673. );
  674. $answerCorrected = api_preg_replace(
  675. '|</s></font>$|',
  676. '',
  677. $answerCorrected
  678. );
  679. $answerCorrected = '['.$answerCorrected.']';
  680. $studentAnswerList[] = $answerCorrected;
  681. }
  682. }
  683. // If display preview of answer in test view for exemple, set the student answer to the correct answers
  684. if ($debug_mark_answer) {
  685. // contain the rights answers surronded with brackets
  686. $studentAnswerList = $correctAnswerList[0];
  687. }
  688. /*
  689. Split the response by bracket
  690. tabComments is an array with text surrounding the text to find
  691. we add a space before and after the answerQuestion to be sure to
  692. have a block of text before and after [xxx] patterns
  693. so we have n text to find ([xxx]) and n+1 block of texts before,
  694. between and after the text to find
  695. */
  696. $tabComments = api_preg_split(
  697. '/\[[^]]+\]/',
  698. ' '.$answer.' '
  699. );
  700. if (!empty($correctAnswerList) && !empty($studentAnswerList)) {
  701. $answer = "";
  702. $i = 0;
  703. foreach ($studentAnswerList as $studentItem) {
  704. // remove surronding brackets
  705. $studentResponse = api_substr(
  706. $studentItem,
  707. 1,
  708. api_strlen($studentItem) - 2
  709. );
  710. $size = strlen($studentItem);
  711. $attributes['class'] = self::detectInputAppropriateClass(
  712. $size
  713. );
  714. $answer .= $tabComments[$i].
  715. Display::input(
  716. 'text',
  717. "choice[$questionId][]",
  718. $studentResponse,
  719. $attributes
  720. );
  721. $i++;
  722. }
  723. $answer .= $tabComments[$i];
  724. } else {
  725. // display exercise with empty input fields
  726. // every [xxx] are replaced with an empty input field
  727. foreach ($correctAnswerList[0] as $item) {
  728. $size = strlen($item);
  729. $attributes['class'] = self::detectInputAppropriateClass(
  730. $size
  731. );
  732. $answer = str_replace(
  733. $item,
  734. Display::input(
  735. 'text',
  736. "choice[$questionId][]",
  737. '',
  738. $attributes
  739. ),
  740. $answer
  741. );
  742. }
  743. }
  744. if ($origin !== null) {
  745. $s = $answer;
  746. break;
  747. } else {
  748. $s .= $answer;
  749. }
  750. break;
  751. case MATCHING:
  752. // matching type, showing suggestions and answers
  753. // TODO: replace $answerId by $numAnswer
  754. if ($answerCorrect != 0) {
  755. // only show elements to be answered (not the contents of
  756. // the select boxes, who are correct = 0)
  757. $s .= '<tr><td width="45%" valign="top">';
  758. $parsed_answer = $answer;
  759. // Left part questions
  760. $s .= '<p class="indent">'.$lines_count.'.&nbsp;'.$parsed_answer.'</p></td>';
  761. // Middle part (matches selects)
  762. // Id of select is # question + # of option
  763. $s .= '<td width="10%" valign="top" align="center">
  764. <div class="select-matching">
  765. <select id="choice_id_'.$current_item.'_'.$lines_count.'" name="choice['.$questionId.']['.$numAnswer.']">';
  766. // fills the list-box
  767. foreach ($select_items as $key => $val) {
  768. // set $debug_mark_answer to true at function start to
  769. // show the correct answer with a suffix '-x'
  770. $selected = '';
  771. if ($debug_mark_answer) {
  772. if ($val['id'] == $answerCorrect) {
  773. $selected = 'selected="selected"';
  774. }
  775. }
  776. //$user_choice_array_position
  777. if (isset($user_choice_array_position[$numAnswer]) && $val['id'] == $user_choice_array_position[$numAnswer]) {
  778. $selected = 'selected="selected"';
  779. }
  780. $s .= '<option value="'.$val['id'].'" '.$selected.'>'.$val['letter'].'</option>';
  781. } // end foreach()
  782. $s .= '</select></div></td><td width="5%" class="separate">&nbsp;</td>';
  783. $s .= '<td width="40%" valign="top" >';
  784. if (isset($select_items[$lines_count])) {
  785. $s .= '<div class="text-right"><p class="indent">'.$select_items[$lines_count]['letter'].'.&nbsp; '.$select_items[$lines_count]['answer'].'</p></div>';
  786. } else {
  787. $s .= '&nbsp;';
  788. }
  789. $s .= '</td>';
  790. $s .= '</tr>';
  791. $lines_count++;
  792. //if the left side of the "matching" has been completely
  793. // shown but the right side still has values to show...
  794. if (($lines_count - 1) == $num_suggestions) {
  795. // if it remains answers to shown at the right side
  796. while (isset($select_items[$lines_count])) {
  797. $s .= '<tr>
  798. <td colspan="2"></td>
  799. <td valign="top">';
  800. $s .= '<b>'.$select_items[$lines_count]['letter'].'.</b> '.$select_items[$lines_count]['answer'];
  801. $s .= "</td>
  802. </tr>";
  803. $lines_count++;
  804. } // end while()
  805. } // end if()
  806. $matching_correct_answer++;
  807. }
  808. break;
  809. case DRAGGABLE:
  810. if ($answerCorrect) {
  811. $parsed_answer = $answer;
  812. /*$lines_count = '';
  813. $data = $objAnswerTmp->getAnswerByAutoId($numAnswer);
  814. $data = $objAnswerTmp->getAnswerByAutoId($data['correct']);
  815. $lines_count = $data['answer'];*/
  816. $windowId = $questionId.'_'.$lines_count;
  817. $s .= '<li class="touch-items" id="'.$windowId.'">';
  818. $s .= Display::div(
  819. $parsed_answer,
  820. [
  821. 'id' => "window_$windowId",
  822. 'class' => "window{$questionId}_question_draggable exercise-draggable-answer-option"
  823. ]
  824. );
  825. $draggableSelectOptions = [];
  826. $selectedValue = 0;
  827. $selectedIndex = 0;
  828. if ($user_choice) {
  829. foreach ($user_choice as $chosen) {
  830. if ($answerCorrect != $chosen['answer']) {
  831. continue;
  832. }
  833. $selectedValue = $chosen['answer'];
  834. }
  835. }
  836. foreach ($select_items as $key => $select_item) {
  837. $draggableSelectOptions[$select_item['id']] = $select_item['letter'];
  838. }
  839. foreach ($draggableSelectOptions as $value => $text) {
  840. if ($value == $selectedValue) {
  841. break;
  842. }
  843. $selectedIndex++;
  844. }
  845. $s .= Display::select(
  846. "choice[$questionId][$numAnswer]",
  847. $draggableSelectOptions,
  848. $selectedValue,
  849. [
  850. 'id' => "window_{$windowId}_select",
  851. 'class' => 'select_option hidden',
  852. ],
  853. false
  854. );
  855. if ($selectedValue && $selectedIndex) {
  856. $s .= "
  857. <script>
  858. $(function() {
  859. DraggableAnswer.deleteItem(
  860. $('#{$questionId}_$lines_count'),
  861. $('#drop_{$questionId}_{$selectedIndex}')
  862. );
  863. });
  864. </script>
  865. ";
  866. }
  867. if (isset($select_items[$lines_count])) {
  868. $s .= Display::div(
  869. Display::tag(
  870. 'b',
  871. $select_items[$lines_count]['letter']
  872. ).$select_items[$lines_count]['answer'],
  873. [
  874. 'id' => "window_{$windowId}_answer",
  875. 'class' => 'hidden'
  876. ]
  877. );
  878. } else {
  879. $s .= '&nbsp;';
  880. }
  881. $lines_count++;
  882. if (($lines_count - 1) == $num_suggestions) {
  883. while (isset($select_items[$lines_count])) {
  884. $s .= Display::tag('b', $select_items[$lines_count]['letter']);
  885. $s .= $select_items[$lines_count]['answer'];
  886. $lines_count++;
  887. }
  888. }
  889. $matching_correct_answer++;
  890. $s .= '</li>';
  891. }
  892. break;
  893. case MATCHING_DRAGGABLE:
  894. if ($answerId == 1) {
  895. echo $objAnswerTmp->getJs();
  896. }
  897. if ($answerCorrect != 0) {
  898. $parsed_answer = $answer;
  899. $windowId = "{$questionId}_{$lines_count}";
  900. $s .= <<<HTML
  901. <tr>
  902. <td widht="45%">
  903. <div id="window_{$windowId}" class="window window_left_question window{$questionId}_question">
  904. <strong>$lines_count.</strong> $parsed_answer
  905. </div>
  906. </td>
  907. <td width="10%">
  908. HTML;
  909. $draggableSelectOptions = [];
  910. $selectedValue = 0;
  911. $selectedIndex = 0;
  912. if ($user_choice) {
  913. foreach ($user_choice as $chosen) {
  914. if ($answerCorrect != $chosen['answer']) {
  915. continue;
  916. }
  917. $selectedValue = $chosen['answer'];
  918. }
  919. }
  920. foreach ($select_items as $key => $select_item) {
  921. $draggableSelectOptions[$select_item['id']] = $select_item['letter'];
  922. }
  923. foreach ($draggableSelectOptions as $value => $text) {
  924. if ($value == $selectedValue) {
  925. break;
  926. }
  927. $selectedIndex++;
  928. }
  929. $s .= Display::select(
  930. "choice[$questionId][$numAnswer]",
  931. $draggableSelectOptions,
  932. $selectedValue,
  933. [
  934. 'id' => "window_{$windowId}_select",
  935. 'class' => 'hidden'
  936. ],
  937. false
  938. );
  939. if (!empty($answerCorrect) && !empty($selectedValue)) {
  940. // Show connect if is not freeze (question preview)
  941. if (!$freeze) {
  942. $s .= "
  943. <script>
  944. $(document).on('ready', function () {
  945. jsPlumb.ready(function() {
  946. jsPlumb.connect({
  947. source: 'window_$windowId',
  948. target: 'window_{$questionId}_{$selectedIndex}_answer',
  949. endpoint: ['Blank', {radius: 15}],
  950. anchors: ['RightMiddle', 'LeftMiddle'],
  951. paintStyle: {strokeStyle: '#8A8888', lineWidth: 8},
  952. connector: [
  953. MatchingDraggable.connectorType,
  954. {curvines: MatchingDraggable.curviness}
  955. ]
  956. });
  957. });
  958. });
  959. </script>
  960. ";
  961. }
  962. }
  963. $s .= <<<HTML
  964. </td>
  965. <td width="45%">
  966. HTML;
  967. if (isset($select_items[$lines_count])) {
  968. $s .= <<<HTML
  969. <div id="window_{$windowId}_answer" class="window window_right_question">
  970. <strong>{$select_items[$lines_count]['letter']}.</strong> {$select_items[$lines_count]['answer']}
  971. </div>
  972. HTML;
  973. } else {
  974. $s .= '&nbsp;';
  975. }
  976. $s .= '</td></tr>';
  977. $lines_count++;
  978. if (($lines_count - 1) == $num_suggestions) {
  979. while (isset($select_items[$lines_count])) {
  980. $s .= <<<HTML
  981. <tr>
  982. <td colspan="2"></td>
  983. <td>
  984. <strong>{$select_items[$lines_count]['letter']}</strong>
  985. {$select_items[$lines_count]['answer']}
  986. </td>
  987. </tr>
  988. HTML;
  989. $lines_count++;
  990. }
  991. }
  992. $matching_correct_answer++;
  993. }
  994. break;
  995. }
  996. } // end for()
  997. if ($show_comment) {
  998. $s .= '</table>';
  999. } elseif (in_array(
  1000. $answerType,
  1001. [
  1002. MATCHING,
  1003. MATCHING_DRAGGABLE,
  1004. UNIQUE_ANSWER_NO_OPTION,
  1005. MULTIPLE_ANSWER_TRUE_FALSE,
  1006. MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE,
  1007. ]
  1008. )) {
  1009. $s .= '</table>';
  1010. }
  1011. if ($answerType == DRAGGABLE) {
  1012. $isVertical = $objQuestionTmp->extra == 'v';
  1013. $s .= "</ul>";
  1014. $s .= "</div>"; //clearfix
  1015. $counterAnswer = 1;
  1016. $s .= $isVertical ? '' : '<div class="row">';
  1017. for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
  1018. $answerCorrect = $objAnswerTmp->isCorrect($answerId);
  1019. $windowId = $questionId.'_'.$counterAnswer;
  1020. if ($answerCorrect) {
  1021. $s .= $isVertical ? '<div class="row">' : '';
  1022. $s .= '
  1023. <div class="'.($isVertical ? 'col-md-12' : 'col-xs-12 col-sm-4 col-md-3 col-lg-2').'">
  1024. <div id="drop_'.$windowId.'" class="droppable">&nbsp;</div>
  1025. </div>
  1026. ';
  1027. $s .= $isVertical ? '</div>' : '';
  1028. $counterAnswer++;
  1029. }
  1030. }
  1031. $s .= $isVertical ? '' : '</div>'; // row
  1032. $s .= '</div>'; // col-md-12 ui-widget ui-helper-clearfix
  1033. }
  1034. if (in_array($answerType, [MATCHING, MATCHING_DRAGGABLE])) {
  1035. $s .= '</div>'; //drag_question
  1036. }
  1037. $s .= '</div>'; //question_options row
  1038. // destruction of the Answer object
  1039. unset($objAnswerTmp);
  1040. // destruction of the Question object
  1041. unset($objQuestionTmp);
  1042. if ($origin == 'export') {
  1043. return $s;
  1044. }
  1045. echo $s;
  1046. } elseif ($answerType == HOT_SPOT || $answerType == HOT_SPOT_DELINEATION) {
  1047. global $exerciseId, $exe_id;
  1048. // Question is a HOT_SPOT
  1049. //checking document/images visibility
  1050. if (api_is_platform_admin() || api_is_course_admin()) {
  1051. $doc_id = $objQuestionTmp->getPictureId();
  1052. if (is_numeric($doc_id)) {
  1053. $images_folder_visibility = api_get_item_visibility(
  1054. $course,
  1055. 'document',
  1056. $doc_id,
  1057. api_get_session_id()
  1058. );
  1059. if (!$images_folder_visibility) {
  1060. //This message is shown only to the course/platform admin if the image is set to visibility = false
  1061. echo Display::return_message(
  1062. get_lang('ChangeTheVisibilityOfTheCurrentImage'),
  1063. 'warning'
  1064. );
  1065. }
  1066. }
  1067. }
  1068. $questionName = $objQuestionTmp->selectTitle();
  1069. $questionDescription = $objQuestionTmp->selectDescription();
  1070. // Get the answers, make a list
  1071. $objAnswerTmp = new Answer($questionId);
  1072. $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
  1073. // get answers of hotpost
  1074. $answers_hotspot = array();
  1075. for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
  1076. $answers = $objAnswerTmp->selectAnswerByAutoId(
  1077. $objAnswerTmp->selectAutoId($answerId)
  1078. );
  1079. $answers_hotspot[$answers['id']] = $objAnswerTmp->selectAnswer(
  1080. $answerId
  1081. );
  1082. }
  1083. $answerList = '';
  1084. $hotspotColor = 0;
  1085. if ($answerType != HOT_SPOT_DELINEATION) {
  1086. $answerList = '
  1087. <div class="well well-sm">
  1088. <h5 class="page-header">' . get_lang('HotspotZones').'</h5>
  1089. <ol>
  1090. ';
  1091. if (!empty($answers_hotspot)) {
  1092. Session::write("hotspot_ordered$questionId", array_keys($answers_hotspot));
  1093. foreach ($answers_hotspot as $value) {
  1094. $answerList .= "<li>";
  1095. if ($freeze) {
  1096. $answerList .= '<span class="hotspot-color-'.$hotspotColor
  1097. .' fa fa-square" aria-hidden="true"></span>'.PHP_EOL;
  1098. }
  1099. $answerList .= $value;
  1100. $answerList .= "</li>";
  1101. $hotspotColor++;
  1102. }
  1103. }
  1104. $answerList .= '
  1105. </ul>
  1106. </div>
  1107. ';
  1108. if ($freeze) {
  1109. $relPath = api_get_path(WEB_CODE_PATH);
  1110. echo "
  1111. <div class=\"row\">
  1112. <div class=\"col-sm-9\">
  1113. <div id=\"hotspot-preview-$questionId\"></div>
  1114. </div>
  1115. <div class=\"col-sm-3\">
  1116. $answerList
  1117. </div>
  1118. </div>
  1119. <script>
  1120. new ".($answerType == HOT_SPOT ? "HotspotQuestion" : "DelineationQuestion")."({
  1121. questionId: $questionId,
  1122. exerciseId: $exerciseId,
  1123. selector: '#hotspot-preview-$questionId',
  1124. for: 'preview',
  1125. relPath: '$relPath'
  1126. });
  1127. </script>
  1128. ";
  1129. return;
  1130. }
  1131. }
  1132. if (!$only_questions) {
  1133. if ($show_title) {
  1134. TestCategory::displayCategoryAndTitle($objQuestionTmp->id);
  1135. echo $objQuestionTmp->getTitleToDisplay($current_item);
  1136. }
  1137. //@todo I need to the get the feedback type
  1138. echo <<<HOTSPOT
  1139. <input type="hidden" name="hidden_hotspot_id" value="$questionId" />
  1140. <div class="exercise_questions">
  1141. $questionDescription
  1142. <div class="row">
  1143. HOTSPOT;
  1144. }
  1145. $relPath = api_get_path(WEB_CODE_PATH);
  1146. $s .= "
  1147. <div class=\"col-sm-8 col-md-9\">
  1148. <div class=\"hotspot-image\"></div>
  1149. <script>
  1150. $(document).on('ready', function () {
  1151. new " . ($answerType == HOT_SPOT_DELINEATION ? 'DelineationQuestion' : 'HotspotQuestion')."({
  1152. questionId: $questionId,
  1153. exerciseId: $exe_id,
  1154. selector: '#question_div_' + $questionId + ' .hotspot-image',
  1155. for: 'user',
  1156. relPath: '$relPath'
  1157. });
  1158. });
  1159. </script>
  1160. </div>
  1161. <div class=\"col-sm-4 col-md-3\">
  1162. $answerList
  1163. </div>
  1164. ";
  1165. echo <<<HOTSPOT
  1166. $s
  1167. </div>
  1168. </div>
  1169. HOTSPOT;
  1170. } elseif ($answerType == ANNOTATION) {
  1171. global $exe_id;
  1172. $relPath = api_get_path(WEB_CODE_PATH);
  1173. if (api_is_platform_admin() || api_is_course_admin()) {
  1174. $docId = DocumentManager::get_document_id($course, '/images/'.$pictureName);
  1175. if ($docId) {
  1176. $images_folder_visibility = api_get_item_visibility(
  1177. $course,
  1178. 'document',
  1179. $docId,
  1180. api_get_session_id()
  1181. );
  1182. if (!$images_folder_visibility) {
  1183. echo Display::return_message(get_lang('ChangeTheVisibilityOfTheCurrentImage'), 'warning');
  1184. }
  1185. }
  1186. if ($freeze) {
  1187. echo Display::img(
  1188. api_get_path(WEB_COURSE_PATH).$course['path'].'/document/images/'.$pictureName,
  1189. $objQuestionTmp->selectTitle(),
  1190. ['width' => '600px']
  1191. );
  1192. return 0;
  1193. }
  1194. }
  1195. if (!$only_questions) {
  1196. if ($show_title) {
  1197. TestCategory::displayCategoryAndTitle($objQuestionTmp->id);
  1198. echo $objQuestionTmp->getTitleToDisplay($current_item);
  1199. }
  1200. echo '
  1201. <input type="hidden" name="hidden_hotspot_id" value="'.$questionId.'" />
  1202. <div class="exercise_questions">
  1203. '.$objQuestionTmp->selectDescription().'
  1204. <div class="row">
  1205. <div class="col-sm-8 col-md-9">
  1206. <div id="annotation-canvas-'.$questionId.'" class="annotation-canvas center-block">
  1207. </div>
  1208. <script>
  1209. AnnotationQuestion({
  1210. questionId: '.$questionId.',
  1211. exerciseId: '.$exe_id.',
  1212. relPath: \''.$relPath.'\'
  1213. });
  1214. </script>
  1215. </div>
  1216. <div class="col-sm-4 col-md-3">
  1217. <div class="well well-sm" id="annotation-toolbar-'.$questionId.'">
  1218. <div class="btn-toolbar">
  1219. <div class="btn-group" data-toggle="buttons">
  1220. <label class="btn btn-default active"
  1221. aria-label="'.get_lang('AddAnnotationPath').'">
  1222. <input type="radio" value="0" name="'.$questionId.'-options" autocomplete="off" checked>
  1223. <span class="fa fa-pencil" aria-hidden="true"></span>
  1224. </label>
  1225. <label class="btn btn-default"
  1226. aria-label="'.get_lang('AddAnnotationText').'">
  1227. <input type="radio" value="1" name="'.$questionId.'-options" autocomplete="off">
  1228. <span class="fa fa-font fa-fw" aria-hidden="true"></span>
  1229. </label>
  1230. </div>
  1231. </div>
  1232. <ul class="list-unstyled"></ul>
  1233. </div>
  1234. </div>
  1235. </div>
  1236. </div>
  1237. ';
  1238. }
  1239. $objAnswerTmp = new Answer($questionId);
  1240. $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
  1241. unset($objAnswerTmp, $objQuestionTmp);
  1242. }
  1243. return $nbrAnswers;
  1244. }
  1245. /**
  1246. * @param int $exe_id
  1247. * @return array
  1248. */
  1249. public static function get_exercise_track_exercise_info($exe_id)
  1250. {
  1251. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  1252. $TBL_TRACK_EXERCICES = Database::get_main_table(
  1253. TABLE_STATISTIC_TRACK_E_EXERCISES
  1254. );
  1255. $TBL_COURSE = Database::get_main_table(TABLE_MAIN_COURSE);
  1256. $exe_id = intval($exe_id);
  1257. $result = array();
  1258. if (!empty($exe_id)) {
  1259. $sql = " SELECT q.*, tee.*
  1260. FROM $TBL_EXERCICES as q
  1261. INNER JOIN $TBL_TRACK_EXERCICES as tee
  1262. ON q.id = tee.exe_exo_id
  1263. INNER JOIN $TBL_COURSE c
  1264. ON c.id = tee.c_id
  1265. WHERE tee.exe_id = $exe_id
  1266. AND q.c_id = c.id";
  1267. $res_fb_type = Database::query($sql);
  1268. $result = Database::fetch_array($res_fb_type, 'ASSOC');
  1269. }
  1270. return $result;
  1271. }
  1272. /**
  1273. * Validates the time control key
  1274. * @param int $exercise_id
  1275. * @param int $lp_id
  1276. * @param int $lp_item_id
  1277. * @return bool
  1278. */
  1279. public static function exercise_time_control_is_valid(
  1280. $exercise_id,
  1281. $lp_id = 0,
  1282. $lp_item_id = 0
  1283. ) {
  1284. $course_id = api_get_course_int_id();
  1285. $exercise_id = intval($exercise_id);
  1286. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  1287. $sql = "SELECT expired_time FROM $TBL_EXERCICES
  1288. WHERE c_id = $course_id AND id = $exercise_id";
  1289. $result = Database::query($sql);
  1290. $row = Database::fetch_array($result, 'ASSOC');
  1291. if (!empty($row['expired_time'])) {
  1292. $current_expired_time_key = self::get_time_control_key(
  1293. $exercise_id,
  1294. $lp_id,
  1295. $lp_item_id
  1296. );
  1297. if (isset($_SESSION['expired_time'][$current_expired_time_key])) {
  1298. $current_time = time();
  1299. $expired_time = api_strtotime(
  1300. $_SESSION['expired_time'][$current_expired_time_key],
  1301. 'UTC'
  1302. );
  1303. $total_time_allowed = $expired_time + 30;
  1304. if ($total_time_allowed < $current_time) {
  1305. return false;
  1306. }
  1307. return true;
  1308. } else {
  1309. return false;
  1310. }
  1311. } else {
  1312. return true;
  1313. }
  1314. }
  1315. /**
  1316. * Deletes the time control token
  1317. *
  1318. * @param int $exercise_id
  1319. * @param int $lp_id
  1320. * @param int $lp_item_id
  1321. */
  1322. public static function exercise_time_control_delete(
  1323. $exercise_id,
  1324. $lp_id = 0,
  1325. $lp_item_id = 0
  1326. ) {
  1327. $current_expired_time_key = self::get_time_control_key(
  1328. $exercise_id,
  1329. $lp_id,
  1330. $lp_item_id
  1331. );
  1332. unset($_SESSION['expired_time'][$current_expired_time_key]);
  1333. }
  1334. /**
  1335. * Generates the time control key
  1336. */
  1337. public static function get_time_control_key(
  1338. $exercise_id,
  1339. $lp_id = 0,
  1340. $lp_item_id = 0
  1341. ) {
  1342. $exercise_id = intval($exercise_id);
  1343. $lp_id = intval($lp_id);
  1344. $lp_item_id = intval($lp_item_id);
  1345. return
  1346. api_get_course_int_id().'_'.
  1347. api_get_session_id().'_'.
  1348. $exercise_id.'_'.
  1349. api_get_user_id().'_'.
  1350. $lp_id.'_'.
  1351. $lp_item_id;
  1352. }
  1353. /**
  1354. * Get session time control
  1355. *
  1356. * @param int $exercise_id
  1357. * @param int $lp_id
  1358. * @param int $lp_item_id
  1359. * @return int
  1360. */
  1361. public static function get_session_time_control_key(
  1362. $exercise_id,
  1363. $lp_id = 0,
  1364. $lp_item_id = 0
  1365. ) {
  1366. $return_value = 0;
  1367. $time_control_key = self::get_time_control_key(
  1368. $exercise_id,
  1369. $lp_id,
  1370. $lp_item_id
  1371. );
  1372. if (isset($_SESSION['expired_time']) && isset($_SESSION['expired_time'][$time_control_key])) {
  1373. $return_value = $_SESSION['expired_time'][$time_control_key];
  1374. }
  1375. return $return_value;
  1376. }
  1377. /**
  1378. * Gets count of exam results
  1379. * @todo this function should be moved in a library + no global calls
  1380. */
  1381. public static function get_count_exam_results($exercise_id, $extra_where_conditions)
  1382. {
  1383. $count = self::get_exam_results_data(
  1384. null,
  1385. null,
  1386. null,
  1387. null,
  1388. $exercise_id,
  1389. $extra_where_conditions,
  1390. true
  1391. );
  1392. return $count;
  1393. }
  1394. /**
  1395. * @param string $in_hotpot_path
  1396. * @return int
  1397. */
  1398. public static function get_count_exam_hotpotatoes_results($in_hotpot_path)
  1399. {
  1400. return self::get_exam_results_hotpotatoes_data(
  1401. 0,
  1402. 0,
  1403. '',
  1404. '',
  1405. $in_hotpot_path,
  1406. true,
  1407. ''
  1408. );
  1409. }
  1410. /**
  1411. * @param int $in_from
  1412. * @param int $in_number_of_items
  1413. * @param int $in_column
  1414. * @param int $in_direction
  1415. * @param string $in_hotpot_path
  1416. * @param bool $in_get_count
  1417. * @param null $where_condition
  1418. * @return array|int
  1419. */
  1420. public static function get_exam_results_hotpotatoes_data(
  1421. $in_from,
  1422. $in_number_of_items,
  1423. $in_column,
  1424. $in_direction,
  1425. $in_hotpot_path,
  1426. $in_get_count = false,
  1427. $where_condition = null
  1428. ) {
  1429. $courseId = api_get_course_int_id();
  1430. // by default in_column = 1 If parameters given, it is the name of the column witch is the bdd field name
  1431. if ($in_column == 1) {
  1432. $in_column = 'firstname';
  1433. }
  1434. $in_hotpot_path = Database::escape_string($in_hotpot_path);
  1435. $in_direction = Database::escape_string($in_direction);
  1436. $in_column = Database::escape_string($in_column);
  1437. $in_number_of_items = intval($in_number_of_items);
  1438. $in_from = intval($in_from);
  1439. $TBL_TRACK_HOTPOTATOES = Database::get_main_table(
  1440. TABLE_STATISTIC_TRACK_E_HOTPOTATOES
  1441. );
  1442. $TBL_USER = Database::get_main_table(TABLE_MAIN_USER);
  1443. $sql = "SELECT * FROM $TBL_TRACK_HOTPOTATOES thp
  1444. JOIN $TBL_USER u ON thp.exe_user_id = u.user_id
  1445. WHERE thp.c_id = $courseId AND exe_name LIKE '$in_hotpot_path%'";
  1446. // just count how many answers
  1447. if ($in_get_count) {
  1448. $res = Database::query($sql);
  1449. return Database::num_rows($res);
  1450. }
  1451. // get a number of sorted results
  1452. $sql .= " $where_condition
  1453. ORDER BY $in_column $in_direction
  1454. LIMIT $in_from, $in_number_of_items";
  1455. $res = Database::query($sql);
  1456. $result = array();
  1457. $apiIsAllowedToEdit = api_is_allowed_to_edit();
  1458. $urlBase = api_get_path(WEB_CODE_PATH).
  1459. 'exercise/hotpotatoes_exercise_report.php?action=delete&'.
  1460. api_get_cidreq().'&id=';
  1461. while ($data = Database::fetch_array($res)) {
  1462. $actions = null;
  1463. if ($apiIsAllowedToEdit) {
  1464. $url = $urlBase.$data['id'].'&path='.$data['exe_name'];
  1465. $actions = Display::url(
  1466. Display::return_icon('delete.png', get_lang('Delete')),
  1467. $url
  1468. );
  1469. }
  1470. $result[] = array(
  1471. 'firstname' => $data['firstname'],
  1472. 'lastname' => $data['lastname'],
  1473. 'username' => $data['username'],
  1474. 'group_name' => implode(
  1475. "<br/>",
  1476. GroupManager::get_user_group_name($data['user_id'])
  1477. ),
  1478. 'exe_date' => $data['exe_date'],
  1479. 'score' => $data['exe_result'].' / '.$data['exe_weighting'],
  1480. 'actions' => $actions,
  1481. );
  1482. }
  1483. return $result;
  1484. }
  1485. /**
  1486. * @param string $exercisePath
  1487. * @param int $userId
  1488. * @param int $courseId
  1489. * @param int $sessionId
  1490. *
  1491. * @return array
  1492. */
  1493. public static function getLatestHotPotatoResult(
  1494. $exercisePath,
  1495. $userId,
  1496. $courseId,
  1497. $sessionId
  1498. ) {
  1499. $table = Database::get_main_table(
  1500. TABLE_STATISTIC_TRACK_E_HOTPOTATOES
  1501. );
  1502. $exercisePath = Database::escape_string($exercisePath);
  1503. $userId = intval($userId);
  1504. $sql = "SELECT * FROM $table
  1505. WHERE
  1506. c_id = $courseId AND
  1507. exe_name LIKE '$exercisePath%' AND
  1508. exe_user_id = $userId
  1509. ORDER BY id
  1510. LIMIT 1";
  1511. $result = Database::query($sql);
  1512. $attempt = array();
  1513. if (Database::num_rows($result)) {
  1514. $attempt = Database::fetch_array($result, 'ASSOC');
  1515. }
  1516. return $attempt;
  1517. }
  1518. /**
  1519. * Gets the exam'data results
  1520. * @todo this function should be moved in a library + no global calls
  1521. * @param int $from
  1522. * @param int $number_of_items
  1523. * @param int $column
  1524. * @param string $direction
  1525. * @param int $exercise_id
  1526. * @param null $extra_where_conditions
  1527. * @param bool $get_count
  1528. * @param string $courseCode
  1529. * @return array
  1530. */
  1531. public static function get_exam_results_data(
  1532. $from,
  1533. $number_of_items,
  1534. $column,
  1535. $direction,
  1536. $exercise_id,
  1537. $extra_where_conditions = null,
  1538. $get_count = false,
  1539. $courseCode = null
  1540. ) {
  1541. //@todo replace all this globals
  1542. global $documentPath, $filter;
  1543. $courseCode = empty($courseCode) ? api_get_course_id() : $courseCode;
  1544. $courseInfo = api_get_course_info($courseCode);
  1545. $course_id = $courseInfo['real_id'];
  1546. $sessionId = api_get_session_id();
  1547. $is_allowedToEdit = api_is_allowed_to_edit(null, true) || api_is_allowed_to_edit(true) || api_is_drh() || api_is_student_boss();
  1548. $TBL_USER = Database::get_main_table(TABLE_MAIN_USER);
  1549. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  1550. $TBL_GROUP_REL_USER = Database::get_course_table(TABLE_GROUP_USER);
  1551. $TBL_GROUP = Database::get_course_table(TABLE_GROUP);
  1552. $TBL_TRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1553. $TBL_TRACK_HOTPOTATOES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  1554. $TBL_TRACK_ATTEMPT_RECORDING = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1555. $session_id_and = ' AND te.session_id = '.$sessionId.' ';
  1556. $exercise_id = intval($exercise_id);
  1557. $exercise_where = '';
  1558. if (!empty($exercise_id)) {
  1559. $exercise_where .= ' AND te.exe_exo_id = '.$exercise_id.' ';
  1560. }
  1561. $hotpotatoe_where = '';
  1562. if (!empty($_GET['path'])) {
  1563. $hotpotatoe_path = Database::escape_string($_GET['path']);
  1564. $hotpotatoe_where .= ' AND exe_name = "'.$hotpotatoe_path.'" ';
  1565. }
  1566. // sql for chamilo-type tests for teacher / tutor view
  1567. $sql_inner_join_tbl_track_exercices = "
  1568. (
  1569. SELECT DISTINCT ttte.*, if(tr.exe_id,1, 0) as revised
  1570. FROM $TBL_TRACK_EXERCICES ttte LEFT JOIN $TBL_TRACK_ATTEMPT_RECORDING tr
  1571. ON (ttte.exe_id = tr.exe_id)
  1572. WHERE
  1573. c_id = $course_id AND
  1574. exe_exo_id = $exercise_id AND
  1575. ttte.session_id = ".$sessionId."
  1576. )";
  1577. if ($is_allowedToEdit) {
  1578. //@todo fix to work with COURSE_RELATION_TYPE_RRHH in both queries
  1579. // Hack in order to filter groups
  1580. $sql_inner_join_tbl_user = '';
  1581. if (strpos($extra_where_conditions, 'group_id')) {
  1582. $sql_inner_join_tbl_user = "
  1583. (
  1584. SELECT
  1585. u.user_id,
  1586. firstname,
  1587. lastname,
  1588. official_code,
  1589. email,
  1590. username,
  1591. g.name as group_name,
  1592. g.id as group_id
  1593. FROM $TBL_USER u
  1594. INNER JOIN $TBL_GROUP_REL_USER gru
  1595. ON (gru.user_id = u.user_id AND gru.c_id=".$course_id.")
  1596. INNER JOIN $TBL_GROUP g
  1597. ON (gru.group_id = g.id AND g.c_id=".$course_id.")
  1598. )";
  1599. }
  1600. if (strpos($extra_where_conditions, 'group_all')) {
  1601. $extra_where_conditions = str_replace(
  1602. "AND ( group_id = 'group_all' )",
  1603. '',
  1604. $extra_where_conditions
  1605. );
  1606. $extra_where_conditions = str_replace(
  1607. "AND group_id = 'group_all'",
  1608. '',
  1609. $extra_where_conditions
  1610. );
  1611. $extra_where_conditions = str_replace(
  1612. "group_id = 'group_all' AND",
  1613. '',
  1614. $extra_where_conditions
  1615. );
  1616. $sql_inner_join_tbl_user = "
  1617. (
  1618. SELECT
  1619. u.user_id,
  1620. firstname,
  1621. lastname,
  1622. official_code,
  1623. email,
  1624. username,
  1625. '' as group_name,
  1626. '' as group_id
  1627. FROM $TBL_USER u
  1628. )";
  1629. $sql_inner_join_tbl_user = null;
  1630. }
  1631. if (strpos($extra_where_conditions, 'group_none')) {
  1632. $extra_where_conditions = str_replace(
  1633. "AND ( group_id = 'group_none' )",
  1634. "AND ( group_id is null )",
  1635. $extra_where_conditions
  1636. );
  1637. $extra_where_conditions = str_replace(
  1638. "AND group_id = 'group_none'",
  1639. "AND ( group_id is null )",
  1640. $extra_where_conditions
  1641. );
  1642. $sql_inner_join_tbl_user = "
  1643. (
  1644. SELECT
  1645. u.user_id,
  1646. firstname,
  1647. lastname,
  1648. official_code,
  1649. email,
  1650. username,
  1651. g.name as group_name,
  1652. g.id as group_id
  1653. FROM $TBL_USER u
  1654. LEFT OUTER JOIN $TBL_GROUP_REL_USER gru
  1655. ON ( gru.user_id = u.user_id AND gru.c_id=".$course_id." )
  1656. LEFT OUTER JOIN $TBL_GROUP g
  1657. ON (gru.group_id = g.id AND g.c_id = ".$course_id.")
  1658. )";
  1659. }
  1660. // All
  1661. $is_empty_sql_inner_join_tbl_user = false;
  1662. if (empty($sql_inner_join_tbl_user)) {
  1663. $is_empty_sql_inner_join_tbl_user = true;
  1664. $sql_inner_join_tbl_user = "
  1665. (
  1666. SELECT u.user_id, firstname, lastname, email, username, ' ' as group_name, '' as group_id, official_code
  1667. FROM $TBL_USER u
  1668. WHERE u.status NOT IN(".api_get_users_status_ignored_in_reports('string').")
  1669. )";
  1670. }
  1671. $sqlFromOption = " , $TBL_GROUP_REL_USER AS gru ";
  1672. $sqlWhereOption = " AND gru.c_id = ".$course_id." AND gru.user_id = user.user_id ";
  1673. $first_and_last_name = api_is_western_name_order() ? "firstname, lastname" : "lastname, firstname";
  1674. if ($get_count) {
  1675. $sql_select = "SELECT count(te.exe_id) ";
  1676. } else {
  1677. $sql_select = "SELECT DISTINCT
  1678. user_id,
  1679. $first_and_last_name,
  1680. official_code,
  1681. ce.title,
  1682. username,
  1683. te.exe_result,
  1684. te.exe_weighting,
  1685. te.exe_date,
  1686. te.exe_id,
  1687. email as exemail,
  1688. te.start_date,
  1689. ce.expired_time,
  1690. steps_counter,
  1691. exe_user_id,
  1692. te.exe_duration,
  1693. te.status as completion_status,
  1694. propagate_neg,
  1695. revised,
  1696. group_name,
  1697. group_id,
  1698. orig_lp_id,
  1699. te.user_ip";
  1700. }
  1701. $sql = " $sql_select
  1702. FROM $TBL_EXERCICES AS ce
  1703. INNER JOIN $sql_inner_join_tbl_track_exercices AS te
  1704. ON (te.exe_exo_id = ce.id)
  1705. INNER JOIN $sql_inner_join_tbl_user AS user
  1706. ON (user.user_id = exe_user_id)
  1707. WHERE
  1708. te.c_id = ".$course_id." $session_id_and AND
  1709. ce.active <>-1 AND
  1710. ce.c_id = ".$course_id."
  1711. $exercise_where
  1712. $extra_where_conditions
  1713. ";
  1714. // sql for hotpotatoes tests for teacher / tutor view
  1715. if ($get_count) {
  1716. $hpsql_select = "SELECT count(username)";
  1717. } else {
  1718. $hpsql_select = "SELECT
  1719. $first_and_last_name ,
  1720. username,
  1721. official_code,
  1722. tth.exe_name,
  1723. tth.exe_result ,
  1724. tth.exe_weighting,
  1725. tth.exe_date";
  1726. }
  1727. $hpsql = " $hpsql_select
  1728. FROM
  1729. $TBL_TRACK_HOTPOTATOES tth,
  1730. $TBL_USER user
  1731. $sqlFromOption
  1732. WHERE
  1733. user.user_id=tth.exe_user_id
  1734. AND tth.c_id = ".$course_id."
  1735. $hotpotatoe_where
  1736. $sqlWhereOption
  1737. AND user.status NOT IN(".api_get_users_status_ignored_in_reports('string').")
  1738. ORDER BY
  1739. tth.c_id ASC,
  1740. tth.exe_date DESC";
  1741. }
  1742. if ($get_count) {
  1743. $resx = Database::query($sql);
  1744. $rowx = Database::fetch_row($resx, 'ASSOC');
  1745. return $rowx[0];
  1746. }
  1747. $teacher_list = CourseManager::get_teacher_list_from_course_code(
  1748. $courseCode
  1749. );
  1750. $teacher_id_list = array();
  1751. if (!empty($teacher_list)) {
  1752. foreach ($teacher_list as $teacher) {
  1753. $teacher_id_list[] = $teacher['user_id'];
  1754. }
  1755. }
  1756. $list_info = array();
  1757. // Simple exercises
  1758. if (empty($hotpotatoe_where)) {
  1759. $column = !empty($column) ? Database::escape_string($column) : null;
  1760. $from = intval($from);
  1761. $number_of_items = intval($number_of_items);
  1762. if (!empty($column)) {
  1763. $sql .= " ORDER BY $column $direction ";
  1764. }
  1765. $sql .= " LIMIT $from, $number_of_items";
  1766. $results = array();
  1767. $resx = Database::query($sql);
  1768. while ($rowx = Database::fetch_array($resx, 'ASSOC')) {
  1769. $results[] = $rowx;
  1770. }
  1771. $group_list = GroupManager::get_group_list(null, $courseCode);
  1772. $clean_group_list = array();
  1773. if (!empty($group_list)) {
  1774. foreach ($group_list as $group) {
  1775. $clean_group_list[$group['id']] = $group['name'];
  1776. }
  1777. }
  1778. $lp_list_obj = new LearnpathList(api_get_user_id());
  1779. $lp_list = $lp_list_obj->get_flat_list();
  1780. $oldIds = array_column($lp_list, 'lp_old_id', 'iid');
  1781. if (is_array($results)) {
  1782. $users_array_id = array();
  1783. $from_gradebook = false;
  1784. if (isset($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
  1785. $from_gradebook = true;
  1786. }
  1787. $sizeof = count($results);
  1788. $user_list_id = array();
  1789. $locked = api_resource_is_locked_by_gradebook(
  1790. $exercise_id,
  1791. LINK_EXERCISE
  1792. );
  1793. $timeNow = strtotime(api_get_utc_datetime());
  1794. // Looping results
  1795. for ($i = 0; $i < $sizeof; $i++) {
  1796. $revised = $results[$i]['revised'];
  1797. if ($results[$i]['completion_status'] == 'incomplete') {
  1798. // If the exercise was incomplete, we need to determine
  1799. // if it is still into the time allowed, or if its
  1800. // allowed time has expired and it can be closed
  1801. // (it's "unclosed")
  1802. $minutes = $results[$i]['expired_time'];
  1803. if ($minutes == 0) {
  1804. // There's no time limit, so obviously the attempt
  1805. // can still be "ongoing", but the teacher should
  1806. // be able to choose to close it, so mark it as
  1807. // "unclosed" instead of "ongoing"
  1808. $revised = 2;
  1809. } else {
  1810. $allowedSeconds = $minutes * 60;
  1811. $timeAttemptStarted = strtotime($results[$i]['start_date']);
  1812. $secondsSinceStart = $timeNow - $timeAttemptStarted;
  1813. if ($secondsSinceStart > $allowedSeconds) {
  1814. $revised = 2; // mark as "unclosed"
  1815. } else {
  1816. $revised = 3; // mark as "ongoing"
  1817. }
  1818. }
  1819. }
  1820. if ($from_gradebook && ($is_allowedToEdit)) {
  1821. if (in_array(
  1822. $results[$i]['username'].$results[$i]['firstname'].$results[$i]['lastname'],
  1823. $users_array_id
  1824. )) {
  1825. continue;
  1826. }
  1827. $users_array_id[] = $results[$i]['username'].$results[$i]['firstname'].$results[$i]['lastname'];
  1828. }
  1829. $lp_obj = isset($results[$i]['orig_lp_id']) && isset($lp_list[$results[$i]['orig_lp_id']]) ? $lp_list[$results[$i]['orig_lp_id']] : null;
  1830. if (empty($lp_obj)) {
  1831. // Try to get the old id (id instead of iid)
  1832. $lpNewId = isset($results[$i]['orig_lp_id']) && isset($oldIds[$results[$i]['orig_lp_id']]) ? $oldIds[$results[$i]['orig_lp_id']] : null;
  1833. if ($lpNewId) {
  1834. $lp_obj = isset($lp_list[$lpNewId]) ? $lp_list[$lpNewId] : null;
  1835. }
  1836. }
  1837. $lp_name = null;
  1838. if ($lp_obj) {
  1839. $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq().'&action=view&lp_id='.$results[$i]['orig_lp_id'];
  1840. $lp_name = Display::url(
  1841. $lp_obj['lp_name'],
  1842. $url,
  1843. array('target' => '_blank')
  1844. );
  1845. }
  1846. // Add all groups by user
  1847. $group_name_list = null;
  1848. if ($is_empty_sql_inner_join_tbl_user) {
  1849. $group_list = GroupManager::get_group_ids(
  1850. api_get_course_int_id(),
  1851. $results[$i]['user_id']
  1852. );
  1853. foreach ($group_list as $id) {
  1854. $group_name_list .= $clean_group_list[$id].'<br/>';
  1855. }
  1856. $results[$i]['group_name'] = $group_name_list;
  1857. }
  1858. $results[$i]['exe_duration'] = !empty($results[$i]['exe_duration']) ? round($results[$i]['exe_duration'] / 60) : 0;
  1859. $user_list_id[] = $results[$i]['exe_user_id'];
  1860. $id = $results[$i]['exe_id'];
  1861. $dt = api_convert_and_format_date($results[$i]['exe_weighting']);
  1862. // we filter the results if we have the permission to
  1863. if (isset($results[$i]['results_disabled'])) {
  1864. $result_disabled = intval(
  1865. $results[$i]['results_disabled']
  1866. );
  1867. } else {
  1868. $result_disabled = 0;
  1869. }
  1870. if ($result_disabled == 0) {
  1871. $my_res = $results[$i]['exe_result'];
  1872. $my_total = $results[$i]['exe_weighting'];
  1873. $results[$i]['start_date'] = api_get_local_time(
  1874. $results[$i]['start_date']
  1875. );
  1876. $results[$i]['exe_date'] = api_get_local_time(
  1877. $results[$i]['exe_date']
  1878. );
  1879. if (!$results[$i]['propagate_neg'] && $my_res < 0) {
  1880. $my_res = 0;
  1881. }
  1882. $score = self::show_score($my_res, $my_total);
  1883. $actions = '<div class="pull-right">';
  1884. if ($is_allowedToEdit) {
  1885. if (isset($teacher_id_list)) {
  1886. if (in_array(
  1887. $results[$i]['exe_user_id'],
  1888. $teacher_id_list
  1889. )) {
  1890. $actions .= Display::return_icon(
  1891. 'teacher.png',
  1892. get_lang('Teacher')
  1893. );
  1894. }
  1895. }
  1896. $revisedLabel = '';
  1897. switch ($revised) {
  1898. case 0:
  1899. $actions .= "<a href='exercise_show.php?".api_get_cidreq()."&action=qualify&id=$id'>".
  1900. Display:: return_icon(
  1901. 'quiz.png',
  1902. get_lang('Qualify')
  1903. );
  1904. $actions .= '</a>';
  1905. $revisedLabel = Display::label(
  1906. get_lang('NotValidated'),
  1907. 'info'
  1908. );
  1909. break;
  1910. case 1:
  1911. $actions .= "<a href='exercise_show.php?".api_get_cidreq()."&action=edit&id=$id'>".
  1912. Display:: return_icon(
  1913. 'edit.png',
  1914. get_lang('Edit'),
  1915. array(),
  1916. ICON_SIZE_SMALL
  1917. );
  1918. $actions .= '</a>';
  1919. $revisedLabel = Display::label(
  1920. get_lang('Validated'),
  1921. 'success'
  1922. );
  1923. break;
  1924. case 2: //finished but not marked as such
  1925. $actions .= '<a href="exercise_report.php?'
  1926. . api_get_cidreq()
  1927. . '&exerciseId='
  1928. . $exercise_id
  1929. . '&a=close&id='
  1930. . $id
  1931. . '">'.
  1932. Display:: return_icon(
  1933. 'lock.png',
  1934. get_lang('MarkAttemptAsClosed'),
  1935. array(),
  1936. ICON_SIZE_SMALL
  1937. );
  1938. $actions .= '</a>';
  1939. $revisedLabel = Display::label(
  1940. get_lang('Unclosed'),
  1941. 'warning'
  1942. );
  1943. break;
  1944. case 3: //still ongoing
  1945. $actions .= "".
  1946. Display:: return_icon(
  1947. 'clock.png',
  1948. get_lang('AttemptStillOngoingPleaseWait'),
  1949. array(),
  1950. ICON_SIZE_SMALL
  1951. );
  1952. $actions .= '';
  1953. $revisedLabel = Display::label(
  1954. get_lang('Ongoing'),
  1955. 'danger'
  1956. );
  1957. break;
  1958. }
  1959. if ($filter == 2) {
  1960. $actions .= ' <a href="exercise_history.php?'.api_get_cidreq().'&exe_id='.$id.'">'.
  1961. Display:: return_icon(
  1962. 'history.png',
  1963. get_lang('ViewHistoryChange')
  1964. ).'</a>';
  1965. }
  1966. //Admin can always delete the attempt
  1967. if (($locked == false || api_is_platform_admin()) && !api_is_student_boss()) {
  1968. $ip = TrackingUserLog::get_ip_from_user_event(
  1969. $results[$i]['exe_user_id'],
  1970. api_get_utc_datetime(),
  1971. false
  1972. );
  1973. $actions .= '<a href="http://www.whatsmyip.org/ip-geo-location/?ip='.$ip.'" target="_blank">'
  1974. . Display::return_icon('info.png', $ip)
  1975. .'</a>';
  1976. $recalculateUrl = api_get_path(WEB_CODE_PATH).'exercise/recalculate.php?'.
  1977. api_get_cidreq().'&'.
  1978. http_build_query([
  1979. 'id' => $id,
  1980. 'exercise' => $exercise_id,
  1981. 'user' => $results[$i]['exe_user_id']
  1982. ]);
  1983. $actions .= Display::url(
  1984. Display::return_icon('reload.png', get_lang('RecalculateResults')),
  1985. $recalculateUrl,
  1986. [
  1987. 'data-exercise' => $exercise_id,
  1988. 'data-user' => $results[$i]['exe_user_id'],
  1989. 'data-id' => $id,
  1990. 'class' => 'exercise-recalculate'
  1991. ]
  1992. );
  1993. $delete_link = '<a href="exercise_report.php?'.api_get_cidreq().'&filter_by_user='.intval($_GET['filter_by_user']).'&filter='.$filter.'&exerciseId='.$exercise_id.'&delete=delete&did='.$id.'"
  1994. onclick="javascript:if(!confirm(\'' . sprintf(
  1995. get_lang('DeleteAttempt'),
  1996. $results[$i]['username'],
  1997. $dt
  1998. ).'\')) return false;">'.Display:: return_icon(
  1999. 'delete.png',
  2000. get_lang('Delete')
  2001. ).'</a>';
  2002. $delete_link = utf8_encode($delete_link);
  2003. if (api_is_drh() && !api_is_platform_admin()) {
  2004. $delete_link = null;
  2005. }
  2006. if ($revised == 3) {
  2007. $delete_link = null;
  2008. }
  2009. $actions .= $delete_link;
  2010. }
  2011. } else {
  2012. $attempt_url = api_get_path(WEB_CODE_PATH).'exercise/result.php?'.api_get_cidreq().'&id='.$results[$i]['exe_id'].'&id_session='.$sessionId;
  2013. $attempt_link = Display::url(
  2014. get_lang('Show'),
  2015. $attempt_url,
  2016. [
  2017. 'class' => 'ajax btn btn-default',
  2018. 'data-title' => get_lang('Show')
  2019. ]
  2020. );
  2021. $actions .= $attempt_link;
  2022. }
  2023. $actions .= '</div>';
  2024. $results[$i]['id'] = $results[$i]['exe_id'];
  2025. if ($is_allowedToEdit) {
  2026. $results[$i]['status'] = $revisedLabel;
  2027. $results[$i]['score'] = $score;
  2028. $results[$i]['lp'] = $lp_name;
  2029. $results[$i]['actions'] = $actions;
  2030. $list_info[] = $results[$i];
  2031. } else {
  2032. $results[$i]['status'] = $revisedLabel;
  2033. $results[$i]['score'] = $score;
  2034. $results[$i]['actions'] = $actions;
  2035. $list_info[] = $results[$i];
  2036. }
  2037. }
  2038. }
  2039. }
  2040. } else {
  2041. $hpresults = StatsUtils::getManyResultsXCol($hpsql, 6);
  2042. // Print HotPotatoes test results.
  2043. if (is_array($hpresults)) {
  2044. for ($i = 0; $i < sizeof($hpresults); $i++) {
  2045. $hp_title = GetQuizName($hpresults[$i][3], $documentPath);
  2046. if ($hp_title == '') {
  2047. $hp_title = basename($hpresults[$i][3]);
  2048. }
  2049. $hp_date = api_get_local_time(
  2050. $hpresults[$i][6],
  2051. null,
  2052. date_default_timezone_get()
  2053. );
  2054. $hp_result = round(($hpresults[$i][4] / ($hpresults[$i][5] != 0 ? $hpresults[$i][5] : 1)) * 100, 2)
  2055. .'% ('.$hpresults[$i][4].' / '.$hpresults[$i][5].')';
  2056. if ($is_allowedToEdit) {
  2057. $list_info[] = array(
  2058. $hpresults[$i][0],
  2059. $hpresults[$i][1],
  2060. $hpresults[$i][2],
  2061. '',
  2062. $hp_title,
  2063. '-',
  2064. $hp_date,
  2065. $hp_result,
  2066. '-'
  2067. );
  2068. } else {
  2069. $list_info[] = array(
  2070. $hp_title,
  2071. '-',
  2072. $hp_date,
  2073. $hp_result,
  2074. '-'
  2075. );
  2076. }
  2077. }
  2078. }
  2079. }
  2080. return $list_info;
  2081. }
  2082. /**
  2083. * Converts the score with the exercise_max_note and exercise_min_score
  2084. * the platform settings + formats the results using the float_format function
  2085. *
  2086. * @param float $score
  2087. * @param float $weight
  2088. * @param bool $show_percentage show percentage or not
  2089. * @param bool $use_platform_settings use or not the platform settings
  2090. * @param bool $show_only_percentage
  2091. * @return string an html with the score modified
  2092. */
  2093. public static function show_score(
  2094. $score,
  2095. $weight,
  2096. $show_percentage = true,
  2097. $use_platform_settings = true,
  2098. $show_only_percentage = false
  2099. ) {
  2100. if (is_null($score) && is_null($weight)) {
  2101. return '-';
  2102. }
  2103. $max_note = api_get_setting('exercise_max_score');
  2104. $min_note = api_get_setting('exercise_min_score');
  2105. if ($use_platform_settings) {
  2106. if ($max_note != '' && $min_note != '') {
  2107. if (!empty($weight) && intval($weight) != 0) {
  2108. $score = $min_note + ($max_note - $min_note) * $score / $weight;
  2109. } else {
  2110. $score = $min_note;
  2111. }
  2112. $weight = $max_note;
  2113. }
  2114. }
  2115. $percentage = (100 * $score) / ($weight != 0 ? $weight : 1);
  2116. // Formats values
  2117. $percentage = float_format($percentage, 1);
  2118. $score = float_format($score, 1);
  2119. $weight = float_format($weight, 1);
  2120. $html = null;
  2121. if ($show_percentage) {
  2122. $parent = '('.$score.' / '.$weight.')';
  2123. $html = $percentage."% $parent";
  2124. if ($show_only_percentage) {
  2125. $html = $percentage."% ";
  2126. }
  2127. } else {
  2128. $html = $score.' / '.$weight;
  2129. }
  2130. $html = Display::span($html, array('class' => 'score_exercise'));
  2131. return $html;
  2132. }
  2133. /**
  2134. * @param float $score
  2135. * @param float $weight
  2136. * @param string $pass_percentage
  2137. * @return bool
  2138. */
  2139. public static function isSuccessExerciseResult($score, $weight, $pass_percentage)
  2140. {
  2141. $percentage = float_format(
  2142. ($score / ($weight != 0 ? $weight : 1)) * 100,
  2143. 1
  2144. );
  2145. if (isset($pass_percentage) && !empty($pass_percentage)) {
  2146. if ($percentage >= $pass_percentage) {
  2147. return true;
  2148. }
  2149. }
  2150. return false;
  2151. }
  2152. /**
  2153. * @param float $score
  2154. * @param float $weight
  2155. * @param string $pass_percentage
  2156. * @return string
  2157. */
  2158. public static function showSuccessMessage($score, $weight, $pass_percentage)
  2159. {
  2160. $res = '';
  2161. if (self::isPassPercentageEnabled($pass_percentage)) {
  2162. $isSuccess = self::isSuccessExerciseResult(
  2163. $score,
  2164. $weight,
  2165. $pass_percentage
  2166. );
  2167. if ($isSuccess) {
  2168. $html = get_lang('CongratulationsYouPassedTheTest');
  2169. $icon = Display::return_icon(
  2170. 'completed.png',
  2171. get_lang('Correct'),
  2172. array(),
  2173. ICON_SIZE_MEDIUM
  2174. );
  2175. } else {
  2176. //$html .= Display::return_message(get_lang('YouDidNotReachTheMinimumScore'), 'warning');
  2177. $html = get_lang('YouDidNotReachTheMinimumScore');
  2178. $icon = Display::return_icon(
  2179. 'warning.png',
  2180. get_lang('Wrong'),
  2181. array(),
  2182. ICON_SIZE_MEDIUM
  2183. );
  2184. }
  2185. $html = Display::tag('h4', $html);
  2186. $html .= Display::tag(
  2187. 'h5',
  2188. $icon,
  2189. array('style' => 'width:40px; padding:2px 10px 0px 0px')
  2190. );
  2191. $res = $html;
  2192. }
  2193. return $res;
  2194. }
  2195. /**
  2196. * Return true if pass_pourcentage activated (we use the pass pourcentage feature
  2197. * return false if pass_percentage = 0 (we don't use the pass pourcentage feature
  2198. * @param $value
  2199. * @return boolean
  2200. * In this version, pass_percentage and show_success_message are disabled if
  2201. * pass_percentage is set to 0
  2202. */
  2203. public static function isPassPercentageEnabled($value)
  2204. {
  2205. return $value > 0;
  2206. }
  2207. /**
  2208. * Converts a numeric value in a percentage example 0.66666 to 66.67 %
  2209. * @param $value
  2210. * @return float Converted number
  2211. */
  2212. public static function convert_to_percentage($value)
  2213. {
  2214. $return = '-';
  2215. if ($value != '') {
  2216. $return = float_format($value * 100, 1).' %';
  2217. }
  2218. return $return;
  2219. }
  2220. /**
  2221. * Converts a score/weight values to the platform scale
  2222. * @param float $score
  2223. * @param float $weight
  2224. * @deprecated seem not to be used
  2225. * @return float the score rounded converted to the new range
  2226. */
  2227. public static function convert_score($score, $weight)
  2228. {
  2229. $max_note = api_get_setting('exercise_max_score');
  2230. $min_note = api_get_setting('exercise_min_score');
  2231. if ($score != '' && $weight != '') {
  2232. if ($max_note != '' && $min_note != '') {
  2233. if (!empty($weight)) {
  2234. $score = $min_note + ($max_note - $min_note) * $score / $weight;
  2235. } else {
  2236. $score = $min_note;
  2237. }
  2238. }
  2239. }
  2240. $score_rounded = float_format($score, 1);
  2241. return $score_rounded;
  2242. }
  2243. /**
  2244. * Getting all active exercises from a course from a session
  2245. * (if a session_id is provided we will show all the exercises in the course +
  2246. * all exercises in the session)
  2247. * @param array $course_info
  2248. * @param int $session_id
  2249. * @param boolean $check_publication_dates
  2250. * @param string $search Search exercise name
  2251. * @param boolean $search_all_sessions Search exercises in all sessions
  2252. * @param int 0 = only inactive exercises
  2253. * 1 = only active exercises,
  2254. * 2 = all exercises
  2255. * 3 = active <> -1
  2256. * @return array array with exercise data
  2257. */
  2258. public static function get_all_exercises(
  2259. $course_info = null,
  2260. $session_id = 0,
  2261. $check_publication_dates = false,
  2262. $search = '',
  2263. $search_all_sessions = false,
  2264. $active = 2
  2265. ) {
  2266. $course_id = api_get_course_int_id();
  2267. if (!empty($course_info) && !empty($course_info['real_id'])) {
  2268. $course_id = $course_info['real_id'];
  2269. }
  2270. if ($session_id == -1) {
  2271. $session_id = 0;
  2272. }
  2273. $now = api_get_utc_datetime();
  2274. $time_conditions = '';
  2275. if ($check_publication_dates) {
  2276. //start and end are set
  2277. $time_conditions = " AND ((start_time <> '' AND start_time < '$now' AND end_time <> '' AND end_time > '$now' ) OR ";
  2278. // only start is set
  2279. $time_conditions .= " (start_time <> '' AND start_time < '$now' AND end_time is NULL) OR ";
  2280. // only end is set
  2281. $time_conditions .= " (start_time IS NULL AND end_time <> '' AND end_time > '$now') OR ";
  2282. // nothing is set
  2283. $time_conditions .= " (start_time IS NULL AND end_time IS NULL)) ";
  2284. }
  2285. $needle_where = !empty($search) ? " AND title LIKE '?' " : '';
  2286. $needle = !empty($search) ? "%".$search."%" : '';
  2287. // Show courses by active status
  2288. $active_sql = '';
  2289. if ($active == 3) {
  2290. $active_sql = ' active <> -1 AND';
  2291. } else {
  2292. if ($active != 2) {
  2293. $active_sql = sprintf(' active = %d AND', $active);
  2294. }
  2295. }
  2296. if ($search_all_sessions == true) {
  2297. $conditions = array(
  2298. 'where' => array(
  2299. $active_sql.' c_id = ? '.$needle_where.$time_conditions => array(
  2300. $course_id,
  2301. $needle
  2302. )
  2303. ),
  2304. 'order' => 'title'
  2305. );
  2306. } else {
  2307. if ($session_id == 0) {
  2308. $conditions = array(
  2309. 'where' => array(
  2310. $active_sql.' session_id = ? AND c_id = ? '.$needle_where.$time_conditions => array(
  2311. $session_id,
  2312. $course_id,
  2313. $needle
  2314. )
  2315. ),
  2316. 'order' => 'title'
  2317. );
  2318. } else {
  2319. $conditions = array(
  2320. 'where' => array(
  2321. $active_sql.' (session_id = 0 OR session_id = ? ) AND c_id = ? '.$needle_where.$time_conditions => array(
  2322. $session_id,
  2323. $course_id,
  2324. $needle
  2325. )
  2326. ),
  2327. 'order' => 'title'
  2328. );
  2329. }
  2330. }
  2331. $table = Database::get_course_table(TABLE_QUIZ_TEST);
  2332. return Database::select('*', $table, $conditions);
  2333. }
  2334. /**
  2335. * Get exercise information by id
  2336. * @param int $exerciseId Exercise Id
  2337. * @param int $courseId The course ID (necessary as c_quiz.id is not unique)
  2338. * @return array Exercise info
  2339. */
  2340. public static function get_exercise_by_id($exerciseId = 0, $courseId = 0)
  2341. {
  2342. $table = Database::get_course_table(TABLE_QUIZ_TEST);
  2343. if (empty($courseId)) {
  2344. $courseId = api_get_course_int_id();
  2345. } else {
  2346. $courseId = intval($courseId);
  2347. }
  2348. $conditions = array(
  2349. 'where' => array(
  2350. 'id = ?' => array($exerciseId),
  2351. ' AND c_id = ? ' => $courseId
  2352. )
  2353. );
  2354. return Database::select('*', $table, $conditions);
  2355. }
  2356. /**
  2357. * Getting all exercises (active only or all)
  2358. * from a course from a session
  2359. * (if a session_id is provided we will show all the exercises in the
  2360. * course + all exercises in the session)
  2361. * @param array course data
  2362. * @param int session id
  2363. * @param int course c_id
  2364. * @param boolean $only_active_exercises
  2365. * @return array array with exercise data
  2366. * modified by Hubert Borderiou
  2367. */
  2368. public static function get_all_exercises_for_course_id(
  2369. $course_info = null,
  2370. $session_id = 0,
  2371. $course_id = 0,
  2372. $only_active_exercises = true
  2373. ) {
  2374. $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
  2375. if ($only_active_exercises) {
  2376. // Only active exercises.
  2377. $sql_active_exercises = "active = 1 AND ";
  2378. } else {
  2379. // Not only active means visible and invisible NOT deleted (-2)
  2380. $sql_active_exercises = "active IN (1, 0) AND ";
  2381. }
  2382. if ($session_id == -1) {
  2383. $session_id = 0;
  2384. }
  2385. $params = array(
  2386. $session_id,
  2387. $course_id
  2388. );
  2389. if ($session_id == 0) {
  2390. $conditions = array(
  2391. 'where' => array("$sql_active_exercises session_id = ? AND c_id = ?" => $params),
  2392. 'order' => 'title'
  2393. );
  2394. } else {
  2395. // All exercises
  2396. $conditions = array(
  2397. 'where' => array("$sql_active_exercises (session_id = 0 OR session_id = ? ) AND c_id=?" => $params),
  2398. 'order' => 'title'
  2399. );
  2400. }
  2401. return Database::select('*', $TBL_EXERCISES, $conditions);
  2402. }
  2403. /**
  2404. * Gets the position of the score based in a given score (result/weight)
  2405. * and the exe_id based in the user list
  2406. * (NO Exercises in LPs )
  2407. * @param float $my_score user score to be compared *attention*
  2408. * $my_score = score/weight and not just the score
  2409. * @param int $my_exe_id exe id of the exercise
  2410. * (this is necessary because if 2 students have the same score the one
  2411. * with the minor exe_id will have a best position, just to be fair and FIFO)
  2412. * @param int $exercise_id
  2413. * @param string $course_code
  2414. * @param int $session_id
  2415. * @param array $user_list
  2416. * @param bool $return_string
  2417. *
  2418. * @return int the position of the user between his friends in a course
  2419. * (or course within a session)
  2420. */
  2421. public static function get_exercise_result_ranking(
  2422. $my_score,
  2423. $my_exe_id,
  2424. $exercise_id,
  2425. $course_code,
  2426. $session_id = 0,
  2427. $user_list = array(),
  2428. $return_string = true
  2429. ) {
  2430. //No score given we return
  2431. if (is_null($my_score)) {
  2432. return '-';
  2433. }
  2434. if (empty($user_list)) {
  2435. return '-';
  2436. }
  2437. $best_attempts = array();
  2438. foreach ($user_list as $user_data) {
  2439. $user_id = $user_data['user_id'];
  2440. $best_attempts[$user_id] = self::get_best_attempt_by_user(
  2441. $user_id,
  2442. $exercise_id,
  2443. $course_code,
  2444. $session_id
  2445. );
  2446. }
  2447. if (empty($best_attempts)) {
  2448. return 1;
  2449. } else {
  2450. $position = 1;
  2451. $my_ranking = array();
  2452. foreach ($best_attempts as $user_id => $result) {
  2453. if (!empty($result['exe_weighting']) && intval(
  2454. $result['exe_weighting']
  2455. ) != 0
  2456. ) {
  2457. $my_ranking[$user_id] = $result['exe_result'] / $result['exe_weighting'];
  2458. } else {
  2459. $my_ranking[$user_id] = 0;
  2460. }
  2461. }
  2462. //if (!empty($my_ranking)) {
  2463. asort($my_ranking);
  2464. $position = count($my_ranking);
  2465. if (!empty($my_ranking)) {
  2466. foreach ($my_ranking as $user_id => $ranking) {
  2467. if ($my_score >= $ranking) {
  2468. if ($my_score == $ranking && isset($best_attempts[$user_id]['exe_id'])) {
  2469. $exe_id = $best_attempts[$user_id]['exe_id'];
  2470. if ($my_exe_id < $exe_id) {
  2471. $position--;
  2472. }
  2473. } else {
  2474. $position--;
  2475. }
  2476. }
  2477. }
  2478. }
  2479. //}
  2480. $return_value = array(
  2481. 'position' => $position,
  2482. 'count' => count($my_ranking)
  2483. );
  2484. if ($return_string) {
  2485. if (!empty($position) && !empty($my_ranking)) {
  2486. $return_value = $position.'/'.count($my_ranking);
  2487. } else {
  2488. $return_value = '-';
  2489. }
  2490. }
  2491. return $return_value;
  2492. }
  2493. }
  2494. /**
  2495. * Gets the position of the score based in a given score (result/weight) and the exe_id based in all attempts
  2496. * (NO Exercises in LPs ) old functionality by attempt
  2497. * @param float user score to be compared attention => score/weight
  2498. * @param int exe id of the exercise
  2499. * (this is necessary because if 2 students have the same score the one
  2500. * with the minor exe_id will have a best position, just to be fair and FIFO)
  2501. * @param int exercise id
  2502. * @param string course code
  2503. * @param int session id
  2504. * @param bool $return_string
  2505. * @return int the position of the user between his friends in a course (or course within a session)
  2506. */
  2507. public static function get_exercise_result_ranking_by_attempt(
  2508. $my_score,
  2509. $my_exe_id,
  2510. $exercise_id,
  2511. $courseId,
  2512. $session_id = 0,
  2513. $return_string = true
  2514. ) {
  2515. if (empty($session_id)) {
  2516. $session_id = 0;
  2517. }
  2518. if (is_null($my_score)) {
  2519. return '-';
  2520. }
  2521. $user_results = Event::get_all_exercise_results(
  2522. $exercise_id,
  2523. $courseId,
  2524. $session_id,
  2525. false
  2526. );
  2527. $position_data = array();
  2528. if (empty($user_results)) {
  2529. return 1;
  2530. } else {
  2531. $position = 1;
  2532. $my_ranking = array();
  2533. foreach ($user_results as $result) {
  2534. //print_r($result);
  2535. if (!empty($result['exe_weighting']) && intval(
  2536. $result['exe_weighting']
  2537. ) != 0
  2538. ) {
  2539. $my_ranking[$result['exe_id']] = $result['exe_result'] / $result['exe_weighting'];
  2540. } else {
  2541. $my_ranking[$result['exe_id']] = 0;
  2542. }
  2543. }
  2544. asort($my_ranking);
  2545. $position = count($my_ranking);
  2546. if (!empty($my_ranking)) {
  2547. foreach ($my_ranking as $exe_id => $ranking) {
  2548. if ($my_score >= $ranking) {
  2549. if ($my_score == $ranking) {
  2550. if ($my_exe_id < $exe_id) {
  2551. $position--;
  2552. }
  2553. } else {
  2554. $position--;
  2555. }
  2556. }
  2557. }
  2558. }
  2559. $return_value = array(
  2560. 'position' => $position,
  2561. 'count' => count($my_ranking)
  2562. );
  2563. if ($return_string) {
  2564. if (!empty($position) && !empty($my_ranking)) {
  2565. return $position.'/'.count($my_ranking);
  2566. }
  2567. }
  2568. return $return_value;
  2569. }
  2570. }
  2571. /**
  2572. * Get the best attempt in a exercise (NO Exercises in LPs )
  2573. * @param int $exercise_id
  2574. * @param int $courseId
  2575. * @param int $session_id
  2576. *
  2577. * @return array
  2578. */
  2579. public static function get_best_attempt_in_course($exercise_id, $courseId, $session_id)
  2580. {
  2581. $user_results = Event::get_all_exercise_results(
  2582. $exercise_id,
  2583. $courseId,
  2584. $session_id,
  2585. false
  2586. );
  2587. $best_score_data = array();
  2588. $best_score = 0;
  2589. if (!empty($user_results)) {
  2590. foreach ($user_results as $result) {
  2591. if (!empty($result['exe_weighting']) &&
  2592. intval($result['exe_weighting']) != 0
  2593. ) {
  2594. $score = $result['exe_result'] / $result['exe_weighting'];
  2595. if ($score >= $best_score) {
  2596. $best_score = $score;
  2597. $best_score_data = $result;
  2598. }
  2599. }
  2600. }
  2601. }
  2602. return $best_score_data;
  2603. }
  2604. /**
  2605. * Get the best score in a exercise (NO Exercises in LPs )
  2606. * @param int $user_id
  2607. * @param int $exercise_id
  2608. * @param int $courseId
  2609. * @param int $session_id
  2610. *
  2611. * @return array
  2612. */
  2613. public static function get_best_attempt_by_user(
  2614. $user_id,
  2615. $exercise_id,
  2616. $courseId,
  2617. $session_id
  2618. )
  2619. {
  2620. $user_results = Event::get_all_exercise_results(
  2621. $exercise_id,
  2622. $courseId,
  2623. $session_id,
  2624. false,
  2625. $user_id
  2626. );
  2627. $best_score_data = array();
  2628. $best_score = 0;
  2629. if (!empty($user_results)) {
  2630. foreach ($user_results as $result) {
  2631. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  2632. $score = $result['exe_result'] / $result['exe_weighting'];
  2633. if ($score >= $best_score) {
  2634. $best_score = $score;
  2635. $best_score_data = $result;
  2636. }
  2637. }
  2638. }
  2639. }
  2640. return $best_score_data;
  2641. }
  2642. /**
  2643. * Get average score (NO Exercises in LPs )
  2644. * @param int exercise id
  2645. * @param int $courseId
  2646. * @param int session id
  2647. * @return float Average score
  2648. */
  2649. public static function get_average_score($exercise_id, $courseId, $session_id)
  2650. {
  2651. $user_results = Event::get_all_exercise_results(
  2652. $exercise_id,
  2653. $courseId,
  2654. $session_id
  2655. );
  2656. $avg_score = 0;
  2657. if (!empty($user_results)) {
  2658. foreach ($user_results as $result) {
  2659. if (!empty($result['exe_weighting']) && intval(
  2660. $result['exe_weighting']
  2661. ) != 0
  2662. ) {
  2663. $score = $result['exe_result'] / $result['exe_weighting'];
  2664. $avg_score += $score;
  2665. }
  2666. }
  2667. $avg_score = float_format($avg_score / count($user_results), 1);
  2668. }
  2669. return $avg_score;
  2670. }
  2671. /**
  2672. * Get average score by score (NO Exercises in LPs )
  2673. * @param int exercise id
  2674. * @param int $courseId
  2675. * @param int session id
  2676. * @return float Average score
  2677. */
  2678. public static function get_average_score_by_course($courseId, $session_id)
  2679. {
  2680. $user_results = Event::get_all_exercise_results_by_course(
  2681. $courseId,
  2682. $session_id,
  2683. false
  2684. );
  2685. //echo $course_code.' - '.$session_id.'<br />';
  2686. $avg_score = 0;
  2687. if (!empty($user_results)) {
  2688. foreach ($user_results as $result) {
  2689. if (!empty($result['exe_weighting']) && intval(
  2690. $result['exe_weighting']
  2691. ) != 0
  2692. ) {
  2693. $score = $result['exe_result'] / $result['exe_weighting'];
  2694. $avg_score += $score;
  2695. }
  2696. }
  2697. //We asume that all exe_weighting
  2698. $avg_score = ($avg_score / count($user_results));
  2699. }
  2700. return $avg_score;
  2701. }
  2702. /**
  2703. * @param int $user_id
  2704. * @param int $courseId
  2705. * @param int $session_id
  2706. *
  2707. * @return float|int
  2708. */
  2709. public static function get_average_score_by_course_by_user(
  2710. $user_id,
  2711. $courseId,
  2712. $session_id
  2713. )
  2714. {
  2715. $user_results = Event::get_all_exercise_results_by_user(
  2716. $user_id,
  2717. $courseId,
  2718. $session_id
  2719. );
  2720. $avg_score = 0;
  2721. if (!empty($user_results)) {
  2722. foreach ($user_results as $result) {
  2723. if (!empty($result['exe_weighting']) && intval(
  2724. $result['exe_weighting']
  2725. ) != 0
  2726. ) {
  2727. $score = $result['exe_result'] / $result['exe_weighting'];
  2728. $avg_score += $score;
  2729. }
  2730. }
  2731. // We asumme that all exe_weighting
  2732. $avg_score = ($avg_score / count($user_results));
  2733. }
  2734. return $avg_score;
  2735. }
  2736. /**
  2737. * Get average score by score (NO Exercises in LPs )
  2738. * @param int exercise id
  2739. * @param int $courseId
  2740. * @param int session id
  2741. * @return float Best average score
  2742. */
  2743. public static function get_best_average_score_by_exercise(
  2744. $exercise_id,
  2745. $courseId,
  2746. $session_id,
  2747. $user_count
  2748. )
  2749. {
  2750. $user_results = Event::get_best_exercise_results_by_user(
  2751. $exercise_id,
  2752. $courseId,
  2753. $session_id
  2754. );
  2755. $avg_score = 0;
  2756. if (!empty($user_results)) {
  2757. foreach ($user_results as $result) {
  2758. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  2759. $score = $result['exe_result'] / $result['exe_weighting'];
  2760. $avg_score += $score;
  2761. }
  2762. }
  2763. //We asumme that all exe_weighting
  2764. //$avg_score = show_score( $avg_score / count($user_results) , $result['exe_weighting']);
  2765. //$avg_score = ($avg_score / count($user_results));
  2766. if (!empty($user_count)) {
  2767. $avg_score = float_format($avg_score / $user_count, 1) * 100;
  2768. } else {
  2769. $avg_score = 0;
  2770. }
  2771. }
  2772. return $avg_score;
  2773. }
  2774. /**
  2775. * @param string $course_code
  2776. * @param int $session_id
  2777. *
  2778. * @return array
  2779. */
  2780. public static function get_exercises_to_be_taken($course_code, $session_id)
  2781. {
  2782. $course_info = api_get_course_info($course_code);
  2783. $exercises = self::get_all_exercises($course_info, $session_id);
  2784. $result = array();
  2785. $now = time() + 15 * 24 * 60 * 60;
  2786. foreach ($exercises as $exercise_item) {
  2787. if (isset($exercise_item['end_time']) &&
  2788. !empty($exercise_item['end_time']) &&
  2789. api_strtotime($exercise_item['end_time'], 'UTC') < $now
  2790. ) {
  2791. $result[] = $exercise_item;
  2792. }
  2793. }
  2794. return $result;
  2795. }
  2796. /**
  2797. * Get student results (only in completed exercises) stats by question
  2798. * @param int $question_id
  2799. * @param int $exercise_id
  2800. * @param string $course_code
  2801. * @param int $session_id
  2802. *
  2803. **/
  2804. public static function get_student_stats_by_question(
  2805. $question_id,
  2806. $exercise_id,
  2807. $course_code,
  2808. $session_id
  2809. ) {
  2810. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  2811. $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  2812. $question_id = intval($question_id);
  2813. $exercise_id = intval($exercise_id);
  2814. $course_code = Database::escape_string($course_code);
  2815. $session_id = intval($session_id);
  2816. $courseId = api_get_course_int_id($course_code);
  2817. $sql = "SELECT MAX(marks) as max, MIN(marks) as min, AVG(marks) as average
  2818. FROM $track_exercises e
  2819. INNER JOIN $track_attempt a
  2820. ON (
  2821. a.exe_id = e.exe_id AND
  2822. e.c_id = a.c_id AND
  2823. e.session_id = a.session_id
  2824. )
  2825. WHERE
  2826. exe_exo_id = $exercise_id AND
  2827. a.c_id = $courseId AND
  2828. e.session_id = $session_id AND
  2829. question_id = $question_id AND
  2830. status = ''
  2831. LIMIT 1";
  2832. $result = Database::query($sql);
  2833. $return = array();
  2834. if ($result) {
  2835. $return = Database::fetch_array($result, 'ASSOC');
  2836. }
  2837. return $return;
  2838. }
  2839. /**
  2840. * Get the correct answer count for a fill blanks question
  2841. *
  2842. * @param int $question_id
  2843. * @param int $exercise_id
  2844. * @return int
  2845. */
  2846. public static function getNumberStudentsFillBlanksAnwserCount(
  2847. $question_id,
  2848. $exercise_id
  2849. )
  2850. {
  2851. $listStudentsId = [];
  2852. $listAllStudentInfo = CourseManager::get_student_list_from_course_code(
  2853. api_get_course_id(),
  2854. true
  2855. );
  2856. foreach ($listAllStudentInfo as $i => $listStudentInfo) {
  2857. $listStudentsId[] = $listStudentInfo['user_id'];
  2858. }
  2859. $listFillTheBlankResult = FillBlanks::getFillTheBlankTabResult(
  2860. $exercise_id,
  2861. $question_id,
  2862. $listStudentsId,
  2863. '1970-01-01',
  2864. '3000-01-01'
  2865. );
  2866. $arrayCount = [];
  2867. foreach ($listFillTheBlankResult as $resultCount) {
  2868. foreach ($resultCount as $index => $count) {
  2869. //this is only for declare the array index per answer
  2870. $arrayCount[$index] = 0;
  2871. }
  2872. }
  2873. foreach ($listFillTheBlankResult as $resultCount) {
  2874. foreach ($resultCount as $index => $count) {
  2875. $count = ($count === 0) ? 1 : 0;
  2876. $arrayCount[$index] += $count;
  2877. }
  2878. }
  2879. return $arrayCount;
  2880. }
  2881. /**
  2882. * @param int $question_id
  2883. * @param int $exercise_id
  2884. * @param string $course_code
  2885. * @param int $session_id
  2886. * @param string $questionType
  2887. * @return int
  2888. */
  2889. public static function get_number_students_question_with_answer_count(
  2890. $question_id,
  2891. $exercise_id,
  2892. $course_code,
  2893. $session_id,
  2894. $questionType = ''
  2895. ) {
  2896. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  2897. $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  2898. $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2899. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  2900. $courseUserSession = Database::get_main_table(
  2901. TABLE_MAIN_SESSION_COURSE_USER
  2902. );
  2903. $question_id = intval($question_id);
  2904. $exercise_id = intval($exercise_id);
  2905. $courseId = api_get_course_int_id($course_code);
  2906. $session_id = intval($session_id);
  2907. if ($questionType == FILL_IN_BLANKS) {
  2908. $listStudentsId = array();
  2909. $listAllStudentInfo = CourseManager::get_student_list_from_course_code(
  2910. api_get_course_id(),
  2911. true
  2912. );
  2913. foreach ($listAllStudentInfo as $i => $listStudentInfo) {
  2914. $listStudentsId[] = $listStudentInfo['user_id'];
  2915. }
  2916. $listFillTheBlankResult = FillBlanks::getFillTheBlankTabResult(
  2917. $exercise_id,
  2918. $question_id,
  2919. $listStudentsId,
  2920. '1970-01-01',
  2921. '3000-01-01'
  2922. );
  2923. return FillBlanks::getNbResultFillBlankAll($listFillTheBlankResult);
  2924. }
  2925. if (empty($session_id)) {
  2926. $courseCondition = "
  2927. INNER JOIN $courseUser cu
  2928. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  2929. $courseConditionWhere = " AND relation_type <> 2 AND cu.status = ".STUDENT;
  2930. } else {
  2931. $courseCondition = "
  2932. INNER JOIN $courseUserSession cu
  2933. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  2934. $courseConditionWhere = " AND cu.status = 0 ";
  2935. }
  2936. $sql = "SELECT DISTINCT exe_user_id
  2937. FROM $track_exercises e
  2938. INNER JOIN $track_attempt a
  2939. ON (
  2940. a.exe_id = e.exe_id AND
  2941. e.c_id = a.c_id AND
  2942. e.session_id = a.session_id
  2943. )
  2944. INNER JOIN $courseTable c
  2945. ON (c.id = a.c_id)
  2946. $courseCondition
  2947. WHERE
  2948. exe_exo_id = $exercise_id AND
  2949. a.c_id = $courseId AND
  2950. e.session_id = $session_id AND
  2951. question_id = $question_id AND
  2952. answer <> '0' AND
  2953. e.status = ''
  2954. $courseConditionWhere
  2955. ";
  2956. $result = Database::query($sql);
  2957. $return = 0;
  2958. if ($result) {
  2959. $return = Database::num_rows($result);
  2960. }
  2961. return $return;
  2962. }
  2963. /**
  2964. * @param int $answer_id
  2965. * @param int $question_id
  2966. * @param int $exercise_id
  2967. * @param string $course_code
  2968. * @param int $session_id
  2969. *
  2970. * @return int
  2971. */
  2972. public static function get_number_students_answer_hotspot_count(
  2973. $answer_id,
  2974. $question_id,
  2975. $exercise_id,
  2976. $course_code,
  2977. $session_id
  2978. )
  2979. {
  2980. $track_exercises = Database::get_main_table(
  2981. TABLE_STATISTIC_TRACK_E_EXERCISES
  2982. );
  2983. $track_hotspot = Database::get_main_table(
  2984. TABLE_STATISTIC_TRACK_E_HOTSPOT
  2985. );
  2986. $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2987. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  2988. $courseUserSession = Database::get_main_table(
  2989. TABLE_MAIN_SESSION_COURSE_USER
  2990. );
  2991. $question_id = intval($question_id);
  2992. $answer_id = intval($answer_id);
  2993. $exercise_id = intval($exercise_id);
  2994. $course_code = Database::escape_string($course_code);
  2995. $session_id = intval($session_id);
  2996. if (empty($session_id)) {
  2997. $courseCondition = "
  2998. INNER JOIN $courseUser cu
  2999. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  3000. $courseConditionWhere = " AND relation_type <> 2 AND cu.status = ".STUDENT;
  3001. } else {
  3002. $courseCondition = "
  3003. INNER JOIN $courseUserSession cu
  3004. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  3005. $courseConditionWhere = " AND cu.status = 0 ";
  3006. }
  3007. $sql = "SELECT DISTINCT exe_user_id
  3008. FROM $track_exercises e
  3009. INNER JOIN $track_hotspot a
  3010. ON (a.hotspot_exe_id = e.exe_id)
  3011. INNER JOIN $courseTable c
  3012. ON (hotspot_course_code = c.code)
  3013. $courseCondition
  3014. WHERE
  3015. exe_exo_id = $exercise_id AND
  3016. a.hotspot_course_code = '$course_code' AND
  3017. e.session_id = $session_id AND
  3018. hotspot_answer_id = $answer_id AND
  3019. hotspot_question_id = $question_id AND
  3020. hotspot_correct = 1 AND
  3021. e.status = ''
  3022. $courseConditionWhere
  3023. ";
  3024. $result = Database::query($sql);
  3025. $return = 0;
  3026. if ($result) {
  3027. $return = Database::num_rows($result);
  3028. }
  3029. return $return;
  3030. }
  3031. /**
  3032. * @param int $answer_id
  3033. * @param int $question_id
  3034. * @param int $exercise_id
  3035. * @param string $course_code
  3036. * @param int $session_id
  3037. * @param string $question_type
  3038. * @param string $correct_answer
  3039. * @param string $current_answer
  3040. * @return int
  3041. */
  3042. public static function get_number_students_answer_count(
  3043. $answer_id,
  3044. $question_id,
  3045. $exercise_id,
  3046. $course_code,
  3047. $session_id,
  3048. $question_type = null,
  3049. $correct_answer = null,
  3050. $current_answer = null
  3051. ) {
  3052. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  3053. $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  3054. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  3055. $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  3056. $courseUserSession = Database::get_main_table(
  3057. TABLE_MAIN_SESSION_COURSE_USER
  3058. );
  3059. $question_id = intval($question_id);
  3060. $answer_id = intval($answer_id);
  3061. $exercise_id = intval($exercise_id);
  3062. $courseId = api_get_course_int_id($course_code);
  3063. $course_code = Database::escape_string($course_code);
  3064. $session_id = intval($session_id);
  3065. switch ($question_type) {
  3066. case FILL_IN_BLANKS:
  3067. $answer_condition = "";
  3068. $select_condition = " e.exe_id, answer ";
  3069. break;
  3070. case MATCHING:
  3071. //no break
  3072. case MATCHING_DRAGGABLE:
  3073. //no break
  3074. default:
  3075. $answer_condition = " answer = $answer_id AND ";
  3076. $select_condition = " DISTINCT exe_user_id ";
  3077. }
  3078. if (empty($session_id)) {
  3079. $courseCondition = "
  3080. INNER JOIN $courseUser cu
  3081. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  3082. $courseConditionWhere = " AND relation_type <> 2 AND cu.status = ".STUDENT;
  3083. } else {
  3084. $courseCondition = "
  3085. INNER JOIN $courseUserSession cu
  3086. ON cu.c_id = a.c_id AND cu.user_id = exe_user_id";
  3087. $courseConditionWhere = " AND cu.status = 0 ";
  3088. }
  3089. $sql = "SELECT $select_condition
  3090. FROM $track_exercises e
  3091. INNER JOIN $track_attempt a
  3092. ON (
  3093. a.exe_id = e.exe_id AND
  3094. e.c_id = a.c_id AND
  3095. e.session_id = a.session_id
  3096. )
  3097. INNER JOIN $courseTable c
  3098. ON c.id = a.c_id
  3099. $courseCondition
  3100. WHERE
  3101. exe_exo_id = $exercise_id AND
  3102. a.c_id = $courseId AND
  3103. e.session_id = $session_id AND
  3104. $answer_condition
  3105. question_id = $question_id AND
  3106. e.status = ''
  3107. $courseConditionWhere
  3108. ";
  3109. $result = Database::query($sql);
  3110. $return = 0;
  3111. if ($result) {
  3112. $good_answers = 0;
  3113. switch ($question_type) {
  3114. case FILL_IN_BLANKS:
  3115. while ($row = Database::fetch_array($result, 'ASSOC')) {
  3116. $fill_blank = self::check_fill_in_blanks(
  3117. $correct_answer,
  3118. $row['answer'],
  3119. $current_answer
  3120. );
  3121. if (isset($fill_blank[$current_answer]) && $fill_blank[$current_answer] == 1) {
  3122. $good_answers++;
  3123. }
  3124. }
  3125. return $good_answers;
  3126. break;
  3127. case MATCHING:
  3128. //no break
  3129. case MATCHING_DRAGGABLE:
  3130. //no break
  3131. default:
  3132. $return = Database::num_rows($result);
  3133. }
  3134. }
  3135. return $return;
  3136. }
  3137. /**
  3138. * @param array $answer
  3139. * @param string $user_answer
  3140. * @return array
  3141. */
  3142. public static function check_fill_in_blanks($answer, $user_answer, $current_answer)
  3143. {
  3144. // the question is encoded like this
  3145. // [A] B [C] D [E] F::10,10,10@1
  3146. // number 1 before the "@" means that is a switchable fill in blank question
  3147. // [A] B [C] D [E] F::10,10,10@ or [A] B [C] D [E] F::10,10,10
  3148. // means that is a normal fill blank question
  3149. // first we explode the "::"
  3150. $pre_array = explode('::', $answer);
  3151. // is switchable fill blank or not
  3152. $last = count($pre_array) - 1;
  3153. $is_set_switchable = explode('@', $pre_array[$last]);
  3154. $switchable_answer_set = false;
  3155. if (isset ($is_set_switchable[1]) && $is_set_switchable[1] == 1) {
  3156. $switchable_answer_set = true;
  3157. }
  3158. $answer = '';
  3159. for ($k = 0; $k < $last; $k++) {
  3160. $answer .= $pre_array[$k];
  3161. }
  3162. // splits weightings that are joined with a comma
  3163. $answerWeighting = explode(',', $is_set_switchable[0]);
  3164. // we save the answer because it will be modified
  3165. //$temp = $answer;
  3166. $temp = $answer;
  3167. $answer = '';
  3168. $j = 0;
  3169. //initialise answer tags
  3170. $user_tags = $correct_tags = $real_text = array();
  3171. // the loop will stop at the end of the text
  3172. while (1) {
  3173. // quits the loop if there are no more blanks (detect '[')
  3174. if (($pos = api_strpos($temp, '[')) === false) {
  3175. // adds the end of the text
  3176. $answer = $temp;
  3177. $real_text[] = $answer;
  3178. break; //no more "blanks", quit the loop
  3179. }
  3180. // adds the piece of text that is before the blank
  3181. //and ends with '[' into a general storage array
  3182. $real_text[] = api_substr($temp, 0, $pos + 1);
  3183. $answer .= api_substr($temp, 0, $pos + 1);
  3184. //take the string remaining (after the last "[" we found)
  3185. $temp = api_substr($temp, $pos + 1);
  3186. // quit the loop if there are no more blanks, and update $pos to the position of next ']'
  3187. if (($pos = api_strpos($temp, ']')) === false) {
  3188. // adds the end of the text
  3189. $answer .= $temp;
  3190. break;
  3191. }
  3192. $str = $user_answer;
  3193. preg_match_all('#\[([^[]*)\]#', $str, $arr);
  3194. $str = str_replace('\r\n', '', $str);
  3195. $choices = $arr[1];
  3196. $choice = [];
  3197. $check = false;
  3198. $i = 0;
  3199. foreach ($choices as $item) {
  3200. if ($current_answer === $item) {
  3201. $check = true;
  3202. }
  3203. if ($check) {
  3204. $choice[] = $item;
  3205. $i++;
  3206. }
  3207. if ($i == 3) {
  3208. break;
  3209. }
  3210. }
  3211. $tmp = api_strrpos($choice[$j], ' / ');
  3212. if ($tmp !== false) {
  3213. $choice[$j] = api_substr($choice[$j], 0, $tmp);
  3214. }
  3215. $choice[$j] = trim($choice[$j]);
  3216. //Needed to let characters ' and " to work as part of an answer
  3217. $choice[$j] = stripslashes($choice[$j]);
  3218. $user_tags[] = api_strtolower($choice[$j]);
  3219. //put the contents of the [] answer tag into correct_tags[]
  3220. $correct_tags[] = api_strtolower(api_substr($temp, 0, $pos));
  3221. $j++;
  3222. $temp = api_substr($temp, $pos + 1);
  3223. }
  3224. $answer = '';
  3225. $real_correct_tags = $correct_tags;
  3226. $chosen_list = array();
  3227. $good_answer = array();
  3228. for ($i = 0; $i < count($real_correct_tags); $i++) {
  3229. if (!$switchable_answer_set) {
  3230. //needed to parse ' and " characters
  3231. $user_tags[$i] = stripslashes($user_tags[$i]);
  3232. if ($correct_tags[$i] == $user_tags[$i]) {
  3233. $good_answer[$correct_tags[$i]] = 1;
  3234. } elseif (!empty ($user_tags[$i])) {
  3235. $good_answer[$correct_tags[$i]] = 0;
  3236. } else {
  3237. $good_answer[$correct_tags[$i]] = 0;
  3238. }
  3239. } else {
  3240. // switchable fill in the blanks
  3241. if (in_array($user_tags[$i], $correct_tags)) {
  3242. $correct_tags = array_diff($correct_tags, $chosen_list);
  3243. $good_answer[$correct_tags[$i]] = 1;
  3244. } elseif (!empty ($user_tags[$i])) {
  3245. $good_answer[$correct_tags[$i]] = 0;
  3246. } else {
  3247. $good_answer[$correct_tags[$i]] = 0;
  3248. }
  3249. }
  3250. // adds the correct word, followed by ] to close the blank
  3251. $answer .= ' / <font color="green"><b>'.$real_correct_tags[$i].'</b></font>]';
  3252. if (isset ($real_text[$i + 1])) {
  3253. $answer .= $real_text[$i + 1];
  3254. }
  3255. }
  3256. return $good_answer;
  3257. }
  3258. /**
  3259. * @param int $exercise_id
  3260. * @param string $course_code
  3261. * @param int $session_id
  3262. * @return int
  3263. */
  3264. public static function get_number_students_finish_exercise(
  3265. $exercise_id,
  3266. $course_code,
  3267. $session_id
  3268. ) {
  3269. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  3270. $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  3271. $exercise_id = intval($exercise_id);
  3272. $course_code = Database::escape_string($course_code);
  3273. $session_id = intval($session_id);
  3274. $sql = "SELECT DISTINCT exe_user_id
  3275. FROM $track_exercises e
  3276. INNER JOIN $track_attempt a
  3277. ON (a.exe_id = e.exe_id)
  3278. WHERE
  3279. exe_exo_id = $exercise_id AND
  3280. course_code = '$course_code' AND
  3281. e.session_id = $session_id AND
  3282. status = ''";
  3283. $result = Database::query($sql);
  3284. $return = 0;
  3285. if ($result) {
  3286. $return = Database::num_rows($result);
  3287. }
  3288. return $return;
  3289. }
  3290. /**
  3291. * @param string $in_name is the name and the id of the <select>
  3292. * @param string $in_default default value for option
  3293. * @param string $in_onchange
  3294. * @return string the html code of the <select>
  3295. */
  3296. public static function displayGroupMenu($in_name, $in_default, $in_onchange = "")
  3297. {
  3298. // check the default value of option
  3299. $tabSelected = array($in_default => " selected='selected' ");
  3300. $res = "";
  3301. $res .= "<select name='$in_name' id='$in_name' onchange='".$in_onchange."' >";
  3302. $res .= "<option value='-1'".$tabSelected["-1"].">-- ".get_lang(
  3303. 'AllGroups'
  3304. )." --</option>";
  3305. $res .= "<option value='0'".$tabSelected["0"].">- ".get_lang(
  3306. 'NotInAGroup'
  3307. )." -</option>";
  3308. $tabGroups = GroupManager::get_group_list();
  3309. $currentCatId = 0;
  3310. for ($i = 0; $i < count($tabGroups); $i++) {
  3311. $tabCategory = GroupManager::get_category_from_group(
  3312. $tabGroups[$i]['iid']
  3313. );
  3314. if ($tabCategory["id"] != $currentCatId) {
  3315. $res .= "<option value='-1' disabled='disabled'>".$tabCategory["title"]."</option>";
  3316. $currentCatId = $tabCategory["id"];
  3317. }
  3318. $res .= "<option ".$tabSelected[$tabGroups[$i]["id"]]."style='margin-left:40px' value='".$tabGroups[$i]["id"]."'>".$tabGroups[$i]["name"]."</option>";
  3319. }
  3320. $res .= "</select>";
  3321. return $res;
  3322. }
  3323. /**
  3324. * @param int $exe_id
  3325. */
  3326. public static function create_chat_exercise_session($exe_id)
  3327. {
  3328. if (!isset($_SESSION['current_exercises'])) {
  3329. $_SESSION['current_exercises'] = array();
  3330. }
  3331. $_SESSION['current_exercises'][$exe_id] = true;
  3332. }
  3333. /**
  3334. * @param int $exe_id
  3335. */
  3336. public static function delete_chat_exercise_session($exe_id)
  3337. {
  3338. if (isset($_SESSION['current_exercises'])) {
  3339. $_SESSION['current_exercises'][$exe_id] = false;
  3340. }
  3341. }
  3342. /**
  3343. * Display the exercise results
  3344. * @param Exercise $objExercise
  3345. * @param int $exe_id
  3346. * @param bool $save_user_result save users results (true) or just show the results (false)
  3347. * @param string $remainingMessage
  3348. */
  3349. public static function displayQuestionListByAttempt(
  3350. $objExercise,
  3351. $exe_id,
  3352. $save_user_result = false,
  3353. $remainingMessage = ''
  3354. ) {
  3355. $origin = api_get_origin();
  3356. // Getting attempt info
  3357. $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id(
  3358. $exe_id
  3359. );
  3360. // Getting question list
  3361. $question_list = array();
  3362. if (!empty($exercise_stat_info['data_tracking'])) {
  3363. $question_list = explode(',', $exercise_stat_info['data_tracking']);
  3364. } else {
  3365. // Try getting the question list only if save result is off
  3366. if ($save_user_result == false) {
  3367. $question_list = $objExercise->get_validated_question_list();
  3368. }
  3369. if ($objExercise->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  3370. $question_list = $objExercise->get_validated_question_list();
  3371. }
  3372. }
  3373. $counter = 1;
  3374. $total_score = $total_weight = 0;
  3375. $exercise_content = null;
  3376. // Hide results
  3377. $show_results = false;
  3378. $show_only_score = false;
  3379. if ($objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS) {
  3380. $show_results = true;
  3381. }
  3382. if (in_array(
  3383. $objExercise->results_disabled,
  3384. array(
  3385. RESULT_DISABLE_SHOW_SCORE_ONLY,
  3386. RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES
  3387. )
  3388. )
  3389. ) {
  3390. $show_only_score = true;
  3391. }
  3392. // Not display expected answer, but score, and feedback
  3393. $show_all_but_expected_answer = false;
  3394. if ($objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ONLY &&
  3395. $objExercise->feedback_type == EXERCISE_FEEDBACK_TYPE_END
  3396. ) {
  3397. $show_all_but_expected_answer = true;
  3398. $show_results = true;
  3399. $show_only_score = false;
  3400. }
  3401. $showTotalScoreAndUserChoicesInLastAttempt = true;
  3402. if ($objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  3403. $show_only_score = true;
  3404. $show_results = true;
  3405. if ($objExercise->attempts > 0) {
  3406. $attempts = Event::getExerciseResultsByUser(
  3407. api_get_user_id(),
  3408. $objExercise->id,
  3409. api_get_course_int_id(),
  3410. api_get_session_id(),
  3411. $exercise_stat_info['orig_lp_id'],
  3412. $exercise_stat_info['orig_lp_item_id'],
  3413. 'desc'
  3414. );
  3415. if ($attempts) {
  3416. $numberAttempts = count($attempts);
  3417. } else {
  3418. $numberAttempts = 0;
  3419. }
  3420. if ($save_user_result) {
  3421. $numberAttempts++;
  3422. }
  3423. if ($numberAttempts >= $objExercise->attempts) {
  3424. $show_results = true;
  3425. $show_only_score = false;
  3426. $showTotalScoreAndUserChoicesInLastAttempt = true;
  3427. } else {
  3428. $showTotalScoreAndUserChoicesInLastAttempt = false;
  3429. }
  3430. }
  3431. }
  3432. if ($show_results || $show_only_score) {
  3433. if (isset($exercise_stat_info['exe_user_id'])) {
  3434. $user_info = api_get_user_info($exercise_stat_info['exe_user_id']);
  3435. if ($user_info) {
  3436. // Shows exercise header
  3437. echo $objExercise->show_exercise_result_header(
  3438. $user_info,
  3439. api_convert_and_format_date(
  3440. $exercise_stat_info['start_date'],
  3441. DATE_TIME_FORMAT_LONG
  3442. ),
  3443. $exercise_stat_info['duration'],
  3444. $exercise_stat_info['user_ip']
  3445. );
  3446. }
  3447. }
  3448. }
  3449. // Display text when test is finished #4074 and for LP #4227
  3450. $end_of_message = $objExercise->selectTextWhenFinished();
  3451. if (!empty($end_of_message)) {
  3452. echo Display::return_message($end_of_message, 'normal', false);
  3453. echo "<div class='clear'>&nbsp;</div>";
  3454. }
  3455. $question_list_answers = array();
  3456. $media_list = array();
  3457. $category_list = array();
  3458. $loadChoiceFromSession = false;
  3459. $fromDatabase = true;
  3460. $exerciseResult = null;
  3461. $exerciseResultCoordinates = null;
  3462. $delineationResults = null;
  3463. if ($objExercise->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  3464. $loadChoiceFromSession = true;
  3465. $fromDatabase = false;
  3466. $exerciseResult = Session::read('exerciseResult');
  3467. $exerciseResultCoordinates = Session::read('exerciseResultCoordinates');
  3468. $delineationResults = Session::read('hotspot_delineation_result');
  3469. $delineationResults = isset($delineationResults[$objExercise->id]) ? $delineationResults[$objExercise->id] : null;
  3470. }
  3471. $countPendingQuestions = 0;
  3472. // Loop over all question to show results for each of them, one by one
  3473. if (!empty($question_list)) {
  3474. foreach ($question_list as $questionId) {
  3475. // creates a temporary Question object
  3476. $objQuestionTmp = Question::read($questionId);
  3477. // This variable came from exercise_submit_modal.php
  3478. ob_start();
  3479. $choice = null;
  3480. $delineationChoice = null;
  3481. if ($loadChoiceFromSession) {
  3482. $choice = isset($exerciseResult[$questionId]) ? $exerciseResult[$questionId] : null;
  3483. $delineationChoice = isset($delineationResults[$questionId]) ? $delineationResults[$questionId] : null;
  3484. }
  3485. // We're inside *one* question. Go through each possible answer for this question
  3486. $result = $objExercise->manage_answer(
  3487. $exe_id,
  3488. $questionId,
  3489. $choice,
  3490. 'exercise_result',
  3491. $exerciseResultCoordinates,
  3492. $save_user_result,
  3493. $fromDatabase,
  3494. $show_results,
  3495. $objExercise->selectPropagateNeg(),
  3496. $delineationChoice,
  3497. $showTotalScoreAndUserChoicesInLastAttempt
  3498. );
  3499. if (empty($result)) {
  3500. continue;
  3501. }
  3502. $total_score += $result['score'];
  3503. $total_weight += $result['weight'];
  3504. $question_list_answers[] = array(
  3505. 'question' => $result['open_question'],
  3506. 'answer' => $result['open_answer'],
  3507. 'answer_type' => $result['answer_type']
  3508. );
  3509. $my_total_score = $result['score'];
  3510. $my_total_weight = $result['weight'];
  3511. // Category report
  3512. $category_was_added_for_this_test = false;
  3513. if (isset($objQuestionTmp->category) && !empty($objQuestionTmp->category)) {
  3514. if (!isset($category_list[$objQuestionTmp->category]['score'])) {
  3515. $category_list[$objQuestionTmp->category]['score'] = 0;
  3516. }
  3517. if (!isset($category_list[$objQuestionTmp->category]['total'])) {
  3518. $category_list[$objQuestionTmp->category]['total'] = 0;
  3519. }
  3520. $category_list[$objQuestionTmp->category]['score'] += $my_total_score;
  3521. $category_list[$objQuestionTmp->category]['total'] += $my_total_weight;
  3522. $category_was_added_for_this_test = true;
  3523. }
  3524. if (isset($objQuestionTmp->category_list) && !empty($objQuestionTmp->category_list)) {
  3525. foreach ($objQuestionTmp->category_list as $category_id) {
  3526. $category_list[$category_id]['score'] += $my_total_score;
  3527. $category_list[$category_id]['total'] += $my_total_weight;
  3528. $category_was_added_for_this_test = true;
  3529. }
  3530. }
  3531. // No category for this question!
  3532. if ($category_was_added_for_this_test == false) {
  3533. if (!isset($category_list['none']['score'])) {
  3534. $category_list['none']['score'] = 0;
  3535. }
  3536. if (!isset($category_list['none']['total'])) {
  3537. $category_list['none']['total'] = 0;
  3538. }
  3539. $category_list['none']['score'] += $my_total_score;
  3540. $category_list['none']['total'] += $my_total_weight;
  3541. }
  3542. if ($objExercise->selectPropagateNeg() == 0 &&
  3543. $my_total_score < 0
  3544. ) {
  3545. $my_total_score = 0;
  3546. }
  3547. $comnt = null;
  3548. if ($show_results) {
  3549. $comnt = Event::get_comments($exe_id, $questionId);
  3550. if (!empty($comnt)) {
  3551. echo '<b>'.get_lang('Feedback').'</b>';
  3552. echo ExerciseLib::getFeedbackText($comnt);
  3553. }
  3554. }
  3555. if ($show_results) {
  3556. $score = [
  3557. 'result' => self::show_score(
  3558. $my_total_score,
  3559. $my_total_weight,
  3560. false,
  3561. true
  3562. ),
  3563. 'pass' => $my_total_score >= $my_total_weight ? true : false,
  3564. 'score' => $my_total_score,
  3565. 'weight' => $my_total_weight,
  3566. 'comments' => $comnt,
  3567. ];
  3568. } else {
  3569. $score = [];
  3570. }
  3571. if (in_array($objQuestionTmp->type, [FREE_ANSWER, ORAL_EXPRESSION])) {
  3572. $check = $objQuestionTmp->isQuestionWaitingReview($score);
  3573. if ($check === false) {
  3574. $countPendingQuestions++;
  3575. }
  3576. }
  3577. $contents = ob_get_clean();
  3578. $question_content = '';
  3579. if ($show_results) {
  3580. $question_content = '<div class="question_row_answer">';
  3581. // Shows question title an description
  3582. $question_content .= $objQuestionTmp->return_header(
  3583. $objExercise,
  3584. $counter,
  3585. $score
  3586. );
  3587. }
  3588. $counter++;
  3589. $question_content .= $contents;
  3590. if ($show_results) {
  3591. $question_content .= '</div>';
  3592. }
  3593. $exercise_content .= $question_content;
  3594. } // end foreach() block that loops over all questions
  3595. }
  3596. $total_score_text = null;
  3597. if ($show_results || $show_only_score) {
  3598. $total_score_text .= '<div class="question_row_score">';
  3599. $total_score_text .= self::getTotalScoreRibbon(
  3600. $objExercise,
  3601. $total_score,
  3602. $total_weight,
  3603. true,
  3604. $countPendingQuestions
  3605. );
  3606. $total_score_text .= '</div>';
  3607. }
  3608. if (!empty($category_list) && ($show_results || $show_only_score)) {
  3609. // Adding total
  3610. $category_list['total'] = array(
  3611. 'score' => $total_score,
  3612. 'total' => $total_weight
  3613. );
  3614. echo TestCategory::get_stats_table_by_attempt(
  3615. $objExercise->id,
  3616. $category_list
  3617. );
  3618. }
  3619. if ($show_all_but_expected_answer) {
  3620. $exercise_content .= "<div class='normal-message'>".get_lang(
  3621. "ExerciseWithFeedbackWithoutCorrectionComment"
  3622. )."</div>";
  3623. }
  3624. // Remove audio auto play from questions on results page - refs BT#7939
  3625. $exercise_content = preg_replace(
  3626. ['/autoplay[\=\".+\"]+/', '/autostart[\=\".+\"]+/'],
  3627. '',
  3628. $exercise_content
  3629. );
  3630. echo $total_score_text;
  3631. // Ofaj change BT#11784
  3632. if (!empty($objExercise->description)) {
  3633. echo Display::div($objExercise->description, array('class'=>'exercise_description'));
  3634. }
  3635. echo $exercise_content;
  3636. if (!$show_only_score) {
  3637. echo $total_score_text;
  3638. }
  3639. if (!empty($remainingMessage)) {
  3640. echo Display::return_message($remainingMessage, 'normal', false);
  3641. }
  3642. if ($save_user_result) {
  3643. // Tracking of results
  3644. if ($exercise_stat_info) {
  3645. $learnpath_id = $exercise_stat_info['orig_lp_id'];
  3646. $learnpath_item_id = $exercise_stat_info['orig_lp_item_id'];
  3647. $learnpath_item_view_id = $exercise_stat_info['orig_lp_item_view_id'];
  3648. if (api_is_allowed_to_session_edit()) {
  3649. Event::update_event_exercise(
  3650. $exercise_stat_info['exe_id'],
  3651. $objExercise->selectId(),
  3652. $total_score,
  3653. $total_weight,
  3654. api_get_session_id(),
  3655. $learnpath_id,
  3656. $learnpath_item_id,
  3657. $learnpath_item_view_id,
  3658. $exercise_stat_info['exe_duration'],
  3659. $question_list,
  3660. '',
  3661. array()
  3662. );
  3663. }
  3664. }
  3665. // Send notification at the end
  3666. if (!api_is_allowed_to_edit(null, true) &&
  3667. !api_is_excluded_user_type()
  3668. ) {
  3669. $objExercise->send_mail_notification_for_exam(
  3670. 'end',
  3671. $question_list_answers,
  3672. $origin,
  3673. $exe_id,
  3674. $total_score,
  3675. $total_weight
  3676. );
  3677. }
  3678. }
  3679. }
  3680. /**
  3681. * @param string $class
  3682. * @param string $scoreLabel
  3683. * @param string $result
  3684. *
  3685. * @return string
  3686. */
  3687. public static function getQuestionRibbon($class, $scoreLabel, $result)
  3688. {
  3689. // ofaj
  3690. $hideLabel = api_get_configuration_value('exercise_hide_label');
  3691. $label = '<div class="rib rib-'.$class.'">
  3692. <h3>'.$scoreLabel.'</h3>
  3693. </div>
  3694. <h4>'.get_lang('Score').': '.$result.'</h4>';
  3695. if ($hideLabel === true) {
  3696. $label = "<h4>{$result}<h4/>";
  3697. }
  3698. return '<div class="ribbon">
  3699. '.$label.'
  3700. </div>'
  3701. ;
  3702. }
  3703. /**
  3704. * @param Exercise $objExercise
  3705. * @param float $score
  3706. * @param float $weight
  3707. * @param bool $checkPassPercentage
  3708. * @param int $countPendingQuestions
  3709. * @return string
  3710. */
  3711. public static function getTotalScoreRibbon(
  3712. $objExercise,
  3713. $score,
  3714. $weight,
  3715. $checkPassPercentage = false,
  3716. $countPendingQuestions = 0
  3717. ) {
  3718. $passPercentage = $objExercise->selectPassPercentage();
  3719. $ribbon = '<div class="title-score">';
  3720. if ($checkPassPercentage) {
  3721. $isSuccess = self::isSuccessExerciseResult(
  3722. $score,
  3723. $weight,
  3724. $passPercentage
  3725. );
  3726. // Color the final test score if pass_percentage activated
  3727. $class = '';
  3728. if (self::isPassPercentageEnabled($passPercentage)) {
  3729. if ($isSuccess) {
  3730. $class = ' ribbon-total-success';
  3731. } else {
  3732. $class = ' ribbon-total-error';
  3733. }
  3734. }
  3735. $ribbon .= '<div class="total '.$class.'">';
  3736. } else {
  3737. $ribbon .= '<div class="total">';
  3738. }
  3739. $ribbon .= '<h3>'.get_lang('YourTotalScore').":&nbsp;";
  3740. $ribbon .= self::show_score($score, $weight, false, true);
  3741. $ribbon .= '</h3>';
  3742. $ribbon .= '</div>';
  3743. if ($checkPassPercentage) {
  3744. $ribbon .= self::showSuccessMessage(
  3745. $score,
  3746. $weight,
  3747. $passPercentage
  3748. );
  3749. }
  3750. $ribbon .= '</div>';
  3751. if (!empty($countPendingQuestions)) {
  3752. $ribbon .= '<br />';
  3753. $ribbon .= Display::return_message(
  3754. sprintf(
  3755. get_lang('TempScoreXQuestionsNotCorrectedYet'),
  3756. $countPendingQuestions
  3757. ),
  3758. 'warning'
  3759. );
  3760. }
  3761. return $ribbon;
  3762. }
  3763. /**
  3764. * @param int $countLetter
  3765. * @return mixed
  3766. */
  3767. public static function detectInputAppropriateClass($countLetter)
  3768. {
  3769. $limits = array(
  3770. 0 => 'input-mini',
  3771. 10 => 'input-mini',
  3772. 15 => 'input-medium',
  3773. 20 => 'input-xlarge',
  3774. 40 => 'input-xlarge',
  3775. 60 => 'input-xxlarge',
  3776. 100 => 'input-xxlarge',
  3777. 200 => 'input-xxlarge',
  3778. );
  3779. foreach ($limits as $size => $item) {
  3780. if ($countLetter <= $size) {
  3781. return $item;
  3782. }
  3783. }
  3784. return $limits[0];
  3785. }
  3786. /**
  3787. * @param int $senderId
  3788. * @param array $course_info
  3789. * @param string $test
  3790. * @param string $url
  3791. *
  3792. * @return string
  3793. */
  3794. public static function getEmailNotification($senderId, $course_info, $test, $url)
  3795. {
  3796. $teacher_info = api_get_user_info($senderId);
  3797. $from_name = api_get_person_name(
  3798. $teacher_info['firstname'],
  3799. $teacher_info['lastname'],
  3800. null,
  3801. PERSON_NAME_EMAIL_ADDRESS
  3802. );
  3803. $message = '<p>'.get_lang('DearStudentEmailIntroduction').'</p><p>'.get_lang('AttemptVCC');
  3804. $message .= '<h3>'.get_lang('CourseName').'</h3><p>'.Security::remove_XSS($course_info['name']).'';
  3805. $message .= '<h3>'.get_lang('Exercise').'</h3><p>'.Security::remove_XSS($test);
  3806. $message .= '<p>'.get_lang('ClickLinkToViewComment').' <br /><a href="#url#">#url#</a><br />';
  3807. $message .= '<p>'.get_lang('Regards').'</p>';
  3808. $message .= $from_name;
  3809. $message = str_replace("#test#", Security::remove_XSS($test), $message);
  3810. $message = str_replace("#url#", $url, $message);
  3811. return $message;
  3812. }
  3813. /**
  3814. * @return string
  3815. */
  3816. public static function getNotCorrectedYetText()
  3817. {
  3818. return Display::return_message(get_lang('notCorrectedYet'), 'warning');
  3819. }
  3820. /**
  3821. * @param string $message
  3822. * @return string
  3823. */
  3824. public static function getFeedbackText($message)
  3825. {
  3826. // Old style
  3827. //return '<div id="question_feedback">'.$message.'</div>';
  3828. return Display::return_message($message, 'warning', false);
  3829. }
  3830. }