user_move_stats.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * User move script (to move between courses and sessions)
  5. * @package chamilo.admin
  6. */
  7. $cidReset = true;
  8. require_once __DIR__.'/../inc/global.inc.php';
  9. $this_section = SECTION_PLATFORM_ADMIN;
  10. api_protect_admin_script();
  11. $interbreadcrumb[] = array("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  12. $debug = 0;
  13. function compare_data($result_message)
  14. {
  15. foreach ($result_message as $table => $data) {
  16. $title = $table;
  17. if ($table == 'TRACK_E_EXERCISES') {
  18. $title = get_lang('Exercises');
  19. } elseif ($table == 'TRACK_E_EXERCISES_IN_LP') {
  20. $title = get_lang('ExercisesInLp');
  21. } elseif ($table == 'LP_VIEW') {
  22. $title = get_lang('LearningPaths');
  23. }
  24. echo '<br / ><h3>'.get_lang($title).' </h3><hr />';
  25. if (is_array($data)) {
  26. foreach ($data as $id => $item) {
  27. if ($table == 'TRACK_E_EXERCISES' || $table == 'TRACK_E_EXERCISES_IN_LP') {
  28. echo "<br /><h3>".get_lang('Attempt')." #$id</h3>";
  29. echo '<h3>';
  30. echo get_lang('Exercise').' #'.$item['exe_exo_id'];
  31. echo '</h3>';
  32. if (!empty($item['orig_lp_id'])) {
  33. echo '<h3>';
  34. echo get_lang('LearningPath').' #'.$item['orig_lp_id'];
  35. echo '</h3>';
  36. }
  37. //Process data
  38. $array = array(
  39. 'exe_date' => get_lang('Date'),
  40. 'exe_result' => get_lang('Score'),
  41. 'exe_weighting' => get_lang('Weighting')
  42. );
  43. foreach ($item as $key => $value) {
  44. if (in_array($key, array_keys($array))) {
  45. $key = $array[$key];
  46. echo "$key = $value <br />";
  47. }
  48. }
  49. } else {
  50. echo "<br /><h3>".get_lang('Id')." #$id</h3>";
  51. //process data
  52. foreach ($item as $key => $value) {
  53. echo "$key = $value <br />";
  54. }
  55. }
  56. }
  57. } else {
  58. echo get_lang('NoResults');
  59. }
  60. }
  61. }
  62. if (isset($_REQUEST['load_ajax'])) {
  63. //Checking the variable $_SESSION['combination'] that has all the
  64. // information of the selected course (instead of using a lots of
  65. // hidden variables ... )
  66. if (isset($_SESSION['combination']) && !empty($_SESSION['combination'])) {
  67. $combinations = $_SESSION['combination'];
  68. $combination_result = $combinations[$_REQUEST['unique_id']];
  69. if (empty($combination_result)) {
  70. echo get_lang('ThereWasAnError');
  71. } else {
  72. $origin_course_code = $combination_result['course_code'];
  73. $origin_session_id = intval($combination_result['session_id']);
  74. $new_session_id = intval($_REQUEST['session_id']);
  75. $session = $em->find('ChamiloCoreBundle:Session', $new_session_id);
  76. //if (!isset($_REQUEST['view_stat'])) {
  77. if ($origin_session_id == $new_session_id) {
  78. echo get_lang('CantMoveToTheSameSession');
  79. exit;
  80. }
  81. //}
  82. $user_id = intval($_REQUEST['user_id']);
  83. $new_course_list = SessionManager::get_course_list_by_session_id($new_session_id);
  84. $course_founded = false;
  85. foreach ($new_course_list as $course_item) {
  86. if ($origin_course_code == $course_item['code']) {
  87. $course_founded = true;
  88. }
  89. }
  90. $result_message = array();
  91. $result_message_compare = array();
  92. $update_database = true;
  93. if (isset($_REQUEST['view_stat']) && $_REQUEST['view_stat'] == 1) {
  94. $update_database = false;
  95. }
  96. //Check if the same course exist in the session destination
  97. if ($course_founded) {
  98. //Check if the user is registered in the session otherwise we will add it
  99. $result = SessionManager::get_users_by_session($new_session_id);
  100. if (empty($result) || !in_array($user_id, array_keys($result))) {
  101. if ($debug) {
  102. echo 'User added to the session';
  103. }
  104. //Registering user to the new session
  105. SessionManager::subscribe_users_to_session(
  106. $new_session_id,
  107. array($user_id),
  108. false
  109. );
  110. }
  111. //Begin with the import process
  112. $course_info = api_get_course_info($origin_course_code);
  113. $course_id = $course_info['real_id'];
  114. $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  115. $TBL_TRACK_ATTEMPT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  116. $TBL_TRACK_E_COURSE_ACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  117. $TBL_TRACK_E_LAST_ACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  118. $TBL_LP_VIEW = Database::get_course_table(TABLE_LP_VIEW);
  119. $TBL_NOTEBOOK = Database::get_course_table(TABLE_NOTEBOOK);
  120. $TBL_STUDENT_PUBLICATION = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  121. $TBL_STUDENT_PUBLICATION_ASSIGNMENT = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  122. $TBL_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  123. $TBL_DROPBOX_FILE = Database::get_course_table(TABLE_DROPBOX_FILE);
  124. $TBL_DROPBOX_POST = Database::get_course_table(TABLE_DROPBOX_POST);
  125. $TBL_AGENDA = Database::get_course_table(TABLE_AGENDA);
  126. //1. track_e_exercises
  127. //ORIGINAL COURSE
  128. $sql = "SELECT * FROM $TABLETRACK_EXERCICES
  129. WHERE c_id = $course_id AND session_id = $origin_session_id AND exe_user_id = $user_id ";
  130. $res = Database::query($sql);
  131. $list = array();
  132. while ($row = Database::fetch_array($res, 'ASSOC')) {
  133. $list[$row['exe_id']] = $row;
  134. }
  135. if (!empty($list)) {
  136. foreach ($list as $exe_id => $data) {
  137. if ($update_database) {
  138. $sql = "UPDATE $TABLETRACK_EXERCICES SET session_id = '$new_session_id' WHERE exe_id = $exe_id";
  139. $res = Database::query($sql);
  140. $result_message[$TABLETRACK_EXERCICES]++;
  141. } else {
  142. if (!empty($data['orig_lp_id']) && !empty($data['orig_lp_item_id'])) {
  143. $result_message['TRACK_E_EXERCISES'][$exe_id] = $data;
  144. } else {
  145. $result_message['TRACK_E_EXERCISES_IN_LP'][$exe_id] = $data;
  146. }
  147. }
  148. }
  149. }
  150. // DESTINY COURSE
  151. if (!$update_database) {
  152. $sql = "SELECT * FROM $TABLETRACK_EXERCICES
  153. WHERE
  154. c_id = $course_id AND
  155. session_id = $new_session_id AND
  156. exe_user_id = $user_id ";
  157. $res = Database::query($sql);
  158. $list = array();
  159. while ($row = Database::fetch_array($res, 'ASSOC')) {
  160. $list[$row['exe_id']] = $row;
  161. }
  162. if (!empty($list)) {
  163. foreach ($list as $exe_id => $data) {
  164. if ($update_database) {
  165. $sql = "UPDATE $TABLETRACK_EXERCICES
  166. SET session_id = '$new_session_id'
  167. WHERE exe_id = $exe_id";
  168. $res = Database::query($sql);
  169. $result_message[$TABLETRACK_EXERCICES]++;
  170. } else {
  171. if (!empty($data['orig_lp_id']) && !empty($data['orig_lp_item_id'])) {
  172. $result_message_compare['TRACK_E_EXERCISES'][$exe_id] = $data;
  173. } else {
  174. $result_message_compare['TRACK_E_EXERCISES_IN_LP'][$exe_id] = $data;
  175. }
  176. }
  177. }
  178. }
  179. }
  180. //2.track_e_attempt, track_e_attempt_recording, track_e_downloads
  181. //Nothing to do because there are not relationship with a session
  182. //3. track_e_course_access
  183. $sql = "SELECT * FROM $TBL_TRACK_E_COURSE_ACCESS
  184. WHERE c_id = $course_id AND session_id = $origin_session_id AND user_id = $user_id ";
  185. $res = Database::query($sql);
  186. $list = array();
  187. while ($row = Database::fetch_array($res, 'ASSOC')) {
  188. $list[$row['course_access_id']] = $row;
  189. }
  190. if (!empty($list)) {
  191. foreach ($list as $id => $data) {
  192. if ($update_database) {
  193. $sql = "UPDATE $TBL_TRACK_E_COURSE_ACCESS
  194. SET session_id = $new_session_id
  195. WHERE course_access_id = $id";
  196. if ($debug) {
  197. echo $sql;
  198. }
  199. $res = Database::query($sql);
  200. $result_message[$TBL_TRACK_E_COURSE_ACCESS]++;
  201. }
  202. }
  203. }
  204. //4. track_e_lastaccess
  205. $sql = "SELECT access_id FROM $TBL_TRACK_E_LAST_ACCESS
  206. WHERE c_id = $course_id
  207. AND access_session_id = $origin_session_id
  208. AND access_user_id = $user_id ";
  209. $res = Database::query($sql);
  210. $list = array();
  211. while ($row = Database::fetch_array($res, 'ASSOC')) {
  212. $list[] = $row['access_id'];
  213. }
  214. if (!empty($list)) {
  215. foreach ($list as $id) {
  216. if ($update_database) {
  217. $sql = "UPDATE $TBL_TRACK_E_LAST_ACCESS
  218. SET access_session_id = $new_session_id
  219. WHERE access_id = $id";
  220. if ($debug) {
  221. echo $sql;
  222. }
  223. $res = Database::query($sql);
  224. //if ($debug) var_dump($res);
  225. $result_message[$TBL_TRACK_E_LAST_ACCESS]++;
  226. }
  227. }
  228. }
  229. //5. lp_item_view
  230. //CHECK ORIGIN
  231. $sql = "SELECT * FROM $TBL_LP_VIEW
  232. WHERE user_id = $user_id AND session_id = $origin_session_id AND c_id = $course_id ";
  233. $res = Database::query($sql);
  234. //Getting the list of LPs in the new session
  235. $lp_list = new LearnpathList($user_id, $origin_course_code, $new_session_id);
  236. $flat_list = $lp_list->get_flat_list();
  237. $list = array();
  238. while ($row = Database::fetch_array($res, 'ASSOC')) {
  239. //Checking if the LP exist in the new session
  240. if (in_array($row['lp_id'], array_keys($flat_list))) {
  241. $list[$row['id']] = $row;
  242. }
  243. }
  244. if (!empty($list)) {
  245. foreach ($list as $id => $data) {
  246. if ($update_database) {
  247. $sql = "UPDATE $TBL_LP_VIEW
  248. SET session_id = $new_session_id
  249. WHERE c_id = $course_id AND id = $id ";
  250. if ($debug) {
  251. var_dump($sql);
  252. }
  253. $res = Database::query($sql);
  254. if ($debug) {
  255. var_dump($res);
  256. }
  257. $result_message[$TBL_LP_VIEW]++;
  258. } else {
  259. //Getting all information of that lp_item_id
  260. $score = Tracking::get_avg_student_score(
  261. $user_id,
  262. $origin_course_code,
  263. array($data['lp_id']),
  264. $origin_session_id
  265. );
  266. $progress = Tracking::get_avg_student_progress(
  267. $user_id,
  268. $origin_course_code,
  269. array($data['lp_id']),
  270. $origin_session_id
  271. );
  272. $result_message['LP_VIEW'][$data['lp_id']] = array(
  273. 'score' => $score,
  274. 'progress' => $progress
  275. );
  276. }
  277. }
  278. }
  279. //CHECk DESTINY
  280. if (!$update_database) {
  281. $sql = "SELECT * FROM $TBL_LP_VIEW WHERE user_id = $user_id AND session_id = $new_session_id AND c_id = $course_id";
  282. $res = Database::query($sql);
  283. //Getting the list of LPs in the new session
  284. $lp_list = new LearnpathList($user_id, $origin_course_code, $new_session_id);
  285. $flat_list = $lp_list->get_flat_list();
  286. $list = array();
  287. while ($row = Database::fetch_array($res, 'ASSOC')) {
  288. //Checking if the LP exist in the new session
  289. if (in_array($row['lp_id'], array_keys($flat_list))) {
  290. $list[$row['id']] = $row;
  291. }
  292. }
  293. if (!empty($list)) {
  294. foreach ($list as $id => $data) {
  295. //Getting all information of that lp_item_id
  296. $score = Tracking::get_avg_student_score(
  297. $user_id,
  298. $origin_course_code,
  299. array($data['lp_id']),
  300. $new_session_id
  301. );
  302. $progress = Tracking::get_avg_student_progress(
  303. $user_id,
  304. $origin_course_code,
  305. array($data['lp_id']),
  306. $new_session_id
  307. );
  308. $result_message_compare['LP_VIEW'][$data['lp_id']] = array(
  309. 'score' => $score,
  310. 'progress' => $progress
  311. );
  312. }
  313. }
  314. }
  315. //6. Agenda
  316. //calendar_event_attachment no problems no session_id
  317. $sql = "SELECT ref FROM $TBL_ITEM_PROPERTY
  318. WHERE tool = 'calendar_event' AND insert_user_id = $user_id AND c_id = $course_id ";
  319. $res = Database::query($sql);
  320. while ($row = Database::fetch_array($res, 'ASSOC')) {
  321. $id = $row['ref'];
  322. if ($update_database) {
  323. $sql = "UPDATE $TBL_AGENDA SET session_id = $new_session_id WHERE c_id = $course_id AND id = $id ";
  324. if ($debug) var_dump($sql);
  325. $res_update = Database::query($sql);
  326. if ($debug) var_dump($res_update);
  327. $result_message['agenda']++;
  328. }
  329. }
  330. //7. Forum ?? So much problems when trying to import data
  331. //8. Student publication - Works
  332. $sql = "SELECT ref FROM $TBL_ITEM_PROPERTY
  333. WHERE tool = 'work' AND insert_user_id = $user_id AND c_id = $course_id";
  334. if ($debug) echo $sql;
  335. $res = Database::query($sql);
  336. while ($row = Database::fetch_array($res, 'ASSOC')) {
  337. $id = $row['ref'];
  338. $sql = "SELECT * FROM $TBL_STUDENT_PUBLICATION WHERE id = $id AND session_id = $origin_session_id AND c_id = $course_id";
  339. if ($debug) var_dump($sql);
  340. $sub_res = Database::query($sql);
  341. if (Database::num_rows($sub_res) > 0) {
  342. $data = Database::fetch_array($sub_res, 'ASSOC');
  343. if ($debug) var_dump($data);
  344. $parent_id = $data['parent_id'];
  345. if (isset($data['parent_id']) && !empty($data['parent_id'])) {
  346. $sql = "SELECT * FROM $TBL_STUDENT_PUBLICATION
  347. WHERE id = $parent_id AND c_id = $course_id";
  348. $select_res = Database::query($sql);
  349. $parent_data = Database::fetch_array(
  350. $select_res,
  351. 'ASSOC'
  352. );
  353. if ($debug) {
  354. var_dump($parent_data);
  355. }
  356. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  357. $course_dir = $sys_course_path.$course_info['path'];
  358. $base_work_dir = $course_dir.'/work';
  359. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  360. // Creating the parent folder in the session if does not exists already
  361. //@todo ugly fix
  362. $search_this = "folder_moved_from_session_id_$origin_session_id";
  363. $search_this2 = $parent_data['url'];
  364. $sql = "SELECT * FROM $TBL_STUDENT_PUBLICATION
  365. WHERE description like '%$search_this%' AND url LIKE '%$search_this2%' AND session_id = $new_session_id AND c_id = $course_id
  366. ORDER BY id desc LIMIT 1";
  367. if ($debug) echo $sql;
  368. $sub_res = Database::query($sql);
  369. $num_rows = Database::num_rows($sub_res);
  370. if ($num_rows > 0) {
  371. $new_result = Database::fetch_array($sub_res, 'ASSOC');
  372. $created_dir = $new_result['url'];
  373. $new_parent_id = $new_result['id'];
  374. } else {
  375. if ($update_database) {
  376. $dir_name = substr($parent_data['url'], 1);
  377. $created_dir = create_unexisting_work_directory($base_work_dir, $dir_name);
  378. $created_dir = '/'.$created_dir;
  379. $now = new DateTime(api_get_utc_datetime(), new DateTimeZone('UTC'));
  380. //Creating directory
  381. $publication = new \Chamilo\CourseBundle\Entity\CStudentPublication();
  382. $publication
  383. ->setUrl($created_dir)
  384. ->setCId($course_id)
  385. ->setTitle($parent_data['title'])
  386. ->setDescription(
  387. $parent_data['description']."folder_moved_from_session_id_$origin_session_id"
  388. )
  389. ->setActive(false)
  390. ->setAccepted(true)
  391. ->setFiletype('folder')
  392. ->setSentDate($now)
  393. ->setQualification($parent_data['qualification'])
  394. ->setParentId(0)
  395. ->setQualificatorId(0)
  396. ->setSession($session);
  397. $id = $publication->getIid();
  398. //Folder created
  399. api_item_property_update($course_info, 'work', $id, 'DirectoryCreated', api_get_user_id());
  400. $new_parent_id = $id;
  401. $result_message[$TBL_STUDENT_PUBLICATION.' - new folder created called: '.$created_dir]++;
  402. }
  403. }
  404. //Creating student_publication_assignment if exists
  405. $sql = "SELECT * FROM $TBL_STUDENT_PUBLICATION_ASSIGNMENT WHERE publication_id = $parent_id AND c_id = $course_id";
  406. if ($debug) var_dump($sql);
  407. $rest_select = Database::query($sql);
  408. if (Database::num_rows($rest_select) > 0) {
  409. if ($update_database) {
  410. $assignment_data = Database::fetch_array($rest_select, 'ASSOC');
  411. $sql_add_publication = "INSERT INTO ".$TBL_STUDENT_PUBLICATION_ASSIGNMENT." SET
  412. c_id = '$course_id',
  413. expires_on = '".$assignment_data['expires_on']."',
  414. ends_on = '".$assignment_data['ends_on']."',
  415. add_to_calendar = '".$assignment_data['add_to_calendar']."',
  416. enable_qualification = '".$assignment_data['enable_qualification']."',
  417. publication_id = '".$new_parent_id."'";
  418. if ($debug) echo $sql_add_publication;
  419. $rest_select = Database::query($sql_add_publication);
  420. $id = Database::insert_id();
  421. $sql_update = "UPDATE ".$TBL_STUDENT_PUBLICATION." SET ".
  422. "has_properties = '".$id."',
  423. view_properties = '1'
  424. WHERE id = ".$new_parent_id;
  425. if ($debug) echo $sql_update;
  426. $rest_update = Database::query($sql_update);
  427. if ($debug) var_dump($sql_update);
  428. $result_message[$TBL_STUDENT_PUBLICATION_ASSIGNMENT]++;
  429. }
  430. }
  431. $doc_url = $data['url'];
  432. $new_url = str_replace($parent_data['url'], $created_dir, $doc_url);
  433. if ($update_database) {
  434. //Creating a new work
  435. $data['sent_date'] = new DateTime($data['sent_date'], new DateTimeZone('UTC'));
  436. $publication = new \Chamilo\CourseBundle\Entity\CStudentPublication();
  437. $publication
  438. ->setUrl($new_url)
  439. ->setCId($course_id)
  440. ->setTitle($data['title'])
  441. ->setDescription($data['description'].' file moved')
  442. ->setActive($data['active'])
  443. ->setAccepted($data['accepted'])
  444. ->setPostGroupId($data['post_group_id'])
  445. ->setSentDate($data['sent_date'])
  446. ->setParentId($new_parent_id)
  447. ->setSession($session);
  448. $em->persist($publication);
  449. $em->flush();
  450. $id = $publication->getIid();
  451. api_item_property_update($course_info, 'work', $id, 'DocumentAdded', $user_id);
  452. $result_message[$TBL_STUDENT_PUBLICATION]++;
  453. $full_file_name = $course_dir.'/'.$doc_url;
  454. $new_file = $course_dir.'/'.$new_url;
  455. if (file_exists($full_file_name)) {
  456. //deleting old assignment
  457. $result = copy($full_file_name, $new_file);
  458. if ($result) {
  459. unlink($full_file_name);
  460. $sql = "DELETE FROM $TBL_STUDENT_PUBLICATION WHERE id= ".$data['id'];
  461. if ($debug) var_dump($sql);
  462. $result_delete = Database::query($sql);
  463. api_item_property_update($course_info, 'work', $data['id'], 'DocumentDeleted', api_get_user_id());
  464. }
  465. }
  466. }
  467. }
  468. }
  469. }
  470. //9. Survey Pending
  471. //10. Dropbox - not neccesary to move categories (no presence of session_id)
  472. $sql = "SELECT id FROM $TBL_DROPBOX_FILE
  473. WHERE uploader_id = $user_id AND session_id = $origin_session_id AND c_id = $course_id";
  474. if ($debug) var_dump($sql);
  475. $res = Database::query($sql);
  476. while ($row = Database::fetch_array($res, 'ASSOC')) {
  477. $id = $row['id'];
  478. if ($update_database) {
  479. $sql = "UPDATE $TBL_DROPBOX_FILE SET session_id = $new_session_id WHERE c_id = $course_id AND id = $id";
  480. if ($debug) var_dump($sql);
  481. $res = Database::query($sql);
  482. if ($debug) var_dump($res);
  483. $sql = "UPDATE $TBL_DROPBOX_POST SET session_id = $new_session_id WHERE file_id = $id";
  484. if ($debug)
  485. var_dump($sql);
  486. $res = Database::query($sql);
  487. if ($debug)
  488. var_dump($res);
  489. $result_message[$TBL_DROPBOX_FILE]++;
  490. }
  491. }
  492. //11. Notebook
  493. $sql = "SELECT notebook_id FROM $TBL_NOTEBOOK
  494. WHERE user_id = $user_id AND session_id = $origin_session_id AND course = '$origin_course_code' AND c_id = $course_id";
  495. if ($debug) var_dump($sql);
  496. $res = Database::query($sql);
  497. while ($row = Database::fetch_array($res, 'ASSOC')) {
  498. $id = $row['notebook_id'];
  499. if ($update_database) {
  500. $sql = "UPDATE $TBL_NOTEBOOK
  501. SET session_id = $new_session_id
  502. WHERE c_id = $course_id AND notebook_id = $id";
  503. if ($debug) var_dump($sql);
  504. $res = Database::query($sql);
  505. if ($debug) var_dump($res);
  506. }
  507. }
  508. if ($update_database) {
  509. echo '<h2>'.get_lang('StatsMoved').'</h2>';
  510. if (is_array($result_message))
  511. foreach ($result_message as $table => $times) {
  512. echo 'Table '.$table.' - '.$times.' records updated <br />';
  513. }
  514. } else {
  515. echo '<h2>'.get_lang('UserInformationOfThisCourse').'</h2>';
  516. echo '<br />';
  517. echo '<table width="100%">';
  518. echo '<tr>';
  519. echo '<td width="50%" valign="top">';
  520. if ($origin_session_id == 0) {
  521. echo '<h4>'.get_lang('OriginCourse').'</h4>';
  522. } else {
  523. echo '<h4>'.get_lang('OriginSession').' #'.$origin_session_id.'</h4>';
  524. }
  525. compare_data($result_message);
  526. echo '</td>';
  527. echo '<td width="50%" valign="top">';
  528. if ($new_session_id == 0) {
  529. echo '<h4>'.get_lang('DestinyCourse').'</h4>';
  530. } else {
  531. echo '<h4>'.get_lang('DestinySession').' #'.$new_session_id.'</h4>';
  532. }
  533. compare_data($result_message_compare);
  534. echo '</td>';
  535. echo '</tr>';
  536. echo '</table>';
  537. }
  538. } else {
  539. echo get_lang('CourseDoesNotExistInThisSession');
  540. }
  541. }
  542. } else {
  543. echo get_lang('ThereWasAnError');
  544. }
  545. exit;
  546. }
  547. $htmlHeadXtra[] = '<script>
  548. function moveto (unique_id, user_id) {
  549. var session_id = document.getElementById(unique_id).options[document.getElementById(unique_id).selectedIndex].value;
  550. $.ajax({
  551. contentType: "application/x-www-form-urlencoded",
  552. beforeSend: function(objeto) {
  553. $("div#reponse_"+unique_id).html("<img src=\''.Display::return_icon('indicator.gif', '', [], ICON_SIZE_SMALL, true, true).'\' />"); },
  554. type: "POST",
  555. url: "user_move_stats.php",
  556. data: "load_ajax=1"+"&unique_id="+unique_id+"&user_id="+user_id+"&session_id="+session_id,
  557. success: function(datos) {
  558. $("div#reponse_"+unique_id).html(datos);
  559. }
  560. });
  561. }
  562. function view_stat (unique_id, user_id) {
  563. var session_id = document.getElementById(unique_id).options[document.getElementById(unique_id).selectedIndex].value;
  564. $.ajax({
  565. contentType: "application/x-www-form-urlencoded",
  566. beforeSend: function(objeto) {
  567. $("div#reponse_"+unique_id).html("<img src=\''.Display::return_icon('indicator.gif', '', [], ICON_SIZE_SMALL, true, true).'\' />"); },
  568. type: "POST",
  569. url: "user_move_stats.php",
  570. data: "load_ajax=1&view_stat=1"+"&unique_id="+unique_id+"&user_id="+user_id+"&session_id="+session_id,
  571. success: function(datos) {
  572. $("div#reponse_"+unique_id).html(datos);
  573. }
  574. });
  575. }
  576. </script>';
  577. function get_courses_list_by_user_id_based_in_exercises($user_id)
  578. {
  579. $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  580. $user_id = intval($user_id);
  581. $sql = "SELECT DISTINCT exe_user_id, c_id, session_id
  582. FROM $TABLETRACK_EXERCICES
  583. WHERE exe_user_id = $user_id
  584. ORDER by exe_user_id, c_id ASC";
  585. $res = Database::query($sql);
  586. $course_list = array();
  587. while ($row = Database::fetch_array($res, 'ASSOC')) {
  588. $course_list [] = $row;
  589. }
  590. return $course_list;
  591. }
  592. Display::addFlash(
  593. Display::return_message(
  594. get_lang('CompareUserResultsBetweenCoursesAndCoursesInASession'),
  595. 'normal',
  596. false
  597. )
  598. );
  599. Display::display_header(get_lang('MoveUserStats'));
  600. echo '<div class="actions">';
  601. echo '<a href="../admin/index.php">'.Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('PlatformAdmin'), '', ICON_SIZE_MEDIUM).'</a>';
  602. echo '</div>';
  603. // Some pagination
  604. $page = 1;
  605. if (isset($_GET['page']) && !empty($_GET['page'])) {
  606. $page = intval($_GET['page']);
  607. }
  608. $default = 20;
  609. $count = UserManager::get_number_of_users();
  610. $nro_pages = round($count / $default) + 1;
  611. $begin = $default * ($page - 1);
  612. $end = $default * $page;
  613. $navigation = "$begin - $end / $count<br />";
  614. if ($page > 1) {
  615. $navigation .= '<a href="'.api_get_self().'?page='.($page - 1).'">'.get_lang('Previous').'</a>';
  616. } else {
  617. $navigation .= get_lang('Previous');
  618. }
  619. $navigation .= '&nbsp;';
  620. $page++;
  621. if ($page < $nro_pages) {
  622. $navigation .= '<a href="'.api_get_self().'?page='.$page.'">'.get_lang('Next').'</a>';
  623. } else {
  624. $navigation .= get_lang('Next');
  625. }
  626. echo $navigation;
  627. $user_list = UserManager::get_user_list(array(), array(), $begin, $default);
  628. $session_list = SessionManager::get_sessions_list(array(), array('name'));
  629. $options = '';
  630. $options .= '<option value="0">--'.get_lang('SelectASession').'--</option>';
  631. foreach ($session_list as $session_data) {
  632. $my_session_list[$session_data['id']] = $session_data['name'];
  633. $options .= '<option value="'.$session_data['id'].'">'.$session_data['name'].'</option>';
  634. }
  635. $combinations = array();
  636. if (!empty($user_list)) {
  637. foreach ($user_list as $user) {
  638. $user_id = $user['user_id'];
  639. $name = $user['firstname'].' '.$user['lastname'];
  640. $course_list_registered = CourseManager::get_courses_list_by_user_id(
  641. $user_id,
  642. true,
  643. false
  644. );
  645. $new_course_list = array();
  646. foreach ($course_list_registered as $course_reg) {
  647. if (empty($course_reg['session_id'])) {
  648. $course_reg['session_id'] = 0;
  649. }
  650. // Recover the code for historical reasons. If it can be proven
  651. // that the code can be safely replaced by c_id in the following
  652. // PHP code, feel free to do so
  653. $courseInfo = api_get_course_info_by_id($course_reg['real_id']);
  654. $course_reg['code'] = $courseInfo['code'];
  655. $new_course_list[] = $course_reg['code'].'_'.$course_reg['session_id'];
  656. }
  657. $course_list = get_courses_list_by_user_id_based_in_exercises($user_id);
  658. if (is_array($course_list) && !empty($course_list)) {
  659. foreach ($course_list as $my_course) {
  660. $courseInfo = api_get_course_info_by_id($my_course['c_id']);
  661. $my_course['real_id'] = $my_course['c_id'];
  662. $key = $courseInfo['code'].'_'.$my_course['session_id'];
  663. if (!in_array($key, $new_course_list)) {
  664. $my_course['not_registered'] = 1;
  665. $course_list_registered[] = $my_course;
  666. }
  667. }
  668. }
  669. foreach ($course_list_registered as & $course) {
  670. $courseInfo = api_get_course_info_by_id($course['real_id']);
  671. $course['name'] = $courseInfo['name'];
  672. }
  673. $course_list = $course_list_registered;
  674. echo '<div>';
  675. echo '<table class="data_table">';
  676. echo '<tr>';
  677. echo '<th style="text-align:left;" colspan="'.count($course_list).'">';
  678. echo "<h3>$name #$user_id </h3> ";
  679. echo '</th>';
  680. echo '</tr>';
  681. if (!empty($course_list)) {
  682. echo '<tr>';
  683. foreach ($course_list as $course) {
  684. echo '<td>';
  685. if (isset($course['id_session']) && !empty($course['id_session'])) {
  686. echo '<b>'.get_lang('SessionName').'</b> '.$my_session_list[$course['id_session']].'<br />';
  687. }
  688. echo $course['name'];
  689. echo ' ('.$course['code'].') ';
  690. if (isset($course['not_registered']) && !empty($course['not_registered'])) {
  691. echo ' <i>'.get_lang('UserNotRegistered').'</i>';
  692. }
  693. echo '</td>';
  694. }
  695. echo '</tr>';
  696. echo '<tr>';
  697. foreach ($course_list as $course) {
  698. $course_code = $course['code'];
  699. if (empty($course['id_session'])) {
  700. $session_id = 0;
  701. } else {
  702. $session_id = $course['id_session'];
  703. }
  704. echo '<td>';
  705. echo get_lang('MoveTo'); echo '<br />';
  706. $unique_id = uniqid();
  707. $combinations[$unique_id] = array('course_code' =>$course_code, 'session_id'=>$session_id);
  708. echo '<select id="'.$unique_id.'" name="'.$unique_id.'">';
  709. echo $options;
  710. echo '</select>';
  711. echo '<br />';
  712. echo '<button type="submit" class="btn btn-success" onclick="view_stat(\''.$unique_id.'\', \''.$user_id.'\');"> '.get_lang('CompareStats').'</button>';
  713. echo '<button type="submit" class="btn btn-success" onclick="moveto(\''.$unique_id.'\', \''.$user_id.'\');"> '.get_lang('Move').'</button>';
  714. echo '<div id ="reponse_'.$unique_id.'"></div>';
  715. echo '</td>';
  716. }
  717. echo '</tr>';
  718. } else {
  719. echo '<td>';
  720. echo get_lang('NoCoursesForThisUser');
  721. echo '</td>';
  722. }
  723. echo '</table>';
  724. echo '</div>';
  725. }
  726. }
  727. echo $navigation;
  728. $_SESSION['combination'] = $combinations;