user_move_stats.php 39 KB

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