exercise.lib.php 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Exercise library
  5. * @todo convert this lib into a static class
  6. *
  7. * shows a question and its answers
  8. * @package chamilo.exercise
  9. * @author Olivier Brouckaert <oli.brouckaert@skynet.be>
  10. * @version $Id: exercise.lib.php 22247 2009-07-20 15:57:25Z ivantcholakov $
  11. * Modified by Hubert Borderiou 2011-10-21 Question Category
  12. */
  13. /**
  14. * Code
  15. */
  16. // The initialization class for the online editor is needed here.
  17. require_once dirname(__FILE__).'/../inc/lib/fckeditor/fckeditor.php';
  18. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  19. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  20. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  21. $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
  22. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  23. $main_user_table = Database::get_main_table(TABLE_MAIN_USER);
  24. $main_course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  25. $TBL_TRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  26. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  27. /**
  28. * Shows a question
  29. *
  30. * @param int question id
  31. * @param bool if true only show the questions, no exercise title
  32. * @param bool origin i.e = learnpath
  33. * @param int current item from the list of questions
  34. * @param int number of total questions
  35. * */
  36. function showQuestion($questionId, $only_questions = false, $origin = false, $current_item = '', $show_title = true, $freeze = false, $user_choice = array()) {
  37. // Text direction for the current language
  38. $is_ltr_text_direction = api_get_text_direction() != 'rtl';
  39. $remind_question = 1;
  40. // Change false to true in the following line to enable answer hinting.
  41. $debug_mark_answer = api_is_allowed_to_edit() && false;
  42. // Reads question information
  43. if (!$objQuestionTmp = Question::read($questionId)) {
  44. // Question not found
  45. return false;
  46. }
  47. $answerType = $objQuestionTmp->selectType();
  48. $pictureName = $objQuestionTmp->selectPicture();
  49. $html = '';
  50. if ($answerType != HOT_SPOT && $answerType != HOT_SPOT_DELINEATION) {
  51. // Question is not a hotspot
  52. if (!$only_questions) {
  53. $questionDescription = $objQuestionTmp->selectDescription();
  54. if ($show_title) {
  55. Testcategory::displayCategoryAndTitle($objQuestionTmp->id); //
  56. echo Display::div($current_item.'. '.$objQuestionTmp->selectTitle(), array('class'=>'question_title'));
  57. }
  58. if (!empty($questionDescription)) {
  59. echo Display::div($questionDescription, array('class'=>'question_description'));
  60. }
  61. //@deprecated
  62. if (!empty($pictureName)) {
  63. //echo "<img src='../document/download.php?doc_url=%2Fimages%2F'".$pictureName."' border='0'>";
  64. }
  65. }
  66. echo '<div class="question_options">';
  67. if ($answerType == FREE_ANSWER && $freeze) {
  68. return '';
  69. }
  70. //$s .= '<table width="720" class="exercise_options" style="width: 720px;'.$option_ie.' background-color:#fff;">';
  71. $s = '';
  72. $s .= '<table class="exercise_options">';
  73. // construction of the Answer object (also gets all answers details)
  74. $objAnswerTmp = new Answer($questionId);
  75. $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
  76. $course_id = api_get_course_int_id();
  77. $quiz_question_options = Question::readQuestionOption($questionId, $course_id);
  78. // For "matching" type here, we need something a little bit special
  79. // because the match between the suggestions and the answers cannot be
  80. // done easily (suggestions and answers are in the same table), so we
  81. // have to go through answers first (elems with "correct" value to 0).
  82. $select_items = array();
  83. //This will contain the number of answers on the left side. We call them
  84. // suggestions here, for the sake of comprehensions, while the ones
  85. // on the right side are called answers
  86. $num_suggestions = 0;
  87. if ($answerType == MATCHING) {
  88. $x = 1; //iterate through answers
  89. $letter = 'A'; //mark letters for each answer
  90. $answer_matching = $cpt1 = array();
  91. $answer_suggestions = $nbrAnswers;
  92. for ($answerId=1; $answerId <= $nbrAnswers; $answerId++) {
  93. $answerCorrect = $objAnswerTmp->isCorrect($answerId);
  94. $numAnswer = $objAnswerTmp->selectAutoId($answerId);
  95. $answer=$objAnswerTmp->selectAnswer($answerId);
  96. if ($answerCorrect==0) {
  97. // options (A, B, C, ...) that will be put into the list-box
  98. // have the "correct" field set to 0 because they are answer
  99. $cpt1[$x] = $letter;
  100. $answer_matching[$x]=$objAnswerTmp->selectAnswerByAutoId($numAnswer);
  101. $x++; $letter++;
  102. }
  103. }
  104. $i = 1;
  105. $select_items[0]['id'] = 0;
  106. $select_items[0]['letter'] = '--';
  107. $select_items[0]['answer'] = '';
  108. foreach ($answer_matching as $id => $value) {
  109. $select_items[$i]['id'] = $value['id'];
  110. $select_items[$i]['letter'] = $cpt1[$id];
  111. $select_items[$i]['answer'] = $value['answer'];
  112. $i ++;
  113. }
  114. $num_suggestions = ($nbrAnswers - $x) + 1;
  115. } elseif ($answerType == FREE_ANSWER) {
  116. $fck_content = isset($user_choice[0]) && !empty($user_choice[0]['answer']) ? $user_choice[0]['answer']:null;
  117. $oFCKeditor = new FCKeditor("choice[".$questionId."]") ;
  118. $oFCKeditor->ToolbarSet = 'TestFreeAnswer';
  119. $oFCKeditor->Width = '100%';
  120. $oFCKeditor->Height = '200';
  121. $oFCKeditor->Value = $fck_content;
  122. $s .= '<tr><td colspan="3">';
  123. $s .= $oFCKeditor->CreateHtml();
  124. $s .= '</td></tr>';
  125. }
  126. // Now navigate through the possible answers, using the max number of
  127. // answers for the question as a limiter
  128. $lines_count = 1; // a counter for matching-type answers
  129. $question_list = array();
  130. if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE || $answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
  131. $header = '';
  132. $header .= Display::tag('th', get_lang('Options'));
  133. foreach ($objQuestionTmp->options as $key=>$item) {
  134. $header .= Display::tag('th', $item);
  135. }
  136. $s.= Display::tag('tr',$header, array('style'=>'text-align:left;'));
  137. }
  138. $matching_correct_answer = 0;
  139. $user_choice_array = array();
  140. if (!empty($user_choice)) {
  141. foreach($user_choice as $item) {
  142. $user_choice_array[] = $item['answer'];
  143. }
  144. }
  145. for ($answerId=1; $answerId <= $nbrAnswers; $answerId++) {
  146. $answer = $objAnswerTmp->selectAnswer($answerId);
  147. $answerCorrect = $objAnswerTmp->isCorrect($answerId);
  148. $numAnswer = $objAnswerTmp->selectAutoId($answerId);
  149. // Unique answer
  150. if ($answerType == UNIQUE_ANSWER || $answerType == UNIQUE_ANSWER_NO_OPTION) {
  151. // set $debug_mark_answer to true at function start to
  152. // show the correct answer with a suffix '-x'
  153. $help = $selected = '';
  154. if ($debug_mark_answer) {
  155. if ($answerCorrect) {
  156. $help = 'x-';
  157. $selected = 'checked';
  158. }
  159. }
  160. $input_id = 'choice-'.$questionId.'-'.$answerId;
  161. if (isset($user_choice[0]['answer']) && $user_choice[0]['answer'] == $numAnswer ) {
  162. $attributes = array('id' =>$input_id, 'class'=>'checkbox','checked'=>1, 'selected'=>1);
  163. } else {
  164. $attributes = array('id' =>$input_id, 'class'=>'checkbox');
  165. }
  166. $answer = Security::remove_XSS($answer, STUDENT);
  167. $s .= Display::input('hidden','choice2['.$questionId.']','0');
  168. //@todo fix $is_ltr_text_direction
  169. //<p style="float: '.($is_ltr_text_direction ? 'left' : 'right').'; padding-'.($is_ltr_text_direction ? 'right' : 'left').': 4px;">
  170. //$s .= '<div style="margin-'.($is_ltr_text_direction ? 'left' : 'right').': 24px;">'.
  171. $s .= '<tr><td colspan="3">';
  172. $s .= '<span class="question_answer">';
  173. $s .= Display::input('radio', 'choice['.$questionId.']', $numAnswer, $attributes);
  174. $s .= Display::tag('label', $answer, array('for'=>$input_id)).'</span>';
  175. $s .= '</td></tr>';
  176. } elseif ($answerType == MULTIPLE_ANSWER || $answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
  177. // multiple answers
  178. // set $debug_mark_answer to true at function start to
  179. // show the correct answer with a suffix '-x'
  180. $help = $selected = '';
  181. if ($debug_mark_answer) {
  182. if ($answerCorrect) {
  183. $help = 'x-';
  184. $selected = 'checked="checked"';
  185. }
  186. }
  187. $input_id = 'choice-'.$questionId.'-'.$answerId;
  188. $answer = Security::remove_XSS($answer, STUDENT);
  189. if (in_array($numAnswer, $user_choice_array)) {
  190. $attributes = array('id' =>$input_id, 'class'=>'checkbox','checked'=>1, 'selected'=>1);
  191. } else {
  192. $attributes = array('id' =>$input_id, 'class'=>'checkbox');
  193. }
  194. $answer = Security::remove_XSS($answer, STUDENT);
  195. if ($answerType == MULTIPLE_ANSWER) {
  196. $s .= '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
  197. $s .= '<tr><td colspan="3">';
  198. $s .= '<span class="question_answer">';
  199. if ($debug_mark_answer) {
  200. if ($answerCorrect) {
  201. //$options['checked'] = 'checked';
  202. }
  203. }
  204. $s .= Display::tag('span', Display::input('checkbox', 'choice['.$questionId.']['.$numAnswer.']', $numAnswer, $attributes));
  205. $s .= Display::tag('label', $answer, array('for'=>$input_id)).'</span></td></tr>';
  206. } elseif ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
  207. $my_choice = array();
  208. if (!empty($user_choice_array)) {
  209. foreach ($user_choice_array as $item) {
  210. $item = explode(':', $item);
  211. $my_choice[$item[0]] = $item[1];
  212. }
  213. }
  214. $s .='<tr>';
  215. $s .= Display::tag('td', $answer);
  216. if (!empty($quiz_question_options)) {
  217. foreach ($quiz_question_options as $id=>$item) {
  218. if (isset($my_choice[$numAnswer]) && $id == $my_choice[$numAnswer]) {
  219. $attributes = array('class'=>'checkbox','checked'=>1, 'selected'=>1);
  220. } else {
  221. $attributes = array('class'=>'checkbox');
  222. }
  223. $s .= Display::tag('td', Display::input('radio', 'choice['.$questionId.']['.$numAnswer.']', $id, $attributes));
  224. }
  225. }
  226. $s.='<tr>';
  227. }
  228. } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION) {
  229. // multiple answers
  230. // set $debug_mark_answer to true at function start to
  231. // show the correct answer with a suffix '-x'
  232. $help = $selected = '';
  233. if ($debug_mark_answer) {
  234. if ($answerCorrect) {
  235. $help = 'x-';
  236. $selected = 'checked="checked"';
  237. }
  238. }
  239. $input_id = 'choice-'.$questionId.'-'.$answerId;
  240. if (in_array($numAnswer, $user_choice_array)) {
  241. $attributes = array('id'=>$input_id, 'class'=>'checkbox','checked'=>1, 'selected'=>1);
  242. } else {
  243. $attributes = array('id'=>$input_id, 'class'=>'checkbox');
  244. }
  245. $answer = Security::remove_XSS($answer, STUDENT);
  246. $s .= '<input type="hidden" name="choice2['.$questionId.']" value="0" />'.
  247. '<tr><td colspan="3">';
  248. $s .= '<span class="question_answer">';
  249. $s .= Display::tag('span', Display::input('checkbox', 'choice['.$questionId.']['.$numAnswer.']', 1, $attributes));
  250. $s .= Display::tag('label', $answer, array('for'=>$input_id)).'</span></td></tr>';
  251. } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
  252. // multiple answers
  253. // set $debug_mark_answer to true at function start to
  254. // show the correct answer with a suffix '-x'
  255. $s .= '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
  256. $my_choice = array();
  257. if (!empty($user_choice_array)) {
  258. foreach ($user_choice_array as $item) {
  259. $item = explode(':', $item);
  260. $my_choice[$item[0]] = $item[1];
  261. }
  262. }
  263. $answer = Security::remove_XSS($answer, STUDENT);
  264. $s .='<tr>';
  265. $s .= Display::tag('td', $answer);
  266. foreach ($objQuestionTmp->options as $key => $item) {
  267. //$options['value'] = $key;
  268. if (isset($my_choice[$numAnswer]) && $key == $my_choice[$numAnswer]) {
  269. $attributes = array('class'=>'checkbox','checked'=>1, 'selected'=>1);
  270. } else {
  271. $attributes = array('class'=>'checkbox');
  272. }
  273. $s .= Display::tag('td', Display::input('radio','choice['.$questionId.']['.$numAnswer.']', $key, $attributes));
  274. }
  275. $s.='<tr>';
  276. } elseif ($answerType == FILL_IN_BLANKS) {
  277. /*
  278. // splits text and weightings that are joined with the character '::'
  279. list($answer) = explode('::',$answer);
  280. //getting the matches
  281. $answer = api_ereg_replace('\[[^]]+\]','<input type="text" name="choice['.$questionId.'][]" size="10" />',($answer));
  282. $answer = api_preg_replace('/\[[^]]+\]/', Display::input('text', "choice[$questionId][]", '', $attributes), $answer);
  283. api_preg_match_all('/\[[^]]+\]/', $answer, $fill_list);
  284. if (isset($user_choice[0]['answer'])) {
  285. api_preg_match_all('/\[[^]]+\]/', $user_choice[0]['answer'], $user_fill_list);
  286. $user_fill_list = $user_fill_list[0];
  287. }*/
  288. list($answer) = explode('::',$answer);
  289. api_preg_match_all('/\[[^]]+\]/', $answer, $teacher_answer_list);
  290. if (isset($user_choice[0]['answer'])) {
  291. api_preg_match_all('/\[[^]]+\]/', $user_choice[0]['answer'], $student_answer_list);
  292. $student_answer_list = $student_answer_list[0];
  293. }
  294. if (!empty($teacher_answer_list) && !empty($student_answer_list)) {
  295. $teacher_answer_list = $teacher_answer_list[0];
  296. $i = 0;
  297. foreach($teacher_answer_list as $teacher_item) {
  298. $value = null;
  299. if (isset($student_answer_list[$i]) && !empty($student_answer_list[$i])) {
  300. //Cleaning student answer list
  301. $value = strip_tags($student_answer_list[$i]);
  302. $value = api_substr($value,1, api_strlen($value)-2);
  303. $value = explode('/', $value);
  304. if (!empty($value[0])) {
  305. $value = trim($value[0]);
  306. $value = str_replace('&nbsp;', '', $value);
  307. }
  308. $answer = api_preg_replace('/\['.$teacher_item.'+\]/', Display::input('text', "choice[$questionId][]", $value), $answer);
  309. }
  310. $i++;
  311. }
  312. } else {
  313. $answer = api_preg_replace('/\[[^]]+\]/', Display::input('text', "choice[$questionId][]", '', $attributes), $answer);
  314. }
  315. $s .= '<tr><td colspan="3">'.$answer.'</td></tr>';
  316. } elseif ($answerType == MATCHING) {
  317. // matching type, showing suggestions and answers
  318. // TODO: replace $answerId by $numAnswer
  319. if ($answerCorrect != 0) {
  320. // only show elements to be answered (not the contents of
  321. // the select boxes, who are corrrect = 0)
  322. $s .= '<tr><td width="45%" valign="top">';
  323. $parsed_answer = $answer;
  324. //$question_list[] = $parsed_answer;
  325. //left part questions
  326. $s .= ' <span style="float:left; width:8%;"><b>'.$lines_count.'</b>.&nbsp;</span>
  327. <span style="float:left; width:92%;">'.$parsed_answer.'</span></td>';
  328. //middle part (matches selects)
  329. $s .= '<td width="10%" valign="top" align="center">&nbsp;&nbsp;
  330. <select name="choice['.$questionId.']['.$numAnswer.']">';
  331. // fills the list-box
  332. foreach ($select_items as $key=>$val) {
  333. // set $debug_mark_answer to true at function start to
  334. // show the correct answer with a suffix '-x'
  335. $help = $selected = '';
  336. if ($debug_mark_answer) {
  337. if ($val['id'] == $answerCorrect) {
  338. $help = '-x';
  339. //$selected = 'selected="selected"';
  340. }
  341. }
  342. if (isset($user_choice[$matching_correct_answer]) && $val['id'] == $user_choice[$matching_correct_answer]['answer']) {
  343. $selected = 'selected="selected"';
  344. }
  345. $s .= '<option value="'.$val['id'].'" '.$selected.'>'.$val['letter'].$help.'</option>';
  346. } // end foreach()
  347. $s .= '</select></td>';
  348. //print_r($select_items);
  349. //right part (answers)
  350. $s.='<td width="45%" valign="top" >';
  351. if (isset($select_items[$lines_count])) {
  352. $s.='<span style="float:left; width:5%;"><b>'.$select_items[$lines_count]['letter'].'.</b></span>'.
  353. '<span style="float:left; width:95%;">'.$select_items[$lines_count]['answer'].'</span>';
  354. } else {
  355. $s.='&nbsp;';
  356. }
  357. $s .= '</td>';
  358. $s .= '</tr>';
  359. $lines_count++;
  360. //if the left side of the "matching" has been completely
  361. // shown but the right side still has values to show...
  362. if (($lines_count -1) == $num_suggestions) {
  363. // if it remains answers to shown at the right side
  364. while (isset($select_items[$lines_count])) {
  365. $s .= '<tr>
  366. <td colspan="2"></td>
  367. <td valign="top">';
  368. $s.='<b>'.$select_items[$lines_count]['letter'].'.</b> '.$select_items[$lines_count]['answer'];
  369. $s.="</td>
  370. </tr>";
  371. $lines_count++;
  372. } // end while()
  373. } // end if()
  374. $matching_correct_answer++;
  375. }
  376. }
  377. } // end for()
  378. $s .= '</table>';
  379. $s .= '</div>';
  380. // destruction of the Answer object
  381. unset($objAnswerTmp);
  382. // destruction of the Question object
  383. unset($objQuestionTmp);
  384. if ($origin != 'export') {
  385. echo $s;
  386. } else {
  387. return $s;
  388. }
  389. } elseif ($answerType == HOT_SPOT || $answerType == HOT_SPOT_DELINEATION) {
  390. // Question is a HOT_SPOT
  391. //checking document/images visibility
  392. if (api_is_platform_admin() || api_is_course_admin()) {
  393. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  394. $course = api_get_course_info();
  395. $doc_id = DocumentManager::get_document_id($course, '/images/'.$pictureName);
  396. if (is_numeric($doc_id)) {
  397. $images_folder_visibility = api_get_item_visibility($course,'document', $doc_id, api_get_session_id());
  398. if (!$images_folder_visibility) {
  399. //This message is shown only to the course/platform admin if the image is set to visibility = false
  400. Display::display_warning_message(get_lang('ChangeTheVisibilityOfTheCurrentImage'));
  401. }
  402. }
  403. }
  404. $questionName = $objQuestionTmp->selectTitle();
  405. $questionDescription = $objQuestionTmp->selectDescription();
  406. if ($freeze) {
  407. echo Display::img($objQuestionTmp->selectPicturePath());
  408. return;
  409. }
  410. // Get the answers, make a list
  411. $objAnswerTmp = new Answer($questionId);
  412. $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
  413. // get answers of hotpost
  414. $answers_hotspot = array();
  415. for ($answerId=1;$answerId <= $nbrAnswers;$answerId++) {
  416. $answers = $objAnswerTmp->selectAnswerByAutoId($objAnswerTmp->selectAutoId($answerId));
  417. $answers_hotspot[$answers['id']] = $objAnswerTmp->selectAnswer($answerId);
  418. }
  419. // display answers of hotpost order by id
  420. $answer_list = '<div style="padding: 10px; margin-left: 0px; border: 1px solid #A4A4A4; height: 408px; width: 200px;"><b>'.get_lang('HotspotZones').'</b><dl>';
  421. if (!empty($answers_hotspot)) {
  422. ksort($answers_hotspot);
  423. foreach ($answers_hotspot as $key => $value) {
  424. $answer_list .= '<dt>'.$key.'.- '.$value.'</dt><br />';
  425. }
  426. }
  427. $answer_list .= '</dl></div>';
  428. if ($answerType == HOT_SPOT_DELINEATION) {
  429. $answer_list='';
  430. $swf_file = 'hotspot_delineation_user';
  431. $swf_height = 405;
  432. } else {
  433. $swf_file = 'hotspot_user';
  434. $swf_height = 436;
  435. }
  436. if (!$only_questions) {
  437. if ($show_title) {
  438. Testcategory::displayCategoryAndTitle($objQuestionTmp->id); //
  439. echo '<div class="question_title">'.$current_item.'. '.$questionName.'</div>';
  440. }
  441. //@todo I need to the get the feedback type
  442. echo '<input type="hidden" name="hidden_hotspot_id" value="'.$questionId.'" />';
  443. echo '<table class="exercise_questions" >
  444. <tr>
  445. <td valign="top" colspan="2">';
  446. echo $questionDescription;
  447. echo '</td></tr>';
  448. }
  449. $canClick = isset($_GET['editQuestion']) ? '0' : (isset($_GET['modifyAnswers']) ? '0' : '1');
  450. $s .= '<script language="JavaScript" type="text/javascript" src="../plugin/hotspot/JavaScriptFlashGateway.js"></script>
  451. <script src="../plugin/hotspot/hotspot.js" type="text/javascript" language="JavaScript"></script>
  452. <script language="JavaScript" type="text/javascript">
  453. <!--
  454. // Globals
  455. // Major version of Flash required
  456. var requiredMajorVersion = 7;
  457. // Minor version of Flash required
  458. var requiredMinorVersion = 0;
  459. // Minor version of Flash required
  460. var requiredRevision = 0;
  461. // the version of javascript supported
  462. var jsVersion = 1.0;
  463. // -->
  464. </script>
  465. <script language="VBScript" type="text/vbscript">
  466. <!-- // Visual basic helper required to detect Flash Player ActiveX control version information
  467. Function VBGetSwfVer(i)
  468. on error resume next
  469. Dim swControl, swVersion
  470. swVersion = 0
  471. set swControl = CreateObject("ShockwaveFlash.ShockwaveFlash." + CStr(i))
  472. if (IsObject(swControl)) then
  473. swVersion = swControl.GetVariable("$version")
  474. end if
  475. VBGetSwfVer = swVersion
  476. End Function
  477. // -->
  478. </script>
  479. <script language="JavaScript1.1" type="text/javascript">
  480. <!-- // Detect Client Browser type
  481. var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
  482. var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
  483. var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
  484. jsVersion = 1.1;
  485. // JavaScript helper required to detect Flash Player PlugIn version information
  486. function JSGetSwfVer(i) {
  487. // NS/Opera version >= 3 check for Flash plugin in plugin array
  488. if (navigator.plugins != null && navigator.plugins.length > 0) {
  489. if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
  490. var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
  491. var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
  492. descArray = flashDescription.split(" ");
  493. tempArrayMajor = descArray[2].split(".");
  494. versionMajor = tempArrayMajor[0];
  495. versionMinor = tempArrayMajor[1];
  496. if ( descArray[3] != "" ) {
  497. tempArrayMinor = descArray[3].split("r");
  498. } else {
  499. tempArrayMinor = descArray[4].split("r");
  500. }
  501. versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
  502. flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
  503. } else {
  504. flashVer = -1;
  505. }
  506. }
  507. // MSN/WebTV 2.6 supports Flash 4
  508. else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
  509. // WebTV 2.5 supports Flash 3
  510. else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
  511. // older WebTV supports Flash 2
  512. else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
  513. // Can\'t detect in all other cases
  514. else
  515. {
  516. flashVer = -1;
  517. }
  518. return flashVer;
  519. }
  520. // When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
  521. function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision) {
  522. reqVer = parseFloat(reqMajorVer + "." + reqRevision);
  523. // loop backwards through the versions until we find the newest version
  524. for (i=25;i>0;i--) {
  525. if (isIE && isWin && !isOpera) {
  526. versionStr = VBGetSwfVer(i);
  527. } else {
  528. versionStr = JSGetSwfVer(i);
  529. }
  530. if (versionStr == -1 ) {
  531. return false;
  532. } else if (versionStr != 0) {
  533. if(isIE && isWin && !isOpera) {
  534. tempArray = versionStr.split(" ");
  535. tempString = tempArray[1];
  536. versionArray = tempString .split(",");
  537. } else {
  538. versionArray = versionStr.split(".");
  539. }
  540. versionMajor = versionArray[0];
  541. versionMinor = versionArray[1];
  542. versionRevision = versionArray[2];
  543. versionString = versionMajor + "." + versionRevision; // 7.0r24 == 7.24
  544. versionNum = parseFloat(versionString);
  545. // is the major.revision >= requested major.revision AND the minor version >= requested minor
  546. if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
  547. return true;
  548. } else {
  549. return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
  550. }
  551. }
  552. }
  553. }
  554. // -->
  555. </script>';
  556. $s .= '<tr><td valign="top" colspan="2" width="520"><table><tr><td width="520">
  557. <script language="JavaScript" type="text/javascript">
  558. <!--
  559. // Version check based upon the values entered above in "Globals"
  560. var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
  561. // Check to see if the version meets the requirements for playback
  562. if (hasReqestedVersion) { // if we\'ve detected an acceptable version
  563. var oeTags = \'<object type="application/x-shockwave-flash" data="../plugin/hotspot/'.$swf_file.'.swf?modifyAnswers='.$questionId.'&amp;canClick:'.$canClick.'" width="600" height="'.$swf_height.'">\'
  564. + \'<param name="wmode" value="transparent">\'
  565. + \'<param name="movie" value="../plugin/hotspot/'.$swf_file.'.swf?modifyAnswers='.$questionId.'&amp;canClick:'.$canClick.'" />\'
  566. + \'<\/object>\';
  567. document.write(oeTags); // embed the Flash Content SWF when all tests are passed
  568. } else { // flash is too old or we can\'t detect the plugin
  569. var alternateContent = "Error<br \/>"
  570. + "Hotspots requires Macromedia Flash 7.<br \/>"
  571. + "<a href=\"http://www.macromedia.com/go/getflash/\">Get Flash<\/a>";
  572. document.write(alternateContent); // insert non-flash content
  573. }
  574. // -->
  575. </script>
  576. </td>
  577. <td valign="top" align="left">'.$answer_list.'</td></tr>
  578. </table>
  579. </td></tr>';
  580. echo $s;
  581. }
  582. echo '</table>';
  583. return $nbrAnswers;
  584. }
  585. function get_exercise_track_exercise_info($exe_id) {
  586. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  587. $TBL_TRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  588. $exe_id = intval($exe_id);
  589. $result = array();
  590. if (!empty($exe_id)) {
  591. $sql_fb_type = 'SELECT * FROM '.$TBL_EXERCICES.' as e INNER JOIN '.$TBL_TRACK_EXERCICES.' as te ON (e.id=te.exe_exo_id) WHERE te.exe_id='.$exe_id;
  592. $res_fb_type = Database::query($sql_fb_type);
  593. $result = Database::fetch_array($res_fb_type, 'ASSOC');
  594. }
  595. return $result;
  596. }
  597. /**
  598. * Validates the time control key
  599. */
  600. function exercise_time_control_is_valid($exercise_id) {
  601. $course_id = api_get_course_int_id();
  602. $exercise_id = intval($exercise_id);
  603. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  604. $sql = "SELECT expired_time FROM $TBL_EXERCICES WHERE c_id = $course_id AND id = $exercise_id";
  605. $result = Database::query($sql);
  606. $row = Database::fetch_array($result, 'ASSOC');
  607. if (!empty($row['expired_time']) ) {
  608. $current_expired_time_key = get_time_control_key($exercise_id);
  609. if (isset($_SESSION['expired_time'][$current_expired_time_key])) {
  610. $current_time = time();
  611. $expired_time = api_strtotime($_SESSION['expired_time'][$current_expired_time_key], 'UTC');
  612. $total_time_allowed = $expired_time + 30;
  613. //error_log('expired time converted + 30: '.$total_time_allowed);
  614. //error_log('$current_time: '.$current_time);
  615. if ($total_time_allowed < $current_time) {
  616. return false;
  617. }
  618. return true;
  619. } else {
  620. return false;
  621. }
  622. } else {
  623. return true;
  624. }
  625. }
  626. /**
  627. Deletes the time control token
  628. */
  629. function exercise_time_control_delete($exercise_id) {
  630. $current_expired_time_key = get_time_control_key($exercise_id);
  631. unset($_SESSION['expired_time'][$current_expired_time_key]);
  632. }
  633. /**
  634. Generates the time control key
  635. */
  636. function generate_time_control_key($exercise_id) {
  637. $exercise_id = intval($exercise_id);
  638. return api_get_course_int_id().'_'.api_get_session_id().'_'.$exercise_id.'_'.api_get_user_id();
  639. }
  640. /**
  641. Returns the time controller key
  642. @todo this function is the same as generate_time_control_key
  643. */
  644. function get_time_control_key($exercise_id) {
  645. $exercise_id = intval($exercise_id);
  646. return api_get_course_int_id().'_'.api_get_session_id().'_'.$exercise_id.'_'.api_get_user_id();
  647. }
  648. /**
  649. * @todo use this function instead of get_time_control_key
  650. */
  651. function get_session_time_control_key($exercise_id) {
  652. $time_control_key = get_time_control_key($exercise_id);
  653. $return_value = $_SESSION['expired_time'][$time_control_key];
  654. return $return_value;
  655. }
  656. /**
  657. * Gets count of exam results
  658. * @todo this function should be moved in a library + no global calls
  659. * Modified by hubert borderiou 08-11-2011
  660. */
  661. function get_count_exam_results() {
  662. // I know it's bad to add a static integer here... but
  663. // it factorise function get_exam_results_data
  664. // and I think it worths it.
  665. //This is not bad for the hardcoded value, this is bad because you call 2 times get_exam_results_data()! jm
  666. //@todo use a real count select I know the sql constructions are
  667. $tabres = get_exam_results_data(0, 9999999, 0, "ASC");
  668. return count($tabres);
  669. }
  670. /**
  671. * Gets the exam'data results
  672. * @todo this function should be moved in a library + no global calls
  673. */
  674. function get_exam_results_data($from, $number_of_items, $column, $direction) {
  675. //@todo replace all this globals
  676. global $is_allowedToEdit, $is_tutor, $filter_by_not_revised, $filter_by_revised, $documentPath, $filter, $filterByGroup;
  677. $is_tutor = api_is_allowed_to_edit(true);
  678. $is_tutor_course = api_is_course_tutor();
  679. $TBL_USER = Database :: get_main_table(TABLE_MAIN_USER);
  680. $TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
  681. $TBL_TRACK_EXERCICES = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  682. $TBL_TRACK_HOTPOTATOES = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  683. $TBL_TRACK_ATTEMPT = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  684. $TBL_TRACK_ATTEMPT_RECORDING= Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  685. $TBL_GROUP_REL_USER = Database :: get_course_table(TABLE_GROUP_USER);
  686. $session_id_and = ' AND te.session_id = ' . api_get_session_id() . ' ';
  687. $exercise_id = intval($_GET['exerciseId']);
  688. $exercise_where = '';
  689. if (!empty($exercise_id)) {
  690. $exercise_where .= ' AND te.exe_exo_id = '.$exercise_id.' ';
  691. }
  692. $hotpotatoe_where = '';
  693. if (!empty($_GET['path'])) {
  694. $hotpotatoe_path = Database::escape_string($_GET['path']);
  695. $hotpotatoe_where .= ' AND exe_name = "'.$hotpotatoe_path.'" ';
  696. }
  697. if ($is_allowedToEdit || $is_tutor) {
  698. $user_id_and = '';
  699. if (!empty ($_REQUEST['filter_by_user'])) {
  700. if ($_REQUEST['filter_by_user'] == 'all') {
  701. $user_id_and = " AND user_id LIKE '%'";
  702. } else {
  703. $user_id_and = " AND user_id = '" . intval($_REQUEST['filter_by_user']) . "' ";
  704. }
  705. }
  706. if ($_GET['gradebook'] == 'view') {
  707. $exercise_where_query = ' te.exe_exo_id = ce.id AND ';
  708. }
  709. $sqlFromOption = "";
  710. $sqlWhereOption = ""; // for hpsql
  711. $sql_inner_join_tbl_user = "";
  712. $sql_inner_join_tbl_track_exercices = "";
  713. $filterByGroup = intval($filterByGroup);
  714. //@todo fix to work with COURSE_RELATION_TYPE_RRHH in both queries
  715. //
  716. // Filter by test revised or not
  717. //
  718. switch ($filter) {
  719. case 0 : // filter_by_not_revised
  720. case 1 :
  721. $sql_inner_join_tbl_track_exercices = "
  722. (SELECT * FROM $TBL_TRACK_EXERCICES AS tte
  723. WHERE tte.exe_id NOT IN (SELECT DISTINCT exe_id FROM $TBL_TRACK_ATTEMPT_RECORDING))";
  724. break;
  725. case 2 : // filter_by_revised
  726. $sql_inner_join_tbl_track_exercices = "
  727. (SELECT * FROM $TBL_TRACK_EXERCICES AS tte
  728. WHERE tte.exe_id IN (SELECT DISTINCT exe_id FROM $TBL_TRACK_ATTEMPT_RECORDING))";
  729. break;
  730. default : // no filter for revised
  731. $sql_inner_join_tbl_track_exercices = $TBL_TRACK_EXERCICES;
  732. break;
  733. }
  734. //
  735. // Filter by group
  736. //
  737. switch ($filterByGroup) {
  738. case -1 : // no filter
  739. $sql_inner_join_tbl_user = $TBL_USER;
  740. break;
  741. case 0 : // user not in any group
  742. $sql_inner_join_tbl_user = "
  743. (SELECT u.user_id, firstname, lastname, email , username
  744. FROM $TBL_USER u
  745. WHERE
  746. u.user_id NOT IN (
  747. SELECT user_id
  748. FROM $TBL_GROUP_REL_USER
  749. WHERE c_id=".api_get_course_int_id()."))";
  750. $sqlFromOption = " ,$TBL_GROUP_REL_USER AS gru ";
  751. $sqlWhereOption = " AND user.user_id NOT IN ( SELECT user_id FROM $TBL_GROUP_REL_USER WHERE c_id=".api_get_course_int_id().") ";
  752. break;
  753. default : // user in group $filterByGroup
  754. $sql_inner_join_tbl_user = "
  755. (SELECT u.user_id, firstname, lastname, email, username
  756. FROM $TBL_USER u
  757. INNER JOIN $TBL_GROUP_REL_USER gru ON (
  758. gru.user_id=u.user_id
  759. AND gru.group_id=$filterByGroup
  760. AND gru.c_id=".api_get_course_int_id()."))";
  761. $sqlFromOption = " ,$TBL_GROUP_REL_USER AS gru ";
  762. $sqlWhereOption = " AND gru.c_id=".api_get_course_int_id()." AND gru.user_id=user.user_id AND gru.group_id=".Database :: escape_string($filterByGroup);
  763. break;
  764. }
  765. $first_and_last_name = api_is_western_name_order() ? "firstname as col0, lastname col1" : "lastname as col0, firstname as col1";
  766. // sql for chamilo-type tests for teacher / tutor view
  767. $sql = "SELECT
  768. user_id,
  769. $first_and_last_name,
  770. ce.title as col3,
  771. username as col2,
  772. te.exe_result as exresult ,
  773. te.exe_weighting as exweight,
  774. te.exe_date as exdate,
  775. te.exe_id as exid,
  776. email as exemail,
  777. te.start_date as col6,
  778. steps_counter as exstep,
  779. exe_user_id as excruid,
  780. te.exe_duration as exduration,
  781. propagate_neg
  782. FROM
  783. $TBL_EXERCICES AS ce
  784. INNER JOIN $sql_inner_join_tbl_track_exercices AS te ON (te.exe_exo_id = ce.id)
  785. INNER JOIN $sql_inner_join_tbl_user AS user ON (user.user_id = exe_user_id)
  786. WHERE
  787. te.status != 'incomplete'
  788. AND te.exe_cours_id='" . api_get_course_id() . "' $user_id_and $session_id_and
  789. AND ce.active <>-1
  790. AND orig_lp_id = 0
  791. AND orig_lp_item_id = 0
  792. AND ce.c_id=".api_get_course_int_id()."
  793. $exercise_where ";
  794. // sql for hotpotatoes tests for teacher / tutor view
  795. $hpsql = "SELECT
  796. $first_and_last_name ,
  797. username,
  798. tth.exe_name,
  799. tth.exe_result ,
  800. tth.exe_weighting,
  801. tth.exe_date
  802. FROM
  803. $TBL_TRACK_HOTPOTATOES tth,
  804. $TBL_USER user
  805. $sqlFromOption
  806. WHERE
  807. user.user_id=tth.exe_user_id
  808. AND tth.exe_cours_id = '" . api_get_course_id()."'
  809. $hotpotatoe_where
  810. $sqlWhereOption
  811. ORDER BY
  812. tth.exe_cours_id ASC,
  813. tth.exe_date DESC";
  814. } else {
  815. // get only this user's results
  816. $user_id_and = ' AND te.exe_user_id = ' . api_get_user_id() . ' ';
  817. /*$sql="SELECT ".(api_is_western_name_order() ? "firstname as col0, lastname col1" : "lastname as col0, firstname as col1").", ce.title as extitle, te.exe_result as exresult, " .
  818. "te.exe_weighting as exweight, te.exe_date as exdate, te.exe_id as exid, email as exemail, " .
  819. "te.start_date as exstart, steps_counter as exstep, cuser.user_id as excruid, te.exe_duration as exduration, ce.results_disabled as exdisabled
  820. FROM $TBL_EXERCICES AS ce , $TBL_TRACK_EXERCICES AS te, $TBL_USER AS user,$tbl_course_rel_user AS cuser
  821. WHERE user.user_id=cuser.user_id AND te.exe_exo_id = ce.id AND te.status != 'incomplete' AND cuser.user_id=te.exe_user_id
  822. AND te.exe_cours_id='" . Database :: escape_string($_cid) . "'
  823. AND cuser.relation_type<>".COURSE_RELATION_TYPE_RRHH." $user_id_and $session_id_and AND ce.active <>-1 AND" .
  824. " orig_lp_id = 0 AND orig_lp_item_id = 0 AND cuser.course_code=te.exe_cours_id ORDER BY col1, te.exe_cours_id ASC, ce.title ASC, te.exe_date DESC";*/
  825. $sql = "SELECT
  826. ce.title as col0,
  827. ce.title as col1,
  828. te.exe_duration as exduration,
  829. firstname,
  830. lastname,
  831. te.exe_result as exresult,
  832. te.exe_weighting as exweight,
  833. te.exe_date as exdate,
  834. te.exe_id as exid,
  835. email as exemail,
  836. te.start_date as col2,
  837. steps_counter as exstep,
  838. exe_user_id as excruid,
  839. ce.results_disabled as exdisabled,
  840. propagate_neg
  841. FROM
  842. $TBL_EXERCICES AS ce
  843. INNER JOIN $TBL_TRACK_EXERCICES AS te ON (te.exe_exo_id = ce.id)
  844. INNER JOIN $TBL_USER AS user ON (user.user_id = exe_user_id)
  845. WHERE
  846. te.status != 'incomplete'
  847. AND te.exe_cours_id='" . api_get_course_id() . "'
  848. $user_id_and $session_id_and
  849. AND ce.active <>-1
  850. AND orig_lp_id = 0
  851. AND orig_lp_item_id = 0
  852. AND ce.c_id= ".api_get_course_int_id()."
  853. $exercise_where";
  854. $hpsql = "SELECT '', '', '', exe_name, exe_result , exe_weighting, exe_date
  855. FROM $TBL_TRACK_HOTPOTATOES
  856. WHERE exe_user_id = '" . api_get_user_id() . "' AND exe_cours_id = '" . api_get_course_id() . "' $hotpotatoe_where
  857. ORDER BY exe_cours_id ASC, exe_date DESC";
  858. }
  859. $teacher_list = CourseManager::get_teacher_list_from_course_code(api_get_course_id());
  860. $teacher_id_list = array();
  861. foreach ($teacher_list as $teacher) {
  862. $teacher_id_list[] = $teacher['user_id'];
  863. }
  864. if (empty($hotpotatoe_where)) {
  865. $column = intval($column);
  866. $from = intval($from);
  867. $number_of_items = intval($number_of_items);
  868. $sql .= " ORDER BY col$column $direction ";
  869. $sql .= " LIMIT $from, $number_of_items";
  870. $results = array();
  871. $resx = Database::query($sql);
  872. while ($rowx = Database::fetch_array($resx,'ASSOC')) {
  873. $results[] = $rowx;
  874. }
  875. $list_info = array();
  876. // Print test results.
  877. $lang_nostartdate = get_lang('NoStartDate') . ' / ';
  878. if (is_array($results)) {
  879. $users_array_id = array();
  880. if ($_GET['gradebook'] == 'view') {
  881. $filter_by_no_revised = true;
  882. $from_gradebook = true;
  883. }
  884. $sizeof = count($results);
  885. $user_list_id = array ();
  886. $user_last_name = '';
  887. $user_first_name = '';
  888. $quiz_name_list = '';
  889. $duration_list = '';
  890. $date_list = '';
  891. $result_list = '';
  892. $more_details_list = '';
  893. for ($i = 0; $i < $sizeof; $i++) {
  894. $revised = false;
  895. $sql_exe = 'SELECT exe_id FROM ' . $TBL_TRACK_ATTEMPT_RECORDING . ' WHERE author != "" AND exe_id = ' . Database :: escape_string($results[$i]['exid']) .' LIMIT 1';
  896. $query = Database::query($sql_exe);
  897. if (Database :: num_rows($query) > 0) {
  898. $revised = true;
  899. }
  900. if ($from_gradebook && ($is_allowedToEdit || $is_tutor)) {
  901. if (in_array($results[$i]['col2'] . $results[$i]['col0'] . $results[$i]['col1'], $users_array_id)) {
  902. continue;
  903. }
  904. $users_array_id[] = $results[$i]['col2'] . $results[$i]['col0'] . $results[$i]['col1'];
  905. }
  906. if ($is_allowedToEdit || $is_tutor) {
  907. $user_first_name = $results[$i]['col0'];
  908. $user_last_name = $results[$i]['col1'];
  909. $user_login = $results[$i]['col2'];
  910. $user = $results[$i]['col0'] . $results[$i]['col1'];
  911. $test = $results[$i]['col3'];
  912. $user_groups = displayGroupsForUser('<br/>', $results[$i]['user_id']);
  913. } else {
  914. $user_first_name = $results[$i]['firstname'];
  915. $user_last_name = $results[$i]['lastname'];
  916. $user = $results[$i]['firstname'] . $results[$i]['lastname'];
  917. $test = $results[$i]['col0'];
  918. }
  919. $user_list_id[] = $results[$i]['excruid'];
  920. $id = $results[$i]['exid'];
  921. $quiz_name_list = $test;
  922. $dt = api_convert_and_format_date($results[$i]['exweight']);
  923. $res = $results[$i]['exresult'];
  924. $duration = intval($results[$i]['exduration']);
  925. // we filter the results if we have the permission to
  926. if (isset ($results[$i]['exdisabled']))
  927. $result_disabled = intval($results[$i]['exdisabled']);
  928. else
  929. $result_disabled = 0;
  930. if ($result_disabled == 0) {
  931. $add_start_date = $lang_nostartdate;
  932. if ($is_allowedToEdit || $is_tutor) {
  933. //var_dump($results[$i]);
  934. $user = $results[$i]['col0'] . $results[$i]['col1'];
  935. $start_date = $results[$i]['col6'];
  936. } else {
  937. $start_date = $results[$i]['col2'];
  938. }
  939. if ($start_date != "0000-00-00 00:00:00") {
  940. $start_date_timestamp = api_strtotime($start_date);
  941. $exe_date_timestamp = api_strtotime($results[$i]['exdate']);
  942. $my_duration = ceil((($exe_date_timestamp - $start_date_timestamp) / 60));
  943. //var_dump($start_date .' - '.$results[$i]['exdate'].' - '.$my_duration);
  944. if ($my_duration == 1 ) {
  945. $duration_list = $my_duration . ' ' . get_lang('MinMinute');
  946. } else {
  947. $duration_list = $my_duration. ' ' . get_lang('MinMinutes');
  948. }
  949. if ($results[$i]['exstep'] > 1) {
  950. //echo ' ( ' . $results[$i][8] . ' ' . get_lang('Steps') . ' )';
  951. $duration_list = ' ( ' . $results[$i]['exstep'] . ' ' . get_lang('Steps') . ' )';
  952. }
  953. //$add_start_date = api_convert_and_format_date($start_date) . ' / ';
  954. } else {
  955. $duration_list = get_lang('NoLogOfDuration');
  956. //echo get_lang('NoLogOfDuration');
  957. }
  958. // Date conversion
  959. if ($is_allowedToEdit || $is_tutor) {
  960. $date_list = api_get_local_time($results[$i]['col6']). ' / ' . api_get_local_time($results[$i]['exdate']);
  961. } else {
  962. $date_list = api_get_local_time($results[$i]['col2']). ' / ' . api_get_local_time($results[$i]['exdate']);
  963. }
  964. // there are already a duration test period calculated??
  965. //echo '<td>'.sprintf(get_lang('DurationFormat'), $duration).'</td>';
  966. // if the float look like 10.00 we show only 10
  967. $my_res = $results[$i]['exresult'];
  968. $my_total = $results[$i]['exweight'];
  969. if (!$results[$i]['propagate_neg'] && $my_res < 0) {
  970. $my_res = 0;
  971. }
  972. $ex = show_score($my_res, $my_total);
  973. $result_list = $ex;
  974. $html_link = '';
  975. if ($is_allowedToEdit || $is_tutor) {
  976. if (in_array($results[$i]['excruid'], $teacher_id_list)) {
  977. $html_link.= Display::return_icon('teachers.gif', get_lang('Teacher'));
  978. }
  979. if ($revised) {
  980. $html_link.= "<a href='exercise_show.php?".api_get_cidreq()."&action=edit&id=$id'>".Display :: return_icon('edit.png', get_lang('Edit'), array(), 22);
  981. $html_link.= '&nbsp;';
  982. } else {
  983. $html_link.="<a href='exercise_show.php?".api_get_cidreq()."&action=qualify&id=$id'>".Display :: return_icon('quiz.gif', get_lang('Qualify'));
  984. $html_link.='&nbsp;';
  985. }
  986. $html_link.="</a>";
  987. if ($is_allowedToEdit) {
  988. if ($filter==2){
  989. $html_link.=' <a href="exercise_history.php?'.api_get_cidreq().'&exe_id=' . $id . '">' .Display :: return_icon('history.gif', get_lang('ViewHistoryChange')).'</a>';
  990. }
  991. }
  992. if (api_is_platform_admin() || $is_tutor) {
  993. $html_link.=' <a href="exercise_report.php?'.api_get_cidreq().'&filter_by_user='.intval($_GET['filter_by_user']).'&filter=' . $filter . '&exerciseId='.$exercise_id.'&delete=delete&did=' . $id . '" onclick="javascript:if(!confirm(\'' . sprintf(get_lang('DeleteAttempt'), $user, $dt) . '\')) return false;">'.Display :: return_icon('delete.png', get_lang('Delete')).'</a>';
  994. $html_link.='&nbsp;';
  995. }
  996. } else {
  997. $attempt_url = api_get_path(WEB_CODE_PATH).'exercice/result.php?'.api_get_cidreq().'&id='.$results[$i]['exid'].'&id_session='.api_get_session_id().'&height=500&width=750';
  998. $attempt_link = Display::url(get_lang('Show'), $attempt_url, array('class'=>'thickbox a_button white small'))."&nbsp;&nbsp;&nbsp;";
  999. $html_link.= $attempt_link;
  1000. if ($revised) {
  1001. $html_link.= Display::span(get_lang('Validated'), array('class'=>'label_tag success'));
  1002. } else {
  1003. $html_link.= Display::span(get_lang('NotValidated'), array('class'=>'label_tag notice'));
  1004. }
  1005. }
  1006. $more_details_list = $html_link;
  1007. if ($is_allowedToEdit || $is_tutor) {
  1008. $revised = $revised ? get_lang('Revised') : get_lang('NotRevised');
  1009. $list_info[] = array($user_first_name,$user_last_name,$user_login,$user_groups,$quiz_name_list,$duration_list,$date_list,$result_list, $revised, $more_details_list);
  1010. } else {
  1011. $list_info[] = array($quiz_name_list,$duration_list,$date_list,$result_list,$more_details_list);
  1012. }
  1013. }
  1014. }
  1015. }
  1016. } else {
  1017. //echo $hpsql; var_dump($hpsql);
  1018. $hpresults = getManyResultsXCol($hpsql, 6);
  1019. // Print HotPotatoes test results.
  1020. if (is_array($hpresults)) {
  1021. for ($i = 0; $i < sizeof($hpresults); $i++) {
  1022. $hp_title = GetQuizName($hpresults[$i][3], $documentPath);
  1023. if ($hp_title == '') {
  1024. $hp_title = basename($hpresults[$i][3]);
  1025. }
  1026. //var_dump($hpresults[$i]);
  1027. $hp_date = api_get_local_time($hpresults[$i][6], null, date_default_timezone_get());
  1028. $hp_result = round(($hpresults[$i][4] / ($hpresults[$i][5] != 0 ? $hpresults[$i][5] : 1)) * 100, 2).'% ('.$hpresults[$i][4].' / '.$hpresults[$i][5].')';
  1029. if ($is_allowedToEdit || $is_tutor) {
  1030. $list_info[] = array($hpresults[$i][0], $hpresults[$i][1], $hpresults[$i][2], '', $hp_title, '-', $hp_date , $hp_result , '-');
  1031. } else {
  1032. $list_info[] = array($hp_title, '-', $hp_date , $hp_result , '-');
  1033. }
  1034. }
  1035. }
  1036. }
  1037. return $list_info;
  1038. }
  1039. /**
  1040. * Converts the score with the exercise_max_note and exercise_min_score the platform settings + formats the results using the float_format function
  1041. *
  1042. * @param float score
  1043. * @param float weight
  1044. * @param bool show porcentage or not
  1045. * @param bool use or not the platform settings
  1046. * @return string an html with the score modified
  1047. */
  1048. function show_score($score, $weight, $show_percentage = true, $use_platform_settings = true) {
  1049. if (is_null($score) && is_null($weight)) {
  1050. return '-';
  1051. }
  1052. $html = '';
  1053. $score_rounded = $score;
  1054. $max_note = api_get_setting('exercise_max_score');
  1055. $min_note = api_get_setting('exercise_min_score');
  1056. if ($use_platform_settings) {
  1057. if ($max_note != '' && $min_note != '') {
  1058. if (!empty($weight) && intval($weight) != 0) {
  1059. $score = $min_note + ($max_note - $min_note) * $score /$weight;
  1060. } else {
  1061. $score = $min_note;
  1062. }
  1063. $weight = $max_note;
  1064. }
  1065. }
  1066. $score_rounded = float_format($score, 1);
  1067. $weight = float_format($weight, 1);
  1068. if ($show_percentage) {
  1069. $parent = '(' . $score_rounded . ' / ' . $weight . ')';
  1070. $html = float_format(($score / ($weight != 0 ? $weight : 1)) * 100, 1) . "% $parent";
  1071. } else {
  1072. $html = $score_rounded . ' / ' . $weight;
  1073. }
  1074. return $html;
  1075. }
  1076. /**
  1077. * Converts a numeric value in a percentage example 0.66666 to 66.67 %
  1078. * @param $value
  1079. * @return float Converted number
  1080. */
  1081. function convert_to_percentage($value) {
  1082. $return = '-';
  1083. if ($value != '') {
  1084. $return = float_format($value * 100, 1).' %';
  1085. }
  1086. return $return;
  1087. }
  1088. /**
  1089. * Converts a score/weight values to the platform scale
  1090. * @param float score
  1091. * @param float weight
  1092. * @return float the score rounded converted to the new range
  1093. */
  1094. function convert_score($score, $weight) {
  1095. $max_note = api_get_setting('exercise_max_score');
  1096. $min_note = api_get_setting('exercise_min_score');
  1097. if ($score != '' && $weight != '') {
  1098. if ($max_note != '' && $min_note != '') {
  1099. if (!empty($weight)) {
  1100. $score = $min_note + ($max_note - $min_note) * $score / $weight;
  1101. } else {
  1102. $score = $min_note;
  1103. }
  1104. }
  1105. }
  1106. $score_rounded = float_format($score, 1);
  1107. return $score_rounded;
  1108. }
  1109. /**
  1110. * Getting all active exercises from a course from a session (if a session_id is provided we will show all the exercises in the course + all exercises in the session)
  1111. * @param array course data
  1112. * @param int session id
  1113. * @return array array with exercise data
  1114. */
  1115. function get_all_exercises($course_info = null, $session_id = 0, $check_dates = false) {
  1116. $TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
  1117. $course_id = api_get_course_int_id();
  1118. if (!empty($course_info) && !empty($course_info['real_id'])) {
  1119. $course_id = $course_info['real_id'];
  1120. }
  1121. if ($session_id == -1) {
  1122. $session_id = 0;
  1123. }
  1124. if ($session_id == 0) {
  1125. $conditions = array('where'=>array('active = ? AND session_id = ? AND c_id = ? '=>array('1', $session_id, $course_id)), 'order'=>'title');
  1126. } else {
  1127. //All exercises
  1128. $conditions = array('where'=>array('active = ? AND (session_id = 0 OR session_id = ? ) AND c_id = ? ' => array('1', $session_id, $course_id)), 'order'=>'title');
  1129. }
  1130. return Database::select('*',$TBL_EXERCICES, $conditions);
  1131. }
  1132. /**
  1133. * Getting all active exercises from a course from a session (if a session_id is provided we will show all the exercises in the course + all exercises in the session)
  1134. * @param array course data
  1135. * @param int session id
  1136. * @param int course c_id
  1137. * @return array array with exercise data
  1138. * modified by Hubert Borderiou
  1139. */
  1140. function get_all_exercises_for_course_id($course_info = null, $session_id = 0, $course_id=0) {
  1141. $TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
  1142. if ($session_id == -1) {
  1143. $session_id = 0;
  1144. }
  1145. if ($session_id == 0) {
  1146. $conditions = array('where'=>array('active = ? AND session_id = ? AND c_id = ?'=>array('1', $session_id, $course_id)), 'order'=>'title');
  1147. } else {
  1148. //All exercises
  1149. $conditions = array('where'=>array('active = ? AND (session_id = 0 OR session_id = ? ) AND c_id=?' =>array('1', $session_id, $course_id)), 'order'=>'title');
  1150. }
  1151. return Database::select('*',$TBL_EXERCICES, $conditions);
  1152. }
  1153. /**
  1154. * Gets the position of the score based in a given score (result/weight) and the exe_id based in the user list
  1155. * (NO Exercises in LPs )
  1156. * @param float user score to be compared *attention* $my_score = score/weight and not just the score
  1157. * @param int exe id of the exercise (this is necesary because if 2 students have the same score the one with the minor exe_id will have a best position, just to be fair and FIFO)
  1158. * @param int exercise id
  1159. * @param string course code
  1160. * @param int session id
  1161. * @return int the position of the user between his friends in a course (or course within a session)
  1162. */
  1163. function get_exercise_result_ranking($my_score, $my_exe_id, $exercise_id, $course_code, $session_id = 0, $user_list, $return_string = true) {
  1164. //No score given we return
  1165. if (is_null($my_score)) {
  1166. return '-';
  1167. }
  1168. if (empty($user_list)) {
  1169. return '-';
  1170. }
  1171. $best_attempts = array();
  1172. foreach($user_list as $user_data) {
  1173. $user_id = $user_data['user_id'];
  1174. $best_attempts[$user_id]= get_best_attempt_by_user($user_id, $exercise_id, $course_code, $session_id);
  1175. }
  1176. $position_data = array();
  1177. if (empty($best_attempts)) {
  1178. return 1;
  1179. } else {
  1180. $position = 1;
  1181. $my_ranking = array();
  1182. foreach($best_attempts as $user_id => $result) {
  1183. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1184. $my_ranking[$user_id] = $result['exe_result']/$result['exe_weighting'];
  1185. } else {
  1186. $my_ranking[$user_id] = 0;
  1187. }
  1188. }
  1189. //if (!empty($my_ranking)) {
  1190. asort($my_ranking);
  1191. $position = count($my_ranking);
  1192. if (!empty($my_ranking)) {
  1193. foreach($my_ranking as $user_id => $ranking) {
  1194. if ($my_score >= $ranking) {
  1195. if ($my_score == $ranking) {
  1196. $exe_id = $best_attempts[$user_id]['exe_id'];
  1197. if ($my_exe_id < $exe_id) {
  1198. $position--;
  1199. }
  1200. } else {
  1201. $position--;
  1202. }
  1203. }
  1204. }
  1205. }
  1206. //}
  1207. $return_value = array('position'=>$position, 'count'=>count($my_ranking));
  1208. //var_dump($my_score, $my_ranking);
  1209. if ($return_string) {
  1210. if (!empty($position) && !empty($my_ranking)) {
  1211. $return_value = $position.'/'.count($my_ranking);
  1212. } else {
  1213. $return_value = '-';
  1214. }
  1215. }
  1216. return $return_value;
  1217. }
  1218. }
  1219. /**
  1220. * Gets the position of the score based in a given score (result/weight) and the exe_id based in all attempts
  1221. * (NO Exercises in LPs ) old funcionality by attempt
  1222. * @param float user score to be compared attention => score/weight
  1223. * @param int exe id of the exercise (this is necesary because if 2 students have the same score the one with the minor exe_id will have a best position, just to be fair and FIFO)
  1224. * @param int exercise id
  1225. * @param string course code
  1226. * @param int session id
  1227. * @return int the position of the user between his friends in a course (or course within a session)
  1228. */
  1229. function get_exercise_result_ranking_by_attempt($my_score, $my_exe_id, $exercise_id, $course_code, $session_id = 0, $return_string = true) {
  1230. if (empty($session_id)) {
  1231. $session_id = 0;
  1232. }
  1233. if (is_null($my_score)) {
  1234. return '-';
  1235. }
  1236. $user_results = get_all_exercise_results($exercise_id, $course_code, $session_id, false);
  1237. $position_data = array();
  1238. if (empty($user_results)) {
  1239. return 1;
  1240. } else {
  1241. $position = 1;
  1242. $my_ranking = array();
  1243. foreach($user_results as $result) {
  1244. //print_r($result);
  1245. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1246. $my_ranking[$result['exe_id']] = $result['exe_result']/$result['exe_weighting'];
  1247. } else {
  1248. $my_ranking[$result['exe_id']] = 0;
  1249. }
  1250. }
  1251. asort($my_ranking);
  1252. $position = count($my_ranking);
  1253. if (!empty($my_ranking)) {
  1254. foreach($my_ranking as $exe_id=>$ranking) {
  1255. if ($my_score >= $ranking) {
  1256. if ($my_score == $ranking) {
  1257. if ($my_exe_id < $exe_id) {
  1258. $position--;
  1259. }
  1260. } else {
  1261. $position--;
  1262. }
  1263. }
  1264. }
  1265. }
  1266. $return_value = array('position'=>$position, 'count'=>count($my_ranking));
  1267. //var_dump($my_score, $my_ranking);
  1268. if ($return_string) {
  1269. if (!empty($position) && !empty($my_ranking)) {
  1270. return $position.'/'.count($my_ranking);
  1271. }
  1272. }
  1273. return $return_value;
  1274. }
  1275. }
  1276. /*
  1277. * Get the best attempt in a exercise (NO Exercises in LPs )
  1278. */
  1279. function get_best_attempt_in_course($exercise_id, $course_code, $session_id) {
  1280. $user_results = get_all_exercise_results($exercise_id, $course_code, $session_id, false);
  1281. $best_score_data = array();
  1282. $best_score = 0;
  1283. if (!empty($user_results)) {
  1284. foreach($user_results as $result) {
  1285. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1286. $score = $result['exe_result']/$result['exe_weighting'];
  1287. if ($score >= $best_score) {
  1288. $best_score = $score;
  1289. $best_score_data = $result;
  1290. }
  1291. }
  1292. }
  1293. }
  1294. return $best_score_data;
  1295. }
  1296. /*
  1297. * Get the best score in a exercise (NO Exercises in LPs )
  1298. */
  1299. function get_best_attempt_by_user($user_id, $exercise_id, $course_code, $session_id) {
  1300. $user_results = get_all_exercise_results($exercise_id, $course_code, $session_id, false);
  1301. $best_score_data = array();
  1302. $best_score = 0;
  1303. if (!empty($user_results)) {
  1304. foreach($user_results as $result) {
  1305. if ($result['exe_user_id'] != $user_id) continue;
  1306. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1307. $score = $result['exe_result']/$result['exe_weighting'];
  1308. if ($score >= $best_score) {
  1309. $best_score = $score;
  1310. $best_score_data = $result;
  1311. }
  1312. }
  1313. }
  1314. }
  1315. return $best_score_data;
  1316. }
  1317. /**
  1318. * Get average score (NO Exercises in LPs )
  1319. * @param int exercise id
  1320. * @param string course code
  1321. * @param int session id
  1322. * @return float Average score
  1323. */
  1324. function get_average_score($exercise_id, $course_code, $session_id) {
  1325. $user_results = get_all_exercise_results($exercise_id, $course_code, $session_id);
  1326. $avg_score_data = array();
  1327. $avg_score = 0;
  1328. if (!empty($user_results)) {
  1329. foreach($user_results as $result) {
  1330. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1331. $score = $result['exe_result']/$result['exe_weighting'];
  1332. $avg_score +=$score;
  1333. }
  1334. }
  1335. $avg_score = float_format($avg_score / count($user_results), 1);
  1336. }
  1337. return $avg_score;
  1338. }
  1339. /**
  1340. * Get average score by score (NO Exercises in LPs )
  1341. * @param int exercise id
  1342. * @param string course code
  1343. * @param int session id
  1344. * @return float Average score
  1345. */
  1346. function get_average_score_by_course($course_code, $session_id) {
  1347. $user_results = get_all_exercise_results_by_course($course_code, $session_id, false);
  1348. //echo $course_code.' - '.$session_id.'<br />';
  1349. $avg_score = 0;
  1350. if (!empty($user_results)) {
  1351. foreach($user_results as $result) {
  1352. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1353. $score = $result['exe_result']/$result['exe_weighting'];
  1354. //var_dump($score);
  1355. $avg_score +=$score;
  1356. }
  1357. }
  1358. //We asume that all exe_weighting
  1359. //$avg_score = show_score( $avg_score / count($user_results) , $result['exe_weighting']);
  1360. $avg_score = ($avg_score / count($user_results));
  1361. }
  1362. //var_dump($avg_score);
  1363. return $avg_score;
  1364. }
  1365. function get_average_score_by_course_by_user($user_id, $course_code, $session_id) {
  1366. $user_results = get_all_exercise_results_by_user($user_id, $course_code, $session_id);
  1367. $avg_score = 0;
  1368. if (!empty($user_results)) {
  1369. foreach($user_results as $result) {
  1370. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1371. $score = $result['exe_result']/$result['exe_weighting'];
  1372. $avg_score +=$score;
  1373. }
  1374. }
  1375. //We asume that all exe_weighting
  1376. //$avg_score = show_score( $avg_score / count($user_results) , $result['exe_weighting']);
  1377. $avg_score = ($avg_score / count($user_results));
  1378. }
  1379. return $avg_score;
  1380. }
  1381. /**
  1382. * Get average score by score (NO Exercises in LPs )
  1383. * @param int exercise id
  1384. * @param string course code
  1385. * @param int session id
  1386. * @return float Best average score
  1387. */
  1388. function get_best_average_score_by_exercise($exercise_id, $course_code, $session_id, $user_count) {
  1389. $user_results = get_best_exercise_results_by_user($exercise_id, $course_code, $session_id);
  1390. $avg_score = 0;
  1391. if (!empty($user_results)) {
  1392. foreach($user_results as $result) {
  1393. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1394. $score = $result['exe_result']/$result['exe_weighting'];
  1395. $avg_score +=$score;
  1396. }
  1397. }
  1398. //We asume that all exe_weighting
  1399. //$avg_score = show_score( $avg_score / count($user_results) , $result['exe_weighting']);
  1400. //$avg_score = ($avg_score / count($user_results));
  1401. if(!empty($user_count)) {
  1402. $avg_score = float_format($avg_score / $user_count, 1) * 100;
  1403. } else {
  1404. $avg_score = 0;
  1405. }
  1406. }
  1407. return $avg_score;
  1408. }
  1409. function get_exercises_to_be_taken($course_code, $session_id) {
  1410. $course_info = api_get_course_info($course_code);
  1411. $exercises = get_all_exercises($course_info, $session_id);
  1412. $result = array();
  1413. $now = time() + 15*24*60*60;
  1414. foreach($exercises as $exercise_item) {
  1415. if (isset($exercise_item['end_time']) && !empty($exercise_item['end_time']) && $exercise_item['end_time'] != '0000-00-00 00:00:00' && api_strtotime($exercise_item['end_time']) < $now) {
  1416. $result[] = $exercise_item;
  1417. }
  1418. }
  1419. return $result;
  1420. }
  1421. /**
  1422. * Get student results (only in completed exercises) stats by question
  1423. * @param int question id
  1424. * @param int exercise id
  1425. * @param string course code
  1426. * @param int session id
  1427. *
  1428. * */
  1429. function get_student_stats_by_question($question_id, $exercise_id, $course_code, $session_id) {
  1430. $track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1431. $track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1432. $question_id = intval($question_id);
  1433. $exercise_id = intval($exercise_id);
  1434. $course_code = Database::escape_string($course_code);
  1435. $session_id = intval($session_id);
  1436. $sql = "SELECT count(exe_user_id) as users, MAX(marks) as max , MIN(marks) as min, AVG(marks) as average
  1437. FROM $track_exercises e INNER JOIN $track_attempt a ON (a.exe_id = e.exe_id)
  1438. WHERE exe_exo_id = $exercise_id AND
  1439. course_code = '$course_code' AND
  1440. e.session_id = $session_id AND
  1441. question_id = $question_id AND status = '' LIMIT 1";
  1442. $result = Database::query($sql);
  1443. $return = array();
  1444. if ($result) {
  1445. $return = Database::fetch_array($result, 'ASSOC');
  1446. }
  1447. return $return;
  1448. }
  1449. // ---------------------------------------------------------
  1450. // return the HTML code for a menu with students group
  1451. // @input : $in_name : is the name and the id of the <select>
  1452. // $in_default : default value for option
  1453. // @return : the html code of the <select>
  1454. // ---------------------------------------------------------
  1455. function displayGroupMenu($in_name, $in_default, $in_onchange="") {
  1456. // check the default value of option
  1457. $tabSelected = array($in_default => " selected='selected' ");
  1458. $res = "";
  1459. $res .= "<select name='$in_name' id='$in_name' onchange='".$in_onchange."' >";
  1460. $res .= "<option value='-1'".$tabSelected["-1"].">-- ".get_lang('AllGroups')." --</option>";
  1461. $res .= "<option value='0'".$tabSelected["0"].">- ".get_lang('NotInAGroup')." -</option>";
  1462. $tabGroups = GroupManager::get_group_list();
  1463. $currentCatId = 0;
  1464. for ($i=0; $i < count($tabGroups); $i++) {
  1465. $tabCategory = GroupManager::get_category_from_group($tabGroups[$i]["id"]);
  1466. if ($tabCategory["id"] != $currentCatId) {
  1467. $res .= "<option value='-1' disabled='disabled'>".$tabCategory["title"]."</option>";
  1468. $currentCatId = $tabCategory["id"];
  1469. }
  1470. $res .= "<option ".$tabSelected[$tabGroups[$i]["id"]]."style='margin-left:40px' value='".$tabGroups[$i]["id"]."'>".$tabGroups[$i]["name"]."</option>";
  1471. }
  1472. $res .= "</select>";
  1473. return $res;
  1474. }
  1475. // ------------------------------------------------------
  1476. // return a list of group for user with user_id=in_userid
  1477. // separated with in_separator
  1478. // --------------------------------------------------
  1479. function displayGroupsForUser($in_separator, $in_userid) {
  1480. $res = implode($in_separator, GroupManager::get_user_group_name($in_userid));
  1481. if ($res == "") {
  1482. $res = "<div style='text-align:center'>-</div>";
  1483. }
  1484. return $res;
  1485. }