exercise.lib.php 151 KB

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