exercise.lib.php 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Exercise library
  5. * @todo move this in exercise.class or question.class depending of the functions.
  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. use ChamiloSession as Session;
  17. class ExerciseLib
  18. {
  19. /**
  20. * @param int $exe_id
  21. * @return array
  22. */
  23. public static function get_exercise_track_exercise_info($exe_id)
  24. {
  25. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  26. $TBL_TRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  27. $TBL_COURSE = Database::get_main_table(TABLE_MAIN_COURSE);
  28. $exe_id = intval($exe_id);
  29. $result_array = array();
  30. if (!empty($exe_id)) {
  31. $sql = "SELECT q.*, tee.*
  32. FROM $TBL_EXERCICES as q
  33. INNER JOIN $TBL_TRACK_EXERCICES as tee
  34. ON q.iid = tee.exe_exo_id
  35. INNER JOIN $TBL_COURSE c
  36. ON c.id = tee.c_id
  37. WHERE tee.exe_id = $exe_id
  38. AND q.c_id = c.id";
  39. $result = Database::query($sql);
  40. $result_array = Database::fetch_array($result, 'ASSOC');
  41. }
  42. return $result_array;
  43. }
  44. /**
  45. * Validates the time control key
  46. */
  47. public static function exercise_time_control_is_valid($exercise_id, $lp_id = 0, $lp_item_id = 0, $course_id = null, $session_id = null)
  48. {
  49. if (!$course_id) {
  50. $course_id = api_get_course_int_id();
  51. }
  52. if (!$session_id) {
  53. $session_id = api_get_session_id();
  54. }
  55. $exercise_id = intval($exercise_id);
  56. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  57. $sql = "SELECT expired_time FROM $TBL_EXERCICES WHERE c_id = $course_id AND iid = $exercise_id";
  58. $result = Database::query($sql);
  59. $row = Database::fetch_array($result, 'ASSOC');
  60. if (!empty($row['expired_time'])) {
  61. $current_expired_time_key = ExerciseLib::get_time_control_key($exercise_id, $lp_id, $lp_item_id, $course_id, $session_id);
  62. if (isset($_SESSION['expired_time'][$current_expired_time_key])) {
  63. $current_time = time();
  64. $expired_time = api_strtotime($_SESSION['expired_time'][$current_expired_time_key], 'UTC');
  65. $total_time_allowed = $expired_time + 30;
  66. //error_log('expired time converted + 30: '.$total_time_allowed);
  67. //error_log('$current_time: '.$current_time);
  68. if ($total_time_allowed < $current_time) {
  69. return false;
  70. }
  71. return true;
  72. } else {
  73. return false;
  74. }
  75. } else {
  76. return true;
  77. }
  78. }
  79. /**
  80. Deletes the time control token
  81. */
  82. public static function exercise_time_control_delete($exercise_id, $lp_id = 0, $lp_item_id = 0)
  83. {
  84. $current_expired_time_key = self::get_time_control_key($exercise_id, $lp_id, $lp_item_id);
  85. unset($_SESSION['expired_time'][$current_expired_time_key]);
  86. }
  87. /**
  88. Generates the time control key
  89. */
  90. public static function get_time_control_key($exercise_id, $lp_id = 0, $lp_item_id = 0, $course_id = null, $session_id = null)
  91. {
  92. $exercise_id = intval($exercise_id);
  93. $lp_id = intval($lp_id);
  94. $lp_item_id = intval($lp_item_id);
  95. if (!$course_id) {
  96. $course_id = api_get_course_int_id();
  97. }
  98. if (!$session_id) {
  99. $session_id = api_get_session_id();
  100. }
  101. return $course_id.'_'.$session_id.'_'.$exercise_id.'_'.api_get_user_id().'_'.$lp_id.'_'.$lp_item_id;
  102. }
  103. /**
  104. * Get session time control
  105. */
  106. public static function get_session_time_control_key($exercise_id, $lp_id = 0, $lp_item_id = 0)
  107. {
  108. $return_value = 0;
  109. $time_control_key = self::get_time_control_key($exercise_id, $lp_id, $lp_item_id);
  110. if (isset($_SESSION['expired_time']) && isset($_SESSION['expired_time'][$time_control_key])) {
  111. $return_value = $_SESSION['expired_time'][$time_control_key];
  112. }
  113. return $return_value;
  114. }
  115. /**
  116. * Gets count of exam results
  117. * @todo this public static function should be moved in a library + no global calls
  118. */
  119. public static function get_count_exam_results($exercise_id, $extra_where_conditions)
  120. {
  121. $count = self::get_exam_results_data(null, null, null, null, $exercise_id, $extra_where_conditions, true);
  122. return $count;
  123. }
  124. /**
  125. * Gets count of exam results
  126. * @todo this public static function should be moved in a library + no global calls
  127. */
  128. public static function get_admin_count_exam_results($extra_where_conditions)
  129. {
  130. $count = self::get_admin_exam_results_data(null, null, null, null, $extra_where_conditions, true);
  131. return $count;
  132. }
  133. /**
  134. * Gets the exam'data results
  135. * @todo this function should be moved in a library + no global calls
  136. */
  137. public static function get_admin_exam_results_data($from, $number_of_items, $column, $direction, $extra_where_conditions = null, $get_count = false)
  138. {
  139. if (empty($extra_where_conditions)) {
  140. $extra_where_conditions = "1 = 1 ";
  141. }
  142. $TBL_USER = Database :: get_main_table(TABLE_MAIN_USER);
  143. $tableCourse = Database :: get_main_table(TABLE_MAIN_COURSE);
  144. $tableSession = Database :: get_main_table(TABLE_MAIN_SESSION);
  145. $TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
  146. $TBL_TRACK_EXERCICES = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  147. $TBL_TRACK_ATTEMPT_RECORDING = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  148. // sql for chamilo-type tests for teacher / tutor view
  149. $sql_inner_join_tbl_track_exercices = " (
  150. SELECT DISTINCT ttte.*
  151. FROM $TBL_TRACK_EXERCICES ttte LEFT JOIN $TBL_TRACK_ATTEMPT_RECORDING tr
  152. ON (ttte.exe_id = tr.exe_id)
  153. )";
  154. $first_and_last_name = api_is_western_name_order() ? "firstname, lastname" : "lastname, firstname";
  155. if ($get_count) {
  156. $sql_select = "SELECT count(te.exe_id) ";
  157. } else {
  158. $sql_select = "SELECT DISTINCT
  159. user_id,
  160. $first_and_last_name,
  161. ce.title,
  162. username,
  163. session.name as session,
  164. te.exe_result,
  165. te.exe_weighting,
  166. te.exe_date,
  167. te.exe_id,
  168. email as exemail,
  169. te.start_date,
  170. steps_counter,
  171. exe_user_id,
  172. te.exe_duration,
  173. propagate_neg,
  174. te.c_id,
  175. te.session_id,
  176. course.code,
  177. orig_lp_id";
  178. }
  179. $sql = " $sql_select
  180. FROM $TBL_EXERCICES AS ce
  181. INNER JOIN $sql_inner_join_tbl_track_exercices AS te ON (te.exe_exo_id = ce.iid)
  182. INNER JOIN $TBL_USER AS user ON (user.user_id = exe_user_id)
  183. INNER JOIN $tableCourse AS course ON (course.id = te.c_id)
  184. INNER JOIN $tableSession AS session ON (session.id = te.session_id)
  185. WHERE $extra_where_conditions AND
  186. te.status != 'incomplete' AND
  187. ce.active <>-1
  188. ";
  189. $column = !empty($column) ? Database::escape_string($column) : null;
  190. $from = intval($from);
  191. $number_of_items = intval($number_of_items);
  192. // just count how many answers
  193. if ($get_count) {
  194. $res = Database::query($sql);
  195. return Database::num_rows($res);
  196. }
  197. if (!empty($column)) {
  198. $sql .= " ORDER BY $column $direction ";
  199. }
  200. if ($get_count == false) {
  201. $sql .= " LIMIT $from, $number_of_items";
  202. }
  203. $results = array();
  204. $resx = Database::query($sql);
  205. while ($rowx = Database::fetch_array($resx, 'ASSOC')) {
  206. $results[] = $rowx;
  207. }
  208. $list_info = array();
  209. if (is_array($results)) {
  210. // Looping results.
  211. foreach ($results as $result) {
  212. $id = $result['exe_id'];
  213. $my_res = $result['exe_result'];
  214. $my_total = $result['exe_weighting'];
  215. if (!$result['propagate_neg'] && $my_res < 0) {
  216. $my_res = 0;
  217. }
  218. $score = self::show_score($my_res, $my_total);
  219. $courseLink = 'cidReq='.$result['code'].'&id_session='.$result['session_id'];
  220. $url = api_get_path(WEB_CODE_PATH).'exercice/exercise_show.php?'.$courseLink.'&action=edit&id='.$id;
  221. $link = Display::url('exercise', $url);
  222. $result['link'] = $link;
  223. $result['score'] = $score;
  224. $list_info[] = $result;
  225. }
  226. }
  227. return $list_info;
  228. }
  229. public static function get_count_exam_hotpotatoes_results($in_hotpot_path)
  230. {
  231. return self::get_exam_results_hotpotatoes_data(0, 0, '', '', $in_hotpot_path, true, '');
  232. }
  233. //function get_exam_results_hotpotatoes_data($from, $number_of_items, $column, $direction, $exercise_id, $extra_where_conditions = null, $get_count = false) {
  234. public static function get_exam_results_hotpotatoes_data($in_from, $in_number_of_items, $in_column, $in_direction, $in_hotpot_path, $in_get_count = false, $where_condition = null)
  235. {
  236. $tab_res = array();
  237. $courseId = api_get_course_int_id();
  238. // by default in_column = 1 If parameters given, it is the name of the column witch is the bdd field name
  239. if ($in_column == 1) {
  240. $in_column = 'firstname';
  241. }
  242. $TBL_TRACK_HOTPOTATOES = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  243. $TBL_USER = Database :: get_main_table(TABLE_MAIN_USER);
  244. $sql = "SELECT * FROM $TBL_TRACK_HOTPOTATOES thp
  245. JOIN $TBL_USER u ON thp.exe_user_id = u.user_id
  246. WHERE thp.c_id = '$courseId' AND
  247. exe_name LIKE '$in_hotpot_path%'";
  248. // just count how many answers
  249. if ($in_get_count) {
  250. $res = Database::query($sql);
  251. return Database::num_rows($res);
  252. }
  253. // get a number of sorted results
  254. $sql .= " $where_condition ORDER BY $in_column $in_direction LIMIT $in_from, $in_number_of_items";
  255. $res = Database::query($sql);
  256. while ($data = Database::fetch_array($res)) {
  257. $tab_one_res = array();
  258. $tab_one_res['firstname'] = $data['firstname'];
  259. $tab_one_res['lastname'] = $data['lastname'];
  260. $tab_one_res['username'] = $data['username'];
  261. $tab_one_res['group_name'] = implode("<br/>", GroupManager::get_user_group_name($data['user_id']));
  262. $tab_one_res['exe_date'] = $data['exe_date'];
  263. $tab_one_res['score'] = $data['exe_result'].'/'.$data['exe_weighting'];
  264. $tab_one_res['actions'] = "";
  265. $tab_res[] = $tab_one_res;
  266. }
  267. return $tab_res;
  268. }
  269. /**
  270. * Gets the exam'data results
  271. * @todo this function should be moved in a library + no global calls
  272. */
  273. public static function get_exam_results_data($from, $number_of_items, $column, $direction, $exercise_id, $extra_where_conditions = null, $get_count = false)
  274. {
  275. //@todo replace all this globals
  276. global $documentPath, $filter;
  277. if (empty($extra_where_conditions)) {
  278. $extra_where_conditions = "1 = 1 ";
  279. }
  280. $course_id = api_get_course_int_id();
  281. $is_allowedToEdit = api_is_allowed_to_edit(null, true) || api_is_allowed_to_edit(true) || api_is_drh();
  282. $TBL_USER = Database :: get_main_table(TABLE_MAIN_USER);
  283. $TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
  284. $TBL_GROUP_REL_USER = Database :: get_course_table(TABLE_GROUP_USER);
  285. $TBL_GROUP = Database :: get_course_table(TABLE_GROUP);
  286. $TBL_TRACK_EXERCICES = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  287. $TBL_TRACK_HOTPOTATOES = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  288. $TBL_TRACK_ATTEMPT_RECORDING = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  289. $session_id_and = ' AND te.session_id = '.api_get_session_id().' ';
  290. $exercise_id = intval($exercise_id);
  291. $exercise_where = '';
  292. if (!empty($exercise_id)) {
  293. $exercise_where .= ' AND te.exe_exo_id = '.$exercise_id.' ';
  294. }
  295. $hotpotatoe_where = '';
  296. if (!empty($_GET['path'])) {
  297. $hotpotatoe_path = Database::escape_string($_GET['path']);
  298. $hotpotatoe_where .= ' AND exe_name = "'.$hotpotatoe_path.'" ';
  299. }
  300. // sql for chamilo-type tests for teacher / tutor view
  301. $sql_inner_join_tbl_track_exercices = " (
  302. SELECT DISTINCT ttte.*, if(tr.exe_id,1, 0) as revised
  303. FROM $TBL_TRACK_EXERCICES ttte LEFT JOIN $TBL_TRACK_ATTEMPT_RECORDING tr
  304. ON (ttte.exe_id = tr.exe_id)
  305. WHERE ttte.c_id = '$course_id' AND
  306. exe_exo_id = $exercise_id AND
  307. ttte.session_id = ".api_get_session_id()."
  308. )";
  309. if ($is_allowedToEdit) {
  310. //Teacher view
  311. if (isset($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
  312. //$exercise_where_query = ' te.exe_exo_id = ce.id AND ';
  313. }
  314. $sqlFromOption = "";
  315. $sqlWhereOption = "";
  316. //@todo fix to work with COURSE_RELATION_TYPE_RRHH in both queries
  317. //Hack in order to filter groups
  318. $sql_inner_join_tbl_user = '';
  319. if (strpos($extra_where_conditions, 'group_id')) {
  320. $sql_inner_join_tbl_user = "
  321. (
  322. SELECT u.user_id, firstname, lastname, email, username, g.name as group_name, g.id as group_id
  323. FROM $TBL_USER u
  324. INNER JOIN $TBL_GROUP_REL_USER gru ON ( gru.user_id = u.user_id AND gru.c_id=".$course_id.")
  325. INNER JOIN $TBL_GROUP g ON (gru.group_id = g.id AND g.c_id=".$course_id.")
  326. )";
  327. }
  328. if (strpos($extra_where_conditions, 'group_all')) {
  329. $extra_where_conditions = str_replace("AND ( group_id = 'group_all' )", '', $extra_where_conditions);
  330. $extra_where_conditions = str_replace("AND group_id = 'group_all'", '', $extra_where_conditions);
  331. $extra_where_conditions = str_replace("group_id = 'group_all' AND", '', $extra_where_conditions);
  332. $sql_inner_join_tbl_user = "
  333. (
  334. SELECT u.user_id, firstname, lastname, email, username, '' as group_name, '' as group_id
  335. FROM $TBL_USER u
  336. )";
  337. $sql_inner_join_tbl_user = null;
  338. }
  339. if (strpos($extra_where_conditions, 'group_none')) {
  340. $extra_where_conditions = str_replace("AND ( group_id = 'group_none' )", "AND ( group_id is null )", $extra_where_conditions);
  341. $extra_where_conditions = str_replace("AND group_id = 'group_none'", "AND ( group_id is null )", $extra_where_conditions);
  342. $sql_inner_join_tbl_user = "
  343. (
  344. SELECT u.user_id, firstname, lastname, email, username, g.name as group_name, g.id as group_id
  345. FROM $TBL_USER u
  346. LEFT OUTER JOIN $TBL_GROUP_REL_USER gru ON ( gru.user_id = u.user_id AND gru.c_id=".$course_id." )
  347. LEFT OUTER JOIN $TBL_GROUP g ON (gru.group_id = g.id AND g.c_id = ".$course_id.")
  348. )";
  349. }
  350. //All
  351. $is_empty_sql_inner_join_tbl_user = false;
  352. if (empty($sql_inner_join_tbl_user)) {
  353. $is_empty_sql_inner_join_tbl_user = true;
  354. $sql_inner_join_tbl_user = "
  355. (
  356. SELECT u.user_id, firstname, lastname, email, username, ' ' as group_name, '' as group_id
  357. FROM $TBL_USER u
  358. )";
  359. }
  360. $sqlFromOption = " , $TBL_GROUP_REL_USER AS gru ";
  361. $sqlWhereOption = " AND gru.c_id = ".api_get_course_int_id()." AND gru.user_id = user.user_id ";
  362. $first_and_last_name = api_is_western_name_order() ? "firstname, lastname" : "lastname, firstname";
  363. if ($get_count) {
  364. $sql_select = "SELECT count(te.exe_id) ";
  365. } else {
  366. $sql_select = "SELECT DISTINCT
  367. user_id,
  368. $first_and_last_name,
  369. ce.title,
  370. username,
  371. te.exe_result,
  372. te.exe_weighting,
  373. te.exe_date,
  374. te.exe_id,
  375. email as exemail,
  376. te.start_date,
  377. steps_counter,
  378. exe_user_id,
  379. te.exe_duration,
  380. propagate_neg,
  381. revised,
  382. group_name,
  383. group_id,
  384. orig_lp_id";
  385. }
  386. $sql = " $sql_select
  387. FROM $TBL_EXERCICES AS ce
  388. INNER JOIN $sql_inner_join_tbl_track_exercices AS te ON (te.exe_exo_id = ce.iid)
  389. INNER JOIN $sql_inner_join_tbl_user AS user ON (user.user_id = exe_user_id)
  390. WHERE $extra_where_conditions AND
  391. te.status != 'incomplete'
  392. AND te.c_id='".$course_id."' $session_id_and
  393. AND ce.active <>-1
  394. AND ce.c_id=".$course_id."
  395. $exercise_where ";
  396. // sql for hotpotatoes tests for teacher / tutor view
  397. if ($get_count) {
  398. $hpsql_select = "SELECT count(username)";
  399. } else {
  400. $hpsql_select = "SELECT
  401. $first_and_last_name ,
  402. username,
  403. tth.exe_name,
  404. tth.exe_result ,
  405. tth.exe_weighting,
  406. tth.exe_date";
  407. }
  408. // AND $where_condition seems not to be used
  409. $hpsql = " $hpsql_select
  410. FROM
  411. $TBL_TRACK_HOTPOTATOES tth,
  412. $TBL_USER user
  413. $sqlFromOption
  414. WHERE
  415. user.user_id=tth.exe_user_id
  416. AND tth.c_id = '".$course_id."'
  417. $hotpotatoe_where
  418. $sqlWhereOption
  419. ORDER BY
  420. tth.c_id ASC,
  421. tth.exe_date DESC";
  422. }
  423. if ($get_count) {
  424. $resx = Database::query($sql);
  425. $rowx = Database::fetch_row($resx, 'ASSOC');
  426. return $rowx[0];
  427. }
  428. $teacher_list = CourseManager::get_teacher_list_from_course_code(api_get_course_int_id());
  429. $teacher_id_list = array();
  430. foreach ($teacher_list as $teacher) {
  431. $teacher_id_list[] = $teacher['user_id'];
  432. }
  433. // Simple exercises.
  434. if (empty($hotpotatoe_where)) {
  435. $column = !empty($column) ? Database::escape_string($column) : null;
  436. $from = intval($from);
  437. $number_of_items = intval($number_of_items);
  438. if (!empty($column)) {
  439. $sql .= " ORDER BY $column $direction ";
  440. }
  441. $sql .= " LIMIT $from, $number_of_items";
  442. $results = array();
  443. $resx = Database::query($sql);
  444. while ($rowx = Database::fetch_array($resx, 'ASSOC')) {
  445. $results[] = $rowx;
  446. }
  447. $list_info = array();
  448. $group_list = GroupManager::get_group_list();
  449. $clean_group_list = array();
  450. if (!empty($group_list)) {
  451. foreach ($group_list as $group) {
  452. $clean_group_list[$group['id']] = $group['name'];
  453. }
  454. }
  455. $lp_list_obj = new LearnpathList(api_get_user_id());
  456. $lp_list = $lp_list_obj->get_flat_list();
  457. if (is_array($results)) {
  458. $users_array_id = array();
  459. if (isset($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
  460. $from_gradebook = true;
  461. }
  462. $sizeof = count($results);
  463. $user_list_id = array();
  464. $locked = api_resource_is_locked_by_gradebook($exercise_id, LINK_EXERCISE);
  465. //Looping results
  466. for ($i = 0; $i < $sizeof; $i++) {
  467. $revised = $results[$i]['revised'];
  468. if (isset($from_gradebook) && $from_gradebook && $is_allowedToEdit) {
  469. if (in_array($results[$i]['username'].$results[$i]['firstname'].$results[$i]['lastname'], $users_array_id)) {
  470. continue;
  471. }
  472. $users_array_id[] = $results[$i]['username'].$results[$i]['firstname'].$results[$i]['lastname'];
  473. }
  474. $lp_obj = isset($results[$i]['orig_lp_id']) && isset($lp_list[$results[$i]['orig_lp_id']]) ? $lp_list[$results[$i]['orig_lp_id']] : null;
  475. $lp_name = null;
  476. if ($lp_obj) {
  477. $url = api_get_path(WEB_CODE_PATH).'newscorm/lp_controller.php?'.api_get_cidreq().'&action=view&lp_id='.$results[$i]['orig_lp_id'];
  478. $lp_name = Display::url($lp_obj['lp_name'], $url, array('target' => '_blank'));
  479. }
  480. //Add all groups by user
  481. $group_name_list = null;
  482. if ($is_empty_sql_inner_join_tbl_user) {
  483. $group_list = GroupManager::get_group_ids(api_get_course_int_id(), $results[$i]['user_id']);
  484. foreach ($group_list as $id) {
  485. $group_name_list .= $clean_group_list[$id].'<br/>';
  486. }
  487. $results[$i]['group_name'] = $group_name_list;
  488. }
  489. $results[$i]['exe_duration'] = !empty($results[$i]['exe_duration']) ? round($results[$i]['exe_duration'] / 60) : 0;
  490. $user_list_id[] = $results[$i]['exe_user_id'];
  491. $id = $results[$i]['exe_id'];
  492. $dt = api_convert_and_format_date($results[$i]['exe_weighting']);
  493. // we filter the results if we have the permission to
  494. if (isset($results[$i]['results_disabled'])) {
  495. $result_disabled = intval($results[$i]['results_disabled']);
  496. } else {
  497. $result_disabled = 0;
  498. }
  499. if ($result_disabled == 0) {
  500. $my_res = $results[$i]['exe_result'];
  501. $my_total = $results[$i]['exe_weighting'];
  502. $results[$i]['start_date'] = api_get_local_time($results[$i]['start_date']);
  503. $results[$i]['exe_date'] = api_get_local_time($results[$i]['exe_date']);
  504. if (!$results[$i]['propagate_neg'] && $my_res < 0) {
  505. $my_res = 0;
  506. }
  507. $score = self::show_score($my_res, $my_total);
  508. $actions = '';
  509. if ($is_allowedToEdit) {
  510. if (isset($teacher_id_list)) {
  511. if (in_array($results[$i]['exe_user_id'], $teacher_id_list)) {
  512. $actions .= Display::return_icon('teachers.gif', get_lang('Teacher'));
  513. }
  514. }
  515. if ($revised) {
  516. $actions .= "<a href='exercise_show.php?".api_get_cidreq()."&action=edit&id=$id'>".Display :: return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL);
  517. $actions .= '&nbsp;';
  518. } else {
  519. $actions .="<a href='exercise_show.php?".api_get_cidreq()."&action=qualify&id=$id'>".Display :: return_icon('quiz.gif', get_lang('Qualify'));
  520. $actions .='&nbsp;';
  521. }
  522. $actions .="</a>";
  523. if ($filter == 2) {
  524. $actions .=' <a href="exercise_history.php?'.api_get_cidreq().'&exe_id='.$id.'">'.Display :: return_icon('history.gif', get_lang('ViewHistoryChange')).'</a>';
  525. }
  526. //Admin can always delete the attempt
  527. if ($locked == false || api_is_platform_admin()) {
  528. $ip = TrackingUserLog::get_ip_from_user_event($results[$i]['exe_user_id'], $results[$i]['exe_date'], false);
  529. $actions .= '<a href="http://www.whatsmyip.org/ip-geo-location/?ip='.$ip.'" target="_blank"><img src="'.api_get_path(WEB_CODE_PATH).'img/icons/22/info.png" title="'.$ip.'" /></a>';
  530. $delete_link = '<a href="exercise_report.php?'.api_get_cidreq().'&filter_by_user='.intval($_GET['filter_by_user']).'&filter=' . $filter . '&exerciseId='.$exercise_id.'&delete=delete&did=' . $id . '"
  531. onclick="javascript:if(!confirm(\'' . sprintf(get_lang('DeleteAttempt'), $results[$i]['username'], $dt) . '\')) return false;">'.Display :: return_icon('delete.png', get_lang('Delete')).'</a>';
  532. $delete_link = utf8_encode($delete_link);
  533. $actions .= $delete_link.'&nbsp;';
  534. }
  535. } else {
  536. $attempt_url = api_get_path(WEB_CODE_PATH).'exercice/result.php?'.api_get_cidreq().'&id='.$results[$i]['exe_id'].'&id_session='.api_get_session_id().'&height=500&width=750';
  537. $attempt_link = Display::url(get_lang('Show'), $attempt_url, array('class' => 'ajax btn'));
  538. $actions .= $attempt_link;
  539. }
  540. if ($revised) {
  541. $revised = Display::label(get_lang('Validated'), 'success');
  542. } else {
  543. $revised = Display::label(get_lang('NotValidated'), 'info');
  544. }
  545. if ($is_allowedToEdit) {
  546. $results[$i]['status'] = $revised;
  547. $results[$i]['score'] = $score;
  548. $results[$i]['lp'] = $lp_name;
  549. $results[$i]['actions'] = $actions;
  550. $list_info[] = $results[$i];
  551. } else {
  552. $results[$i]['status'] = $revised;
  553. $results[$i]['score'] = $score;
  554. $results[$i]['actions'] = $actions;
  555. $list_info[] = $results[$i];
  556. }
  557. }
  558. }
  559. }
  560. } else {
  561. $hpresults = getManyResultsXCol($hpsql, 6);
  562. // Print HotPotatoes test results.
  563. if (is_array($hpresults)) {
  564. for ($i = 0; $i < sizeof($hpresults); $i++) {
  565. $hp_title = GetQuizName($hpresults[$i][3], $documentPath);
  566. if ($hp_title == '') {
  567. $hp_title = basename($hpresults[$i][3]);
  568. }
  569. $hp_date = api_get_local_time($hpresults[$i][6], null, date_default_timezone_get());
  570. $hp_result = round(($hpresults[$i][4] / ($hpresults[$i][5] != 0 ? $hpresults[$i][5] : 1)) * 100, 2).'% ('.$hpresults[$i][4].' / '.$hpresults[$i][5].')';
  571. if ($is_allowedToEdit) {
  572. $list_info[] = array($hpresults[$i][0], $hpresults[$i][1], $hpresults[$i][2], '', $hp_title, '-', $hp_date, $hp_result, '-');
  573. } else {
  574. $list_info[] = array($hp_title, '-', $hp_date, $hp_result, '-');
  575. }
  576. }
  577. }
  578. }
  579. return $list_info;
  580. }
  581. /**
  582. * Converts the score with the exercise_max_note and exercise_min_score the platform settings + formats the results using the Text::float_format function
  583. *
  584. * @param float score
  585. * @param float weight
  586. * @param bool show porcentage or not
  587. * @param bool use or not the platform settings
  588. * @return string an html with the score modified
  589. */
  590. public static function show_score($score, $weight, $show_percentage = true, $use_platform_settings = true, $show_only_percentage = false)
  591. {
  592. if (is_null($score) && is_null($weight)) {
  593. return '-';
  594. }
  595. $max_note = api_get_setting('exercise_max_score');
  596. $min_note = api_get_setting('exercise_min_score');
  597. if ($use_platform_settings) {
  598. if ($max_note != '' && $min_note != '') {
  599. if (!empty($weight) && intval($weight) != 0) {
  600. $score = $min_note + ($max_note - $min_note) * $score / $weight;
  601. } else {
  602. $score = $min_note;
  603. }
  604. $weight = $max_note;
  605. }
  606. }
  607. $percentage = (100 * $score) / ($weight != 0 ? $weight : 1);
  608. //Formats values
  609. $percentage = Text::float_format($percentage, 1);
  610. $score = Text::float_format($score, 1);
  611. $weight = Text::float_format($weight, 1);
  612. $html = null;
  613. if ($show_percentage) {
  614. $parent = '('.$score.' / '.$weight.')';
  615. $html = $percentage." % $parent";
  616. if ($show_only_percentage) {
  617. $html = $percentage."% ";
  618. }
  619. } else {
  620. $html = $score.' / '.$weight;
  621. }
  622. $html = Display::span($html, array('class' => 'score_exercise'));
  623. return $html;
  624. }
  625. public static function is_success_exercise_result($score, $weight, $pass_percentage)
  626. {
  627. $percentage = Text::float_format(($score / ($weight != 0 ? $weight : 1)) * 100, 1);
  628. if (isset($pass_percentage) && !empty($pass_percentage)) {
  629. if ($percentage >= $pass_percentage) {
  630. return true;
  631. }
  632. }
  633. return false;
  634. }
  635. public static function show_success_message($score, $weight, $pass_percentage)
  636. {
  637. $res = "";
  638. if (self::is_pass_pourcentage_enabled($pass_percentage)) {
  639. $is_success = self::is_success_exercise_result($score, $weight, $pass_percentage);
  640. $icon = '';
  641. if ($is_success) {
  642. $html = get_lang('CongratulationsYouPassedTheTest');
  643. $icon = Display::return_icon('completed.png', get_lang('Correct'), array(), ICON_SIZE_MEDIUM);
  644. } else {
  645. //$html .= Display::return_message(get_lang('YouDidNotReachTheMinimumScore'), 'warning');
  646. $html = get_lang('YouDidNotReachTheMinimumScore');
  647. $icon = Display::return_icon('warning.png', get_lang('Wrong'), array(), ICON_SIZE_MEDIUM);
  648. }
  649. $html = Display::tag('h4', $html);
  650. $html .= Display::tag('h5', $icon, array('style' => 'width:40px; padding:2px 10px 0px 0px'));
  651. $res = $html;
  652. }
  653. return $res;
  654. }
  655. /**
  656. * Return true if pass_pourcentage activated (we use the pass pourcentage feature
  657. * return false if pass_percentage = 0 (we don't use the pass pourcentage feature
  658. * @param $in_pass_pourcentage
  659. * @return boolean
  660. * In this version, pass_percentage and show_success_message are disabled if
  661. * pass_percentage is set to 0
  662. */
  663. public static function is_pass_pourcentage_enabled($in_pass_pourcentage)
  664. {
  665. return $in_pass_pourcentage > 0;
  666. }
  667. /**
  668. * Converts a numeric value in a percentage example 0.66666 to 66.67 %
  669. * @param $value
  670. * @return float Converted number
  671. */
  672. public static function convert_to_percentage($value)
  673. {
  674. $return = '-';
  675. if ($value != '') {
  676. $return = Text::float_format($value * 100, 1).' %';
  677. }
  678. return $return;
  679. }
  680. /**
  681. * Converts a score/weight values to the platform scale
  682. * @param float score
  683. * @param float weight
  684. * @return float the score rounded converted to the new range
  685. */
  686. public static function convert_score($score, $weight)
  687. {
  688. $max_note = api_get_setting('exercise_max_score');
  689. $min_note = api_get_setting('exercise_min_score');
  690. if ($score != '' && $weight != '') {
  691. if ($max_note != '' && $min_note != '') {
  692. if (!empty($weight)) {
  693. $score = $min_note + ($max_note - $min_note) * $score / $weight;
  694. } else {
  695. $score = $min_note;
  696. }
  697. }
  698. }
  699. $score_rounded = Text::float_format($score, 1);
  700. return $score_rounded;
  701. }
  702. /**
  703. * 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)
  704. * @param array course data
  705. * @param int session id
  706. * @return array array with exercise data
  707. */
  708. public static function get_all_exercises($course_info = null, $session_id = 0, $check_publication_dates = false)
  709. {
  710. $TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
  711. $course_id = api_get_course_int_id();
  712. if (!empty($course_info) && !empty($course_info['real_id'])) {
  713. $course_id = $course_info['real_id'];
  714. }
  715. if ($session_id == -1) {
  716. $session_id = 0;
  717. }
  718. $now = api_get_utc_datetime();
  719. $time_conditions = '';
  720. if ($check_publication_dates) {
  721. $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 "; //start and end are set
  722. $time_conditions .= " (start_time <> '0000-00-00 00:00:00' AND start_time < '$now' AND end_time = '0000-00-00 00:00:00') OR "; // only start is set
  723. $time_conditions .= " (start_time = '0000-00-00 00:00:00' AND end_time <> '0000-00-00 00:00:00' AND end_time > '$now') OR "; // only end is set
  724. $time_conditions .= " (start_time = '0000-00-00 00:00:00' AND end_time = '0000-00-00 00:00:00')) "; // nothing is set
  725. }
  726. if ($session_id == 0) {
  727. $conditions = array('where' => array('active = ? AND session_id = ? AND c_id = ? '.$time_conditions => array('1', $session_id, $course_id)), 'order' => 'title');
  728. } else {
  729. //All exercises
  730. $conditions = array('where' => array('active = ? AND (session_id = 0 OR session_id = ? ) AND c_id = ? '.$time_conditions => array('1', $session_id, $course_id)), 'order' => 'title');
  731. }
  732. $result = Database::select('*', $TBL_EXERCICES, $conditions);
  733. return $result;
  734. }
  735. /**
  736. * 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)
  737. * @param int session id
  738. * @param int course c_id
  739. * @return array array with exercise data
  740. * modified by Hubert Borderiou
  741. */
  742. public static function get_all_exercises_for_course_id($session_id = 0, $course_id = 0)
  743. {
  744. $TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
  745. if ($session_id == -1) {
  746. $session_id = 0;
  747. }
  748. if ($session_id == 0) {
  749. $conditions = array('where' => array('active = ? AND session_id = ? AND c_id = ?' => array('1', $session_id, $course_id)), 'order' => 'title');
  750. } else {
  751. //All exercises
  752. $conditions = array('where' => array('active = ? AND (session_id = 0 OR session_id = ? ) AND c_id=?' => array('1', $session_id, $course_id)), 'order' => 'title');
  753. }
  754. return Database::select('*', $TBL_EXERCICES, $conditions);
  755. }
  756. /**
  757. * Gets the position of the score based in a given score (result/weight) and the exe_id based in the user list
  758. * (NO Exercises in LPs )
  759. * @param float user score to be compared *attention* $my_score = score/weight and not just the score
  760. * @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)
  761. * @param int exercise id
  762. * @param string course code
  763. * @param int session id
  764. * @return int the position of the user between his friends in a course (or course within a session)
  765. */
  766. public static function get_exercise_result_ranking($my_score, $my_exe_id, $exercise_id, $course_id, $session_id = 0, $user_list = array(), $return_string = true)
  767. {
  768. //No score given we return
  769. if (is_null($my_score)) {
  770. return '-';
  771. }
  772. if (empty($user_list)) {
  773. return '-';
  774. }
  775. $best_attempts = array();
  776. foreach ($user_list as $user_data) {
  777. $user_id = $user_data['user_id'];
  778. $best_attempts[$user_id] = self::get_best_attempt_by_user($user_id, $exercise_id, $course_id, $session_id);
  779. }
  780. if (empty($best_attempts)) {
  781. return 1;
  782. } else {
  783. $position = 1;
  784. $my_ranking = array();
  785. foreach ($best_attempts as $user_id => $result) {
  786. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  787. $my_ranking[$user_id] = $result['exe_result'] / $result['exe_weighting'];
  788. } else {
  789. $my_ranking[$user_id] = 0;
  790. }
  791. }
  792. //if (!empty($my_ranking)) {
  793. asort($my_ranking);
  794. $position = count($my_ranking);
  795. if (!empty($my_ranking)) {
  796. foreach ($my_ranking as $user_id => $ranking) {
  797. if ($my_score >= $ranking) {
  798. if ($my_score == $ranking) {
  799. $exe_id = $best_attempts[$user_id]['exe_id'];
  800. if ($my_exe_id < $exe_id) {
  801. $position--;
  802. }
  803. } else {
  804. $position--;
  805. }
  806. }
  807. }
  808. }
  809. //}
  810. $return_value = array('position' => $position, 'count' => count($my_ranking));
  811. if ($return_string) {
  812. if (!empty($position) && !empty($my_ranking)) {
  813. $return_value = $position.'/'.count($my_ranking);
  814. } else {
  815. $return_value = '-';
  816. }
  817. }
  818. return $return_value;
  819. }
  820. }
  821. /**
  822. * Gets the position of the score based in a given score (result/weight) and the exe_id based in all attempts
  823. * (NO Exercises in LPs ) old funcionality by attempt
  824. * @param float user score to be compared attention => score/weight
  825. * @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)
  826. * @param int exercise id
  827. * @param int course id
  828. * @param int session id
  829. * @todo seems that is not used
  830. * @return int the position of the user between his friends in a course (or course within a session)
  831. */
  832. public static function get_exercise_result_ranking_by_attempt($my_score, $my_exe_id, $exercise_id, $courseId, $session_id = 0, $return_string = true)
  833. {
  834. if (empty($session_id)) {
  835. $session_id = 0;
  836. }
  837. if (is_null($my_score)) {
  838. return '-';
  839. }
  840. $user_results = self::get_all_exercise_results($exercise_id, $courseId, $session_id, false);
  841. $position_data = array();
  842. if (empty($user_results)) {
  843. return 1;
  844. } else {
  845. $position = 1;
  846. $my_ranking = array();
  847. foreach ($user_results as $result) {
  848. //print_r($result);
  849. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  850. $my_ranking[$result['exe_id']] = $result['exe_result'] / $result['exe_weighting'];
  851. } else {
  852. $my_ranking[$result['exe_id']] = 0;
  853. }
  854. }
  855. asort($my_ranking);
  856. $position = count($my_ranking);
  857. if (!empty($my_ranking)) {
  858. foreach ($my_ranking as $exe_id => $ranking) {
  859. if ($my_score >= $ranking) {
  860. if ($my_score == $ranking) {
  861. if ($my_exe_id < $exe_id) {
  862. $position--;
  863. }
  864. } else {
  865. $position--;
  866. }
  867. }
  868. }
  869. }
  870. $return_value = array('position' => $position, 'count' => count($my_ranking));
  871. if ($return_string) {
  872. if (!empty($position) && !empty($my_ranking)) {
  873. return $position.'/'.count($my_ranking);
  874. }
  875. }
  876. return $return_value;
  877. }
  878. }
  879. /*
  880. * Get the best attempt in a exercise (NO Exercises in LPs )
  881. */
  882. public static function get_best_attempt_in_course($exercise_id, $courseId, $session_id)
  883. {
  884. $user_results = self::get_all_exercise_results($exercise_id, $courseId, $session_id, false);
  885. $best_score_data = array();
  886. $best_score = 0;
  887. if (!empty($user_results)) {
  888. foreach ($user_results as $result) {
  889. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  890. $score = $result['exe_result'] / $result['exe_weighting'];
  891. if ($score >= $best_score) {
  892. $best_score = $score;
  893. $best_score_data = $result;
  894. }
  895. }
  896. }
  897. }
  898. return $best_score_data;
  899. }
  900. /**
  901. * Get the best score in a exercise (NO Exercises in LPs )
  902. * @param $user_id
  903. * @param $exercise_id
  904. * @param $courseId
  905. * @param $session_id
  906. * @return array
  907. */
  908. public static function get_best_attempt_by_user($user_id, $exercise_id, $courseId, $session_id)
  909. {
  910. $user_results = self::get_all_exercise_results($exercise_id, $courseId, $session_id, false, $user_id);
  911. $best_score_data = array();
  912. $best_score = 0;
  913. if (!empty($user_results)) {
  914. foreach ($user_results as $result) {
  915. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  916. $score = $result['exe_result'] / $result['exe_weighting'];
  917. if ($score >= $best_score) {
  918. $best_score = $score;
  919. $best_score_data = $result;
  920. }
  921. }
  922. }
  923. }
  924. return $best_score_data;
  925. }
  926. /**
  927. * Get average score (NO Exercises in LPs )
  928. * @param int exercise id
  929. * @param int course id
  930. * @param int session id
  931. * @return float Average score
  932. */
  933. public static function get_average_score($exercise_id, $courseId, $session_id)
  934. {
  935. $user_results = self::get_all_exercise_results($exercise_id, $courseId, $session_id);
  936. $avg_score = 0;
  937. if (!empty($user_results)) {
  938. foreach ($user_results as $result) {
  939. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  940. $score = $result['exe_result'] / $result['exe_weighting'];
  941. $avg_score +=$score;
  942. }
  943. }
  944. $avg_score = Text::float_format($avg_score / count($user_results), 1);
  945. }
  946. return $avg_score;
  947. }
  948. /**
  949. * Get average score by score (NO Exercises in LPs )
  950. * @param int exercise id
  951. * @param int course id
  952. * @param int session id
  953. * @return float Average score
  954. */
  955. public static function get_average_score_by_course($courseId, $session_id)
  956. {
  957. $user_results = self::get_all_exercise_results_by_course($courseId, $session_id, false);
  958. $avg_score = 0;
  959. if (!empty($user_results)) {
  960. foreach ($user_results as $result) {
  961. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  962. $score = $result['exe_result'] / $result['exe_weighting'];
  963. $avg_score +=$score;
  964. }
  965. }
  966. //We asume that all exe_weighting
  967. //$avg_score = show_score( $avg_score / count($user_results) , $result['exe_weighting']);
  968. $avg_score = ($avg_score / count($user_results));
  969. }
  970. return $avg_score;
  971. }
  972. /**
  973. *
  974. * @param $user_id
  975. * @param $courseId
  976. * @param $session_id
  977. *
  978. * @return float|int
  979. */
  980. public static function get_average_score_by_course_by_user($user_id, $courseId, $session_id)
  981. {
  982. $user_results = self::get_all_exercise_results_by_user($user_id, $courseId, $session_id);
  983. $avg_score = 0;
  984. if (!empty($user_results)) {
  985. foreach ($user_results as $result) {
  986. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  987. $score = $result['exe_result'] / $result['exe_weighting'];
  988. $avg_score +=$score;
  989. }
  990. }
  991. //We asume that all exe_weighting
  992. //$avg_score = show_score( $avg_score / count($user_results) , $result['exe_weighting']);
  993. $avg_score = ($avg_score / count($user_results));
  994. }
  995. return $avg_score;
  996. }
  997. /**
  998. * Get average score by score (NO Exercises in LPs )
  999. * @param int exercise id
  1000. * @param int course id
  1001. * @param int session id
  1002. *
  1003. * @return float Best average score
  1004. */
  1005. public static function get_best_average_score_by_exercise($exercise_id, $course_code, $session_id, $user_count)
  1006. {
  1007. $user_results = self::get_best_exercise_results_by_user($exercise_id, $course_code, $session_id);
  1008. $avg_score = 0;
  1009. if (!empty($user_results)) {
  1010. foreach ($user_results as $result) {
  1011. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1012. $score = $result['exe_result'] / $result['exe_weighting'];
  1013. $avg_score +=$score;
  1014. }
  1015. }
  1016. //We asume that all exe_weighting
  1017. //$avg_score = show_score( $avg_score / count($user_results) , $result['exe_weighting']);
  1018. //$avg_score = ($avg_score / count($user_results));
  1019. if (!empty($user_count)) {
  1020. $avg_score = Text::float_format($avg_score / $user_count, 1) * 100;
  1021. } else {
  1022. $avg_score = 0;
  1023. }
  1024. }
  1025. return $avg_score;
  1026. }
  1027. /**
  1028. * @param $course_code
  1029. * @param $session_id
  1030. * @return array
  1031. */
  1032. public static function get_exercises_to_be_taken($course_code, $session_id)
  1033. {
  1034. $course_info = api_get_course_info($course_code);
  1035. $exercises = self::get_all_exercises($course_info, $session_id);
  1036. $result = array();
  1037. $now = time() + 15 * 24 * 60 * 60;
  1038. foreach ($exercises as $exercise_item) {
  1039. 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'], 'UTC') < $now) {
  1040. $result[] = $exercise_item;
  1041. }
  1042. }
  1043. return $result;
  1044. }
  1045. /**
  1046. * Get student results (only in completed exercises) stats by question
  1047. *
  1048. * @param int question id
  1049. * @param int exercise id
  1050. * @param int course id
  1051. * @param int session id
  1052. *
  1053. * */
  1054. public static function get_student_stats_by_question($question_id, $exercise_id, $courseId, $session_id)
  1055. {
  1056. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1057. $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1058. $question_id = intval($question_id);
  1059. $exercise_id = intval($exercise_id);
  1060. $courseId = intval($courseId);
  1061. $session_id = intval($session_id);
  1062. $sql = "SELECT MAX(marks) as max , MIN(marks) as min, AVG(marks) as average
  1063. FROM $track_exercises e INNER JOIN $track_attempt a ON (a.exe_id = e.exe_id)
  1064. WHERE exe_exo_id = $exercise_id AND
  1065. e.c_id = $courseId AND
  1066. a.c_id = $courseId AND
  1067. e.session_id = $session_id AND
  1068. question_id = $question_id AND
  1069. status = ''
  1070. LIMIT 1";
  1071. $result = Database::query($sql);
  1072. $return = array();
  1073. if ($result) {
  1074. $return = Database::fetch_array($result, 'ASSOC');
  1075. }
  1076. return $return;
  1077. }
  1078. /**
  1079. * @param int $question_id
  1080. * @param int $exercise_id
  1081. * @param int $courseId
  1082. * @param int $session_id
  1083. * @return int
  1084. */
  1085. public static function get_number_students_question_with_answer_count($question_id, $exercise_id, $courseId, $session_id)
  1086. {
  1087. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1088. $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1089. $course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1090. $question_id = intval($question_id);
  1091. $exercise_id = intval($exercise_id);
  1092. $courseId = intval($courseId);
  1093. $session_id = intval($session_id);
  1094. $sql = "SELECT DISTINCT exe_user_id
  1095. FROM $track_exercises e INNER JOIN $track_attempt a ON (a.exe_id = e.exe_id) INNER JOIN $course_user cu
  1096. ON cu.c_id = a.c_id AND cu.user_id = exe_user_id
  1097. WHERE exe_exo_id = $exercise_id AND
  1098. a.c_id = $courseId AND
  1099. e.session_id = $session_id AND
  1100. question_id = $question_id AND
  1101. answer <> '0' AND
  1102. cu.status = ".STUDENT." AND
  1103. relation_type <> 2 AND
  1104. e.status = ''";
  1105. $result = Database::query($sql);
  1106. $return = 0;
  1107. if ($result) {
  1108. $return = Database::num_rows($result);
  1109. }
  1110. return $return;
  1111. }
  1112. /**
  1113. * @param int $answer_id
  1114. * @param int $question_id
  1115. * @param int $exercise_id
  1116. * @param int $courseId
  1117. * @param int $session_id
  1118. * @return int
  1119. */
  1120. public static function get_number_students_answer_hotspot_count($answer_id, $question_id, $exercise_id, $courseId, $session_id)
  1121. {
  1122. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1123. $track_hotspot = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  1124. $course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1125. $question_id = intval($question_id);
  1126. $answer_id = intval($answer_id);
  1127. $exercise_id = intval($exercise_id);
  1128. $courseId = intval($courseId);
  1129. $session_id = intval($session_id);
  1130. $sql = "SELECT DISTINCT exe_user_id
  1131. FROM $track_exercises e
  1132. INNER JOIN $track_hotspot a ON (a.hotspot_exe_id = e.exe_id)
  1133. INNER JOIN $course_user cu
  1134. ON cu.c_id = a.c_id AND cu.user_id = exe_user_id
  1135. WHERE exe_exo_id = $exercise_id AND
  1136. a.c_id = $courseId AND
  1137. e.session_id = $session_id AND
  1138. hotspot_answer_id = $answer_id AND
  1139. hotspot_question_id = $question_id AND
  1140. cu.status = ".STUDENT." AND
  1141. hotspot_correct = 1 AND
  1142. relation_type <> 2 AND
  1143. e.status = ''";
  1144. $result = Database::query($sql);
  1145. $return = 0;
  1146. if ($result) {
  1147. $return = Database::num_rows($result);
  1148. }
  1149. return $return;
  1150. }
  1151. /**
  1152. *
  1153. * @param int $answer_id
  1154. * @param int $question_id
  1155. * @param int $exercise_id
  1156. * @param int $courseId
  1157. * @param int $session_id
  1158. * @param string $question_type
  1159. * @param null $correct_answer
  1160. * @param null $current_answer
  1161. * @return int
  1162. */
  1163. public static function get_number_students_answer_count($answer_id, $question_id, $exercise_id, $courseId, $session_id, $question_type = null, $correct_answer = null, $current_answer = null)
  1164. {
  1165. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1166. $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1167. $course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1168. $question_id = intval($question_id);
  1169. $answer_id = intval($answer_id);
  1170. $exercise_id = intval($exercise_id);
  1171. $courseId = intval($courseId);
  1172. $session_id = intval($session_id);
  1173. switch ($question_type) {
  1174. case FILL_IN_BLANKS:
  1175. $answer_condition = "";
  1176. $select_condition = " e.exe_id, answer ";
  1177. break;
  1178. case MATCHING:
  1179. default:
  1180. $answer_condition = " answer = $answer_id AND ";
  1181. $select_condition = " DISTINCT exe_user_id ";
  1182. }
  1183. $sql = "SELECT $select_condition
  1184. FROM $track_exercises e
  1185. INNER JOIN $track_attempt a ON (a.exe_id = e.exe_id)
  1186. INNER JOIN $course_user cu
  1187. ON cu.c_id = a.c_id AND cu.user_id = exe_user_id
  1188. WHERE exe_exo_id = $exercise_id AND
  1189. a.c_id = $courseId AND
  1190. e.session_id = $session_id AND
  1191. $answer_condition
  1192. question_id = $question_id AND
  1193. cu.status = ".STUDENT." AND
  1194. relation_type <> 2 AND
  1195. e.status = ''";
  1196. $result = Database::query($sql);
  1197. $return = 0;
  1198. if ($result) {
  1199. $good_answers = 0;
  1200. switch ($question_type) {
  1201. case FILL_IN_BLANKS:
  1202. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1203. $fill_blank = self::check_fill_in_blanks($correct_answer, $row['answer']);
  1204. if (isset($fill_blank[$current_answer]) && $fill_blank[$current_answer] == 1) {
  1205. $good_answers++;
  1206. }
  1207. }
  1208. return $good_answers;
  1209. break;
  1210. case MATCHING:
  1211. default:
  1212. $return = Database::num_rows($result);
  1213. }
  1214. }
  1215. return $return;
  1216. }
  1217. public static function check_fill_in_blanks($answer, $user_answer)
  1218. {
  1219. // the question is encoded like this
  1220. // [A] B [C] D [E] F::10,10,10@1
  1221. // number 1 before the "@" means that is a switchable fill in blank question
  1222. // [A] B [C] D [E] F::10,10,10@ or [A] B [C] D [E] F::10,10,10
  1223. // means that is a normal fill blank question
  1224. // first we explode the "::"
  1225. $pre_array = explode('::', $answer);
  1226. // is switchable fill blank or not
  1227. $last = count($pre_array) - 1;
  1228. $is_set_switchable = explode('@', $pre_array[$last]);
  1229. $switchable_answer_set = false;
  1230. if (isset($is_set_switchable[1]) && $is_set_switchable[1] == 1) {
  1231. $switchable_answer_set = true;
  1232. }
  1233. $answer = '';
  1234. for ($k = 0; $k < $last; $k++) {
  1235. $answer .= $pre_array[$k];
  1236. }
  1237. // splits weightings that are joined with a comma
  1238. $answerWeighting = explode(',', $is_set_switchable[0]);
  1239. // we save the answer because it will be modified
  1240. //$temp = $answer;
  1241. $temp = $answer;
  1242. $answer = '';
  1243. $j = 0;
  1244. //initialise answer tags
  1245. $user_tags = $correct_tags = $real_text = array();
  1246. // the loop will stop at the end of the text
  1247. while (1) {
  1248. // quits the loop if there are no more blanks (detect '[')
  1249. if (($pos = api_strpos($temp, '[')) === false) {
  1250. // adds the end of the text
  1251. $answer = $temp;
  1252. /* // Deprecated code
  1253. // TeX parsing - replacement of texcode tags
  1254. $answer = str_replace("{texcode}", $texstring, $answer);
  1255. */
  1256. $real_text[] = $answer;
  1257. break; //no more "blanks", quit the loop
  1258. }
  1259. // adds the piece of text that is before the blank
  1260. //and ends with '[' into a general storage array
  1261. $real_text[] = api_substr($temp, 0, $pos + 1);
  1262. $answer .= api_substr($temp, 0, $pos + 1);
  1263. //take the string remaining (after the last "[" we found)
  1264. $temp = api_substr($temp, $pos + 1);
  1265. // quit the loop if there are no more blanks, and update $pos to the position of next ']'
  1266. if (($pos = api_strpos($temp, ']')) === false) {
  1267. // adds the end of the text
  1268. $answer .= $temp;
  1269. break;
  1270. }
  1271. $str = $user_answer;
  1272. preg_match_all('#\[([^[]*)\]#', $str, $arr);
  1273. $str = str_replace('\r\n', '', $str);
  1274. $choice = $arr[1];
  1275. $tmp = api_strrpos($choice[$j], ' / ');
  1276. $choice[$j] = api_substr($choice[$j], 0, $tmp);
  1277. $choice[$j] = trim($choice[$j]);
  1278. //Needed to let characters ' and " to work as part of an answer
  1279. $choice[$j] = stripslashes($choice[$j]);
  1280. $user_tags[] = api_strtolower($choice[$j]);
  1281. //put the contents of the [] answer tag into correct_tags[]
  1282. $correct_tags[] = api_strtolower(api_substr($temp, 0, $pos));
  1283. $j++;
  1284. $temp = api_substr($temp, $pos + 1);
  1285. }
  1286. $answer = '';
  1287. $real_correct_tags = $correct_tags;
  1288. $chosen_list = array();
  1289. $good_answer = array();
  1290. for ($i = 0; $i < count($real_correct_tags); $i++) {
  1291. if (!$switchable_answer_set) {
  1292. //needed to parse ' and " characters
  1293. $user_tags[$i] = stripslashes($user_tags[$i]);
  1294. if ($correct_tags[$i] == $user_tags[$i]) {
  1295. $good_answer[$correct_tags[$i]] = 1;
  1296. } elseif (!empty($user_tags[$i])) {
  1297. $good_answer[$correct_tags[$i]] = 0;
  1298. } else {
  1299. $good_answer[$correct_tags[$i]] = 0;
  1300. }
  1301. } else {
  1302. // switchable fill in the blanks
  1303. if (in_array($user_tags[$i], $correct_tags)) {
  1304. $correct_tags = array_diff($correct_tags, $chosen_list);
  1305. $good_answer[$correct_tags[$i]] = 1;
  1306. } elseif (!empty($user_tags[$i])) {
  1307. $good_answer[$correct_tags[$i]] = 0;
  1308. } else {
  1309. $good_answer[$correct_tags[$i]] = 0;
  1310. }
  1311. }
  1312. // adds the correct word, followed by ] to close the blank
  1313. $answer .= ' / <font color="green"><b>'.$real_correct_tags[$i].'</b></font>]';
  1314. if (isset($real_text[$i + 1])) {
  1315. $answer .= $real_text[$i + 1];
  1316. }
  1317. }
  1318. return $good_answer;
  1319. }
  1320. /**
  1321. *
  1322. * @param int $exercise_id
  1323. * @param int $courseId
  1324. * @param int $session_id
  1325. * @depracated seems not to be used
  1326. * @return int
  1327. */
  1328. public static function get_number_students_finish_exercise($exercise_id, $courseId, $session_id)
  1329. {
  1330. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1331. $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1332. $exercise_id = intval($exercise_id);
  1333. $courseId = intval($courseId);
  1334. $session_id = intval($session_id);
  1335. $sql = "SELECT DISTINCT exe_user_id
  1336. FROM $track_exercises e INNER JOIN $track_attempt a ON (a.exe_id = e.exe_id)
  1337. WHERE exe_exo_id = $exercise_id AND
  1338. a.c_id = $courseId AND
  1339. e.c_id = $courseId AND
  1340. e.session_id = $session_id AND
  1341. status = ''";
  1342. $result = Database::query($sql);
  1343. $return = 0;
  1344. if ($result) {
  1345. $return = Database::num_rows($result);
  1346. }
  1347. return $return;
  1348. }
  1349. /**
  1350. return the HTML code for a menu with students group
  1351. @input : $in_name : is the name and the id of the <select>
  1352. $in_default : default value for option
  1353. @todo deprecated seems not to be used
  1354. @return : the html code of the <select>
  1355. */
  1356. public static function displayGroupMenu($in_name, $in_default, $in_onchange = "")
  1357. {
  1358. // check the default value of option
  1359. $tabSelected = array($in_default => " selected='selected' ");
  1360. $res = "";
  1361. $res .= "<select name='$in_name' id='$in_name' onchange='".$in_onchange."' >";
  1362. $res .= "<option value='-1'".$tabSelected["-1"].">-- ".get_lang('AllGroups')." --</option>";
  1363. $res .= "<option value='0'".$tabSelected["0"].">- ".get_lang('NotInAGroup')." -</option>";
  1364. $tabGroups = GroupManager::get_group_list();
  1365. $currentCatId = 0;
  1366. for ($i = 0; $i < count($tabGroups); $i++) {
  1367. $tabCategory = GroupManager::get_category_from_group($tabGroups[$i]["id"]);
  1368. if ($tabCategory["id"] != $currentCatId) {
  1369. $res .= "<option value='-1' disabled='disabled'>".$tabCategory["title"]."</option>";
  1370. $currentCatId = $tabCategory["id"];
  1371. }
  1372. $res .= "<option ".$tabSelected[$tabGroups[$i]["id"]]."style='margin-left:40px' value='".$tabGroups[$i]["id"]."'>".$tabGroups[$i]["name"]."</option>";
  1373. }
  1374. $res .= "</select>";
  1375. return $res;
  1376. }
  1377. /**
  1378. * Return a list of group for user with user_id=in_userid separated with in_separator
  1379. * @deprecated ?
  1380. */
  1381. public static function displayGroupsForUser($in_separator, $in_userid)
  1382. {
  1383. $res = implode($in_separator, GroupManager::get_user_group_name($in_userid));
  1384. if ($res == "") {
  1385. $res = "<div style='text-align:center'>-</div>";
  1386. }
  1387. return $res;
  1388. }
  1389. public static function create_chat_exercise_session($exe_id)
  1390. {
  1391. if (!isset($_SESSION['current_exercises'])) {
  1392. $_SESSION['current_exercises'] = array();
  1393. }
  1394. $_SESSION['current_exercises'][$exe_id] = true;
  1395. }
  1396. public static function delete_chat_exercise_session($exe_id)
  1397. {
  1398. if (isset($_SESSION['current_exercises'])) {
  1399. $_SESSION['current_exercises'][$exe_id] = false;
  1400. }
  1401. }
  1402. /**
  1403. * Update attempt date
  1404. * @param int $exeId
  1405. * @param Datetime $last_attempt_date
  1406. */
  1407. public static function update_attempt_date($exeId, $last_attempt_date)
  1408. {
  1409. $exercice_attemp_table = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1410. $exeId = intval($exeId);
  1411. $last_attempt_date = Database::escape_string($last_attempt_date);
  1412. $sql = "UPDATE $exercice_attemp_table SET tms = '".api_get_utc_datetime()."'
  1413. WHERE exe_id = $exeId AND tms = '".$last_attempt_date."' ";
  1414. Database::query($sql);
  1415. }
  1416. public static function getExerciseResult($trackExerciseInfo)
  1417. {
  1418. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  1419. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  1420. $TBL_TRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1421. $TBL_TRACK_ATTEMPT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1422. $query = "SELECT attempts.question_id, answer
  1423. FROM ".$TBL_TRACK_ATTEMPT." as attempts
  1424. INNER JOIN ".$TBL_TRACK_EXERCICES." AS stats_exercices
  1425. ON stats_exercices.exe_id=attempts.exe_id
  1426. INNER JOIN ".$TBL_EXERCICE_QUESTION." AS quizz_rel_questions
  1427. ON quizz_rel_questions.exercice_id=stats_exercices.exe_exo_id AND
  1428. quizz_rel_questions.question_id = attempts.question_id AND
  1429. quizz_rel_questions.c_id=".$trackExerciseInfo['c_id']."
  1430. INNER JOIN ".$TBL_QUESTIONS." AS questions
  1431. ON questions.iid=quizz_rel_questions.question_id AND
  1432. questions.c_id = ".$trackExerciseInfo['c_id']."
  1433. WHERE attempts.exe_id='".$trackExerciseInfo['exe_id']."' AND user_id=".intval($trackExerciseInfo['exe_user_id'])."
  1434. GROUP BY quizz_rel_questions.question_order, attempts.question_id";
  1435. $result = Database::query($query);
  1436. $exerciseResult = array();
  1437. while ($row = Database::fetch_array($result)) {
  1438. $exerciseResult[$row['question_id']] = $row['answer'];
  1439. }
  1440. return $exerciseResult;
  1441. }
  1442. public static function getExerciseResults($exerciseId, $courseId, $sessionId)
  1443. {
  1444. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1445. $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1446. $exerciseId = intval($exerciseId);
  1447. $courseId = intval($courseId);
  1448. $sessionId = intval($sessionId);
  1449. $sql = "SELECT DISTINCT e.exe_id, e.data_tracking
  1450. FROM $track_exercises e INNER JOIN $track_attempt a ON (a.exe_id = e.exe_id)
  1451. WHERE exe_exo_id = $exerciseId AND
  1452. a.c_id = $courseId AND
  1453. e.c_id = $courseId AND
  1454. e.session_id = $sessionId AND
  1455. status = ''";
  1456. $result = Database::query($sql);
  1457. return $result->fetchAll();
  1458. }
  1459. }