exercise.lib.php 151 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918
  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="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="thumbnail" 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-xs-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-sm-4 well'
  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 . '&amp;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 . '&amp;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. $course_code = api_get_course_id();
  1555. $sessionId = api_get_session_id();
  1556. $is_allowedToEdit = api_is_allowed_to_edit(
  1557. null,
  1558. true
  1559. ) || api_is_allowed_to_edit(true) || api_is_drh(
  1560. ) || api_is_student_boss();
  1561. $TBL_USER = Database:: get_main_table(TABLE_MAIN_USER);
  1562. $TBL_EXERCICES = Database:: get_course_table(TABLE_QUIZ_TEST);
  1563. $TBL_GROUP_REL_USER = Database:: get_course_table(TABLE_GROUP_USER);
  1564. $TBL_GROUP = Database:: get_course_table(TABLE_GROUP);
  1565. $TBL_TRACK_EXERCICES = Database:: get_main_table(
  1566. TABLE_STATISTIC_TRACK_E_EXERCISES
  1567. );
  1568. $TBL_TRACK_HOTPOTATOES = Database:: get_main_table(
  1569. TABLE_STATISTIC_TRACK_E_HOTPOTATOES
  1570. );
  1571. $TBL_TRACK_ATTEMPT_RECORDING = Database:: get_main_table(
  1572. TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING
  1573. );
  1574. $session_id_and = ' AND te.session_id = ' . $sessionId . ' ';
  1575. $exercise_id = intval($exercise_id);
  1576. $exercise_where = '';
  1577. if (!empty($exercise_id)) {
  1578. $exercise_where .= ' AND te.exe_exo_id = ' . $exercise_id . ' ';
  1579. }
  1580. $hotpotatoe_where = '';
  1581. if (!empty($_GET['path'])) {
  1582. $hotpotatoe_path = Database::escape_string($_GET['path']);
  1583. $hotpotatoe_where .= ' AND exe_name = "' . $hotpotatoe_path . '" ';
  1584. }
  1585. // sql for chamilo-type tests for teacher / tutor view
  1586. $sql_inner_join_tbl_track_exercices = " (
  1587. SELECT DISTINCT ttte.*, if(tr.exe_id,1, 0) as revised
  1588. FROM $TBL_TRACK_EXERCICES ttte LEFT JOIN $TBL_TRACK_ATTEMPT_RECORDING tr
  1589. ON (ttte.exe_id = tr.exe_id)
  1590. WHERE
  1591. c_id = $course_id AND
  1592. exe_exo_id = $exercise_id AND
  1593. ttte.session_id = " . $sessionId . "
  1594. )";
  1595. if ($is_allowedToEdit) {
  1596. //Teacher view
  1597. if (isset($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
  1598. //$exercise_where_query = ' te.exe_exo_id = ce.id AND ';
  1599. }
  1600. $sqlFromOption = "";
  1601. $sqlWhereOption = ""; // for hpsql
  1602. //@todo fix to work with COURSE_RELATION_TYPE_RRHH in both queries
  1603. //Hack in order to filter groups
  1604. $sql_inner_join_tbl_user = '';
  1605. if (strpos($extra_where_conditions, 'group_id')) {
  1606. $sql_inner_join_tbl_user = "
  1607. (
  1608. SELECT u.user_id, firstname, lastname, email, username, g.name as group_name, g.id as group_id
  1609. FROM $TBL_USER u
  1610. INNER JOIN $TBL_GROUP_REL_USER gru ON ( gru.user_id = u.user_id AND gru.c_id=" . $course_id . ")
  1611. INNER JOIN $TBL_GROUP g ON (gru.group_id = g.id AND g.c_id=" . $course_id . ")
  1612. )";
  1613. }
  1614. if (strpos($extra_where_conditions, 'group_all')) {
  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. "AND group_id = 'group_all'",
  1622. '',
  1623. $extra_where_conditions
  1624. );
  1625. $extra_where_conditions = str_replace(
  1626. "group_id = 'group_all' AND",
  1627. '',
  1628. $extra_where_conditions
  1629. );
  1630. $sql_inner_join_tbl_user = "
  1631. (
  1632. SELECT u.user_id, firstname, lastname, email, username, '' as group_name, '' as group_id
  1633. FROM $TBL_USER u
  1634. )";
  1635. $sql_inner_join_tbl_user = null;
  1636. }
  1637. if (strpos($extra_where_conditions, 'group_none')) {
  1638. $extra_where_conditions = str_replace(
  1639. "AND ( group_id = 'group_none' )",
  1640. "AND ( group_id is null )",
  1641. $extra_where_conditions
  1642. );
  1643. $extra_where_conditions = str_replace(
  1644. "AND group_id = 'group_none'",
  1645. "AND ( group_id is null )",
  1646. $extra_where_conditions
  1647. );
  1648. $sql_inner_join_tbl_user = "
  1649. (
  1650. SELECT u.user_id, firstname, lastname, email, username, g.name as group_name, g.id as group_id
  1651. FROM $TBL_USER u
  1652. LEFT OUTER JOIN $TBL_GROUP_REL_USER gru ON ( gru.user_id = u.user_id AND gru.c_id=" . $course_id . " )
  1653. LEFT OUTER JOIN $TBL_GROUP g ON (gru.group_id = g.id AND g.c_id = " . $course_id . ")
  1654. )";
  1655. }
  1656. // All
  1657. $is_empty_sql_inner_join_tbl_user = false;
  1658. if (empty($sql_inner_join_tbl_user)) {
  1659. $is_empty_sql_inner_join_tbl_user = true;
  1660. $sql_inner_join_tbl_user = "
  1661. (
  1662. SELECT u.user_id, firstname, lastname, email, username, ' ' as group_name, '' as group_id, official_code
  1663. FROM $TBL_USER u
  1664. WHERE u.status NOT IN(" . api_get_users_status_ignored_in_reports(
  1665. 'string'
  1666. ) . ")
  1667. )";
  1668. }
  1669. $sqlFromOption = " , $TBL_GROUP_REL_USER AS gru ";
  1670. $sqlWhereOption = " AND gru.c_id = " . $course_id .
  1671. " AND gru.user_id = user.user_id ";
  1672. $first_and_last_name = api_is_western_name_order(
  1673. ) ? "firstname, lastname" : "lastname, firstname";
  1674. if ($get_count) {
  1675. $sql_select = "SELECT count(te.exe_id) ";
  1676. } else {
  1677. $sql_select = "SELECT DISTINCT
  1678. user_id,
  1679. $first_and_last_name,
  1680. official_code,
  1681. ce.title,
  1682. username,
  1683. te.exe_result,
  1684. te.exe_weighting,
  1685. te.exe_date,
  1686. te.exe_id,
  1687. email as exemail,
  1688. te.start_date,
  1689. steps_counter,
  1690. exe_user_id,
  1691. te.exe_duration,
  1692. propagate_neg,
  1693. revised,
  1694. group_name,
  1695. group_id,
  1696. orig_lp_id,
  1697. te.user_ip";
  1698. }
  1699. $sql = " $sql_select
  1700. FROM $TBL_EXERCICES AS ce
  1701. INNER JOIN $sql_inner_join_tbl_track_exercices AS te ON (te.exe_exo_id = ce.id)
  1702. INNER JOIN $sql_inner_join_tbl_user AS user ON (user.user_id = exe_user_id)
  1703. WHERE
  1704. te.status != 'incomplete' AND
  1705. te.c_id = " . $course_id . " $session_id_and AND
  1706. ce.active <>-1 AND ce.c_id = " . $course_id . "
  1707. $exercise_where
  1708. $extra_where_conditions
  1709. ";
  1710. // sql for hotpotatoes tests for teacher / tutor view
  1711. if ($get_count) {
  1712. $hpsql_select = "SELECT count(username)";
  1713. } else {
  1714. $hpsql_select = "SELECT
  1715. $first_and_last_name ,
  1716. username,
  1717. official_code,
  1718. tth.exe_name,
  1719. tth.exe_result ,
  1720. tth.exe_weighting,
  1721. tth.exe_date";
  1722. }
  1723. $hpsql = " $hpsql_select
  1724. FROM
  1725. $TBL_TRACK_HOTPOTATOES tth,
  1726. $TBL_USER user
  1727. $sqlFromOption
  1728. WHERE
  1729. user.user_id=tth.exe_user_id
  1730. AND tth.c_id = " . $course_id . "
  1731. $hotpotatoe_where
  1732. $sqlWhereOption
  1733. AND user.status NOT IN(" . api_get_users_status_ignored_in_reports(
  1734. 'string'
  1735. ) . ")
  1736. ORDER BY
  1737. tth.c_id ASC,
  1738. tth.exe_date DESC";
  1739. }
  1740. if ($get_count) {
  1741. $resx = Database::query($sql);
  1742. $rowx = Database::fetch_row($resx, 'ASSOC');
  1743. return $rowx[0];
  1744. }
  1745. $teacher_list = CourseManager::get_teacher_list_from_course_code(
  1746. api_get_course_id()
  1747. );
  1748. $teacher_id_list = array();
  1749. foreach ($teacher_list as $teacher) {
  1750. $teacher_id_list[] = $teacher['user_id'];
  1751. }
  1752. $list_info = array();
  1753. // Simple exercises
  1754. if (empty($hotpotatoe_where)) {
  1755. $column = !empty($column) ? Database::escape_string($column) : null;
  1756. $from = intval($from);
  1757. $number_of_items = intval($number_of_items);
  1758. if (!empty($column)) {
  1759. $sql .= " ORDER BY $column $direction ";
  1760. }
  1761. $sql .= " LIMIT $from, $number_of_items";
  1762. $results = array();
  1763. $resx = Database::query($sql);
  1764. while ($rowx = Database::fetch_array($resx, 'ASSOC')) {
  1765. $results[] = $rowx;
  1766. }
  1767. $group_list = GroupManager::get_group_list();
  1768. $clean_group_list = array();
  1769. if (!empty($group_list)) {
  1770. foreach ($group_list as $group) {
  1771. $clean_group_list[$group['id']] = $group['name'];
  1772. }
  1773. }
  1774. $lp_list_obj = new LearnpathList(api_get_user_id());
  1775. $lp_list = $lp_list_obj->get_flat_list();
  1776. if (is_array($results)) {
  1777. $users_array_id = array();
  1778. $from_gradebook = false;
  1779. if (isset($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
  1780. $from_gradebook = true;
  1781. }
  1782. $sizeof = count($results);
  1783. $user_list_id = array();
  1784. $locked = api_resource_is_locked_by_gradebook(
  1785. $exercise_id,
  1786. LINK_EXERCISE
  1787. );
  1788. // Looping results
  1789. for ($i = 0; $i < $sizeof; $i++) {
  1790. $revised = $results[$i]['revised'];
  1791. if ($from_gradebook && ($is_allowedToEdit)) {
  1792. if (in_array(
  1793. $results[$i]['username'] . $results[$i]['firstname'] . $results[$i]['lastname'],
  1794. $users_array_id
  1795. )) {
  1796. continue;
  1797. }
  1798. $users_array_id[] = $results[$i]['username'] . $results[$i]['firstname'] . $results[$i]['lastname'];
  1799. }
  1800. $lp_obj = isset($results[$i]['orig_lp_id']) && isset($lp_list[$results[$i]['orig_lp_id']]) ? $lp_list[$results[$i]['orig_lp_id']] : null;
  1801. $lp_name = null;
  1802. if ($lp_obj) {
  1803. $url = api_get_path(
  1804. WEB_CODE_PATH
  1805. ) . 'newscorm/lp_controller.php?' . api_get_cidreq(
  1806. ) . '&action=view&lp_id=' . $results[$i]['orig_lp_id'];
  1807. $lp_name = Display::url(
  1808. $lp_obj['lp_name'],
  1809. $url,
  1810. array('target' => '_blank')
  1811. );
  1812. }
  1813. //Add all groups by user
  1814. $group_name_list = null;
  1815. if ($is_empty_sql_inner_join_tbl_user) {
  1816. $group_list = GroupManager::get_group_ids(
  1817. api_get_course_int_id(),
  1818. $results[$i]['user_id']
  1819. );
  1820. foreach ($group_list as $id) {
  1821. $group_name_list .= $clean_group_list[$id] . '<br/>';
  1822. }
  1823. $results[$i]['group_name'] = $group_name_list;
  1824. }
  1825. $results[$i]['exe_duration'] = !empty($results[$i]['exe_duration']) ? round(
  1826. $results[$i]['exe_duration'] / 60
  1827. ) : 0;
  1828. $user_list_id[] = $results[$i]['exe_user_id'];
  1829. $id = $results[$i]['exe_id'];
  1830. $dt = api_convert_and_format_date(
  1831. $results[$i]['exe_weighting']
  1832. );
  1833. // we filter the results if we have the permission to
  1834. if (isset($results[$i]['results_disabled'])) {
  1835. $result_disabled = intval(
  1836. $results[$i]['results_disabled']
  1837. );
  1838. } else {
  1839. $result_disabled = 0;
  1840. }
  1841. if ($result_disabled == 0) {
  1842. $my_res = $results[$i]['exe_result'];
  1843. $my_total = $results[$i]['exe_weighting'];
  1844. $results[$i]['start_date'] = api_get_local_time(
  1845. $results[$i]['start_date']
  1846. );
  1847. $results[$i]['exe_date'] = api_get_local_time(
  1848. $results[$i]['exe_date']
  1849. );
  1850. if (!$results[$i]['propagate_neg'] && $my_res < 0) {
  1851. $my_res = 0;
  1852. }
  1853. $score = self::show_score($my_res, $my_total);
  1854. $actions = '';
  1855. if ($is_allowedToEdit) {
  1856. if (isset($teacher_id_list)) {
  1857. if (in_array(
  1858. $results[$i]['exe_user_id'],
  1859. $teacher_id_list
  1860. )) {
  1861. $actions .= Display::return_icon(
  1862. 'teachers.gif',
  1863. get_lang('Teacher')
  1864. );
  1865. }
  1866. }
  1867. if ($revised) {
  1868. $actions .= "<a href='exercise_show.php?" . api_get_cidreq(
  1869. ) . "&action=edit&id=$id'>" .
  1870. Display:: return_icon(
  1871. 'edit.png',
  1872. get_lang('Edit'),
  1873. array(),
  1874. ICON_SIZE_SMALL
  1875. );
  1876. $actions .= '&nbsp;';
  1877. } else {
  1878. $actions .= "<a href='exercise_show.php?" . api_get_cidreq(
  1879. ) . "&action=qualify&id=$id'>" .
  1880. Display:: return_icon(
  1881. 'quiz.gif',
  1882. get_lang('Qualify')
  1883. );
  1884. $actions .= '&nbsp;';
  1885. }
  1886. $actions .= "</a>";
  1887. if ($filter == 2) {
  1888. $actions .= ' <a href="exercise_history.php?' . api_get_cidreq(
  1889. ) . '&exe_id=' . $id . '">' .
  1890. Display:: return_icon(
  1891. 'history.gif',
  1892. get_lang('ViewHistoryChange')
  1893. ) . '</a>';
  1894. }
  1895. //Admin can always delete the attempt
  1896. if (($locked == false || api_is_platform_admin(
  1897. )) && !api_is_student_boss()
  1898. ) {
  1899. $ip = TrackingUserLog::get_ip_from_user_event(
  1900. $results[$i]['exe_user_id'],
  1901. date('Y-m-d h:i:s'),
  1902. false
  1903. );
  1904. $actions .= '<a href="http://www.whatsmyip.org/ip-geo-location/?ip=' . $ip . '" target="_blank"><img src="' . api_get_path(
  1905. WEB_CODE_PATH
  1906. ) . 'img/icons/22/info.png" title="' . $ip . '" /></a>';
  1907. $delete_link = '<a href="exercise_report.php?' . api_get_cidreq(
  1908. ) . '&filter_by_user=' . intval(
  1909. $_GET['filter_by_user']
  1910. ) . '&filter=' . $filter . '&exerciseId=' . $exercise_id . '&delete=delete&did=' . $id . '"
  1911. onclick="javascript:if(!confirm(\'' . sprintf(
  1912. get_lang('DeleteAttempt'),
  1913. $results[$i]['username'],
  1914. $dt
  1915. ) . '\')) return false;">' . Display:: return_icon(
  1916. 'delete.png',
  1917. get_lang('Delete')
  1918. ) . '</a>';
  1919. $delete_link = utf8_encode($delete_link);
  1920. if (api_is_drh() && !api_is_platform_admin()) {
  1921. $delete_link = null;
  1922. }
  1923. $actions .= $delete_link . '&nbsp;';
  1924. }
  1925. } else {
  1926. $attempt_url = api_get_path(
  1927. WEB_CODE_PATH
  1928. ) . 'exercice/result.php?' . api_get_cidreq(
  1929. ) . '&id=' . $results[$i]['exe_id'] . '&id_session=' . $sessionId . '&height=500&width=750';
  1930. $attempt_link = Display::url(
  1931. get_lang('Show'),
  1932. $attempt_url,
  1933. array('class' => 'ajax btn')
  1934. );
  1935. $actions .= $attempt_link;
  1936. }
  1937. if ($revised) {
  1938. $revised = Display::label(
  1939. get_lang('Validated'),
  1940. 'success'
  1941. );
  1942. } else {
  1943. $revised = Display::label(
  1944. get_lang('NotValidated'),
  1945. 'info'
  1946. );
  1947. }
  1948. if ($is_allowedToEdit) {
  1949. $results[$i]['status'] = $revised;
  1950. $results[$i]['score'] = $score;
  1951. $results[$i]['lp'] = $lp_name;
  1952. $results[$i]['actions'] = $actions;
  1953. $list_info[] = $results[$i];
  1954. } else {
  1955. $results[$i]['status'] = $revised;
  1956. $results[$i]['score'] = $score;
  1957. $results[$i]['actions'] = $actions;
  1958. $list_info[] = $results[$i];
  1959. }
  1960. }
  1961. }
  1962. }
  1963. } else {
  1964. $hpresults = StatsUtils::getManyResultsXCol($hpsql, 6);
  1965. // Print HotPotatoes test results.
  1966. if (is_array($hpresults)) {
  1967. for ($i = 0; $i < sizeof($hpresults); $i++) {
  1968. $hp_title = GetQuizName($hpresults[$i][3], $documentPath);
  1969. if ($hp_title == '') {
  1970. $hp_title = basename($hpresults[$i][3]);
  1971. }
  1972. $hp_date = api_get_local_time(
  1973. $hpresults[$i][6],
  1974. null,
  1975. date_default_timezone_get()
  1976. );
  1977. $hp_result = round(
  1978. ($hpresults[$i][4] / ($hpresults[$i][5] != 0 ? $hpresults[$i][5] : 1)) * 100,
  1979. 2
  1980. ) . '% (' . $hpresults[$i][4] . ' / ' . $hpresults[$i][5] . ')';
  1981. if ($is_allowedToEdit) {
  1982. $list_info[] = array(
  1983. $hpresults[$i][0],
  1984. $hpresults[$i][1],
  1985. $hpresults[$i][2],
  1986. '',
  1987. $hp_title,
  1988. '-',
  1989. $hp_date,
  1990. $hp_result,
  1991. '-'
  1992. );
  1993. } else {
  1994. $list_info[] = array(
  1995. $hp_title,
  1996. '-',
  1997. $hp_date,
  1998. $hp_result,
  1999. '-'
  2000. );
  2001. }
  2002. }
  2003. }
  2004. }
  2005. return $list_info;
  2006. }
  2007. /**
  2008. * Converts the score with the exercise_max_note and exercise_min_score
  2009. * the platform settings + formats the results using the float_format function
  2010. *
  2011. * @param float $score
  2012. * @param float $weight
  2013. * @param bool $show_percentage show percentage or not
  2014. * @param bool $use_platform_settings use or not the platform settings
  2015. * @param bool $show_only_percentage
  2016. * @return string an html with the score modified
  2017. */
  2018. public static function show_score(
  2019. $score,
  2020. $weight,
  2021. $show_percentage = true,
  2022. $use_platform_settings = true,
  2023. $show_only_percentage = false
  2024. ) {
  2025. if (is_null($score) && is_null($weight)) {
  2026. return '-';
  2027. }
  2028. $max_note = api_get_setting('exercise_max_score');
  2029. $min_note = api_get_setting('exercise_min_score');
  2030. if ($use_platform_settings) {
  2031. if ($max_note != '' && $min_note != '') {
  2032. if (!empty($weight) && intval($weight) != 0) {
  2033. $score = $min_note + ($max_note - $min_note) * $score / $weight;
  2034. } else {
  2035. $score = $min_note;
  2036. }
  2037. $weight = $max_note;
  2038. }
  2039. }
  2040. $percentage = (100 * $score) / ($weight != 0 ? $weight : 1);
  2041. // Formats values
  2042. $percentage = float_format($percentage, 1);
  2043. $score = float_format($score, 1);
  2044. $weight = float_format($weight, 1);
  2045. $html = null;
  2046. if ($show_percentage) {
  2047. $parent = '(' . $score . ' / ' . $weight . ')';
  2048. $html = $percentage . "% $parent";
  2049. if ($show_only_percentage) {
  2050. $html = $percentage . "% ";
  2051. }
  2052. } else {
  2053. $html = $score . ' / ' . $weight;
  2054. }
  2055. $html = Display::span($html, array('class' => 'score_exercise'));
  2056. return $html;
  2057. }
  2058. /**
  2059. * @param float $score
  2060. * @param float $weight
  2061. * @param string $pass_percentage
  2062. * @return bool
  2063. */
  2064. public static function is_success_exercise_result($score, $weight, $pass_percentage)
  2065. {
  2066. $percentage = float_format(
  2067. ($score / ($weight != 0 ? $weight : 1)) * 100,
  2068. 1
  2069. );
  2070. if (isset($pass_percentage) && !empty($pass_percentage)) {
  2071. if ($percentage >= $pass_percentage) {
  2072. return true;
  2073. }
  2074. }
  2075. return false;
  2076. }
  2077. /**
  2078. * @param float $score
  2079. * @param float $weight
  2080. * @param string $pass_percentage
  2081. * @return string
  2082. */
  2083. public static function show_success_message($score, $weight, $pass_percentage)
  2084. {
  2085. $res = "";
  2086. if (self::is_pass_pourcentage_enabled($pass_percentage)) {
  2087. $is_success = self::is_success_exercise_result(
  2088. $score,
  2089. $weight,
  2090. $pass_percentage
  2091. );
  2092. if ($is_success) {
  2093. $html = get_lang('CongratulationsYouPassedTheTest');
  2094. $icon = Display::return_icon(
  2095. 'completed.png',
  2096. get_lang('Correct'),
  2097. array(),
  2098. ICON_SIZE_MEDIUM
  2099. );
  2100. } else {
  2101. //$html .= Display::return_message(get_lang('YouDidNotReachTheMinimumScore'), 'warning');
  2102. $html = get_lang('YouDidNotReachTheMinimumScore');
  2103. $icon = Display::return_icon(
  2104. 'warning.png',
  2105. get_lang('Wrong'),
  2106. array(),
  2107. ICON_SIZE_MEDIUM
  2108. );
  2109. }
  2110. $html = Display::tag('h4', $html);
  2111. $html .= Display::tag(
  2112. 'h5',
  2113. $icon,
  2114. array('style' => 'width:40px; padding:2px 10px 0px 0px')
  2115. );
  2116. $res = $html;
  2117. }
  2118. return $res;
  2119. }
  2120. /**
  2121. * Return true if pass_pourcentage activated (we use the pass pourcentage feature
  2122. * return false if pass_percentage = 0 (we don't use the pass pourcentage feature
  2123. * @param $in_pass_pourcentage
  2124. * @return boolean
  2125. * In this version, pass_percentage and show_success_message are disabled if
  2126. * pass_percentage is set to 0
  2127. */
  2128. public static function is_pass_pourcentage_enabled($in_pass_pourcentage)
  2129. {
  2130. return $in_pass_pourcentage > 0;
  2131. }
  2132. /**
  2133. * Converts a numeric value in a percentage example 0.66666 to 66.67 %
  2134. * @param $value
  2135. * @return float Converted number
  2136. */
  2137. public static function convert_to_percentage($value)
  2138. {
  2139. $return = '-';
  2140. if ($value != '') {
  2141. $return = float_format($value * 100, 1) . ' %';
  2142. }
  2143. return $return;
  2144. }
  2145. /**
  2146. * Converts a score/weight values to the platform scale
  2147. * @param float $score
  2148. * @param float $weight
  2149. * @deprecated seem not to be used
  2150. * @return float the score rounded converted to the new range
  2151. */
  2152. public static function convert_score($score, $weight)
  2153. {
  2154. $max_note = api_get_setting('exercise_max_score');
  2155. $min_note = api_get_setting('exercise_min_score');
  2156. if ($score != '' && $weight != '') {
  2157. if ($max_note != '' && $min_note != '') {
  2158. if (!empty($weight)) {
  2159. $score = $min_note + ($max_note - $min_note) * $score / $weight;
  2160. } else {
  2161. $score = $min_note;
  2162. }
  2163. }
  2164. }
  2165. $score_rounded = float_format($score, 1);
  2166. return $score_rounded;
  2167. }
  2168. /**
  2169. * Getting all active exercises from a course from a session
  2170. * (if a session_id is provided we will show all the exercises in the course +
  2171. * all exercises in the session)
  2172. * @param array $course_info
  2173. * @param int $session_id
  2174. * @param boolean $check_publication_dates
  2175. * @param string $search Search exercise name
  2176. * @param boolean $search_all_sessions Search exercises in all sessions
  2177. * @param int 0 = only inactive exercises
  2178. * 1 = only active exercises,
  2179. * 2 = all exercises
  2180. * 3 = active <> -1
  2181. * @return array array with exercise data
  2182. */
  2183. public static function get_all_exercises(
  2184. $course_info = null,
  2185. $session_id = 0,
  2186. $check_publication_dates = false,
  2187. $search = '',
  2188. $search_all_sessions = false,
  2189. $active = 2
  2190. ) {
  2191. $course_id = api_get_course_int_id();
  2192. if (!empty($course_info) && !empty($course_info['real_id'])) {
  2193. $course_id = $course_info['real_id'];
  2194. }
  2195. if ($session_id == -1) {
  2196. $session_id = 0;
  2197. }
  2198. $now = api_get_utc_datetime();
  2199. $time_conditions = '';
  2200. if ($check_publication_dates) {
  2201. //start and end are set
  2202. $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 ";
  2203. // only start is set
  2204. $time_conditions .= " (start_time <> '0000-00-00 00:00:00' AND start_time < '$now' AND end_time = '0000-00-00 00:00:00') OR ";
  2205. // only end is set
  2206. $time_conditions .= " (start_time = '0000-00-00 00:00:00' AND end_time <> '0000-00-00 00:00:00' AND end_time > '$now') OR ";
  2207. // nothing is set
  2208. $time_conditions .= " (start_time = '0000-00-00 00:00:00' AND end_time = '0000-00-00 00:00:00')) ";
  2209. }
  2210. $needle_where = !empty($search) ? " AND title LIKE '?' " : '';
  2211. $needle = !empty($search) ? "%" . $search . "%" : '';
  2212. // Show courses by active status
  2213. $active_sql = '';
  2214. if ($active == 3) {
  2215. $active_sql = ' active <> -1 AND';
  2216. } else {
  2217. if ($active != 2) {
  2218. $active_sql = sprintf(' active = %d AND', $active);
  2219. }
  2220. }
  2221. if ($search_all_sessions == true) {
  2222. $conditions = array(
  2223. 'where' => array(
  2224. $active_sql . ' c_id = ? ' . $needle_where . $time_conditions => array(
  2225. $course_id,
  2226. $needle
  2227. )
  2228. ),
  2229. 'order' => 'title'
  2230. );
  2231. } else {
  2232. if ($session_id == 0) {
  2233. $conditions = array(
  2234. 'where' => array(
  2235. $active_sql . ' session_id = ? AND c_id = ? ' . $needle_where . $time_conditions => array(
  2236. $session_id,
  2237. $course_id,
  2238. $needle
  2239. )
  2240. ),
  2241. 'order' => 'title'
  2242. );
  2243. } else {
  2244. $conditions = array(
  2245. 'where' => array(
  2246. $active_sql . ' (session_id = 0 OR 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. }
  2255. }
  2256. $table = Database:: get_course_table(TABLE_QUIZ_TEST);
  2257. return Database::select('*', $table, $conditions);
  2258. }
  2259. /**
  2260. * Get exercise information by id
  2261. * @param int $exerciseId Exercise Id
  2262. * @param int $courseId The course ID (necessary as c_quiz.id is not unique)
  2263. * @return array Exercise info
  2264. */
  2265. public static function get_exercise_by_id($exerciseId = 0, $courseId = null)
  2266. {
  2267. $TBL_EXERCICES = Database:: get_course_table(TABLE_QUIZ_TEST);
  2268. if (empty($courseId)) {
  2269. $courseId = api_get_course_int_id();
  2270. } else {
  2271. $courseId = intval($courseId);
  2272. }
  2273. $conditions = array(
  2274. 'where' => array(
  2275. 'id = ?' => array($exerciseId),
  2276. ' AND c_id = ? ' => $courseId
  2277. )
  2278. );
  2279. return Database::select('*', $TBL_EXERCICES, $conditions);
  2280. }
  2281. /**
  2282. * Getting all exercises (active only or all)
  2283. * from a course from a session
  2284. * (if a session_id is provided we will show all the exercises in the
  2285. * course + all exercises in the session)
  2286. * @param array course data
  2287. * @param int session id
  2288. * @param int course c_id
  2289. * @param boolean $only_active_exercises
  2290. * @return array array with exercise data
  2291. * modified by Hubert Borderiou
  2292. */
  2293. public static function get_all_exercises_for_course_id(
  2294. $course_info = null,
  2295. $session_id = 0,
  2296. $course_id = 0,
  2297. $only_active_exercises = true
  2298. ) {
  2299. $TBL_EXERCISES = Database:: get_course_table(TABLE_QUIZ_TEST);
  2300. if ($only_active_exercises) {
  2301. // Only active exercises.
  2302. $sql_active_exercises = "active = 1 AND ";
  2303. } else {
  2304. // Not only active means visible and invisible NOT deleted (-2)
  2305. $sql_active_exercises = "active IN (1, 0) AND ";
  2306. }
  2307. if ($session_id == -1) {
  2308. $session_id = 0;
  2309. }
  2310. $params = array(
  2311. $session_id,
  2312. $course_id
  2313. );
  2314. if ($session_id == 0) {
  2315. $conditions = array(
  2316. 'where' => array("$sql_active_exercises session_id = ? AND c_id = ?" => $params),
  2317. 'order' => 'title'
  2318. );
  2319. } else {
  2320. // All exercises
  2321. $conditions = array(
  2322. 'where' => array("$sql_active_exercises (session_id = 0 OR session_id = ? ) AND c_id=?" => $params),
  2323. 'order' => 'title'
  2324. );
  2325. }
  2326. return Database::select('*', $TBL_EXERCISES, $conditions);
  2327. }
  2328. /**
  2329. * Gets the position of the score based in a given score (result/weight)
  2330. * and the exe_id based in the user list
  2331. * (NO Exercises in LPs )
  2332. * @param float $my_score user score to be compared *attention*
  2333. * $my_score = score/weight and not just the score
  2334. * @param int $my_exe_id exe id of the exercise
  2335. * (this is necessary because if 2 students have the same score the one
  2336. * with the minor exe_id will have a best position, just to be fair and FIFO)
  2337. * @param int $exercise_id
  2338. * @param string $course_code
  2339. * @param int $session_id
  2340. * @param array $user_list
  2341. * @param bool $return_string
  2342. *
  2343. * @return int the position of the user between his friends in a course
  2344. * (or course within a session)
  2345. */
  2346. public static function get_exercise_result_ranking(
  2347. $my_score,
  2348. $my_exe_id,
  2349. $exercise_id,
  2350. $course_code,
  2351. $session_id = 0,
  2352. $user_list = array(),
  2353. $return_string = true
  2354. ) {
  2355. //No score given we return
  2356. if (is_null($my_score)) {
  2357. return '-';
  2358. }
  2359. if (empty($user_list)) {
  2360. return '-';
  2361. }
  2362. $best_attempts = array();
  2363. foreach ($user_list as $user_data) {
  2364. $user_id = $user_data['user_id'];
  2365. $best_attempts[$user_id] = self::get_best_attempt_by_user(
  2366. $user_id,
  2367. $exercise_id,
  2368. $course_code,
  2369. $session_id
  2370. );
  2371. }
  2372. if (empty($best_attempts)) {
  2373. return 1;
  2374. } else {
  2375. $position = 1;
  2376. $my_ranking = array();
  2377. foreach ($best_attempts as $user_id => $result) {
  2378. if (!empty($result['exe_weighting']) && intval(
  2379. $result['exe_weighting']
  2380. ) != 0
  2381. ) {
  2382. $my_ranking[$user_id] = $result['exe_result'] / $result['exe_weighting'];
  2383. } else {
  2384. $my_ranking[$user_id] = 0;
  2385. }
  2386. }
  2387. //if (!empty($my_ranking)) {
  2388. asort($my_ranking);
  2389. $position = count($my_ranking);
  2390. if (!empty($my_ranking)) {
  2391. foreach ($my_ranking as $user_id => $ranking) {
  2392. if ($my_score >= $ranking) {
  2393. if ($my_score == $ranking) {
  2394. $exe_id = $best_attempts[$user_id]['exe_id'];
  2395. if ($my_exe_id < $exe_id) {
  2396. $position--;
  2397. }
  2398. } else {
  2399. $position--;
  2400. }
  2401. }
  2402. }
  2403. }
  2404. //}
  2405. $return_value = array(
  2406. 'position' => $position,
  2407. 'count' => count($my_ranking)
  2408. );
  2409. if ($return_string) {
  2410. if (!empty($position) && !empty($my_ranking)) {
  2411. $return_value = $position . '/' . count($my_ranking);
  2412. } else {
  2413. $return_value = '-';
  2414. }
  2415. }
  2416. return $return_value;
  2417. }
  2418. }
  2419. /**
  2420. * Gets the position of the score based in a given score (result/weight) and the exe_id based in all attempts
  2421. * (NO Exercises in LPs ) old functionality by attempt
  2422. * @param float user score to be compared attention => score/weight
  2423. * @param int exe id of the exercise
  2424. * (this is necessary because if 2 students have the same score the one
  2425. * with the minor exe_id will have a best position, just to be fair and FIFO)
  2426. * @param int exercise id
  2427. * @param string course code
  2428. * @param int session id
  2429. * @return int the position of the user between his friends in a course (or course within a session)
  2430. */
  2431. public static function get_exercise_result_ranking_by_attempt(
  2432. $my_score,
  2433. $my_exe_id,
  2434. $exercise_id,
  2435. $courseId,
  2436. $session_id = 0,
  2437. $return_string = true
  2438. ) {
  2439. if (empty($session_id)) {
  2440. $session_id = 0;
  2441. }
  2442. if (is_null($my_score)) {
  2443. return '-';
  2444. }
  2445. $user_results = Event::get_all_exercise_results(
  2446. $exercise_id,
  2447. $courseId,
  2448. $session_id,
  2449. false
  2450. );
  2451. $position_data = array();
  2452. if (empty($user_results)) {
  2453. return 1;
  2454. } else {
  2455. $position = 1;
  2456. $my_ranking = array();
  2457. foreach ($user_results as $result) {
  2458. //print_r($result);
  2459. if (!empty($result['exe_weighting']) && intval(
  2460. $result['exe_weighting']
  2461. ) != 0
  2462. ) {
  2463. $my_ranking[$result['exe_id']] = $result['exe_result'] / $result['exe_weighting'];
  2464. } else {
  2465. $my_ranking[$result['exe_id']] = 0;
  2466. }
  2467. }
  2468. asort($my_ranking);
  2469. $position = count($my_ranking);
  2470. if (!empty($my_ranking)) {
  2471. foreach ($my_ranking as $exe_id => $ranking) {
  2472. if ($my_score >= $ranking) {
  2473. if ($my_score == $ranking) {
  2474. if ($my_exe_id < $exe_id) {
  2475. $position--;
  2476. }
  2477. } else {
  2478. $position--;
  2479. }
  2480. }
  2481. }
  2482. }
  2483. $return_value = array(
  2484. 'position' => $position,
  2485. 'count' => count($my_ranking)
  2486. );
  2487. if ($return_string) {
  2488. if (!empty($position) && !empty($my_ranking)) {
  2489. return $position . '/' . count($my_ranking);
  2490. }
  2491. }
  2492. return $return_value;
  2493. }
  2494. }
  2495. /**
  2496. * Get the best attempt in a exercise (NO Exercises in LPs )
  2497. * @param int $exercise_id
  2498. * @param int $courseId
  2499. * @param int $session_id
  2500. *
  2501. * @return array
  2502. */
  2503. public static function get_best_attempt_in_course($exercise_id, $courseId, $session_id)
  2504. {
  2505. $user_results = Event::get_all_exercise_results(
  2506. $exercise_id,
  2507. $courseId,
  2508. $session_id,
  2509. false
  2510. );
  2511. $best_score_data = array();
  2512. $best_score = 0;
  2513. if (!empty($user_results)) {
  2514. foreach ($user_results as $result) {
  2515. if (!empty($result['exe_weighting']) &&
  2516. intval($result['exe_weighting']) != 0
  2517. ) {
  2518. $score = $result['exe_result'] / $result['exe_weighting'];
  2519. if ($score >= $best_score) {
  2520. $best_score = $score;
  2521. $best_score_data = $result;
  2522. }
  2523. }
  2524. }
  2525. }
  2526. return $best_score_data;
  2527. }
  2528. /**
  2529. * Get the best score in a exercise (NO Exercises in LPs )
  2530. * @param int $user_id
  2531. * @param int $exercise_id
  2532. * @param int $courseId
  2533. * @param int $session_id
  2534. *
  2535. * @return array
  2536. */
  2537. public static function get_best_attempt_by_user(
  2538. $user_id,
  2539. $exercise_id,
  2540. $courseId,
  2541. $session_id
  2542. ) {
  2543. $user_results = Event::get_all_exercise_results(
  2544. $exercise_id,
  2545. $courseId,
  2546. $session_id,
  2547. false,
  2548. $user_id
  2549. );
  2550. $best_score_data = array();
  2551. $best_score = 0;
  2552. if (!empty($user_results)) {
  2553. foreach ($user_results as $result) {
  2554. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  2555. $score = $result['exe_result'] / $result['exe_weighting'];
  2556. if ($score >= $best_score) {
  2557. $best_score = $score;
  2558. $best_score_data = $result;
  2559. }
  2560. }
  2561. }
  2562. }
  2563. return $best_score_data;
  2564. }
  2565. /**
  2566. * Get average score (NO Exercises in LPs )
  2567. * @param int exercise id
  2568. * @param int $courseId
  2569. * @param int session id
  2570. * @return float Average score
  2571. */
  2572. public static function get_average_score($exercise_id, $courseId, $session_id)
  2573. {
  2574. $user_results = Event::get_all_exercise_results(
  2575. $exercise_id,
  2576. $courseId,
  2577. $session_id
  2578. );
  2579. $avg_score = 0;
  2580. if (!empty($user_results)) {
  2581. foreach ($user_results as $result) {
  2582. if (!empty($result['exe_weighting']) && intval(
  2583. $result['exe_weighting']
  2584. ) != 0
  2585. ) {
  2586. $score = $result['exe_result'] / $result['exe_weighting'];
  2587. $avg_score += $score;
  2588. }
  2589. }
  2590. $avg_score = float_format($avg_score / count($user_results), 1);
  2591. }
  2592. return $avg_score;
  2593. }
  2594. /**
  2595. * Get average score by score (NO Exercises in LPs )
  2596. * @param int exercise id
  2597. * @param int $courseId
  2598. * @param int session id
  2599. * @return float Average score
  2600. */
  2601. public static function get_average_score_by_course($courseId, $session_id)
  2602. {
  2603. $user_results = Event::get_all_exercise_results_by_course(
  2604. $courseId,
  2605. $session_id,
  2606. false
  2607. );
  2608. //echo $course_code.' - '.$session_id.'<br />';
  2609. $avg_score = 0;
  2610. if (!empty($user_results)) {
  2611. foreach ($user_results as $result) {
  2612. if (!empty($result['exe_weighting']) && intval(
  2613. $result['exe_weighting']
  2614. ) != 0
  2615. ) {
  2616. $score = $result['exe_result'] / $result['exe_weighting'];
  2617. $avg_score += $score;
  2618. }
  2619. }
  2620. //We asume that all exe_weighting
  2621. $avg_score = ($avg_score / count($user_results));
  2622. }
  2623. return $avg_score;
  2624. }
  2625. /**
  2626. * @param int $user_id
  2627. * @param int $courseId
  2628. * @param int $session_id
  2629. *
  2630. * @return float|int
  2631. */
  2632. public static function get_average_score_by_course_by_user(
  2633. $user_id,
  2634. $courseId,
  2635. $session_id
  2636. ) {
  2637. $user_results = Event::get_all_exercise_results_by_user(
  2638. $user_id,
  2639. $courseId,
  2640. $session_id
  2641. );
  2642. $avg_score = 0;
  2643. if (!empty($user_results)) {
  2644. foreach ($user_results as $result) {
  2645. if (!empty($result['exe_weighting']) && intval(
  2646. $result['exe_weighting']
  2647. ) != 0
  2648. ) {
  2649. $score = $result['exe_result'] / $result['exe_weighting'];
  2650. $avg_score += $score;
  2651. }
  2652. }
  2653. //We asume that all exe_weighting
  2654. //$avg_score = show_score( $avg_score / count($user_results) , $result['exe_weighting']);
  2655. $avg_score = ($avg_score / count($user_results));
  2656. }
  2657. return $avg_score;
  2658. }
  2659. /**
  2660. * Get average score by score (NO Exercises in LPs )
  2661. * @param int exercise id
  2662. * @param int $courseId
  2663. * @param int session id
  2664. * @return float Best average score
  2665. */
  2666. public static function get_best_average_score_by_exercise(
  2667. $exercise_id,
  2668. $courseId,
  2669. $session_id,
  2670. $user_count
  2671. ) {
  2672. $user_results = Event::get_best_exercise_results_by_user(
  2673. $exercise_id,
  2674. $courseId,
  2675. $session_id
  2676. );
  2677. $avg_score = 0;
  2678. if (!empty($user_results)) {
  2679. foreach ($user_results as $result) {
  2680. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  2681. $score = $result['exe_result'] / $result['exe_weighting'];
  2682. $avg_score += $score;
  2683. }
  2684. }
  2685. //We asume that all exe_weighting
  2686. //$avg_score = show_score( $avg_score / count($user_results) , $result['exe_weighting']);
  2687. //$avg_score = ($avg_score / count($user_results));
  2688. if (!empty($user_count)) {
  2689. $avg_score = float_format($avg_score / $user_count, 1) * 100;
  2690. } else {
  2691. $avg_score = 0;
  2692. }
  2693. }
  2694. return $avg_score;
  2695. }
  2696. /**
  2697. * @param string $course_code
  2698. * @param int $session_id
  2699. *
  2700. * @return array
  2701. */
  2702. public static function get_exercises_to_be_taken($course_code, $session_id)
  2703. {
  2704. $course_info = api_get_course_info($course_code);
  2705. $exercises = self::get_all_exercises($course_info, $session_id);
  2706. $result = array();
  2707. $now = time() + 15 * 24 * 60 * 60;
  2708. foreach ($exercises as $exercise_item) {
  2709. if (isset($exercise_item['end_time']) &&
  2710. !empty($exercise_item['end_time']) &&
  2711. $exercise_item['end_time'] != '0000-00-00 00:00:00' &&
  2712. api_strtotime($exercise_item['end_time'], 'UTC') < $now
  2713. ) {
  2714. $result[] = $exercise_item;
  2715. }
  2716. }
  2717. return $result;
  2718. }
  2719. /**
  2720. * Get student results (only in completed exercises) stats by question
  2721. * @param int $question_id
  2722. * @param int $exercise_id
  2723. * @param string $course_code
  2724. * @param int $session_id
  2725. *
  2726. **/
  2727. public static function get_student_stats_by_question(
  2728. $question_id,
  2729. $exercise_id,
  2730. $course_code,
  2731. $session_id
  2732. ) {
  2733. $track_exercises = Database::get_main_table(
  2734. TABLE_STATISTIC_TRACK_E_EXERCISES
  2735. );
  2736. $track_attempt = Database::get_main_table(
  2737. TABLE_STATISTIC_TRACK_E_ATTEMPT
  2738. );
  2739. $question_id = intval($question_id);
  2740. $exercise_id = intval($exercise_id);
  2741. $course_code = Database::escape_string($course_code);
  2742. $session_id = intval($session_id);
  2743. $courseId = api_get_course_int_id($course_code);
  2744. $sql = "SELECT MAX(marks) as max, MIN(marks) as min, AVG(marks) as average
  2745. FROM $track_exercises e
  2746. INNER JOIN $track_attempt a
  2747. ON (
  2748. a.exe_id = e.exe_id AND
  2749. e.c_id = a.c_id AND
  2750. e.session_id = a.session_id
  2751. )
  2752. WHERE
  2753. exe_exo_id = $exercise_id AND
  2754. a.c_id = $courseId AND
  2755. e.session_id = $session_id AND
  2756. question_id = $question_id AND
  2757. status = ''
  2758. LIMIT 1";
  2759. $result = Database::query($sql);
  2760. $return = array();
  2761. if ($result) {
  2762. $return = Database::fetch_array($result, 'ASSOC');
  2763. }
  2764. return $return;
  2765. }
  2766. /**
  2767. * @param int $question_id
  2768. * @param int $exercise_id
  2769. * @param string $course_code
  2770. * @param int $session_id
  2771. * @return int
  2772. */
  2773. public static function get_number_students_question_with_answer_count(
  2774. $question_id,
  2775. $exercise_id,
  2776. $course_code,
  2777. $session_id
  2778. ) {
  2779. $track_exercises = Database::get_main_table(
  2780. TABLE_STATISTIC_TRACK_E_EXERCISES
  2781. );
  2782. $track_attempt = Database::get_main_table(
  2783. TABLE_STATISTIC_TRACK_E_ATTEMPT
  2784. );
  2785. $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2786. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  2787. $courseUserSession = Database::get_main_table(
  2788. TABLE_MAIN_SESSION_COURSE_USER
  2789. );
  2790. $question_id = intval($question_id);
  2791. $exercise_id = intval($exercise_id);
  2792. $courseId = api_get_course_int_id($course_code);
  2793. $session_id = intval($session_id);
  2794. if (empty($session_id)) {
  2795. $courseCondition = "
  2796. INNER JOIN $courseUser cu
  2797. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  2798. $courseConditionWhere = " AND relation_type <> 2 AND cu.status = " . STUDENT;
  2799. } else {
  2800. $courseCondition = "
  2801. INNER JOIN $courseUserSession cu
  2802. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  2803. $courseConditionWhere = " AND cu.status = 0 ";
  2804. }
  2805. $sql = "SELECT DISTINCT exe_user_id
  2806. FROM $track_exercises e
  2807. INNER JOIN $track_attempt a
  2808. ON (
  2809. a.exe_id = e.exe_id AND
  2810. e.c_id = a.c_id AND
  2811. e.session_id = a.session_id
  2812. )
  2813. INNER JOIN $courseTable c
  2814. ON (c.id = a.c_id)
  2815. $courseCondition
  2816. WHERE
  2817. exe_exo_id = $exercise_id AND
  2818. a.c_id = $courseId AND
  2819. e.session_id = $session_id AND
  2820. question_id = $question_id AND
  2821. answer <> '0' AND
  2822. e.status = ''
  2823. $courseConditionWhere
  2824. ";
  2825. $result = Database::query($sql);
  2826. $return = 0;
  2827. if ($result) {
  2828. $return = Database::num_rows($result);
  2829. }
  2830. return $return;
  2831. }
  2832. /**
  2833. * @param int $answer_id
  2834. * @param int $question_id
  2835. * @param int $exercise_id
  2836. * @param string $course_code
  2837. * @param int $session_id
  2838. *
  2839. * @return int
  2840. */
  2841. public static function get_number_students_answer_hotspot_count(
  2842. $answer_id,
  2843. $question_id,
  2844. $exercise_id,
  2845. $course_code,
  2846. $session_id
  2847. ) {
  2848. $track_exercises = Database::get_main_table(
  2849. TABLE_STATISTIC_TRACK_E_EXERCISES
  2850. );
  2851. $track_hotspot = Database::get_main_table(
  2852. TABLE_STATISTIC_TRACK_E_HOTSPOT
  2853. );
  2854. $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2855. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  2856. $courseUserSession = Database::get_main_table(
  2857. TABLE_MAIN_SESSION_COURSE_USER
  2858. );
  2859. $question_id = intval($question_id);
  2860. $answer_id = intval($answer_id);
  2861. $exercise_id = intval($exercise_id);
  2862. $course_code = Database::escape_string($course_code);
  2863. $session_id = intval($session_id);
  2864. if (empty($session_id)) {
  2865. $courseCondition = "
  2866. INNER JOIN $courseUser cu
  2867. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  2868. $courseConditionWhere = " AND relation_type <> 2 AND cu.status = " . STUDENT;
  2869. } else {
  2870. $courseCondition = "
  2871. INNER JOIN $courseUserSession cu
  2872. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  2873. $courseConditionWhere = " AND cu.status = 0 ";
  2874. }
  2875. $sql = "SELECT DISTINCT exe_user_id
  2876. FROM $track_exercises e
  2877. INNER JOIN $track_hotspot a
  2878. ON (a.hotspot_exe_id = e.exe_id)
  2879. INNER JOIN $courseTable c
  2880. ON (hotspot_course_code = c.code)
  2881. $courseCondition
  2882. WHERE
  2883. exe_exo_id = $exercise_id AND
  2884. a.hotspot_course_code = '$course_code' AND
  2885. e.session_id = $session_id AND
  2886. hotspot_answer_id = $answer_id AND
  2887. hotspot_question_id = $question_id AND
  2888. hotspot_correct = 1 AND
  2889. e.status = ''
  2890. $courseConditionWhere
  2891. ";
  2892. $result = Database::query($sql);
  2893. $return = 0;
  2894. if ($result) {
  2895. $return = Database::num_rows($result);
  2896. }
  2897. return $return;
  2898. }
  2899. /**
  2900. * @param int $answer_id
  2901. * @param int $question_id
  2902. * @param int $exercise_id
  2903. * @param string $course_code
  2904. * @param int $session_id
  2905. * @param string $question_type
  2906. * @param string $correct_answer
  2907. * @param string $current_answer
  2908. * @return int
  2909. */
  2910. public static function get_number_students_answer_count(
  2911. $answer_id,
  2912. $question_id,
  2913. $exercise_id,
  2914. $course_code,
  2915. $session_id,
  2916. $question_type = null,
  2917. $correct_answer = null,
  2918. $current_answer = null
  2919. ) {
  2920. $track_exercises = Database::get_main_table(
  2921. TABLE_STATISTIC_TRACK_E_EXERCISES
  2922. );
  2923. $track_attempt = Database::get_main_table(
  2924. TABLE_STATISTIC_TRACK_E_ATTEMPT
  2925. );
  2926. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  2927. $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2928. $courseUserSession = Database::get_main_table(
  2929. TABLE_MAIN_SESSION_COURSE_USER
  2930. );
  2931. $question_id = intval($question_id);
  2932. $answer_id = intval($answer_id);
  2933. $exercise_id = intval($exercise_id);
  2934. $courseId = api_get_course_int_id($course_code);
  2935. $course_code = Database::escape_string($course_code);
  2936. $session_id = intval($session_id);
  2937. switch ($question_type) {
  2938. case FILL_IN_BLANKS:
  2939. $answer_condition = "";
  2940. $select_condition = " e.exe_id, answer ";
  2941. break;
  2942. case MATCHING:
  2943. //no break
  2944. case MATCHING_DRAGGABLE:
  2945. //no break
  2946. default:
  2947. $answer_condition = " answer = $answer_id AND ";
  2948. $select_condition = " DISTINCT exe_user_id ";
  2949. }
  2950. if (empty($session_id)) {
  2951. $courseCondition = "
  2952. INNER JOIN $courseUser cu
  2953. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  2954. $courseConditionWhere = " AND relation_type <> 2 AND cu.status = " . STUDENT;
  2955. } else {
  2956. $courseCondition = "
  2957. INNER JOIN $courseUserSession cu
  2958. ON cu.c_id = a.c_id AND cu.user_id = exe_user_id";
  2959. $courseConditionWhere = " AND cu.status = 0 ";
  2960. }
  2961. $sql = "SELECT $select_condition
  2962. FROM $track_exercises e
  2963. INNER JOIN $track_attempt a
  2964. ON (
  2965. a.exe_id = e.exe_id AND
  2966. e.c_id = a.c_id AND
  2967. e.session_id = a.session_id
  2968. )
  2969. INNER JOIN $courseTable c
  2970. ON c.id = a.c_id
  2971. $courseCondition
  2972. WHERE
  2973. exe_exo_id = $exercise_id AND
  2974. a.c_id = $courseId AND
  2975. e.session_id = $session_id AND
  2976. $answer_condition
  2977. question_id = $question_id AND
  2978. e.status = ''
  2979. $courseConditionWhere
  2980. ";
  2981. $result = Database::query($sql);
  2982. $return = 0;
  2983. if ($result) {
  2984. $good_answers = 0;
  2985. switch ($question_type) {
  2986. case FILL_IN_BLANKS:
  2987. while ($row = Database::fetch_array($result, 'ASSOC')) {
  2988. $fill_blank = self::check_fill_in_blanks(
  2989. $correct_answer,
  2990. $row['answer']
  2991. );
  2992. if (isset($fill_blank[$current_answer]) && $fill_blank[$current_answer] == 1) {
  2993. $good_answers++;
  2994. }
  2995. }
  2996. return $good_answers;
  2997. break;
  2998. case MATCHING:
  2999. //no break
  3000. case MATCHING_DRAGGABLE:
  3001. //no break
  3002. default:
  3003. $return = Database::num_rows($result);
  3004. }
  3005. }
  3006. return $return;
  3007. }
  3008. /**
  3009. * @param array $answer
  3010. * @param string $user_answer
  3011. * @return array
  3012. */
  3013. public static function check_fill_in_blanks($answer, $user_answer)
  3014. {
  3015. // the question is encoded like this
  3016. // [A] B [C] D [E] F::10,10,10@1
  3017. // number 1 before the "@" means that is a switchable fill in blank question
  3018. // [A] B [C] D [E] F::10,10,10@ or [A] B [C] D [E] F::10,10,10
  3019. // means that is a normal fill blank question
  3020. // first we explode the "::"
  3021. $pre_array = explode('::', $answer);
  3022. // is switchable fill blank or not
  3023. $last = count($pre_array) - 1;
  3024. $is_set_switchable = explode('@', $pre_array[$last]);
  3025. $switchable_answer_set = false;
  3026. if (isset ($is_set_switchable[1]) && $is_set_switchable[1] == 1) {
  3027. $switchable_answer_set = true;
  3028. }
  3029. $answer = '';
  3030. for ($k = 0; $k < $last; $k++) {
  3031. $answer .= $pre_array[$k];
  3032. }
  3033. // splits weightings that are joined with a comma
  3034. $answerWeighting = explode(',', $is_set_switchable[0]);
  3035. // we save the answer because it will be modified
  3036. //$temp = $answer;
  3037. $temp = $answer;
  3038. $answer = '';
  3039. $j = 0;
  3040. //initialise answer tags
  3041. $user_tags = $correct_tags = $real_text = array();
  3042. // the loop will stop at the end of the text
  3043. while (1) {
  3044. // quits the loop if there are no more blanks (detect '[')
  3045. if (($pos = api_strpos($temp, '[')) === false) {
  3046. // adds the end of the text
  3047. $answer = $temp;
  3048. /* // Deprecated code
  3049. // TeX parsing - replacement of texcode tags
  3050. $texstring = api_parse_tex($texstring);
  3051. $answer = str_replace("{texcode}", $texstring, $answer);
  3052. */
  3053. $real_text[] = $answer;
  3054. break; //no more "blanks", quit the loop
  3055. }
  3056. // adds the piece of text that is before the blank
  3057. //and ends with '[' into a general storage array
  3058. $real_text[] = api_substr($temp, 0, $pos + 1);
  3059. $answer .= api_substr($temp, 0, $pos + 1);
  3060. //take the string remaining (after the last "[" we found)
  3061. $temp = api_substr($temp, $pos + 1);
  3062. // quit the loop if there are no more blanks, and update $pos to the position of next ']'
  3063. if (($pos = api_strpos($temp, ']')) === false) {
  3064. // adds the end of the text
  3065. $answer .= $temp;
  3066. break;
  3067. }
  3068. $str = $user_answer;
  3069. preg_match_all('#\[([^[]*)\]#', $str, $arr);
  3070. $str = str_replace('\r\n', '', $str);
  3071. $choice = $arr[1];
  3072. $tmp = api_strrpos($choice[$j], ' / ');
  3073. $choice[$j] = api_substr($choice[$j], 0, $tmp);
  3074. $choice[$j] = trim($choice[$j]);
  3075. //Needed to let characters ' and " to work as part of an answer
  3076. $choice[$j] = stripslashes($choice[$j]);
  3077. $user_tags[] = api_strtolower($choice[$j]);
  3078. //put the contents of the [] answer tag into correct_tags[]
  3079. $correct_tags[] = api_strtolower(api_substr($temp, 0, $pos));
  3080. $j++;
  3081. $temp = api_substr($temp, $pos + 1);
  3082. }
  3083. $answer = '';
  3084. $real_correct_tags = $correct_tags;
  3085. $chosen_list = array();
  3086. $good_answer = array();
  3087. for ($i = 0; $i < count($real_correct_tags); $i++) {
  3088. if (!$switchable_answer_set) {
  3089. //needed to parse ' and " characters
  3090. $user_tags[$i] = stripslashes($user_tags[$i]);
  3091. if ($correct_tags[$i] == $user_tags[$i]) {
  3092. $good_answer[$correct_tags[$i]] = 1;
  3093. } elseif (!empty ($user_tags[$i])) {
  3094. $good_answer[$correct_tags[$i]] = 0;
  3095. } else {
  3096. $good_answer[$correct_tags[$i]] = 0;
  3097. }
  3098. } else {
  3099. // switchable fill in the blanks
  3100. if (in_array($user_tags[$i], $correct_tags)) {
  3101. $correct_tags = array_diff($correct_tags, $chosen_list);
  3102. $good_answer[$correct_tags[$i]] = 1;
  3103. } elseif (!empty ($user_tags[$i])) {
  3104. $good_answer[$correct_tags[$i]] = 0;
  3105. } else {
  3106. $good_answer[$correct_tags[$i]] = 0;
  3107. }
  3108. }
  3109. // adds the correct word, followed by ] to close the blank
  3110. $answer .= ' / <font color="green"><b>' . $real_correct_tags[$i] . '</b></font>]';
  3111. if (isset ($real_text[$i + 1])) {
  3112. $answer .= $real_text[$i + 1];
  3113. }
  3114. }
  3115. return $good_answer;
  3116. }
  3117. /**
  3118. * @param int $exercise_id
  3119. * @param string $course_code
  3120. * @param int $session_id
  3121. * @return int
  3122. */
  3123. public static function get_number_students_finish_exercise(
  3124. $exercise_id,
  3125. $course_code,
  3126. $session_id
  3127. ) {
  3128. $track_exercises = Database::get_main_table(
  3129. TABLE_STATISTIC_TRACK_E_EXERCISES
  3130. );
  3131. $track_attempt = Database::get_main_table(
  3132. TABLE_STATISTIC_TRACK_E_ATTEMPT
  3133. );
  3134. $exercise_id = intval($exercise_id);
  3135. $course_code = Database::escape_string($course_code);
  3136. $session_id = intval($session_id);
  3137. $sql = "SELECT DISTINCT exe_user_id
  3138. FROM $track_exercises e
  3139. INNER JOIN $track_attempt a ON (a.exe_id = e.exe_id)
  3140. WHERE
  3141. exe_exo_id = $exercise_id AND
  3142. course_code = '$course_code' AND
  3143. e.session_id = $session_id AND
  3144. status = ''";
  3145. $result = Database::query($sql);
  3146. $return = 0;
  3147. if ($result) {
  3148. $return = Database::num_rows($result);
  3149. }
  3150. return $return;
  3151. }
  3152. /**
  3153. * @param string $in_name is the name and the id of the <select>
  3154. * @param string $in_default default value for option
  3155. * @param string $in_onchange
  3156. * @return string the html code of the <select>
  3157. */
  3158. public static function displayGroupMenu($in_name, $in_default, $in_onchange = "")
  3159. {
  3160. // check the default value of option
  3161. $tabSelected = array($in_default => " selected='selected' ");
  3162. $res = "";
  3163. $res .= "<select name='$in_name' id='$in_name' onchange='" . $in_onchange . "' >";
  3164. $res .= "<option value='-1'" . $tabSelected["-1"] . ">-- " . get_lang(
  3165. 'AllGroups'
  3166. ) . " --</option>";
  3167. $res .= "<option value='0'" . $tabSelected["0"] . ">- " . get_lang(
  3168. 'NotInAGroup'
  3169. ) . " -</option>";
  3170. $tabGroups = GroupManager::get_group_list();
  3171. $currentCatId = 0;
  3172. for ($i = 0; $i < count($tabGroups); $i++) {
  3173. $tabCategory = GroupManager::get_category_from_group(
  3174. $tabGroups[$i]["id"]
  3175. );
  3176. if ($tabCategory["id"] != $currentCatId) {
  3177. $res .= "<option value='-1' disabled='disabled'>" . $tabCategory["title"] . "</option>";
  3178. $currentCatId = $tabCategory["id"];
  3179. }
  3180. $res .= "<option " . $tabSelected[$tabGroups[$i]["id"]] . "style='margin-left:40px' value='" . $tabGroups[$i]["id"] . "'>" . $tabGroups[$i]["name"] . "</option>";
  3181. }
  3182. $res .= "</select>";
  3183. return $res;
  3184. }
  3185. /**
  3186. * Return a list of group for user with user_id=in_userid separated with in_separator
  3187. * @deprecated ?
  3188. */
  3189. public static function displayGroupsForUser($in_separator, $in_userid)
  3190. {
  3191. $res = implode(
  3192. $in_separator,
  3193. GroupManager::get_user_group_name($in_userid)
  3194. );
  3195. if ($res == "") {
  3196. $res = "<div style='text-align:center'>-</div>";
  3197. }
  3198. return $res;
  3199. }
  3200. /**
  3201. * @param int $exe_id
  3202. */
  3203. public static function create_chat_exercise_session($exe_id)
  3204. {
  3205. if (!isset($_SESSION['current_exercises'])) {
  3206. $_SESSION['current_exercises'] = array();
  3207. }
  3208. $_SESSION['current_exercises'][$exe_id] = true;
  3209. }
  3210. /**
  3211. * @param int $exe_id
  3212. */
  3213. public static function delete_chat_exercise_session($exe_id)
  3214. {
  3215. if (isset($_SESSION['current_exercises'])) {
  3216. $_SESSION['current_exercises'][$exe_id] = false;
  3217. }
  3218. }
  3219. /**
  3220. * Display the exercise results
  3221. * @param Exercise $objExercise
  3222. * @param int $exe_id
  3223. * @param bool $save_user_result save users results (true) or just show the results (false)
  3224. */
  3225. public static function display_question_list_by_attempt(
  3226. $objExercise,
  3227. $exe_id,
  3228. $save_user_result = false
  3229. ) {
  3230. global $origin;
  3231. // Getting attempt info
  3232. $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id(
  3233. $exe_id
  3234. );
  3235. // Getting question list
  3236. $question_list = array();
  3237. if (!empty($exercise_stat_info['data_tracking'])) {
  3238. $question_list = explode(',', $exercise_stat_info['data_tracking']);
  3239. } else {
  3240. // Try getting the question list only if save result is off
  3241. if ($save_user_result == false) {
  3242. $question_list = $objExercise->get_validated_question_list();
  3243. }
  3244. }
  3245. $counter = 1;
  3246. $total_score = $total_weight = 0;
  3247. $exercise_content = null;
  3248. // Hide results
  3249. $show_results = false;
  3250. $show_only_score = false;
  3251. if ($objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS) {
  3252. $show_results = true;
  3253. }
  3254. if (in_array(
  3255. $objExercise->results_disabled,
  3256. array(
  3257. RESULT_DISABLE_SHOW_SCORE_ONLY,
  3258. RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES
  3259. )
  3260. )
  3261. ) {
  3262. $show_only_score = true;
  3263. }
  3264. // Not display expected answer, but score, and feedback
  3265. $show_all_but_expected_answer = false;
  3266. if ($objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ONLY &&
  3267. $objExercise->feedback_type == EXERCISE_FEEDBACK_TYPE_END
  3268. ) {
  3269. $show_all_but_expected_answer = true;
  3270. $show_results = true;
  3271. $show_only_score = false;
  3272. }
  3273. if ($show_results || $show_only_score) {
  3274. $user_info = api_get_user_info($exercise_stat_info['exe_user_id']);
  3275. //Shows exercise header
  3276. echo $objExercise->show_exercise_result_header(
  3277. $user_info,
  3278. api_convert_and_format_date(
  3279. $exercise_stat_info['start_date'],
  3280. DATE_TIME_FORMAT_LONG
  3281. ),
  3282. $exercise_stat_info['duration'],
  3283. $exercise_stat_info['user_ip']
  3284. );
  3285. }
  3286. // Display text when test is finished #4074 and for LP #4227
  3287. $end_of_message = $objExercise->selectTextWhenFinished();
  3288. if (!empty($end_of_message)) {
  3289. Display::display_normal_message($end_of_message, false);
  3290. echo "<div class='clear'>&nbsp;</div>";
  3291. }
  3292. $question_list_answers = array();
  3293. $media_list = array();
  3294. $category_list = array();
  3295. // Loop over all question to show results for each of them, one by one
  3296. if (!empty($question_list)) {
  3297. foreach ($question_list as $questionId) {
  3298. // creates a temporary Question object
  3299. $objQuestionTmp = Question::read($questionId);
  3300. // This variable came from exercise_submit_modal.php
  3301. ob_start();
  3302. // We're inside *one* question. Go through each possible answer for this question
  3303. $result = $objExercise->manage_answer(
  3304. $exercise_stat_info['exe_id'],
  3305. $questionId,
  3306. null,
  3307. 'exercise_result',
  3308. array(),
  3309. $save_user_result,
  3310. true,
  3311. $show_results,
  3312. $objExercise->selectPropagateNeg(),
  3313. array()
  3314. );
  3315. if (empty($result)) {
  3316. continue;
  3317. }
  3318. // In case of global score, make sure the calculated total score is integer
  3319. /*if (!is_int($result['score'])) {
  3320. $result['score'] = round($result['score']);
  3321. }*/
  3322. $total_score += $result['score'];
  3323. $total_weight += $result['weight'];
  3324. $question_list_answers[] = array(
  3325. 'question' => $result['open_question'],
  3326. 'answer' => $result['open_answer'],
  3327. 'answer_type' => $result['answer_type']
  3328. );
  3329. $my_total_score = $result['score'];
  3330. $my_total_weight = $result['weight'];
  3331. // Category report
  3332. $category_was_added_for_this_test = false;
  3333. if (isset($objQuestionTmp->category) && !empty($objQuestionTmp->category)) {
  3334. $category_list[$objQuestionTmp->category]['score'] += $my_total_score;
  3335. $category_list[$objQuestionTmp->category]['total'] += $my_total_weight;
  3336. $category_was_added_for_this_test = true;
  3337. }
  3338. if (isset($objQuestionTmp->category_list) && !empty($objQuestionTmp->category_list)) {
  3339. foreach ($objQuestionTmp->category_list as $category_id) {
  3340. $category_list[$category_id]['score'] += $my_total_score;
  3341. $category_list[$category_id]['total'] += $my_total_weight;
  3342. $category_was_added_for_this_test = true;
  3343. }
  3344. }
  3345. // No category for this question!
  3346. if ($category_was_added_for_this_test == false) {
  3347. if (!isset($category_list['none']['score'])) {
  3348. $category_list['none']['score'] = 0;
  3349. }
  3350. if (!isset($category_list['none']['total'])) {
  3351. $category_list['none']['total'] = 0;
  3352. }
  3353. $category_list['none']['score'] += $my_total_score;
  3354. $category_list['none']['total'] += $my_total_weight;
  3355. }
  3356. if ($objExercise->selectPropagateNeg(
  3357. ) == 0 && $my_total_score < 0
  3358. ) {
  3359. $my_total_score = 0;
  3360. }
  3361. $comnt = null;
  3362. if ($show_results) {
  3363. $comnt = Event::get_comments($exe_id, $questionId);
  3364. if (!empty($comnt)) {
  3365. echo '<b>' . get_lang('Feedback') . '</b>';
  3366. echo '<div id="question_feedback">' . $comnt . '</div>';
  3367. }
  3368. }
  3369. if ($show_results) {
  3370. $score = array(
  3371. 'result' => get_lang('Score') . " : " . self::show_score(
  3372. $my_total_score,
  3373. $my_total_weight,
  3374. false,
  3375. true
  3376. ),
  3377. 'pass' => $my_total_score >= $my_total_weight ? true : false,
  3378. 'score' => $my_total_score,
  3379. 'weight' => $my_total_weight,
  3380. 'comments' => $comnt,
  3381. );
  3382. } else {
  3383. $score = array();
  3384. }
  3385. $contents = ob_get_clean();
  3386. $question_content = '<div class="question_row">';
  3387. if ($show_results) {
  3388. $show_media = false;
  3389. /*if ($objQuestionTmp->parent_id != 0 && !in_array($objQuestionTmp->parent_id, $media_list)) {
  3390. $show_media = true;
  3391. $media_list[] = $objQuestionTmp->parent_id;
  3392. }*/
  3393. //Shows question title an description
  3394. $question_content .= $objQuestionTmp->return_header(
  3395. null,
  3396. $counter,
  3397. $score
  3398. );
  3399. }
  3400. $counter++;
  3401. $question_content .= $contents;
  3402. $question_content .= '</div>';
  3403. $exercise_content .= $question_content;
  3404. } // end foreach() block that loops over all questions
  3405. }
  3406. $total_score_text = null;
  3407. if ($origin != 'learnpath') {
  3408. if ($show_results || $show_only_score) {
  3409. $total_score_text .= '<div class="question_row">';
  3410. $total_score_text .= self::get_question_ribbon(
  3411. $objExercise,
  3412. $total_score,
  3413. $total_weight,
  3414. true
  3415. );
  3416. $total_score_text .= '</div>';
  3417. }
  3418. }
  3419. if (!empty($category_list) && ($show_results || $show_only_score)) {
  3420. //Adding total
  3421. $category_list['total'] = array(
  3422. 'score' => $total_score,
  3423. 'total' => $total_weight
  3424. );
  3425. echo TestCategory::get_stats_table_by_attempt(
  3426. $objExercise->id,
  3427. $category_list
  3428. );
  3429. }
  3430. if ($show_all_but_expected_answer) {
  3431. $exercise_content .= "<div class='normal-message'>" . get_lang(
  3432. "ExerciseWithFeedbackWithoutCorrectionComment"
  3433. ) . "</div>";
  3434. }
  3435. // Remove audio auto play from questions on results page - refs BT#7939
  3436. $exercise_content = preg_replace(
  3437. ['/autoplay[\=\".+\"]+/','/autostart[\=\".+\"]+/'],
  3438. '',
  3439. $exercise_content
  3440. );
  3441. echo $total_score_text;
  3442. echo $exercise_content;
  3443. if (!$show_only_score) {
  3444. echo $total_score_text;
  3445. }
  3446. if ($save_user_result) {
  3447. // Tracking of results
  3448. $learnpath_id = $exercise_stat_info['orig_lp_id'];
  3449. $learnpath_item_id = $exercise_stat_info['orig_lp_item_id'];
  3450. $learnpath_item_view_id = $exercise_stat_info['orig_lp_item_view_id'];
  3451. if (api_is_allowed_to_session_edit()) {
  3452. Event::update_event_exercice(
  3453. $exercise_stat_info['exe_id'],
  3454. $objExercise->selectId(),
  3455. $total_score,
  3456. $total_weight,
  3457. api_get_session_id(),
  3458. $learnpath_id,
  3459. $learnpath_item_id,
  3460. $learnpath_item_view_id,
  3461. $exercise_stat_info['exe_duration'],
  3462. $question_list,
  3463. '',
  3464. array()
  3465. );
  3466. }
  3467. // Send notification ..
  3468. if (!api_is_allowed_to_edit(null, true) && !apiIsExcludedUserType()
  3469. ) {
  3470. if (api_get_course_setting(
  3471. 'email_alert_manager_on_new_quiz'
  3472. ) == 1
  3473. ) {
  3474. $objExercise->send_mail_notification_for_exam(
  3475. $question_list_answers,
  3476. $origin,
  3477. $exe_id
  3478. );
  3479. }
  3480. $objExercise->send_notification_for_open_questions(
  3481. $question_list_answers,
  3482. $origin,
  3483. $exe_id
  3484. );
  3485. $objExercise->send_notification_for_oral_questions(
  3486. $question_list_answers,
  3487. $origin,
  3488. $exe_id
  3489. );
  3490. }
  3491. }
  3492. }
  3493. /**
  3494. * @param Exercise $objExercise
  3495. * @param float $score
  3496. * @param float $weight
  3497. * @param bool $check_pass_percentage
  3498. * @return string
  3499. */
  3500. public static function get_question_ribbon(
  3501. $objExercise,
  3502. $score,
  3503. $weight,
  3504. $check_pass_percentage = false
  3505. ) {
  3506. $ribbon = '<div class="ribbon">';
  3507. if ($check_pass_percentage) {
  3508. $is_success = self::is_success_exercise_result(
  3509. $score,
  3510. $weight,
  3511. $objExercise->selectPassPercentage()
  3512. );
  3513. // Color the final test score if pass_percentage activated
  3514. $ribbon_total_success_or_error = "";
  3515. if (self::is_pass_pourcentage_enabled(
  3516. $objExercise->selectPassPercentage()
  3517. )) {
  3518. if ($is_success) {
  3519. $ribbon_total_success_or_error = ' ribbon-total-success';
  3520. } else {
  3521. $ribbon_total_success_or_error = ' ribbon-total-error';
  3522. }
  3523. }
  3524. $ribbon .= '<div class="rib rib-total ' . $ribbon_total_success_or_error . '">';
  3525. } else {
  3526. $ribbon .= '<div class="rib rib-total">';
  3527. }
  3528. $ribbon .= '<h3>' . get_lang('YourTotalScore') . ":&nbsp;";
  3529. $ribbon .= self::show_score($score, $weight, false, true);
  3530. $ribbon .= '</h3>';
  3531. $ribbon .= '</div>';
  3532. if ($check_pass_percentage) {
  3533. $ribbon .= self::show_success_message(
  3534. $score,
  3535. $weight,
  3536. $objExercise->selectPassPercentage()
  3537. );
  3538. }
  3539. $ribbon .= '</div>';
  3540. return $ribbon;
  3541. }
  3542. /**
  3543. * @param int $countLetter
  3544. * @return mixed
  3545. */
  3546. public static function detectInputAppropriateClass($countLetter)
  3547. {
  3548. $limits = array(
  3549. 0 => 'input-mini',
  3550. 10 => 'input-mini',
  3551. 15 => 'input-medium',
  3552. 20 => 'input-xlarge',
  3553. 40 => 'input-xlarge',
  3554. 60 => 'input-xxlarge',
  3555. 100 => 'input-xxlarge',
  3556. 200 => 'input-xxlarge',
  3557. );
  3558. foreach ($limits as $size => $item) {
  3559. if ($countLetter <= $size) {
  3560. return $item;
  3561. }
  3562. }
  3563. return $limits[0];
  3564. }
  3565. }